37 return std::make_shared<FixedWidthInt>(1);
39 return std::make_shared<FixedWidthInt>(1);
41 return std::make_shared<FixedWidthInt>(2);
43 return std::make_shared<FixedWidthInt>(4);
45 return std::make_shared<FixedWidthInt>(8);
47 return std::make_shared<FixedWidthReal>(
false);
49 return std::make_shared<FixedWidthReal>(
true);
53 return std::make_shared<FixedWidthInt>(8);
55 CHECK(
false) <<
"ti=" << ti;
59 CHECK(ti.is_string());
63 if (ti.get_size() < ti.get_logical_size()) {
64 return std::make_shared<FixedWidthUnsigned>(ti.get_size());
66 return std::make_shared<FixedWidthInt>(ti.get_size());
70 return std::make_shared<FixedWidthInt>(bit_width / 8);
73 CHECK(ti.is_date_in_days());
75 ? std::make_shared<FixedWidthSmallDate>(2)
76 : std::make_shared<FixedWidthSmallDate>(4);
95 const bool fetch_column,
107 const bool fetch_column,
108 const bool update_query_plan,
115 if (column_key.table_id > 0) {
117 if (cd->isVirtualCol) {
118 CHECK(cd->columnName ==
"rowid");
121 const auto col_ti = cd->columnType;
122 if (col_ti.usesFlatBuffer()) {
123 throw std::runtime_error(
124 "Flatbuffer storage in a real table column not supported yet");
126 if (col_ti.get_physical_coord_cols() > 0) {
127 std::vector<llvm::Value*> cols;
128 const auto col_id = column_key.column_id;
129 auto temp_column_key = column_key;
130 bool fetch_physical_columns = fetch_column;
131 for (
auto i = 0; i < col_ti.get_physical_coord_cols(); i++) {
132 temp_column_key.column_id = col_id + i + 1;
135 const auto col0_ti = cd0->columnType;
136 CHECK(!cd0->isVirtualCol);
137 const auto col0_var =
138 makeExpr<Analyzer::ColumnVar>(col0_ti, temp_column_key, rte_idx);
141 fetch_physical_columns =
true;
143 const auto col =
codegenColVar(col0_var.get(), fetch_physical_columns,
false, co);
144 cols.insert(cols.end(), col.begin(), col.end());
158 if (grouped_col_lv) {
159 return {grouped_col_lv};
162 const auto window_func_context =
169 if (!window_func_context) {
182 if (hash_join_lhs && hash_join_lhs->get_type_info() == col_var->
get_type_info()) {
186 return codegen(hash_join_lhs.get(), fetch_column, co);
188 auto pos_arg =
posArg(col_var);
189 if (window_func_context) {
192 auto col_byte_stream =
colByteStream(col_var, fetch_column, hoist_literals);
194 if (update_query_plan) {
208 if (col_ti.is_string() && col_ti.get_compression() ==
kENCODING_NONE) {
209 const auto varlen_str_column_lvs =
211 if (!window_func_context) {
213 std::make_pair(col_var_hash, varlen_str_column_lvs));
216 return varlen_str_column_lvs;
218 if (col_ti.usesFlatBuffer()) {
219 return {col_byte_stream};
221 if (col_ti.is_array() || col_ti.get_type() ==
kPOINT) {
222 return {col_byte_stream};
224 if (window_func_context) {
226 col_var, col_byte_stream, pos_arg, co, window_func_context)};
228 const auto fixed_length_column_lv =
231 std::make_pair(col_var_hash, std::vector<llvm::Value*>{fixed_length_column_lv}));
232 return {it_ok.first->second};
237 llvm::Value* pos_arg) {
240 "row_number_window_func",
243 return window_position;
250 llvm::Value* col_byte_stream,
251 llvm::Value* pos_arg,
255 auto dec_val = decoder->codegenDecode(col_byte_stream, pos_arg,
cgen_state_->
module_);
257 auto dec_type = dec_val->getType();
258 llvm::Value* dec_val_cast{
nullptr};
260 if (dec_type->isIntegerTy()) {
261 auto dec_width =
static_cast<llvm::IntegerType*
>(dec_type)->getBitWidth();
264 static_cast<size_t>(col_width) > dec_width ? llvm::Instruction::CastOps::SExt
265 : llvm::Instruction::CastOps::Trunc,
268 bool adjust_fixed_enc_null =
true;
269 if (window_function_context &&
273 const auto order_key_ti =
275 if (order_key_ti.is_timestamp() && order_key_ti.get_size() == 4) {
276 adjust_fixed_enc_null =
false;
279 if (adjust_fixed_enc_null &&
281 (col_ti.get_compression() ==
kENCODING_DICT && col_ti.get_size() < 4)) &&
282 !col_ti.get_notnull()) {
287 CHECK(dec_type->isFloatTy() || dec_type->isDoubleTy());
288 if (dec_type->isDoubleTy()) {
290 }
else if (dec_type->isFloatTy()) {
293 dec_val_cast = dec_val;
301 llvm::Value* col_byte_stream,
302 llvm::Value* pos_arg,
307 const auto pos_valid_bb = llvm::BasicBlock::Create(
309 const auto pos_notvalid_bb = llvm::BasicBlock::Create(
311 const auto pos_is_valid =
322 auto n_value_lv =
cgen_state_->
llInt((int64_t)n_value_ptr->get_constval().intval);
325 auto partition_index_lv =
executor_->codegenCurrentPartitionIndex(
326 window_function_context,
this, co, pos_arg);
328 const auto pi32_type =
330 const auto partition_count_buf =
342 const auto num_elem_current_partition_ptr =
344 partition_count_buf_ptr_lv,
348 num_elem_current_partition_ptr->getType()->getPointerElementType(),
349 num_elem_current_partition_ptr),
352 n_value_lv, num_elem_current_partition_lv,
"is_valid_nth_value");
354 is_valid_n_value_lv, pos_is_valid,
"is_valid_row_for_nth_value");
363 col_var, col_byte_stream, pos_arg, window_function_context);
366 const auto window_func_call_phi =
368 window_func_call_phi->addIncoming(fixed_length_column_lv, pos_valid_bb);
373 window_func_call_phi->addIncoming(null_lv, orig_bb);
374 return window_func_call_phi;
378 llvm::Value* col_byte_stream,
379 llvm::Value* pos_arg) {
388 return {string_view, str_lv, len_lv};
395 llvm::Value* start_rowid_lv{
nullptr};
397 if (table_generation.start_rowid > 0) {
401 d.
bigintval = table_generation.start_rowid;
402 const auto start_rowid = makeExpr<Analyzer::Constant>(
kBIGINT,
false, d);
404 CHECK_EQ(
size_t(1), start_rowid_lvs.size());
405 start_rowid_lv = start_rowid_lvs.front();
407 auto rowid_lv =
posArg(col_var);
413 frag_off_ptr->getType()->getScalarType()->getPointerElementType(),
417 input_off_ptr->getType()->getPointerElementType(), input_off_ptr);
420 if (table_generation.start_rowid > 0) {
421 CHECK(start_rowid_lv);
452 const auto col_phys_width = col_ti.
get_size() * 8;
453 auto from_typename =
"int" +
std::to_string(col_phys_width) +
"_t";
455 llvm::Instruction::CastOps::Trunc,
459 from_typename =
"u" + from_typename;
460 llvm::Value* from_null{
nullptr};
498 const bool fetch_column,
502 if (grouped_col_lv) {
503 return {grouped_col_lv};
505 const auto outer_join_args_bb = llvm::BasicBlock::Create(
507 const auto outer_join_nulls_bb = llvm::BasicBlock::Create(
509 const auto phi_bb = llvm::BasicBlock::Create(
512 CHECK(outer_join_match_lv);
514 outer_join_match_lv, outer_join_args_bb, outer_join_nulls_bb);
515 const auto back_from_outer_join_bb = llvm::BasicBlock::Create(
519 const auto orig_lvs =
codegenColVar(col_var, fetch_column,
true, co);
533 const auto null_constant = makeExpr<Analyzer::Constant>(null_ti,
true,
Datum{0});
534 auto const null_target_lvs =
codegen(null_constant.get(), fetch_column, co);
536 CHECK_EQ(orig_lvs.size(), null_target_lvs.size());
538 std::vector<llvm::Value*> target_lvs;
539 for (
size_t i = 0; i < orig_lvs.size(); ++i) {
540 const auto target_type = orig_lvs[i]->getType();
541 const auto null_type = null_target_lvs[i]->getType();
544 const auto orig_lvs_bb = needs_casting_col_var ? cast_bb : outer_join_args_bb;
545 target_phi->addIncoming(orig_lvs[i], orig_lvs_bb);
546 target_phi->addIncoming(null_target_lvs[i], outer_join_nulls_bb);
547 target_lvs.push_back(target_phi);
560 CHECK((column_key.column_id == 0) ||
561 (col_var->
get_rte_idx() >= 0 && column_key.table_id > 0));
562 const auto var =
dynamic_cast<const Analyzer::Var*
>(col_var);
564 const auto var_no = var->get_varno();
575 const bool fetch_column,
576 const bool hoist_literals) {
578 const auto stream_arg_name =
581 if (arg.getName() == stream_arg_name) {
593 if (col_var && col_var->get_rte_idx() > 0) {
594 const auto hash_pos_it =
597 if (hash_pos_it->second->getType()->isPointerTy()) {
598 CHECK(hash_pos_it->second->getType()->getPointerElementType()->isIntegerTy(32));
600 hash_pos_it->second->getType()->getPointerElementType(), hash_pos_it->second);
605 return hash_pos_it->second;
608 if (arg.getName() ==
"pos") {
609 CHECK(arg.getType()->isIntegerTy(64));
620 if (!uoper || uoper->get_optype() !=
kCAST) {
624 if (!target_ti.is_integer()) {
627 return uoper->get_operand();
634 if (dynamic_cast<const Analyzer::ExpressionTuple*>(
635 tautological_eq->get_left_operand())) {
641 auto eq_right_op = tautological_eq->get_right_operand();
646 eq_right_op = tautological_eq->get_right_operand();
648 if (*eq_right_op == *rhs) {
649 auto eq_left_op = tautological_eq->get_left_operand();
650 if (!eq_left_op->get_type_info().is_string()) {
654 eq_left_op = tautological_eq->get_left_operand();
656 if (eq_left_op->get_type_info().is_geometry()) {
666 if (!eq_left_op_col) {
667 if (dynamic_cast<const Analyzer::StringOper*>(eq_left_op)) {
670 if (dynamic_cast<const Analyzer::FunctionOper*>(eq_left_op)) {
673 auto const cast_expr =
dynamic_cast<const Analyzer::UOper*
>(eq_left_op);
674 if (cast_expr && cast_expr->get_type_info().is_date()) {
683 CHECK(eq_left_op_col) <<
"Expect Analyzer::ColumnVar* type expression: "
684 << eq_left_op->toString();
685 if (eq_left_op_col->get_rte_idx() != 0) {
689 return eq_left_op->deep_copy();
698 return makeExpr<Analyzer::UOper>(
709 if (dynamic_cast<const Analyzer::ExpressionTuple*>(
710 tautological_eq->get_left_operand())) {
719 auto eq_right_op = tautological_eq->get_right_operand();
724 eq_right_op = tautological_eq->get_right_operand();
726 if (*eq_right_op == *rhs) {
727 auto eq_left_op = tautological_eq->get_left_operand();
728 if (!eq_left_op->get_type_info().is_string()) {
732 eq_left_op = tautological_eq->get_left_operand();
734 if (eq_left_op->get_type_info().is_geometry()) {
744 if (!eq_left_op_col) {
745 if (dynamic_cast<const Analyzer::StringOper*>(eq_left_op)) {
748 if (dynamic_cast<const Analyzer::FunctionOper*>(eq_left_op)) {
752 CHECK(eq_left_op_col);
753 if (eq_left_op_col->get_rte_idx() != 0) {
772 const auto lhs_tuple_expr =
776 CHECK(lhs_tuple_expr && rhs_tuple_expr);
777 const auto& lhs_tuple = lhs_tuple_expr->getTuple();
778 const auto& rhs_tuple = rhs_tuple_expr->getTuple();
779 CHECK_EQ(lhs_tuple.size(), rhs_tuple.size());
780 for (
size_t i = 0; i < lhs_tuple.size(); ++i) {
781 if (*rhs_tuple[i] == *rhs) {
784 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)
void addColumnToFetch(const shared::ColumnKey &column_key, bool unmark_lazy_fetch=false)
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_
void addColumnToNotFetch(const shared::ColumnKey &column_key)
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)
bool isColumnToFetch(const shared::ColumnKey &column_key) const
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::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)
static const int NUM_EXECUTION_DEVICES
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::Value * codegenFixedLengthColVarInWindow(const Analyzer::ColumnVar *col_var, llvm::Value *col_byte_stream, llvm::Value *pos_arg, const CompilationOptions &co, const WindowFunctionContext *window_function_context=nullptr)
llvm::StructType * createStringViewStructType()
HOST DEVICE int get_dimension() const
HOST DEVICE int get_comp_param() const
bool needCastForHashJoinLhs(const Analyzer::ColumnVar *rhs) const
std::size_t hash_value(RexAbstractInput const &rex_ab_input)
llvm::ConstantInt * llInt(const T v) const
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 * > createPtrWithHoistedMemoryAddr(CgenState *cgen_state, CodeGenerator *code_generator, CompilationOptions const &co, llvm::ConstantInt *ptr_int_val, llvm::Type *type, size_t num_devices_to_hoist_literal)
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