Autonomy Software C++ 24.5.1
Welcome to the Autonomy Software repository of the Mars Rover Design Team (MRDT) at Missouri University of Science and Technology (Missouri S&T)! API reference contains the source code and other resources for the development of the autonomy software for our Mars rover. The Autonomy Software project aims to compete in the University Rover Challenge (URC) by demonstrating advanced autonomous capabilities and robust navigation algorithms.
Loading...
Searching...
No Matches
duckdb::UnnestRewriter Class Reference
Collaboration diagram for duckdb::UnnestRewriter:

Public Member Functions

unique_ptr< LogicalOperator > Optimize (unique_ptr< LogicalOperator > op)
 Rewrite duplicate eliminated joins with UNNESTs.

Private Member Functions

void FindCandidates (unique_ptr< LogicalOperator > &root, unique_ptr< LogicalOperator > &op, vector< reference< unique_ptr< LogicalOperator > > > &candidates)
 Find delim joins that contain an UNNEST.
bool RewriteCandidate (unique_ptr< LogicalOperator > &candidate)
 Rewrite a delim join that contains an UNNEST.
void UpdateRHSBindings (unique_ptr< LogicalOperator > &plan, unique_ptr< LogicalOperator > &candidate, UnnestRewriterPlanUpdater &updater)
 Update the bindings of the RHS sequence of LOGICAL_PROJECTION(s).
void UpdateBoundUnnestBindings (UnnestRewriterPlanUpdater &updater, unique_ptr< LogicalOperator > &candidate)
 Update the bindings of the BOUND_UNNEST expression of the LOGICAL_UNNEST.
void GetDelimColumns (LogicalOperator &op)
 Store all delim columns of the delim join.
void GetLHSExpressions (LogicalOperator &op)
 Store all LHS expressions of the LOGICAL_PROJECTION.

Private Attributes

vector< ColumnBinding > delim_columns
 Keep track of the delim columns to find the correct UNNEST column.
vector< LHSBinding > lhs_bindings
 Store the column bindings of the LHS child of the LOGICAL_DELIM_JOIN.
idx_t overwritten_tbl_idx
 Stores the table index of the former child of the LOGICAL_UNNEST.
idx_t distinct_unnest_count
 The number of distinct columns to unnest.

Detailed Description

The UnnestRewriter optimizer traverses the logical operator tree and rewrites duplicate eliminated joins that contain UNNESTs by moving the UNNESTs into the projection of the SELECT

Constructor & Destructor Documentation

◆ UnnestRewriter()

duckdb::UnnestRewriter::UnnestRewriter ( )
inline
316169 {
316170 }

Member Function Documentation

◆ Optimize()

unique_ptr< LogicalOperator > duckdb::UnnestRewriter::Optimize ( unique_ptr< LogicalOperator > op)

Rewrite duplicate eliminated joins with UNNESTs.

327094 {
327095 UnnestRewriterPlanUpdater updater;
327096 vector<reference<unique_ptr<LogicalOperator>>> candidates;
327097 FindCandidates(op, op, candidates);
327098
327099 // rewrite the plan and update the bindings
327100 for (auto &candidate : candidates) {
327101 // rearrange the logical operators
327102 if (RewriteCandidate(candidate)) {
327103 updater.overwritten_tbl_idx = overwritten_tbl_idx;
327104 // update the bindings of the BOUND_UNNEST expression
327105 UpdateBoundUnnestBindings(updater, candidate);
327106 // update the sequence of LOGICAL_PROJECTION(s)
327107 UpdateRHSBindings(op, candidate, updater);
327108 // reset
327109 delim_columns.clear();
327110 lhs_bindings.clear();
327111 }
327112 }
327113
327114 return op;
327115}
vector< LHSBinding > lhs_bindings
Store the column bindings of the LHS child of the LOGICAL_DELIM_JOIN.
Definition duckdb.cpp:316194
vector< ColumnBinding > delim_columns
Keep track of the delim columns to find the correct UNNEST column.
Definition duckdb.cpp:316192
void FindCandidates(unique_ptr< LogicalOperator > &root, unique_ptr< LogicalOperator > &op, vector< reference< unique_ptr< LogicalOperator > > > &candidates)
Find delim joins that contain an UNNEST.
Definition duckdb.cpp:327117
void UpdateRHSBindings(unique_ptr< LogicalOperator > &plan, unique_ptr< LogicalOperator > &candidate, UnnestRewriterPlanUpdater &updater)
Update the bindings of the RHS sequence of LOGICAL_PROJECTION(s).
Definition duckdb.cpp:327294
bool RewriteCandidate(unique_ptr< LogicalOperator > &candidate)
Rewrite a delim join that contains an UNNEST.
Definition duckdb.cpp:327242
idx_t overwritten_tbl_idx
Stores the table index of the former child of the LOGICAL_UNNEST.
Definition duckdb.cpp:316196
void UpdateBoundUnnestBindings(UnnestRewriterPlanUpdater &updater, unique_ptr< LogicalOperator > &candidate)
Update the bindings of the BOUND_UNNEST expression of the LOGICAL_UNNEST.
Definition duckdb.cpp:327385
Here is the call graph for this function:

◆ FindCandidates()

void duckdb::UnnestRewriter::FindCandidates ( unique_ptr< LogicalOperator > & root,
unique_ptr< LogicalOperator > & op,
vector< reference< unique_ptr< LogicalOperator > > > & candidates )
private

Find delim joins that contain an UNNEST.

327118 {
327119 // search children before adding, so that we add candidates bottom-up
327120 for (auto &child : op->children) {
327121 FindCandidates(root, child, candidates);
327122 }
327123
327124 // search for operator that has a LOGICAL_DELIM_JOIN as its child
327125 if (op->children.size() != 1) {
327126 return;
327127 }
327128 if (op->children[0]->type != LogicalOperatorType::LOGICAL_DELIM_JOIN) {
327129 return;
327130 }
327131
327132 // found a delim join
327133 auto &delim_join = op->children[0]->Cast<LogicalComparisonJoin>();
327134 // only support INNER delim joins
327135 if (delim_join.join_type != JoinType::INNER) {
327136 return;
327137 }
327138 // INNER delim join must have exactly one condition
327139 if (delim_join.conditions.size() != 1) {
327140 return;
327141 }
327142
327143 // LHS child is a window
327144 idx_t delim_idx = delim_join.delim_flipped ? 1 : 0;
327145 idx_t other_idx = 1 - delim_idx;
327146 if (delim_join.children[delim_idx]->type != LogicalOperatorType::LOGICAL_WINDOW) {
327147 return;
327148 }
327149
327150 // RHS child must be projection(s) followed by an UNNEST
327151 auto curr_op = &delim_join.children[other_idx];
327152 while (curr_op->get()->type == LogicalOperatorType::LOGICAL_PROJECTION) {
327153 if (curr_op->get()->children.size() != 1) {
327154 break;
327155 }
327156 curr_op = &curr_op->get()->children[0];
327157 }
327158
327159 // pattern1: delim_get -> unnest-> projection
327160 if (curr_op->get()->type == LogicalOperatorType::LOGICAL_UNNEST &&
327161 curr_op->get()->children[0]->type == LogicalOperatorType::LOGICAL_DELIM_GET) {
327162 candidates.push_back(op);
327163 return;
327164 }
327165
327166 curr_op = &delim_join.children[other_idx];
327167 if (curr_op->get()->type == LogicalOperatorType::LOGICAL_GET) {
327168 auto &get = curr_op->get()->Cast<LogicalGet>();
327169 if (!ExpressionBinder::IsUnnestFunction(get.function.name)) {
327170 return;
327171 }
327172 // pattern2: delim_get -> projection -> table_in_out(unnest)
327173 auto &unnest_get_ref = curr_op->get()->Cast<LogicalGet>();
327174 if (unnest_get_ref.ordinality_idx.IsValid()) {
327175 // we also unnest delim_index so cannot rewrite it
327176 return;
327177 }
327178 curr_op = &curr_op->get()->children[0];
327179
327180 // find pattern2 and convert to pattern1
327181 if (curr_op->get()->type == LogicalOperatorType::LOGICAL_PROJECTION &&
327182 curr_op->get()->children[0]->type == LogicalOperatorType::LOGICAL_DELIM_GET) {
327183 // Pre-check: verify all projection expressions for unnest columns are simple
327184 // column references. With deeply nested struct paths (e.g. t.a.b.c.items),
327185 // the projection may contain struct extraction functions instead,
327186 // which we cannot rewrite. We must check before moving any unique_ptrs.
327187 {
327188 auto &pre_get = *delim_join.children[other_idx];
327189 pre_get.ResolveOperatorTypes();
327190 auto pre_columns = pre_get.GetColumnBindings();
327191 auto pre_tbl_idx = pre_get.GetTableIndex()[0];
327192 auto &pre_proj = curr_op->get()->Cast<LogicalProjection>();
327193 for (idx_t i = 0; i < pre_columns.size(); i++) {
327194 auto &col_bind = pre_columns[i];
327195 auto is_unnest_column = col_bind.table_index == pre_tbl_idx;
327196 auto is_column_ref = pre_proj.expressions[col_bind.column_index]->GetExpressionClass() ==
327197 ExpressionClass::BOUND_COLUMN_REF;
327198 if (is_unnest_column && !is_column_ref) {
327199 return;
327200 }
327201 }
327202 }
327203 auto unnest_get = std::move(delim_join.children[other_idx]);
327204 // ResolveOperatorTypes() already called in the pre-check above
327205 ColumnBindingReplacer replacer;
327206 auto unnest_get_column = unnest_get->GetColumnBindings();
327207 auto &proj = curr_op->get()->Cast<LogicalProjection>();
327208 auto delim_get = std::move(proj.children[0]);
327209 auto unnest = make_uniq<LogicalUnnest>(unnest_get->GetTableIndex()[0]);
327210 unnest->children.push_back(std::move(delim_get));
327211 delim_join.children[other_idx] = std::move(*curr_op);
327212 for (idx_t i = 0; i < unnest_get_column.size(); i++) {
327213 auto &col_bind = unnest_get_column[i];
327214 D_ASSERT(col_bind.table_index == unnest_get->GetTableIndex()[0] ||
327215 col_bind.table_index == proj.table_index);
327216 if (col_bind.table_index == unnest_get->GetTableIndex()[0]) {
327217 // Guaranteed by the pre-check above: all these expressions are BOUND_COLUMN_REF
327218 D_ASSERT(proj.expressions[col_bind.column_index]->GetExpressionClass() ==
327219 ExpressionClass::BOUND_COLUMN_REF);
327220 auto &bind_col = proj.expressions[col_bind.column_index]->Cast<BoundColumnRefExpression>();
327221 auto unnest_expr = make_uniq<BoundUnnestExpression>(unnest_get->types[i]);
327222 unnest_expr->child = proj.expressions[col_bind.column_index]->Copy();
327223 bind_col.binding = ColumnBinding(unnest->GetTableIndex()[0], bind_col.binding.column_index);
327224 unnest->expressions.push_back(std::move(unnest_expr));
327225 auto new_column_ref = ColumnBinding(bind_col.binding.table_index, unnest->expressions.size() - 1);
327226 auto unnest_ref = make_uniq<BoundColumnRefExpression>(bind_col.alias, unnest_get->types[i],
327227 new_column_ref, bind_col.depth);
327228 proj.expressions[col_bind.column_index] = std::move(unnest_ref);
327229 proj.types[col_bind.column_index] = unnest_get->types[i];
327230 replacer.replacement_bindings.push_back(ReplacementBinding(
327231 col_bind, ColumnBinding(proj.table_index, col_bind.column_index), unnest_get->types[i]));
327232 }
327233 }
327234 proj.children[0] = std::move(unnest);
327235 replacer.stop_operator = proj;
327236 replacer.VisitOperator(*root);
327237 candidates.push_back(op);
327238 }
327239 }
327240}
static bool IsUnnestFunction(const string &function_name)
Returns true if the function name is an alias for the UNNEST function.
Definition duckdb.cpp:379192
__host__ cudaTextureObject_t get() const noexcept
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RewriteCandidate()

bool duckdb::UnnestRewriter::RewriteCandidate ( unique_ptr< LogicalOperator > & candidate)
private

Rewrite a delim join that contains an UNNEST.

327242 {
327243 auto &topmost_op = *candidate;
327244 if (topmost_op.type != LogicalOperatorType::LOGICAL_PROJECTION &&
327245 topmost_op.type != LogicalOperatorType::LOGICAL_WINDOW &&
327246 topmost_op.type != LogicalOperatorType::LOGICAL_FILTER &&
327247 topmost_op.type != LogicalOperatorType::LOGICAL_AGGREGATE_AND_GROUP_BY &&
327248 topmost_op.type != LogicalOperatorType::LOGICAL_UNNEST) {
327249 return false;
327250 }
327251
327252 // get the LOGICAL_DELIM_JOIN, which is a child of the candidate
327253 D_ASSERT(topmost_op.children.size() == 1);
327254 auto &delim_join = topmost_op.children[0]->Cast<LogicalComparisonJoin>();
327255 D_ASSERT(delim_join.type == LogicalOperatorType::LOGICAL_DELIM_JOIN);
327256 GetDelimColumns(delim_join);
327257
327258 // LHS of the LOGICAL_DELIM_JOIN is a LOGICAL_WINDOW that contains a LOGICAL_PROJECTION/LOGICAL_CROSS_JOIN
327259 // this lhs_proj later becomes the child of the UNNEST
327260
327261 idx_t delim_idx = delim_join.delim_flipped ? 1 : 0;
327262 idx_t other_idx = 1 - delim_idx;
327263 auto &window = *delim_join.children[delim_idx];
327264 auto &lhs_op = window.children[0];
327265 GetLHSExpressions(*lhs_op);
327266
327267 // find the LOGICAL_UNNEST
327268 // and get the path down to the LOGICAL_UNNEST
327269 vector<unique_ptr<LogicalOperator> *> path_to_unnest;
327270 auto curr_op = &delim_join.children[other_idx];
327271 while (curr_op->get()->type == LogicalOperatorType::LOGICAL_PROJECTION) {
327272 path_to_unnest.push_back(curr_op);
327273 curr_op = &curr_op->get()->children[0];
327274 }
327275
327276 // store the table index of the child of the LOGICAL_UNNEST
327277 // then update the plan by making the lhs_proj the child of the LOGICAL_UNNEST
327278 D_ASSERT(curr_op->get()->type == LogicalOperatorType::LOGICAL_UNNEST);
327279 auto &unnest = curr_op->get()->Cast<LogicalUnnest>();
327280 D_ASSERT(unnest.children[0]->type == LogicalOperatorType::LOGICAL_DELIM_GET);
327281 overwritten_tbl_idx = unnest.children[0]->Cast<LogicalDelimGet>().table_index;
327282
327283 D_ASSERT(!unnest.children.empty());
327284 auto &delim_get = unnest.children[0]->Cast<LogicalDelimGet>();
327285 D_ASSERT(delim_get.chunk_types.size() > 1);
327286 distinct_unnest_count = delim_get.chunk_types.size();
327287 unnest.children[0] = std::move(lhs_op);
327288
327289 // replace the LOGICAL_DELIM_JOIN with its RHS child operator
327290 topmost_op.children[0] = std::move(*path_to_unnest.front());
327291 return true;
327292}
void GetDelimColumns(LogicalOperator &op)
Store all delim columns of the delim join.
Definition duckdb.cpp:327427
void GetLHSExpressions(LogicalOperator &op)
Store all LHS expressions of the LOGICAL_PROJECTION.
Definition duckdb.cpp:327438
idx_t distinct_unnest_count
The number of distinct columns to unnest.
Definition duckdb.cpp:316198
Here is the call graph for this function:
Here is the caller graph for this function:

◆ UpdateRHSBindings()

void duckdb::UnnestRewriter::UpdateRHSBindings ( unique_ptr< LogicalOperator > & plan,
unique_ptr< LogicalOperator > & candidate,
UnnestRewriterPlanUpdater & updater )
private

Update the bindings of the RHS sequence of LOGICAL_PROJECTION(s).

327295 {
327296 auto &topmost_op = *candidate;
327297 idx_t shift = lhs_bindings.size();
327298
327299 vector<unique_ptr<LogicalOperator> *> path_to_unnest;
327300 auto curr_op = &topmost_op.children[0];
327301 while (curr_op->get()->type == LogicalOperatorType::LOGICAL_PROJECTION) {
327302 path_to_unnest.push_back(curr_op);
327303 D_ASSERT(curr_op->get()->type == LogicalOperatorType::LOGICAL_PROJECTION);
327304 auto &proj = curr_op->get()->Cast<LogicalProjection>();
327305
327306 // pop the unnest columns and the delim index
327307 D_ASSERT(proj.expressions.size() > distinct_unnest_count);
327308 for (idx_t i = 0; i < distinct_unnest_count; i++) {
327309 proj.expressions.pop_back();
327310 }
327311
327312 // store all shifted current bindings
327313 idx_t tbl_idx = proj.table_index;
327314 for (idx_t i = 0; i < proj.expressions.size(); i++) {
327315 ReplaceBinding replace_binding(ColumnBinding(tbl_idx, i), ColumnBinding(tbl_idx, i + shift));
327316 updater.replace_bindings.push_back(replace_binding);
327317 }
327318
327319 curr_op = &curr_op->get()->children[0];
327320 }
327321
327322 // update all bindings by shifting them
327323 updater.VisitOperator(*plan);
327324 updater.replace_bindings.clear();
327325
327326 // update all bindings coming from the LHS to RHS bindings
327327 D_ASSERT(topmost_op.children[0]->type == LogicalOperatorType::LOGICAL_PROJECTION);
327328 auto &top_proj = topmost_op.children[0]->Cast<LogicalProjection>();
327329 for (idx_t i = 0; i < lhs_bindings.size(); i++) {
327330 ReplaceBinding replace_binding(lhs_bindings[i].binding, ColumnBinding(top_proj.table_index, i));
327331 updater.replace_bindings.push_back(replace_binding);
327332 }
327333
327334 // temporarily remove the BOUND_UNNESTs and the child of the LOGICAL_UNNEST from the plan
327335 D_ASSERT(curr_op->get()->type == LogicalOperatorType::LOGICAL_UNNEST);
327336 auto &unnest = curr_op->get()->Cast<LogicalUnnest>();
327337 vector<unique_ptr<Expression>> temp_bound_unnests;
327338 for (auto &temp_bound_unnest : unnest.expressions) {
327339 temp_bound_unnests.push_back(std::move(temp_bound_unnest));
327340 }
327341 D_ASSERT(unnest.children.size() == 1);
327342 auto temp_unnest_child = std::move(unnest.children[0]);
327343 unnest.expressions.clear();
327344 unnest.children.clear();
327345 // update the bindings of the plan
327346 updater.VisitOperator(*plan);
327347 updater.replace_bindings.clear();
327348 // add the children again
327349 for (auto &temp_bound_unnest : temp_bound_unnests) {
327350 unnest.expressions.push_back(std::move(temp_bound_unnest));
327351 }
327352 unnest.children.push_back(std::move(temp_unnest_child));
327353
327354 // add the LHS expressions to each LOGICAL_PROJECTION
327355 for (idx_t i = path_to_unnest.size(); i > 0; i--) {
327356 D_ASSERT(path_to_unnest[i - 1]->get()->type == LogicalOperatorType::LOGICAL_PROJECTION);
327357 auto &proj = path_to_unnest[i - 1]->get()->Cast<LogicalProjection>();
327358
327359 // temporarily store the existing expressions
327360 vector<unique_ptr<Expression>> existing_expressions;
327361 for (idx_t expr_idx = 0; expr_idx < proj.expressions.size(); expr_idx++) {
327362 existing_expressions.push_back(std::move(proj.expressions[expr_idx]));
327363 }
327364
327365 proj.expressions.clear();
327366
327367 // add the new expressions
327368 for (idx_t expr_idx = 0; expr_idx < lhs_bindings.size(); expr_idx++) {
327369 auto new_expr = make_uniq<BoundColumnRefExpression>(
327370 lhs_bindings[expr_idx].alias, lhs_bindings[expr_idx].type, lhs_bindings[expr_idx].binding);
327371 proj.expressions.push_back(std::move(new_expr));
327372
327373 // update the table index
327374 lhs_bindings[expr_idx].binding.table_index = proj.table_index;
327375 lhs_bindings[expr_idx].binding.column_index = expr_idx;
327376 }
327377
327378 // add the existing expressions again
327379 for (idx_t expr_idx = 0; expr_idx < existing_expressions.size(); expr_idx++) {
327380 proj.expressions.push_back(std::move(existing_expressions[expr_idx]));
327381 }
327382 }
327383}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ UpdateBoundUnnestBindings()

void duckdb::UnnestRewriter::UpdateBoundUnnestBindings ( UnnestRewriterPlanUpdater & updater,
unique_ptr< LogicalOperator > & candidate )
private

Update the bindings of the BOUND_UNNEST expression of the LOGICAL_UNNEST.

327386 {
327387 auto &topmost_op = *candidate;
327388
327389 // traverse LOGICAL_PROJECTION(s)
327390 auto curr_op = &topmost_op.children[0];
327391 while (curr_op->get()->type == LogicalOperatorType::LOGICAL_PROJECTION) {
327392 curr_op = &curr_op->get()->children[0];
327393 }
327394
327395 // found the LOGICAL_UNNEST
327396 D_ASSERT(curr_op->get()->type == LogicalOperatorType::LOGICAL_UNNEST);
327397 auto &unnest = curr_op->get()->Cast<LogicalUnnest>();
327398
327399 D_ASSERT(unnest.children.size() == 1);
327400 auto unnest_cols = unnest.children[0]->GetColumnBindings();
327401
327402 for (idx_t i = 0; i < delim_columns.size(); i++) {
327403 auto delim_binding = delim_columns[i];
327404
327405 auto unnest_it = unnest_cols.begin();
327406 while (unnest_it != unnest_cols.end()) {
327407 auto unnest_binding = *unnest_it;
327408
327409 if (delim_binding.table_index == unnest_binding.table_index) {
327410 unnest_binding.table_index = overwritten_tbl_idx;
327411 unnest_binding.column_index = i;
327412 updater.replace_bindings.emplace_back(unnest_binding, delim_binding);
327413 unnest_cols.erase(unnest_it);
327414 break;
327415 }
327416 unnest_it++;
327417 }
327418 }
327419
327420 // update bindings
327421 for (auto &unnest_expr : unnest.expressions) {
327422 updater.VisitExpression(&unnest_expr);
327423 }
327424 updater.replace_bindings.clear();
327425}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetDelimColumns()

void duckdb::UnnestRewriter::GetDelimColumns ( LogicalOperator & op)
private

Store all delim columns of the delim join.

327427 {
327428 D_ASSERT(op.type == LogicalOperatorType::LOGICAL_DELIM_JOIN);
327429 auto &delim_join = op.Cast<LogicalComparisonJoin>();
327430 for (idx_t i = 0; i < delim_join.duplicate_eliminated_columns.size(); i++) {
327431 auto &expr = *delim_join.duplicate_eliminated_columns[i];
327432 D_ASSERT(expr.GetExpressionType() == ExpressionType::BOUND_COLUMN_REF);
327433 auto &bound_colref_expr = expr.Cast<BoundColumnRefExpression>();
327434 delim_columns.push_back(bound_colref_expr.binding);
327435 }
327436}
Here is the caller graph for this function:

◆ GetLHSExpressions()

void duckdb::UnnestRewriter::GetLHSExpressions ( LogicalOperator & op)
private

Store all LHS expressions of the LOGICAL_PROJECTION.

327438 {
327439 op.ResolveOperatorTypes();
327440 auto col_bindings = op.GetColumnBindings();
327441 D_ASSERT(op.types.size() == col_bindings.size());
327442
327443 bool set_alias = false;
327444 // we can easily extract the alias for LOGICAL_PROJECTION(s)
327445 if (op.type == LogicalOperatorType::LOGICAL_PROJECTION) {
327446 auto &proj = op.Cast<LogicalProjection>();
327447 if (proj.expressions.size() == op.types.size()) {
327448 set_alias = true;
327449 }
327450 }
327451
327452 for (idx_t i = 0; i < op.types.size(); i++) {
327453 lhs_bindings.emplace_back(col_bindings[i], op.types[i]);
327454 if (set_alias) {
327455 auto &proj = op.Cast<LogicalProjection>();
327456 lhs_bindings.back().alias = proj.expressions[i]->GetAlias();
327457 }
327458 }
327459}
Here is the call graph for this function:
Here is the caller graph for this function:

The documentation for this class was generated from the following file: