OmniSciDB  72c90bc290
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GeoIR.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "Geospatial/Compression.h"
19 #include "QueryEngine/Execute.h"
22 
24  llvm::Value* pos,
25  const SQLTypeInfo& ti,
26  CgenState* cgen_state) {
27  CHECK(byte_stream);
28 
29  const auto key = std::make_pair(byte_stream, pos);
30  auto cache_itr = cgen_state->array_load_cache_.find(key);
31  if (cache_itr != cgen_state->array_load_cache_.end()) {
32  return cache_itr->second;
33  }
34  const bool is_nullable = !ti.get_notnull();
35  CHECK(ti.get_type() == kPOINT); // TODO: lift this
36 
37  auto pt_arr_buf =
38  cgen_state->emitExternalCall("array_buff",
39  llvm::Type::getInt8PtrTy(cgen_state->context_),
40  {key.first, key.second});
41  llvm::Value* pt_is_null{nullptr};
42  if (is_nullable) {
43  pt_is_null = cgen_state->emitExternalCall("point_coord_array_is_null",
44  llvm::Type::getInt1Ty(cgen_state->context_),
45  {key.first, key.second});
46  }
47  ArrayLoadCodegen arr_load{pt_arr_buf, nullptr, pt_is_null};
48  cgen_state->array_load_cache_.insert(std::make_pair(key, arr_load));
49  return arr_load;
50 }
51 
52 std::vector<llvm::Value*> CodeGenerator::codegenGeoColumnVar(
53  const Analyzer::GeoColumnVar* geo_col_var,
54  const bool fetch_columns,
55  const CompilationOptions& co) {
56  auto generate_column_lvs = [this, geo_col_var, &co](const int column_id) {
57  auto column_key = geo_col_var->getColumnKey();
58  column_key.column_id = column_id;
59 
60  auto cd = get_column_descriptor(column_key);
61  CHECK(cd);
62 
63  const auto col_var =
64  Analyzer::ColumnVar(cd->columnType, column_key, geo_col_var->get_rte_idx());
65  const auto lv_vec = codegen(&col_var, /*fetch_columns=*/true, co);
66  CHECK_EQ(lv_vec.size(), size_t(1)); // ptr
67  return lv_vec;
68  };
69 
70  const auto& ti = geo_col_var->get_type_info();
71  switch (ti.get_type()) {
72  case kPOINT:
73  case kLINESTRING:
74  case kMULTILINESTRING:
75  case kPOLYGON:
76  case kMULTIPOLYGON: {
77  std::vector<llvm::Value*> geo_lvs;
78  // iterate over physical columns
79  for (int i = 0; i < ti.get_physical_coord_cols(); i++) {
80  const auto column_id = geo_col_var->getColumnKey().column_id + 1 + i;
81  const auto lvs = generate_column_lvs(column_id);
82  CHECK_EQ(lvs.size(), size_t(1)); // expecting ptr for each column
83  geo_lvs.insert(geo_lvs.end(), lvs.begin(), lvs.end());
84  }
85 
86  return geo_lvs;
87  }
88  default:
89  UNREACHABLE() << geo_col_var->toString();
90  }
91  UNREACHABLE();
92  return {};
93 }
94 
95 std::vector<llvm::Value*> CodeGenerator::codegenGeoExpr(const Analyzer::GeoExpr* expr,
96  const CompilationOptions& co) {
97  auto geo_constant = dynamic_cast<const Analyzer::GeoConstant*>(expr);
98  if (geo_constant) {
99  return codegenGeoConstant(geo_constant, co);
100  }
101  auto geo_operator = dynamic_cast<const Analyzer::GeoOperator*>(expr);
102  if (geo_operator) {
103  return codegenGeoOperator(geo_operator, co);
104  }
105  UNREACHABLE() << expr->toString();
106  return {};
107 }
108 
109 std::vector<llvm::Value*> CodeGenerator::codegenGeoConstant(
110  const Analyzer::GeoConstant* geo_constant,
111  const CompilationOptions& co) {
113 
114  std::vector<llvm::Value*> ret;
115  for (size_t i = 0; i < geo_constant->physicalCols(); i++) {
116  auto physical_constant = geo_constant->makePhysicalConstant(i);
117  auto operand_lvs = codegen(physical_constant.get(), /*fetch_columns=*/true, co);
118  CHECK_EQ(operand_lvs.size(), size_t(2));
119  auto array_buff_lv = operand_lvs[0];
120  if (i > 0) {
121  array_buff_lv = cgen_state_->ir_builder_.CreateBitCast(
122  operand_lvs[0], llvm::Type::getInt8PtrTy(cgen_state_->context_));
123  }
124  ret.push_back(array_buff_lv);
125  ret.push_back(operand_lvs[1]);
126  }
127  return ret;
128 }
129 
130 namespace {
132  PlanState* plan_state) {
133  auto comparator = Analyzer::ColumnVar::colvar_comp;
134  std::set<const Analyzer::ColumnVar*,
135  bool (*)(const Analyzer::ColumnVar*, const Analyzer::ColumnVar*)>
136  colvar_set(comparator);
137  geo_operator->collect_column_var(colvar_set, false);
138  std::for_each(colvar_set.begin(), colvar_set.end(), [&](const Analyzer::ColumnVar* cv) {
139  // we set `unmark_lazy_fetch` to be FALSE to restart the compilation step
140  // if we already generated a code for the expression used in this geo_oper as
141  // lazy-fetch
142  plan_state->addColumnToFetch(cv->getColumnKey(), false);
143  });
144 }
145 } // namespace
146 
147 std::vector<llvm::Value*> CodeGenerator::codegenGeoOperator(
148  const Analyzer::GeoOperator* geo_operator,
149  const CompilationOptions& co) {
151 
152  if (geo_operator->getName() == "ST_X" || geo_operator->getName() == "ST_Y") {
153  const auto key = geo_operator->toString();
154  auto geo_target_cache_it = cgen_state_->geo_target_cache_.find(key);
155  if (geo_target_cache_it != cgen_state_->geo_target_cache_.end()) {
156  return {geo_target_cache_it->second};
157  }
158  }
159 
160  auto op_codegen = spatial_type::Codegen::init(geo_operator);
161  CHECK(op_codegen);
162  // we fetch all physical columns, so we sync the logical geo column w/ it
164 
165  std::vector<llvm::Value*> load_lvs;
166  std::vector<llvm::Value*> pos_lvs;
167  for (size_t i = 0; i < op_codegen->size(); i++) {
168  auto intermediate_lvs =
169  codegen(op_codegen->getOperand(i), /*fetch_columns=*/true, co);
170  load_lvs.insert(load_lvs.end(), intermediate_lvs.begin(), intermediate_lvs.end());
171  pos_lvs.push_back(posArg(op_codegen->getOperand(i)));
172  }
173 
174  auto [arg_lvs, null_lv] = op_codegen->codegenLoads(load_lvs, pos_lvs, cgen_state_);
175 
176  std::unique_ptr<CodeGenerator::NullCheckCodegen> nullcheck_codegen =
177  op_codegen->getNullCheckCodegen(null_lv, cgen_state_, executor());
178  return op_codegen->codegen(arg_lvs, nullcheck_codegen.get(), cgen_state_, co);
179 }
180 
181 std::vector<llvm::Value*> CodeGenerator::codegenGeoUOper(
182  const Analyzer::GeoUOper* geo_expr,
183  const CompilationOptions& co) {
186  if (geo_expr->getOp() != Geospatial::GeoBase::GeoOp::kPROJECTION) {
187  throw QueryMustRunOnCpu();
188  }
189  }
190 
191  auto argument_list = codegenGeoArgs(geo_expr->getArgs0(), co);
192 
193  if (geo_expr->getOp() == Geospatial::GeoBase::GeoOp::kPROJECTION) {
194  return argument_list;
195  }
196 
197 #ifndef ENABLE_GEOS
198  throw std::runtime_error("Geo operation requires GEOS support.");
199 #endif
200 
201  // Basic set of arguments is currently common to all Geos_* func invocations:
202  // op kind, type of the first geo arg0, geo arg0 components
203  std::string func = "Geos_Wkb"s;
204 
205  if (geo_expr->getTypeInfo0().transforms() || geo_expr->get_type_info().transforms()) {
206  // If there is a transform on the argument and/or on the result of the operation,
207  // verify that the argument's output srid is equal to result's input srid
208  if (geo_expr->getTypeInfo0().get_output_srid() !=
209  geo_expr->get_type_info().get_input_srid()) {
210  throw std::runtime_error("GEOS runtime: input/output srids have to match.");
211  }
212  }
213  // Prepend arg0 geo SQLType
214  argument_list.insert(
215  argument_list.begin(),
216  cgen_state_->llInt(static_cast<int>(geo_expr->getTypeInfo0().get_type())));
217  // Prepend geo expr op
218  argument_list.insert(argument_list.begin(),
219  cgen_state_->llInt(static_cast<int>(geo_expr->getOp())));
220  for (auto i = 3; i > geo_expr->getTypeInfo0().get_physical_coord_cols(); i--) {
221  argument_list.insert(argument_list.end(),
222  llvm::ConstantPointerNull::get(
223  llvm::Type::getInt32PtrTy(cgen_state_->context_, 0)));
224  argument_list.insert(argument_list.end(), cgen_state_->llInt(int64_t(0)));
225  }
226  // Append geo expr compression
227  argument_list.insert(
228  argument_list.end(),
229  cgen_state_->llInt(static_cast<int>(
231  // Append geo expr input srid
232  argument_list.insert(
233  argument_list.end(),
234  cgen_state_->llInt(static_cast<int>(geo_expr->getTypeInfo0().get_input_srid())));
235  // Append geo expr output srid
236  argument_list.insert(
237  argument_list.end(),
238  cgen_state_->llInt(static_cast<int>(geo_expr->getTypeInfo0().get_output_srid())));
239 
240  // Deal with unary geo predicates
241  if (geo_expr->getOp() == Geospatial::GeoBase::GeoOp::kISEMPTY ||
243  return codegenGeosPredicateCall(func, argument_list, co);
244  }
245 
246  if (geo_expr->getOp() == Geospatial::GeoBase::GeoOp::kCONVEXHULL) {
247  func += "_double"s; // Use same interface as ST_ConcaveHull, with a dummy double
248 
249  // Insert that dummy double arg
250  argument_list.insert(argument_list.end(), cgen_state_->llFp(double(0)));
251 
252  auto result_srid = cgen_state_->llInt(geo_expr->get_type_info().get_output_srid());
253 
254  return codegenGeosConstructorCall(func, argument_list, result_srid, co);
255  }
256 
257  throw std::runtime_error("Unsupported unary geo operation.");
258  return {};
259 }
260 
261 std::vector<llvm::Value*> CodeGenerator::codegenGeoBinOper(
262  Analyzer::GeoBinOper const* geo_expr,
263  CompilationOptions const& co) {
266  throw QueryMustRunOnCpu();
267  }
268 #ifndef ENABLE_GEOS
269  throw std::runtime_error("Geo operation requires GEOS support.");
270 #endif
271 
272  auto argument_list = codegenGeoArgs(geo_expr->getArgs0(), co);
273 
274  // Basic set of arguments is currently common to all Geos_* func invocations:
275  // op kind, type of the first geo arg0, geo arg0 components
276  std::string func = "Geos_Wkb"s;
277  if (geo_expr->getTypeInfo0().transforms() || geo_expr->get_type_info().transforms()) {
278  // If there is a transform on the argument and/or on the result of the operation,
279  // verify that the argument's output srid is equal to result's input srid
280  if (geo_expr->getTypeInfo0().get_output_srid() !=
281  geo_expr->get_type_info().get_input_srid()) {
282  throw std::runtime_error("GEOS runtime: input/output srids have to match.");
283  }
284  }
285  // Prepend arg0 geo SQLType
286  argument_list.insert(
287  argument_list.begin(),
288  cgen_state_->llInt(static_cast<int>(geo_expr->getTypeInfo0().get_type())));
289  // Prepend geo expr op
290  argument_list.insert(argument_list.begin(),
291  cgen_state_->llInt(static_cast<int>(geo_expr->getOp())));
292  for (auto i = 3; i > geo_expr->getTypeInfo0().get_physical_coord_cols(); i--) {
293  argument_list.insert(argument_list.end(),
294  llvm::ConstantPointerNull::get(
295  llvm::Type::getInt32PtrTy(cgen_state_->context_, 0)));
296  argument_list.insert(argument_list.end(), cgen_state_->llInt(int64_t(0)));
297  }
298  // Append geo expr compression
299  argument_list.insert(
300  argument_list.end(),
301  cgen_state_->llInt(static_cast<int>(
303  // Append geo expr input srid
304  argument_list.insert(
305  argument_list.end(),
306  cgen_state_->llInt(static_cast<int>(geo_expr->getTypeInfo0().get_input_srid())));
307  // Append geo expr output srid
308  argument_list.insert(
309  argument_list.end(),
310  cgen_state_->llInt(static_cast<int>(geo_expr->getTypeInfo0().get_output_srid())));
311 
312  auto arg1_list = codegenGeoArgs(geo_expr->getArgs1(), co);
313 
314  if (geo_expr->getOp() == Geospatial::GeoBase::GeoOp::kDIFFERENCE ||
316  geo_expr->getOp() == Geospatial::GeoBase::GeoOp::kUNION ||
318  func += "_Wkb"s;
319  if (geo_expr->getOp() == Geospatial::GeoBase::GeoOp::kEQUALS) {
320  func += "_Predicate"s;
321  }
322  // Prepend arg1 geo SQLType
323  arg1_list.insert(
324  arg1_list.begin(),
325  cgen_state_->llInt(static_cast<int>(geo_expr->getTypeInfo1().get_type())));
326  for (auto i = 3; i > geo_expr->getTypeInfo1().get_physical_coord_cols(); i--) {
327  arg1_list.insert(arg1_list.end(),
328  llvm::ConstantPointerNull::get(
329  llvm::Type::getInt32PtrTy(cgen_state_->context_, 0)));
330  arg1_list.insert(arg1_list.end(), cgen_state_->llInt(int64_t(0)));
331  }
332  // Append geo expr compression
333  arg1_list.insert(arg1_list.end(),
334  cgen_state_->llInt(static_cast<int>(
336  // Append geo expr input srid
337  arg1_list.insert(arg1_list.end(),
338  cgen_state_->llInt(geo_expr->getTypeInfo1().get_input_srid()));
339  // Append geo expr output srid
340  arg1_list.insert(arg1_list.end(),
342  } else if (geo_expr->getOp() == Geospatial::GeoBase::GeoOp::kBUFFER) {
343  // Extra argument in this case is double
344  func += "_double"s;
345  } else if (geo_expr->getOp() == Geospatial::GeoBase::GeoOp::kCONCAVEHULL) {
346 #if (GEOS_VERSION_MAJOR > 3) || (GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR >= 11)
347  // Extra argument in this case is double
348  func += "_double"s;
349 #else
350  throw std::runtime_error("ST_ConcaveHull requires GEOS 3.11 or newer");
351 #endif
352  } else {
353  throw std::runtime_error("Unsupported binary geo operation.");
354  }
355 
356  // Append arg1 to the list
357  argument_list.insert(argument_list.end(), arg1_list.begin(), arg1_list.end());
358 
359  // Deal with binary geo predicates
360  if (geo_expr->getOp() == Geospatial::GeoBase::GeoOp::kEQUALS) {
361  return codegenGeosPredicateCall(func, argument_list, co);
362  }
363 
364  auto result_srid = cgen_state_->llInt(geo_expr->get_type_info().get_output_srid());
365 
366  return codegenGeosConstructorCall(func, argument_list, result_srid, co);
367 }
368 
369 std::vector<llvm::Value*> CodeGenerator::codegenGeoArgs(
370  const std::vector<std::shared_ptr<Analyzer::Expr>>& geo_args,
371  const CompilationOptions& co) {
373  std::vector<llvm::Value*> argument_list;
374  bool coord_col = true;
375  for (const auto& geo_arg : geo_args) {
376  const auto arg = geo_arg.get();
377  const auto& arg_ti = arg->get_type_info();
378  const auto elem_ti = arg_ti.get_elem_type();
379  const auto arg_lvs = codegen(arg, true, co);
380  if (arg_ti.is_number()) {
381  argument_list.emplace_back(arg_lvs.front());
382  continue;
383  }
384  if (arg_ti.is_geometry()) {
385  argument_list.insert(argument_list.end(), arg_lvs.begin(), arg_lvs.end());
386  continue;
387  }
388  CHECK(arg_ti.is_array());
389  if (arg_lvs.size() > 1) {
390  CHECK_EQ(size_t(2), arg_lvs.size());
391  auto ptr_lv = arg_lvs.front();
392  if (coord_col) {
393  coord_col = false;
394  } else {
395  ptr_lv = cgen_state_->ir_builder_.CreatePointerCast(
396  ptr_lv, llvm::Type::getInt32PtrTy(cgen_state_->context_));
397  }
398  argument_list.emplace_back(ptr_lv);
399  auto cast_len_lv = cgen_state_->ir_builder_.CreateZExt(
400  arg_lvs.back(), get_int_type(64, cgen_state_->context_));
401  argument_list.emplace_back(cast_len_lv);
402  } else {
403  CHECK_EQ(size_t(1), arg_lvs.size());
404  if (arg_ti.get_size() > 0) {
405  // Set up the pointer lv for a dynamically generated point
406  auto ptr_lv = arg_lvs.front();
407  auto col_var = dynamic_cast<const Analyzer::ColumnVar*>(arg);
408  // Override for point coord column access
409  if (col_var) {
410  ptr_lv = cgen_state_->emitExternalCall(
411  "fast_fixlen_array_buff",
412  llvm::Type::getInt8PtrTy(cgen_state_->context_),
413  {arg_lvs.front(), posArg(arg)});
414  }
415  if (coord_col) {
416  coord_col = false;
417  } else {
418  ptr_lv = cgen_state_->ir_builder_.CreatePointerCast(
419  ptr_lv, llvm::Type::getInt32PtrTy(cgen_state_->context_));
420  }
421  argument_list.emplace_back(ptr_lv);
422  argument_list.emplace_back(cgen_state_->llInt<int64_t>(arg_ti.get_size()));
423  } else {
424  auto ptr_lv =
425  cgen_state_->emitExternalCall("array_buff",
426  llvm::Type::getInt8PtrTy(cgen_state_->context_),
427  {arg_lvs.front(), posArg(arg)});
428  if (coord_col) {
429  coord_col = false;
430  } else {
431  ptr_lv = cgen_state_->ir_builder_.CreatePointerCast(
432  ptr_lv, llvm::Type::getInt32PtrTy(cgen_state_->context_));
433  }
434  argument_list.emplace_back(ptr_lv);
435  const auto len_lv = cgen_state_->emitExternalCall(
436  "array_size",
438  {arg_lvs.front(),
439  posArg(arg),
440  cgen_state_->llInt(log2_bytes(elem_ti.get_logical_size()))});
441  auto cast_len_lv = cgen_state_->ir_builder_.CreateZExt(
442  len_lv, get_int_type(64, cgen_state_->context_));
443  argument_list.emplace_back(cast_len_lv);
444  }
445  }
446  }
447  return argument_list;
448 }
449 
450 std::vector<llvm::Value*> CodeGenerator::codegenGeosPredicateCall(
451  const std::string& func,
452  std::vector<llvm::Value*> argument_list,
453  const CompilationOptions& co) {
455  auto i8_type = get_int_type(8, cgen_state_->context_);
456  auto result = cgen_state_->ir_builder_.CreateAlloca(i8_type, nullptr, "result");
457  argument_list.emplace_back(result);
458 
459  // Generate call to GEOS wrapper
460  cgen_state_->needs_geos_ = true;
461  auto status_lv = cgen_state_->emitExternalCall(
462  func, llvm::Type::getInt1Ty(cgen_state_->context_), argument_list);
463  // Need to check the status and throw an error if this call has failed.
464  llvm::BasicBlock* geos_pred_ok_bb{nullptr};
465  llvm::BasicBlock* geos_pred_fail_bb{nullptr};
466  geos_pred_ok_bb = llvm::BasicBlock::Create(
467  cgen_state_->context_, "geos_pred_ok_bb", cgen_state_->current_func_);
468  geos_pred_fail_bb = llvm::BasicBlock::Create(
469  cgen_state_->context_, "geos_pred_fail_bb", cgen_state_->current_func_);
470  if (!status_lv) {
471  status_lv = cgen_state_->llBool(false);
472  }
473  cgen_state_->ir_builder_.CreateCondBr(status_lv, geos_pred_ok_bb, geos_pred_fail_bb);
474  cgen_state_->ir_builder_.SetInsertPoint(geos_pred_fail_bb);
477  cgen_state_->ir_builder_.SetInsertPoint(geos_pred_ok_bb);
478  auto res = cgen_state_->ir_builder_.CreateLoad(
479  result->getType()->getPointerElementType(), result);
480  return {res};
481 }
482 
484  const std::string& func,
485  std::vector<llvm::Value*> argument_list,
486  llvm::Value* result_srid,
487  const CompilationOptions& co) {
489  // Create output buffer pointers, append pointers to output args to
490  auto i8_type = get_int_type(8, cgen_state_->context_);
491  auto i32_type = get_int_type(32, cgen_state_->context_);
492  auto i64_type = get_int_type(64, cgen_state_->context_);
493  auto pi8_type = llvm::PointerType::get(i8_type, 0);
494  auto pi32_type = llvm::PointerType::get(i32_type, 0);
495 
496  auto result_type =
497  cgen_state_->ir_builder_.CreateAlloca(i32_type, nullptr, "result_type");
498  auto result_coords =
499  cgen_state_->ir_builder_.CreateAlloca(pi8_type, nullptr, "result_coords");
500  auto result_coords_size =
501  cgen_state_->ir_builder_.CreateAlloca(i64_type, nullptr, "result_coords_size");
502  auto result_ring_sizes =
503  cgen_state_->ir_builder_.CreateAlloca(pi32_type, nullptr, "result_ring_sizes");
504  auto result_ring_sizes_size =
505  cgen_state_->ir_builder_.CreateAlloca(i64_type, nullptr, "result_ring_sizes_size");
506  auto result_poly_rings =
507  cgen_state_->ir_builder_.CreateAlloca(pi32_type, nullptr, "result_poly_rings");
508  auto result_poly_rings_size =
509  cgen_state_->ir_builder_.CreateAlloca(i64_type, nullptr, "result_poly_rings_size");
510 
511  argument_list.emplace_back(result_type);
512  argument_list.emplace_back(result_coords);
513  argument_list.emplace_back(result_coords_size);
514  argument_list.emplace_back(result_ring_sizes);
515  argument_list.emplace_back(result_ring_sizes_size);
516  argument_list.emplace_back(result_poly_rings);
517  argument_list.emplace_back(result_poly_rings_size);
518  argument_list.emplace_back(result_srid);
519 
520  // Generate call to GEOS wrapper
521  cgen_state_->needs_geos_ = true;
522  auto status_lv = cgen_state_->emitExternalCall(
523  func, llvm::Type::getInt1Ty(cgen_state_->context_), argument_list);
524  // Need to check the status and throw an error if this call has failed.
525  llvm::BasicBlock* geos_ok_bb{nullptr};
526  llvm::BasicBlock* geos_fail_bb{nullptr};
527  geos_ok_bb = llvm::BasicBlock::Create(
528  cgen_state_->context_, "geos_ok_bb", cgen_state_->current_func_);
529  geos_fail_bb = llvm::BasicBlock::Create(
530  cgen_state_->context_, "geos_fail_bb", cgen_state_->current_func_);
531  if (!status_lv) {
532  status_lv = cgen_state_->llBool(false);
533  }
534  cgen_state_->ir_builder_.CreateCondBr(status_lv, geos_ok_bb, geos_fail_bb);
535  cgen_state_->ir_builder_.SetInsertPoint(geos_fail_bb);
538  cgen_state_->ir_builder_.SetInsertPoint(geos_ok_bb);
539 
540  // TODO: Currently forcing the output to MULTIPOLYGON, but need to handle
541  // other possible geometries that geos may return, e.g. a POINT, a LINESTRING
542  // Need to handle empty result, e.g. empty intersection.
543  // The type of result is returned in `result_type`
544 
545  // Load return values
546  auto buf1 = cgen_state_->ir_builder_.CreateLoad(
547  result_coords->getType()->getPointerElementType(), result_coords);
548  auto buf1s = cgen_state_->ir_builder_.CreateLoad(
549  result_coords_size->getType()->getPointerElementType(), result_coords_size);
550  auto buf2 = cgen_state_->ir_builder_.CreateLoad(
551  result_ring_sizes->getType()->getPointerElementType(), result_ring_sizes);
552  auto buf2s = cgen_state_->ir_builder_.CreateLoad(
553  result_ring_sizes_size->getType()->getPointerElementType(), result_ring_sizes_size);
554  auto buf3 = cgen_state_->ir_builder_.CreateLoad(
555  result_poly_rings->getType()->getPointerElementType(), result_poly_rings);
556  auto buf3s = cgen_state_->ir_builder_.CreateLoad(
557  result_poly_rings_size->getType()->getPointerElementType(), result_poly_rings_size);
558 
559  // generate register_buffer_with_executor_rsm() calls to register all output buffers
561  "register_buffer_with_executor_rsm",
562  llvm::Type::getVoidTy(cgen_state_->context_),
563  {cgen_state_->llInt(reinterpret_cast<int64_t>(executor())),
564  cgen_state_->ir_builder_.CreatePointerCast(buf1, pi8_type)});
565  cgen_state_->emitExternalCall(
566  "register_buffer_with_executor_rsm",
567  llvm::Type::getVoidTy(cgen_state_->context_),
568  {cgen_state_->llInt(reinterpret_cast<int64_t>(executor())),
569  cgen_state_->ir_builder_.CreatePointerCast(buf2, pi8_type)});
570  cgen_state_->emitExternalCall(
571  "register_buffer_with_executor_rsm",
572  llvm::Type::getVoidTy(cgen_state_->context_),
573  {cgen_state_->llInt(reinterpret_cast<int64_t>(executor())),
574  cgen_state_->ir_builder_.CreatePointerCast(buf3, pi8_type)});
575 
576  return {cgen_state_->ir_builder_.CreatePointerCast(buf1, pi8_type),
577  buf1s,
578  cgen_state_->ir_builder_.CreatePointerCast(buf2, pi32_type),
579  buf2s,
580  cgen_state_->ir_builder_.CreatePointerCast(buf3, pi32_type),
581  buf3s};
582 }
#define CHECK_EQ(x, y)
Definition: Logger.h:301
const SQLTypeInfo getTypeInfo0() const
Definition: Analyzer.h:2960
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs0() const
Definition: Analyzer.h:2962
void mark_logical_column_to_fetch(const Analyzer::GeoOperator *geo_operator, PlanState *plan_state)
Definition: GeoIR.cpp:131
static bool colvar_comp(const ColumnVar *l, const ColumnVar *r)
Definition: Analyzer.h:215
const SQLTypeInfo getTypeInfo0() const
Definition: Analyzer.h:2931
CgenState * cgen_state_
std::map< std::pair< llvm::Value *, llvm::Value * >, ArrayLoadCodegen > array_load_cache_
Definition: CgenState.h:403
void addColumnToFetch(const shared::ColumnKey &column_key, bool unmark_lazy_fetch=false)
Definition: PlanState.cpp:124
llvm::IRBuilder ir_builder_
Definition: CgenState.h:384
int32_t get_compression_scheme(const SQLTypeInfo &ti)
Definition: Compression.cpp:23
llvm::Value * posArg(const Analyzer::Expr *) const
Definition: ColumnIR.cpp:590
#define UNREACHABLE()
Definition: Logger.h:338
const SQLTypeInfo getTypeInfo1() const
Definition: Analyzer.h:2961
llvm::ConstantInt * llBool(const bool v) const
Definition: CgenState.h:263
bool needs_geos_
Definition: CgenState.h:406
static const int32_t ERR_GEOS
Definition: Execute.h:1629
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:391
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
std::vector< llvm::Value * > codegenGeoBinOper(const Analyzer::GeoBinOper *, const CompilationOptions &)
Definition: GeoIR.cpp:261
std::unordered_map< std::string, llvm::Value * > geo_target_cache_
Definition: CgenState.h:404
std::vector< llvm::Value * > codegenGeoExpr(const Analyzer::GeoExpr *, const CompilationOptions &)
Definition: GeoIR.cpp:95
llvm::LLVMContext & context_
Definition: CgenState.h:382
llvm::Function * current_func_
Definition: CgenState.h:376
llvm::Value * emitExternalCall(const std::string &fname, llvm::Type *ret_type, const std::vector< llvm::Value * > args, const std::vector< llvm::Attribute::AttrKind > &fnattrs={}, const bool has_struct_return=false)
Definition: CgenState.cpp:395
std::vector< llvm::Value * > codegenGeoOperator(const Analyzer::GeoOperator *, const CompilationOptions &)
Definition: GeoIR.cpp:147
std::vector< llvm::Value * > codegenGeoUOper(const Analyzer::GeoUOper *, const CompilationOptions &)
Definition: GeoIR.cpp:181
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs0() const
Definition: Analyzer.h:2932
std::string toString() const override
Definition: Analyzer.cpp:2717
const ColumnDescriptor * get_column_descriptor(const shared::ColumnKey &column_key)
Definition: Execute.h:213
const std::string & getName() const
Definition: Analyzer.h:3167
bool needs_error_check_
Definition: CgenState.h:405
llvm::ConstantFP * llFp(const float v) const
Definition: CgenState.h:253
#define AUTOMATIC_IR_METADATA(CGENSTATE)
std::vector< llvm::Value * > codegenGeosConstructorCall(const std::string &, std::vector< llvm::Value * >, llvm::Value *, const CompilationOptions &)
Definition: GeoIR.cpp:483
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:79
ExecutorDeviceType device_type
PlanState * plan_state_
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
virtual std::string toString() const =0
const shared::ColumnKey & getColumnKey() const
Definition: Analyzer.h:198
std::vector< llvm::Value * > codegenGeosPredicateCall(const std::string &, std::vector< llvm::Value * >, const CompilationOptions &)
Definition: GeoIR.cpp:450
static ArrayLoadCodegen codegenGeoArrayLoadAndNullcheck(llvm::Value *byte_stream, llvm::Value *pos, const SQLTypeInfo &ti, CgenState *cgen_state)
Definition: GeoIR.cpp:23
std::vector< llvm::Value * > codegenGeoColumnVar(const Analyzer::GeoColumnVar *, const bool fetch_columns, const CompilationOptions &co)
Definition: GeoIR.cpp:52
HOST DEVICE int get_input_srid() const
Definition: sqltypes.h:395
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:249
std::vector< llvm::Value * > codegenGeoArgs(const std::vector< std::shared_ptr< Analyzer::Expr >> &, const CompilationOptions &)
Definition: GeoIR.cpp:369
#define CHECK(condition)
Definition: Logger.h:291
Geospatial::GeoBase::GeoOp getOp() const
Definition: Analyzer.h:2930
std::vector< llvm::Value * > codegenGeoConstant(const Analyzer::GeoConstant *, const CompilationOptions &)
Definition: GeoIR.cpp:109
uint32_t log2_bytes(const uint32_t bytes)
Definition: Execute.h:198
std::shared_ptr< Analyzer::Constant > makePhysicalConstant(const size_t index) const
Definition: Analyzer.cpp:4064
void collect_column_var(std::set< const ColumnVar *, bool(*)(const ColumnVar *, const ColumnVar *)> &colvar_set, bool include_agg) const final
Definition: Analyzer.cpp:4145
bool transforms() const
Definition: sqltypes.h:624
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:398
std::string toString() const override
Definition: Analyzer.cpp:4154
int32_t get_rte_idx() const
Definition: Analyzer.h:202
int get_physical_coord_cols() const
Definition: sqltypes.h:449
size_t physicalCols() const
Definition: Analyzer.cpp:4059
Geospatial::GeoBase::GeoOp getOp() const
Definition: Analyzer.h:2959
static std::unique_ptr< Codegen > init(const Analyzer::GeoOperator *geo_operator)
Definition: Codegen.cpp:22
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs1() const
Definition: Analyzer.h:2963
HOST DEVICE int get_output_srid() const
Definition: sqltypes.h:397
Executor * executor() const