OmniSciDB  c0231cc57d
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
IRCodegen.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 "../Parser/ParserNode.h"
18 #include "CodeGenerator.h"
19 #include "Execute.h"
20 #include "ExternalExecutor.h"
21 #include "MaxwellCodegenPatch.h"
22 #include "RelAlgTranslator.h"
23 
25 
26 // Driver methods for the IR generation.
27 
29 
30 std::vector<llvm::Value*> CodeGenerator::codegen(const Analyzer::Expr* expr,
31  const bool fetch_columns,
32  const CompilationOptions& co) {
34  if (!expr) {
35  return {posArg(expr)};
36  }
37  auto bin_oper = dynamic_cast<const Analyzer::BinOper*>(expr);
38  if (bin_oper) {
39  return {codegen(bin_oper, co)};
40  }
41  auto u_oper = dynamic_cast<const Analyzer::UOper*>(expr);
42  if (u_oper) {
43  return {codegen(u_oper, co)};
44  }
45  auto geo_col_var = dynamic_cast<const Analyzer::GeoColumnVar*>(expr);
46  if (geo_col_var) {
47  // inherits from ColumnVar, so it is important we check this first
48  return codegenGeoColumnVar(geo_col_var, fetch_columns, co);
49  }
50  auto col_var = dynamic_cast<const Analyzer::ColumnVar*>(expr);
51  if (col_var) {
52  return codegenColumn(col_var, fetch_columns, co);
53  }
54  auto constant = dynamic_cast<const Analyzer::Constant*>(expr);
55  if (constant) {
56  const auto& ti = constant->get_type_info();
57  if (ti.get_type() == kNULLT) {
58  throw std::runtime_error(
59  "NULL type literals are not currently supported in this context.");
60  }
61  if (constant->get_is_null()) {
62  return {ti.is_fp()
63  ? static_cast<llvm::Value*>(executor_->cgen_state_->inlineFpNull(ti))
64  : static_cast<llvm::Value*>(executor_->cgen_state_->inlineIntNull(ti))};
65  }
66  if (ti.get_compression() == kENCODING_DICT) {
67  // The dictionary encoding case should be handled by the parent expression
68  // (cast, for now), here is too late to know the dictionary id if not already set
69  CHECK_NE(ti.get_comp_param(), 0);
70  return {codegen(constant, ti.get_compression(), ti.get_comp_param(), co)};
71  }
72  return {codegen(constant, ti.get_compression(), 0, co)};
73  }
74  auto case_expr = dynamic_cast<const Analyzer::CaseExpr*>(expr);
75  if (case_expr) {
76  return {codegen(case_expr, co)};
77  }
78  auto extract_expr = dynamic_cast<const Analyzer::ExtractExpr*>(expr);
79  if (extract_expr) {
80  return {codegen(extract_expr, co)};
81  }
82  auto dateadd_expr = dynamic_cast<const Analyzer::DateaddExpr*>(expr);
83  if (dateadd_expr) {
84  return {codegen(dateadd_expr, co)};
85  }
86  auto datediff_expr = dynamic_cast<const Analyzer::DatediffExpr*>(expr);
87  if (datediff_expr) {
88  return {codegen(datediff_expr, co)};
89  }
90  auto datetrunc_expr = dynamic_cast<const Analyzer::DatetruncExpr*>(expr);
91  if (datetrunc_expr) {
92  return {codegen(datetrunc_expr, co)};
93  }
94  auto charlength_expr = dynamic_cast<const Analyzer::CharLengthExpr*>(expr);
95  if (charlength_expr) {
96  return {codegen(charlength_expr, co)};
97  }
98  auto keyforstring_expr = dynamic_cast<const Analyzer::KeyForStringExpr*>(expr);
99  if (keyforstring_expr) {
100  return {codegen(keyforstring_expr, co)};
101  }
102  auto sample_ratio_expr = dynamic_cast<const Analyzer::SampleRatioExpr*>(expr);
103  if (sample_ratio_expr) {
104  return {codegen(sample_ratio_expr, co)};
105  }
106  auto string_oper_expr = dynamic_cast<const Analyzer::StringOper*>(expr);
107  if (string_oper_expr) {
108  return {codegen(string_oper_expr, co)};
109  }
110  auto cardinality_expr = dynamic_cast<const Analyzer::CardinalityExpr*>(expr);
111  if (cardinality_expr) {
112  return {codegen(cardinality_expr, co)};
113  }
114  auto like_expr = dynamic_cast<const Analyzer::LikeExpr*>(expr);
115  if (like_expr) {
116  return {codegen(like_expr, co)};
117  }
118  auto regexp_expr = dynamic_cast<const Analyzer::RegexpExpr*>(expr);
119  if (regexp_expr) {
120  return {codegen(regexp_expr, co)};
121  }
122  auto width_bucket_expr = dynamic_cast<const Analyzer::WidthBucketExpr*>(expr);
123  if (width_bucket_expr) {
124  return {codegen(width_bucket_expr, co)};
125  }
126  auto likelihood_expr = dynamic_cast<const Analyzer::LikelihoodExpr*>(expr);
127  if (likelihood_expr) {
128  return {codegen(likelihood_expr->get_arg(), fetch_columns, co)};
129  }
130  auto in_expr = dynamic_cast<const Analyzer::InValues*>(expr);
131  if (in_expr) {
132  return {codegen(in_expr, co)};
133  }
134  auto in_integer_set_expr = dynamic_cast<const Analyzer::InIntegerSet*>(expr);
135  if (in_integer_set_expr) {
136  return {codegen(in_integer_set_expr, co)};
137  }
138  auto function_oper_with_custom_type_handling_expr =
139  dynamic_cast<const Analyzer::FunctionOperWithCustomTypeHandling*>(expr);
140  if (function_oper_with_custom_type_handling_expr) {
142  function_oper_with_custom_type_handling_expr, co)};
143  }
144  auto array_oper_expr = dynamic_cast<const Analyzer::ArrayExpr*>(expr);
145  if (array_oper_expr) {
146  return {codegenArrayExpr(array_oper_expr, co)};
147  }
148  auto geo_uop = dynamic_cast<const Analyzer::GeoUOper*>(expr);
149  if (geo_uop) {
150  return {codegenGeoUOper(geo_uop, co)};
151  }
152  auto geo_binop = dynamic_cast<const Analyzer::GeoBinOper*>(expr);
153  if (geo_binop) {
154  return {codegenGeoBinOper(geo_binop, co)};
155  }
156  auto function_oper_expr = dynamic_cast<const Analyzer::FunctionOper*>(expr);
157  if (function_oper_expr) {
158  return {codegenFunctionOper(function_oper_expr, co)};
159  }
160  auto geo_expr = dynamic_cast<const Analyzer::GeoExpr*>(expr);
161  if (geo_expr) {
162  return codegenGeoExpr(geo_expr, co);
163  }
164  if (dynamic_cast<const Analyzer::OffsetInFragment*>(expr)) {
165  return {posArg(nullptr)};
166  }
167  if (dynamic_cast<const Analyzer::WindowFunction*>(expr)) {
168  throw NativeExecutionError("Window expression not supported in this context");
169  }
170  abort();
171 }
172 
173 llvm::Value* CodeGenerator::codegen(const Analyzer::BinOper* bin_oper,
174  const CompilationOptions& co) {
176  const auto optype = bin_oper->get_optype();
177  if (IS_ARITHMETIC(optype)) {
178  return codegenArith(bin_oper, co);
179  }
180  if (IS_COMPARISON(optype)) {
181  return codegenCmp(bin_oper, co);
182  }
183  if (IS_LOGIC(optype)) {
184  return codegenLogical(bin_oper, co);
185  }
186  if (optype == kARRAY_AT) {
187  return codegenArrayAt(bin_oper, co);
188  }
189  abort();
190 }
191 
192 llvm::Value* CodeGenerator::codegen(const Analyzer::UOper* u_oper,
193  const CompilationOptions& co) {
195  const auto optype = u_oper->get_optype();
196  switch (optype) {
197  case kNOT: {
198  return codegenLogical(u_oper, co);
199  }
200  case kCAST: {
201  return codegenCast(u_oper, co);
202  }
203  case kUMINUS: {
204  return codegenUMinus(u_oper, co);
205  }
206  case kISNULL: {
207  return codegenIsNull(u_oper, co);
208  }
209  case kUNNEST:
210  return codegenUnnest(u_oper, co);
211  default:
212  UNREACHABLE();
213  }
214  return nullptr;
215 }
216 
218  const CompilationOptions& co) {
220  auto input_expr = expr->get_arg();
221  CHECK(input_expr);
222 
223  auto double_lv = codegen(input_expr, true, co);
224  CHECK_EQ(size_t(1), double_lv.size());
225 
226  std::unique_ptr<CodeGenerator::NullCheckCodegen> nullcheck_codegen;
227  const bool is_nullable = !input_expr->get_type_info().get_notnull();
228  if (is_nullable) {
229  nullcheck_codegen = std::make_unique<NullCheckCodegen>(cgen_state_,
230  executor(),
231  double_lv.front(),
232  input_expr->get_type_info(),
233  "sample_ratio_nullcheck");
234  }
235  CHECK_EQ(input_expr->get_type_info().get_type(), kDOUBLE);
236  std::vector<llvm::Value*> args{double_lv[0], posArg(nullptr)};
237  auto ret = cgen_state_->emitCall("sample_ratio", args);
238  if (nullcheck_codegen) {
239  ret = nullcheck_codegen->finalize(ll_bool(false, cgen_state_->context_), ret);
240  }
241  return ret;
242 }
243 
245  const CompilationOptions& co) {
247  auto target_value_expr = expr->get_target_value();
248  auto lower_bound_expr = expr->get_lower_bound();
249  auto upper_bound_expr = expr->get_upper_bound();
250  auto partition_count_expr = expr->get_partition_count();
251  CHECK(target_value_expr);
252  CHECK(lower_bound_expr);
253  CHECK(upper_bound_expr);
254  CHECK(partition_count_expr);
255 
256  auto is_constant_expr = [](const Analyzer::Expr* expr) {
257  auto target_expr = expr;
258  if (auto cast_expr = dynamic_cast<const Analyzer::UOper*>(expr)) {
259  if (cast_expr->get_optype() == SQLOps::kCAST) {
260  target_expr = cast_expr->get_operand();
261  }
262  }
263  // there are more complex constant expr like 1+2, 1/2*3, and so on
264  // but when considering a typical usage of width_bucket function
265  // it is sufficient to consider a singleton constant expr
266  auto constant_expr = dynamic_cast<const Analyzer::Constant*>(target_expr);
267  if (constant_expr) {
268  return true;
269  }
270  return false;
271  };
272  if (is_constant_expr(lower_bound_expr) && is_constant_expr(upper_bound_expr) &&
273  is_constant_expr(partition_count_expr)) {
274  expr->set_constant_expr();
275  }
276  // compute width_bucket's expresn range and check the possibility of avoiding oob check
277  auto col_range =
278  getExpressionRange(expr,
280  executor_,
281  boost::make_optional(plan_state_->getSimpleQuals()));
282  // check whether target_expr is valid
283  if (col_range.getType() == ExpressionRangeType::Integer &&
284  !expr->can_skip_out_of_bound_check() && col_range.getIntMin() > 0 &&
285  col_range.getIntMax() <= expr->get_partition_count_val()) {
286  // check whether target_col is not-nullable or has filter expr on it
287  if (!col_range.hasNulls()) {
288  // Even if the target_expr has its filter expression, target_col_range may exactly
289  // the same with the col_range of the target_expr's operand col,
290  // i.e., SELECT WIDTH_BUCKET(v1, 1, 10, 10) FROM T WHERE v1 != 1;
291  // In that query, col_range of v1 with/without considering the filter expression
292  // v1 != 1 have exactly the same col ranges, so we cannot recognize the existence
293  // of the filter expression based on them. Also, is (not) null is located in
294  // FilterNode, so we cannot trace it in here.
295  // todo (yoonmin): relax this to allow skipping oob check more cases
296  expr->skip_out_of_bound_check();
297  }
298  }
299  return expr->is_constant_expr() ? codegenConstantWidthBucketExpr(expr, co)
300  : codegenWidthBucketExpr(expr, co);
301 }
302 
304  const Analyzer::WidthBucketExpr* expr,
305  const CompilationOptions& co) {
306  auto target_value_expr = expr->get_target_value();
307  auto lower_bound_expr = expr->get_lower_bound();
308  auto upper_bound_expr = expr->get_upper_bound();
309  auto partition_count_expr = expr->get_partition_count();
310 
311  auto num_partitions = expr->get_partition_count_val();
312  if (num_partitions < 1 || num_partitions > INT32_MAX) {
313  throw std::runtime_error(
314  "PARTITION_COUNT expression of width_bucket function should be in a valid "
315  "range: 0 < PARTITION_COUNT <= 2147483647");
316  }
317  double lower = expr->get_bound_val(lower_bound_expr);
318  double upper = expr->get_bound_val(upper_bound_expr);
319  if (lower == upper) {
320  throw std::runtime_error(
321  "LOWER_BOUND and UPPER_BOUND expressions of width_bucket function cannot have "
322  "the same constant value");
323  }
324  if (lower == NULL_DOUBLE || upper == NULL_DOUBLE) {
325  throw std::runtime_error(
326  "Both LOWER_BOUND and UPPER_BOUND of width_bucket function should be finite "
327  "numeric constants.");
328  }
329 
330  bool const reversed = lower > upper;
331  double const scale_factor = num_partitions / (reversed ? lower - upper : upper - lower);
332  std::string func_name = reversed ? "width_bucket_reversed" : "width_bucket";
333 
334  auto get_double_constant_lvs = [this, &co](double const_val) {
335  Datum d;
336  d.doubleval = const_val;
337  auto double_const_expr =
338  makeExpr<Analyzer::Constant>(SQLTypeInfo(kDOUBLE, false), false, d);
339  return codegen(double_const_expr.get(), false, co);
340  };
341 
342  auto target_value_ti = target_value_expr->get_type_info();
343  auto target_value_expr_lvs = codegen(target_value_expr, true, co);
344  CHECK_EQ(size_t(1), target_value_expr_lvs.size());
345  auto lower_expr_lvs = codegen(lower_bound_expr, true, co);
346  CHECK_EQ(size_t(1), lower_expr_lvs.size());
347  auto scale_factor_lvs = get_double_constant_lvs(scale_factor);
348  CHECK_EQ(size_t(1), scale_factor_lvs.size());
349 
350  std::vector<llvm::Value*> width_bucket_args{target_value_expr_lvs[0],
351  lower_expr_lvs[0]};
352  if (expr->can_skip_out_of_bound_check()) {
353  func_name += "_no_oob_check";
354  width_bucket_args.push_back(scale_factor_lvs[0]);
355  } else {
356  auto upper_expr_lvs = codegen(upper_bound_expr, true, co);
357  CHECK_EQ(size_t(1), upper_expr_lvs.size());
358  auto partition_count_expr_lvs = codegen(partition_count_expr, true, co);
359  CHECK_EQ(size_t(1), partition_count_expr_lvs.size());
360  width_bucket_args.push_back(upper_expr_lvs[0]);
361  width_bucket_args.push_back(scale_factor_lvs[0]);
362  width_bucket_args.push_back(partition_count_expr_lvs[0]);
363  if (!target_value_ti.get_notnull()) {
364  func_name += "_nullable";
365  auto translated_null_value = target_value_ti.is_fp()
366  ? inline_fp_null_val(target_value_ti)
367  : inline_int_null_val(target_value_ti);
368  auto null_value_lvs = get_double_constant_lvs(translated_null_value);
369  CHECK_EQ(size_t(1), null_value_lvs.size());
370  width_bucket_args.push_back(null_value_lvs[0]);
371  }
372  }
373  return cgen_state_->emitCall(func_name, width_bucket_args);
374 }
375 
377  const CompilationOptions& co) {
378  auto target_value_expr = expr->get_target_value();
379  auto lower_bound_expr = expr->get_lower_bound();
380  auto upper_bound_expr = expr->get_upper_bound();
381  auto partition_count_expr = expr->get_partition_count();
382 
383  std::string func_name = "width_bucket_expr";
384  bool nullable_expr = false;
385  if (expr->can_skip_out_of_bound_check()) {
386  func_name += "_no_oob_check";
387  } else if (!target_value_expr->get_type_info().get_notnull()) {
388  func_name += "_nullable";
389  nullable_expr = true;
390  }
391 
392  auto target_value_expr_lvs = codegen(target_value_expr, true, co);
393  CHECK_EQ(size_t(1), target_value_expr_lvs.size());
394  auto lower_bound_expr_lvs = codegen(lower_bound_expr, true, co);
395  CHECK_EQ(size_t(1), lower_bound_expr_lvs.size());
396  auto upper_bound_expr_lvs = codegen(upper_bound_expr, true, co);
397  CHECK_EQ(size_t(1), upper_bound_expr_lvs.size());
398  auto partition_count_expr_lvs = codegen(partition_count_expr, true, co);
399  CHECK_EQ(size_t(1), partition_count_expr_lvs.size());
400  auto target_value_ti = target_value_expr->get_type_info();
401  auto null_value_lv = cgen_state_->inlineFpNull(target_value_ti);
402 
403  // check partition count : 1 ~ INT32_MAX
404  // INT32_MAX will be checked during casting by OVERFLOW checking step
405  auto partition_count_ti = partition_count_expr->get_type_info();
406  CHECK(partition_count_ti.is_integer());
407  auto int32_ti = SQLTypeInfo(kINT, partition_count_ti.get_notnull());
408  auto partition_count_expr_lv =
409  codegenCastBetweenIntTypes(partition_count_expr_lvs[0],
410  partition_count_ti,
411  int32_ti,
412  partition_count_ti.get_size() < int32_ti.get_size());
413  llvm::Value* chosen_min = cgen_state_->llInt(static_cast<int32_t>(0));
414  llvm::Value* partition_count_min =
415  cgen_state_->ir_builder_.CreateICmpSLE(partition_count_expr_lv, chosen_min);
416  llvm::BasicBlock* width_bucket_partition_count_ok_bb =
417  llvm::BasicBlock::Create(cgen_state_->context_,
418  "width_bucket_partition_count_ok_bb",
420  llvm::BasicBlock* width_bucket_argument_check_fail_bb =
421  llvm::BasicBlock::Create(cgen_state_->context_,
422  "width_bucket_argument_check_fail_bb",
424  cgen_state_->ir_builder_.CreateCondBr(partition_count_min,
425  width_bucket_argument_check_fail_bb,
426  width_bucket_partition_count_ok_bb);
427  cgen_state_->ir_builder_.SetInsertPoint(width_bucket_argument_check_fail_bb);
428  cgen_state_->ir_builder_.CreateRet(
430  cgen_state_->ir_builder_.SetInsertPoint(width_bucket_partition_count_ok_bb);
431 
432  llvm::BasicBlock* width_bucket_bound_check_ok_bb =
433  llvm::BasicBlock::Create(cgen_state_->context_,
434  "width_bucket_bound_check_ok_bb",
436  llvm::Value* bound_check{nullptr};
437  if (lower_bound_expr->get_type_info().get_notnull() &&
438  upper_bound_expr->get_type_info().get_notnull()) {
439  bound_check = cgen_state_->ir_builder_.CreateFCmpOEQ(
440  lower_bound_expr_lvs[0], upper_bound_expr_lvs[0], "bound_check");
441  } else {
442  std::vector<llvm::Value*> bound_check_args{
443  lower_bound_expr_lvs[0],
444  upper_bound_expr_lvs[0],
445  null_value_lv,
446  cgen_state_->llInt(static_cast<int8_t>(1))};
447  bound_check = toBool(cgen_state_->emitCall("eq_double_nullable", bound_check_args));
448  }
449  cgen_state_->ir_builder_.CreateCondBr(
450  bound_check, width_bucket_argument_check_fail_bb, width_bucket_bound_check_ok_bb);
451  cgen_state_->ir_builder_.SetInsertPoint(width_bucket_bound_check_ok_bb);
453  auto reversed_expr = toBool(codegenCmp(SQLOps::kGT,
454  kONE,
455  lower_bound_expr_lvs,
456  lower_bound_expr->get_type_info(),
457  upper_bound_expr,
458  co));
459  auto lower_bound_expr_lv = lower_bound_expr_lvs[0];
460  auto upper_bound_expr_lv = upper_bound_expr_lvs[0];
461  std::vector<llvm::Value*> width_bucket_args{target_value_expr_lvs[0],
462  reversed_expr,
463  lower_bound_expr_lv,
464  upper_bound_expr_lv,
465  partition_count_expr_lv};
466  if (nullable_expr) {
467  width_bucket_args.push_back(null_value_lv);
468  }
469  return cgen_state_->emitCall(func_name, width_bucket_args);
470 }
471 
472 namespace {
473 
475  const std::shared_ptr<Analyzer::Expr>& qual) {
476  const auto qual_cf = qual_to_conjunctive_form(qual);
477  ra_exe_unit.simple_quals.insert(ra_exe_unit.simple_quals.end(),
478  qual_cf.simple_quals.begin(),
479  qual_cf.simple_quals.end());
480  ra_exe_unit.quals.insert(
481  ra_exe_unit.quals.end(), qual_cf.quals.begin(), qual_cf.quals.end());
482 }
483 
485  const ExecutionOptions& eo,
486  const std::vector<InputTableInfo>& query_infos,
487  const size_t level_idx,
488  const std::string& fail_reason) {
489  if (eo.allow_loop_joins) {
490  return;
491  }
492  if (level_idx + 1 != ra_exe_unit.join_quals.size()) {
493  throw std::runtime_error(
494  "Hash join failed, reason(s): " + fail_reason +
495  " | Cannot fall back to loop join for intermediate join quals");
496  }
497  if (!is_trivial_loop_join(query_infos, ra_exe_unit)) {
498  throw std::runtime_error(
499  "Hash join failed, reason(s): " + fail_reason +
500  " | Cannot fall back to loop join for non-trivial inner table size");
501  }
502 }
503 
504 void check_valid_join_qual(std::shared_ptr<Analyzer::BinOper>& bin_oper) {
505  // check whether a join qual is valid before entering the hashtable build and codegen
506 
507  auto lhs_cv = dynamic_cast<const Analyzer::ColumnVar*>(bin_oper->get_left_operand());
508  auto rhs_cv = dynamic_cast<const Analyzer::ColumnVar*>(bin_oper->get_right_operand());
509  if (lhs_cv && rhs_cv && !bin_oper->is_overlaps_oper()) {
510  auto lhs_type = lhs_cv->get_type_info().get_type();
511  auto rhs_type = rhs_cv->get_type_info().get_type();
512  // check #1. avoid a join btw full array columns
513  if (lhs_type == SQLTypes::kARRAY && rhs_type == SQLTypes::kARRAY) {
514  throw std::runtime_error(
515  "Join operation between full array columns (i.e., R.arr = S.arr) instead of "
516  "indexed array columns (i.e., R.arr[1] = S.arr[2]) is not supported yet.");
517  }
518  }
519 }
520 
521 } // namespace
522 
523 std::vector<JoinLoop> Executor::buildJoinLoops(
524  RelAlgExecutionUnit& ra_exe_unit,
525  const CompilationOptions& co,
526  const ExecutionOptions& eo,
527  const std::vector<InputTableInfo>& query_infos,
528  ColumnCacheMap& column_cache) {
529  INJECT_TIMER(buildJoinLoops);
531  std::vector<JoinLoop> join_loops;
532  for (size_t level_idx = 0, current_hash_table_idx = 0;
533  level_idx < ra_exe_unit.join_quals.size();
534  ++level_idx) {
535  const auto& current_level_join_conditions = ra_exe_unit.join_quals[level_idx];
536  std::vector<std::string> fail_reasons;
537  const auto current_level_hash_table =
538  buildCurrentLevelHashTable(current_level_join_conditions,
539  level_idx,
540  ra_exe_unit,
541  co,
542  query_infos,
543  column_cache,
544  fail_reasons);
545  const auto found_outer_join_matches_cb =
546  [this, level_idx](llvm::Value* found_outer_join_matches) {
550  found_outer_join_matches;
551  };
552  const auto is_deleted_cb = buildIsDeletedCb(ra_exe_unit, level_idx, co);
553  auto rem_left_join_quals_it =
555  bool has_remaining_left_join_quals =
556  rem_left_join_quals_it != plan_state_->left_join_non_hashtable_quals_.end() &&
557  !rem_left_join_quals_it->second.empty();
558  const auto outer_join_condition_remaining_quals_cb =
559  [this, level_idx, &co](const std::vector<llvm::Value*>& prev_iters) {
560  // when we have multiple quals for the left join in the current join level
561  // we first try to build a hashtable by using one of the possible qual,
562  // and deal with remaining quals as extra join conditions
563  FetchCacheAnchor anchor(cgen_state_.get());
564  addJoinLoopIterator(prev_iters, level_idx + 1);
565  llvm::Value* left_join_cond = cgen_state_->llBool(true);
566  CodeGenerator code_generator(this);
567  auto it = plan_state_->left_join_non_hashtable_quals_.find(level_idx);
568  if (it != plan_state_->left_join_non_hashtable_quals_.end()) {
569  for (auto expr : it->second) {
570  left_join_cond = cgen_state_->ir_builder_.CreateAnd(
571  left_join_cond,
572  code_generator.toBool(
573  code_generator.codegen(expr.get(), true, co).front()));
574  }
575  }
576  return left_join_cond;
577  };
578  if (current_level_hash_table) {
579  const auto hoisted_filters_cb = buildHoistLeftHandSideFiltersCb(
580  ra_exe_unit, level_idx, current_level_hash_table->getInnerTableId(), co);
581  if (current_level_hash_table->getHashType() == HashType::OneToOne) {
582  join_loops.emplace_back(
583  /*kind=*/JoinLoopKind::Singleton,
584  /*type=*/current_level_join_conditions.type,
585  /*iteration_domain_codegen=*/
586  [this, current_hash_table_idx, level_idx, current_level_hash_table, &co](
587  const std::vector<llvm::Value*>& prev_iters) {
588  addJoinLoopIterator(prev_iters, level_idx);
589  JoinLoopDomain domain{{0}};
590  domain.slot_lookup_result =
591  current_level_hash_table->codegenSlot(co, current_hash_table_idx);
592  return domain;
593  },
594  /*outer_condition_match=*/
595  current_level_join_conditions.type == JoinType::LEFT &&
596  has_remaining_left_join_quals
597  ? std::function<llvm::Value*(const std::vector<llvm::Value*>&)>(
598  outer_join_condition_remaining_quals_cb)
599  : nullptr,
600  /*found_outer_matches=*/current_level_join_conditions.type == JoinType::LEFT
601  ? std::function<void(llvm::Value*)>(found_outer_join_matches_cb)
602  : nullptr,
603  /*hoisted_filters=*/hoisted_filters_cb,
604  /*is_deleted=*/is_deleted_cb,
605  /*nested_loop_join=*/false);
606  } else if (auto range_join_table =
607  dynamic_cast<RangeJoinHashTable*>(current_level_hash_table.get())) {
608  join_loops.emplace_back(
609  /* kind= */ JoinLoopKind::MultiSet,
610  /* type= */ current_level_join_conditions.type,
611  /* iteration_domain_codegen= */
612  [this,
613  range_join_table,
614  current_hash_table_idx,
615  level_idx,
616  current_level_hash_table,
617  &co](const std::vector<llvm::Value*>& prev_iters) {
618  addJoinLoopIterator(prev_iters, level_idx);
619  JoinLoopDomain domain{{0}};
620  CHECK(!prev_iters.empty());
621  const auto matching_set = range_join_table->codegenMatchingSetWithOffset(
622  co, current_hash_table_idx, prev_iters.back());
623  domain.values_buffer = matching_set.elements;
624  domain.element_count = matching_set.count;
625  return domain;
626  },
627  /* outer_condition_match= */
628  current_level_join_conditions.type == JoinType::LEFT
629  ? std::function<llvm::Value*(const std::vector<llvm::Value*>&)>(
630  outer_join_condition_remaining_quals_cb)
631  : nullptr,
632  /* found_outer_matches= */
633  current_level_join_conditions.type == JoinType::LEFT
634  ? std::function<void(llvm::Value*)>(found_outer_join_matches_cb)
635  : nullptr,
636  /* hoisted_filters= */ nullptr, // <<! TODO
637  /* is_deleted= */ is_deleted_cb,
638  /*nested_loop_join=*/false);
639  } else {
640  join_loops.emplace_back(
641  /*kind=*/JoinLoopKind::Set,
642  /*type=*/current_level_join_conditions.type,
643  /*iteration_domain_codegen=*/
644  [this, current_hash_table_idx, level_idx, current_level_hash_table, &co](
645  const std::vector<llvm::Value*>& prev_iters) {
646  addJoinLoopIterator(prev_iters, level_idx);
647  JoinLoopDomain domain{{0}};
648  const auto matching_set = current_level_hash_table->codegenMatchingSet(
649  co, current_hash_table_idx);
650  domain.values_buffer = matching_set.elements;
651  domain.element_count = matching_set.count;
652  return domain;
653  },
654  /*outer_condition_match=*/
655  current_level_join_conditions.type == JoinType::LEFT
656  ? std::function<llvm::Value*(const std::vector<llvm::Value*>&)>(
657  outer_join_condition_remaining_quals_cb)
658  : nullptr,
659  /*found_outer_matches=*/current_level_join_conditions.type == JoinType::LEFT
660  ? std::function<void(llvm::Value*)>(found_outer_join_matches_cb)
661  : nullptr,
662  /*hoisted_filters=*/hoisted_filters_cb,
663  /*is_deleted=*/is_deleted_cb,
664  /*nested_loop_join=*/false);
665  }
666  ++current_hash_table_idx;
667  } else {
668  const auto fail_reasons_str = current_level_join_conditions.quals.empty()
669  ? "No equijoin expression found"
670  : boost::algorithm::join(fail_reasons, " | ");
672  ra_exe_unit, eo, query_infos, level_idx, fail_reasons_str);
673  // Callback provided to the `JoinLoop` framework to evaluate the (outer) join
674  // condition.
675  VLOG(1) << "Unable to build hash table, falling back to loop join: "
676  << fail_reasons_str;
677  const auto outer_join_condition_cb =
678  [this, level_idx, &co, &current_level_join_conditions](
679  const std::vector<llvm::Value*>& prev_iters) {
680  // The values generated for the match path don't dominate all uses
681  // since on the non-match path nulls are generated. Reset the cache
682  // once the condition is generated to avoid incorrect reuse.
683  FetchCacheAnchor anchor(cgen_state_.get());
684  addJoinLoopIterator(prev_iters, level_idx + 1);
685  llvm::Value* left_join_cond = cgen_state_->llBool(true);
686  CodeGenerator code_generator(this);
687  for (auto expr : current_level_join_conditions.quals) {
688  left_join_cond = cgen_state_->ir_builder_.CreateAnd(
689  left_join_cond,
690  code_generator.toBool(
691  code_generator.codegen(expr.get(), true, co).front()));
692  }
693  return left_join_cond;
694  };
695  join_loops.emplace_back(
696  /*kind=*/JoinLoopKind::UpperBound,
697  /*type=*/current_level_join_conditions.type,
698  /*iteration_domain_codegen=*/
699  [this, level_idx](const std::vector<llvm::Value*>& prev_iters) {
700  addJoinLoopIterator(prev_iters, level_idx);
701  JoinLoopDomain domain{{0}};
702  auto* arg = get_arg_by_name(cgen_state_->row_func_, "num_rows_per_scan");
703  const auto rows_per_scan_ptr = cgen_state_->ir_builder_.CreateGEP(
704  arg->getType()->getScalarType()->getPointerElementType(),
705  arg,
706  cgen_state_->llInt(int32_t(level_idx + 1)));
707  domain.upper_bound = cgen_state_->ir_builder_.CreateLoad(
708  rows_per_scan_ptr->getType()->getPointerElementType(),
709  rows_per_scan_ptr,
710  "num_rows_per_scan");
711  return domain;
712  },
713  /*outer_condition_match=*/
714  current_level_join_conditions.type == JoinType::LEFT
715  ? std::function<llvm::Value*(const std::vector<llvm::Value*>&)>(
716  outer_join_condition_cb)
717  : nullptr,
718  /*found_outer_matches=*/
719  current_level_join_conditions.type == JoinType::LEFT
720  ? std::function<void(llvm::Value*)>(found_outer_join_matches_cb)
721  : nullptr,
722  /*hoisted_filters=*/nullptr,
723  /*is_deleted=*/is_deleted_cb,
724  /*nested_loop_join=*/true);
725  }
726  }
727  return join_loops;
728 }
729 
730 namespace {
731 
732 class ExprTableIdVisitor : public ScalarExprVisitor<std::set<int>> {
733  protected:
734  std::set<int> visitColumnVar(const Analyzer::ColumnVar* col_expr) const final {
735  return {col_expr->get_table_id()};
736  }
737 
738  std::set<int> visitFunctionOper(const Analyzer::FunctionOper* func_expr) const final {
739  std::set<int> ret;
740  for (size_t i = 0; i < func_expr->getArity(); i++) {
741  ret = aggregateResult(ret, visit(func_expr->getArg(i)));
742  }
743  return ret;
744  }
745 
746  std::set<int> visitBinOper(const Analyzer::BinOper* bin_oper) const final {
747  std::set<int> ret;
748  ret = aggregateResult(ret, visit(bin_oper->get_left_operand()));
749  return aggregateResult(ret, visit(bin_oper->get_right_operand()));
750  }
751 
752  std::set<int> visitUOper(const Analyzer::UOper* u_oper) const final {
753  return visit(u_oper->get_operand());
754  }
755 
756  std::set<int> aggregateResult(const std::set<int>& aggregate,
757  const std::set<int>& next_result) const final {
758  auto ret = aggregate; // copy
759  for (const auto& el : next_result) {
760  ret.insert(el);
761  }
762  return ret;
763  }
764 };
765 
766 } // namespace
767 
769  const RelAlgExecutionUnit& ra_exe_unit,
770  const size_t level_idx,
771  const int inner_table_id,
772  const CompilationOptions& co) {
774  return nullptr;
775  }
776 
777  const auto& current_level_join_conditions = ra_exe_unit.join_quals[level_idx];
778  if (level_idx == 0 && current_level_join_conditions.type == JoinType::LEFT) {
779  const auto& condition = current_level_join_conditions.quals.front();
780  const auto bin_oper = dynamic_cast<const Analyzer::BinOper*>(condition.get());
781  CHECK(bin_oper) << condition->toString();
782  const auto rhs =
783  dynamic_cast<const Analyzer::ColumnVar*>(bin_oper->get_right_operand());
784  const auto lhs =
785  dynamic_cast<const Analyzer::ColumnVar*>(bin_oper->get_left_operand());
786  if (lhs && rhs && lhs->get_table_id() != rhs->get_table_id()) {
787  const Analyzer::ColumnVar* selected_lhs{nullptr};
788  // grab the left hand side column -- this is somewhat similar to normalize column
789  // pair, and a better solution may be to hoist that function out of the join
790  // framework and normalize columns at the top of build join loops
791  if (lhs->get_table_id() == inner_table_id) {
792  selected_lhs = rhs;
793  } else if (rhs->get_table_id() == inner_table_id) {
794  selected_lhs = lhs;
795  }
796  if (selected_lhs) {
797  std::list<std::shared_ptr<Analyzer::Expr>> hoisted_quals;
798  // get all LHS-only filters
799  auto should_hoist_qual = [&hoisted_quals](const auto& qual, const int table_id) {
800  CHECK(qual);
801 
802  ExprTableIdVisitor visitor;
803  const auto table_ids = visitor.visit(qual.get());
804  if (table_ids.size() == 1 && table_ids.find(table_id) != table_ids.end()) {
805  hoisted_quals.push_back(qual);
806  }
807  };
808  for (const auto& qual : ra_exe_unit.simple_quals) {
809  should_hoist_qual(qual, selected_lhs->get_table_id());
810  }
811  for (const auto& qual : ra_exe_unit.quals) {
812  should_hoist_qual(qual, selected_lhs->get_table_id());
813  }
814 
815  // build the filters callback and return it
816  if (!hoisted_quals.empty()) {
817  return [this, hoisted_quals, co](llvm::BasicBlock* true_bb,
818  llvm::BasicBlock* exit_bb,
819  const std::string& loop_name,
820  llvm::Function* parent_func,
821  CgenState* cgen_state) -> llvm::BasicBlock* {
822  // make sure we have quals to hoist
823  bool has_quals_to_hoist = false;
824  for (const auto& qual : hoisted_quals) {
825  // check to see if the filter was previously hoisted. if all filters were
826  // previously hoisted, this callback becomes a noop
827  if (plan_state_->hoisted_filters_.count(qual) == 0) {
828  has_quals_to_hoist = true;
829  break;
830  }
831  }
832 
833  if (!has_quals_to_hoist) {
834  return nullptr;
835  }
836 
837  AUTOMATIC_IR_METADATA(cgen_state);
838 
839  llvm::IRBuilder<>& builder = cgen_state->ir_builder_;
840  auto& context = builder.getContext();
841 
842  const auto filter_bb =
843  llvm::BasicBlock::Create(context,
844  "hoisted_left_join_filters_" + loop_name,
845  parent_func,
846  /*insert_before=*/true_bb);
847  builder.SetInsertPoint(filter_bb);
848 
849  llvm::Value* filter_lv = cgen_state_->llBool(true);
850  CodeGenerator code_generator(this);
851  CHECK(plan_state_);
852  for (const auto& qual : hoisted_quals) {
853  if (plan_state_->hoisted_filters_.insert(qual).second) {
854  // qual was inserted into the hoisted filters map, which means we have not
855  // seen this qual before. Generate filter.
856  VLOG(1) << "Generating code for hoisted left hand side qualifier "
857  << qual->toString();
858  auto cond = code_generator.toBool(
859  code_generator.codegen(qual.get(), true, co).front());
860  filter_lv = builder.CreateAnd(filter_lv, cond);
861  }
862  }
863  CHECK(filter_lv->getType()->isIntegerTy(1));
864 
865  builder.CreateCondBr(filter_lv, true_bb, exit_bb);
866  return filter_bb;
867  };
868  }
869  }
870  }
871  }
872  return nullptr;
873 }
874 
875 std::function<llvm::Value*(const std::vector<llvm::Value*>&, llvm::Value*)>
877  const size_t level_idx,
878  const CompilationOptions& co) {
879  AUTOMATIC_IR_METADATA(cgen_state_.get());
880  if (!co.filter_on_deleted_column) {
881  return nullptr;
882  }
883  CHECK_LT(level_idx + 1, ra_exe_unit.input_descs.size());
884  const auto input_desc = ra_exe_unit.input_descs[level_idx + 1];
885  if (input_desc.getSourceType() != InputSourceType::TABLE) {
886  return nullptr;
887  }
888 
889  const auto deleted_cd = plan_state_->getDeletedColForTable(input_desc.getTableId());
890  if (!deleted_cd) {
891  return nullptr;
892  }
893  CHECK(deleted_cd->columnType.is_boolean());
894  const auto deleted_expr = makeExpr<Analyzer::ColumnVar>(deleted_cd->columnType,
895  input_desc.getTableId(),
896  deleted_cd->columnId,
897  input_desc.getNestLevel());
898  return [this, deleted_expr, level_idx, &co](const std::vector<llvm::Value*>& prev_iters,
899  llvm::Value* have_more_inner_rows) {
900  const auto matching_row_index = addJoinLoopIterator(prev_iters, level_idx + 1);
901  // Avoid fetching the deleted column from a position which is not valid.
902  // An invalid position can be returned by a one to one hash lookup (negative)
903  // or at the end of iteration over a set of matching values.
904  llvm::Value* is_valid_it{nullptr};
905  if (have_more_inner_rows) {
906  is_valid_it = have_more_inner_rows;
907  } else {
908  is_valid_it = cgen_state_->ir_builder_.CreateICmp(
909  llvm::ICmpInst::ICMP_SGE, matching_row_index, cgen_state_->llInt<int64_t>(0));
910  }
911  const auto it_valid_bb = llvm::BasicBlock::Create(
912  cgen_state_->context_, "it_valid", cgen_state_->current_func_);
913  const auto it_not_valid_bb = llvm::BasicBlock::Create(
914  cgen_state_->context_, "it_not_valid", cgen_state_->current_func_);
915  cgen_state_->ir_builder_.CreateCondBr(is_valid_it, it_valid_bb, it_not_valid_bb);
916  const auto row_is_deleted_bb = llvm::BasicBlock::Create(
917  cgen_state_->context_, "row_is_deleted", cgen_state_->current_func_);
918  cgen_state_->ir_builder_.SetInsertPoint(it_valid_bb);
919  CodeGenerator code_generator(this);
920  const auto row_is_deleted = code_generator.toBool(
921  code_generator.codegen(deleted_expr.get(), true, co).front());
922  cgen_state_->ir_builder_.CreateBr(row_is_deleted_bb);
923  cgen_state_->ir_builder_.SetInsertPoint(it_not_valid_bb);
924  const auto row_is_deleted_default = cgen_state_->llBool(false);
925  cgen_state_->ir_builder_.CreateBr(row_is_deleted_bb);
926  cgen_state_->ir_builder_.SetInsertPoint(row_is_deleted_bb);
927  auto row_is_deleted_or_default =
928  cgen_state_->ir_builder_.CreatePHI(row_is_deleted->getType(), 2);
929  row_is_deleted_or_default->addIncoming(row_is_deleted, it_valid_bb);
930  row_is_deleted_or_default->addIncoming(row_is_deleted_default, it_not_valid_bb);
931  return row_is_deleted_or_default;
932  };
933 }
934 
935 std::shared_ptr<HashJoin> Executor::buildCurrentLevelHashTable(
936  const JoinCondition& current_level_join_conditions,
937  size_t level_idx,
938  RelAlgExecutionUnit& ra_exe_unit,
939  const CompilationOptions& co,
940  const std::vector<InputTableInfo>& query_infos,
941  ColumnCacheMap& column_cache,
942  std::vector<std::string>& fail_reasons) {
943  AUTOMATIC_IR_METADATA(cgen_state_.get());
944  std::shared_ptr<HashJoin> current_level_hash_table;
945  auto handleNonHashtableQual = [&ra_exe_unit, &level_idx, this](
946  JoinType join_type,
947  std::shared_ptr<Analyzer::Expr> qual) {
948  if (join_type == JoinType::LEFT) {
949  plan_state_->addNonHashtableQualForLeftJoin(level_idx, qual);
950  } else {
951  add_qualifier_to_execution_unit(ra_exe_unit, qual);
952  }
953  };
954  for (const auto& join_qual : current_level_join_conditions.quals) {
955  auto qual_bin_oper = std::dynamic_pointer_cast<Analyzer::BinOper>(join_qual);
956  if (current_level_hash_table || !qual_bin_oper ||
957  !IS_EQUIVALENCE(qual_bin_oper->get_optype())) {
958  handleNonHashtableQual(current_level_join_conditions.type, join_qual);
959  if (!current_level_hash_table) {
960  fail_reasons.emplace_back("No equijoin expression found");
961  }
962  continue;
963  }
964  check_valid_join_qual(qual_bin_oper);
965  JoinHashTableOrError hash_table_or_error;
966  if (!current_level_hash_table) {
967  hash_table_or_error = buildHashTableForQualifier(
968  qual_bin_oper,
969  query_infos,
972  current_level_join_conditions.type,
974  column_cache,
975  ra_exe_unit.hash_table_build_plan_dag,
976  ra_exe_unit.query_hint,
977  ra_exe_unit.table_id_to_node_map);
978  current_level_hash_table = hash_table_or_error.hash_table;
979  }
980  if (hash_table_or_error.hash_table) {
981  plan_state_->join_info_.join_hash_tables_.push_back(hash_table_or_error.hash_table);
982  plan_state_->join_info_.equi_join_tautologies_.push_back(qual_bin_oper);
983  } else {
984  fail_reasons.push_back(hash_table_or_error.fail_reason);
985  if (!current_level_hash_table) {
986  VLOG(2) << "Building a hashtable based on a qual " << qual_bin_oper->toString()
987  << " fails: " << hash_table_or_error.fail_reason;
988  }
989  handleNonHashtableQual(current_level_join_conditions.type, qual_bin_oper);
990  }
991  }
992  return current_level_hash_table;
993 }
994 
996  if (!cgen_state_->filter_func_) {
997  return;
998  }
999 
1000  // Loop over all the instructions used in the filter func.
1001  // The filter func instructions were generated as if for row func.
1002  // Remap any values used by those instructions to filter func args
1003  // and remember to forward them through the call in the row func.
1004  for (auto bb_it = cgen_state_->filter_func_->begin();
1005  bb_it != cgen_state_->filter_func_->end();
1006  ++bb_it) {
1007  for (auto instr_it = bb_it->begin(); instr_it != bb_it->end(); ++instr_it) {
1008  size_t i = 0;
1009  for (auto op_it = instr_it->value_op_begin(); op_it != instr_it->value_op_end();
1010  ++op_it, ++i) {
1011  llvm::Value* v = *op_it;
1012 
1013  // The last LLVM operand on a call instruction is the function to be called. Never
1014  // remap it.
1015  if (llvm::dyn_cast<const llvm::CallInst>(instr_it) &&
1016  op_it == instr_it->value_op_end() - 1) {
1017  continue;
1018  }
1019 
1020  CHECK(v);
1021  if (auto* instr = llvm::dyn_cast<llvm::Instruction>(v);
1022  instr && instr->getParent() &&
1023  instr->getParent()->getParent() == cgen_state_->row_func_) {
1024  // Remember that this filter func arg is needed.
1025  cgen_state_->filter_func_args_[v] = nullptr;
1026  } else if (auto* argum = llvm::dyn_cast<llvm::Argument>(v);
1027  argum && argum->getParent() == cgen_state_->row_func_) {
1028  // Remember that this filter func arg is needed.
1029  cgen_state_->filter_func_args_[v] = nullptr;
1030  }
1031  }
1032  }
1033  }
1034 
1035  // Create filter_func2 with parameters only for those row func values that are known to
1036  // be used in the filter func code.
1037  std::vector<llvm::Type*> filter_func_arg_types;
1038  filter_func_arg_types.reserve(cgen_state_->filter_func_args_.v_.size());
1039  for (auto& arg : cgen_state_->filter_func_args_.v_) {
1040  filter_func_arg_types.push_back(arg->getType());
1041  }
1042  auto ft = llvm::FunctionType::get(
1043  get_int_type(32, cgen_state_->context_), filter_func_arg_types, false);
1044  cgen_state_->filter_func_->setName("old_filter_func");
1045  auto filter_func2 = llvm::Function::Create(ft,
1046  llvm::Function::ExternalLinkage,
1047  "filter_func",
1048  cgen_state_->filter_func_->getParent());
1049  CHECK_EQ(filter_func2->arg_size(), cgen_state_->filter_func_args_.v_.size());
1050  auto arg_it = cgen_state_->filter_func_args_.begin();
1051  size_t i = 0;
1052  for (llvm::Function::arg_iterator I = filter_func2->arg_begin(),
1053  E = filter_func2->arg_end();
1054  I != E;
1055  ++I, ++arg_it) {
1056  arg_it->second = &*I;
1057  if (arg_it->first->hasName()) {
1058  I->setName(arg_it->first->getName());
1059  } else {
1060  I->setName("extra" + std::to_string(i++));
1061  }
1062  }
1063 
1064  // copy the filter_func function body over
1065  // see
1066  // https://stackoverflow.com/questions/12864106/move-function-body-avoiding-full-cloning/18751365
1067  filter_func2->getBasicBlockList().splice(
1068  filter_func2->begin(), cgen_state_->filter_func_->getBasicBlockList());
1069 
1070  if (cgen_state_->current_func_ == cgen_state_->filter_func_) {
1071  cgen_state_->current_func_ = filter_func2;
1072  }
1073  cgen_state_->filter_func_ = filter_func2;
1074 
1075  // loop over all the operands in the filter func
1076  for (auto bb_it = cgen_state_->filter_func_->begin();
1077  bb_it != cgen_state_->filter_func_->end();
1078  ++bb_it) {
1079  for (auto instr_it = bb_it->begin(); instr_it != bb_it->end(); ++instr_it) {
1080  size_t i = 0;
1081  for (auto op_it = instr_it->op_begin(); op_it != instr_it->op_end(); ++op_it, ++i) {
1082  llvm::Value* v = op_it->get();
1083  if (auto arg_it = cgen_state_->filter_func_args_.find(v);
1084  arg_it != cgen_state_->filter_func_args_.end()) {
1085  // replace row func value with a filter func arg
1086  llvm::Use* use = &*op_it;
1087  use->set(arg_it->second);
1088  }
1089  }
1090  }
1091  }
1092 }
1093 
1094 llvm::Value* Executor::addJoinLoopIterator(const std::vector<llvm::Value*>& prev_iters,
1095  const size_t level_idx) {
1096  AUTOMATIC_IR_METADATA(cgen_state_.get());
1097  // Iterators are added for loop-outer joins when the head of the loop is generated,
1098  // then once again when the body if generated. Allow this instead of special handling
1099  // of call sites.
1100  const auto it = cgen_state_->scan_idx_to_hash_pos_.find(level_idx);
1101  if (it != cgen_state_->scan_idx_to_hash_pos_.end()) {
1102  return it->second;
1103  }
1104  CHECK(!prev_iters.empty());
1105  llvm::Value* matching_row_index = prev_iters.back();
1106  const auto it_ok =
1107  cgen_state_->scan_idx_to_hash_pos_.emplace(level_idx, matching_row_index);
1108  CHECK(it_ok.second);
1109  return matching_row_index;
1110 }
1111 
1112 void Executor::codegenJoinLoops(const std::vector<JoinLoop>& join_loops,
1113  const RelAlgExecutionUnit& ra_exe_unit,
1114  GroupByAndAggregate& group_by_and_aggregate,
1115  llvm::Function* query_func,
1116  llvm::BasicBlock* entry_bb,
1118  const CompilationOptions& co,
1119  const ExecutionOptions& eo) {
1120  AUTOMATIC_IR_METADATA(cgen_state_.get());
1121  const auto exit_bb =
1122  llvm::BasicBlock::Create(cgen_state_->context_, "exit", cgen_state_->current_func_);
1123  cgen_state_->ir_builder_.SetInsertPoint(exit_bb);
1124  cgen_state_->ir_builder_.CreateRet(cgen_state_->llInt<int32_t>(0));
1125  cgen_state_->ir_builder_.SetInsertPoint(entry_bb);
1126  CodeGenerator code_generator(this);
1127 
1128  llvm::BasicBlock* loops_entry_bb{nullptr};
1129  auto has_range_join =
1130  std::any_of(join_loops.begin(), join_loops.end(), [](const auto& join_loop) {
1131  return join_loop.kind() == JoinLoopKind::MultiSet;
1132  });
1133  if (has_range_join) {
1134  CHECK_EQ(join_loops.size(), size_t(1));
1135  const auto element_count =
1136  llvm::ConstantInt::get(get_int_type(64, cgen_state_->context_), 9);
1137 
1138  auto compute_packed_offset = [](const int32_t x, const int32_t y) -> uint64_t {
1139  const uint64_t y_shifted = static_cast<uint64_t>(y) << 32;
1140  return y_shifted | static_cast<uint32_t>(x);
1141  };
1142 
1143  const auto values_arr = std::vector<llvm::Constant*>{
1144  llvm::ConstantInt::get(get_int_type(64, cgen_state_->context_), 0),
1145  llvm::ConstantInt::get(get_int_type(64, cgen_state_->context_),
1146  compute_packed_offset(0, 1)),
1147  llvm::ConstantInt::get(get_int_type(64, cgen_state_->context_),
1148  compute_packed_offset(0, -1)),
1149  llvm::ConstantInt::get(get_int_type(64, cgen_state_->context_),
1150  compute_packed_offset(1, 0)),
1151  llvm::ConstantInt::get(get_int_type(64, cgen_state_->context_),
1152  compute_packed_offset(1, 1)),
1153  llvm::ConstantInt::get(get_int_type(64, cgen_state_->context_),
1154  compute_packed_offset(1, -1)),
1155  llvm::ConstantInt::get(get_int_type(64, cgen_state_->context_),
1156  compute_packed_offset(-1, 0)),
1157  llvm::ConstantInt::get(get_int_type(64, cgen_state_->context_),
1158  compute_packed_offset(-1, 1)),
1159  llvm::ConstantInt::get(get_int_type(64, cgen_state_->context_),
1160  compute_packed_offset(-1, -1))};
1161 
1162  const auto constant_values_array = llvm::ConstantArray::get(
1163  get_int_array_type(64, 9, cgen_state_->context_), values_arr);
1164  CHECK(cgen_state_->module_);
1165  const auto values =
1166  new llvm::GlobalVariable(*cgen_state_->module_,
1167  get_int_array_type(64, 9, cgen_state_->context_),
1168  true,
1169  llvm::GlobalValue::LinkageTypes::InternalLinkage,
1170  constant_values_array);
1171  JoinLoop join_loop(
1174  [element_count, values](const std::vector<llvm::Value*>& v) {
1175  JoinLoopDomain domain{{0}};
1176  domain.element_count = element_count;
1177  domain.values_buffer = values;
1178  return domain;
1179  },
1180  nullptr,
1181  nullptr,
1182  nullptr,
1183  nullptr,
1184  "range_key_loop");
1185 
1186  loops_entry_bb = JoinLoop::codegen(
1187  {join_loop},
1188  [this,
1189  query_func,
1190  &query_mem_desc,
1191  &co,
1192  &eo,
1193  &group_by_and_aggregate,
1194  &join_loops,
1195  &ra_exe_unit](const std::vector<llvm::Value*>& prev_iters) {
1196  auto& builder = cgen_state_->ir_builder_;
1197 
1198  auto body_exit_bb =
1199  llvm::BasicBlock::Create(cgen_state_->context_,
1200  "range_key_inner_body_exit",
1201  builder.GetInsertBlock()->getParent());
1202 
1203  auto range_key_body_bb =
1204  llvm::BasicBlock::Create(cgen_state_->context_,
1205  "range_key_loop_body",
1206  builder.GetInsertBlock()->getParent());
1207  builder.SetInsertPoint(range_key_body_bb);
1208 
1209  const auto body_loops_entry_bb = JoinLoop::codegen(
1210  join_loops,
1211  [this,
1212  query_func,
1213  &query_mem_desc,
1214  &co,
1215  &eo,
1216  &group_by_and_aggregate,
1217  &join_loops,
1218  &ra_exe_unit](const std::vector<llvm::Value*>& prev_iters) {
1219  addJoinLoopIterator(prev_iters, join_loops.size());
1220  auto& builder = cgen_state_->ir_builder_;
1221  const auto loop_body_bb =
1222  llvm::BasicBlock::Create(builder.getContext(),
1223  "loop_body",
1224  builder.GetInsertBlock()->getParent());
1225  builder.SetInsertPoint(loop_body_bb);
1226  const bool can_return_error =
1227  compileBody(ra_exe_unit, group_by_and_aggregate, query_mem_desc, co);
1228  if (can_return_error || cgen_state_->needs_error_check_ ||
1229  eo.with_dynamic_watchdog || eo.allow_runtime_query_interrupt) {
1230  createErrorCheckControlFlow(query_func,
1231  eo.with_dynamic_watchdog,
1232  eo.allow_runtime_query_interrupt,
1233  join_loops,
1234  co.device_type,
1235  group_by_and_aggregate.query_infos_);
1236  }
1237  return loop_body_bb;
1238  },
1239  prev_iters.back(),
1240  body_exit_bb,
1241  cgen_state_.get());
1242 
1243  builder.SetInsertPoint(range_key_body_bb);
1244  cgen_state_->ir_builder_.CreateBr(body_loops_entry_bb);
1245 
1246  builder.SetInsertPoint(body_exit_bb);
1247  return range_key_body_bb;
1248  },
1249  code_generator.posArg(nullptr),
1250  exit_bb,
1251  cgen_state_.get());
1252  } else {
1253  loops_entry_bb = JoinLoop::codegen(
1254  join_loops,
1255  /*body_codegen=*/
1256  [this,
1257  query_func,
1258  &query_mem_desc,
1259  &co,
1260  &eo,
1261  &group_by_and_aggregate,
1262  &join_loops,
1263  &ra_exe_unit](const std::vector<llvm::Value*>& prev_iters) {
1264  AUTOMATIC_IR_METADATA(cgen_state_.get());
1265  addJoinLoopIterator(prev_iters, join_loops.size());
1266  auto& builder = cgen_state_->ir_builder_;
1267  const auto loop_body_bb = llvm::BasicBlock::Create(
1268  builder.getContext(), "loop_body", builder.GetInsertBlock()->getParent());
1269  builder.SetInsertPoint(loop_body_bb);
1270  const bool can_return_error =
1271  compileBody(ra_exe_unit, group_by_and_aggregate, query_mem_desc, co);
1272  if (can_return_error || cgen_state_->needs_error_check_ ||
1274  createErrorCheckControlFlow(query_func,
1277  join_loops,
1278  co.device_type,
1279  group_by_and_aggregate.query_infos_);
1280  }
1281  return loop_body_bb;
1282  },
1283  /*outer_iter=*/code_generator.posArg(nullptr),
1284  exit_bb,
1285  cgen_state_.get());
1286  }
1287  CHECK(loops_entry_bb);
1288  cgen_state_->ir_builder_.SetInsertPoint(entry_bb);
1289  cgen_state_->ir_builder_.CreateBr(loops_entry_bb);
1290 }
1291 
1293  Analyzer::Expr* group_by_col,
1294  const size_t col_width,
1295  const CompilationOptions& co,
1296  const bool translate_null_val,
1297  const int64_t translated_null_val,
1298  DiamondCodegen& diamond_codegen,
1299  std::stack<llvm::BasicBlock*>& array_loops,
1300  const bool thread_mem_shared) {
1301  AUTOMATIC_IR_METADATA(cgen_state_.get());
1302  CHECK_GE(col_width, sizeof(int32_t));
1303  CodeGenerator code_generator(this);
1304  auto group_key = code_generator.codegen(group_by_col, true, co).front();
1305  auto key_to_cache = group_key;
1306  if (dynamic_cast<Analyzer::UOper*>(group_by_col) &&
1307  static_cast<Analyzer::UOper*>(group_by_col)->get_optype() == kUNNEST) {
1308  auto preheader = cgen_state_->ir_builder_.GetInsertBlock();
1309  auto array_loop_head = llvm::BasicBlock::Create(cgen_state_->context_,
1310  "array_loop_head",
1311  cgen_state_->current_func_,
1312  preheader->getNextNode());
1313  diamond_codegen.setFalseTarget(array_loop_head);
1314  const auto ret_ty = get_int_type(32, cgen_state_->context_);
1315  auto array_idx_ptr = cgen_state_->ir_builder_.CreateAlloca(ret_ty);
1316  CHECK(array_idx_ptr);
1317  cgen_state_->ir_builder_.CreateStore(cgen_state_->llInt(int32_t(0)), array_idx_ptr);
1318  const auto arr_expr = static_cast<Analyzer::UOper*>(group_by_col)->get_operand();
1319  const auto& array_ti = arr_expr->get_type_info();
1320  CHECK(array_ti.is_array());
1321  const auto& elem_ti = array_ti.get_elem_type();
1322  auto array_len =
1323  (array_ti.get_size() > 0)
1324  ? cgen_state_->llInt(array_ti.get_size() / elem_ti.get_size())
1325  : cgen_state_->emitExternalCall(
1326  "array_size",
1327  ret_ty,
1328  {group_key,
1329  code_generator.posArg(arr_expr),
1330  cgen_state_->llInt(log2_bytes(elem_ti.get_logical_size()))});
1331  cgen_state_->ir_builder_.CreateBr(array_loop_head);
1332  cgen_state_->ir_builder_.SetInsertPoint(array_loop_head);
1333  CHECK(array_len);
1334  auto array_idx = cgen_state_->ir_builder_.CreateLoad(
1335  array_idx_ptr->getType()->getPointerElementType(), array_idx_ptr);
1336  auto bound_check = cgen_state_->ir_builder_.CreateICmp(
1337  llvm::ICmpInst::ICMP_SLT, array_idx, array_len);
1338  auto array_loop_body = llvm::BasicBlock::Create(
1339  cgen_state_->context_, "array_loop_body", cgen_state_->current_func_);
1340  cgen_state_->ir_builder_.CreateCondBr(
1341  bound_check,
1342  array_loop_body,
1343  array_loops.empty() ? diamond_codegen.orig_cond_false_ : array_loops.top());
1344  cgen_state_->ir_builder_.SetInsertPoint(array_loop_body);
1345  cgen_state_->ir_builder_.CreateStore(
1346  cgen_state_->ir_builder_.CreateAdd(array_idx, cgen_state_->llInt(int32_t(1))),
1347  array_idx_ptr);
1348  auto array_at_fname = "array_at_" + numeric_type_name(elem_ti);
1349  if (array_ti.get_size() < 0) {
1350  if (array_ti.get_notnull()) {
1351  array_at_fname = "notnull_" + array_at_fname;
1352  }
1353  array_at_fname = "varlen_" + array_at_fname;
1354  }
1355  const auto ar_ret_ty =
1356  elem_ti.is_fp()
1357  ? (elem_ti.get_type() == kDOUBLE
1358  ? llvm::Type::getDoubleTy(cgen_state_->context_)
1359  : llvm::Type::getFloatTy(cgen_state_->context_))
1360  : get_int_type(elem_ti.get_logical_size() * 8, cgen_state_->context_);
1361  group_key = cgen_state_->emitExternalCall(
1362  array_at_fname,
1363  ar_ret_ty,
1364  {group_key, code_generator.posArg(arr_expr), array_idx});
1366  elem_ti, isArchMaxwell(co.device_type), thread_mem_shared)) {
1367  key_to_cache = spillDoubleElement(group_key, ar_ret_ty);
1368  } else {
1369  key_to_cache = group_key;
1370  }
1371  CHECK(array_loop_head);
1372  array_loops.push(array_loop_head);
1373  }
1374  cgen_state_->group_by_expr_cache_.push_back(key_to_cache);
1375  llvm::Value* orig_group_key{nullptr};
1376  if (translate_null_val) {
1377  const std::string translator_func_name(
1378  col_width == sizeof(int32_t) ? "translate_null_key_i32_" : "translate_null_key_");
1379  const auto& ti = group_by_col->get_type_info();
1380  const auto key_type = get_int_type(ti.get_logical_size() * 8, cgen_state_->context_);
1381  orig_group_key = group_key;
1382  group_key = cgen_state_->emitCall(
1383  translator_func_name + numeric_type_name(ti),
1384  {group_key,
1385  static_cast<llvm::Value*>(
1386  llvm::ConstantInt::get(key_type, inline_int_null_val(ti))),
1387  static_cast<llvm::Value*>(llvm::ConstantInt::get(
1388  llvm::Type::getInt64Ty(cgen_state_->context_), translated_null_val))});
1389  }
1390  group_key = cgen_state_->ir_builder_.CreateBitCast(
1391  cgen_state_->castToTypeIn(group_key, col_width * 8),
1392  get_int_type(col_width * 8, cgen_state_->context_));
1393  if (orig_group_key) {
1394  orig_group_key = cgen_state_->ir_builder_.CreateBitCast(
1395  cgen_state_->castToTypeIn(orig_group_key, col_width * 8),
1396  get_int_type(col_width * 8, cgen_state_->context_));
1397  }
1398  return {group_key, orig_group_key};
1399 }
1400 
1402  Executor* executor,
1403  llvm::Value* nullable_lv,
1404  const SQLTypeInfo& nullable_ti,
1405  const std::string& name)
1406  : cgen_state(cgen_state), name(name) {
1407  AUTOMATIC_IR_METADATA(cgen_state);
1408  CHECK(nullable_ti.is_number() || nullable_ti.is_time() || nullable_ti.is_boolean() ||
1409  nullable_ti.is_dict_encoded_string());
1410 
1411  llvm::Value* is_null_lv{nullptr};
1412  if (nullable_ti.is_fp()) {
1413  is_null_lv = cgen_state->ir_builder_.CreateFCmp(
1414  llvm::FCmpInst::FCMP_OEQ, nullable_lv, cgen_state->inlineFpNull(nullable_ti));
1415  } else if (nullable_ti.is_boolean()) {
1416  is_null_lv = cgen_state->ir_builder_.CreateICmp(
1417  llvm::ICmpInst::ICMP_EQ, nullable_lv, cgen_state->llBool(true));
1418  } else {
1419  is_null_lv = cgen_state->ir_builder_.CreateICmp(
1420  llvm::ICmpInst::ICMP_EQ, nullable_lv, cgen_state->inlineIntNull(nullable_ti));
1421  }
1422  CHECK(is_null_lv);
1423  null_check =
1424  std::make_unique<DiamondCodegen>(is_null_lv, executor, false, name, nullptr, false);
1425 
1426  // generate a phi node depending on whether we got a null or not
1427  nullcheck_bb = llvm::BasicBlock::Create(
1428  cgen_state->context_, name + "_bb", cgen_state->current_func_);
1429 
1430  // update the blocks created by diamond codegen to point to the newly created phi
1431  // block
1432  cgen_state->ir_builder_.SetInsertPoint(null_check->cond_true_);
1433  cgen_state->ir_builder_.CreateBr(nullcheck_bb);
1434  cgen_state->ir_builder_.SetInsertPoint(null_check->cond_false_);
1435 }
1436 
1437 llvm::Value* CodeGenerator::NullCheckCodegen::finalize(llvm::Value* null_lv,
1438  llvm::Value* notnull_lv) {
1439  AUTOMATIC_IR_METADATA(cgen_state);
1440  CHECK(null_check);
1441  cgen_state->ir_builder_.CreateBr(nullcheck_bb);
1442 
1443  CHECK_EQ(null_lv->getType(), notnull_lv->getType());
1444 
1445  cgen_state->ir_builder_.SetInsertPoint(nullcheck_bb);
1446  nullcheck_value =
1447  cgen_state->ir_builder_.CreatePHI(null_lv->getType(), 2, name + "_value");
1448  nullcheck_value->addIncoming(notnull_lv, null_check->cond_false_);
1449  nullcheck_value->addIncoming(null_lv, null_check->cond_true_);
1450 
1451  null_check.reset(nullptr);
1452  cgen_state->ir_builder_.SetInsertPoint(nullcheck_bb);
1453  return nullcheck_value;
1454 }
#define CHECK_EQ(x, y)
Definition: Logger.h:230
bool g_enable_left_join_filter_hoisting
Definition: Execute.cpp:100
NullCheckCodegen(CgenState *cgen_state, Executor *executor, llvm::Value *nullable_lv, const SQLTypeInfo &nullable_ti, const std::string &name="")
Definition: IRCodegen.cpp:1401
void codegenJoinLoops(const std::vector< JoinLoop > &join_loops, const RelAlgExecutionUnit &ra_exe_unit, GroupByAndAggregate &group_by_and_aggregate, llvm::Function *query_func, llvm::BasicBlock *entry_bb, QueryMemoryDescriptor &query_mem_desc, const CompilationOptions &co, const ExecutionOptions &eo)
Definition: IRCodegen.cpp:1112
#define IS_LOGIC(X)
Definition: sqldefs.h:60
const Expr * get_partition_count() const
Definition: Analyzer.h:1044
#define NULL_DOUBLE
JoinType
Definition: sqldefs.h:156
std::vector< llvm::Value * > outer_join_match_found_per_level_
Definition: CgenState.h:452
bool is_constant_expr() const
Definition: Analyzer.h:1119
std::unordered_map< size_t, std::vector< std::shared_ptr< Analyzer::Expr > > > left_join_non_hashtable_quals_
Definition: PlanState.h:64
llvm::Value * codegenConstantWidthBucketExpr(const Analyzer::WidthBucketExpr *, const CompilationOptions &)
Definition: IRCodegen.cpp:303
llvm::BasicBlock * nullcheck_bb
bool is_trivial_loop_join(const std::vector< InputTableInfo > &query_infos, const RelAlgExecutionUnit &ra_exe_unit)
Definition: Execute.cpp:1554
llvm::Value * element_count
Definition: JoinLoop.h:46
llvm::Value * values_buffer
Definition: JoinLoop.h:49
#define IS_EQUIVALENCE(X)
Definition: sqldefs.h:68
llvm::Value * codegenArith(const Analyzer::BinOper *, const CompilationOptions &)
CgenState * cgen_state_
std::set< int > aggregateResult(const std::set< int > &aggregate, const std::set< int > &next_result) const final
Definition: IRCodegen.cpp:756
GroupColLLVMValue groupByColumnCodegen(Analyzer::Expr *group_by_col, const size_t col_width, const CompilationOptions &, const bool translate_null_val, const int64_t translated_null_val, DiamondCodegen &, std::stack< llvm::BasicBlock * > &, const bool thread_mem_shared)
Definition: IRCodegen.cpp:1292
bool is_fp() const
Definition: sqltypes.h:604
llvm::IRBuilder ir_builder_
Definition: CgenState.h:441
std::function< llvm::BasicBlock *(llvm::BasicBlock *, llvm::BasicBlock *, const std::string &, llvm::Function *, CgenState *)> HoistedFiltersCallback
Definition: JoinLoop.h:61
llvm::Value * posArg(const Analyzer::Expr *) const
Definition: ColumnIR.cpp:550
std::string join(T const &container, std::string const &delim)
std::vector< InputDescriptor > input_descs
#define UNREACHABLE()
Definition: Logger.h:266
#define CHECK_GE(x, y)
Definition: Logger.h:235
bool need_patch_unnest_double(const SQLTypeInfo &ti, const bool is_maxwell, const bool mem_shared)
std::set< int > visitColumnVar(const Analyzer::ColumnVar *col_expr) const final
Definition: IRCodegen.cpp:734
Definition: sqldefs.h:48
llvm::ConstantInt * llBool(const bool v) const
Definition: CgenState.h:320
virtual std::vector< llvm::Value * > codegenColumn(const Analyzer::ColumnVar *, const bool fetch_column, const CompilationOptions &)
Definition: ColumnIR.cpp:92
void set_constant_expr() const
Definition: Analyzer.h:1118
llvm::Value * codegenArrayAt(const Analyzer::BinOper *, const CompilationOptions &)
Definition: ArrayIR.cpp:26
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:404
void setFalseTarget(llvm::BasicBlock *cond_false)
QualsConjunctiveForm qual_to_conjunctive_form(const std::shared_ptr< Analyzer::Expr > qual_expr)
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
bool is_number() const
Definition: sqltypes.h:605
double inline_fp_null_val(const SQL_TYPE_INFO &ti)
std::vector< llvm::Value * > codegenGeoBinOper(const Analyzer::GeoBinOper *, const CompilationOptions &)
Definition: GeoIR.cpp:247
bool is_time() const
Definition: sqltypes.h:606
std::shared_ptr< HashJoin > hash_table
Definition: Execute.h:1033
double get_bound_val(const Analyzer::Expr *bound_expr) const
Definition: Analyzer.cpp:3683
std::string to_string(char const *&&v)
llvm::Function * row_func_
Definition: CgenState.h:431
llvm::Value * codegenIsNull(const Analyzer::UOper *, const CompilationOptions &)
Definition: LogicalIR.cpp:380
SQLOps get_optype() const
Definition: Analyzer.h:447
std::set< int > visitUOper(const Analyzer::UOper *u_oper) const final
Definition: IRCodegen.cpp:752
std::vector< llvm::Value * > codegenGeoExpr(const Analyzer::GeoExpr *, const CompilationOptions &)
Definition: GeoIR.cpp:97
llvm::LLVMContext & context_
Definition: CgenState.h:439
llvm::Function * current_func_
Definition: CgenState.h:433
llvm::Value * get_arg_by_name(llvm::Function *func, const std::string &name)
Definition: Execute.h:166
#define INJECT_TIMER(DESC)
Definition: measure.h:93
#define CHECK_NE(x, y)
Definition: Logger.h:231
const JoinQualsPerNestingLevel join_quals
std::vector< llvm::Value * > codegenGeoUOper(const Analyzer::GeoUOper *, const CompilationOptions &)
Definition: GeoIR.cpp:167
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
TableIdToNodeMap table_id_to_node_map
llvm::Value * codegenWidthBucketExpr(const Analyzer::WidthBucketExpr *, const CompilationOptions &)
Definition: IRCodegen.cpp:376
llvm::Value * codegenCastBetweenIntTypes(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti, bool upscale=true)
Definition: CastIR.cpp:342
llvm::Value * codegenFunctionOper(const Analyzer::FunctionOper *, const CompilationOptions &)
std::set< int > visitFunctionOper(const Analyzer::FunctionOper *func_expr) const final
Definition: IRCodegen.cpp:738
Executor * executor_
static llvm::BasicBlock * codegen(const std::vector< JoinLoop > &join_loops, const std::function< llvm::BasicBlock *(const std::vector< llvm::Value * > &)> &body_codegen, llvm::Value *outer_iter, llvm::BasicBlock *exit_bb, CgenState *cgen_state)
Definition: JoinLoop.cpp:50
void add_qualifier_to_execution_unit(RelAlgExecutionUnit &ra_exe_unit, const std::shared_ptr< Analyzer::Expr > &qual)
Definition: IRCodegen.cpp:474
const std::vector< InputTableInfo > & query_infos_
Definition: PlanState.h:68
bool needs_error_check_
Definition: CgenState.h:461
bool is_boolean() const
Definition: sqltypes.h:607
ExpressionRange getExpressionRange(const Analyzer::BinOper *expr, const std::vector< InputTableInfo > &query_infos, const Executor *, boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>> simple_quals)
std::vector< llvm::Value * > codegenArrayExpr(const Analyzer::ArrayExpr *, const CompilationOptions &)
Definition: ArrayIR.cpp:91
#define AUTOMATIC_IR_METADATA(CGENSTATE)
llvm::BasicBlock * orig_cond_false_
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:82
llvm::Value * codegenUMinus(const Analyzer::UOper *, const CompilationOptions &)
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:219
llvm::Value * slot_lookup_result
Definition: JoinLoop.h:47
std::set< int > visitBinOper(const Analyzer::BinOper *bin_oper) const final
Definition: IRCodegen.cpp:746
ExecutorDeviceType device_type
std::unordered_map< int, std::unordered_map< int, std::shared_ptr< const ColumnarResults >>> ColumnCacheMap
Definition: sqldefs.h:33
PlanState * plan_state_
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
#define CHECK_LT(x, y)
Definition: Logger.h:232
const std::vector< InputTableInfo > & query_infos_
Definition: sqldefs.h:70
Expression class for string functions The &quot;arg&quot; constructor parameter must be an expression that reso...
Definition: Analyzer.h:1464
std::shared_ptr< HashJoin > buildCurrentLevelHashTable(const JoinCondition &current_level_join_conditions, size_t level_idx, RelAlgExecutionUnit &ra_exe_unit, const CompilationOptions &co, const std::vector< InputTableInfo > &query_infos, ColumnCacheMap &column_cache, std::vector< std::string > &fail_reasons)
Definition: IRCodegen.cpp:935
#define IS_ARITHMETIC(X)
Definition: sqldefs.h:61
const Expr * get_arg() const
Definition: Analyzer.h:805
int32_t get_partition_count_val() const
Definition: Analyzer.cpp:3693
static const int32_t ERR_WIDTH_BUCKET_INVALID_ARGUMENT
Definition: Execute.h:1385
llvm::Value * toBool(llvm::Value *)
Definition: LogicalIR.cpp:343
std::vector< llvm::Value * > codegenGeoColumnVar(const Analyzer::GeoColumnVar *, const bool fetch_columns, const CompilationOptions &co)
Definition: GeoIR.cpp:52
llvm::Value * codegenFunctionOperWithCustomTypeHandling(const Analyzer::FunctionOperWithCustomTypeHandling *, const CompilationOptions &)
llvm::Value * codegenCmp(const Analyzer::BinOper *, const CompilationOptions &)
Definition: CompareIR.cpp:230
std::list< std::shared_ptr< Analyzer::Expr > > getSimpleQuals() const
Definition: PlanState.h:99
const Expr * get_target_value() const
Definition: Analyzer.h:1041
std::list< std::shared_ptr< Analyzer::Expr > > quals
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:306
llvm::Value * codegenUnnest(const Analyzer::UOper *, const CompilationOptions &)
Definition: ArrayIR.cpp:20
llvm::Value * addJoinLoopIterator(const std::vector< llvm::Value * > &prev_iters, const size_t level_idx)
Definition: IRCodegen.cpp:1094
std::list< std::shared_ptr< Analyzer::Expr > > quals
RegisteredQueryHint query_hint
llvm::Value * finalize(llvm::Value *null_lv, llvm::Value *notnull_lv)
Definition: IRCodegen.cpp:1437
#define CHECK(condition)
Definition: Logger.h:222
bool can_skip_out_of_bound_check() const
Definition: Analyzer.h:1116
llvm::Value * codegenLogical(const Analyzer::BinOper *, const CompilationOptions &)
Definition: LogicalIR.cpp:298
void check_if_loop_join_is_allowed(RelAlgExecutionUnit &ra_exe_unit, const ExecutionOptions &eo, const std::vector< InputTableInfo > &query_infos, const size_t level_idx, const std::string &fail_reason)
Definition: IRCodegen.cpp:484
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
llvm::ConstantInt * ll_bool(const bool v, llvm::LLVMContext &context)
JoinLoop::HoistedFiltersCallback buildHoistLeftHandSideFiltersCb(const RelAlgExecutionUnit &ra_exe_unit, const size_t level_idx, const int inner_table_id, const CompilationOptions &co)
Definition: IRCodegen.cpp:768
llvm::Value * codegenCast(const Analyzer::UOper *, const CompilationOptions &)
Definition: CastIR.cpp:21
uint32_t log2_bytes(const uint32_t bytes)
Definition: Execute.h:176
std::string numeric_type_name(const SQLTypeInfo &ti)
Definition: Execute.h:209
bool is_dict_encoded_string() const
Definition: sqltypes.h:652
Definition: sqltypes.h:59
void skip_out_of_bound_check() const
Definition: Analyzer.h:1117
void redeclareFilterFunction()
Definition: IRCodegen.cpp:995
const Expr * get_lower_bound() const
Definition: Analyzer.h:1042
std::vector< JoinLoop > buildJoinLoops(RelAlgExecutionUnit &ra_exe_unit, const CompilationOptions &co, const ExecutionOptions &eo, const std::vector< InputTableInfo > &query_infos, ColumnCacheMap &column_cache)
Definition: IRCodegen.cpp:523
std::function< llvm::Value *(const std::vector< llvm::Value * > &, llvm::Value *)> buildIsDeletedCb(const RelAlgExecutionUnit &ra_exe_unit, const size_t level_idx, const CompilationOptions &co)
Definition: IRCodegen.cpp:876
string name
Definition: setup.in.py:72
const Expr * get_upper_bound() const
Definition: Analyzer.h:1043
llvm::ArrayType * get_int_array_type(int const width, int count, llvm::LLVMContext &context)
Definition: sqldefs.h:38
SQLOps get_optype() const
Definition: Analyzer.h:378
#define VLOG(n)
Definition: Logger.h:316
std::unique_ptr< DiamondCodegen > null_check
std::list< std::shared_ptr< Analyzer::Expr > > simple_quals
#define IS_COMPARISON(X)
Definition: sqldefs.h:57
double doubleval
Definition: sqltypes.h:237
HashTableBuildDagMap hash_table_build_plan_dag
llvm::ConstantFP * inlineFpNull(const SQLTypeInfo &)
Definition: CgenState.cpp:106
void check_valid_join_qual(std::shared_ptr< Analyzer::BinOper > &bin_oper)
Definition: IRCodegen.cpp:504
Executor * executor() const
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int32_t width_bucket_expr(const double target_value, const bool reversed, const double lower_bound, const double upper_bound, const int32_t partition_count)