OmniSciDB  b28c0d5765
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
QueryRewrite.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "QueryRewrite.h"
18 
19 #include <algorithm>
20 #include <memory>
21 #include <vector>
22 
23 #include "ExpressionRange.h"
24 #include "ExpressionRewrite.h"
25 #include "Logger/Logger.h"
26 #include "Parser/ParserNode.h"
27 #include "Shared/sqltypes.h"
28 
30  const RelAlgExecutionUnit& ra_exe_unit_in) const {
31  auto rewritten_exe_unit = rewriteConstrainedByIn(ra_exe_unit_in);
32  auto rewritten_exe_unit_for_agg_on_gby_col =
33  rewriteAggregateOnGroupByColumn(rewritten_exe_unit);
34  return rewriteOverlapsJoin(rewritten_exe_unit_for_agg_on_gby_col);
35 }
36 
38  const RelAlgExecutionUnit& ra_exe_unit_in) const {
40  return ra_exe_unit_in;
41  }
42  if (ra_exe_unit_in.join_quals.empty()) {
43  return ra_exe_unit_in;
44  }
45 
46  std::list<std::shared_ptr<Analyzer::Expr>> quals;
47  quals.insert(quals.end(), ra_exe_unit_in.quals.begin(), ra_exe_unit_in.quals.end());
48 
49  JoinQualsPerNestingLevel join_condition_per_nesting_level;
50  for (const auto& join_condition_in : ra_exe_unit_in.join_quals) {
51  JoinCondition join_condition{{}, join_condition_in.type};
52 
53  for (const auto& join_qual_expr_in : join_condition_in.quals) {
54  bool try_to_rewrite_expr_to_overlaps_join = false;
56  auto func_oper = dynamic_cast<Analyzer::FunctionOper*>(join_qual_expr_in.get());
57  if (func_oper) {
58  const auto func_name = func_oper->getName();
60  try_to_rewrite_expr_to_overlaps_join = true;
62  }
63  }
64  auto bin_oper = dynamic_cast<Analyzer::BinOper*>(join_qual_expr_in.get());
65  if (bin_oper && (bin_oper->get_optype() == kLE || bin_oper->get_optype() == kLT)) {
66  auto lhs =
67  dynamic_cast<const Analyzer::GeoOperator*>(bin_oper->get_left_operand());
68  auto rhs = dynamic_cast<const Analyzer::Constant*>(bin_oper->get_right_operand());
69  if (g_enable_distance_rangejoin && lhs && rhs) {
70  try_to_rewrite_expr_to_overlaps_join = true;
72  }
73  }
74  boost::optional<OverlapsJoinConjunction> new_overlaps_quals = boost::none;
75  if (try_to_rewrite_expr_to_overlaps_join) {
76  new_overlaps_quals = rewrite_overlaps_conjunction(
77  join_qual_expr_in, ra_exe_unit_in.input_descs, rewrite_type, executor_);
78  }
79  if (new_overlaps_quals) {
80  const auto& overlaps_quals = *new_overlaps_quals;
81  // Add overlaps qual
82  join_condition.quals.insert(join_condition.quals.end(),
83  overlaps_quals.join_quals.begin(),
84  overlaps_quals.join_quals.end());
85  // Add original quals
86  join_condition.quals.insert(join_condition.quals.end(),
87  overlaps_quals.quals.begin(),
88  overlaps_quals.quals.end());
89  } else {
90  join_condition.quals.push_back(join_qual_expr_in);
91  }
92  }
93  join_condition_per_nesting_level.push_back(join_condition);
94  }
95  return {ra_exe_unit_in.input_descs,
96  ra_exe_unit_in.input_col_descs,
97  ra_exe_unit_in.simple_quals,
98  quals,
99  join_condition_per_nesting_level,
100  ra_exe_unit_in.groupby_exprs,
101  ra_exe_unit_in.target_exprs,
102  ra_exe_unit_in.target_exprs_original_type_infos,
103  ra_exe_unit_in.estimator,
104  ra_exe_unit_in.sort_info,
105  ra_exe_unit_in.scan_limit,
106  ra_exe_unit_in.query_hint,
107  ra_exe_unit_in.query_plan_dag_hash,
108  ra_exe_unit_in.hash_table_build_plan_dag,
109  ra_exe_unit_in.table_id_to_node_map,
110  ra_exe_unit_in.use_bump_allocator};
111 }
112 
114  const RelAlgExecutionUnit& ra_exe_unit_in) const {
115  if (ra_exe_unit_in.groupby_exprs.empty()) {
116  return ra_exe_unit_in;
117  }
118  if (ra_exe_unit_in.groupby_exprs.size() == 1 && !ra_exe_unit_in.groupby_exprs.front()) {
119  return ra_exe_unit_in;
120  }
121  if (!ra_exe_unit_in.simple_quals.empty()) {
122  return ra_exe_unit_in;
123  }
124  if (ra_exe_unit_in.quals.size() != 1) {
125  return ra_exe_unit_in;
126  }
127  auto in_vals =
128  std::dynamic_pointer_cast<Analyzer::InValues>(ra_exe_unit_in.quals.front());
129  if (!in_vals) {
130  in_vals = std::dynamic_pointer_cast<Analyzer::InValues>(
131  rewrite_expr(ra_exe_unit_in.quals.front().get()));
132  }
133  if (!in_vals || in_vals->get_value_list().empty()) {
134  return ra_exe_unit_in;
135  }
136  for (const auto& in_val : in_vals->get_value_list()) {
137  if (!std::dynamic_pointer_cast<Analyzer::Constant>(in_val)) {
138  break;
139  }
140  }
141  if (dynamic_cast<const Analyzer::CaseExpr*>(in_vals->get_arg())) {
142  return ra_exe_unit_in;
143  }
144  auto in_val_cv = dynamic_cast<const Analyzer::ColumnVar*>(in_vals->get_arg());
145  if (in_val_cv) {
146  auto it = std::find_if(
147  ra_exe_unit_in.groupby_exprs.begin(),
148  ra_exe_unit_in.groupby_exprs.end(),
149  [&in_val_cv](std::shared_ptr<Analyzer::Expr> groupby_expr) {
150  if (auto groupby_cv =
151  std::dynamic_pointer_cast<Analyzer::ColumnVar>(groupby_expr)) {
152  return *in_val_cv == *groupby_cv.get();
153  }
154  return false;
155  });
156  if (it != ra_exe_unit_in.groupby_exprs.end()) {
157  // we do not need to deploy case-when rewriting when in_val cv is listed as groupby
158  // col i.e., ... WHERE v IN (SELECT DISTINCT v FROM ...)
159  return ra_exe_unit_in;
160  }
161  }
162  auto case_expr = generateCaseForDomainValues(in_vals.get());
163  return rewriteConstrainedByInImpl(ra_exe_unit_in, case_expr, in_vals.get());
164 }
165 
167  const RelAlgExecutionUnit& ra_exe_unit_in,
168  const std::shared_ptr<Analyzer::CaseExpr> case_expr,
169  const Analyzer::InValues* in_vals) const {
170  std::list<std::shared_ptr<Analyzer::Expr>> new_groupby_list;
171  std::vector<Analyzer::Expr*> new_target_exprs;
172  bool rewrite{false};
173  size_t groupby_idx{0};
174  auto it = ra_exe_unit_in.groupby_exprs.begin();
175  for (const auto& group_expr : ra_exe_unit_in.groupby_exprs) {
176  CHECK(group_expr);
177  ++groupby_idx;
178  if (*group_expr == *in_vals->get_arg()) {
179  const auto expr_range = getExpressionRange(it->get(), query_infos_, executor_);
180  if (expr_range.getType() != ExpressionRangeType::Integer) {
181  ++it;
182  continue;
183  }
184  const size_t range_sz = expr_range.getIntMax() - expr_range.getIntMin() + 1;
185  if (range_sz <= in_vals->get_value_list().size() * g_constrained_by_in_threshold) {
186  ++it;
187  continue;
188  }
189  new_groupby_list.push_back(case_expr);
190  for (size_t i = 0; i < ra_exe_unit_in.target_exprs.size(); ++i) {
191  const auto target = ra_exe_unit_in.target_exprs[i];
192  if (*target == *in_vals->get_arg()) {
193  auto var_case_expr = makeExpr<Analyzer::Var>(
194  case_expr->get_type_info(), Analyzer::Var::kGROUPBY, groupby_idx);
195  target_exprs_owned_.push_back(var_case_expr);
196  new_target_exprs.push_back(var_case_expr.get());
197  } else {
198  new_target_exprs.push_back(target);
199  }
200  }
201  rewrite = true;
202  } else {
203  new_groupby_list.push_back(group_expr);
204  }
205  ++it;
206  }
207  if (!rewrite) {
208  return ra_exe_unit_in;
209  }
210  return {ra_exe_unit_in.input_descs,
211  ra_exe_unit_in.input_col_descs,
212  ra_exe_unit_in.simple_quals,
213  ra_exe_unit_in.quals,
214  ra_exe_unit_in.join_quals,
215  new_groupby_list,
216  new_target_exprs,
217  ra_exe_unit_in.target_exprs_original_type_infos,
218  nullptr,
219  ra_exe_unit_in.sort_info,
220  ra_exe_unit_in.scan_limit,
221  ra_exe_unit_in.query_hint,
222  ra_exe_unit_in.query_plan_dag_hash,
223  ra_exe_unit_in.hash_table_build_plan_dag,
224  ra_exe_unit_in.table_id_to_node_map};
225 }
226 
227 std::shared_ptr<Analyzer::CaseExpr> QueryRewriter::generateCaseForDomainValues(
228  const Analyzer::InValues* in_vals) {
229  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
230  case_expr_list;
231  auto in_val_arg = in_vals->get_arg()->deep_copy();
232  for (const auto& in_val : in_vals->get_value_list()) {
233  auto case_cond = makeExpr<Analyzer::BinOper>(
234  SQLTypeInfo(kBOOLEAN, true), false, kEQ, kONE, in_val_arg, in_val);
235  auto in_val_copy = in_val->deep_copy();
236  auto ti = in_val_copy->get_type_info();
237  if (ti.is_string() && ti.get_compression() == kENCODING_DICT) {
238  ti.set_comp_param(0);
239  }
240  in_val_copy->set_type_info(ti);
241  case_expr_list.emplace_back(case_cond, in_val_copy);
242  }
243  // TODO(alex): refine the expression range for case with empty else expression;
244  // for now, add a dummy else which should never be taken
245  auto else_expr = case_expr_list.front().second;
246  return makeExpr<Analyzer::CaseExpr>(
247  case_expr_list.front().second->get_type_info(), false, case_expr_list, else_expr);
248 }
249 
250 std::shared_ptr<Analyzer::CaseExpr>
252  std::shared_ptr<Analyzer::Expr> expr) const {
253  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
254  case_expr_list;
255  auto is_null = std::make_shared<Analyzer::UOper>(kBOOLEAN, kISNULL, expr);
256  auto is_not_null = std::make_shared<Analyzer::UOper>(kBOOLEAN, kNOT, is_null);
257  Datum then_d;
258  then_d.bigintval = 1;
259  const auto then_constant = makeExpr<Analyzer::Constant>(kBIGINT, false, then_d);
260  case_expr_list.emplace_back(is_not_null, then_constant);
261  Datum else_d;
262  else_d.bigintval = 0;
263  const auto else_constant = makeExpr<Analyzer::Constant>(kBIGINT, false, else_d);
264  auto case_expr = makeExpr<Analyzer::CaseExpr>(
265  then_constant->get_type_info(), false, case_expr_list, else_constant);
266  return case_expr;
267 }
268 
269 namespace {
270 
271 // TODO(adb): centralize and share (e..g with insert_one_dict_str)
272 bool check_string_id_overflow(const int32_t string_id, const SQLTypeInfo& ti) {
273  switch (ti.get_size()) {
274  case 1:
275  return string_id > max_valid_int_value<int8_t>();
276  case 2:
277  return string_id > max_valid_int_value<int16_t>();
278  case 4:
279  return string_id > max_valid_int_value<int32_t>();
280  default:
281  UNREACHABLE();
282  }
283  UNREACHABLE();
284  return false;
285 }
286 
287 } // namespace
288 
289 /* Rewrites an update query of the form `SELECT new_value, OFFSET_IN_FRAGMENT() FROM t
290  * WHERE <update_filter_condition>` to `SELECT CASE WHEN <update_filer_condition> THEN
291  * new_value ELSE existing value END FROM t`
292  */
294  const RelAlgExecutionUnit& ra_exe_unit_in,
295  std::shared_ptr<Analyzer::Expr> column_to_update) const {
296  CHECK_EQ(ra_exe_unit_in.target_exprs.size(), size_t(2));
297  CHECK(ra_exe_unit_in.groupby_exprs.size() == 1 &&
298  !ra_exe_unit_in.groupby_exprs.front());
299 
300  if (ra_exe_unit_in.join_quals.size() > 0) {
301  throw std::runtime_error("Update via join not yet supported for temporary tables.");
302  }
303 
304  auto new_column_value = ra_exe_unit_in.target_exprs.front()->deep_copy();
305  const auto& new_column_ti = new_column_value->get_type_info();
306  if (column_to_update->get_type_info().is_dict_encoded_string()) {
307  CHECK(new_column_ti.is_dict_encoded_string());
308  if (new_column_ti.get_comp_param() > 0 &&
309  new_column_ti.get_comp_param() !=
310  column_to_update->get_type_info().get_comp_param()) {
311  throw std::runtime_error(
312  "Updating a dictionary encoded string using another dictionary encoded string "
313  "column is not yet supported, unless both columns share dictionaries.");
314  }
315  if (auto uoper = dynamic_cast<Analyzer::UOper*>(new_column_value.get())) {
316  if (uoper->get_optype() == kCAST &&
317  dynamic_cast<const Analyzer::Constant*>(uoper->get_operand())) {
318  const auto original_constant_expr =
319  dynamic_cast<const Analyzer::Constant*>(uoper->get_operand());
320  CHECK(original_constant_expr);
321  CHECK(original_constant_expr->get_type_info().is_string());
322  // extract the string, insert it into the dict for the table we are updating,
323  // and place the dictionary ID in the oper
324  auto cat = executor_->getCatalog();
325  CHECK(cat);
326 
327  CHECK(column_to_update->get_type_info().is_dict_encoded_string());
328  const auto dict_id = column_to_update->get_type_info().get_comp_param();
329  std::map<int, StringDictionary*> string_dicts;
330  const auto dd = cat->getMetadataForDict(dict_id, /*load_dict=*/true);
331  CHECK(dd);
332  auto string_dict = dd->stringDict;
333  CHECK(string_dict);
334 
335  auto string_id =
336  string_dict->getOrAdd(*original_constant_expr->get_constval().stringval);
337  if (check_string_id_overflow(string_id, column_to_update->get_type_info())) {
338  throw std::runtime_error(
339  "Ran out of space in dictionary, cannot update column with dictionary "
340  "encoded string value. Dictionary ID: " +
341  std::to_string(dict_id));
342  }
343  if (string_id == inline_int_null_value<int32_t>()) {
344  string_id = inline_fixed_encoding_null_val(column_to_update->get_type_info());
345  }
346 
347  // Codegen expects a string value. The string will be
348  // resolved to its ID during Constant codegen. Copy the string from the
349  // original expr
350  Datum datum;
351  datum.stringval =
352  new std::string(*original_constant_expr->get_constval().stringval);
353  Datum new_string_datum{datum};
354 
355  new_column_value =
356  makeExpr<Analyzer::Constant>(column_to_update->get_type_info(),
357  original_constant_expr->get_is_null(),
358  new_string_datum);
359 
360  // Roll the string dict generation forward, as we have added a string
361  auto row_set_mem_owner = executor_->getRowSetMemoryOwner();
362  CHECK(row_set_mem_owner);
363  auto& str_dict_generations = row_set_mem_owner->getStringDictionaryGenerations();
364  if (str_dict_generations.getGeneration(dict_id) > -1) {
365  str_dict_generations.updateGeneration(dict_id,
366  string_dict->storageEntryCount());
367  } else {
368  // Simple update with no filters does not use a CASE, and therefore does not add
369  // a valid generation
370  str_dict_generations.setGeneration(dict_id, string_dict->storageEntryCount());
371  }
372  }
373  }
374  }
375 
376  auto input_col_descs = ra_exe_unit_in.input_col_descs;
377 
378  std::shared_ptr<Analyzer::Expr> filter;
379  std::vector<std::shared_ptr<Analyzer::Expr>> filter_exprs;
380  filter_exprs.insert(filter_exprs.end(),
381  ra_exe_unit_in.simple_quals.begin(),
382  ra_exe_unit_in.simple_quals.end());
383  filter_exprs.insert(
384  filter_exprs.end(), ra_exe_unit_in.quals.begin(), ra_exe_unit_in.quals.end());
385 
386  if (filter_exprs.size() > 0) {
387  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
388  case_expr_list;
389  if (filter_exprs.size() == 1) {
390  filter = filter_exprs.front();
391  } else {
392  filter = std::accumulate(
393  std::next(filter_exprs.begin()),
394  filter_exprs.end(),
395  filter_exprs.front(),
396  [](const std::shared_ptr<Analyzer::Expr> a,
397  const std::shared_ptr<Analyzer::Expr> b) {
398  CHECK_EQ(a->get_type_info().get_type(), b->get_type_info().get_type());
399  return makeExpr<Analyzer::BinOper>(a->get_type_info().get_type(),
400  SQLOps::kAND,
402  a->deep_copy(),
403  b->deep_copy());
404  });
405  }
406  auto when_expr = filter; // only one filter, will be a BinOper if multiple filters
407  case_expr_list.emplace_back(std::make_pair(when_expr, new_column_value));
408  auto case_expr = Parser::CaseExpr::normalize(case_expr_list, column_to_update);
409 
410  auto col_to_update_var =
411  std::dynamic_pointer_cast<Analyzer::ColumnVar>(column_to_update);
412  CHECK(col_to_update_var);
413  auto col_to_update_desc =
414  std::make_shared<const InputColDescriptor>(col_to_update_var->get_column_id(),
415  col_to_update_var->get_table_id(),
416  col_to_update_var->get_rte_idx());
417  auto existing_col_desc_it = std::find_if(
418  input_col_descs.begin(),
419  input_col_descs.end(),
420  [&col_to_update_desc](const std::shared_ptr<const InputColDescriptor>& in) {
421  return *in == *col_to_update_desc;
422  });
423  if (existing_col_desc_it == input_col_descs.end()) {
424  input_col_descs.push_back(col_to_update_desc);
425  }
426  target_exprs_owned_.emplace_back(case_expr);
427  } else {
428  // no filters, simply project the update value
429  target_exprs_owned_.emplace_back(new_column_value);
430  }
431 
432  std::vector<Analyzer::Expr*> target_exprs;
433  CHECK_EQ(target_exprs_owned_.size(), size_t(1));
434  target_exprs.emplace_back(target_exprs_owned_.front().get());
435 
436  RelAlgExecutionUnit rewritten_exe_unit{ra_exe_unit_in.input_descs,
437  input_col_descs,
438  {},
439  {},
440  ra_exe_unit_in.join_quals,
441  ra_exe_unit_in.groupby_exprs,
442  target_exprs,
443  ra_exe_unit_in.target_exprs_original_type_infos,
444  ra_exe_unit_in.estimator,
445  ra_exe_unit_in.sort_info,
446  ra_exe_unit_in.scan_limit,
447  ra_exe_unit_in.query_hint,
448  ra_exe_unit_in.query_plan_dag_hash,
449  ra_exe_unit_in.hash_table_build_plan_dag,
450  ra_exe_unit_in.table_id_to_node_map,
451  ra_exe_unit_in.use_bump_allocator,
452  ra_exe_unit_in.union_all,
453  ra_exe_unit_in.query_state};
454  return rewritten_exe_unit;
455 }
456 
457 /* Rewrites a delete query of the form `SELECT OFFSET_IN_FRAGMENT() FROM t
458  * WHERE <delete_filter_condition>` to `SELECT CASE WHEN <delete_filter_condition> THEN
459  * true ELSE existing value END FROM t`
460  */
462  const RelAlgExecutionUnit& ra_exe_unit_in,
463  std::shared_ptr<Analyzer::ColumnVar> delete_column) const {
464  CHECK_EQ(ra_exe_unit_in.target_exprs.size(), size_t(1));
465  CHECK(ra_exe_unit_in.groupby_exprs.size() == 1 &&
466  !ra_exe_unit_in.groupby_exprs.front());
467 
468  // TODO(adb): is this possible?
469  if (ra_exe_unit_in.join_quals.size() > 0) {
470  throw std::runtime_error("Delete via join not yet supported for temporary tables.");
471  }
472 
473  Datum true_datum;
474  true_datum.boolval = true;
475  const auto deleted_constant =
476  makeExpr<Analyzer::Constant>(delete_column->get_type_info(), false, true_datum);
477 
478  auto input_col_descs = ra_exe_unit_in.input_col_descs;
479 
480  std::shared_ptr<Analyzer::Expr> filter;
481  std::vector<std::shared_ptr<Analyzer::Expr>> filter_exprs;
482  filter_exprs.insert(filter_exprs.end(),
483  ra_exe_unit_in.simple_quals.begin(),
484  ra_exe_unit_in.simple_quals.end());
485  filter_exprs.insert(
486  filter_exprs.end(), ra_exe_unit_in.quals.begin(), ra_exe_unit_in.quals.end());
487 
488  if (filter_exprs.size() > 0) {
489  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
490  case_expr_list;
491  if (filter_exprs.size() == 1) {
492  filter = filter_exprs.front();
493  } else {
494  filter = std::accumulate(
495  std::next(filter_exprs.begin()),
496  filter_exprs.end(),
497  filter_exprs.front(),
498  [](const std::shared_ptr<Analyzer::Expr> a,
499  const std::shared_ptr<Analyzer::Expr> b) {
500  CHECK_EQ(a->get_type_info().get_type(), b->get_type_info().get_type());
501  return makeExpr<Analyzer::BinOper>(a->get_type_info().get_type(),
502  SQLOps::kAND,
504  a->deep_copy(),
505  b->deep_copy());
506  });
507  }
508  std::shared_ptr<Analyzer::Expr> column_to_update{nullptr};
509  auto when_expr = filter; // only one filter, will be a BinOper if multiple filters
510  case_expr_list.emplace_back(std::make_pair(when_expr, deleted_constant));
511  auto case_expr = Parser::CaseExpr::normalize(case_expr_list, delete_column);
512 
513  // the delete column should not be projected, but check anyway
514  auto delete_col_desc_it = std::find_if(
515  input_col_descs.begin(),
516  input_col_descs.end(),
517  [&delete_column](const std::shared_ptr<const InputColDescriptor>& in) {
518  return in->getColId() == delete_column->get_column_id();
519  });
520  CHECK(delete_col_desc_it == input_col_descs.end());
521  auto delete_col_desc =
522  std::make_shared<const InputColDescriptor>(delete_column->get_column_id(),
523  delete_column->get_table_id(),
524  delete_column->get_rte_idx());
525  input_col_descs.push_back(delete_col_desc);
526  target_exprs_owned_.emplace_back(case_expr);
527  } else {
528  // no filters, simply project the deleted=true column value for all rows
529  auto delete_col_desc =
530  std::make_shared<const InputColDescriptor>(delete_column->get_column_id(),
531  delete_column->get_table_id(),
532  delete_column->get_rte_idx());
533  input_col_descs.push_back(delete_col_desc);
534  target_exprs_owned_.emplace_back(deleted_constant);
535  }
536 
537  std::vector<Analyzer::Expr*> target_exprs;
538  CHECK_EQ(target_exprs_owned_.size(), size_t(1));
539  target_exprs.emplace_back(target_exprs_owned_.front().get());
540 
541  RelAlgExecutionUnit rewritten_exe_unit{ra_exe_unit_in.input_descs,
542  input_col_descs,
543  {},
544  {},
545  ra_exe_unit_in.join_quals,
546  ra_exe_unit_in.groupby_exprs,
547  target_exprs,
548  ra_exe_unit_in.target_exprs_original_type_infos,
549  ra_exe_unit_in.estimator,
550  ra_exe_unit_in.sort_info,
551  ra_exe_unit_in.scan_limit,
552  ra_exe_unit_in.query_hint,
553  ra_exe_unit_in.query_plan_dag_hash,
554  ra_exe_unit_in.hash_table_build_plan_dag,
555  ra_exe_unit_in.table_id_to_node_map,
556  ra_exe_unit_in.use_bump_allocator,
557  ra_exe_unit_in.union_all,
558  ra_exe_unit_in.query_state};
559  return rewritten_exe_unit;
560 }
561 
562 std::pair<bool, std::set<size_t>> QueryRewriter::is_all_groupby_exprs_are_col_var(
563  const std::list<std::shared_ptr<Analyzer::Expr>>& groupby_exprs) const {
564  std::set<size_t> gby_col_exprs_hash;
565  for (auto gby_expr : groupby_exprs) {
566  if (auto gby_col_var = std::dynamic_pointer_cast<Analyzer::ColumnVar>(gby_expr)) {
567  gby_col_exprs_hash.insert(boost::hash_value(gby_col_var->toString()));
568  } else {
569  return {false, {}};
570  }
571  }
572  return {true, gby_col_exprs_hash};
573 }
574 
576  const RelAlgExecutionUnit& ra_exe_unit_in) const {
577  auto check_precond = is_all_groupby_exprs_are_col_var(ra_exe_unit_in.groupby_exprs);
578  auto is_expr_on_gby_col = [&check_precond](const Analyzer::AggExpr* agg_expr) {
579  CHECK(agg_expr);
580  if (agg_expr->get_arg()) {
581  // some expr does not have its own arg, i.e., count(*)
582  auto agg_expr_hash = boost::hash_value(agg_expr->get_arg()->toString());
583  // a valid expr should have hashed value > 0
584  CHECK_GT(agg_expr_hash, 0u);
585  if (check_precond.second.count(agg_expr_hash)) {
586  return true;
587  }
588  }
589  return false;
590  };
591  if (!check_precond.first) {
592  // return the input ra_exe_unit if we have gby expr which is not col_var
593  // i.e., group by x+1, y instead of group by x, y
594  // todo (yoonmin) : can we relax this with a simple analysis of groupby / agg exprs?
595  return ra_exe_unit_in;
596  }
597 
598  std::vector<Analyzer::Expr*> new_target_exprs;
599  for (auto expr : ra_exe_unit_in.target_exprs) {
600  bool rewritten = false;
601  if (auto agg_expr = dynamic_cast<Analyzer::AggExpr*>(expr)) {
602  if (is_expr_on_gby_col(agg_expr)) {
603  auto target_expr = agg_expr->get_arg();
604  // we have some issues when this rewriting is applied to float_type groupby column
605  // in subquery, i.e., SELECT MIN(v1) FROM (SELECT v1, AGG(v1) FROM T GROUP BY v1);
606  if (target_expr && target_expr->get_type_info().get_type() != SQLTypes::kFLOAT) {
607  switch (agg_expr->get_aggtype()) {
608  case SQLAgg::kCOUNT:
609  case SQLAgg::kCOUNT_IF:
611  if (agg_expr->get_aggtype() == SQLAgg::kCOUNT &&
612  !agg_expr->get_is_distinct()) {
613  break;
614  }
615  auto case_expr =
616  generateCaseExprForCountDistinctOnGroupByCol(agg_expr->get_own_arg());
617  new_target_exprs.push_back(case_expr.get());
618  target_exprs_owned_.emplace_back(case_expr);
619  rewritten = true;
620  break;
621  }
623  case SQLAgg::kAVG:
624  case SQLAgg::kSAMPLE:
625  case SQLAgg::kMAX:
626  case SQLAgg::kMIN: {
627  // we just replace the agg_expr into a plain expr
628  // i.e, avg(x1) --> x1
629  auto agg_expr_ti = agg_expr->get_type_info();
630  auto target_expr = agg_expr->get_own_arg();
631  if (agg_expr_ti != target_expr->get_type_info()) {
632  target_expr = target_expr->add_cast(agg_expr_ti);
633  }
634  new_target_exprs.push_back(target_expr.get());
635  target_exprs_owned_.emplace_back(target_expr);
636  rewritten = true;
637  break;
638  }
639  default:
640  break;
641  }
642  }
643  }
644  }
645  if (!rewritten) {
646  new_target_exprs.push_back(expr);
647  }
648  }
649 
650  RelAlgExecutionUnit rewritten_exe_unit{ra_exe_unit_in.input_descs,
651  ra_exe_unit_in.input_col_descs,
652  ra_exe_unit_in.simple_quals,
653  ra_exe_unit_in.quals,
654  ra_exe_unit_in.join_quals,
655  ra_exe_unit_in.groupby_exprs,
656  new_target_exprs,
657  ra_exe_unit_in.target_exprs_original_type_infos,
658  ra_exe_unit_in.estimator,
659  ra_exe_unit_in.sort_info,
660  ra_exe_unit_in.scan_limit,
661  ra_exe_unit_in.query_hint,
662  ra_exe_unit_in.query_plan_dag_hash,
663  ra_exe_unit_in.hash_table_build_plan_dag,
664  ra_exe_unit_in.table_id_to_node_map,
665  ra_exe_unit_in.use_bump_allocator,
666  ra_exe_unit_in.union_all,
667  ra_exe_unit_in.query_state};
668  return rewritten_exe_unit;
669 }
std::vector< Analyzer::Expr * > target_exprs
static std::shared_ptr< Analyzer::Expr > normalize(const std::list< std::pair< std::shared_ptr< Analyzer::Expr >, std::shared_ptr< Analyzer::Expr >>> &, const std::shared_ptr< Analyzer::Expr >, const Executor *executor=nullptr)
#define CHECK_EQ(x, y)
Definition: Logger.h:230
HOST DEVICE int get_size() const
Definition: sqltypes.h:390
size_t g_constrained_by_in_threshold
Definition: Execute.cpp:108
std::string cat(Ts &&...args)
boost::optional< OverlapsJoinConjunction > rewrite_overlaps_conjunction(const std::shared_ptr< Analyzer::Expr > expr, const std::vector< InputDescriptor > &input_table_info, const OverlapsJoinRewriteType rewrite_type, const Executor *executor)
QueryPlanHash query_plan_dag_hash
static bool is_overlaps_supported_func(std::string_view target_func_name)
const std::optional< bool > union_all
Definition: sqldefs.h:34
int8_t boolval
Definition: Datum.h:45
std::vector< InputDescriptor > input_descs
#define UNREACHABLE()
Definition: Logger.h:266
Definition: sqldefs.h:48
Definition: sqldefs.h:29
Constants for Builtin SQL Types supported by HEAVY.AI.
std::vector< JoinCondition > JoinQualsPerNestingLevel
const std::list< std::shared_ptr< Analyzer::Expr > > groupby_exprs
Analyzer::ExpressionPtr rewrite_expr(const Analyzer::Expr *expr)
#define CHECK_GT(x, y)
Definition: Logger.h:234
std::string to_string(char const *&&v)
bool g_enable_overlaps_hashjoin
Definition: Execute.cpp:102
constexpr double a
Definition: Utm.h:32
Definition: sqldefs.h:75
CONSTEXPR DEVICE bool is_null(const T &value)
Classes representing a parse tree.
RelAlgExecutionUnit rewrite(const RelAlgExecutionUnit &ra_exe_unit_in) const
const JoinQualsPerNestingLevel join_quals
std::pair< bool, std::set< size_t > > is_all_groupby_exprs_are_col_var(const std::list< std::shared_ptr< Analyzer::Expr >> &groupby_exprs) const
Executor * executor_
Definition: QueryRewrite.h:70
int64_t bigintval
Definition: Datum.h:49
TableIdToNodeMap table_id_to_node_map
RelAlgExecutionUnit rewriteOverlapsJoin(const RelAlgExecutionUnit &ra_exe_unit_in) const
bool g_enable_distance_rangejoin
Definition: Execute.cpp:103
Definition: sqldefs.h:36
DEVICE auto accumulate(ARGS &&...args)
Definition: gpu_enabled.h:42
ExpressionRange getExpressionRange(const Analyzer::BinOper *expr, const std::vector< InputTableInfo > &query_infos, const Executor *, boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>> simple_quals)
const std::shared_ptr< Analyzer::Estimator > estimator
std::string * stringval
Definition: Datum.h:54
Definition: sqldefs.h:71
bool check_string_id_overflow(const int32_t string_id, const SQLTypeInfo &ti)
RelAlgExecutionUnit rewriteColumnarUpdate(const RelAlgExecutionUnit &ra_exe_unit_in, std::shared_ptr< Analyzer::Expr > column_to_update) const
std::unordered_map< size_t, SQLTypeInfo > target_exprs_original_type_infos
Definition: sqldefs.h:78
const std::list< std::shared_ptr< Analyzer::Expr > > & get_value_list() const
Definition: Analyzer.h:641
virtual std::shared_ptr< Analyzer::Expr > deep_copy() const =0
const std::vector< InputTableInfo > & query_infos_
Definition: QueryRewrite.h:69
std::list< std::shared_ptr< Analyzer::Expr > > quals
int64_t getIntMax() const
RegisteredQueryHint query_hint
#define CHECK(condition)
Definition: Logger.h:222
OverlapsJoinRewriteType
int64_t inline_fixed_encoding_null_val(const SQL_TYPE_INFO &ti)
Definition: sqldefs.h:32
std::shared_ptr< Analyzer::CaseExpr > generateCaseExprForCountDistinctOnGroupByCol(std::shared_ptr< Analyzer::Expr > expr) const
std::shared_ptr< const query_state::QueryState > query_state
std::list< std::shared_ptr< const InputColDescriptor > > input_col_descs
std::string getName() const
Definition: Analyzer.h:2401
RelAlgExecutionUnit rewriteColumnarDelete(const RelAlgExecutionUnit &ra_exe_unit_in, std::shared_ptr< Analyzer::ColumnVar > delete_column) const
Definition: sqldefs.h:76
Definition: Datum.h:44
static std::shared_ptr< Analyzer::CaseExpr > generateCaseForDomainValues(const Analyzer::InValues *)
Definition: sqldefs.h:74
RelAlgExecutionUnit rewriteConstrainedByIn(const RelAlgExecutionUnit &ra_exe_unit_in) const
Definition: sqldefs.h:38
RelAlgExecutionUnit rewriteAggregateOnGroupByColumn(const RelAlgExecutionUnit &ra_exe_unit_in) const
const Expr * get_arg() const
Definition: Analyzer.h:639
std::list< std::shared_ptr< Analyzer::Expr > > simple_quals
RelAlgExecutionUnit rewriteConstrainedByInImpl(const RelAlgExecutionUnit &ra_exe_unit_in, const std::shared_ptr< Analyzer::CaseExpr >, const Analyzer::InValues *) const
std::vector< std::shared_ptr< Analyzer::Expr > > target_exprs_owned_
Definition: QueryRewrite.h:71
HashTableBuildDagMap hash_table_build_plan_dag