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