From d933de6442310890b47d22ce86fad2c0c0dc8633 Mon Sep 17 00:00:00 2001 From: Denis Hirn Date: Mon, 15 Jul 2024 14:02:12 +0200 Subject: [PATCH] Improve handling of DELIM idx --- .../duckdb/planner/operator/logical_delim_get.hpp | 4 ++-- .../planner/subquery/flatten_dependent_join.hpp | 4 ++-- .../storage/serialization/logical_operator.json | 2 +- src/planner/binder/query_node/plan_subquery.cpp | 12 ++++-------- src/planner/subquery/flatten_dependent_join.cpp | 10 ++++------ .../serialization/serialize_logical_operator.cpp | 4 ++-- 6 files changed, 15 insertions(+), 21 deletions(-) diff --git a/src/include/duckdb/planner/operator/logical_delim_get.hpp b/src/include/duckdb/planner/operator/logical_delim_get.hpp index 44e7d5d6daf9..87cfc8dba67f 100644 --- a/src/include/duckdb/planner/operator/logical_delim_get.hpp +++ b/src/include/duckdb/planner/operator/logical_delim_get.hpp @@ -18,8 +18,8 @@ class LogicalDelimGet : public LogicalOperator { static constexpr const LogicalOperatorType TYPE = LogicalOperatorType::LOGICAL_DELIM_GET; public: - LogicalDelimGet(idx_t table_index, vector types) - : LogicalOperator(LogicalOperatorType::LOGICAL_DELIM_GET), table_index(table_index) { + LogicalDelimGet(idx_t table_index, vector types, optional_idx delim_idx) + : LogicalOperator(LogicalOperatorType::LOGICAL_DELIM_GET), table_index(table_index), delim_idx(delim_idx) { D_ASSERT(types.size() > 0); chunk_types = std::move(types); } diff --git a/src/include/duckdb/planner/subquery/flatten_dependent_join.hpp b/src/include/duckdb/planner/subquery/flatten_dependent_join.hpp index d54e1778fcbf..25c06765be45 100644 --- a/src/include/duckdb/planner/subquery/flatten_dependent_join.hpp +++ b/src/include/duckdb/planner/subquery/flatten_dependent_join.hpp @@ -18,8 +18,8 @@ namespace duckdb { //! The FlattenDependentJoins class is responsible for pushing the dependent join down into the plan to create a //! flattened subquery struct FlattenDependentJoins { - FlattenDependentJoins(Binder &binder, const vector &correlated, bool perform_delim = true, - bool any_join = false); + FlattenDependentJoins(Binder &binder, const vector &correlated, idx_t delim_root_idx, + bool perform_delim = true, bool any_join = false); //! Detects which Logical Operators have correlated expressions that they are dependent upon, filling the //! has_correlated_expressions map. diff --git a/src/include/duckdb/storage/serialization/logical_operator.json b/src/include/duckdb/storage/serialization/logical_operator.json index 5cda48b74511..85781e4f3776 100644 --- a/src/include/duckdb/storage/serialization/logical_operator.json +++ b/src/include/duckdb/storage/serialization/logical_operator.json @@ -274,7 +274,7 @@ "default": "optional_idx()" } ], - "constructor": ["table_index", "chunk_types"] + "constructor": ["table_index", "chunk_types", "delim_idx"] }, { "class": "LogicalExpressionGet", diff --git a/src/planner/binder/query_node/plan_subquery.cpp b/src/planner/binder/query_node/plan_subquery.cpp index cb2afc733a4c..393e740f4b18 100644 --- a/src/planner/binder/query_node/plan_subquery.cpp +++ b/src/planner/binder/query_node/plan_subquery.cpp @@ -251,9 +251,8 @@ static unique_ptr PlanCorrelatedSubquery(Binder &binder, BoundSubque // the right side initially is a DEPENDENT join between the duplicate eliminated scan and the subquery // HOWEVER: we do not explicitly create the dependent join // instead, we eliminate the dependent join by pushing it down into the right side of the plan - FlattenDependentJoins flatten(binder, correlated_columns, perform_delim); + FlattenDependentJoins flatten(binder, correlated_columns, mark_index, perform_delim); - flatten.delim_root_idx = mark_index; // first we check which logical operators have correlated expressions in the first place flatten.DetectCorrelatedExpressions(*plan); // now we push the dependent join down @@ -279,8 +278,7 @@ static unique_ptr PlanCorrelatedSubquery(Binder &binder, BoundSubque CreateDuplicateEliminatedJoin(correlated_columns, JoinType::MARK, std::move(root), perform_delim); delim_join->mark_index = mark_index; // RHS - FlattenDependentJoins flatten(binder, correlated_columns, perform_delim, true); - flatten.delim_root_idx = mark_index; + FlattenDependentJoins flatten(binder, correlated_columns, mark_index, perform_delim, true); flatten.DetectCorrelatedExpressions(*plan); auto dependent_join = flatten.PushDownDependentJoin(std::move(plan)); @@ -308,8 +306,7 @@ static unique_ptr PlanCorrelatedSubquery(Binder &binder, BoundSubque CreateDuplicateEliminatedJoin(correlated_columns, JoinType::MARK, std::move(root), perform_delim); delim_join->mark_index = mark_index; // RHS - FlattenDependentJoins flatten(binder, correlated_columns, true, true); - flatten.delim_root_idx = mark_index; + FlattenDependentJoins flatten(binder, correlated_columns, mark_index, true, true); flatten.DetectCorrelatedExpressions(*plan); auto dependent_join = flatten.PushDownDependentJoin(std::move(plan)); @@ -431,8 +428,7 @@ unique_ptr Binder::PlanLateralJoin(unique_ptr auto delim_join = CreateDuplicateEliminatedJoin(correlated, join_type, std::move(left), perform_delim); delim_join->mark_index = delim_idx; - FlattenDependentJoins flatten(*this, correlated, perform_delim); - flatten.delim_root_idx = delim_idx; + FlattenDependentJoins flatten(*this, correlated, delim_idx, perform_delim); // first we check which logical operators have correlated expressions in the first place flatten.DetectCorrelatedExpressions(*right, true); diff --git a/src/planner/subquery/flatten_dependent_join.cpp b/src/planner/subquery/flatten_dependent_join.cpp index 2b2c7d8c0896..27ae5bfcc96b 100644 --- a/src/planner/subquery/flatten_dependent_join.cpp +++ b/src/planner/subquery/flatten_dependent_join.cpp @@ -17,9 +17,9 @@ namespace duckdb { FlattenDependentJoins::FlattenDependentJoins(Binder &binder, const vector &correlated, - bool perform_delim, bool any_join) + idx_t delim_root_idx, bool perform_delim, bool any_join) : binder(binder), delim_offset(DConstants::INVALID_INDEX), correlated_columns(correlated), - perform_delim(perform_delim), any_join(any_join) { + perform_delim(perform_delim), any_join(any_join), delim_root_idx(delim_root_idx) { for (idx_t i = 0; i < correlated_columns.size(); i++) { auto &col = correlated_columns[i]; correlated_map[col.binding] = i; @@ -145,8 +145,7 @@ unique_ptr FlattenDependentJoins::PushDownDependentJoinInternal auto left_columns = plan->GetColumnBindings().size(); delim_offset = left_columns; data_offset = 0; - delim_scan = make_uniq(delim_index, delim_types); - delim_scan->delim_idx = optional_idx(delim_root_idx); + delim_scan = make_uniq(delim_index, delim_types, optional_idx(delim_root_idx)); if (plan->type == LogicalOperatorType::LOGICAL_PROJECTION) { // we want to keep the logical projection for positionality. exit_projection = true; @@ -269,8 +268,7 @@ unique_ptr FlattenDependentJoins::PushDownDependentJoinInternal } } auto left_index = binder.GenerateTableIndex(); - delim_scan = make_uniq(left_index, delim_types); - delim_scan->delim_idx = optional_idx(delim_root_idx); + delim_scan = make_uniq(left_index, delim_types, optional_idx(delim_root_idx)); join->children.push_back(std::move(delim_scan)); join->children.push_back(std::move(plan)); for (idx_t i = 0; i < new_group_count; i++) { diff --git a/src/storage/serialization/serialize_logical_operator.cpp b/src/storage/serialization/serialize_logical_operator.cpp index 5dc3beba346a..533443da3f6d 100644 --- a/src/storage/serialization/serialize_logical_operator.cpp +++ b/src/storage/serialization/serialize_logical_operator.cpp @@ -383,8 +383,8 @@ void LogicalDelimGet::Serialize(Serializer &serializer) const { unique_ptr LogicalDelimGet::Deserialize(Deserializer &deserializer) { auto table_index = deserializer.ReadPropertyWithDefault(200, "table_index"); auto chunk_types = deserializer.ReadPropertyWithDefault>(201, "chunk_types"); - auto result = duckdb::unique_ptr(new LogicalDelimGet(table_index, std::move(chunk_types))); - deserializer.ReadPropertyWithDefault(202, "delim_idx", result->delim_idx, optional_idx()); + auto delim_idx = deserializer.ReadPropertyWithDefault(202, "delim_idx", optional_idx()); + auto result = duckdb::unique_ptr(new LogicalDelimGet(table_index, std::move(chunk_types), delim_idx)); return std::move(result); }