OmniSciDB  ba1bac9284
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
QueryRewriter Class Reference

#include <QueryRewrite.h>

+ Collaboration diagram for QueryRewriter:

Public Member Functions

 QueryRewriter (const std::vector< InputTableInfo > &query_infos, Executor *executor)
 
RelAlgExecutionUnit rewrite (const RelAlgExecutionUnit &ra_exe_unit_in) const
 
RelAlgExecutionUnit rewriteColumnarUpdate (const RelAlgExecutionUnit &ra_exe_unit_in, std::shared_ptr< Analyzer::Expr > column_to_update) const
 
RelAlgExecutionUnit rewriteColumnarDelete (const RelAlgExecutionUnit &ra_exe_unit_in, std::shared_ptr< Analyzer::ColumnVar > delete_column) const
 

Private Member Functions

RelAlgExecutionUnit rewriteOverlapsJoin (const RelAlgExecutionUnit &ra_exe_unit_in) const
 
RelAlgExecutionUnit rewriteConstrainedByIn (const RelAlgExecutionUnit &ra_exe_unit_in) const
 
RelAlgExecutionUnit rewriteConstrainedByInImpl (const RelAlgExecutionUnit &ra_exe_unit_in, const std::shared_ptr< Analyzer::CaseExpr >, const Analyzer::InValues *) const
 

Static Private Member Functions

static std::shared_ptr
< Analyzer::CaseExpr
generateCaseForDomainValues (const Analyzer::InValues *)
 

Private Attributes

const std::vector
< InputTableInfo > & 
query_infos_
 
Executorexecutor_
 
std::vector< std::shared_ptr
< Analyzer::Expr > > 
target_exprs_owned_
 

Detailed Description

Definition at line 21 of file QueryRewrite.h.

Constructor & Destructor Documentation

QueryRewriter::QueryRewriter ( const std::vector< InputTableInfo > &  query_infos,
Executor executor 
)
inline

Definition at line 23 of file QueryRewrite.h.

24  : query_infos_(query_infos), executor_(executor) {}
Executor * executor_
Definition: QueryRewrite.h:51
const std::vector< InputTableInfo > & query_infos_
Definition: QueryRewrite.h:50

Member Function Documentation

std::shared_ptr< Analyzer::CaseExpr > QueryRewriter::generateCaseForDomainValues ( const Analyzer::InValues in_vals)
staticprivate

Definition at line 175 of file QueryRewrite.cpp.

References Analyzer::Expr::deep_copy(), Analyzer::InValues::get_arg(), Analyzer::InValues::get_value_list(), kBOOLEAN, kENCODING_DICT, kEQ, and kONE.

Referenced by rewriteConstrainedByIn().

176  {
177  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
178  case_expr_list;
179  auto in_val_arg = in_vals->get_arg()->deep_copy();
180  for (const auto& in_val : in_vals->get_value_list()) {
181  auto case_cond = makeExpr<Analyzer::BinOper>(
182  SQLTypeInfo(kBOOLEAN, true), false, kEQ, kONE, in_val_arg, in_val);
183  auto in_val_copy = in_val->deep_copy();
184  auto ti = in_val_copy->get_type_info();
185  if (ti.is_string() && ti.get_compression() == kENCODING_DICT) {
186  ti.set_comp_param(0);
187  }
188  in_val_copy->set_type_info(ti);
189  case_expr_list.emplace_back(case_cond, in_val_copy);
190  }
191  // TODO(alex): refine the expression range for case with empty else expression;
192  // for now, add a dummy else which should never be taken
193  auto else_expr = case_expr_list.front().second;
194  return makeExpr<Analyzer::CaseExpr>(
195  case_expr_list.front().second->get_type_info(), false, case_expr_list, else_expr);
196 }
Definition: sqldefs.h:30
Definition: sqldefs.h:69
const std::list< std::shared_ptr< Analyzer::Expr > > & get_value_list() const
Definition: Analyzer.h:586
virtual std::shared_ptr< Analyzer::Expr > deep_copy() const =0
const Expr * get_arg() const
Definition: Analyzer.h:584

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelAlgExecutionUnit QueryRewriter::rewrite ( const RelAlgExecutionUnit ra_exe_unit_in) const

Definition at line 28 of file QueryRewrite.cpp.

References rewriteConstrainedByIn(), and rewriteOverlapsJoin().

Referenced by rewriteConstrainedByInImpl().

29  {
30  auto rewritten_exe_unit = rewriteConstrainedByIn(ra_exe_unit_in);
31  return rewriteOverlapsJoin(rewritten_exe_unit);
32 }
RelAlgExecutionUnit rewriteOverlapsJoin(const RelAlgExecutionUnit &ra_exe_unit_in) const
RelAlgExecutionUnit rewriteConstrainedByIn(const RelAlgExecutionUnit &ra_exe_unit_in) const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelAlgExecutionUnit QueryRewriter::rewriteColumnarDelete ( const RelAlgExecutionUnit ra_exe_unit_in,
std::shared_ptr< Analyzer::ColumnVar delete_column 
) const

Definition at line 386 of file QueryRewrite.cpp.

References gpu_enabled::accumulate(), Datum::boolval, CHECK, CHECK_EQ, RelAlgExecutionUnit::estimator, RelAlgExecutionUnit::groupby_exprs, RelAlgExecutionUnit::input_col_descs, RelAlgExecutionUnit::input_descs, RelAlgExecutionUnit::join_quals, kAND, kONE, Parser::CaseExpr::normalize(), RelAlgExecutionUnit::quals, RelAlgExecutionUnit::query_hint, RelAlgExecutionUnit::query_state, RelAlgExecutionUnit::scan_limit, RelAlgExecutionUnit::simple_quals, RelAlgExecutionUnit::sort_info, RelAlgExecutionUnit::target_exprs, target_exprs_owned_, RelAlgExecutionUnit::union_all, and RelAlgExecutionUnit::use_bump_allocator.

388  {
389  CHECK_EQ(ra_exe_unit_in.target_exprs.size(), size_t(1));
390  CHECK(ra_exe_unit_in.groupby_exprs.size() == 1 &&
391  !ra_exe_unit_in.groupby_exprs.front());
392 
393  // TODO(adb): is this possible?
394  if (ra_exe_unit_in.join_quals.size() > 0) {
395  throw std::runtime_error("Delete via join not yet supported for temporary tables.");
396  }
397 
398  Datum true_datum;
399  true_datum.boolval = true;
400  const auto deleted_constant =
401  makeExpr<Analyzer::Constant>(delete_column->get_type_info(), false, true_datum);
402 
403  auto input_col_descs = ra_exe_unit_in.input_col_descs;
404 
405  std::shared_ptr<Analyzer::Expr> filter;
406  std::vector<std::shared_ptr<Analyzer::Expr>> filter_exprs;
407  filter_exprs.insert(filter_exprs.end(),
408  ra_exe_unit_in.simple_quals.begin(),
409  ra_exe_unit_in.simple_quals.end());
410  filter_exprs.insert(
411  filter_exprs.end(), ra_exe_unit_in.quals.begin(), ra_exe_unit_in.quals.end());
412 
413  if (filter_exprs.size() > 0) {
414  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
415  case_expr_list;
416  if (filter_exprs.size() == 1) {
417  filter = filter_exprs.front();
418  } else {
419  filter = std::accumulate(
420  std::next(filter_exprs.begin()),
421  filter_exprs.end(),
422  filter_exprs.front(),
423  [](const std::shared_ptr<Analyzer::Expr> a,
424  const std::shared_ptr<Analyzer::Expr> b) {
425  CHECK_EQ(a->get_type_info().get_type(), b->get_type_info().get_type());
426  return makeExpr<Analyzer::BinOper>(a->get_type_info().get_type(),
427  SQLOps::kAND,
429  a->deep_copy(),
430  b->deep_copy());
431  });
432  }
433  std::shared_ptr<Analyzer::Expr> column_to_update{nullptr};
434  auto when_expr = filter; // only one filter, will be a BinOper if multiple filters
435  case_expr_list.emplace_back(std::make_pair(when_expr, deleted_constant));
436  auto case_expr = Parser::CaseExpr::normalize(case_expr_list, delete_column);
437 
438  // the delete column should not be projected, but check anyway
439  auto delete_col_desc_it = std::find_if(
440  input_col_descs.begin(),
441  input_col_descs.end(),
442  [&delete_column](const std::shared_ptr<const InputColDescriptor>& in) {
443  return in->getColId() == delete_column->get_column_id();
444  });
445  CHECK(delete_col_desc_it == input_col_descs.end());
446  auto delete_col_desc =
447  std::make_shared<const InputColDescriptor>(delete_column->get_column_id(),
448  delete_column->get_table_id(),
449  delete_column->get_rte_idx());
450  input_col_descs.push_back(delete_col_desc);
451  target_exprs_owned_.emplace_back(case_expr);
452  } else {
453  // no filters, simply project the deleted=true column value for all rows
454  auto delete_col_desc =
455  std::make_shared<const InputColDescriptor>(delete_column->get_column_id(),
456  delete_column->get_table_id(),
457  delete_column->get_rte_idx());
458  input_col_descs.push_back(delete_col_desc);
459  target_exprs_owned_.emplace_back(deleted_constant);
460  }
461 
462  std::vector<Analyzer::Expr*> target_exprs;
463  CHECK_EQ(target_exprs_owned_.size(), size_t(1));
464  target_exprs.emplace_back(target_exprs_owned_.front().get());
465 
466  RelAlgExecutionUnit rewritten_exe_unit{ra_exe_unit_in.input_descs,
467  input_col_descs,
468  {},
469  {},
470  ra_exe_unit_in.join_quals,
471  ra_exe_unit_in.groupby_exprs,
472  target_exprs,
473  ra_exe_unit_in.estimator,
474  ra_exe_unit_in.sort_info,
475  ra_exe_unit_in.scan_limit,
476  ra_exe_unit_in.query_hint,
477  ra_exe_unit_in.use_bump_allocator,
478  ra_exe_unit_in.union_all,
479  ra_exe_unit_in.query_state};
480  return rewritten_exe_unit;
481 }
std::vector< Analyzer::Expr * > target_exprs
#define CHECK_EQ(x, y)
Definition: Logger.h:214
const std::optional< bool > union_all
int8_t boolval
Definition: sqltypes.h:205
std::vector< InputDescriptor > input_descs
const std::list< std::shared_ptr< Analyzer::Expr > > groupby_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 >)
Definition: ParserNode.cpp:934
const SortInfo sort_info
const JoinQualsPerNestingLevel join_quals
Definition: sqldefs.h:37
DEVICE auto accumulate(ARGS &&...args)
Definition: gpu_enabled.h:42
const std::shared_ptr< Analyzer::Estimator > estimator
Definition: sqldefs.h:69
std::list< std::shared_ptr< Analyzer::Expr > > quals
RegisteredQueryHint query_hint
#define CHECK(condition)
Definition: Logger.h:206
std::shared_ptr< const query_state::QueryState > query_state
std::list< std::shared_ptr< const InputColDescriptor > > input_col_descs
std::list< std::shared_ptr< Analyzer::Expr > > simple_quals
std::vector< std::shared_ptr< Analyzer::Expr > > target_exprs_owned_
Definition: QueryRewrite.h:52

+ Here is the call graph for this function:

RelAlgExecutionUnit QueryRewriter::rewriteColumnarUpdate ( const RelAlgExecutionUnit ra_exe_unit_in,
std::shared_ptr< Analyzer::Expr column_to_update 
) const

Definition at line 222 of file QueryRewrite.cpp.

References gpu_enabled::accumulate(), cat(), CHECK, CHECK_EQ, anonymous_namespace{QueryRewrite.cpp}::check_string_id_overflow(), RelAlgExecutionUnit::estimator, executor_, RelAlgExecutionUnit::groupby_exprs, inline_fixed_encoding_null_val(), RelAlgExecutionUnit::input_col_descs, RelAlgExecutionUnit::input_descs, RelAlgExecutionUnit::join_quals, kAND, kCAST, kONE, Parser::CaseExpr::normalize(), RelAlgExecutionUnit::quals, RelAlgExecutionUnit::query_hint, RelAlgExecutionUnit::query_state, RelAlgExecutionUnit::scan_limit, RelAlgExecutionUnit::simple_quals, RelAlgExecutionUnit::sort_info, Datum::stringval, RelAlgExecutionUnit::target_exprs, target_exprs_owned_, to_string(), RelAlgExecutionUnit::union_all, and RelAlgExecutionUnit::use_bump_allocator.

224  {
225  CHECK_EQ(ra_exe_unit_in.target_exprs.size(), size_t(2));
226  CHECK(ra_exe_unit_in.groupby_exprs.size() == 1 &&
227  !ra_exe_unit_in.groupby_exprs.front());
228 
229  if (ra_exe_unit_in.join_quals.size() > 0) {
230  throw std::runtime_error("Update via join not yet supported for temporary tables.");
231  }
232 
233  auto new_column_value = ra_exe_unit_in.target_exprs.front()->deep_copy();
234  const auto& new_column_ti = new_column_value->get_type_info();
235  if (column_to_update->get_type_info().is_dict_encoded_string()) {
236  CHECK(new_column_ti.is_dict_encoded_string());
237  if (new_column_ti.get_comp_param() > 0 &&
238  new_column_ti.get_comp_param() !=
239  column_to_update->get_type_info().get_comp_param()) {
240  throw std::runtime_error(
241  "Updating a dictionary encoded string using another dictionary encoded string "
242  "column is not yet supported, unless both columns share dictionaries.");
243  }
244  if (auto uoper = dynamic_cast<Analyzer::UOper*>(new_column_value.get())) {
245  if (uoper->get_optype() == kCAST &&
246  dynamic_cast<const Analyzer::Constant*>(uoper->get_operand())) {
247  const auto original_constant_expr =
248  dynamic_cast<const Analyzer::Constant*>(uoper->get_operand());
249  CHECK(original_constant_expr);
250  CHECK(original_constant_expr->get_type_info().is_string());
251  // extract the string, insert it into the dict for the table we are updating,
252  // and place the dictionary ID in the oper
253  auto cat = executor_->getCatalog();
254  CHECK(cat);
255 
256  CHECK(column_to_update->get_type_info().is_dict_encoded_string());
257  const auto dict_id = column_to_update->get_type_info().get_comp_param();
258  std::map<int, StringDictionary*> string_dicts;
259  const auto dd = cat->getMetadataForDict(dict_id, /*load_dict=*/true);
260  CHECK(dd);
261  auto string_dict = dd->stringDict;
262  CHECK(string_dict);
263 
264  auto string_id =
265  string_dict->getOrAdd(*original_constant_expr->get_constval().stringval);
266  if (check_string_id_overflow(string_id, column_to_update->get_type_info())) {
267  throw std::runtime_error(
268  "Ran out of space in dictionary, cannot update column with dictionary "
269  "encoded string value. Dictionary ID: " +
270  std::to_string(dict_id));
271  }
272  if (string_id == inline_int_null_value<int32_t>()) {
273  string_id = inline_fixed_encoding_null_val(column_to_update->get_type_info());
274  }
275 
276  // Codegen expects a string value. The string will be
277  // resolved to its ID during Constant codegen. Copy the string from the
278  // original expr
279  Datum datum;
280  datum.stringval =
281  new std::string(*original_constant_expr->get_constval().stringval);
282  Datum new_string_datum{datum};
283 
284  new_column_value =
285  makeExpr<Analyzer::Constant>(column_to_update->get_type_info(),
286  original_constant_expr->get_is_null(),
287  new_string_datum);
288 
289  // Roll the string dict generation forward, as we have added a string
290  auto row_set_mem_owner = executor_->getRowSetMemoryOwner();
291  CHECK(row_set_mem_owner);
292  auto& str_dict_generations = row_set_mem_owner->getStringDictionaryGenerations();
293  if (str_dict_generations.getGeneration(dict_id) > -1) {
294  str_dict_generations.updateGeneration(dict_id,
295  string_dict->storageEntryCount());
296  } else {
297  // Simple update with no filters does not use a CASE, and therefore does not add
298  // a valid generation
299  str_dict_generations.setGeneration(dict_id, string_dict->storageEntryCount());
300  }
301  }
302  }
303  }
304 
305  auto input_col_descs = ra_exe_unit_in.input_col_descs;
306 
307  std::shared_ptr<Analyzer::Expr> filter;
308  std::vector<std::shared_ptr<Analyzer::Expr>> filter_exprs;
309  filter_exprs.insert(filter_exprs.end(),
310  ra_exe_unit_in.simple_quals.begin(),
311  ra_exe_unit_in.simple_quals.end());
312  filter_exprs.insert(
313  filter_exprs.end(), ra_exe_unit_in.quals.begin(), ra_exe_unit_in.quals.end());
314 
315  if (filter_exprs.size() > 0) {
316  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
317  case_expr_list;
318  if (filter_exprs.size() == 1) {
319  filter = filter_exprs.front();
320  } else {
321  filter = std::accumulate(
322  std::next(filter_exprs.begin()),
323  filter_exprs.end(),
324  filter_exprs.front(),
325  [](const std::shared_ptr<Analyzer::Expr> a,
326  const std::shared_ptr<Analyzer::Expr> b) {
327  CHECK_EQ(a->get_type_info().get_type(), b->get_type_info().get_type());
328  return makeExpr<Analyzer::BinOper>(a->get_type_info().get_type(),
329  SQLOps::kAND,
331  a->deep_copy(),
332  b->deep_copy());
333  });
334  }
335  auto when_expr = filter; // only one filter, will be a BinOper if multiple filters
336  case_expr_list.emplace_back(std::make_pair(when_expr, new_column_value));
337  auto case_expr = Parser::CaseExpr::normalize(case_expr_list, column_to_update);
338 
339  auto col_to_update_var =
340  std::dynamic_pointer_cast<Analyzer::ColumnVar>(column_to_update);
341  CHECK(col_to_update_var);
342  auto col_to_update_desc =
343  std::make_shared<const InputColDescriptor>(col_to_update_var->get_column_id(),
344  col_to_update_var->get_table_id(),
345  col_to_update_var->get_rte_idx());
346  auto existing_col_desc_it = std::find_if(
347  input_col_descs.begin(),
348  input_col_descs.end(),
349  [&col_to_update_desc](const std::shared_ptr<const InputColDescriptor>& in) {
350  return *in == *col_to_update_desc;
351  });
352  if (existing_col_desc_it == input_col_descs.end()) {
353  input_col_descs.push_back(col_to_update_desc);
354  }
355  target_exprs_owned_.emplace_back(case_expr);
356  } else {
357  // no filters, simply project the update value
358  target_exprs_owned_.emplace_back(new_column_value);
359  }
360 
361  std::vector<Analyzer::Expr*> target_exprs;
362  CHECK_EQ(target_exprs_owned_.size(), size_t(1));
363  target_exprs.emplace_back(target_exprs_owned_.front().get());
364 
365  RelAlgExecutionUnit rewritten_exe_unit{ra_exe_unit_in.input_descs,
366  input_col_descs,
367  {},
368  {},
369  ra_exe_unit_in.join_quals,
370  ra_exe_unit_in.groupby_exprs,
371  target_exprs,
372  ra_exe_unit_in.estimator,
373  ra_exe_unit_in.sort_info,
374  ra_exe_unit_in.scan_limit,
375  ra_exe_unit_in.query_hint,
376  ra_exe_unit_in.use_bump_allocator,
377  ra_exe_unit_in.union_all,
378  ra_exe_unit_in.query_state};
379  return rewritten_exe_unit;
380 }
std::vector< Analyzer::Expr * > target_exprs
#define CHECK_EQ(x, y)
Definition: Logger.h:214
std::string cat(Ts &&...args)
const std::optional< bool > union_all
std::vector< InputDescriptor > input_descs
Definition: sqldefs.h:49
const std::list< std::shared_ptr< Analyzer::Expr > > groupby_exprs
std::string to_string(char const *&&v)
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 >)
Definition: ParserNode.cpp:934
const SortInfo sort_info
const JoinQualsPerNestingLevel join_quals
Executor * executor_
Definition: QueryRewrite.h:51
Definition: sqldefs.h:37
DEVICE auto accumulate(ARGS &&...args)
Definition: gpu_enabled.h:42
const std::shared_ptr< Analyzer::Estimator > estimator
std::string * stringval
Definition: sqltypes.h:214
Definition: sqldefs.h:69
bool check_string_id_overflow(const int32_t string_id, const SQLTypeInfo &ti)
std::list< std::shared_ptr< Analyzer::Expr > > quals
RegisteredQueryHint query_hint
#define CHECK(condition)
Definition: Logger.h:206
int64_t inline_fixed_encoding_null_val(const SQL_TYPE_INFO &ti)
std::shared_ptr< const query_state::QueryState > query_state
std::list< std::shared_ptr< const InputColDescriptor > > input_col_descs
std::list< std::shared_ptr< Analyzer::Expr > > simple_quals
std::vector< std::shared_ptr< Analyzer::Expr > > target_exprs_owned_
Definition: QueryRewrite.h:52

+ Here is the call graph for this function:

RelAlgExecutionUnit QueryRewriter::rewriteConstrainedByIn ( const RelAlgExecutionUnit ra_exe_unit_in) const
private

Definition at line 84 of file QueryRewrite.cpp.

References generateCaseForDomainValues(), RelAlgExecutionUnit::groupby_exprs, RelAlgExecutionUnit::quals, rewrite_expr(), rewriteConstrainedByInImpl(), and RelAlgExecutionUnit::simple_quals.

Referenced by rewrite().

85  {
86  if (ra_exe_unit_in.groupby_exprs.empty()) {
87  return ra_exe_unit_in;
88  }
89  if (ra_exe_unit_in.groupby_exprs.size() == 1 && !ra_exe_unit_in.groupby_exprs.front()) {
90  return ra_exe_unit_in;
91  }
92  if (!ra_exe_unit_in.simple_quals.empty()) {
93  return ra_exe_unit_in;
94  }
95  if (ra_exe_unit_in.quals.size() != 1) {
96  return ra_exe_unit_in;
97  }
98  auto in_vals =
99  std::dynamic_pointer_cast<Analyzer::InValues>(ra_exe_unit_in.quals.front());
100  if (!in_vals) {
101  in_vals = std::dynamic_pointer_cast<Analyzer::InValues>(
102  rewrite_expr(ra_exe_unit_in.quals.front().get()));
103  }
104  if (!in_vals || in_vals->get_value_list().empty()) {
105  return ra_exe_unit_in;
106  }
107  for (const auto& in_val : in_vals->get_value_list()) {
108  if (!std::dynamic_pointer_cast<Analyzer::Constant>(in_val)) {
109  break;
110  }
111  }
112  if (dynamic_cast<const Analyzer::CaseExpr*>(in_vals->get_arg())) {
113  return ra_exe_unit_in;
114  }
115  auto case_expr = generateCaseForDomainValues(in_vals.get());
116  return rewriteConstrainedByInImpl(ra_exe_unit_in, case_expr, in_vals.get());
117 }
const std::list< std::shared_ptr< Analyzer::Expr > > groupby_exprs
Analyzer::ExpressionPtr rewrite_expr(const Analyzer::Expr *expr)
std::list< std::shared_ptr< Analyzer::Expr > > quals
static std::shared_ptr< Analyzer::CaseExpr > generateCaseForDomainValues(const Analyzer::InValues *)
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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelAlgExecutionUnit QueryRewriter::rewriteConstrainedByInImpl ( const RelAlgExecutionUnit ra_exe_unit_in,
const std::shared_ptr< Analyzer::CaseExpr case_expr,
const Analyzer::InValues in_vals 
) const
private

Definition at line 119 of file QueryRewrite.cpp.

References CHECK, executor_, g_constrained_by_in_threshold, Analyzer::InValues::get_arg(), getExpressionRange(), ExpressionRange::getIntMax(), RelAlgExecutionUnit::groupby_exprs, i, RelAlgExecutionUnit::input_col_descs, RelAlgExecutionUnit::input_descs, Integer, RelAlgExecutionUnit::join_quals, Analyzer::Var::kGROUPBY, RelAlgExecutionUnit::quals, query_infos_, rewrite(), RelAlgExecutionUnit::scan_limit, RelAlgExecutionUnit::simple_quals, RelAlgExecutionUnit::sort_info, RelAlgExecutionUnit::target_exprs, and target_exprs_owned_.

Referenced by rewriteConstrainedByIn().

122  {
123  std::list<std::shared_ptr<Analyzer::Expr>> new_groupby_list;
124  std::vector<Analyzer::Expr*> new_target_exprs;
125  bool rewrite{false};
126  size_t groupby_idx{0};
127  auto it = ra_exe_unit_in.groupby_exprs.begin();
128  for (const auto& group_expr : ra_exe_unit_in.groupby_exprs) {
129  CHECK(group_expr);
130  ++groupby_idx;
131  if (*group_expr == *in_vals->get_arg()) {
132  const auto expr_range = getExpressionRange(it->get(), query_infos_, executor_);
133  if (expr_range.getType() != ExpressionRangeType::Integer) {
134  ++it;
135  continue;
136  }
137  const size_t range_sz = expr_range.getIntMax() - expr_range.getIntMin() + 1;
138  if (range_sz <= in_vals->get_value_list().size() * g_constrained_by_in_threshold) {
139  ++it;
140  continue;
141  }
142  new_groupby_list.push_back(case_expr);
143  for (size_t i = 0; i < ra_exe_unit_in.target_exprs.size(); ++i) {
144  const auto target = ra_exe_unit_in.target_exprs[i];
145  if (*target == *in_vals->get_arg()) {
146  auto var_case_expr = makeExpr<Analyzer::Var>(
147  case_expr->get_type_info(), Analyzer::Var::kGROUPBY, groupby_idx);
148  target_exprs_owned_.push_back(var_case_expr);
149  new_target_exprs.push_back(var_case_expr.get());
150  } else {
151  new_target_exprs.push_back(target);
152  }
153  }
154  rewrite = true;
155  } else {
156  new_groupby_list.push_back(group_expr);
157  }
158  ++it;
159  }
160  if (!rewrite) {
161  return ra_exe_unit_in;
162  }
163  return {ra_exe_unit_in.input_descs,
164  ra_exe_unit_in.input_col_descs,
165  ra_exe_unit_in.simple_quals,
166  ra_exe_unit_in.quals,
167  ra_exe_unit_in.join_quals,
168  new_groupby_list,
169  new_target_exprs,
170  nullptr,
171  ra_exe_unit_in.sort_info,
172  ra_exe_unit_in.scan_limit};
173 }
std::vector< Analyzer::Expr * > target_exprs
size_t g_constrained_by_in_threshold
Definition: Execute.cpp:101
std::vector< InputDescriptor > input_descs
const std::list< std::shared_ptr< Analyzer::Expr > > groupby_exprs
RelAlgExecutionUnit rewrite(const RelAlgExecutionUnit &ra_exe_unit_in) const
const SortInfo sort_info
const JoinQualsPerNestingLevel join_quals
Executor * executor_
Definition: QueryRewrite.h:51
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::vector< InputTableInfo > & query_infos_
Definition: QueryRewrite.h:50
std::list< std::shared_ptr< Analyzer::Expr > > quals
int64_t getIntMax() const
#define CHECK(condition)
Definition: Logger.h:206
std::list< std::shared_ptr< const InputColDescriptor > > input_col_descs
const Expr * get_arg() const
Definition: Analyzer.h:584
std::list< std::shared_ptr< Analyzer::Expr > > simple_quals
std::vector< std::shared_ptr< Analyzer::Expr > > target_exprs_owned_
Definition: QueryRewrite.h:52

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

RelAlgExecutionUnit QueryRewriter::rewriteOverlapsJoin ( const RelAlgExecutionUnit ra_exe_unit_in) const
private

Definition at line 34 of file QueryRewrite.cpp.

References RelAlgExecutionUnit::estimator, g_enable_overlaps_hashjoin, RelAlgExecutionUnit::groupby_exprs, RelAlgExecutionUnit::input_col_descs, RelAlgExecutionUnit::input_descs, RelAlgExecutionUnit::join_quals, RelAlgExecutionUnit::quals, RelAlgExecutionUnit::query_hint, rewrite_overlaps_conjunction(), RelAlgExecutionUnit::scan_limit, RelAlgExecutionUnit::simple_quals, RelAlgExecutionUnit::sort_info, RelAlgExecutionUnit::target_exprs, JoinCondition::type, and RelAlgExecutionUnit::use_bump_allocator.

Referenced by rewrite().

35  {
37  return ra_exe_unit_in;
38  }
39  if (ra_exe_unit_in.join_quals.empty()) {
40  return ra_exe_unit_in;
41  }
42 
43  std::list<std::shared_ptr<Analyzer::Expr>> quals;
44  quals.insert(quals.end(), ra_exe_unit_in.quals.begin(), ra_exe_unit_in.quals.end());
45 
46  JoinQualsPerNestingLevel join_condition_per_nesting_level;
47  for (const auto& join_condition_in : ra_exe_unit_in.join_quals) {
48  JoinCondition join_condition{{}, join_condition_in.type};
49 
50  for (const auto& join_qual_expr_in : join_condition_in.quals) {
51  auto new_overlaps_quals = rewrite_overlaps_conjunction(join_qual_expr_in);
52  if (new_overlaps_quals) {
53  const auto& overlaps_quals = *new_overlaps_quals;
54 
55  // Add overlaps qual
56  join_condition.quals.insert(join_condition.quals.end(),
57  overlaps_quals.join_quals.begin(),
58  overlaps_quals.join_quals.end());
59 
60  // Add original quals
61  join_condition.quals.insert(join_condition.quals.end(),
62  overlaps_quals.quals.begin(),
63  overlaps_quals.quals.end());
64  } else {
65  join_condition.quals.push_back(join_qual_expr_in);
66  }
67  }
68  join_condition_per_nesting_level.push_back(join_condition);
69  }
70  return {ra_exe_unit_in.input_descs,
71  ra_exe_unit_in.input_col_descs,
72  ra_exe_unit_in.simple_quals,
73  quals,
74  join_condition_per_nesting_level,
75  ra_exe_unit_in.groupby_exprs,
76  ra_exe_unit_in.target_exprs,
77  ra_exe_unit_in.estimator,
78  ra_exe_unit_in.sort_info,
79  ra_exe_unit_in.scan_limit,
80  ra_exe_unit_in.query_hint,
81  ra_exe_unit_in.use_bump_allocator};
82 }
std::vector< Analyzer::Expr * > target_exprs
std::vector< InputDescriptor > input_descs
std::vector< JoinCondition > JoinQualsPerNestingLevel
const std::list< std::shared_ptr< Analyzer::Expr > > groupby_exprs
bool g_enable_overlaps_hashjoin
Definition: Execute.cpp:96
const SortInfo sort_info
const JoinQualsPerNestingLevel join_quals
boost::optional< OverlapsJoinConjunction > rewrite_overlaps_conjunction(const std::shared_ptr< Analyzer::Expr > expr)
const std::shared_ptr< Analyzer::Estimator > estimator
std::list< std::shared_ptr< Analyzer::Expr > > quals
RegisteredQueryHint query_hint
std::list< std::shared_ptr< const InputColDescriptor > > input_col_descs
std::list< std::shared_ptr< Analyzer::Expr > > simple_quals

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

Executor* QueryRewriter::executor_
private

Definition at line 51 of file QueryRewrite.h.

Referenced by rewriteColumnarUpdate(), and rewriteConstrainedByInImpl().

const std::vector<InputTableInfo>& QueryRewriter::query_infos_
private

Definition at line 50 of file QueryRewrite.h.

Referenced by rewriteConstrainedByInImpl().

std::vector<std::shared_ptr<Analyzer::Expr> > QueryRewriter::target_exprs_owned_
mutableprivate

The documentation for this class was generated from the following files: