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