38 return ra_exe_unit_in;
41 return ra_exe_unit_in;
44 return ra_exe_unit_in;
46 if (ra_exe_unit_in.
quals.size() != 1) {
47 return ra_exe_unit_in;
55 if (!in_vals || in_vals->get_value_list().empty()) {
56 return ra_exe_unit_in;
58 for (
const auto& in_val : in_vals->get_value_list()) {
59 if (!std::dynamic_pointer_cast<Analyzer::Constant>(in_val)) {
63 if (dynamic_cast<const Analyzer::CaseExpr*>(in_vals->get_arg())) {
64 return ra_exe_unit_in;
68 auto it = std::find_if(
71 [&in_val_cv](std::shared_ptr<Analyzer::Expr> groupby_expr) {
73 std::dynamic_pointer_cast<Analyzer::ColumnVar>(groupby_expr)) {
74 return *in_val_cv == *groupby_cv.get();
81 return ra_exe_unit_in;
90 const std::shared_ptr<Analyzer::CaseExpr> case_expr,
92 std::list<std::shared_ptr<Analyzer::Expr>> new_groupby_list;
93 std::vector<Analyzer::Expr*> new_target_exprs;
95 size_t groupby_idx{0};
100 if (*group_expr == *in_vals->
get_arg()) {
106 const size_t range_sz = expr_range.
getIntMax() - expr_range.getIntMin() + 1;
111 new_groupby_list.push_back(case_expr);
112 for (
size_t i = 0; i < ra_exe_unit_in.
target_exprs.size(); ++i) {
114 if (*target == *in_vals->
get_arg()) {
115 auto var_case_expr = makeExpr<Analyzer::Var>(
118 new_target_exprs.push_back(var_case_expr.get());
120 new_target_exprs.push_back(target);
125 new_groupby_list.push_back(group_expr);
130 return ra_exe_unit_in;
135 ra_exe_unit_in.
quals,
151 std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
155 auto case_cond = makeExpr<Analyzer::BinOper>(
157 auto in_val_copy = in_val->deep_copy();
158 auto ti = in_val_copy->get_type_info();
163 in_val_copy->set_type_info(ti);
164 case_expr_list.emplace_back(case_cond, in_val_copy);
168 auto else_expr = case_expr_list.front().second;
169 return makeExpr<Analyzer::CaseExpr>(
170 case_expr_list.front().second->get_type_info(),
false, case_expr_list, else_expr);
173 std::shared_ptr<Analyzer::CaseExpr>
175 std::shared_ptr<Analyzer::Expr> expr)
const {
176 std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
182 const auto then_constant = makeExpr<Analyzer::Constant>(
kBIGINT,
false, then_d);
183 case_expr_list.emplace_back(is_not_null, then_constant);
186 const auto else_constant = makeExpr<Analyzer::Constant>(
kBIGINT,
false, else_d);
187 auto case_expr = makeExpr<Analyzer::CaseExpr>(
188 then_constant->get_type_info(),
false, case_expr_list, else_constant);
198 return string_id > max_valid_int_value<int8_t>();
200 return string_id > max_valid_int_value<int16_t>();
202 return string_id > max_valid_int_value<int32_t>();
218 std::shared_ptr<Analyzer::ColumnVar> column_to_update)
const {
224 throw std::runtime_error(
"Update via join not yet supported for temporary tables.");
227 auto new_column_value = ra_exe_unit_in.
target_exprs.front()->deep_copy();
228 const auto& new_column_ti = new_column_value->get_type_info();
229 if (column_to_update->get_type_info().is_dict_encoded_string()) {
230 CHECK(new_column_ti.is_dict_encoded_string());
231 if (new_column_ti.getStringDictKey().dict_id > 0 &&
232 new_column_ti.getStringDictKey() !=
233 column_to_update->get_type_info().getStringDictKey()) {
234 throw std::runtime_error(
235 "Updating a dictionary encoded string using another dictionary encoded string "
236 "column is not yet supported, unless both columns share dictionaries.");
238 if (
auto uoper = dynamic_cast<Analyzer::UOper*>(new_column_value.get())) {
239 if (uoper->get_optype() ==
kCAST &&
241 const auto original_constant_expr =
243 CHECK(original_constant_expr);
244 CHECK(original_constant_expr->get_type_info().is_string());
248 CHECK(column_to_update->get_type_info().is_dict_encoded_string());
249 const auto& dict_key = column_to_update->get_type_info().getStringDictKey();
250 std::map<int, StringDictionary*> string_dicts;
254 const auto dd = catalog->getMetadataForDict(dict_key.dict_id,
true);
256 auto string_dict = dd->stringDict;
260 string_dict->getOrAdd(*original_constant_expr->get_constval().stringval);
262 throw std::runtime_error(
263 "Ran out of space in dictionary, cannot update column with dictionary "
264 "encoded string value. Dictionary ID: " +
267 if (string_id == inline_int_null_value<int32_t>()) {
276 new std::string(*original_constant_expr->get_constval().stringval);
277 Datum new_string_datum{datum};
280 makeExpr<Analyzer::Constant>(column_to_update->get_type_info(),
281 original_constant_expr->get_is_null(),
285 auto row_set_mem_owner =
executor_->getRowSetMemoryOwner();
286 CHECK(row_set_mem_owner);
287 auto& str_dict_generations = row_set_mem_owner->getStringDictionaryGenerations();
288 if (str_dict_generations.getGeneration(dict_key) > -1) {
289 str_dict_generations.updateGeneration(dict_key,
290 string_dict->storageEntryCount());
294 str_dict_generations.setGeneration(dict_key, string_dict->storageEntryCount());
302 std::shared_ptr<Analyzer::Expr> filter;
303 std::vector<std::shared_ptr<Analyzer::Expr>> filter_exprs;
304 filter_exprs.insert(filter_exprs.end(),
308 filter_exprs.end(), ra_exe_unit_in.
quals.begin(), ra_exe_unit_in.
quals.end());
310 if (filter_exprs.size() > 0) {
311 std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
313 if (filter_exprs.size() == 1) {
314 filter = filter_exprs.front();
317 std::next(filter_exprs.begin()),
319 filter_exprs.front(),
320 [](
const std::shared_ptr<Analyzer::Expr>
a,
321 const std::shared_ptr<Analyzer::Expr> b) {
322 CHECK_EQ(a->get_type_info().get_type(), b->get_type_info().get_type());
323 return makeExpr<Analyzer::BinOper>(a->get_type_info().get_type(),
330 auto when_expr = filter;
331 case_expr_list.emplace_back(std::make_pair(when_expr, new_column_value));
334 auto col_to_update_var =
336 CHECK(col_to_update_var);
337 const auto& column_key = col_to_update_var->getColumnKey();
338 auto col_to_update_desc =
339 std::make_shared<const InputColDescriptor>(column_key.column_id,
342 col_to_update_var->get_rte_idx());
343 auto existing_col_desc_it = std::find_if(
344 input_col_descs.begin(),
345 input_col_descs.end(),
346 [&col_to_update_desc](
const std::shared_ptr<const InputColDescriptor>& in) {
347 return *in == *col_to_update_desc;
349 if (existing_col_desc_it == input_col_descs.end()) {
350 input_col_descs.push_back(col_to_update_desc);
358 std::vector<Analyzer::Expr*> target_exprs;
380 return rewritten_exe_unit;
389 std::shared_ptr<Analyzer::ColumnVar> delete_column)
const {
396 throw std::runtime_error(
"Delete via join not yet supported for temporary tables.");
401 const auto deleted_constant =
402 makeExpr<Analyzer::Constant>(delete_column->get_type_info(),
false, true_datum);
406 std::shared_ptr<Analyzer::Expr> filter;
407 std::vector<std::shared_ptr<Analyzer::Expr>> filter_exprs;
408 filter_exprs.insert(filter_exprs.end(),
412 filter_exprs.end(), ra_exe_unit_in.
quals.begin(), ra_exe_unit_in.
quals.end());
414 if (filter_exprs.size() > 0) {
415 std::list<std::pair<std::shared_ptr<Analyzer::Expr>, std::shared_ptr<Analyzer::Expr>>>
417 if (filter_exprs.size() == 1) {
418 filter = filter_exprs.front();
421 std::next(filter_exprs.begin()),
423 filter_exprs.front(),
424 [](
const std::shared_ptr<Analyzer::Expr>
a,
425 const std::shared_ptr<Analyzer::Expr> b) {
426 CHECK_EQ(a->get_type_info().get_type(), b->get_type_info().get_type());
427 return makeExpr<Analyzer::BinOper>(a->get_type_info().get_type(),
434 std::shared_ptr<Analyzer::Expr> column_to_update{
nullptr};
435 auto when_expr = filter;
436 case_expr_list.emplace_back(std::make_pair(when_expr, deleted_constant));
440 auto delete_col_desc_it = std::find_if(
441 input_col_descs.begin(),
442 input_col_descs.end(),
443 [&delete_column](
const std::shared_ptr<const InputColDescriptor>& in) {
444 return in->getColId() == delete_column->getColumnKey().column_id;
446 CHECK(delete_col_desc_it == input_col_descs.end());
447 const auto& column_key = delete_column->getColumnKey();
448 auto delete_col_desc =
449 std::make_shared<const InputColDescriptor>(column_key.column_id,
452 delete_column->get_rte_idx());
453 input_col_descs.push_back(delete_col_desc);
457 const auto& column_key = delete_column->getColumnKey();
458 auto delete_col_desc =
459 std::make_shared<const InputColDescriptor>(column_key.column_id,
462 delete_column->get_rte_idx());
463 input_col_descs.push_back(delete_col_desc);
467 std::vector<Analyzer::Expr*> target_exprs;
489 return rewritten_exe_unit;
493 const std::list<std::shared_ptr<Analyzer::Expr>>& groupby_exprs)
const {
494 std::set<size_t> gby_col_exprs_hash;
495 for (
auto gby_expr : groupby_exprs) {
496 if (
auto gby_col_var = std::dynamic_pointer_cast<Analyzer::ColumnVar>(gby_expr)) {
502 return {
true, gby_col_exprs_hash};
510 if (agg_expr->get_arg()) {
515 if (check_precond.second.count(agg_expr_hash)) {
521 if (!check_precond.first) {
525 return ra_exe_unit_in;
528 std::vector<Analyzer::Expr*> new_target_exprs;
530 bool rewritten =
false;
531 if (
auto agg_expr = dynamic_cast<Analyzer::AggExpr*>(expr)) {
532 if (is_expr_on_gby_col(agg_expr)) {
533 auto target_expr = agg_expr->get_arg();
536 if (target_expr && target_expr->get_type_info().get_type() !=
SQLTypes::kFLOAT) {
537 switch (agg_expr->get_aggtype()) {
542 !agg_expr->get_is_distinct()) {
547 new_target_exprs.push_back(case_expr.get());
559 auto agg_expr_ti = agg_expr->get_type_info();
560 auto target_expr = agg_expr->get_own_arg();
561 if (agg_expr_ti != target_expr->get_type_info()) {
562 target_expr = target_expr->add_cast(agg_expr_ti);
564 new_target_exprs.push_back(target_expr.get());
576 new_target_exprs.push_back(expr);
583 ra_exe_unit_in.
quals,
598 return rewritten_exe_unit;
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)
HOST DEVICE int get_size() const
size_t g_constrained_by_in_threshold
QueryPlanHash query_plan_dag_hash
const std::optional< bool > union_all
RelAlgExecutionUnit rewriteColumnarUpdate(const RelAlgExecutionUnit &ra_exe_unit_in, std::shared_ptr< Analyzer::ColumnVar > column_to_update) const
std::vector< InputDescriptor > input_descs
Constants for Builtin SQL Types supported by HEAVY.AI.
const std::list< std::shared_ptr< Analyzer::Expr > > groupby_exprs
Analyzer::ExpressionPtr rewrite_expr(const Analyzer::Expr *expr)
#define TRANSIENT_DICT_ID
static SysCatalog & instance()
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
TableIdToNodeMap table_id_to_node_map
DEVICE auto accumulate(ARGS &&...args)
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::shared_ptr< Catalog > getCatalog(const std::string &dbName)
bool check_string_id_overflow(const int32_t string_id, const SQLTypeInfo &ti)
std::unordered_map< size_t, SQLTypeInfo > target_exprs_original_type_infos
const std::list< std::shared_ptr< Analyzer::Expr > > & get_value_list() const
virtual std::shared_ptr< Analyzer::Expr > deep_copy() const =0
static const StringDictKey kTransientDictKey
const std::vector< InputTableInfo > & query_infos_
std::size_t hash_value(RexAbstractInput const &rex_ab_input)
std::list< std::shared_ptr< Analyzer::Expr > > quals
int64_t getIntMax() const
RegisteredQueryHint query_hint
int64_t inline_fixed_encoding_null_val(const SQL_TYPE_INFO &ti)
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
RelAlgExecutionUnit rewriteColumnarDelete(const RelAlgExecutionUnit &ra_exe_unit_in, std::shared_ptr< Analyzer::ColumnVar > delete_column) const
static std::shared_ptr< Analyzer::CaseExpr > generateCaseForDomainValues(const Analyzer::InValues *)
RelAlgExecutionUnit rewriteConstrainedByIn(const RelAlgExecutionUnit &ra_exe_unit_in) const
RelAlgExecutionUnit rewriteAggregateOnGroupByColumn(const RelAlgExecutionUnit &ra_exe_unit_in) const
const Expr * get_arg() const
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_
HashTableBuildDagMap hash_table_build_plan_dag