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

Public Member Functions

 TopN (ClientContext &context)
unique_ptr< LogicalOperator > Optimize (unique_ptr< LogicalOperator > op)
 Optimize ORDER BY + LIMIT to TopN.

Static Public Member Functions

static bool CanOptimize (LogicalOperator &op, optional_ptr< ClientContext > context=nullptr)
 Whether we can perform the optimization on this operator.

Private Member Functions

void PushdownDynamicFilters (LogicalTopN &op)

Private Attributes

ClientContext & context

Constructor & Destructor Documentation

◆ TopN()

duckdb::TopN::TopN ( ClientContext & context)
explicit
325682 : context(context_p) {
325683}

Member Function Documentation

◆ Optimize()

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

Optimize ORDER BY + LIMIT to TopN.

325799 {
325800 if (CanOptimize(*op, &context)) {
325801 vector<unique_ptr<LogicalOperator>> projections;
325802
325803 // traverse operator tree and collect all projection nodes until we reach
325804 // the order by operator
325805
325806 auto child = std::move(op->children[0]);
325807 // collect all projections until we get to the order by
325808 while (child->type == LogicalOperatorType::LOGICAL_PROJECTION) {
325809 D_ASSERT(!child->children.empty());
325810 auto tmp = std::move(child->children[0]);
325811 projections.push_back(std::move(child));
325812 child = std::move(tmp);
325813 }
325814 D_ASSERT(child->type == LogicalOperatorType::LOGICAL_ORDER_BY);
325815 auto &order_by = child->Cast<LogicalOrder>();
325816
325817 // Move order by operator into children of limit operator
325818 op->children[0] = std::move(child);
325819
325820 auto &limit = op->Cast<LogicalLimit>();
325821 auto limit_val = limit.limit_val.GetConstantValue();
325822 idx_t offset_val = 0;
325823 if (limit.offset_val.Type() == LimitNodeType::CONSTANT_VALUE) {
325824 offset_val = limit.offset_val.GetConstantValue();
325825 }
325826 auto topn = make_uniq<LogicalTopN>(std::move(order_by.orders), limit_val, offset_val);
325827 topn->AddChild(std::move(order_by.children[0]));
325828 auto cardinality = limit_val;
325829 if (topn->children[0]->has_estimated_cardinality && topn->children[0]->estimated_cardinality < limit_val) {
325830 cardinality = topn->children[0]->estimated_cardinality;
325831 }
325832 topn->SetEstimatedCardinality(cardinality);
325833 op = std::move(topn);
325834
325835 // reconstruct all projection nodes above limit operator
325836 while (!projections.empty()) {
325837 auto node = std::move(projections.back());
325838 node->children[0] = std::move(op);
325839 op = std::move(node);
325840 projections.pop_back();
325841 }
325842 }
325843 if (op->type == LogicalOperatorType::LOGICAL_TOP_N) {
325844 PushdownDynamicFilters(op->Cast<LogicalTopN>());
325845 }
325846
325847 for (auto &child : op->children) {
325848 child = Optimize(std::move(child));
325849 }
325850 return op;
325851}
unique_ptr< LogicalOperator > Optimize(unique_ptr< LogicalOperator > op)
Optimize ORDER BY + LIMIT to TopN.
Definition duckdb.cpp:325799
static bool CanOptimize(LogicalOperator &op, optional_ptr< ClientContext > context=nullptr)
Whether we can perform the optimization on this operator.
Definition duckdb.cpp:325685
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:

◆ CanOptimize()

bool duckdb::TopN::CanOptimize ( LogicalOperator & op,
optional_ptr< ClientContext > context = nullptr )
static

Whether we can perform the optimization on this operator.

325685 {
325686 if (op.type == LogicalOperatorType::LOGICAL_LIMIT) {
325687 auto &limit = op.Cast<LogicalLimit>();
325688
325689 if (limit.limit_val.Type() != LimitNodeType::CONSTANT_VALUE) {
325690 // we need LIMIT to be present AND be a constant value for us to be able to use Top-N
325691 return false;
325692 }
325693 if (limit.offset_val.Type() == LimitNodeType::EXPRESSION_VALUE) {
325694 // we need offset to be either not set (i.e. limit without offset) OR have offset be
325695 return false;
325696 }
325697
325698 auto child_op = op.children[0].get();
325699 if (context) {
325700 // estimate child cardinality if the context is available
325701 child_op->EstimateCardinality(*context);
325702 }
325703
325704 if (child_op->has_estimated_cardinality) {
325705 // only check if we should switch to full sorting if we have estimated cardinality
325706 auto constant_limit = static_cast<double>(limit.limit_val.GetConstantValue());
325707 if (limit.offset_val.Type() == LimitNodeType::CONSTANT_VALUE) {
325708 constant_limit += static_cast<double>(limit.offset_val.GetConstantValue());
325709 }
325710 auto child_card = static_cast<double>(child_op->estimated_cardinality);
325711
325712 // if the limit is > 0.7% of the child cardinality, sorting the whole table is faster
325713 bool limit_is_large = constant_limit > 5000;
325714 if (constant_limit > child_card * 0.007 && limit_is_large) {
325715 return false;
325716 }
325717 }
325718
325719 while (child_op->type == LogicalOperatorType::LOGICAL_PROJECTION) {
325720 D_ASSERT(!child_op->children.empty());
325721 child_op = child_op->children[0].get();
325722 }
325723
325724 return child_op->type == LogicalOperatorType::LOGICAL_ORDER_BY;
325725 }
325726 return false;
325727}
Here is the caller graph for this function:

◆ PushdownDynamicFilters()

void duckdb::TopN::PushdownDynamicFilters ( LogicalTopN & op)
private
325729 {
325730 // pushdown dynamic filters through the Top-N operator
325731 bool nulls_first = op.orders[0].null_order == OrderByNullType::NULLS_FIRST;
325732 auto &type = op.orders[0].expression->return_type;
325733 if (!TypeIsIntegral(type.InternalType()) && type.id() != LogicalTypeId::VARCHAR) {
325734 // only supported for integral types currently
325735 return;
325736 }
325737 if (op.orders[0].expression->GetExpressionType() != ExpressionType::BOUND_COLUMN_REF) {
325738 // we can only pushdown on ORDER BY [col] currently
325739 return;
325740 }
325741 if (op.dynamic_filter) {
325742 // dynamic filter is already set
325743 return;
325744 }
325745 auto &colref = op.orders[0].expression->Cast<BoundColumnRefExpression>();
325746 vector<JoinFilterPushdownColumn> columns;
325747 JoinFilterPushdownColumn column;
325748 column.probe_column_index = colref.binding;
325749 columns.emplace_back(column);
325750 vector<PushdownFilterTarget> pushdown_targets;
325751 JoinFilterPushdownOptimizer::GetPushdownFilterTargets(*op.children[0], std::move(columns), pushdown_targets);
325752 if (pushdown_targets.empty()) {
325753 // no pushdown targets
325754 return;
325755 }
325756 // found pushdown targets! generate dynamic filters
325757 ExpressionType comparison_type;
325758 if (op.orders[0].type == OrderType::ASCENDING) {
325759 // for ascending order, we want the lowest N elements, so we filter on C <= [boundary]
325760 // if we only have a single order clause, we can filter on C < boundary
325761 comparison_type =
325762 op.orders.size() == 1 ? ExpressionType::COMPARE_LESSTHAN : ExpressionType::COMPARE_LESSTHANOREQUALTO;
325763 } else {
325764 // for descending order, we want the highest N elements, so we filter on C >= [boundary]
325765 // if we only have a single order clause, we can filter on C > boundary
325766 comparison_type =
325767 op.orders.size() == 1 ? ExpressionType::COMPARE_GREATERTHAN : ExpressionType::COMPARE_GREATERTHANOREQUALTO;
325768 }
325769 Value minimum_value = type.InternalType() == PhysicalType::VARCHAR ? Value("") : Value::MinimumValue(type);
325770 auto base_filter = make_uniq<ConstantFilter>(comparison_type, std::move(minimum_value));
325771 auto filter_data = make_shared_ptr<DynamicFilterData>();
325772 filter_data->filter = std::move(base_filter);
325773
325774 // put the filter into the Top-N clause
325775 op.dynamic_filter = filter_data;
325776
325777 for (auto &target : pushdown_targets) {
325778 auto &get = target.get;
325779 D_ASSERT(target.columns.size() == 1);
325780 auto col_idx = target.columns[0].probe_column_index.column_index;
325781
325782 // create the actual dynamic filter
325783 auto dynamic_filter = make_uniq<DynamicFilter>(filter_data);
325784 unique_ptr<TableFilter> pushed_filter = std::move(dynamic_filter);
325785 if (nulls_first) {
325786 auto or_filter = make_uniq<ConjunctionOrFilter>();
325787 or_filter->child_filters.push_back(make_uniq<IsNullFilter>());
325788 or_filter->child_filters.push_back(std::move(pushed_filter));
325789 pushed_filter = std::move(or_filter);
325790 }
325791 auto optional_filter = make_uniq<OptionalFilter>(std::move(pushed_filter));
325792
325793 // push the filter into the table scan
325794 auto &column_index = get.GetColumnIds()[col_idx];
325795 get.table_filters.PushFilter(column_index, std::move(optional_filter));
325796 }
325797}
__host__ cudaTextureObject_t get() const noexcept
@ VARCHAR
DuckDB Extensions.
Definition duckdb.hpp:2847

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