OmniSciDB  fe05a0c208
 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  return window_position;
235 }
236 
237 // Generate code for fixed length column types (number, timestamp or date,
238 // dictionary-encoded string)
240  llvm::Value* col_byte_stream,
241  llvm::Value* pos_arg) {
243  const auto decoder = get_col_decoder(col_var);
244  auto dec_val = decoder->codegenDecode(col_byte_stream, pos_arg, cgen_state_->module_);
245  cgen_state_->ir_builder_.Insert(dec_val);
246  auto dec_type = dec_val->getType();
247  llvm::Value* dec_val_cast{nullptr};
248  const auto& col_ti = col_var->get_type_info();
249  if (dec_type->isIntegerTy()) {
250  auto dec_width = static_cast<llvm::IntegerType*>(dec_type)->getBitWidth();
251  auto col_width = get_col_bit_width(col_var);
252  dec_val_cast = cgen_state_->ir_builder_.CreateCast(
253  static_cast<size_t>(col_width) > dec_width ? llvm::Instruction::CastOps::SExt
254  : llvm::Instruction::CastOps::Trunc,
255  dec_val,
256  get_int_type(col_width, cgen_state_->context_));
257  if ((col_ti.get_compression() == kENCODING_FIXED ||
258  (col_ti.get_compression() == kENCODING_DICT && col_ti.get_size() < 4)) &&
259  !col_ti.get_notnull()) {
260  dec_val_cast = codgenAdjustFixedEncNull(dec_val_cast, col_ti);
261  }
262  } else {
263  CHECK_EQ(kENCODING_NONE, col_ti.get_compression());
264  CHECK(dec_type->isFloatTy() || dec_type->isDoubleTy());
265  if (dec_type->isDoubleTy()) {
266  CHECK(col_ti.get_type() == kDOUBLE);
267  } else if (dec_type->isFloatTy()) {
268  CHECK(col_ti.get_type() == kFLOAT);
269  }
270  dec_val_cast = dec_val;
271  }
272  CHECK(dec_val_cast);
273  return dec_val_cast;
274 }
275 
277  const Analyzer::ColumnVar* col_var,
278  llvm::Value* col_byte_stream,
279  llvm::Value* pos_arg) {
281  const auto orig_bb = cgen_state_->ir_builder_.GetInsertBlock();
282  const auto pos_is_valid =
283  cgen_state_->ir_builder_.CreateICmpSGE(pos_arg, cgen_state_->llInt(int64_t(0)));
284  const auto pos_valid_bb = llvm::BasicBlock::Create(
285  cgen_state_->context_, "window.pos_valid", cgen_state_->current_func_);
286  const auto pos_notvalid_bb = llvm::BasicBlock::Create(
287  cgen_state_->context_, "window.pos_notvalid", cgen_state_->current_func_);
288  cgen_state_->ir_builder_.CreateCondBr(pos_is_valid, pos_valid_bb, pos_notvalid_bb);
289  cgen_state_->ir_builder_.SetInsertPoint(pos_valid_bb);
290  const auto fixed_length_column_lv =
291  codegenFixedLengthColVar(col_var, col_byte_stream, pos_arg);
292  cgen_state_->ir_builder_.CreateBr(pos_notvalid_bb);
293  cgen_state_->ir_builder_.SetInsertPoint(pos_notvalid_bb);
294  const auto window_func_call_phi =
295  cgen_state_->ir_builder_.CreatePHI(fixed_length_column_lv->getType(), 2);
296  window_func_call_phi->addIncoming(fixed_length_column_lv, pos_valid_bb);
297  const auto& col_ti = col_var->get_type_info();
298  const auto null_lv =
299  col_ti.is_fp() ? static_cast<llvm::Value*>(cgen_state_->inlineFpNull(col_ti))
300  : static_cast<llvm::Value*>(cgen_state_->inlineIntNull(col_ti));
301  window_func_call_phi->addIncoming(null_lv, orig_bb);
302  return window_func_call_phi;
303 }
304 
306  llvm::Value* col_byte_stream,
307  llvm::Value* pos_arg) {
309  // real (not dictionary-encoded) strings; store the pointer to the payload
310  auto ptr_and_len =
311  cgen_state_->emitExternalCall("string_decode",
313  {col_byte_stream, pos_arg});
314  // Unpack the pointer + length, see string_decode function.
315  auto str_lv = cgen_state_->emitCall("extract_str_ptr", {ptr_and_len});
316  auto len_lv = cgen_state_->emitCall("extract_str_len", {ptr_and_len});
317  return {ptr_and_len, str_lv, len_lv};
318 }
319 
321  const CompilationOptions& co) {
323  const auto offset_lv = cgen_state_->frag_offsets_[adjusted_range_table_index(col_var)];
324  llvm::Value* start_rowid_lv{nullptr};
325  const auto& table_generation = executor()->getTableGeneration(col_var->get_table_id());
326  if (table_generation.start_rowid > 0) {
327  // Handle the multi-node case: each leaf receives a start rowid used
328  // to offset the local rowid and generate a cluster-wide unique rowid.
329  Datum d;
330  d.bigintval = table_generation.start_rowid;
331  const auto start_rowid = makeExpr<Analyzer::Constant>(kBIGINT, false, d);
332  const auto start_rowid_lvs = codegen(start_rowid.get(), kENCODING_NONE, -1, co);
333  CHECK_EQ(size_t(1), start_rowid_lvs.size());
334  start_rowid_lv = start_rowid_lvs.front();
335  }
336  auto rowid_lv = posArg(col_var);
337  if (offset_lv) {
338  rowid_lv = cgen_state_->ir_builder_.CreateAdd(rowid_lv, offset_lv);
339  } else if (col_var->get_rte_idx() > 0) {
340  auto frag_off_ptr = get_arg_by_name(cgen_state_->row_func_, "frag_row_off");
341  auto input_off_ptr = cgen_state_->ir_builder_.CreateGEP(
342  frag_off_ptr, cgen_state_->llInt(int32_t(col_var->get_rte_idx())));
343  auto rowid_offset_lv = cgen_state_->ir_builder_.CreateLoad(input_off_ptr);
344  rowid_lv = cgen_state_->ir_builder_.CreateAdd(rowid_lv, rowid_offset_lv);
345  }
346  if (table_generation.start_rowid > 0) {
347  CHECK(start_rowid_lv);
348  rowid_lv = cgen_state_->ir_builder_.CreateAdd(rowid_lv, start_rowid_lv);
349  }
350  return rowid_lv;
351 }
352 
353 namespace {
354 
355 SQLTypes get_phys_int_type(const size_t byte_sz) {
356  switch (byte_sz) {
357  case 1:
358  return kBOOLEAN;
359  // TODO: kTINYINT
360  case 2:
361  return kSMALLINT;
362  case 4:
363  return kINT;
364  case 8:
365  return kBIGINT;
366  default:
367  CHECK(false);
368  }
369  return kNULLT;
370 }
371 
372 } // namespace
373 
374 llvm::Value* CodeGenerator::codgenAdjustFixedEncNull(llvm::Value* val,
375  const SQLTypeInfo& col_ti) {
377  CHECK_LT(col_ti.get_size(), col_ti.get_logical_size());
378  const auto col_phys_width = col_ti.get_size() * 8;
379  auto from_typename = "int" + std::to_string(col_phys_width) + "_t";
380  auto adjusted = cgen_state_->ir_builder_.CreateCast(
381  llvm::Instruction::CastOps::Trunc,
382  val,
383  get_int_type(col_phys_width, cgen_state_->context_));
384  if (col_ti.get_compression() == kENCODING_DICT) {
385  from_typename = "u" + from_typename;
386  llvm::Value* from_null{nullptr};
387  switch (col_ti.get_size()) {
388  case 1:
389  from_null = cgen_state_->llInt(std::numeric_limits<uint8_t>::max());
390  break;
391  case 2:
392  from_null = cgen_state_->llInt(std::numeric_limits<uint16_t>::max());
393  break;
394  default:
395  CHECK(false);
396  }
397  return cgen_state_->emitCall(
398  "cast_" + from_typename + "_to_" + numeric_type_name(col_ti) + "_nullable",
399  {adjusted, from_null, cgen_state_->inlineIntNull(col_ti)});
400  }
401  SQLTypeInfo col_phys_ti(get_phys_int_type(col_ti.get_size()),
402  col_ti.get_dimension(),
403  col_ti.get_scale(),
404  false,
406  0,
407  col_ti.get_subtype());
408  return cgen_state_->emitCall(
409  "cast_" + from_typename + "_to_" + numeric_type_name(col_ti) + "_nullable",
410  {adjusted,
411  cgen_state_->inlineIntNull(col_phys_ti),
412  cgen_state_->inlineIntNull(col_ti)});
413 }
414 
415 llvm::Value* CodeGenerator::foundOuterJoinMatch(const size_t nesting_level) const {
416  CHECK_GE(nesting_level, size_t(1));
417  CHECK_LE(nesting_level,
418  static_cast<size_t>(cgen_state_->outer_join_match_found_per_level_.size()));
419  return cgen_state_->outer_join_match_found_per_level_[nesting_level - 1];
420 }
421 
423  const Analyzer::ColumnVar* col_var,
424  const bool fetch_column,
425  const CompilationOptions& co) {
427  const auto grouped_col_lv = resolveGroupedColumnReference(col_var);
428  if (grouped_col_lv) {
429  return {grouped_col_lv};
430  }
431  const auto outer_join_args_bb = llvm::BasicBlock::Create(
432  cgen_state_->context_, "outer_join_args", cgen_state_->current_func_);
433  const auto outer_join_nulls_bb = llvm::BasicBlock::Create(
434  cgen_state_->context_, "outer_join_nulls", cgen_state_->current_func_);
435  const auto phi_bb = llvm::BasicBlock::Create(
436  cgen_state_->context_, "outer_join_phi", cgen_state_->current_func_);
437  const auto outer_join_match_lv = foundOuterJoinMatch(col_var->get_rte_idx());
438  CHECK(outer_join_match_lv);
439  cgen_state_->ir_builder_.CreateCondBr(
440  outer_join_match_lv, outer_join_args_bb, outer_join_nulls_bb);
441  const auto back_from_outer_join_bb = llvm::BasicBlock::Create(
442  cgen_state_->context_, "back_from_outer_join", cgen_state_->current_func_);
443  cgen_state_->ir_builder_.SetInsertPoint(outer_join_args_bb);
445  const auto orig_lvs = codegenColVar(col_var, fetch_column, true, co);
446  cgen_state_->ir_builder_.CreateBr(phi_bb);
447  cgen_state_->ir_builder_.SetInsertPoint(outer_join_nulls_bb);
448  const auto& null_ti = col_var->get_type_info();
449  if ((null_ti.is_string() && null_ti.get_compression() == kENCODING_NONE) ||
450  null_ti.is_array() || null_ti.is_geometry()) {
451  throw std::runtime_error("Projection type " + null_ti.get_type_name() +
452  " not supported for outer joins yet");
453  }
454  const auto null_constant = makeExpr<Analyzer::Constant>(null_ti, true, Datum{0});
455  const auto null_target_lvs =
456  codegen(null_constant.get(),
457  false,
460  cgen_state_->ir_builder_.CreateBr(phi_bb);
461  CHECK_EQ(orig_lvs.size(), null_target_lvs.size());
462  cgen_state_->ir_builder_.SetInsertPoint(phi_bb);
463  std::vector<llvm::Value*> target_lvs;
464  for (size_t i = 0; i < orig_lvs.size(); ++i) {
465  const auto target_type = orig_lvs[i]->getType();
466  CHECK_EQ(target_type, null_target_lvs[i]->getType());
467  auto target_phi = cgen_state_->ir_builder_.CreatePHI(target_type, 2);
468  target_phi->addIncoming(orig_lvs[i], outer_join_args_bb);
469  target_phi->addIncoming(null_target_lvs[i], outer_join_nulls_bb);
470  target_lvs.push_back(target_phi);
471  }
472  cgen_state_->ir_builder_.CreateBr(back_from_outer_join_bb);
473  cgen_state_->ir_builder_.SetInsertPoint(back_from_outer_join_bb);
474  return target_lvs;
475 }
476 
478  const Analyzer::ColumnVar* col_var) {
479  auto col_id = col_var->get_column_id();
480  if (col_var->get_rte_idx() >= 0) {
481  return nullptr;
482  }
483  CHECK((col_id == 0) || (col_var->get_rte_idx() >= 0 && col_var->get_table_id() > 0));
484  const auto var = dynamic_cast<const Analyzer::Var*>(col_var);
485  CHECK(var);
486  col_id = var->get_varno();
487  CHECK_GE(col_id, 1);
488  if (var->get_which_row() == Analyzer::Var::kGROUPBY) {
489  CHECK_LE(static_cast<size_t>(col_id), cgen_state_->group_by_expr_cache_.size());
490  return cgen_state_->group_by_expr_cache_[col_id - 1];
491  }
492  return nullptr;
493 }
494 
495 // returns the byte stream argument and the position for the given column
497  const bool fetch_column,
498  const bool hoist_literals) {
499  CHECK_GE(cgen_state_->row_func_->arg_size(), size_t(3));
500  const auto stream_arg_name =
501  "col_buf" + std::to_string(plan_state_->getLocalColumnId(col_var, fetch_column));
502  for (auto& arg : cgen_state_->row_func_->args()) {
503  if (arg.getName() == stream_arg_name) {
504  CHECK(arg.getType() == llvm::Type::getInt8PtrTy(cgen_state_->context_));
505  return &arg;
506  }
507  }
508  CHECK(false);
509  return nullptr;
510 }
511 
512 llvm::Value* CodeGenerator::posArg(const Analyzer::Expr* expr) const {
514  const auto col_var = dynamic_cast<const Analyzer::ColumnVar*>(expr);
515  if (col_var && col_var->get_rte_idx() > 0) {
516  const auto hash_pos_it =
517  cgen_state_->scan_idx_to_hash_pos_.find(col_var->get_rte_idx());
518  CHECK(hash_pos_it != cgen_state_->scan_idx_to_hash_pos_.end());
519  if (hash_pos_it->second->getType()->isPointerTy()) {
520  CHECK(hash_pos_it->second->getType()->getPointerElementType()->isIntegerTy(32));
521  llvm::Value* result = cgen_state_->ir_builder_.CreateLoad(hash_pos_it->second);
522  result = cgen_state_->ir_builder_.CreateSExt(
523  result, get_int_type(64, cgen_state_->context_));
524  return result;
525  }
526  return hash_pos_it->second;
527  }
528  for (auto& arg : cgen_state_->row_func_->args()) {
529  if (arg.getName() == "pos") {
530  CHECK(arg.getType()->isIntegerTy(64));
531  return &arg;
532  }
533  }
534  abort();
535 }
536 
538  const auto uoper = dynamic_cast<const Analyzer::UOper*>(expr);
539  if (!uoper || uoper->get_optype() != kCAST) {
540  return nullptr;
541  }
542  const auto& target_ti = uoper->get_type_info();
543  if (!target_ti.is_integer()) {
544  return nullptr;
545  }
546  return uoper->get_operand();
547 }
548 
549 std::shared_ptr<const Analyzer::Expr> CodeGenerator::hashJoinLhs(
550  const Analyzer::ColumnVar* rhs) const {
551  for (const auto& tautological_eq : plan_state_->join_info_.equi_join_tautologies_) {
552  CHECK(IS_EQUIVALENCE(tautological_eq->get_optype()));
553  if (dynamic_cast<const Analyzer::ExpressionTuple*>(
554  tautological_eq->get_left_operand())) {
555  auto lhs_col = hashJoinLhsTuple(rhs, tautological_eq.get());
556  if (lhs_col) {
557  return lhs_col;
558  }
559  } else {
560  auto eq_right_op = tautological_eq->get_right_operand();
561  if (!rhs->get_type_info().is_string()) {
562  eq_right_op = remove_cast_to_int(eq_right_op);
563  }
564  if (!eq_right_op) {
565  eq_right_op = tautological_eq->get_right_operand();
566  }
567  if (*eq_right_op == *rhs) {
568  auto eq_left_op = tautological_eq->get_left_operand();
569  if (!eq_left_op->get_type_info().is_string()) {
570  eq_left_op = remove_cast_to_int(eq_left_op);
571  }
572  if (!eq_left_op) {
573  eq_left_op = tautological_eq->get_left_operand();
574  }
575  if (eq_left_op->get_type_info().is_geometry()) {
576  // skip cast for a geospatial lhs, since the rhs is likely to be a geospatial
577  // physical col without geospatial type info
578  return nullptr;
579  }
580  if (is_constructed_point(eq_left_op)) {
581  // skip cast for a constructed point lhs
582  return nullptr;
583  }
584  const auto eq_left_op_col = dynamic_cast<const Analyzer::ColumnVar*>(eq_left_op);
585  CHECK(eq_left_op_col);
586  if (eq_left_op_col->get_rte_idx() != 0) {
587  return nullptr;
588  }
589  if (rhs->get_type_info().is_string()) {
590  return eq_left_op->deep_copy();
591  }
592  if (rhs->get_type_info().is_array()) {
593  // Note(jclay): Can this be restored from copy as above?
594  // If we fall through to the below return statement,
595  // a superfulous cast from DOUBLE[] to DOUBLE[] is made and
596  // this fails at a later stage in codegen.
597  return nullptr;
598  }
599  return makeExpr<Analyzer::UOper>(
600  rhs->get_type_info(), false, kCAST, eq_left_op->deep_copy());
601  }
602  }
603  }
604  return nullptr;
605 }
606 
607 std::shared_ptr<const Analyzer::ColumnVar> CodeGenerator::hashJoinLhsTuple(
608  const Analyzer::ColumnVar* rhs,
609  const Analyzer::BinOper* tautological_eq) const {
610  const auto lhs_tuple_expr =
611  dynamic_cast<const Analyzer::ExpressionTuple*>(tautological_eq->get_left_operand());
612  const auto rhs_tuple_expr = dynamic_cast<const Analyzer::ExpressionTuple*>(
613  tautological_eq->get_right_operand());
614  CHECK(lhs_tuple_expr && rhs_tuple_expr);
615  const auto& lhs_tuple = lhs_tuple_expr->getTuple();
616  const auto& rhs_tuple = rhs_tuple_expr->getTuple();
617  CHECK_EQ(lhs_tuple.size(), rhs_tuple.size());
618  for (size_t i = 0; i < lhs_tuple.size(); ++i) {
619  if (*rhs_tuple[i] == *rhs) {
620  const auto lhs_col =
621  std::static_pointer_cast<const Analyzer::ColumnVar>(lhs_tuple[i]);
622  return lhs_col->get_rte_idx() == 0 ? lhs_col : nullptr;
623  }
624  }
625  return nullptr;
626 }
int get_table_id() const
Definition: Analyzer.h:194
JoinInfo join_info_
Definition: PlanState.h:64
HOST DEVICE SQLTypes get_subtype() const
Definition: sqltypes.h:315
#define CHECK_EQ(x, y)
Definition: Logger.h:211
std::vector< llvm::Value * > outer_join_match_found_per_level_
Definition: CgenState.h:346
HOST DEVICE int get_size() const
Definition: sqltypes.h:324
const Analyzer::Expr * remove_cast_to_int(const Analyzer::Expr *expr)
Definition: ColumnIR.cpp:537
Definition: sqltypes.h:48
SQLTypes
Definition: sqltypes.h:37
#define IS_EQUIVALENCE(X)
Definition: sqldefs.h:67
tuple d
Definition: test_fsi.py:9
CgenState * cgen_state_
llvm::Value * codegenRowId(const Analyzer::ColumnVar *col_var, const CompilationOptions &co)
Definition: ColumnIR.cpp:320
bool is_fp() const
Definition: sqltypes.h:493
HOST DEVICE int get_scale() const
Definition: sqltypes.h:319
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:228
const Expr * get_right_operand() const
Definition: Analyzer.h:443
bool is_constructed_point(const Analyzer::Expr *expr)
Definition: Execute.h:1180
llvm::IRBuilder ir_builder_
Definition: CgenState.h:335
std::vector< llvm::Value * > codegenOuterJoinNullPlaceholder(const Analyzer::ColumnVar *col_var, const bool fetch_column, const CompilationOptions &co)
Definition: ColumnIR.cpp:422
llvm::Value * posArg(const Analyzer::Expr *) const
Definition: ColumnIR.cpp:512
std::set< std::pair< TableId, ColumnId > > columns_to_fetch_
Definition: PlanState.h:61
#define CHECK_GE(x, y)
Definition: Logger.h:216
llvm::Value * codgenAdjustFixedEncNull(llvm::Value *, const SQLTypeInfo &)
Definition: ColumnIR.cpp:374
Definition: sqldefs.h:49
const int8_t * output() const
llvm::Value * foundOuterJoinMatch(const size_t nesting_level) const
Definition: ColumnIR.cpp:415
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:325
std::set< std::pair< TableId, ColumnId > > columns_to_not_fetch_
Definition: PlanState.h:62
std::vector< llvm::Value * > group_by_expr_cache_
Definition: CgenState.h:342
std::shared_ptr< const Analyzer::Expr > hashJoinLhs(const Analyzer::ColumnVar *rhs) const
Definition: ColumnIR.cpp:549
llvm::Module * module_
Definition: CgenState.h:324
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:333
llvm::Function * current_func_
Definition: CgenState.h:327
llvm::Value * get_arg_by_name(llvm::Function *func, const std::string &name)
Definition: Execute.h:167
int get_logical_size() const
Definition: sqltypes.h:325
std::unordered_map< int, std::vector< llvm::Value * > > fetch_cache_
Definition: CgenState.h:336
size_t get_col_bit_width(const Analyzer::ColumnVar *col_var)
Definition: ColumnIR.cpp:81
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:28
int64_t bigintval
Definition: sqltypes.h:209
std::unordered_map< int, llvm::Value * > scan_idx_to_hash_pos_
Definition: CgenState.h:347
#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:174
SQLTypes decimal_to_int_type(const SQLTypeInfo &ti)
Definition: Datum.cpp:419
PlanState * plan_state_
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:28
#define CHECK_LT(x, y)
Definition: Logger.h:213
Definition: sqltypes.h:52
#define CHECK_LE(x, y)
Definition: Logger.h:214
int get_comp_param() const
Definition: Analyzer.h:198
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:322
std::vector< llvm::Value * > frag_offsets_
Definition: CgenState.h:344
int get_rte_idx() const
Definition: Analyzer.h:196
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:316
llvm::Value * codegenFixedLengthColVarInWindow(const Analyzer::ColumnVar *col_var, llvm::Value *col_byte_stream, llvm::Value *pos_arg)
Definition: ColumnIR.cpp:276
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:306
#define CHECK(condition)
Definition: Logger.h:203
bool is_geometry() const
Definition: sqltypes.h:501
llvm::Value * colByteStream(const Analyzer::ColumnVar *col_var, const bool fetch_column, const bool hoist_literals)
Definition: ColumnIR.cpp:496
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:210
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:489
std::shared_ptr< const Analyzer::ColumnVar > hashJoinLhsTuple(const Analyzer::ColumnVar *rhs, const Analyzer::BinOper *tautological_eq) const
Definition: ColumnIR.cpp:607
std::vector< llvm::Value * > codegenVariableLengthStringColVar(llvm::Value *col_byte_stream, llvm::Value *pos_arg)
Definition: ColumnIR.cpp:305
llvm::Value * codegenFixedLengthColVar(const Analyzer::ColumnVar *col_var, llvm::Value *col_byte_stream, llvm::Value *pos_arg)
Definition: ColumnIR.cpp:239
SQLTypes get_phys_int_type(const size_t byte_sz)
Definition: ColumnIR.cpp:355
int adjusted_range_table_index(const Analyzer::ColumnVar *col_var)
Definition: ColumnIR.cpp:86
bool is_array() const
Definition: sqltypes.h:497
const ColumnDescriptor * get_column_descriptor(const int col_id, const int table_id, const Catalog_Namespace::Catalog &cat)
Definition: Execute.h:192
llvm::Value * resolveGroupedColumnReference(const Analyzer::ColumnVar *)
Definition: ColumnIR.cpp:477
llvm::ConstantFP * inlineFpNull(const SQLTypeInfo &)
Definition: CgenState.cpp:66
Executor * executor() const