Skip to content

Commit

Permalink
Improve handling of DELIM idx
Browse files Browse the repository at this point in the history
  • Loading branch information
kryonix committed Jul 15, 2024
1 parent 23ccd79 commit d933de6
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/include/duckdb/planner/operator/logical_delim_get.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ class LogicalDelimGet : public LogicalOperator {
static constexpr const LogicalOperatorType TYPE = LogicalOperatorType::LOGICAL_DELIM_GET;

public:
LogicalDelimGet(idx_t table_index, vector<LogicalType> types)
: LogicalOperator(LogicalOperatorType::LOGICAL_DELIM_GET), table_index(table_index) {
LogicalDelimGet(idx_t table_index, vector<LogicalType> 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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CorrelatedColumnInfo> &correlated, bool perform_delim = true,
bool any_join = false);
FlattenDependentJoins(Binder &binder, const vector<CorrelatedColumnInfo> &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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@
"default": "optional_idx()"
}
],
"constructor": ["table_index", "chunk_types"]
"constructor": ["table_index", "chunk_types", "delim_idx"]
},
{
"class": "LogicalExpressionGet",
Expand Down
12 changes: 4 additions & 8 deletions src/planner/binder/query_node/plan_subquery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,8 @@ static unique_ptr<Expression> 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
Expand All @@ -279,8 +278,7 @@ static unique_ptr<Expression> 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));

Expand Down Expand Up @@ -308,8 +306,7 @@ static unique_ptr<Expression> 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));

Expand Down Expand Up @@ -431,8 +428,7 @@ unique_ptr<LogicalOperator> Binder::PlanLateralJoin(unique_ptr<LogicalOperator>
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);
Expand Down
10 changes: 4 additions & 6 deletions src/planner/subquery/flatten_dependent_join.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
namespace duckdb {

FlattenDependentJoins::FlattenDependentJoins(Binder &binder, const vector<CorrelatedColumnInfo> &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;
Expand Down Expand Up @@ -145,8 +145,7 @@ unique_ptr<LogicalOperator> FlattenDependentJoins::PushDownDependentJoinInternal
auto left_columns = plan->GetColumnBindings().size();
delim_offset = left_columns;
data_offset = 0;
delim_scan = make_uniq<LogicalDelimGet>(delim_index, delim_types);
delim_scan->delim_idx = optional_idx(delim_root_idx);
delim_scan = make_uniq<LogicalDelimGet>(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;
Expand Down Expand Up @@ -269,8 +268,7 @@ unique_ptr<LogicalOperator> FlattenDependentJoins::PushDownDependentJoinInternal
}
}
auto left_index = binder.GenerateTableIndex();
delim_scan = make_uniq<LogicalDelimGet>(left_index, delim_types);
delim_scan->delim_idx = optional_idx(delim_root_idx);
delim_scan = make_uniq<LogicalDelimGet>(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++) {
Expand Down
4 changes: 2 additions & 2 deletions src/storage/serialization/serialize_logical_operator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,8 @@ void LogicalDelimGet::Serialize(Serializer &serializer) const {
unique_ptr<LogicalOperator> LogicalDelimGet::Deserialize(Deserializer &deserializer) {
auto table_index = deserializer.ReadPropertyWithDefault<idx_t>(200, "table_index");
auto chunk_types = deserializer.ReadPropertyWithDefault<vector<LogicalType>>(201, "chunk_types");
auto result = duckdb::unique_ptr<LogicalDelimGet>(new LogicalDelimGet(table_index, std::move(chunk_types)));
deserializer.ReadPropertyWithDefault<optional_idx>(202, "delim_idx", result->delim_idx, optional_idx());
auto delim_idx = deserializer.ReadPropertyWithDefault<optional_idx>(202, "delim_idx", optional_idx());
auto result = duckdb::unique_ptr<LogicalDelimGet>(new LogicalDelimGet(table_index, std::move(chunk_types), delim_idx));
return std::move(result);
}

Expand Down

0 comments on commit d933de6

Please sign in to comment.