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

Public Member Functions

 BuildProbeSideOptimizer (ClientContext &context, LogicalOperator &op)
void VisitOperator (LogicalOperator &op) override
void VisitExpression (unique_ptr< Expression > *expression) override

Private Member Functions

bool TryFlipJoinChildren (LogicalOperator &op) const
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)

Static Private Member Functions

static idx_t ChildHasJoins (LogicalOperator &op)
static BuildSize GetBuildSizes (const LogicalOperator &op, idx_t lhs_cardinality, idx_t rhs_cardinality)
static double GetBuildSize (vector< LogicalType > types, idx_t cardinality)
Static Private Member Functions inherited from duckdb::LogicalOperatorVisitor
static void EnumerateExpressions (LogicalOperator &op, const std::function< void(unique_ptr< Expression > *child)> &callback)

Private Attributes

ClientContext & context
vector< ColumnBinding > preferred_on_probe_side

Static Private Attributes

static constexpr idx_t COLUMN_COUNT_PENALTY = 2
static constexpr double PREFER_RIGHT_DEEP_PENALTY = 0.15

Constructor & Destructor Documentation

◆ BuildProbeSideOptimizer()

duckdb::BuildProbeSideOptimizer::BuildProbeSideOptimizer ( ClientContext & context,
LogicalOperator & op )
explicit
303579 : context(context) {
303580 vector<ColumnBinding> updating_columns, current_op_bindings;
303581 auto bindings = op.GetColumnBindings();
303582 vector<ColumnBinding> row_id_bindings;
303583 // If any column bindings are a row_id, there is a good chance the statement is an insert/delete/update statement.
303584 // As an initialization step, we travers the plan and find which bindings are row_id bindings.
303585 // When we eventually do our build side probe side optimizations, if we get to a join where the left and right
303586 // cardinalities are the same, we prefer to have the child with the rowid bindings in the probe side.
303587 GetRowidBindings(op, preferred_on_probe_side);
303588 op.ResolveOperatorTypes();
303589}

Member Function Documentation

◆ VisitOperator()

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

Reimplemented from duckdb::LogicalOperatorVisitor.

303751 {
303752 // then the currentoperator
303753 switch (op.type) {
303754 case LogicalOperatorType::LOGICAL_DELIM_JOIN: {
303755 auto &join = op.Cast<LogicalComparisonJoin>();
303756 if (HasInverseJoinType(join.join_type)) {
303757 join.delim_flipped = TryFlipJoinChildren(join);
303758 }
303759 break;
303760 }
303761 case LogicalOperatorType::LOGICAL_COMPARISON_JOIN: {
303762 auto &join = op.Cast<LogicalComparisonJoin>();
303763 switch (join.join_type) {
303764 case JoinType::SEMI:
303765 case JoinType::ANTI: {
303766 // if the conditions have no equality, do not flip the children.
303767 // There is no physical join operator (yet) that can do an inequality right_semi/anti join.
303768 idx_t has_range = 0;
303769 bool prefer_range_joins = Settings::Get<PreferRangeJoinsSetting>(context);
303770 if (op.type == LogicalOperatorType::LOGICAL_ANY_JOIN ||
303771 (op.Cast<LogicalComparisonJoin>().HasEquality(has_range) && !prefer_range_joins)) {
303772 TryFlipJoinChildren(join);
303773 }
303774 break;
303775 }
303776 default:
303777 if (HasInverseJoinType(join.join_type)) {
303778 TryFlipJoinChildren(op);
303779 }
303780 }
303781 break;
303782 }
303783 case LogicalOperatorType::LOGICAL_ANY_JOIN: {
303784 auto &join = op.Cast<LogicalJoin>();
303785 // We do not yet support the RIGHT_SEMI or RIGHT_ANTI join types for these, so don't try to flip
303786 switch (join.join_type) {
303787 case JoinType::SEMI:
303788 case JoinType::ANTI:
303789 break; // RIGHT_SEMI/RIGHT_ANTI not supported yet for ANY/ASOF
303790 default:
303791 // We cannot flip projection maps are set (YET), but not flipping is worse than just clearing them
303792 // They will be set in the 2nd round of ColumnLifetimeAnalyzer
303793 join.left_projection_map.clear();
303794 join.right_projection_map.clear();
303795 TryFlipJoinChildren(op);
303796 }
303797 break;
303798 }
303799 case LogicalOperatorType::LOGICAL_CROSS_PRODUCT: {
303800 TryFlipJoinChildren(op);
303801 break;
303802 }
303803 default:
303804 break;
303805 }
303806
303808}
void VisitOperatorChildren(LogicalOperator &op)
Definition duckdb.cpp:380983
cv::String join(const cv::String &base, const cv::String &path)
bool HasInverseJoinType(JoinType type)
Whether the JoinType has an inverse.
Definition duckdb.cpp:68432
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ VisitExpression()

void duckdb::BuildProbeSideOptimizer::VisitExpression ( unique_ptr< Expression > * expression)
inlineoverridevirtual

Reimplemented from duckdb::LogicalOperatorVisitor.

303522{};

◆ TryFlipJoinChildren()

bool duckdb::BuildProbeSideOptimizer::TryFlipJoinChildren ( LogicalOperator & op) const
private
303696 {
303697 auto &left_child = *op.children[0];
303698 auto &right_child = *op.children[1];
303699 const auto lhs_cardinality = left_child.has_estimated_cardinality ? left_child.estimated_cardinality
303700 : left_child.EstimateCardinality(context);
303701 const auto rhs_cardinality = right_child.has_estimated_cardinality ? right_child.estimated_cardinality
303702 : right_child.EstimateCardinality(context);
303703
303704 auto build_sizes = GetBuildSizes(op, lhs_cardinality, rhs_cardinality);
303705 auto &left_side_build_cost = build_sizes.left_side;
303706 auto &right_side_build_cost = build_sizes.right_side;
303707
303708 bool swap = false;
303709
303710 idx_t left_child_joins = ChildHasJoins(*op.children[0]);
303711 idx_t right_child_joins = ChildHasJoins(*op.children[1]);
303712 // if the right child is a table scan, and the left child has joins, we should prefer the left child
303713 // to be the build side. Since the tuples of the left side will already have been built on/be in flight,
303714 // it will be faster to build on them again.
303715 if (right_child_joins == 0 && left_child_joins > 0) {
303716 right_side_build_cost *= (1 + PREFER_RIGHT_DEEP_PENALTY);
303717 }
303718
303719 // RHS is build side.
303720 // if right_side metric is larger than left_side metric, then right_side is more costly to build on
303721 // than the lhs. So we swap
303722 if (right_side_build_cost > left_side_build_cost) {
303723 swap = true;
303724 }
303725
303726 // swap for preferred on probe side
303727 if (rhs_cardinality == lhs_cardinality && !preferred_on_probe_side.empty()) {
303728 // inspect final bindings, we prefer them on the probe side
303729 auto bindings_left = left_child.GetColumnBindings();
303730 auto bindings_right = right_child.GetColumnBindings();
303731 auto bindings_in_left = ComputeOverlappingBindings(bindings_left, preferred_on_probe_side);
303732 auto bindings_in_right = ComputeOverlappingBindings(bindings_right, preferred_on_probe_side);
303733 // (if the sides are planning to be swapped AND
303734 // if more projected bindings are in the left (meaning right/build side after the swap)
303735 // then swap them back. The projected bindings stay in the left/probe side.)
303736 // OR
303737 // (if the sides are planning not to be swapped AND
303738 // if more projected bindings are in the right (meaning right/build)
303739 // then swap them. The projected bindings are swapped to the left/probe side.)
303740 if ((swap && bindings_in_left > bindings_in_right) || (!swap && bindings_in_right > bindings_in_left)) {
303741 swap = !swap;
303742 }
303743 }
303744
303745 if (swap) {
303746 FlipChildren(op);
303747 }
303748 return swap;
303749}
void swap(Mat &a, Mat &b)

◆ ChildHasJoins()

idx_t duckdb::BuildProbeSideOptimizer::ChildHasJoins ( LogicalOperator & op)
staticprivate
303685 {
303686 if (op.children.empty()) {
303687 return 0;
303688 } else if (op.type == LogicalOperatorType::LOGICAL_COMPARISON_JOIN ||
303689 op.type == LogicalOperatorType::LOGICAL_ASOF_JOIN ||
303690 op.type == LogicalOperatorType::LOGICAL_CROSS_PRODUCT) {
303691 return 1 + ChildHasJoins(*op.children[0]) + ChildHasJoins(*op.children[1]);
303692 }
303693 return ChildHasJoins(*op.children[0]);
303694}

◆ GetBuildSizes()

BuildSize duckdb::BuildProbeSideOptimizer::GetBuildSizes ( const LogicalOperator & op,
idx_t lhs_cardinality,
idx_t rhs_cardinality )
staticprivate
303632 {
303633 BuildSize ret;
303634 switch (op.type) {
303635 case LogicalOperatorType::LOGICAL_COMPARISON_JOIN:
303636 case LogicalOperatorType::LOGICAL_CROSS_PRODUCT:
303637 case LogicalOperatorType::LOGICAL_ANY_JOIN:
303638 case LogicalOperatorType::LOGICAL_DELIM_JOIN: {
303639 ret.left_side = GetBuildSize(op.children[0]->types, lhs_cardinality);
303640 ret.right_side = GetBuildSize(op.children[1]->types, rhs_cardinality);
303641 return ret;
303642 }
303643 default:
303644 break;
303645 }
303646 return ret;
303647}

◆ GetBuildSize()

double duckdb::BuildProbeSideOptimizer::GetBuildSize ( vector< LogicalType > types,
idx_t cardinality )
staticprivate
303649 {
303650 // Row width in the hash table
303651 types.push_back(LogicalType::HASH);
303652 auto tuple_layout = TupleDataLayout();
303653 tuple_layout.Initialize(types, TupleDataValidityType::CAN_HAVE_NULL_VALUES);
303654 auto row_width = tuple_layout.GetRowWidth();
303655
303656 for (const auto &type : types) {
303657 TypeVisitor::VisitReplace(type, [&](const LogicalType &visited_type) {
303658 // Penalty for variable-size types (we don't have statistics here yet)
303659 switch (visited_type.InternalType()) {
303661 row_width += 8;
303662 break;
303663 case PhysicalType::LIST:
303664 case PhysicalType::ARRAY:
303665 row_width += 32;
303666 break;
303667 default:
303668 break;
303669 }
303670
303671 // Penalty for number of (recursive) columns
303672 row_width += COLUMN_COUNT_PENALTY;
303673
303674 return visited_type;
303675 });
303676 }
303677
303678 // There is also a cost of NextPowerOfTwo(count * 2) * sizeof(ht_entry_t) per tuple in the hash table
303679 // This is a not a smooth cost function, so instead we do the average, which is ~3 * sizeof(ht_entry_t)
303680 row_width += 3 * sizeof(ht_entry_t);
303681
303682 return static_cast<double>(row_width * cardinality);
303683}
@ LIST
A list of some logical data type.
Definition duckdb.hpp:2813
@ VARCHAR
DuckDB Extensions.
Definition duckdb.hpp:2847

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