29 const auto key = std::make_pair(byte_stream, pos);
32 return cache_itr->second;
39 llvm::Type::getInt8PtrTy(cgen_state->
context_),
40 {key.first, key.second});
41 llvm::Value* pt_is_null{
nullptr};
43 pt_is_null = cgen_state->emitExternalCall(
"point_coord_array_is_null",
44 llvm::Type::getInt1Ty(cgen_state->context_),
45 {key.first, key.second});
48 cgen_state->array_load_cache_.insert(std::make_pair(key, arr_load));
54 const bool fetch_columns,
56 const auto catalog =
executor()->getCatalog();
59 auto generate_column_lvs = [
this, catalog, geo_col_var, &co](
const int column_id) {
64 geo_col_var->get_table_id(),
66 geo_col_var->get_rte_idx());
67 const auto lv_vec =
codegen(&col_var,
true, co);
73 switch (ti.get_type()) {
78 std::vector<llvm::Value*> geo_lvs;
80 for (
int i = 0; i < ti.get_physical_coord_cols(); i++) {
81 const auto column_id = geo_col_var->get_column_id() + 1 + i;
82 const auto lvs = generate_column_lvs(column_id);
84 geo_lvs.insert(geo_lvs.end(), lvs.begin(), lvs.end());
115 std::vector<llvm::Value*> ret;
116 for (
size_t i = 0; i < geo_constant->
physicalCols(); i++) {
118 auto operand_lvs =
codegen(physical_constant.get(),
true, co);
119 CHECK_EQ(operand_lvs.size(), size_t(2));
120 auto array_buff_lv = operand_lvs[0];
125 ret.push_back(array_buff_lv);
126 ret.push_back(operand_lvs[1]);
136 if (geo_operator->
getName() ==
"ST_X" || geo_operator->
getName() ==
"ST_Y") {
137 const auto key = geo_operator->
toString();
140 return {geo_target_cache_it->second};
144 const auto catalog =
executor()->getCatalog();
150 std::vector<llvm::Value*> load_lvs;
151 std::vector<llvm::Value*> pos_lvs;
152 for (
size_t i = 0; i < op_codegen->size(); i++) {
153 auto intermediate_lvs =
154 codegen(op_codegen->getOperand(i),
true, co);
155 load_lvs.insert(load_lvs.end(), intermediate_lvs.begin(), intermediate_lvs.end());
156 pos_lvs.push_back(
posArg(op_codegen->getOperand(i)));
159 auto [arg_lvs, null_lv] = op_codegen->codegenLoads(load_lvs, pos_lvs,
cgen_state_);
161 std::unique_ptr<CodeGenerator::NullCheckCodegen> nullcheck_codegen =
163 return op_codegen->codegen(arg_lvs, nullcheck_codegen.get(),
cgen_state_, co);
179 return argument_list;
183 throw std::runtime_error(
"Geo operation requires GEOS support.");
188 std::string func =
"Geos_Wkb"s;
195 throw std::runtime_error(
"GEOS runtime: input/output srids have to match.");
199 argument_list.insert(
200 argument_list.begin(),
203 argument_list.insert(argument_list.begin(),
207 argument_list.insert(argument_list.end(),
208 llvm::ConstantPointerNull::get(
212 argument_list.insert(
217 argument_list.insert(
221 argument_list.insert(
231 throw std::runtime_error(
"Unsupported unary geo operation.");
243 throw std::runtime_error(
"Geo operation requires GEOS support.");
250 std::string func =
"Geos_Wkb"s;
256 throw std::runtime_error(
"GEOS runtime: input/output srids have to match.");
260 argument_list.insert(
261 argument_list.begin(),
264 argument_list.insert(argument_list.begin(),
268 argument_list.insert(argument_list.end(),
269 llvm::ConstantPointerNull::get(
273 argument_list.insert(
278 argument_list.insert(
282 argument_list.insert(
294 func +=
"_Predicate"s;
302 arg1_list.insert(arg1_list.end(),
303 llvm::ConstantPointerNull::get(
307 arg1_list.insert(arg1_list.end(),
311 arg1_list.insert(arg1_list.end(),
314 arg1_list.insert(arg1_list.end(),
320 throw std::runtime_error(
"Unsupported binary geo operation.");
324 argument_list.insert(argument_list.end(), arg1_list.begin(), arg1_list.end());
337 const std::vector<std::shared_ptr<Analyzer::Expr>>& geo_args,
340 std::vector<llvm::Value*> argument_list;
341 bool coord_col =
true;
342 for (
const auto& geo_arg : geo_args) {
343 const auto arg = geo_arg.get();
344 const auto& arg_ti = arg->get_type_info();
345 const auto elem_ti = arg_ti.get_elem_type();
346 const auto arg_lvs =
codegen(arg,
true, co);
347 if (arg_ti.is_number()) {
348 argument_list.emplace_back(arg_lvs.front());
351 if (arg_ti.is_geometry()) {
352 argument_list.insert(argument_list.end(), arg_lvs.begin(), arg_lvs.end());
355 CHECK(arg_ti.is_array());
356 if (arg_lvs.size() > 1) {
357 CHECK_EQ(
size_t(2), arg_lvs.size());
358 auto ptr_lv = arg_lvs.front();
365 argument_list.emplace_back(ptr_lv);
368 argument_list.emplace_back(cast_len_lv);
370 CHECK_EQ(
size_t(1), arg_lvs.size());
371 if (arg_ti.get_size() > 0) {
373 auto ptr_lv = arg_lvs.front();
378 "fast_fixlen_array_buff",
380 {arg_lvs.front(),
posArg(arg)});
388 argument_list.emplace_back(ptr_lv);
394 {arg_lvs.front(),
posArg(arg)});
401 argument_list.emplace_back(ptr_lv);
410 argument_list.emplace_back(cast_len_lv);
414 return argument_list;
418 const std::string& func,
419 std::vector<llvm::Value*> argument_list,
424 argument_list.emplace_back(
result);
431 llvm::BasicBlock* geos_pred_ok_bb{
nullptr};
432 llvm::BasicBlock* geos_pred_fail_bb{
nullptr};
433 geos_pred_ok_bb = llvm::BasicBlock::Create(
435 geos_pred_fail_bb = llvm::BasicBlock::Create(
451 const std::string& func,
452 std::vector<llvm::Value*> argument_list,
453 llvm::Value* result_srid,
460 auto pi8_type = llvm::PointerType::get(i8_type, 0);
461 auto pi32_type = llvm::PointerType::get(i32_type, 0);
467 auto result_coords_size =
469 auto result_ring_sizes =
471 auto result_ring_sizes_size =
473 auto result_poly_rings =
475 auto result_poly_rings_size =
478 argument_list.emplace_back(result_type);
479 argument_list.emplace_back(result_coords);
480 argument_list.emplace_back(result_coords_size);
481 argument_list.emplace_back(result_ring_sizes);
482 argument_list.emplace_back(result_ring_sizes_size);
483 argument_list.emplace_back(result_poly_rings);
484 argument_list.emplace_back(result_poly_rings_size);
485 argument_list.emplace_back(result_srid);
492 llvm::BasicBlock* geos_ok_bb{
nullptr};
493 llvm::BasicBlock* geos_fail_bb{
nullptr};
494 geos_ok_bb = llvm::BasicBlock::Create(
496 geos_fail_bb = llvm::BasicBlock::Create(
514 result_coords->getType()->getPointerElementType(), result_coords);
516 result_coords_size->getType()->getPointerElementType(), result_coords_size);
518 result_ring_sizes->getType()->getPointerElementType(), result_ring_sizes);
520 result_ring_sizes_size->getType()->getPointerElementType(), result_ring_sizes_size);
522 result_poly_rings->getType()->getPointerElementType(), result_poly_rings);
524 result_poly_rings_size->getType()->getPointerElementType(), result_poly_rings_size);
528 "register_buffer_with_executor_rsm",
532 cgen_state_->emitExternalCall(
533 "register_buffer_with_executor_rsm",
534 llvm::Type::getVoidTy(cgen_state_->context_),
535 {cgen_state_->llInt(reinterpret_cast<int64_t>(executor())),
536 cgen_state_->ir_builder_.CreatePointerCast(buf2, pi8_type)});
537 cgen_state_->emitExternalCall(
538 "register_buffer_with_executor_rsm",
539 llvm::Type::getVoidTy(cgen_state_->context_),
540 {cgen_state_->llInt(reinterpret_cast<int64_t>(executor())),
541 cgen_state_->ir_builder_.CreatePointerCast(buf3, pi8_type)});
543 return {cgen_state_->ir_builder_.CreatePointerCast(buf1, pi8_type),
545 cgen_state_->ir_builder_.CreatePointerCast(buf2, pi32_type),
547 cgen_state_->ir_builder_.CreatePointerCast(buf3, pi32_type),
const SQLTypeInfo getTypeInfo0() const
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs0() const
const SQLTypeInfo getTypeInfo0() const
std::map< std::pair< llvm::Value *, llvm::Value * >, ArrayLoadCodegen > array_load_cache_
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)
llvm::IRBuilder ir_builder_
int32_t get_compression_scheme(const SQLTypeInfo &ti)
llvm::Value * posArg(const Analyzer::Expr *) const
const SQLTypeInfo getTypeInfo1() const
llvm::ConstantInt * llBool(const bool v) const
static const int32_t ERR_GEOS
HOST DEVICE SQLTypes get_type() const
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
std::vector< llvm::Value * > codegenGeoBinOper(const Analyzer::GeoBinOper *, const CompilationOptions &)
std::unordered_map< std::string, llvm::Value * > geo_target_cache_
std::vector< llvm::Value * > codegenGeoExpr(const Analyzer::GeoExpr *, const CompilationOptions &)
llvm::LLVMContext & context_
llvm::Function * current_func_
std::vector< llvm::Value * > codegenGeoOperator(const Analyzer::GeoOperator *, const CompilationOptions &)
std::vector< llvm::Value * > codegenGeoUOper(const Analyzer::GeoUOper *, const CompilationOptions &)
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs0() const
const std::string & getName() const
std::vector< llvm::Value * > codegenGeosConstructorCall(const std::string &, std::vector< llvm::Value * >, llvm::Value *, const CompilationOptions &)
const SQLTypeInfo & get_type_info() const
ExecutorDeviceType device_type
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
virtual std::string toString() const =0
std::vector< llvm::Value * > codegenGeosPredicateCall(const std::string &, std::vector< llvm::Value * >, const CompilationOptions &)
static ArrayLoadCodegen codegenGeoArrayLoadAndNullcheck(llvm::Value *byte_stream, llvm::Value *pos, const SQLTypeInfo &ti, CgenState *cgen_state)
std::vector< llvm::Value * > codegenGeoColumnVar(const Analyzer::GeoColumnVar *, const bool fetch_columns, const CompilationOptions &co)
HOST DEVICE int get_input_srid() const
llvm::ConstantInt * llInt(const T v) const
std::vector< llvm::Value * > codegenGeoArgs(const std::vector< std::shared_ptr< Analyzer::Expr >> &, const CompilationOptions &)
Geospatial::GeoBase::GeoOp getOp() const
std::vector< llvm::Value * > codegenGeoConstant(const Analyzer::GeoConstant *, const CompilationOptions &)
uint32_t log2_bytes(const uint32_t bytes)
std::shared_ptr< Analyzer::Constant > makePhysicalConstant(const size_t index) const
HOST DEVICE bool get_notnull() const
static std::unique_ptr< Codegen > init(const Analyzer::GeoOperator *geo_operator, const Catalog_Namespace::Catalog *catalog)
std::string toString() const override
int get_physical_coord_cols() const
size_t physicalCols() const
Geospatial::GeoBase::GeoOp getOp() const
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs1() const
const ColumnDescriptor * get_column_descriptor(const int col_id, const int table_id, const Catalog_Namespace::Catalog &cat)
HOST DEVICE int get_output_srid() const
Executor * executor() const