23 using heavyai::ErrorCode;
31 const auto key = std::make_pair(byte_stream, pos);
34 return cache_itr->second;
41 llvm::Type::getInt8PtrTy(cgen_state->
context_),
42 {key.first, key.second});
43 llvm::Value* pt_is_null{
nullptr};
45 pt_is_null = cgen_state->emitExternalCall(
"point_coord_array_is_null",
46 llvm::Type::getInt1Ty(cgen_state->context_),
47 {key.first, key.second});
50 cgen_state->array_load_cache_.insert(std::make_pair(key, arr_load));
56 const bool fetch_columns,
58 auto generate_column_lvs = [
this, geo_col_var, &co](
const int column_id) {
67 const auto lv_vec =
codegen(&col_var,
true, co);
73 switch (ti.get_type()) {
79 std::vector<llvm::Value*> geo_lvs;
81 for (
int i = 0; i < ti.get_physical_coord_cols(); i++) {
83 const auto lvs = generate_column_lvs(column_id);
85 geo_lvs.insert(geo_lvs.end(), lvs.begin(), lvs.end());
116 std::vector<llvm::Value*> ret;
117 for (
size_t i = 0; i < geo_constant->
physicalCols(); i++) {
119 auto operand_lvs =
codegen(physical_constant.get(),
true, co);
120 CHECK_EQ(operand_lvs.size(), size_t(2));
121 auto array_buff_lv = operand_lvs[0];
126 ret.push_back(array_buff_lv);
127 ret.push_back(operand_lvs[1]);
137 bool (*)(
const Analyzer::ColumnVar*,
const Analyzer::ColumnVar*)>
138 colvar_set(comparator);
140 std::for_each(colvar_set.begin(), colvar_set.end(), [&](
const Analyzer::ColumnVar* cv) {
154 if (geo_operator->
getName() ==
"ST_X" || geo_operator->
getName() ==
"ST_Y") {
155 const auto key = geo_operator->
toString();
158 return {geo_target_cache_it->second};
167 std::vector<llvm::Value*> load_lvs;
168 std::vector<llvm::Value*> pos_lvs;
169 for (
size_t i = 0; i < op_codegen->size(); i++) {
170 auto intermediate_lvs =
171 codegen(op_codegen->getOperand(i),
true, co);
172 load_lvs.insert(load_lvs.end(), intermediate_lvs.begin(), intermediate_lvs.end());
173 pos_lvs.push_back(
posArg(op_codegen->getOperand(i)));
176 auto [arg_lvs, null_lv] = op_codegen->codegenLoads(load_lvs, pos_lvs,
cgen_state_);
178 std::unique_ptr<CodeGenerator::NullCheckCodegen> nullcheck_codegen =
180 return op_codegen->codegen(arg_lvs, nullcheck_codegen.get(),
cgen_state_, co);
196 return argument_list;
200 throw std::runtime_error(
"Geo operation requires GEOS support.");
205 std::string func =
"Geos_Wkb"s;
212 throw std::runtime_error(
"GEOS runtime: input/output srids have to match.");
216 argument_list.insert(
217 argument_list.begin(),
220 argument_list.insert(argument_list.begin(),
223 argument_list.insert(argument_list.end(),
224 llvm::ConstantPointerNull::get(
229 argument_list.insert(
234 argument_list.insert(
238 argument_list.insert(
252 argument_list.insert(argument_list.end(),
cgen_state_->
llFp(
double(0)));
259 throw std::runtime_error(
"Unsupported unary geo operation.");
271 throw std::runtime_error(
"Geo operation requires GEOS support.");
278 std::string func =
"Geos_Wkb"s;
284 throw std::runtime_error(
"GEOS runtime: input/output srids have to match.");
288 argument_list.insert(
289 argument_list.begin(),
292 argument_list.insert(argument_list.begin(),
295 argument_list.insert(argument_list.end(),
296 llvm::ConstantPointerNull::get(
301 argument_list.insert(
306 argument_list.insert(
310 argument_list.insert(
322 func +=
"_Predicate"s;
329 arg1_list.insert(arg1_list.end(),
330 llvm::ConstantPointerNull::get(
335 arg1_list.insert(arg1_list.end(),
339 arg1_list.insert(arg1_list.end(),
342 arg1_list.insert(arg1_list.end(),
348 #if (GEOS_VERSION_MAJOR > 3) || (GEOS_VERSION_MAJOR == 3 && GEOS_VERSION_MINOR >= 11)
352 throw std::runtime_error(
"ST_ConcaveHull requires GEOS 3.11 or newer");
355 throw std::runtime_error(
"Unsupported binary geo operation.");
359 argument_list.insert(argument_list.end(), arg1_list.begin(), arg1_list.end());
372 const std::vector<std::shared_ptr<Analyzer::Expr>>& geo_args,
375 std::vector<llvm::Value*> argument_list;
376 bool coord_col =
true;
377 for (
const auto& geo_arg : geo_args) {
378 const auto arg = geo_arg.get();
379 const auto& arg_ti = arg->get_type_info();
380 const auto elem_ti = arg_ti.get_elem_type();
381 const auto arg_lvs =
codegen(arg,
true, co);
382 if (arg_ti.is_number()) {
383 argument_list.emplace_back(arg_lvs.front());
386 if (arg_ti.is_geometry()) {
387 argument_list.insert(argument_list.end(), arg_lvs.begin(), arg_lvs.end());
390 CHECK(arg_ti.is_array());
391 if (arg_lvs.size() > 1) {
392 CHECK_EQ(
size_t(2), arg_lvs.size());
393 auto ptr_lv = arg_lvs.front();
400 argument_list.emplace_back(ptr_lv);
403 argument_list.emplace_back(cast_len_lv);
405 CHECK_EQ(
size_t(1), arg_lvs.size());
406 if (arg_ti.get_size() > 0) {
408 auto ptr_lv = arg_lvs.front();
413 "fast_fixlen_array_buff",
415 {arg_lvs.front(),
posArg(arg)});
423 argument_list.emplace_back(ptr_lv);
429 {arg_lvs.front(),
posArg(arg)});
436 argument_list.emplace_back(ptr_lv);
445 argument_list.emplace_back(cast_len_lv);
449 return argument_list;
453 const std::string& func,
454 std::vector<llvm::Value*> argument_list,
459 argument_list.emplace_back(
result);
466 llvm::BasicBlock* geos_pred_ok_bb{
nullptr};
467 llvm::BasicBlock* geos_pred_fail_bb{
nullptr};
468 geos_pred_ok_bb = llvm::BasicBlock::Create(
470 geos_pred_fail_bb = llvm::BasicBlock::Create(
486 const std::string& func,
487 std::vector<llvm::Value*> argument_list,
488 llvm::Value* result_srid,
495 auto pi8_type = llvm::PointerType::get(i8_type, 0);
496 auto pi32_type = llvm::PointerType::get(i32_type, 0);
502 auto result_coords_size =
504 auto result_ring_sizes =
506 auto result_ring_sizes_size =
508 auto result_poly_rings =
510 auto result_poly_rings_size =
513 argument_list.emplace_back(result_type);
514 argument_list.emplace_back(result_coords);
515 argument_list.emplace_back(result_coords_size);
516 argument_list.emplace_back(result_ring_sizes);
517 argument_list.emplace_back(result_ring_sizes_size);
518 argument_list.emplace_back(result_poly_rings);
519 argument_list.emplace_back(result_poly_rings_size);
520 argument_list.emplace_back(result_srid);
527 llvm::BasicBlock* geos_ok_bb{
nullptr};
528 llvm::BasicBlock* geos_fail_bb{
nullptr};
529 geos_ok_bb = llvm::BasicBlock::Create(
531 geos_fail_bb = llvm::BasicBlock::Create(
549 result_coords->getType()->getPointerElementType(), result_coords);
551 result_coords_size->getType()->getPointerElementType(), result_coords_size);
553 result_ring_sizes->getType()->getPointerElementType(), result_ring_sizes);
555 result_ring_sizes_size->getType()->getPointerElementType(), result_ring_sizes_size);
557 result_poly_rings->getType()->getPointerElementType(), result_poly_rings);
559 result_poly_rings_size->getType()->getPointerElementType(), result_poly_rings_size);
563 "register_buffer_with_executor_rsm",
567 cgen_state_->emitExternalCall(
568 "register_buffer_with_executor_rsm",
569 llvm::Type::getVoidTy(cgen_state_->context_),
570 {cgen_state_->llInt(reinterpret_cast<int64_t>(executor())),
571 cgen_state_->ir_builder_.CreatePointerCast(buf2, pi8_type)});
572 cgen_state_->emitExternalCall(
573 "register_buffer_with_executor_rsm",
574 llvm::Type::getVoidTy(cgen_state_->context_),
575 {cgen_state_->llInt(reinterpret_cast<int64_t>(executor())),
576 cgen_state_->ir_builder_.CreatePointerCast(buf3, pi8_type)});
578 return {cgen_state_->ir_builder_.CreatePointerCast(buf1, pi8_type),
580 cgen_state_->ir_builder_.CreatePointerCast(buf2, pi32_type),
582 cgen_state_->ir_builder_.CreatePointerCast(buf3, pi32_type),
const SQLTypeInfo getTypeInfo0() const
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs0() const
void mark_logical_column_to_fetch(const Analyzer::GeoOperator *geo_operator, PlanState *plan_state)
static bool colvar_comp(const ColumnVar *l, const ColumnVar *r)
const SQLTypeInfo getTypeInfo0() const
std::map< std::pair< llvm::Value *, llvm::Value * >, ArrayLoadCodegen > array_load_cache_
void addColumnToFetch(const shared::ColumnKey &column_key, bool unmark_lazy_fetch=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
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_
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)
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
std::string toString() const override
const ColumnDescriptor * get_column_descriptor(const shared::ColumnKey &column_key)
const std::string & getName() const
llvm::ConstantFP * llFp(const float v) 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
const shared::ColumnKey & getColumnKey() const
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
void collect_column_var(std::set< const ColumnVar *, bool(*)(const ColumnVar *, const ColumnVar *)> &colvar_set, bool include_agg) const final
HOST DEVICE bool get_notnull() const
std::string toString() const override
int32_t get_rte_idx() const
int get_physical_coord_cols() const
size_t physicalCols() const
Geospatial::GeoBase::GeoOp getOp() const
static std::unique_ptr< Codegen > init(const Analyzer::GeoOperator *geo_operator)
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs1() const
HOST DEVICE int get_output_srid() const
Executor * executor() const