OmniSciDB  bf83d84833
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ColumnIR.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 "CodeGenerator.h"
18 #include "Codec.h"
19 #include "Execute.h"
20 #include "WindowContext.h"
21 
22 // Code generation routines and helpers for working with column expressions.
23 
24 namespace {
25 
26 // Return the right decoder for a given column expression. Doesn't handle
27 // variable length data. The decoder encapsulates the code generation logic.
28 std::shared_ptr<Decoder> get_col_decoder(const Analyzer::ColumnVar* col_var) {
29  const auto enc_type = col_var->get_compression();
30  const auto& ti = col_var->get_type_info();
31  switch (enc_type) {
32  case kENCODING_NONE: {
33  const auto int_type = ti.is_decimal() ? decimal_to_int_type(ti) : ti.get_type();
34  switch (int_type) {
35  case kBOOLEAN:
36  return std::make_shared<FixedWidthInt>(1);
37  case kTINYINT:
38  return std::make_shared<FixedWidthInt>(1);
39  case kSMALLINT:
40  return std::make_shared<FixedWidthInt>(2);
41  case kINT:
42  return std::make_shared<FixedWidthInt>(4);
43  case kBIGINT:
44  return std::make_shared<FixedWidthInt>(8);
45  case kFLOAT:
46  return std::make_shared<FixedWidthReal>(false);
47  case kDOUBLE:
48  return std::make_shared<FixedWidthReal>(true);
49  case kTIME:
50  case kTIMESTAMP:
51  case kDATE:
52  return std::make_shared<FixedWidthInt>(8);
53  default:
54  CHECK(false);
55  }
56  }
57  case kENCODING_DICT:
58  CHECK(ti.is_string());
59  // For dictionary-encoded columns encoded on less than 4 bytes, we can use
60  // unsigned representation for double the maximum cardinality. The inline
61  // null value is going to be the maximum value of the underlying type.
62  if (ti.get_size() < ti.get_logical_size()) {
63  return std::make_shared<FixedWidthUnsigned>(ti.get_size());
64  }
65  return std::make_shared<FixedWidthInt>(ti.get_size());
66  case kENCODING_FIXED: {
67  const auto bit_width = col_var->get_comp_param();
68  CHECK_EQ(0, bit_width % 8);
69  return std::make_shared<FixedWidthInt>(bit_width / 8);
70  }
72  CHECK(ti.is_date_in_days());
73  return col_var->get_comp_param() == 16 ? std::make_shared<FixedWidthSmallDate>(2)
74  : std::make_shared<FixedWidthSmallDate>(4);
75  }
76  default:
77  abort();
78  }
79 }
80 
81 size_t get_col_bit_width(const Analyzer::ColumnVar* col_var) {
82  const auto& type_info = col_var->get_type_info();
83  return get_bit_width(type_info);
84 }
85 
87  return col_var->get_rte_idx() == -1 ? 0 : col_var->get_rte_idx();
88 }
89 
90 } // namespace
91 
92 std::vector<llvm::Value*> CodeGenerator::codegenColumn(const Analyzer::ColumnVar* col_var,
93  const bool fetch_column,
94  const CompilationOptions& co) {
96  if (col_var->get_rte_idx() <= 0 ||
98  !foundOuterJoinMatch(col_var->get_rte_idx())) {
99  return codegenColVar(col_var, fetch_column, true, co);
100  }
101  return codegenOuterJoinNullPlaceholder(col_var, fetch_column, co);
102 }
103 
104 std::vector<llvm::Value*> CodeGenerator::codegenColVar(const Analyzer::ColumnVar* col_var,
105  const bool fetch_column,
106  const bool update_query_plan,
107  const CompilationOptions& co) {
109  const bool hoist_literals = co.hoist_literals;
110  auto col_id = col_var->get_column_id();
111  const int rte_idx = adjusted_range_table_index(col_var);
112  CHECK_LT(static_cast<size_t>(rte_idx), cgen_state_->frag_offsets_.size());
113  const auto catalog = executor()->getCatalog();
114  CHECK(catalog);
115  if (col_var->get_table_id() > 0) {
116  auto cd = get_column_descriptor(col_id, col_var->get_table_id(), *catalog);
117  if (cd->isVirtualCol) {
118  CHECK(cd->columnName == "rowid");
119  return {codegenRowId(col_var, co)};
120  }
121  auto col_ti = cd->columnType;
122  if (col_ti.get_physical_coord_cols() > 0) {
123  std::vector<llvm::Value*> cols;
124  for (auto i = 0; i < col_ti.get_physical_coord_cols(); i++) {
125  auto cd0 =
126  get_column_descriptor(col_id + i + 1, col_var->get_table_id(), *catalog);
127  auto col0_ti = cd0->columnType;
128  CHECK(!cd0->isVirtualCol);
129  auto col0_var = makeExpr<Analyzer::ColumnVar>(
130  col0_ti, col_var->get_table_id(), cd0->columnId, rte_idx);
131  auto col = codegenColVar(col0_var.get(), fetch_column, false, co);
132  cols.insert(cols.end(), col.begin(), col.end());
133  if (!fetch_column && plan_state_->isLazyFetchColumn(col_var)) {
135  std::make_pair(col_var->get_table_id(), col0_var->get_column_id()));
136  }
137  }
138  if (!fetch_column && plan_state_->isLazyFetchColumn(col_var)) {
140  std::make_pair(col_var->get_table_id(), col_var->get_column_id()));
141  } else {
143  std::make_pair(col_var->get_table_id(), col_var->get_column_id()));
144  }
145  return cols;
146  }
147  } else {
148  if (col_var->get_type_info().is_geometry()) {
149  throw std::runtime_error(
150  "Geospatial columns not supported in temporary tables yet");
151  }
152  }
153  const auto grouped_col_lv = resolveGroupedColumnReference(col_var);
154  if (grouped_col_lv) {
155  return {grouped_col_lv};
156  }
157  const int local_col_id = plan_state_->getLocalColumnId(col_var, fetch_column);
158  const auto window_func_context =
160  // only generate the decoding code once; if a column has been previously
161  // fetched in the generated IR, we'll reuse it
162  if (!window_func_context) {
163  auto it = cgen_state_->fetch_cache_.find(local_col_id);
164  if (it != cgen_state_->fetch_cache_.end()) {
165  return {it->second};
166  }
167  }
168  const auto hash_join_lhs = hashJoinLhs(col_var);
169  // Note(jclay): This has been prone to cause failures in some overlaps joins.
170  // I believe most of the issues are worked out now, but a good place to check if
171  // failures are happening.
172 
173  // Use the already fetched left-hand side of an equi-join if the types are identical.
174  // Currently, types can only be different because of different underlying dictionaries.
175  if (hash_join_lhs && hash_join_lhs->get_type_info() == col_var->get_type_info()) {
176  if (plan_state_->isLazyFetchColumn(col_var)) {
178  std::make_pair(col_var->get_table_id(), col_var->get_column_id()));
179  }
180  return codegen(hash_join_lhs.get(), fetch_column, co);
181  }
182  auto pos_arg = posArg(col_var);
183  if (window_func_context) {
184  pos_arg = codegenWindowPosition(window_func_context, pos_arg);
185  }
186  auto col_byte_stream = colByteStream(col_var, fetch_column, hoist_literals);
187  if (plan_state_->isLazyFetchColumn(col_var)) {
188  if (update_query_plan) {
190  std::make_pair(col_var->get_table_id(), col_var->get_column_id()));
191  }
192  if (rte_idx > 0) {
193  const auto offset = cgen_state_->frag_offsets_[rte_idx];
194  if (offset) {
195  return {cgen_state_->ir_builder_.CreateAdd(pos_arg, offset)};
196  } else {
197  return {pos_arg};
198  }
199  }
200  return {pos_arg};
201  }
202  const auto& col_ti = col_var->get_type_info();
203  if (col_ti.is_string() && col_ti.get_compression() == kENCODING_NONE) {
204  const auto varlen_str_column_lvs =
205  codegenVariableLengthStringColVar(col_byte_stream, pos_arg);
206  if (!window_func_context) {
207  auto it_ok = cgen_state_->fetch_cache_.insert(
208  std::make_pair(local_col_id, varlen_str_column_lvs));
209  CHECK(it_ok.second);
210  }
211  return varlen_str_column_lvs;
212  }
213  if (col_ti.is_array()) {
214  return {col_byte_stream};
215  }
216  if (window_func_context) {
217  return {codegenFixedLengthColVarInWindow(col_var, col_byte_stream, pos_arg)};
218  }
219  const auto fixed_length_column_lv =
220  codegenFixedLengthColVar(col_var, col_byte_stream, pos_arg);
221  auto it_ok = cgen_state_->fetch_cache_.insert(
222  std::make_pair(local_col_id, std::vector<llvm::Value*>{fixed_length_column_lv}));
223  return {it_ok.first->second};
224 }
225 
227  WindowFunctionContext* window_func_context,
228  llvm::Value* pos_arg) {
230  const auto window_position = cgen_state_->emitCall(
231  "row_number_window_func",
232  {cgen_state_->llInt(reinterpret_cast<const int64_t>(window_func_context->output())),
233  pos_arg});
234  window_func_context->setRowNumber(window_position);
235  return window_position;
236 }
237 
238 // Generate code for fixed length column types (number, timestamp or date,
239 // dictionary-encoded string)
241  llvm::Value* col_byte_stream,
242  llvm::Value* pos_arg) {
244  const auto decoder = get_col_decoder(col_var);
245  auto dec_val = decoder->codegenDecode(col_byte_stream, pos_arg, cgen_state_->module_);
246  cgen_state_->ir_builder_.Insert(dec_val);
247  auto dec_type = dec_val->getType();
248  llvm::Value* dec_val_cast{nullptr};
249  const auto& col_ti = col_var->get_type_info();
250  if (dec_type->isIntegerTy()) {
251  auto dec_width = static_cast<llvm::IntegerType*>(dec_type)->getBitWidth();
252  auto col_width = get_col_bit_width(col_var);
253  dec_val_cast = cgen_state_->ir_builder_.CreateCast(
254  static_cast<size_t>(col_width) > dec_width ? llvm::Instruction::CastOps::SExt
255  : llvm::Instruction::CastOps::Trunc,
256  dec_val,
257  get_int_type(col_width, cgen_state_->context_));
258  if ((col_ti.get_compression() == kENCODING_FIXED ||
259  (col_ti.get_compression() == kENCODING_DICT && col_ti.get_size() < 4)) &&
260  !col_ti.get_notnull()) {
261  dec_val_cast = codgenAdjustFixedEncNull(dec_val_cast, col_ti);
262  }
263  } else {
264  CHECK_EQ(kENCODING_NONE, col_ti.get_compression());
265  CHECK(dec_type->isFloatTy() || dec_type->isDoubleTy());
266  if (dec_type->isDoubleTy()) {
267  CHECK(col_ti.get_type() == kDOUBLE);
268  } else if (dec_type->isFloatTy()) {
269  CHECK(col_ti.get_type() == kFLOAT);
270  }
271  dec_val_cast = dec_val;
272  }
273  CHECK(dec_val_cast);
274  return dec_val_cast;
275 }
276 
278  const Analyzer::ColumnVar* col_var,
279  llvm::Value* col_byte_stream,
280  llvm::Value* pos_arg) {
282  const auto orig_bb = cgen_state_->ir_builder_.GetInsertBlock();
283  const auto pos_is_valid =
284  cgen_state_->ir_builder_.CreateICmpSGE(pos_arg, cgen_state_->llInt(int64_t(0)));
285  const auto pos_valid_bb = llvm::BasicBlock::Create(
286  cgen_state_->context_, "window.pos_valid", cgen_state_->current_func_);
287  const auto pos_notvalid_bb = llvm::BasicBlock::Create(
288  cgen_state_->context_, "window.pos_notvalid", cgen_state_->current_func_);
289  cgen_state_->ir_builder_.CreateCondBr(pos_is_valid, pos_valid_bb, pos_notvalid_bb);
290  cgen_state_->ir_builder_.SetInsertPoint(pos_valid_bb);
291  const auto fixed_length_column_lv =
292  codegenFixedLengthColVar(col_var, col_byte_stream, pos_arg);
293  cgen_state_->ir_builder_.CreateBr(pos_notvalid_bb);
294  cgen_state_->ir_builder_.SetInsertPoint(pos_notvalid_bb);
295  const auto window_func_call_phi =
296  cgen_state_->ir_builder_.CreatePHI(fixed_length_column_lv->getType(), 2);
297  window_func_call_phi->addIncoming(fixed_length_column_lv, pos_valid_bb);
298  const auto& col_ti = col_var->get_type_info();
299  const auto null_lv =
300  col_ti.is_fp() ? static_cast<llvm::Value*>(cgen_state_->inlineFpNull(col_ti))
301  : static_cast<llvm::Value*>(cgen_state_->inlineIntNull(col_ti));
302  window_func_call_phi->addIncoming(null_lv, orig_bb);
303  return window_func_call_phi;
304 }
305 
307  llvm::Value* col_byte_stream,
308  llvm::Value* pos_arg) {
310  // real (not dictionary-encoded) strings; store the pointer to the payload
311  auto ptr_and_len =
312  cgen_state_->emitExternalCall("string_decode",
314  {col_byte_stream, pos_arg});
315  // Unpack the pointer + length, see string_decode function.
316  auto str_lv = cgen_state_->emitCall("extract_str_ptr", {ptr_and_len});
317  auto len_lv = cgen_state_->emitCall("extract_str_len", {ptr_and_len});
318  return {ptr_and_len, str_lv, len_lv};
319 }
320 
322  const CompilationOptions& co) {
324  const auto offset_lv = cgen_state_->frag_offsets_[adjusted_range_table_index(col_var)];
325  llvm::Value* start_rowid_lv{nullptr};
326  const auto& table_generation = executor()->getTableGeneration(col_var->get_table_id());
327  if (table_generation.start_rowid > 0) {
328  // Handle the multi-node case: each leaf receives a start rowid used
329  // to offset the local rowid and generate a cluster-wide unique rowid.
330  Datum d;
331  d.bigintval = table_generation.start_rowid;
332  const auto start_rowid = makeExpr<Analyzer::Constant>(kBIGINT, false, d);
333  const auto start_rowid_lvs = codegen(start_rowid.get(), kENCODING_NONE, -1, co);
334  CHECK_EQ(size_t(1), start_rowid_lvs.size());
335  start_rowid_lv = start_rowid_lvs.front();
336  }
337  auto rowid_lv = posArg(col_var);
338  if (offset_lv) {
339  rowid_lv = cgen_state_->ir_builder_.CreateAdd(rowid_lv, offset_lv);
340  } else if (col_var->get_rte_idx() > 0) {
341  auto frag_off_ptr = get_arg_by_name(cgen_state_->row_func_, "frag_row_off");
342  auto input_off_ptr = cgen_state_->ir_builder_.CreateGEP(
343  frag_off_ptr, cgen_state_->llInt(int32_t(col_var->get_rte_idx())));
344  auto rowid_offset_lv = cgen_state_->ir_builder_.CreateLoad(input_off_ptr);
345  rowid_lv = cgen_state_->ir_builder_.CreateAdd(rowid_lv, rowid_offset_lv);
346  }
347  if (table_generation.start_rowid > 0) {
348  CHECK(start_rowid_lv);
349  rowid_lv = cgen_state_->ir_builder_.CreateAdd(rowid_lv, start_rowid_lv);
350  }
351  return rowid_lv;
352 }
353 
354 namespace {
355 
356 SQLTypes get_phys_int_type(const size_t byte_sz) {
357  switch (byte_sz) {
358  case 1:
359  return kBOOLEAN;
360  // TODO: kTINYINT
361  case 2:
362  return kSMALLINT;
363  case 4:
364  return kINT;
365  case 8:
366  return kBIGINT;
367  default:
368  CHECK(false);
369  }
370  return kNULLT;
371 }
372 
373 } // namespace
374 
375 llvm::Value* CodeGenerator::codgenAdjustFixedEncNull(llvm::Value* val,
376  const SQLTypeInfo& col_ti) {
378  CHECK_LT(col_ti.get_size(), col_ti.get_logical_size());
379  const auto col_phys_width = col_ti.get_size() * 8;
380  auto from_typename = "int" + std::to_string(col_phys_width) + "_t";
381  auto adjusted = cgen_state_->ir_builder_.CreateCast(
382  llvm::Instruction::CastOps::Trunc,
383  val,
384  get_int_type(col_phys_width, cgen_state_->context_));
385  if (col_ti.get_compression() == kENCODING_DICT) {
386  from_typename = "u" + from_typename;
387  llvm::Value* from_null{nullptr};
388  switch (col_ti.get_size()) {
389  case 1:
390  from_null = cgen_state_->llInt(std::numeric_limits<uint8_t>::max());
391  break;
392  case 2:
393  from_null = cgen_state_->llInt(std::numeric_limits<uint16_t>::max());
394  break;
395  default:
396  CHECK(false);
397  }
398  return cgen_state_->emitCall(
399  "cast_" + from_typename + "_to_" + numeric_type_name(col_ti) + "_nullable",
400  {adjusted, from_null, cgen_state_->inlineIntNull(col_ti)});
401  }
402  SQLTypeInfo col_phys_ti(get_phys_int_type(col_ti.get_size()),
403  col_ti.get_dimension(),
404  col_ti.get_scale(),
405  false,
407  0,
408  col_ti.get_subtype());
409  return cgen_state_->emitCall(
410  "cast_" + from_typename + "_to_" + numeric_type_name(col_ti) + "_nullable",
411  {adjusted,
412  cgen_state_->inlineIntNull(col_phys_ti),
413  cgen_state_->inlineIntNull(col_ti)});
414 }
415 
416 llvm::Value* CodeGenerator::foundOuterJoinMatch(const size_t nesting_level) const {
417  CHECK_GE(nesting_level, size_t(1));
418  CHECK_LE(nesting_level,
419  static_cast<size_t>(cgen_state_->outer_join_match_found_per_level_.size()));
420  return cgen_state_->outer_join_match_found_per_level_[nesting_level - 1];
421 }
422 
424  const Analyzer::ColumnVar* col_var,
425  const bool fetch_column,
426  const CompilationOptions& co) {
428  const auto grouped_col_lv = resolveGroupedColumnReference(col_var);
429  if (grouped_col_lv) {
430  return {grouped_col_lv};
431  }
432  const auto outer_join_args_bb = llvm::BasicBlock::Create(
433  cgen_state_->context_, "outer_join_args", cgen_state_->current_func_);
434  const auto outer_join_nulls_bb = llvm::BasicBlock::Create(
435  cgen_state_->context_, "outer_join_nulls", cgen_state_->current_func_);
436  const auto phi_bb = llvm::BasicBlock::Create(
437  cgen_state_->context_, "outer_join_phi", cgen_state_->current_func_);
438  const auto outer_join_match_lv = foundOuterJoinMatch(col_var->get_rte_idx());
439  CHECK(outer_join_match_lv);
440  cgen_state_->ir_builder_.CreateCondBr(
441  outer_join_match_lv, outer_join_args_bb, outer_join_nulls_bb);
442  const auto back_from_outer_join_bb = llvm::BasicBlock::Create(
443  cgen_state_->context_, "back_from_outer_join", cgen_state_->current_func_);
444  cgen_state_->ir_builder_.SetInsertPoint(outer_join_args_bb);
446  const auto orig_lvs = codegenColVar(col_var, fetch_column, true, co);
447  cgen_state_->ir_builder_.CreateBr(phi_bb);
448  cgen_state_->ir_builder_.SetInsertPoint(outer_join_nulls_bb);
449  const auto& null_ti = col_var->get_type_info();
450  if ((null_ti.is_string() && null_ti.get_compression() == kENCODING_NONE) ||
451  null_ti.is_array() || null_ti.is_geometry()) {
452  throw std::runtime_error("Projection type " + null_ti.get_type_name() +
453  " not supported for outer joins yet");
454  }
455  const auto null_constant = makeExpr<Analyzer::Constant>(null_ti, true, Datum{0});
456  const auto null_target_lvs =
457  codegen(null_constant.get(),
458  false,
461  cgen_state_->ir_builder_.CreateBr(phi_bb);
462  CHECK_EQ(orig_lvs.size(), null_target_lvs.size());
463  cgen_state_->ir_builder_.SetInsertPoint(phi_bb);
464  std::vector<llvm::Value*> target_lvs;
465  for (size_t i = 0; i < orig_lvs.size(); ++i) {
466  const auto target_type = orig_lvs[i]->getType();
467  CHECK_EQ(target_type, null_target_lvs[i]->getType());
468  auto target_phi = cgen_state_->ir_builder_.CreatePHI(target_type, 2);
469  target_phi->addIncoming(orig_lvs[i], outer_join_args_bb);
470  target_phi->addIncoming(null_target_lvs[i], outer_join_nulls_bb);
471  target_lvs.push_back(target_phi);
472  }
473  cgen_state_->ir_builder_.CreateBr(back_from_outer_join_bb);
474  cgen_state_->ir_builder_.SetInsertPoint(back_from_outer_join_bb);
475  return target_lvs;
476 }
477 
479  const Analyzer::ColumnVar* col_var) {
480  auto col_id = col_var->get_column_id();
481  if (col_var->get_rte_idx() >= 0) {
482  return nullptr;
483  }
484  CHECK((col_id == 0) || (col_var->get_rte_idx() >= 0 && col_var->get_table_id() > 0));
485  const auto var = dynamic_cast<const Analyzer::Var*>(col_var);
486  CHECK(var);
487  col_id = var->get_varno();
488  CHECK_GE(col_id, 1);
489  if (var->get_which_row() == Analyzer::Var::kGROUPBY) {
490  CHECK_LE(static_cast<size_t>(col_id), cgen_state_->group_by_expr_cache_.size());
491  return cgen_state_->group_by_expr_cache_[col_id - 1];
492  }
493  return nullptr;
494 }
495 
496 // returns the byte stream argument and the position for the given column
498  const bool fetch_column,
499  const bool hoist_literals) {
500  CHECK_GE(cgen_state_->row_func_->arg_size(), size_t(3));
501  const auto stream_arg_name =
502  "col_buf" + std::to_string(plan_state_->getLocalColumnId(col_var, fetch_column));
503  for (auto& arg : cgen_state_->row_func_->args()) {
504  if (arg.getName() == stream_arg_name) {
505  CHECK(arg.getType() == llvm::Type::getInt8PtrTy(cgen_state_->context_));
506  return &arg;
507  }
508  }
509  CHECK(false);
510  return nullptr;
511 }
512 
513 llvm::Value* CodeGenerator::posArg(const Analyzer::Expr* expr) const {
515  const auto col_var = dynamic_cast<const Analyzer::ColumnVar*>(expr);
516  if (col_var && col_var->get_rte_idx() > 0) {
517  const auto hash_pos_it =
518  cgen_state_->scan_idx_to_hash_pos_.find(col_var->get_rte_idx());
519  CHECK(hash_pos_it != cgen_state_->scan_idx_to_hash_pos_.end());
520  if (hash_pos_it->second->getType()->isPointerTy()) {
521  CHECK(hash_pos_it->second->getType()->getPointerElementType()->isIntegerTy(32));
522  llvm::Value* result = cgen_state_->ir_builder_.CreateLoad(hash_pos_it->second);
523  result = cgen_state_->ir_builder_.CreateSExt(
524  result, get_int_type(64, cgen_state_->context_));
525  return result;
526  }
527  return hash_pos_it->second;
528  }
529  for (auto& arg : cgen_state_->row_func_->args()) {
530  if (arg.getName() == "pos") {
531  CHECK(arg.getType()->isIntegerTy(64));
532  return &arg;
533  }
534  }
535  abort();
536 }
537 
539  const auto uoper = dynamic_cast<const Analyzer::UOper*>(expr);
540  if (!uoper || uoper->get_optype() != kCAST) {
541  return nullptr;
542  }
543  const auto& target_ti = uoper->get_type_info();
544  if (!target_ti.is_integer()) {
545  return nullptr;
546  }
547  return uoper->get_operand();
548 }
549 
550 std::shared_ptr<const Analyzer::Expr> CodeGenerator::hashJoinLhs(
551  const Analyzer::ColumnVar* rhs) const {
552  for (const auto& tautological_eq : plan_state_->join_info_.equi_join_tautologies_) {
553  CHECK(IS_EQUIVALENCE(tautological_eq->get_optype()));
554  if (dynamic_cast<const Analyzer::ExpressionTuple*>(
555  tautological_eq->get_left_operand())) {
556  auto lhs_col = hashJoinLhsTuple(rhs, tautological_eq.get());
557  if (lhs_col) {
558  return lhs_col;
559  }
560  } else {
561  auto eq_right_op = tautological_eq->get_right_operand();
562  if (!rhs->get_type_info().is_string()) {
563  eq_right_op = remove_cast_to_int(eq_right_op);
564  }
565  if (!eq_right_op) {
566  eq_right_op = tautological_eq->get_right_operand();
567  }
568  if (*eq_right_op == *rhs) {
569  auto eq_left_op = tautological_eq->get_left_operand();
570  if (!eq_left_op->get_type_info().is_string()) {
571  eq_left_op = remove_cast_to_int(eq_left_op);
572  }
573  if (!eq_left_op) {
574  eq_left_op = tautological_eq->get_left_operand();
575  }
576  if (eq_left_op->get_type_info().is_geometry()) {
577  // skip cast for a geospatial lhs, since the rhs is likely to be a geospatial
578  // physical col without geospatial type info
579  return nullptr;
580  }
581  const auto eq_left_op_col = dynamic_cast<const Analyzer::ColumnVar*>(eq_left_op);
582  CHECK(eq_left_op_col);
583  if (eq_left_op_col->get_rte_idx() != 0) {
584  return nullptr;
585  }
586  if (rhs->get_type_info().is_string()) {
587  return eq_left_op->deep_copy();
588  }
589  if (rhs->get_type_info().is_array()) {
590  // Note(jclay): Can this be restored from copy as above?
591  // If we fall through to the below return statement,
592  // a superfulous cast from DOUBLE[] to DOUBLE[] is made and
593  // this fails at a later stage in codegen.
594  return nullptr;
595  }
596  return makeExpr<Analyzer::UOper>(
597  rhs->get_type_info(), false, kCAST, eq_left_op->deep_copy());
598  }
599  }
600  }
601  return nullptr;
602 }
603 
604 std::shared_ptr<const Analyzer::ColumnVar> CodeGenerator::hashJoinLhsTuple(
605  const Analyzer::ColumnVar* rhs,
606  const Analyzer::BinOper* tautological_eq) const {
607  const auto lhs_tuple_expr =
608  dynamic_cast<const Analyzer::ExpressionTuple*>(tautological_eq->get_left_operand());
609  const auto rhs_tuple_expr = dynamic_cast<const Analyzer::ExpressionTuple*>(
610  tautological_eq->get_right_operand());
611  CHECK(lhs_tuple_expr && rhs_tuple_expr);
612  const auto& lhs_tuple = lhs_tuple_expr->getTuple();
613  const auto& rhs_tuple = rhs_tuple_expr->getTuple();
614  CHECK_EQ(lhs_tuple.size(), rhs_tuple.size());
615  for (size_t i = 0; i < lhs_tuple.size(); ++i) {
616  if (*rhs_tuple[i] == *rhs) {
617  const auto lhs_col =
618  std::static_pointer_cast<const Analyzer::ColumnVar>(lhs_tuple[i]);
619  return lhs_col->get_rte_idx() == 0 ? lhs_col : nullptr;
620  }
621  }
622  return nullptr;
623 }
int get_table_id() const
Definition: Analyzer.h:194
JoinInfo join_info_
Definition: PlanState.h:62
HOST DEVICE SQLTypes get_subtype() const
Definition: sqltypes.h:312
#define CHECK_EQ(x, y)
Definition: Logger.h:205
std::vector< llvm::Value * > outer_join_match_found_per_level_
Definition: CgenState.h:342
HOST DEVICE int get_size() const
Definition: sqltypes.h:321
const Analyzer::Expr * remove_cast_to_int(const Analyzer::Expr *expr)
Definition: ColumnIR.cpp:538
Definition: sqltypes.h:48
SQLTypes
Definition: sqltypes.h:37
#define IS_EQUIVALENCE(X)
Definition: sqldefs.h:67
CgenState * cgen_state_
llvm::Value * codegenRowId(const Analyzer::ColumnVar *col_var, const CompilationOptions &co)
Definition: ColumnIR.cpp:321
bool is_fp() const
Definition: sqltypes.h:482
HOST DEVICE int get_scale() const
Definition: sqltypes.h:316
llvm::Value * emitExternalCall(const std::string &fname, llvm::Type *ret_type, const std::vector< llvm::Value * > args, const std::vector< llvm::Attribute::AttrKind > &fnattrs={}, const bool has_struct_return=false)
Definition: CgenState.h:222
const Expr * get_right_operand() const
Definition: Analyzer.h:443
llvm::IRBuilder ir_builder_
Definition: CgenState.h:331
std::vector< llvm::Value * > codegenOuterJoinNullPlaceholder(const Analyzer::ColumnVar *col_var, const bool fetch_column, const CompilationOptions &co)
Definition: ColumnIR.cpp:423
llvm::Value * posArg(const Analyzer::Expr *) const
Definition: ColumnIR.cpp:513
std::set< std::pair< TableId, ColumnId > > columns_to_fetch_
Definition: PlanState.h:59
#define CHECK_GE(x, y)
Definition: Logger.h:210
llvm::Value * codgenAdjustFixedEncNull(llvm::Value *, const SQLTypeInfo &)
Definition: ColumnIR.cpp:375
Definition: sqldefs.h:49
const int8_t * output() const
llvm::Value * foundOuterJoinMatch(const size_t nesting_level) const
Definition: ColumnIR.cpp:416
virtual std::vector< llvm::Value * > codegenColumn(const Analyzer::ColumnVar *, const bool fetch_column, const CompilationOptions &)
Definition: ColumnIR.cpp:92
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
std::shared_ptr< Decoder > get_col_decoder(const Analyzer::ColumnVar *col_var)
Definition: ColumnIR.cpp:28
EncodingType get_compression() const
Definition: Analyzer.h:197
static WindowFunctionContext * getActiveWindowFunctionContext(Executor *executor)
std::string to_string(char const *&&v)
llvm::Function * row_func_
Definition: CgenState.h:321
std::set< std::pair< TableId, ColumnId > > columns_to_not_fetch_
Definition: PlanState.h:60
std::vector< llvm::Value * > group_by_expr_cache_
Definition: CgenState.h:338
std::shared_ptr< const Analyzer::Expr > hashJoinLhs(const Analyzer::ColumnVar *rhs) const
Definition: ColumnIR.cpp:550
llvm::Module * module_
Definition: CgenState.h:320
int getLocalColumnId(const Analyzer::ColumnVar *col_var, const bool fetch_column)
Definition: PlanState.cpp:62
size_t get_bit_width(const SQLTypeInfo &ti)
llvm::LLVMContext & context_
Definition: CgenState.h:329
llvm::Function * current_func_
Definition: CgenState.h:323
llvm::Value * get_arg_by_name(llvm::Function *func, const std::string &name)
Definition: Execute.h:162
int get_logical_size() const
Definition: sqltypes.h:322
std::unordered_map< int, std::vector< llvm::Value * > > fetch_cache_
Definition: CgenState.h:332
size_t get_col_bit_width(const Analyzer::ColumnVar *col_var)
Definition: ColumnIR.cpp:81
void setRowNumber(llvm::Value *row_number)
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:27
int64_t bigintval
Definition: sqltypes.h:206
std::unordered_map< int, llvm::Value * > scan_idx_to_hash_pos_
Definition: CgenState.h:343
#define AUTOMATIC_IR_METADATA(CGENSTATE)
std::vector< llvm::Value * > codegenColVar(const Analyzer::ColumnVar *, const bool fetch_column, const bool update_query_plan, const CompilationOptions &)
Definition: ColumnIR.cpp:104
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:78
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:137
SQLTypes decimal_to_int_type(const SQLTypeInfo &ti)
Definition: Datum.cpp:303
PlanState * plan_state_
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:26
#define CHECK_LT(x, y)
Definition: Logger.h:207
Definition: sqltypes.h:52
#define CHECK_LE(x, y)
Definition: Logger.h:208
int get_comp_param() const
Definition: Analyzer.h:198
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:319
std::vector< llvm::Value * > frag_offsets_
Definition: CgenState.h:340
int get_rte_idx() const
Definition: Analyzer.h:196
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:313
llvm::Value * codegenFixedLengthColVarInWindow(const Analyzer::ColumnVar *col_var, llvm::Value *col_byte_stream, llvm::Value *pos_arg)
Definition: ColumnIR.cpp:277
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:300
#define CHECK(condition)
Definition: Logger.h:197
bool is_geometry() const
Definition: sqltypes.h:490
llvm::Value * colByteStream(const Analyzer::ColumnVar *col_var, const bool fetch_column, const bool hoist_literals)
Definition: ColumnIR.cpp:497
llvm::Value * codegenWindowPosition(WindowFunctionContext *window_func_context, llvm::Value *pos_arg)
Definition: ColumnIR.cpp:226
const Expr * get_left_operand() const
Definition: Analyzer.h:442
std::string numeric_type_name(const SQLTypeInfo &ti)
Definition: Execute.h:205
Definition: sqltypes.h:44
SQLTypeInfo columnType
int get_column_id() const
Definition: Analyzer.h:195
bool isLazyFetchColumn(const Analyzer::Expr *target_expr)
Definition: PlanState.cpp:20
bool is_string() const
Definition: sqltypes.h:478
std::shared_ptr< const Analyzer::ColumnVar > hashJoinLhsTuple(const Analyzer::ColumnVar *rhs, const Analyzer::BinOper *tautological_eq) const
Definition: ColumnIR.cpp:604
std::vector< llvm::Value * > codegenVariableLengthStringColVar(llvm::Value *col_byte_stream, llvm::Value *pos_arg)
Definition: ColumnIR.cpp:306
llvm::Value * codegenFixedLengthColVar(const Analyzer::ColumnVar *col_var, llvm::Value *col_byte_stream, llvm::Value *pos_arg)
Definition: ColumnIR.cpp:240
SQLTypes get_phys_int_type(const size_t byte_sz)
Definition: ColumnIR.cpp:356
int adjusted_range_table_index(const Analyzer::ColumnVar *col_var)
Definition: ColumnIR.cpp:86
bool is_array() const
Definition: sqltypes.h:486
const ColumnDescriptor * get_column_descriptor(const int col_id, const int table_id, const Catalog_Namespace::Catalog &cat)
Definition: Execute.h:187
llvm::Value * resolveGroupedColumnReference(const Analyzer::ColumnVar *)
Definition: ColumnIR.cpp:478
llvm::ConstantFP * inlineFpNull(const SQLTypeInfo &)
Definition: CgenState.cpp:65
Executor * executor() const