27 #include <rapidjson/error/en.h>
28 #include <rapidjson/error/error.h>
29 #include <rapidjson/stringbuffer.h>
30 #include <rapidjson/writer.h>
33 #include <unordered_set>
51 const std::shared_ptr<const ExecutionResult>
result) {
52 auto row_set = result->getRows();
54 CHECK_EQ(
size_t(1), row_set->colCount());
55 *(type_.get()) = row_set->getColType(0);
56 (*(result_.get())) =
result;
60 return std::make_unique<RexSubQuery>(type_, result_, ra_->deepCopy());
72 : old_input_(old_input), new_input_(new_input) {}
78 if (old_source == old_input_) {
96 std::vector<RexInput> outputs;
98 for (
size_t i = 0; i <
n; ++i) {
99 outputs.emplace_back(node, i);
109 std::unordered_map<unsigned, unsigned> old_to_new_index_map)
113 RexRebindInputsVisitor::visitInput(rex_input);
114 auto mapping_itr = mapping_.find(rex_input->
getIndex());
115 CHECK(mapping_itr != mapping_.end());
116 rex_input->
setIndex(mapping_itr->second);
121 const std::unordered_map<unsigned, unsigned>
mapping_;
129 std::shared_ptr<RelProject> new_project,
130 std::vector<std::unique_ptr<const RexScalar>>& scalar_exprs_for_new_project,
131 std::vector<std::string>& fields_for_new_project,
132 std::unordered_map<size_t, size_t>& expr_offset_cache)
133 : new_project_(new_project)
134 , scalar_exprs_for_new_project_(scalar_exprs_for_new_project)
135 , fields_for_new_project_(fields_for_new_project)
136 , expr_offset_cache_(expr_offset_cache)
137 , found_case_expr_window_operand_(
false)
138 , has_partition_expr_(
false) {}
141 auto hash = expr->
toHash();
142 auto it = expr_offset_cache_.find(hash);
143 auto new_offset = -1;
144 if (it == expr_offset_cache_.end()) {
146 expr_offset_cache_.emplace(hash, scalar_exprs_for_new_project_.size()).second);
147 new_offset = scalar_exprs_for_new_project_.size();
148 fields_for_new_project_.emplace_back(
"");
149 scalar_exprs_for_new_project_.emplace_back(deep_copier_.visit(expr));
152 new_offset = it->second;
158 size_t expr_offset)
const {
162 case WindowExprType::PARTITION_KEY: {
163 auto it = pushed_down_partition_key_offset_.find(expr_offset);
164 CHECK(it != pushed_down_partition_key_offset_.end());
167 case WindowExprType::ORDER_KEY: {
168 auto it = pushed_down_order_key_offset_.find(expr_offset);
169 CHECK(it != pushed_down_order_key_offset_.end());
185 pushed_down_window_operands_offset_.clear();
186 pushed_down_partition_key_offset_.clear();
187 pushed_down_order_key_offset_.clear();
188 for (
size_t offset = 0; offset < window_expr->
size(); ++offset) {
190 auto literal_expr =
dynamic_cast<const RexLiteral*
>(expr);
191 auto case_expr =
dynamic_cast<const RexCase*
>(expr);
197 found_case_expr_window_operand_ =
true;
200 auto new_offset = pushDownExpressionImpl(expr);
201 pushed_down_window_operands_offset_.emplace(offset, new_offset);
206 auto new_offset = pushDownExpressionImpl(partition_key.get());
207 pushed_down_partition_key_offset_.emplace(offset, new_offset);
212 for (
const auto& order_key : window_expr->
getOrderKeys()) {
213 auto new_offset = pushDownExpressionImpl(order_key.get());
214 pushed_down_order_key_offset_.emplace(offset, new_offset);
220 std::vector<std::unique_ptr<const RexScalar>> window_operands;
222 for (
size_t idx = 0; idx < window_expr->
size(); ++idx) {
223 auto it = pushed_down_window_operands_offset_.find(idx);
224 if (it != pushed_down_window_operands_offset_.end()) {
225 auto new_input = std::make_unique<const RexInput>(new_project_.get(), it->second);
227 window_operands.emplace_back(std::move(new_input));
229 auto copied_expr = deep_copier_.visit(window_expr->
getOperand(idx));
230 window_operands.emplace_back(std::move(copied_expr));
233 deconst_window_expr->replaceOperands(std::move(window_operands));
236 auto new_offset = getOffsetForPushedDownExpr(WindowExprType::PARTITION_KEY, idx);
238 auto new_input = std::make_unique<const RexInput>(new_project_.get(), *new_offset);
240 deconst_window_expr->replacePartitionKey(idx, std::move(new_input));
243 for (
size_t idx = 0; idx < window_expr->
getOrderKeys().size(); ++idx) {
244 auto new_offset = getOffsetForPushedDownExpr(WindowExprType::ORDER_KEY, idx);
246 auto new_input = std::make_unique<const RexInput>(new_project_.get(), *new_offset);
248 deconst_window_expr->replaceOrderKey(idx, std::move(new_input));
253 auto new_offset = pushDownExpressionImpl(rex_input);
254 CHECK_LT(new_offset, scalar_exprs_for_new_project_.size());
255 auto hash = rex_input->
toHash();
256 auto it = expr_offset_cache_.find(hash);
257 CHECK(it != expr_offset_cache_.end());
259 auto new_input = std::make_unique<const RexInput>(new_project_.get(), new_offset);
265 const RexLiteral* rex_literal)
const override {
266 return deep_copier_.visit(rex_literal);
269 std::unique_ptr<const RexScalar>
visitRef(
const RexRef* rex_ref)
const override {
270 return deep_copier_.visit(rex_ref);
274 const RexSubQuery* rex_subquery)
const override {
275 return deep_copier_.visit(rex_subquery);
280 std::pair<std::unique_ptr<const RexScalar>, std::unique_ptr<const RexScalar>>>
282 std::unique_ptr<const RexScalar> new_else_expr;
283 for (
size_t i = 0; i < rex_case->
branchCount(); ++i) {
284 const auto when = rex_case->
getWhen(i);
285 auto new_when = PushDownGenericExpressionInWindowFunction::visit(when);
286 const auto then = rex_case->
getThen(i);
287 auto new_then = PushDownGenericExpressionInWindowFunction::visit(then);
288 new_expr_pair_list.emplace_back(std::move(new_when), std::move(new_then));
291 new_else_expr = deep_copier_.visit(rex_case->
getElse());
293 auto new_case = std::make_unique<const RexCase>(new_expr_pair_list, new_else_expr);
299 const auto rex_window_func_operator =
301 if (rex_window_func_operator) {
302 pushDownExpressionInWindowFunction(rex_window_func_operator);
303 return deep_copier_.visit(rex_operator);
305 std::unique_ptr<const RexOperator> new_operator{
nullptr};
306 std::vector<std::unique_ptr<const RexScalar>> new_operands;
307 for (
size_t i = 0; i < rex_operator->
size(); ++i) {
308 const auto operand = rex_operator->
getOperand(i);
309 auto new_operand = PushDownGenericExpressionInWindowFunction::visit(operand);
310 new_operands.emplace_back(std::move(new_operand));
312 if (
auto function_op = dynamic_cast<const RexFunctionOperator*>(rex_operator)) {
313 new_operator = std::make_unique<const RexFunctionOperator>(
314 function_op->getName(), new_operands, rex_operator->
getType());
316 new_operator = std::make_unique<const RexOperator>(
329 std::unique_ptr<const RexScalar>
defaultResult()
const override {
return nullptr; }
346 std::shared_ptr<const RelAlgNode> old_input,
347 std::shared_ptr<const RelAlgNode> input,
348 std::optional<std::unordered_map<unsigned, unsigned>> old_to_new_index_map) {
350 std::unique_ptr<RexRebindInputsVisitor> rebind_inputs;
351 if (old_to_new_index_map) {
352 rebind_inputs = std::make_unique<RexRebindReindexInputsVisitor>(
353 old_input.get(), input.get(), *old_to_new_index_map);
356 std::make_unique<RexRebindInputsVisitor>(old_input.get(), input.get());
358 CHECK(rebind_inputs);
360 rebind_inputs->visit(scalar_expr.get());
365 std::unique_ptr<const RexScalar> new_input) {
366 fields_.emplace_back(std::move(new_field_name));
371 const auto scan_node =
dynamic_cast<const RelScan*
>(ra_node);
374 CHECK_EQ(
size_t(0), scan_node->inputCount());
375 return n_outputs(scan_node, scan_node->size());
377 const auto project_node =
dynamic_cast<const RelProject*
>(ra_node);
380 CHECK_EQ(
size_t(1), project_node->inputCount());
381 return n_outputs(project_node, project_node->size());
383 const auto filter_node =
dynamic_cast<const RelFilter*
>(ra_node);
386 CHECK_EQ(
size_t(1), filter_node->inputCount());
388 return n_outputs(filter_node, prev_out.size());
390 const auto aggregate_node =
dynamic_cast<const RelAggregate*
>(ra_node);
391 if (aggregate_node) {
393 CHECK_EQ(
size_t(1), aggregate_node->inputCount());
394 return n_outputs(aggregate_node, aggregate_node->size());
396 const auto compound_node =
dynamic_cast<const RelCompound*
>(ra_node);
399 CHECK_EQ(
size_t(1), compound_node->inputCount());
400 return n_outputs(compound_node, compound_node->size());
402 const auto join_node =
dynamic_cast<const RelJoin*
>(ra_node);
406 CHECK_EQ(
size_t(2), join_node->inputCount());
411 lhs_out.insert(lhs_out.end(), rhs_out.begin(), rhs_out.end());
414 const auto table_func_node =
dynamic_cast<const RelTableFunction*
>(ra_node);
415 if (table_func_node) {
417 return n_outputs(table_func_node, table_func_node->size());
419 const auto sort_node =
dynamic_cast<const RelSort*
>(ra_node);
422 CHECK_EQ(
size_t(1), sort_node->inputCount());
424 return n_outputs(sort_node, prev_out.size());
426 const auto logical_values_node =
dynamic_cast<const RelLogicalValues*
>(ra_node);
427 if (logical_values_node) {
428 CHECK_EQ(
size_t(0), logical_values_node->inputCount());
429 return n_outputs(logical_values_node, logical_values_node->size());
431 const auto logical_union_node =
dynamic_cast<const RelLogicalUnion*
>(ra_node);
432 if (logical_union_node) {
433 return n_outputs(logical_union_node, logical_union_node->size());
445 if (dynamic_cast<const RelJoin*>(source)) {
454 const auto input =
dynamic_cast<const RexInput*
>(scalar_expr.get());
456 CHECK_EQ(source, input->getSourceNode());
460 if (input->getSourceNode() != source_shape[i].getSourceNode()) {
471 const std::string& new_name) {
473 if (
auto join = dynamic_cast<const RelJoin*>(node)) {
475 const auto lhs_size =
join->getInput(0)->size();
476 if (index < lhs_size) {
483 if (
auto scan = dynamic_cast<const RelScan*>(node)) {
484 return new_name != scan->getFieldName(index);
487 if (
auto aggregate = dynamic_cast<const RelAggregate*>(node)) {
488 return new_name != aggregate->getFieldName(index);
491 if (
auto project = dynamic_cast<const RelProject*>(node)) {
492 return new_name != project->getFieldName(index);
495 if (
auto table_func = dynamic_cast<const RelTableFunction*>(node)) {
496 return new_name != table_func->getFieldName(index);
499 if (
auto logical_values = dynamic_cast<const RelLogicalValues*>(node)) {
500 const auto& tuple_type = logical_values->getTupleType();
502 return new_name != tuple_type[index].get_resname();
505 CHECK(dynamic_cast<const RelSort*>(node) || dynamic_cast<const RelFilter*>(node) ||
506 dynamic_cast<const RelLogicalUnion*>(node));
517 for (
size_t i = 0; i <
fields_.size(); ++i) {
528 std::shared_ptr<const RelAlgNode> input) {
530 RexRebindInputsVisitor rebind_inputs(old_input.get(), input.get());
537 std::shared_ptr<const RelAlgNode> input) {
539 RexRebindInputsVisitor rebind_inputs(old_input.get(), input.get());
540 rebind_inputs.visit(
filter_.get());
544 std::shared_ptr<const RelAlgNode> input) {
546 RexRebindInputsVisitor rebind_inputs(old_input.get(), input.get());
548 rebind_inputs.visit(scalar_source.get());
558 , fields_(rhs.fields_)
559 , hint_applied_(
false)
560 , hints_(std::make_unique<
Hints>())
561 , has_pushed_down_window_expr_(rhs.has_pushed_down_window_expr_) {
567 for (
auto const& kv : *rhs.
hints_) {
575 , tuple_type_(rhs.tuple_type_)
585 , groupby_count_(rhs.groupby_count_)
586 , fields_(rhs.fields_)
587 , hint_applied_(
false)
588 , hints_(std::make_unique<
Hints>()) {
594 for (
auto const& kv : *rhs.
hints_) {
602 , join_type_(rhs.join_type_)
603 , hint_applied_(
false)
604 , hints_(std::make_unique<
Hints>()) {
608 for (
auto const& kv : *rhs.
hints_) {
617 std::vector<std::unique_ptr<const RexAgg>>
const& agg_exprs) {
618 std::vector<std::unique_ptr<const RexAgg>> agg_exprs_copy;
619 agg_exprs_copy.reserve(agg_exprs.size());
620 for (
auto const& agg_expr : agg_exprs) {
621 agg_exprs_copy.push_back(agg_expr->deepCopy());
623 return agg_exprs_copy;
627 std::vector<std::unique_ptr<const RexScalar>>
const& scalar_sources) {
628 std::vector<std::unique_ptr<const RexScalar>> scalar_sources_copy;
629 scalar_sources_copy.reserve(scalar_sources.size());
631 for (
auto const& scalar_source : scalar_sources) {
632 scalar_sources_copy.push_back(copier.
visit(scalar_source.get()));
634 return scalar_sources_copy;
638 std::vector<std::unique_ptr<const RexAgg>>
const& agg_exprs_new,
639 std::vector<std::unique_ptr<const RexScalar>>
const& scalar_sources_new,
640 std::vector<std::unique_ptr<const RexAgg>>
const& agg_exprs_old,
641 std::vector<std::unique_ptr<const RexScalar>>
const& scalar_sources_old,
642 std::vector<const Rex*>
const& target_exprs_old) {
643 std::vector<const Rex*> target_exprs(target_exprs_old);
644 std::unordered_map<const Rex*, const Rex*> old_to_new_target(target_exprs.size());
645 for (
size_t i = 0; i < agg_exprs_new.size(); ++i) {
646 old_to_new_target.emplace(agg_exprs_old[i].
get(), agg_exprs_new[i].
get());
648 for (
size_t i = 0; i < scalar_sources_new.size(); ++i) {
649 old_to_new_target.emplace(scalar_sources_old[i].
get(), scalar_sources_new[i].
get());
651 for (
auto& target : target_exprs) {
652 auto target_it = old_to_new_target.find(target);
653 CHECK(target_it != old_to_new_target.end());
654 target = target_it->second;
664 , groupby_count_(rhs.groupby_count_)
666 , fields_(rhs.fields_)
667 , is_agg_(rhs.is_agg_)
674 , hint_applied_(
false)
675 , hints_(std::make_unique<
Hints>()) {
679 for (
auto const& kv : *rhs.
hints_) {
686 std::shared_ptr<const RelAlgNode> input) {
688 RexRebindInputsVisitor rebind_inputs(old_input.get(), input.get());
690 rebind_inputs.visit(target_expr.get());
693 rebind_inputs.visit(func_input.get());
698 int32_t literal_args = 0;
700 const auto rex_literal =
dynamic_cast<const RexLiteral*
>(arg.get());
711 std::vector<const Rex*>& column_inputs,
712 const std::vector<std::unique_ptr<const RexScalar>>& old_table_func_inputs,
713 const std::vector<std::unique_ptr<const RexScalar>>& new_table_func_inputs) {
714 CHECK_EQ(old_table_func_inputs.size(), new_table_func_inputs.size());
715 std::unordered_map<const Rex*, const Rex*> old_to_new_input;
716 for (
size_t i = 0; i < old_table_func_inputs.size(); ++i) {
717 old_to_new_input.emplace(old_table_func_inputs[i].
get(),
718 new_table_func_inputs[i].
get());
720 for (
auto& target : column_inputs) {
721 auto target_it = old_to_new_input.find(target);
722 CHECK(target_it != old_to_new_input.end());
723 target = target_it->second;
730 std::vector<std::unique_ptr<const RexScalar>>&& exprs) {
741 , function_name_(rhs.function_name_)
742 , fields_(rhs.fields_)
743 , col_inputs_(rhs.col_inputs_)
751 struct hash<std::pair<const RelAlgNode*, int>> {
752 size_t operator()(
const std::pair<const RelAlgNode*, int>& input_col)
const {
753 auto ptr_val =
reinterpret_cast<const int64_t*
>(&input_col.first);
754 auto h =
static_cast<size_t>(*ptr_val);
755 boost::hash_combine(h, input_col.second);
764 const size_t which_col) {
765 std::set<std::pair<const RelAlgNode*, int>> work_set;
767 auto curr_col = which_col;
769 work_set.insert(std::make_pair(walker, curr_col));
770 if (dynamic_cast<const RelScan*>(walker) || dynamic_cast<const RelJoin*>(walker)) {
773 CHECK_EQ(
size_t(1), walker->inputCount());
774 auto only_source = walker->getInput(0);
775 if (
auto project = dynamic_cast<const RelProject*>(walker)) {
776 if (
auto input = dynamic_cast<const RexInput*>(project->getProjectAt(curr_col))) {
777 const auto join_source =
dynamic_cast<const RelJoin*
>(only_source);
779 CHECK_EQ(
size_t(2), join_source->inputCount());
780 auto lhs = join_source->getInput(0);
781 CHECK((input->getIndex() < lhs->size() && lhs == input->getSourceNode()) ||
782 join_source->getInput(1) == input->getSourceNode());
784 CHECK_EQ(input->getSourceNode(), only_source);
786 curr_col = input->getIndex();
790 }
else if (
auto aggregate = dynamic_cast<const RelAggregate*>(walker)) {
791 if (curr_col >= aggregate->getGroupByCount()) {
795 walker = only_source;
807 for (
size_t i = 0, e =
collation_.size(); i < e; ++i) {
810 if (this_sort_key.getSortDir() != that_sort_key.getSortDir()) {
813 if (this_sort_key.getNullsPosition() != that_sort_key.getNullsPosition()) {
816 auto this_equiv_keys =
get_equiv_cols(
this, this_sort_key.getField());
817 auto that_equiv_keys =
get_equiv_cols(&that, that_sort_key.getField());
818 std::vector<std::pair<const RelAlgNode*, int>> intersect;
819 std::set_intersection(this_equiv_keys.begin(),
820 this_equiv_keys.end(),
821 that_equiv_keys.begin(),
822 that_equiv_keys.end(),
823 std::back_inserter(intersect));
824 if (intersect.empty()) {
834 :
RelAlgNode(std::move(inputs)), is_all_(is_all) {
837 "The DEPRECATED enable-union option is set to off. Please remove this option as "
838 "it may be disabled in the future.");
847 return inputs_.front()->size();
863 if (
auto const* input = dynamic_cast<RelCompound const*>(
inputs_[0].
get())) {
864 return input->getFieldName(i);
865 }
else if (
auto const* input = dynamic_cast<RelProject const*>(
inputs_[0].
get())) {
866 return input->getFieldName(i);
867 }
else if (
auto const* input = dynamic_cast<RelLogicalUnion const*>(
inputs_[0].
get())) {
868 return input->getFieldName(i);
869 }
else if (
auto const* input = dynamic_cast<RelAggregate const*>(
inputs_[0].
get())) {
870 return input->getFieldName(i);
871 }
else if (
auto const* input = dynamic_cast<RelScan const*>(
inputs_[0].
get())) {
872 return input->getFieldName(i);
873 }
else if (
auto const* input =
874 dynamic_cast<RelTableFunction const*>(
inputs_[0].
get())) {
875 return input->getFieldName(i);
882 std::vector<bool>
get_notnulls(std::vector<TargetMetaInfo>
const& tmis0) {
883 std::vector<bool> notnulls(tmis0.size());
884 for (
size_t j = 0; j < tmis0.size(); ++j) {
885 notnulls[j] = tmis0[j].get_type_info().get_notnull();
896 void set_notnulls(std::vector<TargetMetaInfo>* tmis0, std::vector<bool>
const& notnulls) {
897 for (
size_t j = 0; j < tmis0->size(); ++j) {
899 SQLTypeInfo physical_ti = (*tmis0)[j].get_physical_type_info();
902 (*tmis0)[j] =
TargetMetaInfo((*tmis0)[j].get_resname(), ti, physical_ti);
912 std::vector<TargetMetaInfo> tmis0 =
inputs_[0]->getOutputMetainfo();
914 for (
size_t i = 1; i <
inputs_.size(); ++i) {
915 std::vector<TargetMetaInfo>
const& tmisi =
inputs_[i]->getOutputMetainfo();
916 if (tmis0.size() != tmisi.size()) {
917 LOG(
INFO) <<
"tmis0.size()=" << tmis0.size() <<
" != " << tmisi.size()
918 <<
"=tmisi.size() for i=" << i;
919 throw std::runtime_error(
"Subqueries of a UNION must have matching data types.");
921 for (
size_t j = 0; j < tmis0.size(); ++j) {
926 LOG(
INFO) <<
"Types do not match for UNION:\n tmis0[" << j
927 <<
"].get_type_info().to_string() = " << ti0.
to_string() <<
"\n tmis"
929 <<
"].get_type_info().to_string() = " << ti1.
to_string();
932 throw std::runtime_error(
933 "Subqueries of a UNION must have the exact same data types.");
945 size_t input_idx)
const {
946 if (
auto const* rex_input_ptr = dynamic_cast<RexInput const*>(rex_scalar)) {
949 scalar_exprs_.emplace_back(std::make_shared<RexInput const>(std::move(rex_input)));
957 unsigned node_id(
const rapidjson::Value& ra_node) noexcept {
958 const auto&
id =
field(ra_node,
"id");
963 rapidjson::StringBuffer buffer;
964 rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
966 return buffer.GetString();
974 const rapidjson::Value& expr) noexcept {
975 const auto& input =
field(expr,
"input");
980 CHECK(expr.IsObject());
981 const auto& literal =
field(expr,
"literal");
987 const auto type_precision =
json_i64(
field(expr,
"type_precision"));
988 if (literal.IsNull()) {
989 return std::unique_ptr<RexLiteral>(
new RexLiteral(target_type));
1000 return std::unique_ptr<RexLiteral>(
new RexLiteral(
json_i64(literal),
1008 if (literal.IsDouble()) {
1009 return std::unique_ptr<RexLiteral>(
new RexLiteral(
json_double(literal),
1016 }
else if (literal.IsInt64()) {
1017 return std::make_unique<RexLiteral>(
static_cast<double>(literal.GetInt64()),
1025 }
else if (literal.IsUint64()) {
1026 return std::make_unique<RexLiteral>(
static_cast<double>(literal.GetUint64()),
1034 UNREACHABLE() <<
"Unhandled type: " << literal.GetType();
1037 return std::unique_ptr<RexLiteral>(
new RexLiteral(
json_str(literal),
1045 return std::unique_ptr<RexLiteral>(
new RexLiteral(
json_bool(literal),
1053 return std::unique_ptr<RexLiteral>(
new RexLiteral(target_type));
1061 std::unique_ptr<const RexScalar>
parse_scalar_expr(
const rapidjson::Value& expr,
1065 if (type_obj.IsArray()) {
1068 CHECK(type_obj.IsObject() && type_obj.MemberCount() >= 2)
1072 const auto precision_it = type_obj.FindMember(
"precision");
1073 const int precision =
1074 precision_it != type_obj.MemberEnd() ?
json_i64(precision_it->value) : 0;
1075 const auto scale_it = type_obj.FindMember(
"scale");
1076 const int scale = scale_it != type_obj.MemberEnd() ?
json_i64(scale_it->value) : 0;
1084 const rapidjson::Value& arr,
1086 std::vector<std::unique_ptr<const RexScalar>> exprs;
1087 for (
auto it = arr.Begin(); it != arr.End(); ++it) {
1094 if (name ==
"ROW_NUMBER") {
1097 if (name ==
"RANK") {
1100 if (name ==
"DENSE_RANK") {
1103 if (name ==
"PERCENT_RANK") {
1106 if (name ==
"CUME_DIST") {
1109 if (name ==
"NTILE") {
1112 if (name ==
"LAG") {
1115 if (name ==
"LAG_IN_FRAME") {
1118 if (name ==
"LEAD") {
1121 if (name ==
"LEAD_IN_FRAME") {
1124 if (name ==
"FIRST_VALUE") {
1127 if (name ==
"LAST_VALUE") {
1130 if (name ==
"NTH_VALUE") {
1133 if (name ==
"NTH_VALUE_IN_FRAME") {
1136 if (name ==
"AVG") {
1139 if (name ==
"MIN") {
1142 if (name ==
"MAX") {
1145 if (name ==
"SUM") {
1148 if (name ==
"COUNT") {
1151 if (name ==
"COUNT_IF") {
1154 if (name ==
"SUM_IF") {
1157 if (name ==
"$SUM0") {
1160 throw std::runtime_error(
"Unsupported window function: " + name);
1164 const rapidjson::Value& arr,
1166 std::vector<std::unique_ptr<const RexScalar>> exprs;
1167 for (
auto it = arr.Begin(); it != arr.End(); ++it) {
1174 return json_str(
field(collation,
"direction")) == std::string(
"DESCENDING")
1180 return json_str(
field(collation,
"nulls")) == std::string(
"FIRST")
1187 std::vector<SortField> collation;
1188 size_t field_idx = 0;
1189 for (
auto it = arr.Begin(); it != arr.End(); ++it, ++field_idx) {
1192 collation.emplace_back(field_idx, sort_dir, null_pos);
1198 const rapidjson::Value& window_bound_obj,
1200 CHECK(window_bound_obj.IsObject());
1203 window_bound.preceding =
json_bool(
field(window_bound_obj,
"preceding"));
1204 window_bound.following =
json_bool(
field(window_bound_obj,
"following"));
1205 window_bound.is_current_row =
json_bool(
field(window_bound_obj,
"is_current_row"));
1206 const auto& offset_field =
field(window_bound_obj,
"offset");
1207 if (offset_field.IsObject()) {
1210 CHECK(offset_field.IsNull());
1212 window_bound.order_key =
json_i64(
field(window_bound_obj,
"order_key"));
1213 return window_bound;
1218 const auto& operands =
field(expr,
"operands");
1219 CHECK(operands.IsArray());
1220 CHECK_GE(operands.Size(), unsigned(0));
1221 const auto& subquery_ast =
field(expr,
"subquery");
1224 const auto subquery_root_node = subquery_dag->getRootNodeShPtr();
1225 auto subquery = std::make_shared<RexSubQuery>(subquery_root_node);
1226 auto query_hint = subquery_dag->getQueryHint(subquery_dag->getRootNodeShPtr().get());
1228 const auto subquery_global_hint = subquery_dag->getGlobalHints();
1229 if (subquery_global_hint.isAnyQueryHintDelivered()) {
1231 const auto new_global_hint = root_dag.
getGlobalHints() || subquery_global_hint;
1234 const auto subquery_local_hint = subquery_dag->getQueryHint(subquery_root_node.get());
1235 if (subquery_local_hint) {
1240 return subquery->deepCopy();
1246 const bool is_quantifier =
1247 op_name == std::string(
"PG_ANY") || op_name == std::string(
"PG_ALL");
1249 const auto& operators_json_arr =
field(expr,
"operands");
1250 CHECK(operators_json_arr.IsArray());
1252 const auto type_it = expr.FindMember(
"type");
1253 CHECK(type_it != expr.MemberEnd());
1255 if (op ==
kIN && expr.HasMember(
"subquery")) {
1257 operands.emplace_back(std::move(subquery));
1259 if (expr.FindMember(
"partition_keys") != expr.MemberEnd()) {
1260 const auto& partition_keys_arr =
field(expr,
"partition_keys");
1262 const auto& order_keys_arr =
field(expr,
"order_keys");
1269 ti.set_notnull(
false);
1270 return std::make_unique<RexWindowFunctionOperator>(kind,
1280 return std::unique_ptr<RexOperator>(op ==
kFUNCTION
1286 const auto& operands =
field(expr,
"operands");
1287 CHECK(operands.IsArray());
1288 CHECK_GE(operands.Size(), unsigned(2));
1289 std::unique_ptr<const RexScalar> else_expr;
1291 std::pair<std::unique_ptr<const RexScalar>, std::unique_ptr<const RexScalar>>>
1293 for (
auto operands_it = operands.Begin(); operands_it != operands.End();) {
1295 if (operands_it == operands.End()) {
1296 else_expr = std::move(when_expr);
1300 expr_pair_list.emplace_back(std::move(when_expr), std::move(then_expr));
1302 return std::unique_ptr<RexCase>(
new RexCase(expr_pair_list, else_expr));
1306 const rapidjson::Value& json_str_arr) noexcept {
1307 CHECK(json_str_arr.IsArray());
1308 std::vector<std::string> fields;
1309 for (
auto json_str_arr_it = json_str_arr.Begin(); json_str_arr_it != json_str_arr.End();
1310 ++json_str_arr_it) {
1311 CHECK(json_str_arr_it->IsString());
1312 fields.emplace_back(json_str_arr_it->GetString());
1318 const rapidjson::Value& json_idx_arr) noexcept {
1319 CHECK(json_idx_arr.IsArray());
1320 std::vector<size_t> indices;
1321 for (
auto json_idx_arr_it = json_idx_arr.Begin(); json_idx_arr_it != json_idx_arr.End();
1322 ++json_idx_arr_it) {
1323 CHECK(json_idx_arr_it->IsInt());
1324 CHECK_GE(json_idx_arr_it->GetInt(), 0);
1325 indices.emplace_back(json_idx_arr_it->GetInt());
1332 if (agg_str ==
"APPROX_QUANTILE") {
1333 LOG(
INFO) <<
"APPROX_QUANTILE is deprecated. Please use APPROX_PERCENTILE instead.";
1339 bool const allow_multiple_args =
1340 shared::is_any<kAPPROX_COUNT_DISTINCT, kAPPROX_QUANTILE, kSUM_IF>(agg);
1341 if (operands.size() > 1 && (operands.size() != 2 || !allow_multiple_args)) {
1344 return std::unique_ptr<const RexAgg>(
new RexAgg(agg, distinct, agg_ti, operands));
1349 CHECK(expr.IsObject());
1350 if (expr.IsObject() && expr.HasMember(
"input")) {
1353 if (expr.IsObject() && expr.HasMember(
"literal")) {
1354 return std::unique_ptr<const RexScalar>(
parse_literal(expr));
1356 if (expr.IsObject() && expr.HasMember(
"op")) {
1358 if (op_str == std::string(
"CASE")) {
1359 return std::unique_ptr<const RexScalar>(
parse_case(expr, root_dag));
1361 if (op_str == std::string(
"$SCALAR_QUERY")) {
1362 return std::unique_ptr<const RexScalar>(
parse_subquery(expr, root_dag));
1364 return std::unique_ptr<const RexScalar>(
parse_operator(expr, root_dag));
1371 if (join_type_name ==
"inner") {
1374 if (join_type_name ==
"left") {
1377 if (join_type_name ==
"semi") {
1380 if (join_type_name ==
"anti") {
1391 std::vector<std::unique_ptr<const RexScalar>> disambiguated_operands;
1392 for (
size_t i = 0; i < rex_operator->size(); ++i) {
1393 auto operand = rex_operator->getOperand(i);
1394 if (dynamic_cast<const RexSubQuery*>(operand)) {
1395 disambiguated_operands.emplace_back(rex_operator->getOperandAndRelease(i));
1400 const auto rex_window_function_operator =
1402 if (rex_window_function_operator) {
1403 const auto& partition_keys = rex_window_function_operator->
getPartitionKeys();
1404 std::vector<std::unique_ptr<const RexScalar>> disambiguated_partition_keys;
1405 for (
const auto& partition_key : partition_keys) {
1406 disambiguated_partition_keys.emplace_back(
1409 std::vector<std::unique_ptr<const RexScalar>> disambiguated_order_keys;
1410 const auto& order_keys = rex_window_function_operator->getOrderKeys();
1411 for (
const auto& order_key : order_keys) {
1412 disambiguated_order_keys.emplace_back(
disambiguate_rex(order_key.get(), ra_output));
1414 return rex_window_function_operator->disambiguatedOperands(
1415 disambiguated_operands,
1416 disambiguated_partition_keys,
1417 disambiguated_order_keys,
1418 rex_window_function_operator->getCollation());
1420 return rex_operator->getDisambiguated(disambiguated_operands);
1426 std::pair<std::unique_ptr<const RexScalar>, std::unique_ptr<const RexScalar>>>
1427 disambiguated_expr_pair_list;
1428 for (
size_t i = 0; i < rex_case->
branchCount(); ++i) {
1431 disambiguated_expr_pair_list.emplace_back(std::move(disambiguated_when),
1432 std::move(disambiguated_then));
1434 std::unique_ptr<const RexScalar> disambiguated_else{
1436 return std::unique_ptr<const RexCase>(
1437 new RexCase(disambiguated_expr_pair_list, disambiguated_else));
1446 const auto rex_abstract_input =
dynamic_cast<const RexAbstractInput*
>(rex_scalar);
1447 if (rex_abstract_input) {
1448 CHECK_LT(static_cast<size_t>(rex_abstract_input->getIndex()), ra_output.size());
1449 return std::unique_ptr<const RexInput>(
1450 new RexInput(ra_output[rex_abstract_input->getIndex()]));
1452 const auto rex_operator =
dynamic_cast<const RexOperator*
>(rex_scalar);
1456 const auto rex_case =
dynamic_cast<const RexCase*
>(rex_scalar);
1460 if (
auto const rex_literal = dynamic_cast<const RexLiteral*>(rex_scalar)) {
1461 return rex_literal->deepCopy();
1462 }
else if (
auto const rex_subquery = dynamic_cast<const RexSubQuery*>(rex_scalar)) {
1463 return rex_subquery->deepCopy();
1466 std::string(
typeid(*rex_scalar).name()));
1471 CHECK_EQ(
size_t(1), project_node->inputCount());
1472 std::vector<std::unique_ptr<const RexScalar>> disambiguated_exprs;
1473 for (
size_t i = 0; i < project_node->size(); ++i) {
1474 const auto projected_expr = project_node->getProjectAt(i);
1475 if (dynamic_cast<const RexSubQuery*>(projected_expr)) {
1476 disambiguated_exprs.emplace_back(project_node->getProjectAtAndRelease(i));
1481 project_node->setExpressions(disambiguated_exprs);
1486 std::vector<std::unique_ptr<const RexScalar>> disambiguated_exprs;
1487 for (
size_t i = 0; i < table_func_node->getTableFuncInputsSize(); ++i) {
1488 const auto target_expr = table_func_node->getTableFuncInputAt(i);
1489 if (dynamic_cast<const RexSubQuery*>(target_expr)) {
1490 disambiguated_exprs.emplace_back(table_func_node->getTableFuncInputAtAndRelease(i));
1495 table_func_node->setTableFuncInputs(std::move(disambiguated_exprs));
1498 void bind_inputs(
const std::vector<std::shared_ptr<RelAlgNode>>& nodes) noexcept {
1499 for (
auto ra_node : nodes) {
1500 const auto filter_node = std::dynamic_pointer_cast<
RelFilter>(ra_node);
1502 CHECK_EQ(
size_t(1), filter_node->inputCount());
1504 filter_node->getCondition(),
get_node_output(filter_node->getInput(0)));
1505 filter_node->setCondition(disambiguated_condition);
1508 const auto join_node = std::dynamic_pointer_cast<
RelJoin>(ra_node);
1510 CHECK_EQ(
size_t(2), join_node->inputCount());
1511 auto disambiguated_condition =
1513 join_node->setCondition(disambiguated_condition);
1516 const auto project_node = std::dynamic_pointer_cast<
RelProject>(ra_node);
1522 const auto table_func_node = std::dynamic_pointer_cast<
RelTableFunction>(ra_node);
1523 if (table_func_node) {
1529 input.reserve(table_func_node->inputCount());
1530 for (
size_t i = 0; i < table_func_node->inputCount(); i++) {
1532 input.insert(input.end(), node_output.begin(), node_output.end());
1545 for (
auto node : nodes) {
1546 Hints* hint_delivered =
nullptr;
1547 const auto agg_node = std::dynamic_pointer_cast<
RelAggregate>(node);
1549 if (agg_node->hasDeliveredHint()) {
1553 const auto project_node = std::dynamic_pointer_cast<
RelProject>(node);
1555 if (project_node->hasDeliveredHint()) {
1559 const auto compound_node = std::dynamic_pointer_cast<
RelCompound>(node);
1560 if (compound_node) {
1561 if (compound_node->hasDeliveredHint()) {
1565 if (hint_delivered && !hint_delivered->empty()) {
1566 rel_alg_dag.registerQueryHints(node, hint_delivered, global_query_hint);
1572 const auto existing_global_query_hints = rel_alg_dag.getGlobalHints();
1573 const auto new_global_query_hints = existing_global_query_hints || global_query_hint;
1574 rel_alg_dag.setGlobalQueryHints(new_global_query_hints);
1585 nodes.rbegin(), nodes.rend(), [](
const std::shared_ptr<RelAlgNode>& node) {
1586 auto node_hash = node->toHash();
1587 CHECK_NE(node_hash, static_cast<size_t>(0));
1591 void mark_nops(
const std::vector<std::shared_ptr<RelAlgNode>>& nodes) noexcept {
1592 for (
auto node : nodes) {
1593 const auto agg_node = std::dynamic_pointer_cast<
RelAggregate>(node);
1594 if (!agg_node || agg_node->getAggExprsCount()) {
1597 CHECK_EQ(
size_t(1), node->inputCount());
1598 const auto agg_input_node =
dynamic_cast<const RelAggregate*
>(node->getInput(0));
1599 if (agg_input_node && !agg_input_node->getAggExprsCount() &&
1600 agg_node->getGroupByCount() == agg_input_node->getGroupByCount()) {
1610 const std::vector<const Rex*>& target_exprs) noexcept {
1611 std::vector<const Rex*>
result;
1612 for (
size_t i = 0; i < simple_project->size(); ++i) {
1613 const auto input_rex =
dynamic_cast<const RexInput*
>(simple_project->getProjectAt(i));
1615 CHECK_LT(static_cast<size_t>(input_rex->getIndex()), target_exprs.size());
1616 result.push_back(target_exprs[input_rex->getIndex()]);
1630 const std::vector<std::unique_ptr<const RexScalar>>& scalar_sources)
1631 : node_to_keep_(node_to_keep), scalar_sources_(scalar_sources) {}
1635 if (input->getSourceNode() == node_to_keep_) {
1636 const auto index = input->getIndex();
1637 CHECK_LT(index, scalar_sources_.size());
1638 return visit(scalar_sources_[index].
get());
1640 return input->deepCopy();
1652 std::vector<std::shared_ptr<RelAlgNode>>& nodes,
1653 const std::vector<size_t>& pattern,
1654 std::unordered_map<
size_t, std::unordered_map<unsigned, RegisteredQueryHint>>&
1655 query_hints) noexcept {
1656 CHECK_GE(pattern.size(), size_t(2));
1657 CHECK_LE(pattern.size(), size_t(4));
1659 std::unique_ptr<const RexScalar> filter_rex;
1660 std::vector<std::unique_ptr<const RexScalar>> scalar_sources;
1661 size_t groupby_count{0};
1662 std::vector<std::string> fields;
1663 std::vector<const RexAgg*> agg_exprs;
1664 std::vector<const Rex*> target_exprs;
1665 bool first_project{
true};
1669 std::shared_ptr<ModifyManipulationTarget> manipulation_target;
1670 size_t node_hash{0};
1672 bool hint_registered{
false};
1674 for (
const auto node_idx : pattern) {
1675 const auto ra_node = nodes[node_idx];
1676 auto registered_query_hint_map_it = query_hints.find(ra_node->toHash());
1677 if (registered_query_hint_map_it != query_hints.end()) {
1678 auto& registered_query_hint_map = registered_query_hint_map_it->second;
1679 auto registered_query_hint_it = registered_query_hint_map.find(ra_node->getId());
1680 if (registered_query_hint_it != registered_query_hint_map.end()) {
1681 hint_registered =
true;
1682 node_hash = registered_query_hint_map_it->first;
1683 node_id = registered_query_hint_it->first;
1684 registered_query_hint = registered_query_hint_it->second;
1687 const auto ra_filter = std::dynamic_pointer_cast<
RelFilter>(ra_node);
1690 filter_rex.reset(ra_filter->getAndReleaseCondition());
1692 last_node = ra_node.get();
1695 const auto ra_project = std::dynamic_pointer_cast<
RelProject>(ra_node);
1698 manipulation_target = ra_project;
1700 if (first_project) {
1701 CHECK_EQ(
size_t(1), ra_project->inputCount());
1705 const auto filter_input =
dynamic_cast<const RelFilter*
>(ra_project->getInput(0));
1707 CHECK_EQ(
size_t(1), filter_input->inputCount());
1711 scalar_sources = ra_project->getExpressionsAndRelease();
1712 for (
const auto& scalar_expr : scalar_sources) {
1713 target_exprs.push_back(scalar_expr.get());
1715 first_project =
false;
1717 if (ra_project->isSimple()) {
1722 std::vector<const Rex*>
result;
1723 RexInputReplacementVisitor visitor(last_node, scalar_sources);
1724 for (
size_t i = 0; i < ra_project->size(); ++i) {
1725 const auto rex = ra_project->getProjectAt(i);
1726 if (
auto rex_input = dynamic_cast<const RexInput*>(rex)) {
1727 const auto index = rex_input->getIndex();
1728 CHECK_LT(index, target_exprs.size());
1729 result.push_back(target_exprs[index]);
1731 scalar_sources.push_back(visitor.visit(rex));
1732 result.push_back(scalar_sources.back().get());
1738 last_node = ra_node.get();
1741 const auto ra_aggregate = std::dynamic_pointer_cast<
RelAggregate>(ra_node);
1744 fields = ra_aggregate->getFields();
1745 agg_exprs = ra_aggregate->getAggregatesAndRelease();
1746 groupby_count = ra_aggregate->getGroupByCount();
1747 decltype(target_exprs){}.swap(target_exprs);
1748 CHECK_LE(groupby_count, scalar_sources.size());
1749 for (
size_t group_idx = 0; group_idx < groupby_count; ++group_idx) {
1750 const auto rex_ref =
new RexRef(group_idx + 1);
1751 target_exprs.push_back(rex_ref);
1752 scalar_sources.emplace_back(rex_ref);
1754 for (
const auto rex_agg : agg_exprs) {
1755 target_exprs.push_back(rex_agg);
1757 last_node = ra_node.get();
1762 auto compound_node =
1763 std::make_shared<RelCompound>(filter_rex,
1770 manipulation_target->isUpdateViaSelect(),
1771 manipulation_target->isDeleteViaSelect(),
1772 manipulation_target->isVarlenUpdateRequired(),
1773 manipulation_target->getModifiedTableDescriptor(),
1774 manipulation_target->getTargetColumns(),
1775 manipulation_target->getModifiedTableCatalog());
1776 auto old_node = nodes[pattern.back()];
1777 nodes[pattern.back()] = compound_node;
1778 auto first_node = nodes[pattern.front()];
1779 CHECK_EQ(
size_t(1), first_node->inputCount());
1780 compound_node->addManagedInput(first_node->getAndOwnInput(0));
1781 if (hint_registered) {
1784 auto registered_query_hint_map_it = query_hints.find(node_hash);
1785 CHECK(registered_query_hint_map_it != query_hints.end());
1786 auto registered_query_hint_map = registered_query_hint_map_it->second;
1787 if (registered_query_hint_map.size() > 1) {
1788 registered_query_hint_map.erase(
node_id);
1790 CHECK_EQ(registered_query_hint_map.size(),
static_cast<size_t>(1));
1791 query_hints.erase(node_hash);
1793 std::unordered_map<unsigned, RegisteredQueryHint> hint_map;
1794 hint_map.emplace(compound_node->getId(), registered_query_hint);
1795 query_hints.emplace(compound_node->toHash(), hint_map);
1797 for (
size_t i = 0; i < pattern.size() - 1; ++i) {
1798 nodes[pattern[i]].reset();
1800 for (
auto node : nodes) {
1804 node->replaceInput(old_node, compound_node);
1808 class RANodeIterator :
public std::vector<std::shared_ptr<RelAlgNode>>::const_iterator {
1810 using Super = std::vector<ElementType>::const_iterator;
1817 : Super(nodes.begin()), owner_(nodes), nodeCount_([&nodes]() -> size_t {
1818 size_t non_zero_count = 0;
1819 for (
const auto& node : nodes) {
1827 explicit operator size_t() {
1828 return std::distance(owner_.begin(), *
static_cast<Super*
>(
this));
1831 RANodeIterator operator++() =
delete;
1834 Super& super = *
this;
1836 case AdvancingMode::DUChain: {
1837 size_t use_count = 0;
1838 Super only_use = owner_.end();
1839 for (
Super nodeIt = std::next(super); nodeIt != owner_.end(); ++nodeIt) {
1843 for (
size_t i = 0; i < (*nodeIt)->inputCount(); ++i) {
1844 if ((*super) == (*nodeIt)->getAndOwnInput(i)) {
1846 if (1 == use_count) {
1849 super = owner_.end();
1858 case AdvancingMode::InOrder:
1859 for (
size_t i = 0; i != owner_.size(); ++i) {
1860 if (!visited_.count(i)) {
1861 super = owner_.begin();
1862 std::advance(super, i);
1866 super = owner_.end();
1876 visited_.insert(
size_t(*
this));
1877 Super& super = *
this;
1893 const bool first_rex_is_input) {
1894 if (
auto agg_node = dynamic_cast<const RelAggregate*>(parent_node)) {
1895 if (index == 0 && agg_node->getGroupByCount() > 0) {
1901 return first_rex_is_input;
1904 return first_rex_is_input;
1929 return aggregate && next_result;
1937 const auto case_operator =
dynamic_cast<const RexCase*
>(rex);
1938 if (case_operator && case_operator->branchCount() == 1) {
1939 const auto then_window =
1954 const auto case_operator =
1956 const auto second_window =
1958 if (case_operator && second_window &&
1971 if (dynamic_cast<const RexWindowFunctionOperator*>(rex)) {
1976 const auto rex_cast =
dynamic_cast<const RexOperator*
>(rex);
1977 if (rex_cast && rex_cast->getOperator() ==
kCAST) {
1978 CHECK_EQ(rex_cast->size(), size_t(1));
1992 std::vector<std::shared_ptr<RelAlgNode>>& nodes,
1993 const std::vector<const RelAlgNode*>& left_deep_joins,
1994 std::unordered_map<
size_t, std::unordered_map<unsigned, RegisteredQueryHint>>&
1996 enum class CoalesceState { Initial, Filter, FirstProject, Aggregate };
1997 std::vector<size_t> crt_pattern;
1998 CoalesceState crt_state{CoalesceState::Initial};
2000 auto reset_state = [&crt_pattern, &crt_state]() {
2001 crt_state = CoalesceState::Initial;
2002 std::vector<size_t>().
swap(crt_pattern);
2006 const auto ra_node = nodeIt != nodes.end() ? *nodeIt :
nullptr;
2007 switch (crt_state) {
2008 case CoalesceState::Initial: {
2009 if (std::dynamic_pointer_cast<const RelFilter>(ra_node) &&
2010 std::find(left_deep_joins.begin(), left_deep_joins.end(), ra_node.get()) ==
2011 left_deep_joins.end()) {
2012 crt_pattern.push_back(
size_t(nodeIt));
2013 crt_state = CoalesceState::Filter;
2014 nodeIt.advance(RANodeIterator::AdvancingMode::DUChain);
2015 }
else if (
auto project_node =
2016 std::dynamic_pointer_cast<const RelProject>(ra_node)) {
2017 if (project_node->hasWindowFunctionExpr()) {
2018 nodeIt.advance(RANodeIterator::AdvancingMode::InOrder);
2020 crt_pattern.push_back(
size_t(nodeIt));
2021 crt_state = CoalesceState::FirstProject;
2022 nodeIt.advance(RANodeIterator::AdvancingMode::DUChain);
2025 nodeIt.advance(RANodeIterator::AdvancingMode::InOrder);
2029 case CoalesceState::Filter: {
2030 if (
auto project_node = std::dynamic_pointer_cast<const RelProject>(ra_node)) {
2033 CHECK(!project_node->hasWindowFunctionExpr());
2034 crt_pattern.push_back(
size_t(nodeIt));
2035 crt_state = CoalesceState::FirstProject;
2036 nodeIt.advance(RANodeIterator::AdvancingMode::DUChain);
2042 case CoalesceState::FirstProject: {
2043 if (std::dynamic_pointer_cast<const RelAggregate>(ra_node)) {
2044 crt_pattern.push_back(
size_t(nodeIt));
2045 crt_state = CoalesceState::Aggregate;
2046 nodeIt.advance(RANodeIterator::AdvancingMode::DUChain);
2048 if (crt_pattern.size() >= 2) {
2055 case CoalesceState::Aggregate: {
2056 if (
auto project_node = std::dynamic_pointer_cast<const RelProject>(ra_node)) {
2057 if (!project_node->hasWindowFunctionExpr()) {
2059 bool is_simple_project{
true};
2060 for (
size_t i = 0; i < project_node->size(); i++) {
2061 const auto scalar_rex = project_node->getProjectAt(i);
2063 if (
auto input_rex = dynamic_cast<const RexInput*>(scalar_rex)) {
2065 input_rex->getSourceNode(), input_rex->getIndex(),
true)) {
2066 is_simple_project =
false;
2071 CoalesceSecondaryProjectVisitor visitor;
2072 if (!visitor.visit(project_node->getProjectAt(i))) {
2073 is_simple_project =
false;
2077 if (is_simple_project) {
2078 crt_pattern.push_back(
size_t(nodeIt));
2079 nodeIt.advance(RANodeIterator::AdvancingMode::InOrder);
2083 CHECK_GE(crt_pattern.size(), size_t(2));
2092 if (crt_state == CoalesceState::FirstProject || crt_state == CoalesceState::Aggregate) {
2093 if (crt_pattern.size() >= 2) {
2096 CHECK(!crt_pattern.empty());
2103 std::unordered_map<size_t, const RexScalar*>& collected_window_func,
2104 bool only_add_window_expr)
2105 : collected_window_func_(collected_window_func)
2106 , only_add_window_expr_(only_add_window_expr) {}
2112 tryAddWindowExpr(rex_operator);
2114 const size_t operand_count = rex_operator->size();
2115 for (
size_t i = 0; i < operand_count; ++i) {
2116 const auto operand = rex_operator->getOperand(i);
2120 tryAddWindowExpr(operand);
2125 return defaultResult();
2134 tryAddWindowExpr(rex_case);
2135 if (!only_add_window_expr_) {
2140 for (
size_t i = 0; i < rex_case->
branchCount(); ++i) {
2141 const auto when = rex_case->
getWhen(i);
2143 tryAddWindowExpr(when);
2147 const auto then = rex_case->
getThen(i);
2149 tryAddWindowExpr(then);
2155 auto else_expr = rex_case->
getElse();
2157 tryAddWindowExpr(else_expr);
2162 return defaultResult();
2166 if (!only_add_window_expr_) {
2167 collected_window_func_.emplace(expr->
toHash(), expr);
2169 if (
auto window_expr = dynamic_cast<RexWindowFunctionOperator const*>(expr)) {
2170 collected_window_func_.emplace(window_expr->toHash(), window_expr);
2185 std::unordered_set<size_t>& collected_window_func_hash,
2186 std::vector<std::unique_ptr<const RexScalar>>& new_rex_input_for_window_func,
2187 std::unordered_map<size_t, size_t>& window_func_to_new_rex_input_idx_map,
2189 std::unordered_map<
size_t, std::unique_ptr<const RexInput>>&
2190 new_rex_input_from_child_node)
2191 : collected_window_func_hash_(collected_window_func_hash)
2192 , new_rex_input_for_window_func_(new_rex_input_for_window_func)
2193 , window_func_to_new_rex_input_idx_map_(window_func_to_new_rex_input_idx_map)
2194 , new_project_(new_project)
2195 , new_rex_input_from_child_node_(new_rex_input_from_child_node) {
2196 CHECK_EQ(collected_window_func_hash_.size(),
2197 window_func_to_new_rex_input_idx_map_.size());
2198 for (
auto hash : collected_window_func_hash_) {
2199 auto rex_it = window_func_to_new_rex_input_idx_map_.find(hash);
2200 CHECK(rex_it != window_func_to_new_rex_input_idx_map_.end());
2201 CHECK_LT(rex_it->second, new_rex_input_for_window_func_.size());
2203 CHECK(new_project_);
2208 if (rex_input->getSourceNode() != new_project_) {
2209 const auto cur_index = rex_input->getIndex();
2210 auto cur_source_node = rex_input->getSourceNode();
2211 std::string field_name =
"";
2212 if (
auto cur_project_node = dynamic_cast<const RelProject*>(cur_source_node)) {
2213 field_name = cur_project_node->getFieldName(cur_index);
2215 auto rex_input_hash = rex_input->toHash();
2216 auto rex_input_it = new_rex_input_from_child_node_.find(rex_input_hash);
2217 if (rex_input_it == new_rex_input_from_child_node_.end()) {
2218 auto new_rex_input =
2219 std::make_unique<RexInput>(new_project_, new_project_->size());
2220 new_project_->appendInput(field_name, rex_input->deepCopy());
2221 new_rex_input_from_child_node_.emplace(rex_input_hash, new_rex_input->deepCopy());
2222 return new_rex_input;
2224 return rex_input_it->second->deepCopy();
2227 return rex_input->deepCopy();
2232 auto new_rex_idx = is_collected_window_function(rex_operator->toHash());
2234 return get_new_rex_input(*new_rex_idx);
2237 const auto rex_window_function_operator =
2239 if (rex_window_function_operator) {
2241 return visitWindowFunctionOperator(rex_window_function_operator);
2244 const size_t operand_count = rex_operator->size();
2245 std::vector<RetType> new_opnds;
2246 for (
size_t i = 0; i < operand_count; ++i) {
2247 const auto operand = rex_operator->getOperand(i);
2248 auto new_rex_idx_for_operand = is_collected_window_function(operand->toHash());
2249 if (new_rex_idx_for_operand) {
2250 new_opnds.push_back(get_new_rex_input(*new_rex_idx_for_operand));
2252 new_opnds.emplace_back(visit(rex_operator->getOperand(i)));
2255 return rex_operator->getDisambiguated(new_opnds);
2259 auto new_rex_idx = is_collected_window_function(rex_case->
toHash());
2261 return get_new_rex_input(*new_rex_idx);
2264 std::vector<std::pair<RetType, RetType>> new_pair_list;
2265 for (
size_t i = 0; i < rex_case->
branchCount(); ++i) {
2266 auto when_operand = rex_case->
getWhen(i);
2267 auto new_rex_idx_for_when_operand =
2268 is_collected_window_function(when_operand->toHash());
2270 auto then_operand = rex_case->
getThen(i);
2271 auto new_rex_idx_for_then_operand =
2272 is_collected_window_function(then_operand->toHash());
2274 new_pair_list.emplace_back(
2275 new_rex_idx_for_when_operand ? get_new_rex_input(*new_rex_idx_for_when_operand)
2276 : visit(when_operand),
2277 new_rex_idx_for_then_operand ? get_new_rex_input(*new_rex_idx_for_then_operand)
2278 : visit(then_operand));
2280 auto new_rex_idx_for_else_operand =
2281 is_collected_window_function(rex_case->
getElse()->
toHash());
2282 auto new_else = new_rex_idx_for_else_operand
2283 ? get_new_rex_input(*new_rex_idx_for_else_operand)
2285 return std::make_unique<RexCase>(new_pair_list, new_else);
2290 auto rex_it = window_func_to_new_rex_input_idx_map_.find(rex_hash);
2291 if (rex_it != window_func_to_new_rex_input_idx_map_.end()) {
2292 return rex_it->second;
2294 return std::nullopt;
2299 CHECK_LT(rex_idx, new_rex_input_for_window_func_.size());
2300 auto& new_rex_input = new_rex_input_for_window_func_.at(rex_idx);
2301 CHECK(new_rex_input);
2302 auto copied_rex_input = copier_.visit(new_rex_input.get());
2303 return copied_rex_input;
2312 std::unordered_map<size_t, std::unique_ptr<const RexInput>>&
2318 std::shared_ptr<RelProject> prev_node,
2319 std::shared_ptr<RelProject> new_node,
2320 std::unordered_map<
size_t, std::unordered_map<unsigned, RegisteredQueryHint>>&
2322 auto delivered_hints = prev_node->getDeliveredHints();
2323 bool needs_propagate_hints = !delivered_hints->empty();
2324 if (needs_propagate_hints) {
2325 for (
auto& kv : *delivered_hints) {
2326 new_node->addHint(kv.second);
2328 auto prev_it = query_hints.find(prev_node->toHash());
2330 CHECK(prev_it != query_hints.end());
2331 auto prev_hint_it = prev_it->second.find(prev_node->getId());
2332 CHECK(prev_hint_it != prev_it->second.end());
2333 std::unordered_map<unsigned, RegisteredQueryHint> hint_map;
2334 hint_map.emplace(new_node->getId(), prev_hint_it->second);
2335 query_hints.emplace(new_node->toHash(), hint_map);
2362 std::vector<std::shared_ptr<RelAlgNode>>& nodes,
2363 std::unordered_map<
size_t, std::unordered_map<unsigned, RegisteredQueryHint>>&
2365 std::list<std::shared_ptr<RelAlgNode>> node_list(nodes.begin(), nodes.end());
2366 for (
auto node_itr = node_list.begin(); node_itr != node_list.end(); ++node_itr) {
2367 const auto node = *node_itr;
2368 auto window_func_project_node = std::dynamic_pointer_cast<
RelProject>(node);
2369 if (!window_func_project_node) {
2373 const auto prev_node_itr = std::prev(node_itr);
2374 const auto prev_node = *prev_node_itr;
2378 std::unordered_map<size_t, const RexScalar*> collected_window_func;
2382 for (
size_t i = 0; i < window_func_project_node->size(); i++) {
2383 const auto scalar_rex = window_func_project_node->getProjectAt(i);
2388 collector.visit(scalar_rex);
2391 if (!collected_window_func.empty()) {
2393 std::unordered_set<size_t> collected_window_func_hash;
2396 std::vector<std::unique_ptr<const RexScalar>> new_rex_input_for_window_func;
2398 std::vector<std::unique_ptr<const RexScalar>> new_scalar_expr_for_window_project;
2401 std::unordered_map<size_t, size_t> window_func_to_new_rex_input_idx_map;
2405 std::unordered_map<size_t, std::unique_ptr<const RexInput>>
2406 new_rex_input_from_child_node;
2409 std::vector<std::unique_ptr<const RexScalar>> dummy_scalar_exprs;
2410 std::vector<std::string> dummy_fields;
2411 std::vector<std::string> new_project_field_names;
2414 std::make_shared<RelProject>(dummy_scalar_exprs, dummy_fields, prev_node);
2417 node_list.insert(node_itr, new_project);
2421 std::for_each(collected_window_func.begin(),
2422 collected_window_func.end(),
2423 [&new_project_field_names,
2424 &collected_window_func_hash,
2425 &new_rex_input_for_window_func,
2426 &new_scalar_expr_for_window_project,
2429 &window_func_to_new_rex_input_idx_map](
const auto& kv) {
2432 collected_window_func_hash.insert(kv.first);
2436 const auto rex_idx = new_rex_input_for_window_func.size();
2437 window_func_to_new_rex_input_idx_map.emplace(kv.first, rex_idx);
2441 new_rex_input_for_window_func.emplace_back(
2442 std::make_unique<const RexInput>(new_project.get(), rex_idx));
2443 new_scalar_expr_for_window_project.push_back(
2444 std::move(copier.visit(kv.second)));
2445 new_project_field_names.emplace_back(
"");
2447 new_project->setExpressions(new_scalar_expr_for_window_project);
2448 new_project->setFields(std::move(new_project_field_names));
2450 auto window_func_scalar_exprs =
2451 window_func_project_node->getExpressionsAndRelease();
2453 new_rex_input_for_window_func,
2454 window_func_to_new_rex_input_idx_map,
2456 new_rex_input_from_child_node);
2458 for (
auto& scalar_expr : window_func_scalar_exprs) {
2461 auto new_parent_rex = replacer.
visit(scalar_expr.get());
2462 window_func_scalar_exprs[rex_idx] = std::move(new_parent_rex);
2466 window_func_project_node->setExpressions(window_func_scalar_exprs);
2467 window_func_project_node->replaceInput(prev_node, new_project);
2469 new_project->setPushedDownWindowExpr();
2472 nodes.assign(node_list.begin(), node_list.end());
2487 result.insert(next_result.begin(), next_result.end());
2494 bool& has_generic_expr_in_window_func) {
2496 auto partition_input =
dynamic_cast<RexInput const*
>(partition_key.get());
2497 if (!partition_input) {
2501 for (
auto const& order_key : window_expr->
getOrderKeys()) {
2502 auto order_input =
dynamic_cast<RexInput const*
>(order_key.get());
2507 for (
size_t k = 0; k < window_expr->
size(); k++) {
2508 if (!shared::dynamic_castable_to_any<RexInput, RexLiteral>(
2510 has_generic_expr_in_window_func =
true;
2518 RelProject const* window_func_project_node) {
2519 bool has_generic_expr_in_window_func =
false;
2521 for (
size_t i = 0; i < window_func_project_node->
size(); ++i) {
2522 auto const projected_target = window_func_project_node->
getProjectAt(i);
2523 if (
auto const* window_expr =
2524 dynamic_cast<RexWindowFunctionOperator const*>(projected_target)) {
2527 }
else if (
auto const* case_expr = dynamic_cast<RexCase const*>(projected_target)) {
2528 std::unordered_map<size_t, const RexScalar*> collected_window_func;
2530 collector.visit(case_expr);
2531 for (
auto const& kv : collected_window_func) {
2532 auto const* candidate_window_expr =
2534 CHECK(candidate_window_expr);
2536 has_generic_expr_in_window_func);
2540 return std::make_pair(has_generic_expr_in_window_func, res);
2556 std::vector<std::shared_ptr<RelAlgNode>>& nodes,
2557 const bool always_add_project_if_first_project_is_window_expr,
2558 std::unordered_map<
size_t, std::unordered_map<unsigned, RegisteredQueryHint>>&
2560 std::list<std::shared_ptr<RelAlgNode>> node_list(nodes.begin(), nodes.end());
2561 size_t project_node_counter{0};
2562 for (
auto node_itr = node_list.begin(); node_itr != node_list.end(); ++node_itr) {
2563 const auto node = *node_itr;
2565 auto window_func_project_node = std::dynamic_pointer_cast<
RelProject>(node);
2566 if (!window_func_project_node) {
2569 project_node_counter++;
2570 if (!window_func_project_node->hasWindowFunctionExpr()) {
2576 const auto prev_node_itr = std::prev(node_itr);
2577 const auto prev_node = *prev_node_itr;
2580 auto filter_node = std::dynamic_pointer_cast<
RelFilter>(prev_node);
2581 auto join_node = std::dynamic_pointer_cast<
RelJoin>(prev_node);
2583 auto scan_node = std::dynamic_pointer_cast<
RelScan>(prev_node);
2584 const bool has_multi_fragment_scan_input =
2586 (scan_node->getNumShards() > 0 || scan_node->getNumFragments() > 1));
2587 auto const [has_generic_expr_in_window_func, needs_expr_pushdown] =
2613 if (!((always_add_project_if_first_project_is_window_expr &&
2614 project_node_counter == 1) ||
2615 filter_node || join_node || has_multi_fragment_scan_input ||
2616 needs_expr_pushdown)) {
2620 if (needs_expr_pushdown || join_node) {
2624 std::unordered_map<size_t, size_t> expr_offset_cache;
2625 std::vector<std::unique_ptr<const RexScalar>> scalar_exprs_for_new_project;
2626 std::vector<std::unique_ptr<const RexScalar>> scalar_exprs_for_window_project;
2627 std::vector<std::string> fields_for_window_project;
2628 std::vector<std::string> fields_for_new_project;
2631 std::vector<std::unique_ptr<const RexScalar>> dummy_scalar_exprs;
2632 std::vector<std::string> dummy_fields;
2634 std::make_shared<RelProject>(dummy_scalar_exprs, dummy_fields, prev_node);
2638 scalar_exprs_for_new_project,
2639 fields_for_new_project,
2641 for (
size_t i = 0; i < window_func_project_node->size(); ++i) {
2642 auto projected_target = window_func_project_node->getProjectAt(i);
2643 auto new_projection_target = visitor.visit(projected_target);
2644 scalar_exprs_for_window_project.emplace_back(
2645 std::move(new_projection_target.release()));
2647 new_project->setExpressions(scalar_exprs_for_new_project);
2648 new_project->setFields(std::move(fields_for_new_project));
2649 bool has_groupby =
false;
2650 auto aggregate = std::dynamic_pointer_cast<
RelAggregate>(prev_node);
2655 if (has_groupby && visitor.hasPartitionExpression()) {
2661 <<
"Query output overridden to row-wise format due to presence of a window "
2662 "function with partition expression and group-by expression.";
2663 new_project->forceRowwiseOutput();
2664 }
else if (has_generic_expr_in_window_func) {
2665 VLOG(1) <<
"Query output overridden to row-wise format due to presence of a "
2666 "generic expression as an input expression of the window "
2668 new_project->forceRowwiseOutput();
2669 }
else if (visitor.hasCaseExprAsWindowOperand()) {
2671 <<
"Query output overridden to row-wise format due to presence of a window "
2672 "function with a case statement as its operand.";
2673 new_project->forceRowwiseOutput();
2678 new_project->setPushedDownWindowExpr();
2679 node_list.insert(node_itr, new_project);
2680 window_func_project_node->replaceInput(prev_node, new_project);
2681 window_func_project_node->setExpressions(scalar_exprs_for_window_project);
2686 for (
size_t i = 0; i < window_func_project_node->size(); i++) {
2688 input_collector.visit(window_func_project_node->getProjectAt(i));
2689 inputs.insert(new_inputs.begin(), new_inputs.end());
2694 std::vector<RexInput> sorted_inputs(inputs.begin(), inputs.end());
2696 sorted_inputs.end(),
2697 [](
const auto&
a,
const auto& b) {
return a.getIndex() < b.getIndex(); });
2699 std::vector<std::unique_ptr<const RexScalar>> scalar_exprs;
2700 std::vector<std::string> fields;
2701 std::unordered_map<unsigned, unsigned> old_index_to_new_index;
2702 for (
auto& input : sorted_inputs) {
2703 CHECK_EQ(input.getSourceNode(), prev_node.get());
2704 CHECK(old_index_to_new_index
2705 .insert(std::make_pair(input.getIndex(), scalar_exprs.size()))
2707 scalar_exprs.emplace_back(input.deepCopy());
2708 fields.emplace_back(
"");
2711 auto new_project = std::make_shared<RelProject>(scalar_exprs, fields, prev_node);
2713 new_project->setPushedDownWindowExpr();
2714 node_list.insert(node_itr, new_project);
2715 window_func_project_node->replaceInput(
2716 prev_node, new_project, old_index_to_new_index);
2719 nodes.assign(node_list.begin(), node_list.end());
2724 const int64_t default_val) noexcept {
2725 const auto it = obj.FindMember(
field);
2726 if (it == obj.MemberEnd()) {
2731 CHECK_EQ(
unsigned(0), lit->getScale());
2732 CHECK_EQ(
unsigned(0), lit->getTargetScale());
2733 return lit->getVal<int64_t>();
2737 const auto& inputs_json =
field(node,
"inputs");
2738 CHECK(inputs_json.IsArray() && !inputs_json.Size());
2741 const std::pair<const Catalog_Namespace::Catalog*, const TableDescriptor*>
2743 const auto& table_json =
field(scan_ra,
"table");
2744 CHECK(table_json.IsArray());
2745 CHECK_EQ(
unsigned(2), table_json.Size());
2749 const auto td = cat->getMetadataForTable(table_json[1].GetString());
2751 return {cat.get(), td};
2755 const auto& fields_json =
field(scan_ra,
"fieldNames");
2775 std::vector<std::shared_ptr<RelAlgNode>>
run(
const rapidjson::Value& rels,
2777 for (
auto rels_it = rels.Begin(); rels_it != rels.End(); ++rels_it) {
2778 const auto& crt_node = *rels_it;
2779 const auto id =
node_id(crt_node);
2781 CHECK(crt_node.IsObject());
2782 std::shared_ptr<RelAlgNode> ra_node =
nullptr;
2784 if (rel_op == std::string(
"EnumerableTableScan") ||
2785 rel_op == std::string(
"LogicalTableScan")) {
2787 }
else if (rel_op == std::string(
"LogicalProject")) {
2789 }
else if (rel_op == std::string(
"LogicalFilter")) {
2791 }
else if (rel_op == std::string(
"LogicalAggregate")) {
2793 }
else if (rel_op == std::string(
"LogicalJoin")) {
2795 }
else if (rel_op == std::string(
"LogicalSort")) {
2797 }
else if (rel_op == std::string(
"LogicalValues")) {
2799 }
else if (rel_op == std::string(
"LogicalTableModify")) {
2801 }
else if (rel_op == std::string(
"LogicalTableFunctionScan")) {
2803 }
else if (rel_op == std::string(
"LogicalUnion")) {
2808 nodes_.push_back(ra_node);
2811 return std::move(
nodes_);
2817 CHECK(scan_ra.IsObject());
2820 if (scan_ra.HasMember(
"hints")) {
2821 auto scan_node = std::make_shared<RelScan>(td, field_names, *
cat);
2825 return std::make_shared<RelScan>(td, field_names, *
cat);
2831 CHECK_EQ(
size_t(1), inputs.size());
2832 const auto& exprs_json =
field(proj_ra,
"exprs");
2833 CHECK(exprs_json.IsArray());
2834 std::vector<std::unique_ptr<const RexScalar>> exprs;
2835 for (
auto exprs_json_it = exprs_json.Begin(); exprs_json_it != exprs_json.End();
2839 const auto& fields =
field(proj_ra,
"fields");
2840 if (proj_ra.HasMember(
"hints")) {
2841 auto project_node = std::make_shared<RelProject>(
2844 return project_node;
2846 return std::make_shared<RelProject>(
2853 CHECK_EQ(
size_t(1), inputs.size());
2854 const auto id =
node_id(filter_ra);
2857 return std::make_shared<RelFilter>(condition, inputs.front());
2862 CHECK_EQ(
size_t(1), inputs.size());
2865 for (
size_t i = 0; i < group.size(); ++i) {
2868 if (agg_ra.HasMember(
"groups") || agg_ra.HasMember(
"indicator")) {
2871 const auto& aggs_json_arr =
field(agg_ra,
"aggs");
2872 CHECK(aggs_json_arr.IsArray());
2873 std::vector<std::unique_ptr<const RexAgg>> aggs;
2874 for (
auto aggs_json_arr_it = aggs_json_arr.Begin();
2875 aggs_json_arr_it != aggs_json_arr.End();
2876 ++aggs_json_arr_it) {
2879 if (agg_ra.HasMember(
"hints")) {
2881 std::make_shared<RelAggregate>(group.size(), aggs, fields, inputs.front());
2885 return std::make_shared<RelAggregate>(group.size(), aggs, fields, inputs.front());
2891 CHECK_EQ(
size_t(2), inputs.size());
2894 if (join_ra.HasMember(
"hints")) {
2896 std::make_shared<RelJoin>(inputs[0], inputs[1], filter_rex, join_type);
2900 return std::make_shared<RelJoin>(inputs[0], inputs[1], filter_rex, join_type);
2905 CHECK_EQ(
size_t(1), inputs.size());
2906 std::vector<SortField> collation;
2907 const auto& collation_arr =
field(sort_ra,
"collation");
2908 CHECK(collation_arr.IsArray());
2909 for (
auto collation_arr_it = collation_arr.Begin();
2910 collation_arr_it != collation_arr.End();
2911 ++collation_arr_it) {
2912 const size_t field_idx =
json_i64(
field(*collation_arr_it,
"field"));
2915 collation.emplace_back(field_idx, sort_dir, null_pos);
2919 auto ret = std::make_shared<RelSort>(
2920 collation, limit > 0 ? limit : 0, offset, inputs.front(), limit > 0);
2921 ret->setEmptyResult(limit == 0);
2925 std::shared_ptr<RelModify>
dispatchModify(
const rapidjson::Value& logical_modify_ra) {
2927 CHECK_EQ(
size_t(1), inputs.size());
2929 const auto [
cat, table_descriptor] =
2931 if (table_descriptor->isView) {
2932 throw std::runtime_error(
"UPDATE of a view is unsupported.");
2935 bool flattened =
json_bool(
field(logical_modify_ra,
"flattened"));
2936 std::string op =
json_str(
field(logical_modify_ra,
"operation"));
2939 if (op ==
"UPDATE") {
2940 const auto& update_columns =
field(logical_modify_ra,
"updateColumnList");
2941 CHECK(update_columns.IsArray());
2943 for (
auto column_arr_it = update_columns.Begin();
2944 column_arr_it != update_columns.End();
2946 target_column_list.push_back(column_arr_it->GetString());
2950 auto modify_node = std::make_shared<RelModify>(
2951 *
cat, table_descriptor, flattened, op, target_column_list, inputs[0]);
2952 switch (modify_node->getOperation()) {
2954 modify_node->applyDeleteModificationsToInputNode();
2958 modify_node->applyUpdateModificationsToInputNode();
2962 throw std::runtime_error(
"Unsupported RelModify operation: " +
2970 const rapidjson::Value& table_func_ra,
2973 const auto& invocation =
field(table_func_ra,
"invocation");
2974 CHECK(invocation.IsObject());
2976 const auto& operands =
field(invocation,
"operands");
2977 CHECK(operands.IsArray());
2978 CHECK_GE(operands.Size(), unsigned(0));
2980 std::vector<const Rex*> col_inputs;
2981 std::vector<std::unique_ptr<const RexScalar>> table_func_inputs;
2982 std::vector<std::string> fields;
2984 for (
auto exprs_json_it = operands.Begin(); exprs_json_it != operands.End();
2986 const auto& expr_json = *exprs_json_it;
2987 CHECK(expr_json.IsObject());
2988 if (expr_json.HasMember(
"op")) {
2990 if (op_str ==
"CAST" && expr_json.HasMember(
"type")) {
2991 const auto& expr_type =
field(expr_json,
"type");
2992 CHECK(expr_type.IsObject());
2993 CHECK(expr_type.HasMember(
"type"));
2994 const auto& expr_type_name =
json_str(
field(expr_type,
"type"));
2995 if (expr_type_name ==
"CURSOR") {
2996 CHECK(expr_json.HasMember(
"operands"));
2997 const auto& expr_operands =
field(expr_json,
"operands");
2998 CHECK(expr_operands.IsArray());
2999 if (expr_operands.Size() != 1) {
3000 throw std::runtime_error(
3001 "Table functions currently only support one ResultSet input");
3003 auto pos =
field(expr_operands[0],
"input").GetInt();
3005 for (
size_t i = inputs[pos]->size(); i > 0; i--) {
3006 table_func_inputs.emplace_back(
3007 std::make_unique<RexAbstractInput>(col_inputs.size()));
3008 col_inputs.emplace_back(table_func_inputs.back().get());
3017 const auto& op_name =
field(invocation,
"op");
3018 CHECK(op_name.IsString());
3020 std::vector<std::unique_ptr<const RexScalar>> table_function_projected_outputs;
3021 const auto& row_types =
field(table_func_ra,
"rowType");
3022 CHECK(row_types.IsArray());
3023 CHECK_GE(row_types.Size(), unsigned(0));
3024 const auto& row_types_array = row_types.GetArray();
3025 for (
size_t i = 0; i < row_types_array.Size(); i++) {
3028 table_function_projected_outputs.emplace_back(std::make_unique<RexRef>(i));
3029 fields.emplace_back(
"");
3031 return std::make_shared<RelTableFunction>(op_name.GetString(),
3036 table_function_projected_outputs);
3040 const rapidjson::Value& logical_values_ra) {
3041 const auto& tuple_type_arr =
field(logical_values_ra,
"type");
3042 CHECK(tuple_type_arr.IsArray());
3043 std::vector<TargetMetaInfo> tuple_type;
3044 for (
auto tuple_type_arr_it = tuple_type_arr.Begin();
3045 tuple_type_arr_it != tuple_type_arr.End();
3046 ++tuple_type_arr_it) {
3047 auto component_type =
parse_type(*tuple_type_arr_it);
3048 const auto component_name =
json_str(
field(*tuple_type_arr_it,
"name"));
3049 if (component_type.is_none_encoded_string()) {
3053 component_type.set_size(4);
3055 tuple_type.emplace_back(component_name, component_type);
3057 const auto& inputs_arr =
field(logical_values_ra,
"inputs");
3058 CHECK(inputs_arr.IsArray());
3059 const auto& tuples_arr =
field(logical_values_ra,
"tuples");
3060 CHECK(tuples_arr.IsArray());
3062 if (inputs_arr.Size()) {
3066 std::vector<RelLogicalValues::RowValues> values;
3067 if (tuples_arr.Size()) {
3068 for (
const auto& row : tuples_arr.GetArray()) {
3069 CHECK(row.IsArray());
3070 const auto values_json = row.GetArray();
3071 if (!values.empty()) {
3072 CHECK_EQ(values[0].size(), values_json.Size());
3075 for (
const auto& value : values_json) {
3076 CHECK(value.IsObject());
3077 CHECK(value.HasMember(
"literal"));
3083 return std::make_shared<RelLogicalValues>(tuple_type, values);
3087 const rapidjson::Value& logical_union_ra) {
3089 auto const& all_type_bool =
field(logical_union_ra,
"all");
3090 CHECK(all_type_bool.IsBool());
3091 return std::make_shared<RelLogicalUnion>(std::move(inputs), all_type_bool.GetBool());
3095 if (node.HasMember(
"inputs")) {
3098 for (
const auto& str_id : str_input_ids) {
3099 ra_inputs.push_back(
nodes_[std::stoi(str_id)]);
3103 return {
prev(node)};
3107 auto option = str.substr(0, pos);
3108 std::string delim =
"=";
3109 size_t delim_pos = option.find(delim);
3110 auto key = option.substr(0, delim_pos);
3111 auto val = option.substr(delim_pos + 1, option.length());
3112 str.erase(0, pos + delim.length() + 1);
3117 std::string white_space_delim =
" ";
3118 int l = hint_string.length();
3119 hint_string = hint_string.erase(0, 1).substr(0, l - 2);
3121 auto global_hint_checker = [&](
const std::string& input_hint_name) ->
HintIdentifier {
3122 bool global_hint =
false;
3123 std::string hint_name = input_hint_name;
3124 auto global_hint_identifier = hint_name.substr(0, 2);
3125 if (global_hint_identifier.compare(
"g_") == 0) {
3127 hint_name = hint_name.substr(2, hint_string.length());
3129 return {global_hint, hint_name};
3132 global_hint_checker(hint_string.substr(0, hint_string.find(white_space_delim)));
3134 if ((pos = hint_string.find(
"options:")) != std::string::npos) {
3136 std::vector<std::string> tokens;
3137 bool kv_list_op =
false;
3138 std::string raw_options = hint_string.substr(pos + 8, hint_string.length() - 2);
3139 if (raw_options.find(
'{') != std::string::npos) {
3142 CHECK(raw_options.find(
'[') != std::string::npos);
3144 auto t1 = raw_options.erase(0, 1);
3145 raw_options = t1.substr(0, t1.length() - 1);
3146 std::string op_delim =
", ";
3149 std::unordered_map<std::string, std::string> kv_options;
3150 while ((pos = raw_options.find(op_delim)) != std::string::npos) {
3152 kv_options.emplace(kv_pair.first, kv_pair.second);
3156 kv_options.emplace(kv_pair.first, kv_pair.second);
3157 return {hint_type, parsed_hint.global_hint,
false,
true, kv_options};
3159 std::vector<std::string> list_options;
3160 while ((pos = raw_options.find(op_delim)) != std::string::npos) {
3161 list_options.emplace_back(raw_options.substr(0, pos));
3162 raw_options.erase(0, pos + white_space_delim.length() + 1);
3165 list_options.emplace_back(raw_options.substr(0, pos));
3166 return {hint_type, parsed_hint.global_hint,
false,
false, list_options};
3170 return {hint_type, parsed_hint.global_hint,
true,
false};
3175 std::shared_ptr<RelAlgNode> node) {
3176 std::string hint_explained =
json_str(
field(json_node,
"hints"));
3178 std::string delim =
"|";
3179 std::vector<std::string> hint_list;
3180 while ((pos = hint_explained.find(delim)) != std::string::npos) {
3181 hint_list.emplace_back(hint_explained.substr(0, pos));
3182 hint_explained.erase(0, pos + delim.length());
3185 hint_list.emplace_back(hint_explained.substr(0, pos));
3187 const auto agg_node = std::dynamic_pointer_cast<
RelAggregate>(node);
3189 for (std::string& hint : hint_list) {
3191 agg_node->addHint(parsed_hint);
3194 const auto project_node = std::dynamic_pointer_cast<
RelProject>(node);
3196 for (std::string& hint : hint_list) {
3198 project_node->addHint(parsed_hint);
3201 const auto scan_node = std::dynamic_pointer_cast<
RelScan>(node);
3203 for (std::string& hint : hint_list) {
3205 scan_node->addHint(parsed_hint);
3208 const auto join_node = std::dynamic_pointer_cast<
RelJoin>(node);
3210 for (std::string& hint : hint_list) {
3212 join_node->addHint(parsed_hint);
3216 const auto compound_node = std::dynamic_pointer_cast<
RelCompound>(node);
3217 if (compound_node) {
3218 for (std::string& hint : hint_list) {
3220 compound_node->addHint(parsed_hint);
3225 std::shared_ptr<const RelAlgNode>
prev(
const rapidjson::Value& crt_node) {
3226 const auto id =
node_id(crt_node);
3232 std::vector<std::shared_ptr<RelAlgNode>>
nodes_;
3238 const bool optimize_dag) {
3239 rapidjson::Document query_ast;
3240 query_ast.Parse(query_ra.c_str());
3241 VLOG(2) <<
"Parsing query RA JSON: " << query_ra;
3242 if (query_ast.HasParseError()) {
3243 query_ast.GetParseError();
3244 LOG(
ERROR) <<
"Failed to parse RA tree from Calcite (offset "
3245 << query_ast.GetErrorOffset() <<
"):\n"
3246 << rapidjson::GetParseError_En(query_ast.GetParseError());
3247 VLOG(1) <<
"Failed to parse query RA: " << query_ra;
3248 throw std::runtime_error(
3249 "Failed to parse relational algebra tree. Possible query syntax error.");
3251 CHECK(query_ast.IsObject());
3254 return build(query_ast,
nullptr, optimize_dag);
3259 const rapidjson::Value& query_ast) {
3260 return build(query_ast, &root_dag,
true);
3265 const bool optimize_dag) {
3266 const auto& rels =
field(query_ast,
"rels");
3267 CHECK(rels.IsArray());
3269 auto rel_alg_dag_ptr = std::make_unique<RelAlgDag>();
3270 auto& rel_alg_dag = *rel_alg_dag_ptr;
3271 auto& nodes =
getNodes(rel_alg_dag);
3278 CHECK(!nodes.empty());
3287 return rel_alg_dag_ptr;
3297 <<
static_cast<int>(build_state);
3299 auto& nodes =
getNodes(rel_alg_dag);
3310 std::vector<const RelAlgNode*> filtered_left_deep_joins;
3311 std::vector<const RelAlgNode*> left_deep_joins;
3312 for (
const auto& node : nodes) {
3316 if (left_deep_join_root) {
3317 left_deep_joins.push_back(left_deep_join_root.get());
3318 if (std::dynamic_pointer_cast<const RelFilter>(left_deep_join_root)) {
3319 filtered_left_deep_joins.push_back(left_deep_join_root.get());
3323 if (filtered_left_deep_joins.empty()) {
3334 CHECK(nodes.back().use_count() == 1);
3341 for (
auto const& node :
nodes_) {
3343 callback(node.get());
3349 for (
auto& node :
nodes_) {
3351 node->resetQueryExecutionState();
3359 for (
size_t i = 0; i < ra->
inputCount(); ++i) {
3366 return cat(::
typeName(
this),
"(", ra_->toString(config),
")");
3371 hash_ =
typeid(RexSubQuery).hash_code();
3372 boost::hash_combine(*hash_, ra_->toHash());
3378 const auto scan_node =
dynamic_cast<const RelScan*
>(
node_);
3381 auto table_name = scan_node->getTableDescriptor()->tableName;
3407 ret += expr->toString(config) +
" ";
3409 ret +=
", agg_exps=";
3411 ret += expr->toString(config) +
" ";
3413 ret +=
", scalar_sources=";
3415 ret += expr->toString(config) +
" ";
3433 if (
auto rex_scalar = dynamic_cast<const RexScalar*>(target_expr)) {
3434 boost::hash_combine(*
hash_, rex_scalar->toHash());
3438 boost::hash_combine(*
hash_, agg_expr->toHash());
3441 boost::hash_combine(*
hash_, scalar_source->toHash());
std::vector< std::shared_ptr< const RexScalar > > scalar_exprs_
DEVICE auto upper_bound(ARGS &&...args)
const size_t getGroupByCount() const
SQLTypes to_sql_type(const std::string &type_name)
void setGlobalQueryHints(const RegisteredQueryHint &global_hints)
std::optional< size_t > is_collected_window_function(size_t rex_hash) const
NullSortedPosition parse_nulls_position(const rapidjson::Value &collation)
bool is_agg(const Analyzer::Expr *expr)
std::unique_ptr< const RexScalar > condition_
std::unique_ptr< const RexOperator > disambiguate_operator(const RexOperator *rex_operator, const RANodeOutput &ra_output) noexcept
std::vector< ElementType > Container
RelCompound(const TableDescriptor *td, const Catalog_Namespace::Catalog *catalog)
const RexScalar * getThen(const size_t idx) const
std::shared_ptr< RelAggregate > dispatchAggregate(const rapidjson::Value &agg_ra)
std::shared_ptr< RelFilter > dispatchFilter(const rapidjson::Value &filter_ra, RelAlgDag &root_dag)
std::shared_ptr< RelAlgNode > ElementType
void set_notnulls(std::vector< TargetMetaInfo > *tmis0, std::vector< bool > const ¬nulls)
void mark_nops(const std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
std::unique_ptr< RexSubQuery > deepCopy() const
void replaceInput(std::shared_ptr< const RelAlgNode > old_input, std::shared_ptr< const RelAlgNode > input) override
static std::unordered_map< size_t, std::unordered_map< unsigned, RegisteredQueryHint > > & getQueryHints(RelAlgDag &rel_alg_dag)
std::vector< std::unique_ptr< const RexScalar > > table_func_inputs_
std::optional< size_t > getOffsetForPushedDownExpr(WindowExprType type, size_t expr_offset) const
RexWindowFuncReplacementVisitor(std::unordered_set< size_t > &collected_window_func_hash, std::vector< std::unique_ptr< const RexScalar >> &new_rex_input_for_window_func, std::unordered_map< size_t, size_t > &window_func_to_new_rex_input_idx_map, RelProject *new_project, std::unordered_map< size_t, std::unique_ptr< const RexInput >> &new_rex_input_from_child_node)
std::vector< std::unique_ptr< const RexScalar > > parse_window_order_exprs(const rapidjson::Value &arr, RelAlgDag &root_dag)
void hoist_filter_cond_to_cross_join(std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
std::vector< bool > get_notnulls(std::vector< TargetMetaInfo > const &tmis0)
std::vector< std::unique_ptr< const RexScalar > > & scalar_exprs_for_new_project_
size_t size() const override
void addHint(const ExplainedQueryHint &hint_explained)
std::shared_ptr< const RelAlgNode > get_left_deep_join_root(const std::shared_ptr< RelAlgNode > &node)
const ElementType & operator*()
void sink_projected_boolean_expr_to_join(std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
bool input_can_be_coalesced(const RelAlgNode *parent_node, const size_t index, const bool first_rex_is_input)
bool is_window_function_avg(const RexScalar *rex)
std::string toString(RelRexToStringConfig config=RelRexToStringConfig::defaults()) const override
void eliminate_identical_copy(std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
RetType visitInput(const RexInput *rex_input) const final
std::vector< RexInput > RANodeOutput
std::unique_ptr< const RexCase > disambiguate_case(const RexCase *rex_case, const RANodeOutput &ra_output)
const RexScalar * getElse() const
static thread_local unsigned crt_id_
std::unique_ptr< const RexScalar > visitOperator(const RexOperator *rex_operator) const override
SqlWindowFunctionKind parse_window_function_kind(const std::string &name)
RexDeepCopyVisitor deep_copier_
std::shared_ptr< RelScan > dispatchTableScan(const rapidjson::Value &scan_ra)
RelProject(const TableDescriptor *td, const Catalog_Namespace::Catalog *catalog)
std::pair< std::shared_ptr< RelLeftDeepInnerJoin >, std::shared_ptr< const RelAlgNode > > create_left_deep_join(const std::shared_ptr< RelAlgNode > &left_deep_join_root)
RexScalar const * copyAndRedirectSource(RexScalar const *, size_t input_idx) const
const RelAlgNode * node_to_keep_
void replaceInput(std::shared_ptr< const RelAlgNode > old_input, std::shared_ptr< const RelAlgNode > input) override
SQLAgg to_agg_kind(const std::string &agg_name)
std::shared_ptr< RelLogicalUnion > dispatchUnion(const rapidjson::Value &logical_union_ra)
std::vector< std::string > TargetColumnList
void advance(AdvancingMode mode)
std::unique_ptr< RexCase > parse_case(const rapidjson::Value &expr, RelAlgDag &root_dag)
const SQLTypeInfo & getType() const
std::unique_ptr< const RexScalar > get_new_rex_input(size_t rex_idx) const
Hints * getDeliveredHints()
std::shared_ptr< RelProject > dispatchProject(const rapidjson::Value &proj_ra, RelAlgDag &root_dag)
const bool json_bool(const rapidjson::Value &obj) noexcept
const RexScalar * getOperand(const size_t idx) const
std::vector< const Rex * > col_inputs_
std::string json_node_to_string(const rapidjson::Value &node) noexcept
bool hasEquivCollationOf(const RelSort &that) const
JoinType to_join_type(const std::string &join_type_name)
void resetQueryExecutionState()
std::pair< bool, bool > need_pushdown_generic_expr(RelProject const *window_func_project_node)
const std::string json_str(const rapidjson::Value &obj) noexcept
std::vector< std::shared_ptr< RelAlgNode > > nodes_
std::unique_ptr< const RexSubQuery > parse_subquery(const rapidjson::Value &expr, RelAlgDag &root_dag)
void handle_query_hint(const std::vector< std::shared_ptr< RelAlgNode >> &nodes, RelAlgDag &rel_alg_dag) noexcept
DEVICE void sort(ARGS &&...args)
std::unordered_map< size_t, size_t > & expr_offset_cache_
std::optional< size_t > getIdInPlanTree() const
void registerQueryHint(const RelAlgNode *node, const RegisteredQueryHint &query_hint)
std::vector< std::string > fields_
const std::pair< const Catalog_Namespace::Catalog *, const TableDescriptor * > getCatalogAndTableFromScanNode(const rapidjson::Value &scan_ra)
void pushDownExpressionInWindowFunction(const RexWindowFunctionOperator *window_expr) const
void addHint(const ExplainedQueryHint &hint_explained)
std::unique_ptr< const RexScalar > visitCase(const RexCase *rex_case) const override
const RexScalar * getWhen(const size_t idx) const
std::vector< size_t > indices_from_json_array(const rapidjson::Value &json_idx_arr) noexcept
const RegisteredQueryHint & getGlobalHints() const
#define TRANSIENT_DICT_DB_ID
void appendInput(std::string new_field_name, std::unique_ptr< const RexScalar > new_input)
void propagate_hints_to_new_project(std::shared_ptr< RelProject > prev_node, std::shared_ptr< RelProject > new_node, std::unordered_map< size_t, std::unordered_map< unsigned, RegisteredQueryHint >> &query_hints)
bool isRenamedInput(const RelAlgNode *node, const size_t index, const std::string &new_name)
RexDeepCopyVisitor copier_
size_t pushDownExpressionImpl(const RexScalar *expr) const
std::unique_ptr< const RexScalar > defaultResult() const override
void addHint(const ExplainedQueryHint &hint_explained)
std::unique_ptr< const RexAgg > parse_aggregate_expr(const rapidjson::Value &expr)
std::unordered_map< size_t, const RexScalar * > & collected_window_func_
#define TRANSIENT_DICT_ID
std::vector< std::unique_ptr< const RexScalar > > scalar_sources_
std::unique_ptr< RexAbstractInput > parse_abstract_input(const rapidjson::Value &expr) noexcept
static std::vector< std::shared_ptr< RexSubQuery > > & getSubqueries(RelAlgDag &rel_alg_dag)
std::unique_ptr< const RexScalar > disambiguate_rex(const RexScalar *, const RANodeOutput &)
std::unique_ptr< const RexScalar > visitLiteral(const RexLiteral *rex_literal) const override
const ElementType * operator->()
void add_window_function_pre_project(std::vector< std::shared_ptr< RelAlgNode >> &nodes, const bool always_add_project_if_first_project_is_window_expr, std::unordered_map< size_t, std::unordered_map< unsigned, RegisteredQueryHint >> &query_hints)
const std::string getFieldName(const size_t i) const
std::unique_ptr< const RexScalar > visitSubQuery(const RexSubQuery *rex_subquery) const override
std::unordered_map< size_t, size_t > pushed_down_window_operands_offset_
void simplify_sort(std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
std::vector< SortField > collation_
std::shared_ptr< RelJoin > dispatchJoin(const rapidjson::Value &join_ra, RelAlgDag &root_dag)
RelLogicalValues()=default
std::vector< std::unique_ptr< const RexScalar > > & new_rex_input_for_window_func_
void * defaultResult() const final
std::unordered_set< RexInput > RexInputSet
This file contains the class specification and related data structures for Catalog.
virtual T visit(const RexScalar *rex_scalar) const
bool hasCaseExprAsWindowOperand()
std::string to_string() const
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
virtual std::string toString(RelRexToStringConfig config) const =0
void separate_window_function_expressions(std::vector< std::shared_ptr< RelAlgNode >> &nodes, std::unordered_map< size_t, std::unordered_map< unsigned, RegisteredQueryHint >> &query_hints)
static SysCatalog & instance()
bool aggregateResult(const bool &aggregate, const bool &next_result) const final
SQLOps getOperator() const
void setExecutionResult(const ExecutionResultShPtr result)
std::shared_ptr< RelTableFunction > dispatchTableFunction(const rapidjson::Value &table_func_ra, RelAlgDag &root_dag)
std::unordered_map< size_t, size_t > pushed_down_order_key_offset_
std::unordered_map< size_t, std::unique_ptr< const RexInput > > & new_rex_input_from_child_node_
std::set< std::pair< const RelAlgNode *, int > > get_equiv_cols(const RelAlgNode *node, const size_t which_col)
std::vector< std::string > & fields_for_new_project_
bool visitRef(const RexRef *) const final
std::unique_ptr< RexOperator > parse_operator(const rapidjson::Value &expr, RelAlgDag &root_dag)
static std::unique_ptr< RelAlgDag > buildDagForSubquery(RelAlgDag &root_dag, const rapidjson::Value &query_ast)
std::unordered_set< size_t > & collected_window_func_hash_
std::unique_ptr< const RexScalar > visitInput(const RexInput *rex_input) const override
static QueryHint translateQueryHint(const std::string &hint_name)
DEVICE auto copy(ARGS &&...args)
Hints * getDeliveredHints()
size_t toHash() const override
void coalesce_nodes(std::vector< std::shared_ptr< RelAlgNode >> &nodes, const std::vector< const RelAlgNode * > &left_deep_joins, std::unordered_map< size_t, std::unordered_map< unsigned, RegisteredQueryHint >> &query_hints)
std::vector< std::string > fields_
SQLOps to_sql_op(const std::string &op_str)
std::unique_ptr< Hints > hints_
const int64_t json_i64(const rapidjson::Value &obj) noexcept
std::unique_ptr< Hints > hints_
std::vector< std::unique_ptr< const RexScalar > > copyRexScalars(std::vector< std::unique_ptr< const RexScalar >> const &scalar_sources)
std::vector< std::shared_ptr< const RelAlgNode >> RelAlgInputs
std::vector< std::unique_ptr< const RexScalar > > scalar_exprs_
RetType visitOperator(const RexOperator *rex_operator) const final
bool defaultResult() const final
const double json_double(const rapidjson::Value &obj) noexcept
const std::vector< std::unique_ptr< const RexScalar > > & scalar_sources_
void addHint(const ExplainedQueryHint &hint_explained)
const unsigned FIRST_RA_NODE_ID
size_t branchCount() const
const RelAlgNode * getInput(const size_t idx) const
SQLTypeInfo parse_type(const rapidjson::Value &type_obj)
Checked json field retrieval.
void * visitCase(const RexCase *rex_case) const final
std::vector< std::shared_ptr< RelAlgNode > > nodes_
std::unique_ptr< const RexScalar > filter_
std::vector< const Rex * > remapTargetPointers(std::vector< std::unique_ptr< const RexAgg >> const &agg_exprs_new, std::vector< std::unique_ptr< const RexScalar >> const &scalar_sources_new, std::vector< std::unique_ptr< const RexAgg >> const &agg_exprs_old, std::vector< std::unique_ptr< const RexScalar >> const &scalar_sources_old, std::vector< const Rex * > const &target_exprs_old)
std::optional< size_t > hash_
std::string toString(const ExecutorDeviceType &device_type)
std::vector< const Rex * > target_exprs_
void bind_inputs(const std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
std::optional< size_t > hash_
bool only_add_window_expr_
virtual void replaceInput(std::shared_ptr< const RelAlgNode > old_input, std::shared_ptr< const RelAlgNode > input)
bool find_generic_expr_in_window_func(RexWindowFunctionOperator const *window_expr, bool &has_generic_expr_in_window_func)
void bind_project_to_input(RelProject *project_node, const RANodeOutput &input) noexcept
std::vector< TargetMetaInfo > getCompatibleMetainfoTypes() const
static std::unique_ptr< RelAlgDag > buildDag(const std::string &query_ra, const bool optimize_dag)
std::string tree_string(const RelAlgNode *ra, const size_t depth)
std::unordered_set< size_t > visited_
std::vector< std::unique_ptr< const RexAgg > > agg_exprs_
std::vector< SortField > parse_window_order_collation(const rapidjson::Value &arr, RelAlgDag &root_dag)
void compute_node_hash(const std::vector< std::shared_ptr< RelAlgNode >> &nodes)
Hints * getDeliveredHints()
void setTableFuncInputs(std::vector< std::unique_ptr< const RexScalar >> &&exprs)
void replaceInput(std::shared_ptr< const RelAlgNode > old_input, std::shared_ptr< const RelAlgNode > input) override
size_t toHash() const override
PushDownGenericExpressionInWindowFunction(std::shared_ptr< RelProject > new_project, std::vector< std::unique_ptr< const RexScalar >> &scalar_exprs_for_new_project, std::vector< std::string > &fields_for_new_project, std::unordered_map< size_t, size_t > &expr_offset_cache)
const RexScalar * getProjectAt(const size_t idx) const
RetType visitInput(const RexInput *input) const final
std::shared_ptr< Catalog > getCatalog(const std::string &dbName)
static RegisteredQueryHint defaults()
static std::unique_ptr< RelAlgDag > build(const rapidjson::Value &query_ast, RelAlgDag *root_dag, const bool optimize_dag)
int32_t countRexLiteralArgs() const
std::unique_ptr< Hints > hints_
std::vector< const Rex * > reproject_targets(const RelProject *simple_project, const std::vector< const Rex * > &target_exprs) noexcept
const ConstRexScalarPtrVector & getPartitionKeys() const
std::vector< std::shared_ptr< RelAlgNode > > run(const rapidjson::Value &rels, RelAlgDag &root_dag)
DEVICE auto lower_bound(ARGS &&...args)
std::unique_ptr< Hints > hints_
int64_t get_int_literal_field(const rapidjson::Value &obj, const char field[], const int64_t default_val) noexcept
void replaceInput(std::shared_ptr< const RelAlgNode > old_input, std::shared_ptr< const RelAlgNode > input) override
void registerSubquery(std::shared_ptr< RexSubQuery > subquery)
std::vector< std::unique_ptr< const RexAgg > > agg_exprs_
std::vector< std::unique_ptr< const RexScalar > > parse_expr_array(const rapidjson::Value &arr, RelAlgDag &root_dag)
std::unique_ptr< const RexScalar > filter_expr_
static std::vector< std::shared_ptr< RelAlgNode > > & getNodes(RelAlgDag &rel_alg_dag)
bool hasWindowFunctionExpr() const
std::shared_ptr< RelModify > dispatchModify(const rapidjson::Value &logical_modify_ra)
std::vector< ElementType >::const_iterator Super
std::vector< std::unique_ptr< const RexAgg > > copyAggExprs(std::vector< std::unique_ptr< const RexAgg >> const &agg_exprs)
std::unique_ptr< RexLiteral > parse_literal(const rapidjson::Value &expr)
std::vector< std::string > strings_from_json_array(const rapidjson::Value &json_str_arr) noexcept
std::unordered_map< QueryHint, ExplainedQueryHint > Hints
virtual size_t size() const =0
std::string toString(RelRexToStringConfig config=RelRexToStringConfig::defaults()) const override
size_t toHash() const override
std::string typeName(const T *v)
ExplainedQueryHint parseHintString(std::string &hint_string)
void tryAddWindowExpr(RexScalar const *expr) const
void * visitOperator(const RexOperator *rex_operator) const final
bool is_window_function_operator(const RexScalar *rex)
void eachNode(std::function< void(RelAlgNode const *)> const &) const
std::string toString(RelRexToStringConfig config=RelRexToStringConfig::defaults()) const override
std::shared_ptr< RelSort > dispatchSort(const rapidjson::Value &sort_ra)
const std::vector< std::string > & getFields() const
std::unique_ptr< const RexScalar > visitRef(const RexRef *rex_ref) const override
std::string getFieldName(const size_t i) const
static void optimizeDag(RelAlgDag &rel_alg_dag)
std::shared_ptr< RelProject > new_project_
bool g_enable_watchdog false
bool visitSubQuery(const RexSubQuery *) const final
const ConstRexScalarPtrVector & getOrderKeys() const
std::unordered_map< size_t, size_t > pushed_down_partition_key_offset_
std::unique_ptr< const RexScalar > parse_scalar_expr(const rapidjson::Value &expr, RelAlgDag &root_dag)
bool hasPartitionExpression()
RexInputReplacementVisitor(const RelAlgNode *node_to_keep, const std::vector< std::unique_ptr< const RexScalar >> &scalar_sources)
void create_compound(std::vector< std::shared_ptr< RelAlgNode >> &nodes, const std::vector< size_t > &pattern, std::unordered_map< size_t, std::unordered_map< unsigned, RegisteredQueryHint >> &query_hints) noexcept
bool visitInput(const RexInput *input) const final
std::vector< RexInput > n_outputs(const RelAlgNode *node, const size_t n)
std::shared_ptr< const RelAlgNode > prev(const rapidjson::Value &crt_node)
void replaceInput(std::shared_ptr< const RelAlgNode > old_input, std::shared_ptr< const RelAlgNode > input) override
void getRelAlgHints(const rapidjson::Value &json_node, std::shared_ptr< RelAlgNode > node)
virtual size_t toHash() const =0
SortDirection parse_sort_direction(const rapidjson::Value &collation)
RexWindowFunctionOperator::RexWindowBound parse_window_bound(const rapidjson::Value &window_bound_obj, RelAlgDag &root_dag)
Common Enum definitions for SQL processing.
bool is_dict_encoded_string() const
bool found_case_expr_window_operand_
void fold_filters(std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
std::vector< std::unique_ptr< const RexScalar >> RowValues
void bind_table_func_to_input(RelTableFunction *table_func_node, const RANodeOutput &input) noexcept
bool is_window_function_sum(const RexScalar *rex)
RetType visitCase(const RexCase *rex_case) const final
const size_t inputCount() const
void rebind_inputs_from_left_deep_join(const RexScalar *rex, const RelLeftDeepInnerJoin *left_deep_join)
void check_empty_inputs_field(const rapidjson::Value &node) noexcept
WindowFunctionCollector(std::unordered_map< size_t, const RexScalar * > &collected_window_func, bool only_add_window_expr)
HOST DEVICE bool get_notnull() const
unsigned node_id(const rapidjson::Value &ra_node) noexcept
void eliminate_dead_subqueries(std::vector< std::shared_ptr< RexSubQuery >> &subqueries, RelAlgNode const *root)
size_t size() const override
size_t operator()(const std::pair< const RelAlgNode *, int > &input_col) const
std::unordered_map< size_t, size_t > & window_func_to_new_rex_input_idx_map_
RelAlgInputs getRelAlgInputs(const rapidjson::Value &node)
std::vector< std::string > getFieldNamesFromScanNode(const rapidjson::Value &scan_ra)
RelTableFunction()=default
std::shared_ptr< RelLogicalValues > dispatchLogicalValues(const rapidjson::Value &logical_values_ra)
DEVICE void swap(ARGS &&...args)
std::unique_ptr< const RexScalar > RetType
size_t toHash() const override
RANodeOutput get_node_output(const RelAlgNode *ra_node)
virtual size_t toHash() const =0
bool visitLiteral(const RexLiteral *) const final
BuildState getBuildState() const
void reset_table_function_inputs(std::vector< const Rex * > &column_inputs, const std::vector< std::unique_ptr< const RexScalar >> &old_table_func_inputs, const std::vector< std::unique_ptr< const RexScalar >> &new_table_func_inputs)
void set_precision(int d)
std::pair< std::string, std::string > getKVOptionPair(std::string &str, size_t &pos)
void eliminate_dead_columns(std::vector< std::shared_ptr< RelAlgNode >> &nodes) noexcept
bool same_ignoring_notnull(SQLTypeInfo ti0, SQLTypeInfo ti1)
std::vector< std::unique_ptr< const RexScalar > > target_exprs_
static void setBuildState(RelAlgDag &rel_alg_dag, const RelAlgDag::BuildState build_state)
RelProject * new_project_
static void resetRelAlgFirstId() noexcept