384415 {
384416
384417 auto entry = has_correlated_expressions.find(*plan);
384418 bool exit_projection = false;
384419 unique_ptr<LogicalDelimGet> delim_scan;
384420 D_ASSERT(entry != has_correlated_expressions.end());
384421 if (!entry->second) {
384422
384423
384424
384425 if (plan->type == LogicalOperatorType::LOGICAL_CTE_REF) {
384426 auto &op = plan->Cast<LogicalCTERef>();
384427
384428 auto rec_cte = binder.recursive_ctes.find(op.cte_index);
384429 if (rec_cte != binder.recursive_ctes.end()) {
384430 D_ASSERT(rec_cte->second->type == LogicalOperatorType::LOGICAL_RECURSIVE_CTE ||
384431 rec_cte->second->type == LogicalOperatorType::LOGICAL_MATERIALIZED_CTE);
384432
384433 auto &rec_cte_op = rec_cte->second->Cast<LogicalCTE>();
384434 if (op.correlated_columns == 0) {
384435 RewriteCTEScan cte_rewriter(op.cte_index, rec_cte_op.correlated_columns,
384437 cte_rewriter.VisitOperator(*plan);
384438 }
384439 }
384440 }
384441
384442
384443 auto delim_index = binder.GenerateTableIndex();
384444 base_binding = ColumnBinding(delim_index, 0);
384445
384446 auto left_columns = plan->GetColumnBindings().size();
384447 delim_offset = left_columns;
384448 data_offset = 0;
384449 delim_scan = make_uniq<LogicalDelimGet>(delim_index, delim_types);
384450 if (plan->type == LogicalOperatorType::LOGICAL_PROJECTION) {
384451
384452 exit_projection = true;
384453 } else if (plan->type == LogicalOperatorType::LOGICAL_CTE_REF) {
384454
384455
384456
384457
384458 auto &cteref = plan->Cast<LogicalCTERef>();
384459 auto join = make_uniq<LogicalComparisonJoin>(JoinType::INNER);
384460 auto left_binding =
384461 ColumnBinding(cteref.table_index, cteref.chunk_types.size() - cteref.correlated_columns);
384462
384463 for (
idx_t i = 0; i < cteref.correlated_columns; i++) {
384464 JoinCondition cond;
384465 cond.left = make_uniq<BoundColumnRefExpression>(
384466 correlated_columns[i].type, ColumnBinding(left_binding.table_index, left_binding.column_index + i));
384467 cond.right = make_uniq<BoundColumnRefExpression>(
384468 correlated_columns[i].type, ColumnBinding(base_binding.table_index, base_binding.column_index + i));
384469 cond.comparison = ExpressionType::COMPARE_NOT_DISTINCT_FROM;
384470 join->conditions.push_back(std::move(cond));
384471 }
384472 join->children.push_back(std::move(plan));
384473 join->children.push_back(std::move(delim_scan));
384474
384475 return std::move(join);
384476 } else {
384477 auto cross_product = LogicalCrossProduct::Create(Decorrelate(std::move(plan)), std::move(delim_scan));
384478 return cross_product;
384479 }
384480 }
384481 switch (plan->type) {
384482 case LogicalOperatorType::LOGICAL_UNNEST:
384483 case LogicalOperatorType::LOGICAL_FILTER: {
384484
384485
384486 for (auto &expr : plan->expressions) {
384487 any_join |= SubqueryDependentFilter(*expr);
384488 }
384489 plan->children[0] =
384490 PushDownDependentJoinInternal(std::move(plan->children[0]), parent_propagate_null_values, lateral_depth);
384491
384492
384493 RewriteCorrelatedExpressions rewriter(base_binding, correlated_map, lateral_depth);
384494 rewriter.VisitOperator(*plan);
384495 return plan;
384496 }
384497 case LogicalOperatorType::LOGICAL_PROJECTION: {
384498
384499
384500 for (auto &expr : plan->expressions) {
384501 parent_propagate_null_values &= expr->PropagatesNullValues();
384502 }
384503
384504
384505
384506
384507 bool child_is_dependent_join = plan->children[0]->type == LogicalOperatorType::LOGICAL_DEPENDENT_JOIN;
384508 parent_propagate_null_values &= !child_is_dependent_join;
384509
384510
384511
384512
384513
384514 if (exit_projection) {
384515 auto cross_product =
384516 LogicalCrossProduct::Create(Decorrelate(std::move(plan->children[0])), std::move(delim_scan));
384517 plan->children[0] = std::move(cross_product);
384518 } else {
384519 plan->children[0] = PushDownDependentJoinInternal(std::move(plan->children[0]),
384520 parent_propagate_null_values, lateral_depth);
384521 }
384522
384523
384524 RewriteCorrelatedExpressions rewriter(base_binding, correlated_map, lateral_depth);
384525 rewriter.VisitOperator(*plan);
384526
384527 auto &proj = plan->Cast<LogicalProjection>();
384528 for (
idx_t i = 0; i < correlated_columns.size(); i++) {
384529 auto &col = correlated_columns[i];
384530 auto colref = make_uniq<BoundColumnRefExpression>(
384531 col.name, col.type, ColumnBinding(base_binding.table_index, base_binding.column_index + i));
384532 plan->expressions.push_back(std::move(colref));
384533 }
384534
384535 base_binding.table_index = proj.table_index;
384536 base_binding.column_index = plan->expressions.size() - correlated_columns.size();
384537 this->delim_offset = base_binding.column_index;
384538 this->data_offset = 0;
384539 return plan;
384540 }
384541 case LogicalOperatorType::LOGICAL_AGGREGATE_AND_GROUP_BY: {
384542 auto &aggr = plan->Cast<LogicalAggregate>();
384543
384544
384545 for (auto &expr : plan->expressions) {
384546 parent_propagate_null_values &= expr->PropagatesNullValues();
384547 }
384548 plan->children[0] =
384549 PushDownDependentJoinInternal(std::move(plan->children[0]), parent_propagate_null_values, lateral_depth);
384550
384551 RewriteCorrelatedExpressions rewriter(base_binding, correlated_map, lateral_depth);
384552 rewriter.VisitOperator(*plan);
384553
384554 idx_t delim_table_index;
384555 idx_t delim_column_offset;
384556 idx_t delim_data_offset;
384557 auto new_group_count = perform_delim ? correlated_columns.size() : 1;
384558 for (
idx_t i = 0; i < new_group_count; i++) {
384559 auto &col = correlated_columns[i];
384560 auto colref = make_uniq<BoundColumnRefExpression>(
384561 col.name, col.type, ColumnBinding(base_binding.table_index, base_binding.column_index + i));
384562 for (auto &set : aggr.grouping_sets) {
384563 set.insert(aggr.groups.size());
384564 }
384565 aggr.groups.push_back(std::move(colref));
384566 }
384567 if (!perform_delim) {
384568
384569
384570 delim_table_index = aggr.aggregate_index;
384571 delim_column_offset = aggr.expressions.size();
384572 delim_data_offset = aggr.groups.size();
384573 for (
idx_t i = 0; i < correlated_columns.size(); i++) {
384574 auto &col = correlated_columns[i];
384575 auto first_aggregate = FirstFunctionGetter::GetFunction(col.type);
384576 auto colref = make_uniq<BoundColumnRefExpression>(
384577 col.name, col.type, ColumnBinding(base_binding.table_index, base_binding.column_index + i));
384578 vector<unique_ptr<Expression>> aggr_children;
384579 aggr_children.push_back(std::move(colref));
384580 auto first_fun =
384581 make_uniq<BoundAggregateExpression>(std::move(first_aggregate), std::move(aggr_children), nullptr,
384582 nullptr, AggregateType::NON_DISTINCT);
384583 aggr.expressions.push_back(std::move(first_fun));
384584 }
384585 } else {
384586 delim_table_index = aggr.group_index;
384587 delim_column_offset = aggr.groups.size() - correlated_columns.size();
384588 delim_data_offset = aggr.groups.size();
384589 }
384590 bool ungrouped_join = false;
384591 if (aggr.grouping_sets.empty()) {
384592 ungrouped_join = aggr.groups.size() == new_group_count;
384593 } else {
384594 for (auto &grouping_set : aggr.grouping_sets) {
384595 if (grouping_set.size() == new_group_count) {
384596 ungrouped_join = true;
384597 }
384598 }
384599 }
384600 if (ungrouped_join) {
384601
384602
384603
384604 JoinType join_type = JoinType::INNER;
384605 if (any_join || !parent_propagate_null_values) {
384606 join_type = JoinType::LEFT;
384607 }
384608 for (auto &aggr_exp : aggr.expressions) {
384609 auto &b_aggr_exp = aggr_exp->Cast<BoundAggregateExpression>();
384610 if (!b_aggr_exp.PropagatesNullValues()) {
384611 join_type = JoinType::LEFT;
384612 break;
384613 }
384614 }
384615 unique_ptr<LogicalComparisonJoin>
join = make_uniq<LogicalComparisonJoin>(join_type);
384616 auto left_index = binder.GenerateTableIndex();
384617 delim_scan = make_uniq<LogicalDelimGet>(left_index, delim_types);
384618 join->children.push_back(std::move(delim_scan));
384619 join->children.push_back(std::move(plan));
384620 for (
idx_t i = 0; i < new_group_count; i++) {
384621 auto &col = correlated_columns[i];
384622 JoinCondition cond;
384623 cond.left = make_uniq<BoundColumnRefExpression>(col.name, col.type, ColumnBinding(left_index, i));
384624 cond.right = make_uniq<BoundColumnRefExpression>(
384625 correlated_columns[i].type, ColumnBinding(delim_table_index, delim_column_offset + i));
384626 cond.comparison = ExpressionType::COMPARE_NOT_DISTINCT_FROM;
384627 join->conditions.push_back(std::move(cond));
384628 }
384629
384630
384631 for (
idx_t i = 0; i < aggr.expressions.size(); i++) {
384632 D_ASSERT(aggr.expressions[i]->GetExpressionClass() == ExpressionClass::BOUND_AGGREGATE);
384633 auto &bound = aggr.expressions[i]->Cast<BoundAggregateExpression>();
384634 vector<LogicalType> arguments;
384635 if (bound.function == CountFunctionBase::GetFunction() ||
384636 bound.function == CountStarFun::GetFunction()) {
384637
384638 replacement_map[ColumnBinding(aggr.aggregate_index, i)] = i;
384639 }
384640 }
384641
384642 base_binding.table_index = left_index;
384643 this->delim_offset = base_binding.column_index = 0;
384644 this->data_offset = 0;
384645 return std::move(join);
384646 } else {
384647
384648 base_binding.table_index = delim_table_index;
384649 this->delim_offset = base_binding.column_index = delim_column_offset;
384650 this->data_offset = delim_data_offset;
384651 return plan;
384652 }
384653 }
384654 case LogicalOperatorType::LOGICAL_CROSS_PRODUCT: {
384655
384656
384657 bool left_has_correlation = has_correlated_expressions.find(*plan->children[0])->second;
384658 bool right_has_correlation = has_correlated_expressions.find(*plan->children[1])->second;
384659 if (!right_has_correlation) {
384660
384661 plan->children[0] = PushDownDependentJoinInternal(std::move(plan->children[0]),
384662 parent_propagate_null_values, lateral_depth);
384663
384664
384665 plan->children[1] = DecorrelateIndependent(binder, std::move(plan->children[1]));
384666 return plan;
384667 }
384668 if (!left_has_correlation) {
384669
384670 plan->children[1] = PushDownDependentJoinInternal(std::move(plan->children[1]),
384671 parent_propagate_null_values, lateral_depth);
384672
384673
384674 plan->children[0] = DecorrelateIndependent(binder, std::move(plan->children[0]));
384675
384676 delim_offset += plan->children[0]->GetColumnBindings().size();
384677 return plan;
384678 }
384679
384680
384681
384682 auto join = make_uniq<LogicalComparisonJoin>(JoinType::INNER);
384683 plan->children[1] =
384684 PushDownDependentJoinInternal(std::move(plan->children[1]), parent_propagate_null_values, lateral_depth);
384685 auto right_binding = this->base_binding;
384686 plan->children[0] =
384687 PushDownDependentJoinInternal(std::move(plan->children[0]), parent_propagate_null_values, lateral_depth);
384688
384689 for (
idx_t i = 0; i < correlated_columns.size(); i++) {
384690 JoinCondition cond;
384691 cond.left = make_uniq<BoundColumnRefExpression>(
384692 correlated_columns[i].type, ColumnBinding(base_binding.table_index, base_binding.column_index + i));
384693 cond.right = make_uniq<BoundColumnRefExpression>(
384694 correlated_columns[i].type, ColumnBinding(right_binding.table_index, right_binding.column_index + i));
384695 cond.comparison = ExpressionType::COMPARE_NOT_DISTINCT_FROM;
384696 join->conditions.push_back(std::move(cond));
384697 }
384698 join->children.push_back(std::move(plan->children[0]));
384699 join->children.push_back(std::move(plan->children[1]));
384700 return std::move(join);
384701 }
384702 case LogicalOperatorType::LOGICAL_DEPENDENT_JOIN: {
384703 D_ASSERT(plan->children.size() == 2);
384704 return Decorrelate(std::move(plan), parent_propagate_null_values, lateral_depth);
384705 }
384706 case LogicalOperatorType::LOGICAL_ANY_JOIN:
384707 case LogicalOperatorType::LOGICAL_ASOF_JOIN:
384708 case LogicalOperatorType::LOGICAL_COMPARISON_JOIN: {
384709 auto &
join = plan->Cast<LogicalJoin>();
384710 D_ASSERT(plan->children.size() == 2);
384711
384712 bool left_has_correlation = has_correlated_expressions.find(*plan->children[0])->second;
384713 bool right_has_correlation = has_correlated_expressions.find(*plan->children[1])->second;
384714
384715 if (
join.join_type == JoinType::INNER) {
384716
384717 if (!right_has_correlation) {
384718
384719 plan->children[0] = PushDownDependentJoinInternal(std::move(plan->children[0]),
384720 parent_propagate_null_values, lateral_depth);
384721 plan->children[1] = DecorrelateIndependent(binder, std::move(plan->children[1]));
384722
384723 return plan;
384724 }
384725 if (!left_has_correlation) {
384726
384727 plan->children[1] = PushDownDependentJoinInternal(std::move(plan->children[1]),
384728 parent_propagate_null_values, lateral_depth);
384729 plan->children[0] = DecorrelateIndependent(binder, std::move(plan->children[0]));
384730 delim_offset += plan->children[0]->GetColumnBindings().size();
384731
384732 return plan;
384733 }
384734 }
else if (
join.join_type == JoinType::LEFT) {
384735
384736 if (!right_has_correlation) {
384737
384738 plan->children[0] = PushDownDependentJoinInternal(std::move(plan->children[0]),
384739 parent_propagate_null_values, lateral_depth);
384740 plan->children[1] = DecorrelateIndependent(binder, std::move(plan->children[1]));
384741
384742 return plan;
384743 }
384744 }
else if (
join.join_type == JoinType::RIGHT) {
384745
384746 if (!left_has_correlation) {
384747
384748 plan->children[1] = PushDownDependentJoinInternal(std::move(plan->children[1]),
384749 parent_propagate_null_values, lateral_depth);
384750 plan->children[0] = DecorrelateIndependent(binder, std::move(plan->children[0]));
384751 delim_offset += plan->children[0]->GetColumnBindings().size();
384752 return plan;
384753 }
384754 }
else if (
join.join_type == JoinType::MARK) {
384755 if (!left_has_correlation && right_has_correlation) {
384756
384757
384758 ColumnBinding right_binding;
384759
384760
384761
384762 plan->children[1] = PushDownDependentJoinInternal(std::move(plan->children[1]),
384763 parent_propagate_null_values, lateral_depth);
384764 right_binding = this->base_binding;
384765
384766
384767
384768
384769 plan->children[0] = PushDownDependentJoinInternal(std::move(plan->children[0]),
384770 parent_propagate_null_values, lateral_depth);
384771
384772 auto left_binding = this->base_binding;
384773
384774
384775 for (
idx_t i = 0; i < correlated_columns.size(); i++) {
384776 JoinCondition cond;
384777 cond.left = make_uniq<BoundColumnRefExpression>(
384778 correlated_columns[i].type,
384779 ColumnBinding(left_binding.table_index, left_binding.column_index + i));
384780 cond.right = make_uniq<BoundColumnRefExpression>(
384781 correlated_columns[i].type,
384782 ColumnBinding(right_binding.table_index, right_binding.column_index + i));
384783 cond.comparison = ExpressionType::COMPARE_NOT_DISTINCT_FROM;
384784
384785 auto &comparison_join =
join.Cast<LogicalComparisonJoin>();
384786 comparison_join.conditions.push_back(std::move(cond));
384787 }
384788 return plan;
384789 }
384790
384791
384792 plan->children[0] = PushDownDependentJoinInternal(std::move(plan->children[0]),
384793 parent_propagate_null_values, lateral_depth);
384794 plan->children[1] = DecorrelateIndependent(binder, std::move(plan->children[1]));
384795
384796 RewriteCorrelatedExpressions rewriter(base_binding, correlated_map, lateral_depth);
384797 rewriter.VisitOperator(*plan);
384798 return plan;
384799 } else {
384800 throw NotImplementedException("Unsupported join type for flattening correlated subquery");
384801 }
384802
384803
384804 plan->children[0] =
384805 PushDownDependentJoinInternal(std::move(plan->children[0]), parent_propagate_null_values, lateral_depth);
384806 auto left_delim_offset = delim_offset;
384807 auto left_binding = this->base_binding;
384808 plan->children[1] =
384809 PushDownDependentJoinInternal(std::move(plan->children[1]), parent_propagate_null_values, lateral_depth);
384810 auto right_binding = this->base_binding;
384811
384812
384813
384814 if (
join.join_type == JoinType::LEFT) {
384815 this->base_binding = left_binding;
384816 delim_offset = left_delim_offset;
384817 }
else if (
join.join_type == JoinType::RIGHT) {
384818 this->base_binding = right_binding;
384819 delim_offset += plan->children[0]->GetColumnBindings().size();
384820 }
384821
384822 for (
idx_t i = 0; i < correlated_columns.size(); i++) {
384823 auto left = make_uniq<BoundColumnRefExpression>(
384824 correlated_columns[i].type, ColumnBinding(left_binding.table_index, left_binding.column_index + i));
384825 auto right = make_uniq<BoundColumnRefExpression>(
384826 correlated_columns[i].type, ColumnBinding(right_binding.table_index, right_binding.column_index + i));
384827
384828 if (
join.type == LogicalOperatorType::LOGICAL_COMPARISON_JOIN ||
384829 join.type == LogicalOperatorType::LOGICAL_ASOF_JOIN) {
384830 JoinCondition cond;
384831 cond.left = std::move(left);
384832 cond.right = std::move(right);
384833 cond.comparison = ExpressionType::COMPARE_NOT_DISTINCT_FROM;
384834
384835 auto &comparison_join =
join.Cast<LogicalComparisonJoin>();
384836 comparison_join.conditions.push_back(std::move(cond));
384837 } else {
384838 auto &logical_any_join =
join.Cast<LogicalAnyJoin>();
384839 auto comparison = make_uniq<BoundComparisonExpression>(ExpressionType::COMPARE_NOT_DISTINCT_FROM,
384840 std::move(left), std::move(right));
384841 auto conjunction = make_uniq<BoundConjunctionExpression>(
384842 ExpressionType::CONJUNCTION_AND, std::move(comparison), std::move(logical_any_join.condition));
384843 logical_any_join.condition = std::move(conjunction);
384844 }
384845 }
384846
384847 RewriteCorrelatedExpressions rewriter(right_binding, correlated_map, lateral_depth);
384848 rewriter.VisitOperator(*plan);
384849 return plan;
384850 }
384851 case LogicalOperatorType::LOGICAL_LIMIT: {
384852 auto &limit = plan->Cast<LogicalLimit>();
384853 switch (limit.limit_val.Type()) {
384854 case LimitNodeType::CONSTANT_PERCENTAGE:
384855 case LimitNodeType::EXPRESSION_PERCENTAGE:
384856
384857
384858
384859 throw ParserException("Limit percent operator not supported in correlated subquery");
384860 case LimitNodeType::EXPRESSION_VALUE:
384861 throw ParserException("Non-constant limit not supported in correlated subquery");
384862 default:
384863 break;
384864 }
384865 switch (limit.offset_val.Type()) {
384866 case LimitNodeType::EXPRESSION_VALUE:
384867 throw ParserException("Non-constant offset not supported in correlated subquery");
384868 case LimitNodeType::CONSTANT_PERCENTAGE:
384869 case LimitNodeType::EXPRESSION_PERCENTAGE:
384870 throw InternalException("Percentage offset in FlattenDependentJoin");
384871 default:
384872 break;
384873 }
384874 auto rownum_alias = "limit_rownum";
384875 unique_ptr<LogicalOperator> child;
384876 unique_ptr<LogicalOrder> order_by;
384877
384878
384879
384880 if (plan->children[0]->type == LogicalOperatorType::LOGICAL_ORDER_BY) {
384881 order_by = unique_ptr_cast<LogicalOperator, LogicalOrder>(std::move(plan->children[0]));
384882 child = PushDownDependentJoinInternal(std::move(order_by->children[0]), parent_propagate_null_values,
384883 lateral_depth);
384884 } else {
384885 child = PushDownDependentJoinInternal(std::move(plan->children[0]), parent_propagate_null_values,
384886 lateral_depth);
384887 }
384888 auto child_column_count = child->GetColumnBindings().size();
384889
384890 auto window_index = binder.GenerateTableIndex();
384891 auto window = make_uniq<LogicalWindow>(window_index);
384892 auto row_number =
384893 make_uniq<BoundWindowExpression>(ExpressionType::WINDOW_ROW_NUMBER, LogicalType::BIGINT, nullptr, nullptr);
384894 auto partition_count = perform_delim ? correlated_columns.size() : 1;
384895 for (
idx_t i = 0; i < partition_count; i++) {
384896 auto &col = correlated_columns[i];
384897 auto colref = make_uniq<BoundColumnRefExpression>(
384898 col.name, col.type, ColumnBinding(base_binding.table_index, base_binding.column_index + i));
384899 row_number->partitions.push_back(std::move(colref));
384900 }
384901 if (order_by) {
384902
384903
384904
384905 row_number->orders = std::move(order_by->orders);
384906 }
384907 row_number->start = WindowBoundary::UNBOUNDED_PRECEDING;
384908 row_number->end = WindowBoundary::CURRENT_ROW_ROWS;
384909 window->expressions.push_back(std::move(row_number));
384910 window->children.push_back(std::move(child));
384911
384912
384913
384914 auto filter = make_uniq<LogicalFilter>();
384915 unique_ptr<Expression> condition;
384916 auto row_num_ref =
384917 make_uniq<BoundColumnRefExpression>(rownum_alias, LogicalType::BIGINT, ColumnBinding(window_index, 0));
384918
384919 if (limit.limit_val.Type() == LimitNodeType::CONSTANT_VALUE) {
384920 auto upper_bound_limit = NumericLimits<int64_t>::Maximum();
384921 auto limit_val =
int64_t(limit.limit_val.GetConstantValue());
384922 if (limit.offset_val.Type() == LimitNodeType::CONSTANT_VALUE) {
384923
384924 auto offset_val =
int64_t(limit.offset_val.GetConstantValue());
384925 TryAddOperator::Operation(limit_val, offset_val, upper_bound_limit);
384926 } else {
384927
384928 upper_bound_limit = limit_val;
384929 }
384930 auto upper_bound = make_uniq<BoundConstantExpression>(
Value::BIGINT(upper_bound_limit));
384931 condition = make_uniq<BoundComparisonExpression>(ExpressionType::COMPARE_LESSTHANOREQUALTO,
384932 row_num_ref->Copy(), std::move(upper_bound));
384933 }
384934
384935 if (limit.offset_val.Type() == LimitNodeType::CONSTANT_VALUE) {
384936 auto offset_val =
int64_t(limit.offset_val.GetConstantValue());
384937 auto lower_bound = make_uniq<BoundConstantExpression>(
Value::BIGINT(offset_val));
384938 auto lower_comp = make_uniq<BoundComparisonExpression>(ExpressionType::COMPARE_GREATERTHAN,
384939 row_num_ref->Copy(), std::move(lower_bound));
384940 if (condition) {
384941 auto conj = make_uniq<BoundConjunctionExpression>(ExpressionType::CONJUNCTION_AND,
384942 std::move(lower_comp), std::move(condition));
384943 condition = std::move(conj);
384944 } else {
384945 condition = std::move(lower_comp);
384946 }
384947 }
384948 filter->expressions.push_back(std::move(condition));
384949 filter->children.push_back(std::move(window));
384950
384951 for (
idx_t i = 0; i < child_column_count; i++) {
384952 filter->projection_map.push_back(i);
384953 }
384954 return std::move(filter);
384955 }
384956 case LogicalOperatorType::LOGICAL_WINDOW: {
384957 auto &window = plan->Cast<LogicalWindow>();
384958
384959 plan->children[0] =
384960 PushDownDependentJoinInternal(std::move(plan->children[0]), parent_propagate_null_values, lateral_depth);
384961
384962
384963 RewriteCorrelatedExpressions rewriter(base_binding, correlated_map, lateral_depth);
384964 rewriter.VisitOperator(*plan);
384965
384966
384967 for (auto &expr : window.expressions) {
384968 D_ASSERT(expr->GetExpressionClass() == ExpressionClass::BOUND_WINDOW);
384969 auto &w = expr->Cast<BoundWindowExpression>();
384970 for (
idx_t i = 0; i < correlated_columns.size(); i++) {
384971 w.partitions.push_back(make_uniq<BoundColumnRefExpression>(
384972 correlated_columns[i].type,
384973 ColumnBinding(base_binding.table_index, base_binding.column_index + i)));
384974 }
384975 }
384976 return plan;
384977 }
384978 case LogicalOperatorType::LOGICAL_EXCEPT:
384979 case LogicalOperatorType::LOGICAL_INTERSECT:
384980 case LogicalOperatorType::LOGICAL_UNION: {
384981 auto &setop = plan->Cast<LogicalSetOperation>();
384982
384983#ifdef DEBUG
384984 for (auto &child : plan->children) {
384985 child->ResolveOperatorTypes();
384986 }
384987 for (
idx_t i = 1; i < plan->children.size(); i++) {
384988 D_ASSERT(plan->children[0]->types.size() == plan->children[i]->types.size());
384989 }
384990#endif
384991 for (auto &child : plan->children) {
384993 }
384994 for (
idx_t i = 0; i < plan->children.size(); i++) {
384995 if (plan->children[i]->type == LogicalOperatorType::LOGICAL_CROSS_PRODUCT) {
384996 auto proj_index = binder.GenerateTableIndex();
384997 auto bindings = plan->children[i]->GetColumnBindings();
384998 plan->children[i]->ResolveOperatorTypes();
384999 auto types = plan->children[i]->types;
385000 vector<unique_ptr<Expression>> expressions;
385001 expressions.reserve(bindings.size());
385002 D_ASSERT(bindings.size() == types.size());
385003
385004
385005
385006 for (
idx_t col_idx = 0; col_idx < bindings.size(); col_idx++) {
385007 expressions.push_back(make_uniq<BoundColumnRefExpression>(types[col_idx], bindings[col_idx]));
385008 }
385009 auto proj = make_uniq<LogicalProjection>(proj_index, std::move(expressions));
385010 proj->children.push_back(std::move(plan->children[i]));
385011 plan->children[i] = std::move(proj);
385012 }
385013 }
385014
385015
385016
385017#ifdef DEBUG
385018 for (
idx_t i = 1; i < plan->children.size(); i++) {
385019 D_ASSERT(plan->children[0]->GetColumnBindings().size() == plan->children[i]->GetColumnBindings().size());
385020 }
385021 for (auto &child : plan->children) {
385022 child->ResolveOperatorTypes();
385023 }
385024 for (
idx_t i = 1; i < plan->children.size(); i++) {
385025 D_ASSERT(plan->children[0]->types.size() == plan->children[i]->types.size());
385026 }
385027#endif
385028
385029 base_binding.table_index = setop.table_index;
385030 base_binding.column_index = setop.column_count;
385031 setop.column_count += correlated_columns.size();
385032 return plan;
385033 }
385034 case LogicalOperatorType::LOGICAL_DISTINCT: {
385035 auto &distinct = plan->Cast<LogicalDistinct>();
385036
385038
385039 for (
idx_t i = 0; i < correlated_columns.size(); i++) {
385040 distinct.distinct_targets.push_back(make_uniq<BoundColumnRefExpression>(
385041 correlated_columns[i].type, ColumnBinding(base_binding.table_index, base_binding.column_index + i)));
385042 }
385043 return plan;
385044 }
385045 case LogicalOperatorType::LOGICAL_EXPRESSION_GET: {
385046
385047
385048 plan->children[0] =
385049 PushDownDependentJoinInternal(std::move(plan->children[0]), parent_propagate_null_values, lateral_depth);
385050
385051 RewriteCorrelatedExpressions rewriter(base_binding, correlated_map, lateral_depth);
385052 rewriter.VisitOperator(*plan);
385053
385054 auto &expr_get = plan->Cast<LogicalExpressionGet>();
385055 for (
idx_t i = 0; i < correlated_columns.size(); i++) {
385056 for (auto &expr_list : expr_get.expressions) {
385057 auto colref = make_uniq<BoundColumnRefExpression>(
385058 correlated_columns[i].type, ColumnBinding(base_binding.table_index, base_binding.column_index + i));
385059 expr_list.push_back(std::move(colref));
385060 }
385061 expr_get.expr_types.push_back(correlated_columns[i].type);
385062 }
385063
385064 base_binding.table_index = expr_get.table_index;
385065 this->delim_offset = base_binding.column_index = expr_get.expr_types.size() - correlated_columns.size();
385066 this->data_offset = 0;
385067 return plan;
385068 }
385069 case LogicalOperatorType::LOGICAL_PIVOT:
385070 throw BinderException("PIVOT is not supported in correlated subqueries yet");
385071 case LogicalOperatorType::LOGICAL_ORDER_BY:
385073 return plan;
385074 case LogicalOperatorType::LOGICAL_GET: {
385075 auto &
get = plan->Cast<LogicalGet>();
385076 if (
get.children.size() != 1) {
385077 throw InternalException("Flatten dependent joins - logical get encountered without children");
385078 }
385080 for (
idx_t i = 0; i < correlated_columns.size(); i++) {
385081 get.projected_input.push_back(this->delim_offset + i);
385082 }
385083 this->delim_offset =
get.GetColumnIds().size();
385084 this->data_offset = 0;
385085
385086 RewriteCorrelatedExpressions rewriter(base_binding, correlated_map, lateral_depth);
385087 rewriter.VisitOperator(*plan);
385088 return plan;
385089 }
385090 case LogicalOperatorType::LOGICAL_MATERIALIZED_CTE:
385091 case LogicalOperatorType::LOGICAL_RECURSIVE_CTE: {
385092#ifdef DEBUG
385093 plan->children[0]->ResolveOperatorTypes();
385094 plan->children[1]->ResolveOperatorTypes();
385095#endif
385096 if (plan->type == LogicalOperatorType::LOGICAL_MATERIALIZED_CTE) {
385097
385098 bool left_has_correlation = has_correlated_expressions.find(*plan->children[0])->second;
385099 bool right_has_correlation = has_correlated_expressions.find(*plan->children[1])->second;
385100
385101 if (!left_has_correlation && right_has_correlation) {
385102
385103 plan->children[1] = PushDownDependentJoinInternal(std::move(plan->children[1]),
385104 parent_propagate_null_values, lateral_depth);
385105 plan->children[0] = DecorrelateIndependent(binder, std::move(plan->children[0]));
385106 return plan;
385107 }
385108 }
385109
385110 idx_t table_index = 0;
385111 plan->children[0] =
385112 PushDownDependentJoinInternal(std::move(plan->children[0]), parent_propagate_null_values, lateral_depth);
385113
385114 auto &setop = plan->Cast<LogicalCTE>();
385115 base_binding.table_index = setop.table_index;
385116 base_binding.column_index = setop.column_count;
385117 table_index = setop.table_index;
385118 setop.correlated_columns = correlated_columns;
385119 binder.recursive_ctes[setop.table_index] = &setop;
385120
385121 if (plan->type == LogicalOperatorType::LOGICAL_RECURSIVE_CTE) {
385122 auto &setop = plan->Cast<LogicalRecursiveCTE>();
385123
385124 if (!setop.key_targets.empty()) {
385125 for (
idx_t i = 0; i < correlated_columns.size(); i++) {
385126 auto corr = correlated_columns[i];
385127 auto colref = make_uniq<BoundColumnRefExpression>(
385128 correlated_columns[i].type,
385129 ColumnBinding(base_binding.table_index, base_binding.column_index + i));
385130 setop.key_targets.push_back(std::move(colref));
385131 }
385132 }
385133 }
385134
385136 if (plan->type == LogicalOperatorType::LOGICAL_RECURSIVE_CTE) {
385138 } else if (plan->type == LogicalOperatorType::LOGICAL_MATERIALIZED_CTE) {
385140 } else {
385141 throw InternalException("Unsupported CTE operator type for CTEScanRewriteMode selection");
385142 }
385143 RewriteCTEScan cte_rewriter(table_index, correlated_columns, rewrite_mode);
385144 cte_rewriter.VisitOperator(*plan->children[1]);
385145
385146 parent_propagate_null_values = false;
385147 plan->children[1] =
385148 PushDownDependentJoinInternal(std::move(plan->children[1]), parent_propagate_null_values, lateral_depth);
385149
385150 RewriteCorrelatedExpressions rewriter(this->base_binding, correlated_map, lateral_depth);
385151 rewriter.VisitOperator(*plan);
385152
385153 RewriteCorrelatedExpressions recursive_rewriter(this->base_binding, correlated_map, lateral_depth + 1, true);
385154 recursive_rewriter.VisitOperator(*plan->children[0]);
385155 recursive_rewriter.VisitOperator(*plan->children[1]);
385156
385157#ifdef DEBUG
385158 plan->children[0]->ResolveOperatorTypes();
385159 plan->children[1]->ResolveOperatorTypes();
385160#endif
385161
385162 if (plan->type == LogicalOperatorType::LOGICAL_RECURSIVE_CTE) {
385163
385164 base_binding.table_index = setop.table_index;
385165 base_binding.column_index = setop.column_count;
385166 }
385167
385168 setop.column_count += correlated_columns.size();
385169
385170 return plan;
385171 }
385172 case LogicalOperatorType::LOGICAL_CTE_REF: {
385173 auto &cteref = plan->Cast<LogicalCTERef>();
385174
385175 base_binding.table_index = cteref.table_index;
385176 base_binding.column_index = cteref.chunk_types.size() - cteref.correlated_columns;
385177 return plan;
385178 }
385179 case LogicalOperatorType::LOGICAL_DELIM_JOIN: {
385180 throw BinderException("Nested lateral joins or lateral joins in correlated subqueries are not (yet) supported");
385181 }
385182 case LogicalOperatorType::LOGICAL_SAMPLE:
385183 throw BinderException("Sampling in correlated subqueries is not (yet) supported");
385184 case LogicalOperatorType::LOGICAL_POSITIONAL_JOIN:
385185 throw BinderException("Positional join in correlated subqueries is not (yet) supported");
385186 default:
385187 throw InternalException("Logical operator type \"%s\" for dependent join", LogicalOperatorToString(plan->type));
385188 }
385189}
static DUCKDB_API Value BIGINT(int64_t value)
Create a bigint Value from a specified value.
Definition duckdb.cpp:132146
__host__ cudaTextureObject_t get() const noexcept
cv::String join(const cv::String &base, const cv::String &path)
CTEScanRewriteMode
Controls whether RewriteCTEScan rewrites only CTE_REF nodes or also dependent joins.
Definition duckdb.cpp:384041
@ WITH_NON_RECURSIVE_DEPENDENT_JOINS
Rewrite LOGICAL_CTE_REF nodes and non-recursive dependent joins that reference the target CTE.
Definition duckdb.cpp:384045
@ WITH_RECURSIVE_DEPENDENT_JOINS
Rewrite LOGICAL_CTE_REF nodes and dependent joins for recursive CTE rewrites (preserving recursive or...
Definition duckdb.cpp:384047