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

Public Member Functions

 WindowSelfJoinOptimizer (Optimizer &optimizer)
unique_ptr< LogicalOperator > Optimize (unique_ptr< LogicalOperator > op)

Private Member Functions

bool CanOptimize (const BoundWindowExpression &w_expr, const BoundWindowExpression &w_expr0) const
unique_ptr< LogicalOperator > OptimizeInternal (unique_ptr< LogicalOperator > op, ColumnBindingReplacer &replacer)

Private Attributes

Optimizer & optimizer

Constructor & Destructor Documentation

◆ WindowSelfJoinOptimizer()

duckdb::WindowSelfJoinOptimizer::WindowSelfJoinOptimizer ( Optimizer & optimizer)
explicit
327602 : optimizer(optimizer) {
327603}

Member Function Documentation

◆ Optimize()

unique_ptr< LogicalOperator > duckdb::WindowSelfJoinOptimizer::Optimize ( unique_ptr< LogicalOperator > op)
327605 {
327606 ColumnBindingReplacer replacer;
327607 op = OptimizeInternal(std::move(op), replacer);
327608 if (!replacer.replacement_bindings.empty()) {
327609 replacer.VisitOperator(*op);
327610 }
327611 return op;
327612}

◆ CanOptimize()

bool duckdb::WindowSelfJoinOptimizer::CanOptimize ( const BoundWindowExpression & w_expr,
const BoundWindowExpression & w_expr0 ) const
private
327615 {
327616 if (w_expr.type != ExpressionType::WINDOW_AGGREGATE) {
327617 return false;
327618 }
327619 if (!w_expr.orders.empty()) {
327620 return false;
327621 }
327622 if (w_expr.partitions.empty()) {
327623 return false;
327624 }
327625 if (w_expr.exclude_clause != WindowExcludeMode::NO_OTHER) {
327626 return false;
327627 }
327628 if (!w_expr.PartitionsAreEquivalent(w_expr0)) {
327629 return false;
327630 }
327631
327632 // Even with no ORDER BY, we can still have a non-degenerate frame if we have ROWS framing.
327633 switch (w_expr.start) {
327634 case WindowBoundary::UNBOUNDED_PRECEDING:
327635 case WindowBoundary::CURRENT_ROW_RANGE:
327636 case WindowBoundary::CURRENT_ROW_GROUPS:
327637 break;
327638 default:
327639 return false;
327640 }
327641
327642 switch (w_expr.end) {
327643 case WindowBoundary::UNBOUNDED_FOLLOWING:
327644 case WindowBoundary::CURRENT_ROW_RANGE:
327645 case WindowBoundary::CURRENT_ROW_GROUPS:
327646 break;
327647 default:
327648 return false;
327649 }
327650
327651 return true;
327652}

◆ OptimizeInternal()

unique_ptr< LogicalOperator > duckdb::WindowSelfJoinOptimizer::OptimizeInternal ( unique_ptr< LogicalOperator > op,
ColumnBindingReplacer & replacer )
private
327655 {
327656 if (op->type == LogicalOperatorType::LOGICAL_WINDOW) {
327657 auto &window = op->Cast<LogicalWindow>();
327658
327659 // Check recursively
327660 window.children[0] = OptimizeInternal(std::move(window.children[0]), replacer);
327661
327662 if (!WindowSelfJoinTableRebinder::CanRebind(*window.children[0])) {
327663 return op;
327664 }
327665
327666 auto &w_expr0 = window.expressions[0]->Cast<BoundWindowExpression>();
327667 for (auto &expr : window.expressions) {
327668 auto &w_expr = expr->Cast<BoundWindowExpression>();
327669 if (!CanOptimize(w_expr, w_expr0)) {
327670 return op;
327671 }
327672 }
327673 auto &partitions = w_expr0.partitions;
327674
327675 // --- Transformation ---
327676 // try to copy the LHS
327677 unique_ptr<LogicalOperator> copy_child;
327678 try {
327679 copy_child = window.children[0]->Copy(optimizer.context);
327680 } catch (...) {
327681 // failed to copy the LHS - cannot run this optimizer
327682 return op;
327683 }
327684 auto original_child = std::move(window.children[0]);
327685
327686 // Rebind copy_child to avoid duplicate table indices
327687 WindowSelfJoinTableRebinder rebinder(optimizer);
327688 rebinder.VisitOperator(*copy_child);
327689
327690 auto aggregate_index = optimizer.binder.GenerateTableIndex();
327691 auto group_index = optimizer.binder.GenerateTableIndex();
327692
327693 vector<unique_ptr<Expression>> groups;
327694 vector<unique_ptr<Expression>> aggregates;
327695
327696 // Create Aggregate Operator
327697 for (auto &part : partitions) {
327698 auto part_copy = part->Copy();
327699 rebinder.VisitExpression(&part_copy); // Update bindings
327700 groups.push_back(std::move(part_copy));
327701 }
327702
327703 for (auto &expr : window.expressions) {
327704 auto &w_expr = expr->Cast<BoundWindowExpression>();
327705 aggregates.emplace_back(rebinder.TranslateAggregate(w_expr));
327706 }
327707
327708 // args: group_index, aggregate_index, ...
327709 auto agg_op = make_uniq<LogicalAggregate>(group_index, aggregate_index, std::move(aggregates));
327710
327711 agg_op->groups = std::move(groups);
327712 agg_op->children.push_back(std::move(copy_child));
327713 agg_op->ResolveOperatorTypes();
327714
327715 if (agg_op->types.size() <= agg_op->groups.size()) {
327716 throw InternalException("LogicalAggregate types size mismatch");
327717 }
327718
327719 // Inner Join on the partition keys
327720 auto join = make_uniq<LogicalComparisonJoin>(JoinType::INNER);
327721
327722 for (size_t i = 0; i < partitions.size(); ++i) {
327723 JoinCondition cond;
327724 cond.comparison = ExpressionType::COMPARE_NOT_DISTINCT_FROM;
327725 cond.left = partitions[i]->Copy();
327726 cond.right = make_uniq<BoundColumnRefExpression>(partitions[i]->return_type, ColumnBinding(group_index, i));
327727 join->conditions.push_back(std::move(cond));
327728 }
327729
327730 join->children.push_back(std::move(original_child));
327731 join->children.push_back(std::move(agg_op));
327732 join->ResolveOperatorTypes();
327733
327734 // Replace aggregate bindings
327735 // Old window column: (window.window_index, x)
327736 // New constant column: (aggregate_index, x)
327737 for (idx_t column_index = 0; column_index < window.expressions.size(); ++column_index) {
327738 ColumnBinding old_binding(window.window_index, column_index);
327739 ColumnBinding new_binding(aggregate_index, column_index);
327740 replacer.replacement_bindings.emplace_back(old_binding, new_binding);
327741 }
327742
327743 return std::move(join);
327744 } else if (!op->children.empty()) {
327745 for (auto &child : op->children) {
327746 child = OptimizeInternal(std::move(child), replacer);
327747 }
327748 }
327749 return op;
327750}
cv::String join(const cv::String &base, const cv::String &path)
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: