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::SumRewriterOptimizer Class Reference

Rewrites SUM(x + C) into SUM(x) + C * COUNT(x). More...

Inheritance diagram for duckdb::SumRewriterOptimizer:
Collaboration diagram for duckdb::SumRewriterOptimizer:

Public Member Functions

 SumRewriterOptimizer (Optimizer &optimizer)
void Optimize (unique_ptr< LogicalOperator > &op)
void VisitOperator (LogicalOperator &op) override
Public Member Functions inherited from duckdb::LogicalOperatorVisitor
virtual void VisitExpression (unique_ptr< Expression > *expression)

Private Member Functions

void StandardVisitOperator (LogicalOperator &op)
unique_ptr< Expression > VisitReplace (BoundColumnRefExpression &expr, unique_ptr< Expression > *expr_ptr) override
void RewriteSums (unique_ptr< LogicalOperator > &aggr)

Private Attributes

Optimizer & optimizer
column_binding_map_t< ColumnBinding > aggregate_map
unique_ptr< ExpressionMatcher > sum_matcher

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 (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

Rewrites SUM(x + C) into SUM(x) + C * COUNT(x).

Constructor & Destructor Documentation

◆ SumRewriterOptimizer()

duckdb::SumRewriterOptimizer::SumRewriterOptimizer ( Optimizer & optimizer)
explicit
325509 : optimizer(optimizer) {
325510 // set up an expression matcher that detects SUM(x + C) or SUM(C + x)
325511 auto op = make_uniq<AggregateExpressionMatcher>();
325512 op->function = make_uniq<SpecificFunctionMatcher>("sum");
325513 op->policy = SetMatcher::Policy::UNORDERED;
325514
325515 auto arithmetic = make_uniq<FunctionExpressionMatcher>();
325516 // handle X + C where
325517 arithmetic->function = make_uniq<SpecificFunctionMatcher>("+");
325518 // we match only on integral numeric types
325519 arithmetic->type = make_uniq<IntegerTypeMatcher>();
325520 auto child_constant_matcher = make_uniq<ConstantExpressionMatcher>();
325521 auto child_expression_matcher = make_uniq<StableExpressionMatcher>();
325522 child_constant_matcher->type = GetSmallIntegerTypesMatcher();
325523 child_expression_matcher->type = GetSmallIntegerTypesMatcher();
325524 arithmetic->matchers.push_back(std::move(child_constant_matcher));
325525 arithmetic->matchers.push_back(std::move(child_expression_matcher));
325526 arithmetic->policy = SetMatcher::Policy::SOME;
325527 op->matchers.push_back(std::move(arithmetic));
325528
325529 sum_matcher = std::move(op);
325530}
@ UNORDERED
All entries have to be matched, but the order of the matches does not matter.
Definition duckdb.cpp:69123
@ SOME
Only some entries have to be matched, the order of the matches does not matter.
Definition duckdb.cpp:69125

◆ ~SumRewriterOptimizer()

duckdb::SumRewriterOptimizer::~SumRewriterOptimizer ( )
override
325532 {
325533}

Member Function Documentation

◆ Optimize()

void duckdb::SumRewriterOptimizer::Optimize ( unique_ptr< LogicalOperator > & op)
325535 {
325536 if (op->type == LogicalOperatorType::LOGICAL_AGGREGATE_AND_GROUP_BY) {
325537 RewriteSums(op);
325538 }
325539 VisitOperator(*op);
325540}

◆ VisitOperator()

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

Reimplemented from duckdb::LogicalOperatorVisitor.

325551 {
325552 switch (op.type) {
325553 case LogicalOperatorType::LOGICAL_UNION:
325554 case LogicalOperatorType::LOGICAL_EXCEPT:
325555 case LogicalOperatorType::LOGICAL_INTERSECT:
325556 case LogicalOperatorType::LOGICAL_MATERIALIZED_CTE:
325557 case LogicalOperatorType::LOGICAL_PROJECTION: {
325558 SumRewriterOptimizer sum_rewriter(optimizer);
325559 sum_rewriter.StandardVisitOperator(op);
325560 return;
325561 }
325562 default:
325563 break;
325564 }
325565
325566 StandardVisitOperator(op);
325567}

◆ StandardVisitOperator()

void duckdb::SumRewriterOptimizer::StandardVisitOperator ( LogicalOperator & op)
private
325542 {
325543 for (auto &child : op.children) {
325544 Optimize(child);
325545 }
325546 if (!aggregate_map.empty()) {
325548 }
325549}
void VisitOperatorExpressions(LogicalOperator &op)
Definition duckdb.cpp:381188

◆ VisitReplace()

unique_ptr< Expression > duckdb::SumRewriterOptimizer::VisitReplace ( BoundColumnRefExpression & expr,
unique_ptr< Expression > * expr_ptr )
overrideprivatevirtual

Reimplemented from duckdb::LogicalOperatorVisitor.

325570 {
325571 // check if this column ref points to an aggregate that was remapped; if it does we remap it
325572 auto entry = aggregate_map.find(expr.binding);
325573 if (entry != aggregate_map.end()) {
325574 expr.binding = entry->second;
325575 }
325576 return nullptr;
325577}

◆ RewriteSums()

void duckdb::SumRewriterOptimizer::RewriteSums ( unique_ptr< LogicalOperator > & aggr)
private
325579 {
325580 auto &aggr = op->Cast<LogicalAggregate>();
325581 if (!aggr.groups.empty()) {
325582 return;
325583 }
325584
325585 unordered_set<idx_t> rewrote_map;
325586 vector<unique_ptr<Expression>> constants;
325587 idx_t aggr_count = aggr.expressions.size();
325588 for (idx_t i = 0; i < aggr_count; ++i) {
325589 auto &expr = aggr.expressions[i];
325590 vector<reference<Expression>> bindings;
325591 if (!sum_matcher->Match(*expr, bindings)) {
325592 continue;
325593 }
325594 // found SUM(x + C)
325595 auto &sum = bindings[0].get().Cast<BoundAggregateExpression>();
325596 auto &addition = bindings[1].get().Cast<BoundFunctionExpression>();
325597 idx_t const_idx = addition.children[0]->GetExpressionType() == ExpressionType::VALUE_CONSTANT ? 0 : 1;
325598 auto const_expr = std::move(addition.children[const_idx]);
325599 auto main_expr = std::move(addition.children[1 - const_idx]);
325600
325601 // turn this into SUM(x)
325602 sum.children[0] = main_expr->Copy();
325603
325604 // push a new aggregate - COUNT(x)
325605 FunctionBinder function_binder(optimizer.context);
325606
325607 auto count_fun = CountFunctionBase::GetFunction();
325608 vector<unique_ptr<Expression>> children;
325609 children.push_back(std::move(main_expr));
325610 auto count_aggr =
325611 function_binder.BindAggregateFunction(count_fun, std::move(children), nullptr, AggregateType::NON_DISTINCT);
325612 aggr.expressions.push_back(std::move(count_aggr));
325613 constants.push_back(std::move(const_expr));
325614 rewrote_map.insert(i);
325615 }
325616 if (rewrote_map.empty()) {
325617 return;
325618 }
325619 vector<unique_ptr<Expression>> projection_expressions;
325620 // we rewrote aggregates - we need to push a projection in which we re-compute the original result
325621 idx_t rewritten_index = 0;
325622 auto proj_index = optimizer.binder.GenerateTableIndex();
325623 for (idx_t i = 0; i < aggr_count; i++) {
325624 ColumnBinding aggregate_binding(aggr.aggregate_index, i);
325625 aggregate_map[aggregate_binding] = ColumnBinding(proj_index, i);
325626 auto &aggr_type = aggr.expressions[i]->return_type;
325627 auto aggr_ref = make_uniq<BoundColumnRefExpression>(aggr_type, aggregate_binding);
325628 if (rewrote_map.find(i) == rewrote_map.end()) {
325629 // not rewritten - just push a reference
325630 projection_expressions.push_back(std::move(aggr_ref));
325631 continue;
325632 }
325633 // rewritten - need to compute the final result
325634 idx_t count_idx = aggr_count + rewritten_index;
325635 ColumnBinding count_binding(aggr.aggregate_index, count_idx);
325636 auto count_ref = make_uniq<BoundColumnRefExpression>(aggr.expressions[count_idx]->return_type, count_binding);
325637
325638 // cast the count to the sum type
325639 auto cast_count = BoundCastExpression::AddCastToType(optimizer.context, std::move(count_ref), aggr_type);
325640 auto const_expr =
325641 BoundCastExpression::AddCastToType(optimizer.context, std::move(constants[rewritten_index]), aggr_type);
325642
325643 // bind the multiplication
325644 auto multiply = optimizer.BindScalarFunction("*", std::move(cast_count), std::move(const_expr));
325645
325646 // add it to the sum
325647 auto final_result = optimizer.BindScalarFunction("+", std::move(aggr_ref), std::move(multiply));
325648 projection_expressions.push_back(std::move(final_result));
325649
325650 rewritten_index++;
325651 }
325652
325653 // push the projection to replace the aggregate
325654 auto proj = make_uniq<LogicalProjection>(proj_index, std::move(projection_expressions));
325655 if (op->has_estimated_cardinality) {
325656 proj->SetEstimatedCardinality(op->estimated_cardinality);
325657 }
325658 proj->children.push_back(std::move(op));
325659 op = std::move(proj);
325660}
static DUCKDB_API unique_ptr< Expression > AddCastToType(ClientContext &context, unique_ptr< Expression > expr, const LogicalType &target_type, bool try_cast=false)
Cast an expression to the specified SQL type if required.
Definition duckdb.cpp:375869
Scalar sum(InputArray src)
void multiply(InputArray src1, InputArray src2, OutputArray dst, double scale=1, int dtype=-1)
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

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