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::ColumnLifetimeAnalyzer Class Reference
Inheritance diagram for duckdb::ColumnLifetimeAnalyzer:
Collaboration diagram for duckdb::ColumnLifetimeAnalyzer:

Public Member Functions

 ColumnLifetimeAnalyzer (Optimizer &optimizer_p, LogicalOperator &root_p, bool is_root=false)
void VisitOperator (LogicalOperator &op) override
Public Member Functions inherited from duckdb::LogicalOperatorVisitor
virtual void VisitExpression (unique_ptr< Expression > *expression)

Static Public Member Functions

static void ExtractColumnBindings (const Expression &expr, vector< ColumnBinding > &bindings)
Static Public Member Functions inherited from duckdb::LogicalOperatorVisitor
static void EnumerateExpressions (LogicalOperator &op, const std::function< void(unique_ptr< Expression > *child)> &callback)

Protected Member Functions

unique_ptr< Expression > VisitReplace (BoundColumnRefExpression &expr, unique_ptr< Expression > *expr_ptr) override
unique_ptr< Expression > VisitReplace (BoundReferenceExpression &expr, unique_ptr< Expression > *expr_ptr) override
Protected Member Functions inherited from duckdb::LogicalOperatorVisitor
void VisitOperatorChildren (LogicalOperator &op)
void VisitOperatorExpressions (LogicalOperator &op)
void VisitOperatorWithProjectionMapChildren (LogicalOperator &op)
 Alternatives for VisitOperatorChildren for operators that have a projection map.
void VisitChildOfOperatorWithProjectionMap (LogicalOperator &child, vector< idx_t > &projection_map)
virtual void VisitExpressionChildren (Expression &expression)
virtual unique_ptr< Expression > VisitReplace (BoundAggregateExpression &expr, unique_ptr< Expression > *expr_ptr)
virtual unique_ptr< Expression > VisitReplace (BoundBetweenExpression &expr, unique_ptr< Expression > *expr_ptr)
virtual unique_ptr< Expression > VisitReplace (BoundCaseExpression &expr, unique_ptr< Expression > *expr_ptr)
virtual unique_ptr< Expression > VisitReplace (BoundCastExpression &expr, unique_ptr< Expression > *expr_ptr)
virtual unique_ptr< Expression > VisitReplace (BoundComparisonExpression &expr, unique_ptr< Expression > *expr_ptr)
virtual unique_ptr< Expression > VisitReplace (BoundConjunctionExpression &expr, unique_ptr< Expression > *expr_ptr)
virtual unique_ptr< Expression > VisitReplace (BoundConstantExpression &expr, unique_ptr< Expression > *expr_ptr)
virtual unique_ptr< Expression > VisitReplace (BoundDefaultExpression &expr, unique_ptr< Expression > *expr_ptr)
virtual unique_ptr< Expression > VisitReplace (BoundFunctionExpression &expr, unique_ptr< Expression > *expr_ptr)
virtual unique_ptr< Expression > VisitReplace (BoundOperatorExpression &expr, unique_ptr< Expression > *expr_ptr)
virtual unique_ptr< Expression > VisitReplace (BoundSubqueryExpression &expr, unique_ptr< Expression > *expr_ptr)
virtual unique_ptr< Expression > VisitReplace (BoundParameterExpression &expr, unique_ptr< Expression > *expr_ptr)
virtual unique_ptr< Expression > VisitReplace (BoundWindowExpression &expr, unique_ptr< Expression > *expr_ptr)
virtual unique_ptr< Expression > VisitReplace (BoundUnnestExpression &expr, unique_ptr< Expression > *expr_ptr)

Private Member Functions

void VisitOperatorInternal (LogicalOperator &op)
void StandardVisitOperator (LogicalOperator &op)
void ExtractUnusedColumnBindings (const vector< ColumnBinding > &bindings, column_binding_set_t &unused_bindings)
void Verify (LogicalOperator &op)
void AddVerificationProjection (unique_ptr< LogicalOperator > &child)

Static Private Member Functions

static void GenerateProjectionMap (vector< ColumnBinding > bindings, column_binding_set_t &unused_bindings, vector< idx_t > &map)

Private Attributes

Optimizer & optimizer
LogicalOperator & root
bool everything_referenced
column_binding_set_t column_references
 The set of column references.

Detailed Description

The ColumnLifetimeAnalyzer optimizer traverses the logical operator tree and ensures that columns are removed from the plan when no longer required

Constructor & Destructor Documentation

◆ ColumnLifetimeAnalyzer()

duckdb::ColumnLifetimeAnalyzer::ColumnLifetimeAnalyzer ( Optimizer & optimizer_p,
LogicalOperator & root_p,
bool is_root = false )
inlineexplicit
303878 : optimizer(optimizer_p), root(root_p), everything_referenced(is_root) {
303879 }
bool everything_referenced
Definition duckdb.cpp:303895

Member Function Documentation

◆ VisitOperator()

void duckdb::ColumnLifetimeAnalyzer::VisitOperator ( LogicalOperator & op)
overridevirtual

When RETURNING is used, a PROJECTION is the top level operator for INSERTS, UPDATES, and DELETES We still need to project all values from these operators so the projection on top of them can select from only the table values being inserted.

Reimplemented from duckdb::LogicalOperatorVisitor.

304001 {
304002 Verify(op);
304003 if (TopN::CanOptimize(op) && op.children[0]->type == LogicalOperatorType::LOGICAL_ORDER_BY) {
304004 // Let's not mess with this, TopN is more important than projection maps
304005 // TopN does not support a projection map like Order does
304006 VisitOperatorExpressions(op); // Visit LIMIT
304007 VisitOperatorExpressions(*op.children[0]); // Visit ORDER
304008 StandardVisitOperator(*op.children[0]->children[0]); // Recurse into child of ORDER
304009 return;
304010 }
304011 switch (op.type) {
304012 case LogicalOperatorType::LOGICAL_AGGREGATE_AND_GROUP_BY: {
304013 // FIXME: groups that are not referenced can be removed from projection
304014 // recurse into the children of the aggregate
304015 ColumnLifetimeAnalyzer analyzer(optimizer, root);
304016 analyzer.StandardVisitOperator(op);
304017 return;
304018 }
304019 case LogicalOperatorType::LOGICAL_ASOF_JOIN:
304020 case LogicalOperatorType::LOGICAL_DELIM_JOIN:
304021 case LogicalOperatorType::LOGICAL_COMPARISON_JOIN: {
304022 auto &comp_join = op.Cast<LogicalComparisonJoin>();
304023 if (everything_referenced) {
304024 break;
304025 }
304026
304027 // FIXME: for now, we only push into the projection map for equality (hash) joins
304028 idx_t has_range = 0;
304029 bool prefer_range_joins = Settings::Get<PreferRangeJoinsSetting>(optimizer.context);
304030 if (!comp_join.HasEquality(has_range) || prefer_range_joins) {
304031 return;
304032 }
304033
304034 column_binding_set_t lhs_unused;
304035 column_binding_set_t rhs_unused;
304036 ExtractUnusedColumnBindings(op.children[0]->GetColumnBindings(), lhs_unused);
304037 ExtractUnusedColumnBindings(op.children[1]->GetColumnBindings(), rhs_unused);
304038
304039 StandardVisitOperator(op);
304040
304041 // then generate the projection map
304042 if (op.type != LogicalOperatorType::LOGICAL_ASOF_JOIN) {
304043 // FIXME: left_projection_map in ASOF join
304044 GenerateProjectionMap(op.children[0]->GetColumnBindings(), lhs_unused, comp_join.left_projection_map);
304045 }
304046 GenerateProjectionMap(op.children[1]->GetColumnBindings(), rhs_unused, comp_join.right_projection_map);
304047 return;
304048 }
304049 case LogicalOperatorType::LOGICAL_INSERT:
304050 case LogicalOperatorType::LOGICAL_UPDATE:
304051 case LogicalOperatorType::LOGICAL_DELETE:
304052 case LogicalOperatorType::LOGICAL_MERGE_INTO:
304056 case LogicalOperatorType::LOGICAL_GET:
304057 case LogicalOperatorType::LOGICAL_UNION:
304058 case LogicalOperatorType::LOGICAL_EXCEPT:
304059 case LogicalOperatorType::LOGICAL_INTERSECT:
304060 case LogicalOperatorType::LOGICAL_RECURSIVE_CTE:
304061 case LogicalOperatorType::LOGICAL_MATERIALIZED_CTE: {
304062 // for set operations/materialized CTEs we don't remove anything, just recursively visit the children
304063 // FIXME: for UNION we can remove unreferenced columns as long as everything_referenced is false (i.e. we
304064 // encounter a UNION node that is not preceded by a DISTINCT)
304065 ColumnLifetimeAnalyzer analyzer(optimizer, root, true);
304066 analyzer.StandardVisitOperator(op);
304067 return;
304068 }
304069 case LogicalOperatorType::LOGICAL_PROJECTION: {
304070 // then recurse into the children of this projection
304071 ColumnLifetimeAnalyzer analyzer(optimizer, root);
304072 analyzer.StandardVisitOperator(op);
304073 return;
304074 }
304075 case LogicalOperatorType::LOGICAL_ORDER_BY: {
304076 auto &order = op.Cast<LogicalOrder>();
304077 if (everything_referenced) {
304078 break;
304079 }
304080
304081 column_binding_set_t unused_bindings;
304082 ExtractUnusedColumnBindings(op.children[0]->GetColumnBindings(), unused_bindings);
304083
304084 StandardVisitOperator(op);
304085
304086 GenerateProjectionMap(op.children[0]->GetColumnBindings(), unused_bindings, order.projection_map);
304087 return;
304088 }
304089 case LogicalOperatorType::LOGICAL_DISTINCT: {
304090 // DISTINCT ON only references the expressions specified in the target list (and optional ORDER BY),
304091 auto &distinct = op.Cast<LogicalDistinct>();
304092 if (distinct.distinct_type == DistinctType::DISTINCT_ON) {
304093 auto add_bindings = [&](Expression &expr) {
304094 vector<ColumnBinding> bindings;
304095 ExtractColumnBindings(expr, bindings);
304096 for (auto &binding : bindings) {
304097 column_references.insert(binding);
304098 }
304099 };
304100 for (auto &target : distinct.distinct_targets) {
304101 if (target) {
304102 add_bindings(*target);
304103 }
304104 }
304105 if (distinct.order_by) {
304106 for (auto &order : distinct.order_by->orders) {
304107 if (order.expression) {
304108 add_bindings(*order.expression);
304109 }
304110 }
304111 }
304112 break;
304113 }
304114 // DISTINCT without targets references the entire projection list
304115 everything_referenced = true;
304116 break;
304117 }
304118 case LogicalOperatorType::LOGICAL_FILTER: {
304119 auto &filter = op.Cast<LogicalFilter>();
304120 if (everything_referenced) {
304121 break;
304122 }
304123
304124 // filter, figure out which columns are not needed after the filter
304125 column_binding_set_t unused_bindings;
304126 ExtractUnusedColumnBindings(op.children[0]->GetColumnBindings(), unused_bindings);
304127
304128 StandardVisitOperator(op);
304129
304130 // then generate the projection map
304131 GenerateProjectionMap(op.children[0]->GetColumnBindings(), unused_bindings, filter.projection_map);
304132
304133 return;
304134 }
304135 default:
304136 break;
304137 }
304138 StandardVisitOperator(op);
304139}
column_binding_set_t column_references
The set of column references.
Definition duckdb.cpp:303897
void VisitOperatorExpressions(LogicalOperator &op)
Definition duckdb.cpp:381188
static bool CanOptimize(LogicalOperator &op, optional_ptr< ClientContext > context=nullptr)
Whether we can perform the optimization on this operator.
Definition duckdb.cpp:325685
void filter(InputArray image, InputArray kernel, OutputArray output)
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237
Here is the call graph for this function:

◆ ExtractColumnBindings()

void duckdb::ColumnLifetimeAnalyzer::ExtractColumnBindings ( const Expression & expr,
vector< ColumnBinding > & bindings )
static
303996 {
303997 ExpressionIterator::VisitExpression<BoundColumnRefExpression>(
303998 expr, [&](const BoundColumnRefExpression &bound_ref) { bindings.push_back(bound_ref.binding); });
303999}

◆ VisitReplace() [1/2]

unique_ptr< Expression > duckdb::ColumnLifetimeAnalyzer::VisitReplace ( BoundColumnRefExpression & expr,
unique_ptr< Expression > * expr_ptr )
overrideprotectedvirtual

Reimplemented from duckdb::LogicalOperatorVisitor.

304212 {
304213 column_references.insert(expr.binding);
304214 return nullptr;
304215}

◆ VisitReplace() [2/2]

unique_ptr< Expression > duckdb::ColumnLifetimeAnalyzer::VisitReplace ( BoundReferenceExpression & expr,
unique_ptr< Expression > * expr_ptr )
overrideprotectedvirtual

Reimplemented from duckdb::LogicalOperatorVisitor.

304218 {
304219 // BoundReferenceExpression should not be used here yet, they only belong in the physical plan
304220 throw InternalException("BoundReferenceExpression should not be used here yet!");
304221}

◆ StandardVisitOperator()

void duckdb::ColumnLifetimeAnalyzer::StandardVisitOperator ( LogicalOperator & op)
private
303991 {
303994}
void VisitOperatorChildren(LogicalOperator &op)
Definition duckdb.cpp:380983

◆ ExtractUnusedColumnBindings()

void duckdb::ColumnLifetimeAnalyzer::ExtractUnusedColumnBindings ( const vector< ColumnBinding > & bindings,
column_binding_set_t & unused_bindings )
private
303964 {
303965 for (idx_t i = 0; i < bindings.size(); i++) {
303966 if (column_references.find(bindings[i]) == column_references.end()) {
303967 unused_bindings.insert(bindings[i]);
303968 }
303969 }
303970}

◆ GenerateProjectionMap()

void duckdb::ColumnLifetimeAnalyzer::GenerateProjectionMap ( vector< ColumnBinding > bindings,
column_binding_set_t & unused_bindings,
vector< idx_t > & map )
staticprivate
303974 {
303975 projection_map.clear();
303976 if (unused_bindings.empty()) {
303977 return;
303978 }
303979 // now iterate over the result bindings of the child
303980 for (idx_t i = 0; i < bindings.size(); i++) {
303981 // if this binding does not belong to the unused bindings, add it to the projection map
303982 if (unused_bindings.find(bindings[i]) == unused_bindings.end()) {
303983 projection_map.push_back(i);
303984 }
303985 }
303986 if (projection_map.size() == bindings.size()) {
303987 projection_map.clear();
303988 }
303989}

◆ Verify()

void duckdb::ColumnLifetimeAnalyzer::Verify ( LogicalOperator & op)
private
304141 {
304142#ifdef DEBUG
304143 if (everything_referenced) {
304144 return;
304145 }
304146 switch (op.type) {
304147 case LogicalOperatorType::LOGICAL_ASOF_JOIN:
304148 case LogicalOperatorType::LOGICAL_DELIM_JOIN:
304149 case LogicalOperatorType::LOGICAL_COMPARISON_JOIN:
304150 AddVerificationProjection(op.children[0]);
304151 if (op.Cast<LogicalComparisonJoin>().join_type != JoinType::MARK) { // Can't mess up the mark_index
304152 AddVerificationProjection(op.children[1]);
304153 }
304154 break;
304155 case LogicalOperatorType::LOGICAL_ORDER_BY:
304156 case LogicalOperatorType::LOGICAL_FILTER:
304157 AddVerificationProjection(op.children[0]);
304158 break;
304159 default:
304160 break;
304161 }
304162#endif
304163}

◆ AddVerificationProjection()

void duckdb::ColumnLifetimeAnalyzer::AddVerificationProjection ( unique_ptr< LogicalOperator > & child)
private
304165 {
304166 child->ResolveOperatorTypes();
304167 const auto child_types = child->types;
304168 const auto child_bindings = child->GetColumnBindings();
304169 const auto column_count = child_bindings.size();
304170
304171 // If our child has columns [i, j], we will generate a projection like so [NULL, j, NULL, i, NULL]
304172 const auto projection_column_count = column_count * 2 + 1;
304173 vector<unique_ptr<Expression>> expressions;
304174 expressions.reserve(projection_column_count);
304175
304176 // First fill with all NULLs
304177 for (idx_t col_idx = 0; col_idx < projection_column_count; col_idx++) {
304178 expressions.emplace_back(make_uniq<BoundConstantExpression>(Value(LogicalType::UTINYINT)));
304179 }
304180
304181 // Now place the "real" columns in their respective positions, while keeping track of which column becomes which
304182 const auto table_index = optimizer.binder.GenerateTableIndex();
304183 ColumnBindingReplacer replacer;
304184 for (idx_t col_idx = 0; col_idx < column_count; col_idx++) {
304185 const auto &old_binding = child_bindings[col_idx];
304186 const auto new_col_idx = projection_column_count - 2 - col_idx * 2;
304187 expressions[new_col_idx] = make_uniq<BoundColumnRefExpression>(child_types[col_idx], old_binding);
304188 replacer.replacement_bindings.emplace_back(old_binding, ColumnBinding(table_index, new_col_idx));
304189 }
304190
304191 // Create a projection and swap the operators accordingly
304192 auto projection = make_uniq<LogicalProjection>(table_index, std::move(expressions));
304193 if (child->has_estimated_cardinality) {
304194 projection->SetEstimatedCardinality(child->estimated_cardinality);
304195 }
304196 projection->children.emplace_back(std::move(child));
304197 child = std::move(projection);
304198
304199 // Replace references to the old binding (higher up in the plan) with references to the new binding
304200 replacer.stop_operator = child.get();
304201 replacer.VisitOperator(root);
304202
304203 // Add new bindings to column_references, else they are considered "unused"
304204 for (const auto &replacement_binding : replacer.replacement_bindings) {
304205 if (column_references.find(replacement_binding.old_binding) != column_references.end()) {
304206 column_references.insert(replacement_binding.new_binding);
304207 }
304208 }
304209}

Member Data Documentation

◆ everything_referenced

bool duckdb::ColumnLifetimeAnalyzer::everything_referenced
private

Whether or not all the columns are referenced. This happens in the case of the root expression (because the output implicitly refers all the columns below it)


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