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

Public Member Functions

 CommonSubExpressionOptimizer (Binder &binder)
void VisitOperator (LogicalOperator &op) override
Public Member Functions inherited from duckdb::LogicalOperatorVisitor
virtual void VisitExpression (unique_ptr< Expression > *expression)

Private Member Functions

void CountExpressions (Expression &expr, CSEReplacementState &state)
 First iteration: count how many times each expression occurs.
void PerformCSEReplacement (unique_ptr< Expression > &expr, CSEReplacementState &state)
 Second iteration: perform the actual replacement of the duplicate expressions with common subexpressions nodes.
void ExtractCommonSubExpresions (LogicalOperator &op)
 Main method to extract common subexpressions.

Private Attributes

Binder & binder

Additional Inherited Members

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 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 (BoundColumnRefExpression &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 (BoundReferenceExpression &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)

Detailed Description

The CommonSubExpression optimizer traverses the expressions of a LogicalOperator to look for duplicate expressions if there are any, it pushes a projection under the operator that resolves these expressions

Constructor & Destructor Documentation

◆ CommonSubExpressionOptimizer()

duckdb::CommonSubExpressionOptimizer::CommonSubExpressionOptimizer ( Binder & binder)
inlineexplicit
306536 : binder(binder) {
306537 }

Member Function Documentation

◆ VisitOperator()

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

Reimplemented from duckdb::LogicalOperatorVisitor.

306591 {
306592 switch (op.type) {
306593 case LogicalOperatorType::LOGICAL_PROJECTION:
306594 case LogicalOperatorType::LOGICAL_AGGREGATE_AND_GROUP_BY:
306596 break;
306597 default:
306598 break;
306599 }
306600 LogicalOperatorVisitor::VisitOperator(op);
306601}
void ExtractCommonSubExpresions(LogicalOperator &op)
Main method to extract common subexpressions.
Definition duckdb.cpp:306695

◆ CountExpressions()

void duckdb::CommonSubExpressionOptimizer::CountExpressions ( Expression & expr,
CSEReplacementState & state )
private

First iteration: count how many times each expression occurs.

306603 {
306604 // we only consider expressions with children for CSE elimination
306605 switch (expr.GetExpressionClass()) {
306606 case ExpressionClass::BOUND_COLUMN_REF:
306607 case ExpressionClass::BOUND_CONSTANT:
306608 case ExpressionClass::BOUND_PARAMETER:
306609 return;
306610 default:
306611 break;
306612 }
306613 if (expr.GetExpressionClass() != ExpressionClass::BOUND_AGGREGATE && !expr.IsVolatile()) {
306614 // we can't move aggregates to a projection, so we only consider the children of the aggregate
306615 auto node = state.expression_count.find(expr);
306616 if (node == state.expression_count.end()) {
306617 // first time we encounter this expression, insert this node with [count = 1]
306618 // but only if it is not an interior argument of a short circuit sensitive expression.
306619 if (!state.short_circuited) {
306620 state.expression_count[expr] = CSENode();
306621 }
306622 } else {
306623 // we encountered this expression before, increment the occurrence count
306624 node->second.count++;
306625 }
306626 }
306627
306628 // If we have a function that uses short circuiting, then we can only extract CSEs from the leftmost
306629 // side of the argument tree (child_no == 0)
306630 switch (expr.GetExpressionClass()) {
306631 case ExpressionClass::BOUND_CONJUNCTION:
306632 case ExpressionClass::BOUND_CASE: {
306633 // Save the short circuit reference
306634 const auto save_short_circuit = state.short_circuited;
306635 ExpressionIterator::EnumerateChildren(expr, [&](Expression &child) {
306636 CountExpressions(child, state);
306637 state.short_circuited = true;
306638 });
306639 state.short_circuited = save_short_circuit;
306640 break;
306641 }
306642 default:
306643 // recursively count the children
306644 ExpressionIterator::EnumerateChildren(expr, [&](Expression &child) { CountExpressions(child, state); });
306645 break;
306646 }
306647}
void CountExpressions(Expression &expr, CSEReplacementState &state)
First iteration: count how many times each expression occurs.
Definition duckdb.cpp:306603
Here is the call graph for this function:
Here is the caller graph for this function:

◆ PerformCSEReplacement()

void duckdb::CommonSubExpressionOptimizer::PerformCSEReplacement ( unique_ptr< Expression > & expr,
CSEReplacementState & state )
private

Second iteration: perform the actual replacement of the duplicate expressions with common subexpressions nodes.

306649 {
306650 Expression &expr = *expr_ptr;
306651 if (expr.GetExpressionClass() == ExpressionClass::BOUND_COLUMN_REF) {
306652 auto &bound_column_ref = expr.Cast<BoundColumnRefExpression>();
306653 // bound column ref, check if this one has already been recorded in the expression list
306654 auto column_entry = state.column_map.find(bound_column_ref.binding);
306655 if (column_entry == state.column_map.end()) {
306656 // not there yet: push the expression
306657 idx_t new_column_index = state.expressions.size();
306658 state.column_map[bound_column_ref.binding] = new_column_index;
306659 state.expressions.push_back(make_uniq<BoundColumnRefExpression>(
306660 bound_column_ref.GetAlias(), bound_column_ref.return_type, bound_column_ref.binding));
306661 bound_column_ref.binding = ColumnBinding(state.projection_index, new_column_index);
306662 } else {
306663 // else: just update the column binding!
306664 bound_column_ref.binding = ColumnBinding(state.projection_index, column_entry->second);
306665 }
306666 return;
306667 }
306668 // check if this child is eligible for CSE elimination
306669 if (state.expression_count.find(expr) != state.expression_count.end()) {
306670 auto &node = state.expression_count[expr];
306671 if (node.count > 1) {
306672 // this expression occurs more than once! push it into the projection
306673 // check if it has already been pushed into the projection
306674 auto alias = expr.GetAlias();
306675 auto type = expr.return_type;
306676 if (!node.column_index.IsValid()) {
306677 // has not been pushed yet: push it
306678 node.column_index = state.expressions.size();
306679 state.expressions.push_back(std::move(expr_ptr));
306680 } else {
306681 state.cached_expressions.push_back(std::move(expr_ptr));
306682 }
306683 // replace the original expression with a bound column ref
306684 expr_ptr = make_uniq<BoundColumnRefExpression>(
306685 alias, type, ColumnBinding(state.projection_index, node.column_index.GetIndex()));
306686 return;
306687 }
306688 }
306689 // this expression only occurs once, we can't perform CSE elimination
306690 // look into the children to see if we can replace them
306691 ExpressionIterator::EnumerateChildren(expr,
306692 [&](unique_ptr<Expression> &child) { PerformCSEReplacement(child, state); });
306693}
void PerformCSEReplacement(unique_ptr< Expression > &expr, CSEReplacementState &state)
Second iteration: perform the actual replacement of the duplicate expressions with common subexpressi...
Definition duckdb.cpp:306649
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:

◆ ExtractCommonSubExpresions()

void duckdb::CommonSubExpressionOptimizer::ExtractCommonSubExpresions ( LogicalOperator & op)
private

Main method to extract common subexpressions.

306695 {
306696 D_ASSERT(op.children.size() == 1);
306697
306698 // first we count for each expression with children how many types it occurs
306699 CSEReplacementState state;
306700 LogicalOperatorVisitor::EnumerateExpressions(
306701 op, [&](unique_ptr<Expression> *child) { CountExpressions(**child, state); });
306702 // check if there are any expressions to extract
306703 bool perform_replacement = false;
306704 for (auto &expr : state.expression_count) {
306705 if (expr.second.count > 1) {
306706 perform_replacement = true;
306707 break;
306708 }
306709 }
306710 if (!perform_replacement) {
306711 // no CSEs to extract
306712 return;
306713 }
306714 state.projection_index = binder.GenerateTableIndex();
306715 // we found common subexpressions to extract
306716 // now we iterate over all the expressions and perform the actual CSE elimination
306717
306718 LogicalOperatorVisitor::EnumerateExpressions(
306719 op, [&](unique_ptr<Expression> *child) { PerformCSEReplacement(*child, state); });
306720 D_ASSERT(state.expressions.size() > 0);
306721 // create a projection node as the child of this node
306722 auto projection = make_uniq<LogicalProjection>(state.projection_index, std::move(state.expressions));
306723 if (op.children[0]->has_estimated_cardinality) {
306724 projection->SetEstimatedCardinality(op.children[0]->estimated_cardinality);
306725 }
306726 projection->children.push_back(std::move(op.children[0]));
306727 op.children[0] = std::move(projection);
306728}
Here is the call graph for this function:

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