OmniSciDB  94e8789169
 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 384 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.

386  {
387  CHECK_EQ(ra_exe_unit_in.target_exprs.size(), size_t(1));
388  CHECK(ra_exe_unit_in.groupby_exprs.size() == 1 &&
389  !ra_exe_unit_in.groupby_exprs.front());
390 
391  // TODO(adb): is this possible?
392  if (ra_exe_unit_in.join_quals.size() > 0) {
393  throw std::runtime_error("Delete via join not yet supported for temporary tables.");
394  }
395 
396  const auto true_datum = Datum{.boolval = true};
397  const auto deleted_constant =
398  makeExpr<Analyzer::Constant>(delete_column->get_type_info(), false, true_datum);
399 
400  auto input_col_descs = ra_exe_unit_in.input_col_descs;
401 
402  std::shared_ptr<Analyzer::Expr> filter;
403  std::vector<std::shared_ptr<Analyzer::Expr>> filter_exprs;
404  filter_exprs.insert(filter_exprs.end(),
405  ra_exe_unit_in.simple_quals.begin(),
406  ra_exe_unit_in.simple_quals.end());
407  filter_exprs.insert(
408  filter_exprs.end(), ra_exe_unit_in.quals.begin(), ra_exe_unit_in.quals.end());
409 
410  if (filter_exprs.size() > 0) {
411  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
412  case_expr_list;
413  if (filter_exprs.size() == 1) {
414  filter = filter_exprs.front();
415  } else {
416  filter = std::accumulate(
417  std::next(filter_exprs.begin()),
418  filter_exprs.end(),
419  filter_exprs.front(),
420  [](const std::shared_ptr<Analyzer::Expr> a,
421  const std::shared_ptr<Analyzer::Expr> b) {
422  CHECK_EQ(a->get_type_info().get_type(), b->get_type_info().get_type());
423  return makeExpr<Analyzer::BinOper>(a->get_type_info().get_type(),
424  SQLOps::kAND,
426  a->deep_copy(),
427  b->deep_copy());
428  });
429  }
430  std::shared_ptr<Analyzer::Expr> column_to_update{nullptr};
431  auto when_expr = filter; // only one filter, will be a BinOper if multiple filters
432  case_expr_list.emplace_back(std::make_pair(when_expr, deleted_constant));
433  auto case_expr = Parser::CaseExpr::normalize(case_expr_list, delete_column);
434 
435  // the delete column should not be projected, but check anyway
436  auto delete_col_desc_it = std::find_if(
437  input_col_descs.begin(),
438  input_col_descs.end(),
439  [&delete_column](const std::shared_ptr<const InputColDescriptor>& in) {
440  return in->getColId() == delete_column->get_column_id();
441  });
442  CHECK(delete_col_desc_it == input_col_descs.end());
443  auto delete_col_desc =
444  std::make_shared<const InputColDescriptor>(delete_column->get_column_id(),
445  delete_column->get_table_id(),
446  delete_column->get_rte_idx());
447  input_col_descs.push_back(delete_col_desc);
448  target_exprs_owned_.emplace_back(case_expr);
449  } else {
450  // no filters, simply project the deleted=true column value for all rows
451  auto delete_col_desc =
452  std::make_shared<const InputColDescriptor>(delete_column->get_column_id(),
453  delete_column->get_table_id(),
454  delete_column->get_rte_idx());
455  input_col_descs.push_back(delete_col_desc);
456  target_exprs_owned_.emplace_back(deleted_constant);
457  }
458 
459  std::vector<Analyzer::Expr*> target_exprs;
460  CHECK_EQ(target_exprs_owned_.size(), size_t(1));
461  target_exprs.emplace_back(target_exprs_owned_.front().get());
462 
463  RelAlgExecutionUnit rewritten_exe_unit{ra_exe_unit_in.input_descs,
464  input_col_descs,
465  {},
466  {},
467  ra_exe_unit_in.join_quals,
468  ra_exe_unit_in.groupby_exprs,
469  target_exprs,
470  ra_exe_unit_in.estimator,
471  ra_exe_unit_in.sort_info,
472  ra_exe_unit_in.scan_limit,
473  ra_exe_unit_in.query_hint,
474  ra_exe_unit_in.use_bump_allocator,
475  ra_exe_unit_in.union_all,
476  ra_exe_unit_in.query_state};
477  return rewritten_exe_unit;
478 }
std::vector< Analyzer::Expr * > target_exprs
#define CHECK_EQ(x, y)
Definition: Logger.h:205
const std::optional< bool > union_all
bool boolval
Definition: sqltypes.h:202
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:922
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
#define CHECK(condition)
Definition: Logger.h:197
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 new_string_datum{.stringval = new std::string(
280  *original_constant_expr->get_constval().stringval)};
281 
282  new_column_value =
283  makeExpr<Analyzer::Constant>(column_to_update->get_type_info(),
284  original_constant_expr->get_is_null(),
285  new_string_datum);
286 
287  // Roll the string dict generation forward, as we have added a string
288  auto row_set_mem_owner = executor_->getRowSetMemoryOwner();
289  CHECK(row_set_mem_owner);
290  auto& str_dict_generations = row_set_mem_owner->getStringDictionaryGenerations();
291  if (str_dict_generations.getGeneration(dict_id) > -1) {
292  str_dict_generations.updateGeneration(dict_id,
293  string_dict->storageEntryCount());
294  } else {
295  // Simple update with no filters does not use a CASE, and therefore does not add
296  // a valid generation
297  str_dict_generations.setGeneration(dict_id, string_dict->storageEntryCount());
298  }
299  }
300  }
301  }
302 
303  auto input_col_descs = ra_exe_unit_in.input_col_descs;
304 
305  std::shared_ptr<Analyzer::Expr> filter;
306  std::vector<std::shared_ptr<Analyzer::Expr>> filter_exprs;
307  filter_exprs.insert(filter_exprs.end(),
308  ra_exe_unit_in.simple_quals.begin(),
309  ra_exe_unit_in.simple_quals.end());
310  filter_exprs.insert(
311  filter_exprs.end(), ra_exe_unit_in.quals.begin(), ra_exe_unit_in.quals.end());
312 
313  if (filter_exprs.size() > 0) {
314  std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
315  case_expr_list;
316  if (filter_exprs.size() == 1) {
317  filter = filter_exprs.front();
318  } else {
319  filter = std::accumulate(
320  std::next(filter_exprs.begin()),
321  filter_exprs.end(),
322  filter_exprs.front(),
323  [](const std::shared_ptr<Analyzer::Expr> a,
324  const std::shared_ptr<Analyzer::Expr> b) {
325  CHECK_EQ(a->get_type_info().get_type(), b->get_type_info().get_type());
326  return makeExpr<Analyzer::BinOper>(a->get_type_info().get_type(),
327  SQLOps::kAND,
329  a->deep_copy(),
330  b->deep_copy());
331  });
332  }
333  auto when_expr = filter; // only one filter, will be a BinOper if multiple filters
334  case_expr_list.emplace_back(std::make_pair(when_expr, new_column_value));
335  auto case_expr = Parser::CaseExpr::normalize(case_expr_list, column_to_update);
336 
337  auto col_to_update_var =
338  std::dynamic_pointer_cast<Analyzer::ColumnVar>(column_to_update);
339  CHECK(col_to_update_var);
340  auto col_to_update_desc =
341  std::make_shared<const InputColDescriptor>(col_to_update_var->get_column_id(),
342  col_to_update_var->get_table_id(),
343  col_to_update_var->get_rte_idx());
344  auto existing_col_desc_it = std::find_if(
345  input_col_descs.begin(),
346  input_col_descs.end(),
347  [&col_to_update_desc](const std::shared_ptr<const InputColDescriptor>& in) {
348  return *in == *col_to_update_desc;
349  });
350  if (existing_col_desc_it == input_col_descs.end()) {
351  input_col_descs.push_back(col_to_update_desc);
352  }
353  target_exprs_owned_.emplace_back(case_expr);
354  } else {
355  // no filters, simply project the update value
356  target_exprs_owned_.emplace_back(new_column_value);
357  }
358 
359  std::vector<Analyzer::Expr*> target_exprs;
360  CHECK_EQ(target_exprs_owned_.size(), size_t(1));
361  target_exprs.emplace_back(target_exprs_owned_.front().get());
362 
363  RelAlgExecutionUnit rewritten_exe_unit{ra_exe_unit_in.input_descs,
364  input_col_descs,
365  {},
366  {},
367  ra_exe_unit_in.join_quals,
368  ra_exe_unit_in.groupby_exprs,
369  target_exprs,
370  ra_exe_unit_in.estimator,
371  ra_exe_unit_in.sort_info,
372  ra_exe_unit_in.scan_limit,
373  ra_exe_unit_in.query_hint,
374  ra_exe_unit_in.use_bump_allocator,
375  ra_exe_unit_in.union_all,
376  ra_exe_unit_in.query_state};
377  return rewritten_exe_unit;
378 }
std::vector< Analyzer::Expr * > target_exprs
#define CHECK_EQ(x, y)
Definition: Logger.h:205
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:922
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:211
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
#define CHECK(condition)
Definition: Logger.h:197
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, generate_TableFunctionsFactory_init::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:99
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:197
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:94
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
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: