36 return std::make_shared<FixedWidthInt>(1);
38 return std::make_shared<FixedWidthInt>(1);
40 return std::make_shared<FixedWidthInt>(2);
42 return std::make_shared<FixedWidthInt>(4);
44 return std::make_shared<FixedWidthInt>(8);
46 return std::make_shared<FixedWidthReal>(
false);
48 return std::make_shared<FixedWidthReal>(
true);
52 return std::make_shared<FixedWidthInt>(8);
58 CHECK(ti.is_string());
62 if (ti.get_size() < ti.get_logical_size()) {
63 return std::make_shared<FixedWidthUnsigned>(ti.get_size());
65 return std::make_shared<FixedWidthInt>(ti.get_size());
69 return std::make_shared<FixedWidthInt>(bit_width / 8);
72 CHECK(ti.is_date_in_days());
74 ? std::make_shared<FixedWidthSmallDate>(2)
75 : std::make_shared<FixedWidthSmallDate>(4);
94 const bool fetch_column,
106 const bool fetch_column,
107 const bool update_query_plan,
114 if (column_key.table_id > 0) {
116 if (cd->isVirtualCol) {
117 CHECK(cd->columnName ==
"rowid");
120 const auto col_ti = cd->columnType;
121 if (col_ti.get_physical_coord_cols() > 0) {
122 std::vector<llvm::Value*> cols;
123 const auto col_id = column_key.column_id;
124 auto temp_column_key = column_key;
125 for (
auto i = 0; i < col_ti.get_physical_coord_cols(); i++) {
126 temp_column_key.column_id = col_id + i + 1;
129 const auto col0_ti = cd0->columnType;
130 CHECK(!cd0->isVirtualCol);
131 const auto col0_var =
132 makeExpr<Analyzer::ColumnVar>(col0_ti, temp_column_key, rte_idx);
133 const auto col =
codegenColVar(col0_var.get(), fetch_column,
false, co);
134 cols.insert(cols.end(), col.begin(), col.end());
148 throw std::runtime_error(
149 "Geospatial columns not supported in temporary tables yet");
153 if (grouped_col_lv) {
154 return {grouped_col_lv};
156 const auto col_var_hash = boost::hash_value(col_var->
toString());
157 const auto window_func_context =
164 if (!window_func_context) {
177 if (hash_join_lhs && hash_join_lhs->get_type_info() == col_var->
get_type_info()) {
181 return codegen(hash_join_lhs.get(), fetch_column, co);
183 auto pos_arg =
posArg(col_var);
184 if (window_func_context) {
187 auto col_byte_stream =
colByteStream(col_var, fetch_column, hoist_literals);
189 if (update_query_plan) {
203 if (col_ti.is_string() && col_ti.get_compression() ==
kENCODING_NONE) {
204 const auto varlen_str_column_lvs =
206 if (!window_func_context) {
208 std::make_pair(col_var_hash, varlen_str_column_lvs));
211 return varlen_str_column_lvs;
213 if (col_ti.is_array()) {
214 return {col_byte_stream};
216 if (window_func_context) {
218 col_var, col_byte_stream, pos_arg, window_func_context)};
220 const auto fixed_length_column_lv =
223 std::make_pair(col_var_hash, std::vector<llvm::Value*>{fixed_length_column_lv}));
224 return {it_ok.first->second};
229 llvm::Value* pos_arg) {
232 "row_number_window_func",
235 return window_position;
242 llvm::Value* col_byte_stream,
243 llvm::Value* pos_arg,
247 auto dec_val = decoder->codegenDecode(col_byte_stream, pos_arg,
cgen_state_->
module_);
249 auto dec_type = dec_val->getType();
250 llvm::Value* dec_val_cast{
nullptr};
252 if (dec_type->isIntegerTy()) {
253 auto dec_width =
static_cast<llvm::IntegerType*
>(dec_type)->getBitWidth();
256 static_cast<size_t>(col_width) > dec_width ? llvm::Instruction::CastOps::SExt
257 : llvm::Instruction::CastOps::Trunc,
260 bool adjust_fixed_enc_null =
true;
261 if (window_function_context &&
265 const auto order_key_ti =
267 if (order_key_ti.is_timestamp() && order_key_ti.get_size() == 4) {
268 adjust_fixed_enc_null =
false;
271 if (adjust_fixed_enc_null &&
273 (col_ti.get_compression() ==
kENCODING_DICT && col_ti.get_size() < 4)) &&
274 !col_ti.get_notnull()) {
279 CHECK(dec_type->isFloatTy() || dec_type->isDoubleTy());
280 if (dec_type->isDoubleTy()) {
282 }
else if (dec_type->isFloatTy()) {
285 dec_val_cast = dec_val;
293 llvm::Value* col_byte_stream,
294 llvm::Value* pos_arg,
298 const auto pos_valid_bb = llvm::BasicBlock::Create(
300 const auto pos_notvalid_bb = llvm::BasicBlock::Create(
302 const auto pos_is_valid =
313 auto n_value_lv =
cgen_state_->
llInt((int64_t)n_value_ptr->get_constval().intval);
316 auto partition_index_lv =
317 executor_->codegenCurrentPartitionIndex(window_function_context, pos_arg);
319 const auto pi32_type =
321 const auto partition_count_buf =
323 auto partition_count_buf_ptr_lv =
327 const auto num_elem_current_partition_ptr =
329 partition_count_buf_ptr_lv,
333 num_elem_current_partition_ptr->getType()->getPointerElementType(),
334 num_elem_current_partition_ptr),
337 n_value_lv, num_elem_current_partition_lv,
"is_valid_nth_value");
339 is_valid_n_value_lv, pos_is_valid,
"is_valid_row_for_nth_value");
348 col_var, col_byte_stream, pos_arg, window_function_context);
351 const auto window_func_call_phi =
353 window_func_call_phi->addIncoming(fixed_length_column_lv, pos_valid_bb);
358 window_func_call_phi->addIncoming(null_lv, orig_bb);
359 return window_func_call_phi;
363 llvm::Value* col_byte_stream,
364 llvm::Value* pos_arg) {
373 return {string_view, str_lv, len_lv};
380 llvm::Value* start_rowid_lv{
nullptr};
382 if (table_generation.start_rowid > 0) {
386 d.
bigintval = table_generation.start_rowid;
387 const auto start_rowid = makeExpr<Analyzer::Constant>(
kBIGINT,
false, d);
389 CHECK_EQ(
size_t(1), start_rowid_lvs.size());
390 start_rowid_lv = start_rowid_lvs.front();
392 auto rowid_lv =
posArg(col_var);
398 frag_off_ptr->getType()->getScalarType()->getPointerElementType(),
402 input_off_ptr->getType()->getPointerElementType(), input_off_ptr);
405 if (table_generation.start_rowid > 0) {
406 CHECK(start_rowid_lv);
437 const auto col_phys_width = col_ti.
get_size() * 8;
438 auto from_typename =
"int" +
std::to_string(col_phys_width) +
"_t";
440 llvm::Instruction::CastOps::Trunc,
444 from_typename =
"u" + from_typename;
445 llvm::Value* from_null{
nullptr};
483 const bool fetch_column,
487 if (grouped_col_lv) {
488 return {grouped_col_lv};
490 const auto outer_join_args_bb = llvm::BasicBlock::Create(
492 const auto outer_join_nulls_bb = llvm::BasicBlock::Create(
494 const auto phi_bb = llvm::BasicBlock::Create(
497 CHECK(outer_join_match_lv);
499 outer_join_match_lv, outer_join_args_bb, outer_join_nulls_bb);
500 const auto back_from_outer_join_bb = llvm::BasicBlock::Create(
504 const auto orig_lvs =
codegenColVar(col_var, fetch_column,
true, co);
515 if ((null_ti.is_string() && null_ti.get_compression() ==
kENCODING_NONE) ||
516 null_ti.is_array() || null_ti.is_geometry()) {
517 throw std::runtime_error(
"Projection type " + null_ti.get_type_name() +
518 " not supported for outer joins yet");
520 const auto null_constant = makeExpr<Analyzer::Constant>(null_ti,
true,
Datum{0});
521 const auto null_target_lvs =
527 CHECK_EQ(orig_lvs.size(), null_target_lvs.size());
529 std::vector<llvm::Value*> target_lvs;
530 for (
size_t i = 0; i < orig_lvs.size(); ++i) {
531 const auto target_type = orig_lvs[i]->getType();
532 CHECK_EQ(target_type, null_target_lvs[i]->getType());
534 const auto orig_lvs_bb = needs_casting_col_var ? cast_bb : outer_join_args_bb;
535 target_phi->addIncoming(orig_lvs[i], orig_lvs_bb);
536 target_phi->addIncoming(null_target_lvs[i], outer_join_nulls_bb);
537 target_lvs.push_back(target_phi);
550 CHECK((column_key.column_id == 0) ||
551 (col_var->
get_rte_idx() >= 0 && column_key.table_id > 0));
552 const auto var =
dynamic_cast<const Analyzer::Var*
>(col_var);
554 const auto var_no = var->get_varno();
565 const bool fetch_column,
566 const bool hoist_literals) {
568 const auto stream_arg_name =
571 if (arg.getName() == stream_arg_name) {
583 if (col_var && col_var->get_rte_idx() > 0) {
584 const auto hash_pos_it =
587 if (hash_pos_it->second->getType()->isPointerTy()) {
588 CHECK(hash_pos_it->second->getType()->getPointerElementType()->isIntegerTy(32));
590 hash_pos_it->second->getType()->getPointerElementType(), hash_pos_it->second);
595 return hash_pos_it->second;
598 if (arg.getName() ==
"pos") {
599 CHECK(arg.getType()->isIntegerTy(64));
608 if (!uoper || uoper->get_optype() !=
kCAST) {
612 if (!target_ti.is_integer()) {
615 return uoper->get_operand();
622 if (dynamic_cast<const Analyzer::ExpressionTuple*>(
623 tautological_eq->get_left_operand())) {
629 auto eq_right_op = tautological_eq->get_right_operand();
634 eq_right_op = tautological_eq->get_right_operand();
636 if (*eq_right_op == *rhs) {
637 auto eq_left_op = tautological_eq->get_left_operand();
638 if (!eq_left_op->get_type_info().is_string()) {
642 eq_left_op = tautological_eq->get_left_operand();
644 if (eq_left_op->get_type_info().is_geometry()) {
654 if (!eq_left_op_col) {
655 if (dynamic_cast<const Analyzer::StringOper*>(eq_left_op)) {
658 if (dynamic_cast<const Analyzer::FunctionOper*>(eq_left_op)) {
662 CHECK(eq_left_op_col);
663 if (eq_left_op_col->get_rte_idx() != 0) {
667 return eq_left_op->deep_copy();
676 return makeExpr<Analyzer::UOper>(
687 if (dynamic_cast<const Analyzer::ExpressionTuple*>(
688 tautological_eq->get_left_operand())) {
697 auto eq_right_op = tautological_eq->get_right_operand();
702 eq_right_op = tautological_eq->get_right_operand();
704 if (*eq_right_op == *rhs) {
705 auto eq_left_op = tautological_eq->get_left_operand();
706 if (!eq_left_op->get_type_info().is_string()) {
710 eq_left_op = tautological_eq->get_left_operand();
712 if (eq_left_op->get_type_info().is_geometry()) {
722 if (!eq_left_op_col) {
723 if (dynamic_cast<const Analyzer::StringOper*>(eq_left_op)) {
726 if (dynamic_cast<const Analyzer::FunctionOper*>(eq_left_op)) {
730 CHECK(eq_left_op_col);
731 if (eq_left_op_col->get_rte_idx() != 0) {
750 const auto lhs_tuple_expr =
754 CHECK(lhs_tuple_expr && rhs_tuple_expr);
755 const auto& lhs_tuple = lhs_tuple_expr->getTuple();
756 const auto& rhs_tuple = rhs_tuple_expr->getTuple();
757 CHECK_EQ(lhs_tuple.size(), rhs_tuple.size());
758 for (
size_t i = 0; i < lhs_tuple.size(); ++i) {
759 if (*rhs_tuple[i] == *rhs) {
762 return lhs_col->
get_rte_idx() == 0 ? lhs_col :
nullptr;
bool hasRangeModeFraming() const
HOST DEVICE SQLTypes get_subtype() const
llvm::Value * castToTypeIn(llvm::Value *val, const size_t bit_width)
SqlWindowFunctionKind getKind() const
std::vector< llvm::Value * > outer_join_match_found_per_level_
HOST DEVICE int get_size() const
const Analyzer::Expr * remove_cast_to_int(const Analyzer::Expr *expr)
std::unordered_map< size_t, std::vector< llvm::Value * > > fetch_cache_
#define IS_EQUIVALENCE(X)
llvm::Value * codegenRowId(const Analyzer::ColumnVar *col_var, const CompilationOptions &co)
llvm::Value * codegenFixedLengthColVarInWindow(const Analyzer::ColumnVar *col_var, llvm::Value *col_byte_stream, llvm::Value *pos_arg, const WindowFunctionContext *window_function_context=nullptr)
HOST DEVICE int get_scale() const
const Expr * get_right_operand() const
bool is_constructed_point(const Analyzer::Expr *expr)
llvm::IRBuilder ir_builder_
std::vector< llvm::Value * > codegenOuterJoinNullPlaceholder(const Analyzer::ColumnVar *col_var, const bool fetch_column, const CompilationOptions &co)
const std::vector< SQLTypeInfo > & getOrderKeyColumnBufferTypes() const
llvm::Value * posArg(const Analyzer::Expr *) const
llvm::Value * codgenAdjustFixedEncNull(llvm::Value *, const SQLTypeInfo &)
const int8_t * output() const
llvm::Value * foundOuterJoinMatch(const size_t nesting_level) const
const int32_t * counts() const
virtual std::vector< llvm::Value * > codegenColumn(const Analyzer::ColumnVar *, const bool fetch_column, const CompilationOptions &)
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
std::shared_ptr< Decoder > get_col_decoder(const Analyzer::ColumnVar *col_var)
EncodingType get_compression() const
static WindowFunctionContext * getActiveWindowFunctionContext(Executor *executor)
llvm::Function * row_func_
std::vector< llvm::Value * > group_by_expr_cache_
std::shared_ptr< const Analyzer::Expr > hashJoinLhs(const Analyzer::ColumnVar *rhs) const
llvm::Value * codegenWindowPosition(const WindowFunctionContext *window_func_context, llvm::Value *pos_arg)
int getLocalColumnId(const Analyzer::ColumnVar *col_var, const bool fetch_column)
size_t get_bit_width(const SQLTypeInfo &ti)
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)
llvm::Value * get_arg_by_name(llvm::Function *func, const std::string &name)
const std::vector< std::shared_ptr< Analyzer::Expr > > & getArgs() const
bool isLazyFetchColumn(const Analyzer::Expr *target_expr) const
int get_logical_size() const
size_t get_col_bit_width(const Analyzer::ColumnVar *col_var)
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
std::string toString() const override
const ColumnDescriptor * get_column_descriptor(const shared::ColumnKey &column_key)
std::set< shared::ColumnKey > columns_to_not_fetch_
std::unordered_map< int, llvm::Value * > scan_idx_to_hash_pos_
std::vector< llvm::Value * > codegenColVar(const Analyzer::ColumnVar *, const bool fetch_column, const bool update_query_plan, const CompilationOptions &)
const SQLTypeInfo & get_type_info() const
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
SQLTypes decimal_to_int_type(const SQLTypeInfo &ti)
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
const shared::ColumnKey & getColumnKey() const
HOST DEVICE EncodingType get_compression() const
std::vector< llvm::Value * > frag_offsets_
llvm::StructType * createStringViewStructType()
HOST DEVICE int get_dimension() const
HOST DEVICE int get_comp_param() const
bool needCastForHashJoinLhs(const Analyzer::ColumnVar *rhs) const
llvm::ConstantInt * llInt(const T v) const
std::set< shared::ColumnKey > columns_to_fetch_
llvm::Value * colByteStream(const Analyzer::ColumnVar *col_var, const bool fetch_column, const bool hoist_literals)
const Expr * get_left_operand() const
const Analyzer::WindowFunction * getWindowFunction() const
std::string numeric_type_name(const SQLTypeInfo &ti)
std::shared_ptr< const Analyzer::ColumnVar > hashJoinLhsTuple(const Analyzer::ColumnVar *rhs, const Analyzer::BinOper *tautological_eq) const
llvm::Value * codegenFixedLengthColVar(const Analyzer::ColumnVar *col_var, llvm::Value *col_byte_stream, llvm::Value *pos_arg, const WindowFunctionContext *window_function_context=nullptr)
std::vector< llvm::Value * > codegenVariableLengthStringColVar(llvm::Value *col_byte_stream, llvm::Value *pos_arg)
int32_t get_rte_idx() const
SQLTypes get_phys_int_type(const size_t byte_sz)
int adjusted_range_table_index(const Analyzer::ColumnVar *col_var)
shared::TableKey getTableKey() const
llvm::Value * resolveGroupedColumnReference(const Analyzer::ColumnVar *)
llvm::ConstantFP * inlineFpNull(const SQLTypeInfo &)
Executor * executor() const