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

The Deliminator optimizer traverses the logical operator tree and removes any redundant DelimGets/DelimJoins. More...

Collaboration diagram for duckdb::Deliminator:

Public Member Functions

unique_ptr< LogicalOperator > Optimize (unique_ptr< LogicalOperator > op)
 Perform DelimJoin elimination.

Private Member Functions

void FindCandidates (unique_ptr< LogicalOperator > &op, vector< DelimCandidate > &candidates)
 Finds DelimJoins and their corresponding DelimGets.
void FindJoinWithDelimGet (unique_ptr< LogicalOperator > &op, DelimCandidate &candidate, idx_t depth=0)
bool HasSelection (const LogicalOperator &delim_join)
 Whether the DelimJoin is selective.
bool RemoveJoinWithDelimGet (LogicalComparisonJoin &delim_join, const idx_t delim_get_count, unique_ptr< LogicalOperator > &join, bool &all_equality_conditions)
 Remove joins with a DelimGet.
bool RemoveInequalityJoinWithDelimGet (LogicalComparisonJoin &delim_join, const idx_t delim_get_count, unique_ptr< LogicalOperator > &join, const vector< ReplacementBinding > &replacement_bindings)
void TrySwitchSingleToLeft (LogicalComparisonJoin &delim_join)

Private Attributes

optional_ptr< LogicalOperator > root

Detailed Description

The Deliminator optimizer traverses the logical operator tree and removes any redundant DelimGets/DelimJoins.

Constructor & Destructor Documentation

◆ Deliminator()

duckdb::Deliminator::Deliminator ( )
inline
307349 {
307350 }

Member Function Documentation

◆ Optimize()

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

Perform DelimJoin elimination.

307420 {
307421 root = op;
307422
307423 vector<DelimCandidate> candidates;
307424 FindCandidates(op, candidates);
307425
307426 if (candidates.empty()) {
307427 return op;
307428 }
307429
307430 for (auto &candidate : candidates) {
307431 auto &delim_join = candidate.delim_join;
307432
307433 // Sort these so the deepest are first
307434 std::sort(candidate.joins.begin(), candidate.joins.end(),
307435 [](const JoinWithDelimGet &lhs, const JoinWithDelimGet &rhs) { return lhs.depth > rhs.depth; });
307436
307437 bool all_removed = true;
307438 if (!candidate.joins.empty() && HasSelection(delim_join)) {
307439 // Keep the deepest join with DelimGet in these cases,
307440 // as the selection can greatly reduce the cost of the RHS child of the DelimJoin
307441 candidate.joins.erase(candidate.joins.begin());
307442 all_removed = false;
307443 }
307444
307445 bool all_equality_conditions = true;
307446 for (auto &join : candidate.joins) {
307447 all_removed = RemoveJoinWithDelimGet(delim_join, candidate.delim_get_count, join.join.get(),
307448 all_equality_conditions) &&
307449 all_removed;
307450 }
307451
307452 // Change type if there are no more duplicate-eliminated columns
307453 if (candidate.joins.size() == candidate.delim_get_count && all_removed) {
307454 delim_join.type = LogicalOperatorType::LOGICAL_COMPARISON_JOIN;
307455 delim_join.duplicate_eliminated_columns.clear();
307456 }
307457
307458 // Only DelimJoins are ever created as SINGLE joins,
307459 // and we can switch from SINGLE to LEFT if the RHS is de-duplicated by an aggr
307460 if (delim_join.join_type == JoinType::SINGLE) {
307461 TrySwitchSingleToLeft(delim_join);
307462 }
307463 }
307464
307465 return op;
307466}
bool HasSelection(const LogicalOperator &delim_join)
Whether the DelimJoin is selective.
Definition duckdb.cpp:307484
void FindCandidates(unique_ptr< LogicalOperator > &op, vector< DelimCandidate > &candidates)
Finds DelimJoins and their corresponding DelimGets.
Definition duckdb.cpp:307468
bool RemoveJoinWithDelimGet(LogicalComparisonJoin &delim_join, const idx_t delim_get_count, unique_ptr< LogicalOperator > &join, bool &all_equality_conditions)
Remove joins with a DelimGet.
Definition duckdb.cpp:307549
cv::String join(const cv::String &base, const cv::String &path)
Here is the call graph for this function:

◆ FindCandidates()

void duckdb::Deliminator::FindCandidates ( unique_ptr< LogicalOperator > & op,
vector< DelimCandidate > & candidates )
private

Finds DelimJoins and their corresponding DelimGets.

307468 {
307469 for (auto &child : op->children) {
307470 FindCandidates(child, candidates);
307471 }
307472
307473 if (op->type != LogicalOperatorType::LOGICAL_DELIM_JOIN) {
307474 return;
307475 }
307476
307477 candidates.emplace_back(op, op->Cast<LogicalComparisonJoin>());
307478 auto &candidate = candidates.back();
307479
307480 // DelimGets are in the RHS
307481 FindJoinWithDelimGet(op->children[1], candidate);
307482}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ FindJoinWithDelimGet()

void duckdb::Deliminator::FindJoinWithDelimGet ( unique_ptr< LogicalOperator > & op,
DelimCandidate & candidate,
idx_t depth = 0 )
private
307522 {
307523 if (op->type == LogicalOperatorType::LOGICAL_DELIM_JOIN) {
307524 FindJoinWithDelimGet(op->children[0], candidate, depth + 1);
307525 } else if (op->type == LogicalOperatorType::LOGICAL_DELIM_GET) {
307526 candidate.delim_get_count++;
307527 } else {
307528 for (auto &child : op->children) {
307529 FindJoinWithDelimGet(child, candidate, depth + 1);
307530 }
307531 }
307532
307533 if (op->type == LogicalOperatorType::LOGICAL_COMPARISON_JOIN &&
307534 (OperatorIsDelimGet(*op->children[0]) || OperatorIsDelimGet(*op->children[1]))) {
307535 candidate.joins.emplace_back(op, depth);
307536 }
307537}

◆ HasSelection()

bool duckdb::Deliminator::HasSelection ( const LogicalOperator & delim_join)
private

Whether the DelimJoin is selective.

307484 {
307485 // TODO once we implement selectivity estimation using samples we need to use that here
307486 switch (op.type) {
307487 case LogicalOperatorType::LOGICAL_GET: {
307488 auto &get = op.Cast<LogicalGet>();
307489 for (const auto &filter : get.table_filters.filters) {
307490 if (filter.second->filter_type != TableFilterType::IS_NOT_NULL) {
307491 return true;
307492 }
307493 }
307494 break;
307495 }
307496 case LogicalOperatorType::LOGICAL_FILTER:
307497 return true;
307498 default:
307499 break;
307500 }
307501
307502 for (auto &child : op.children) {
307503 if (HasSelection(*child)) {
307504 return true;
307505 }
307506 }
307507
307508 return false;
307509}
__host__ cudaTextureObject_t get() const noexcept
void filter(InputArray image, InputArray kernel, OutputArray output)
Here is the call graph for this function:
Here is the caller graph for this function:

◆ RemoveJoinWithDelimGet()

bool duckdb::Deliminator::RemoveJoinWithDelimGet ( LogicalComparisonJoin & delim_join,
const idx_t delim_get_count,
unique_ptr< LogicalOperator > & join,
bool & all_equality_conditions )
private

Remove joins with a DelimGet.

307550 {
307551 auto &comparison_join = join->Cast<LogicalComparisonJoin>();
307552 if (!ChildJoinTypeCanBeDeliminated(comparison_join.join_type)) {
307553 return false;
307554 }
307555
307556 // Get the index (left or right) of the DelimGet side of the join
307557 const idx_t delim_idx = OperatorIsDelimGet(*join->children[0]) ? 0 : 1;
307558
307559 // Get the filter (if any)
307560 optional_ptr<LogicalFilter> filter;
307561 vector<unique_ptr<Expression>> filter_expressions;
307562 if (join->children[delim_idx]->type == LogicalOperatorType::LOGICAL_FILTER) {
307563 filter = &join->children[delim_idx]->Cast<LogicalFilter>();
307564 for (auto &expr : filter->expressions) {
307565 filter_expressions.emplace_back(expr->Copy());
307566 }
307567 }
307568
307569 auto &delim_get = (filter ? filter->children[0] : join->children[delim_idx])->Cast<LogicalDelimGet>();
307570 if (comparison_join.conditions.size() != delim_get.chunk_types.size()) {
307571 return false; // Joining with DelimGet adds new information
307572 }
307573
307574 // Check if joining with the DelimGet is redundant, and collect relevant column information
307575 ColumnBindingReplacer replacer;
307576 auto &replacement_bindings = replacer.replacement_bindings;
307577 for (auto &cond : comparison_join.conditions) {
307578 all_equality_conditions = all_equality_conditions && IsEqualityJoinCondition(cond);
307579 auto &delim_side = delim_idx == 0 ? *cond.left : *cond.right;
307580 auto &other_side = delim_idx == 0 ? *cond.right : *cond.left;
307581 if (delim_side.GetExpressionType() != ExpressionType::BOUND_COLUMN_REF ||
307582 other_side.GetExpressionType() != ExpressionType::BOUND_COLUMN_REF) {
307583 return false;
307584 }
307585 auto &delim_colref = delim_side.Cast<BoundColumnRefExpression>();
307586 auto &other_colref = other_side.Cast<BoundColumnRefExpression>();
307587 replacement_bindings.emplace_back(delim_colref.binding, other_colref.binding);
307588
307589 // Only add IS NOT NULL filter for regular equality/inequality comparisons
307590 // Do NOT add for DISTINCT FROM variants, as they handle NULL correctly
307591 if (cond.comparison != ExpressionType::COMPARE_NOT_DISTINCT_FROM &&
307592 cond.comparison != ExpressionType::COMPARE_DISTINCT_FROM) {
307593 auto is_not_null_expr =
307594 make_uniq<BoundOperatorExpression>(ExpressionType::OPERATOR_IS_NOT_NULL, LogicalType::BOOLEAN);
307595 is_not_null_expr->children.push_back(other_side.Copy());
307596 filter_expressions.push_back(std::move(is_not_null_expr));
307597 }
307598 }
307599
307600 if (!all_equality_conditions &&
307601 !RemoveInequalityJoinWithDelimGet(delim_join, delim_get_count, join, replacement_bindings)) {
307602 return false;
307603 }
307604
307605 unique_ptr<LogicalOperator> replacement_op = std::move(comparison_join.children[1 - delim_idx]);
307606 if (!filter_expressions.empty()) { // Create filter if necessary
307607 auto new_filter = make_uniq<LogicalFilter>();
307608 new_filter->expressions = std::move(filter_expressions);
307609 new_filter->children.emplace_back(std::move(replacement_op));
307610 replacement_op = std::move(new_filter);
307611 }
307612
307613 join = std::move(replacement_op);
307614
307615 // TODO: Maybe go from delim join instead to save work
307616 replacer.VisitOperator(*root);
307617 return true;
307618}
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:

◆ RemoveInequalityJoinWithDelimGet()

bool duckdb::Deliminator::RemoveInequalityJoinWithDelimGet ( LogicalComparisonJoin & delim_join,
const idx_t delim_get_count,
unique_ptr< LogicalOperator > & join,
const vector< ReplacementBinding > & replacement_bindings )
private
307648 {
307649 auto &comparison_join = join->Cast<LogicalComparisonJoin>();
307650 auto &delim_conditions = delim_join.conditions;
307651 const auto &join_conditions = comparison_join.conditions;
307652 if (delim_get_count != 1 || !InequalityDelimJoinCanBeEliminated(delim_join.join_type) ||
307653 delim_conditions.size() != join_conditions.size()) {
307654 return false;
307655 }
307656
307657 // TODO: we cannot perform the optimization here because our pure inequality joins don't implement
307658 // JoinType::SINGLE yet, and JoinType::MARK is a special case
307659 if (delim_join.join_type == JoinType::SINGLE || delim_join.join_type == JoinType::MARK) {
307660 bool has_one_equality = false;
307661 for (auto &cond : join_conditions) {
307662 has_one_equality = has_one_equality || IsEqualityJoinCondition(cond);
307663 }
307664 if (!has_one_equality) {
307665 return false;
307666 }
307667 }
307668
307669 // We only support colref's
307670 vector<ColumnBinding> traced_bindings;
307671 for (const auto &cond : delim_conditions) {
307672 if (cond.right->GetExpressionType() != ExpressionType::BOUND_COLUMN_REF) {
307673 return false;
307674 }
307675 auto &colref = cond.right->Cast<BoundColumnRefExpression>();
307676 traced_bindings.emplace_back(colref.binding);
307677 }
307678
307679 // Now we trace down the bindings to the join (for now, we only trace it through a few operators)
307680 reference<LogicalOperator> current_op = *delim_join.children[1];
307681 while (&current_op.get() != join.get()) {
307682 if (current_op.get().children.size() != 1) {
307683 return false;
307684 }
307685
307686 switch (current_op.get().type) {
307687 case LogicalOperatorType::LOGICAL_PROJECTION:
307688 FindAndReplaceBindings(traced_bindings, current_op.get().expressions, current_op.get().GetColumnBindings());
307689 break;
307690 case LogicalOperatorType::LOGICAL_FILTER:
307691 break; // Doesn't change bindings
307692 default:
307693 return false;
307694 }
307695 current_op = *current_op.get().children[0];
307696 }
307697
307698 // Get the index (left or right) of the DelimGet side of the join
307699 const idx_t delim_idx = OperatorIsDelimGet(*join->children[0]) ? 0 : 1;
307700
307701 bool found_all = true;
307702 for (idx_t cond_idx = 0; cond_idx < delim_conditions.size(); cond_idx++) {
307703 auto &delim_condition = delim_conditions[cond_idx];
307704 const auto &traced_binding = traced_bindings[cond_idx];
307705
307706 bool found = false;
307707 for (auto &join_condition : join_conditions) {
307708 auto &delim_side = delim_idx == 0 ? *join_condition.left : *join_condition.right;
307709 auto &colref = delim_side.Cast<BoundColumnRefExpression>();
307710 if (colref.binding == traced_binding) {
307711 auto join_comparison = join_condition.comparison;
307712 auto original_join_comparison = join_condition.comparison; // Save original for later check
307713 if (delim_condition.comparison == ExpressionType::COMPARE_DISTINCT_FROM ||
307714 delim_condition.comparison == ExpressionType::COMPARE_NOT_DISTINCT_FROM) {
307715 // We need to compare NULL values
307716 if (join_comparison == ExpressionType::COMPARE_EQUAL) {
307717 join_comparison = ExpressionType::COMPARE_NOT_DISTINCT_FROM;
307718 } else if (join_comparison == ExpressionType::COMPARE_NOTEQUAL) {
307719 join_comparison = ExpressionType::COMPARE_DISTINCT_FROM;
307720 } else if (join_comparison != ExpressionType::COMPARE_DISTINCT_FROM &&
307721 join_comparison != ExpressionType::COMPARE_NOT_DISTINCT_FROM) {
307722 // The optimization does not work here
307723 found = false;
307724 break;
307725 }
307726 }
307727 delim_condition.comparison = FlipComparisonExpression(join_comparison);
307728 // join condition was a not equal and filtered out all NULLS.
307729 // DELIM JOIN need to do that for not DELIM_GET side. Easiest way is to change the
307730 // comparison expression type. See duckdb/duckdb#16803
307731 // Only convert if the ORIGINAL join had != or = (not DISTINCT FROM variants)
307732 if (delim_join.join_type != JoinType::MARK &&
307733 original_join_comparison != ExpressionType::COMPARE_DISTINCT_FROM &&
307734 original_join_comparison != ExpressionType::COMPARE_NOT_DISTINCT_FROM) {
307735 if (delim_condition.comparison == ExpressionType::COMPARE_DISTINCT_FROM) {
307736 delim_condition.comparison = ExpressionType::COMPARE_NOTEQUAL;
307737 }
307738 if (delim_condition.comparison == ExpressionType::COMPARE_NOT_DISTINCT_FROM) {
307739 delim_condition.comparison = ExpressionType::COMPARE_EQUAL;
307740 }
307741 }
307742 found = true;
307743 break;
307744 }
307745 }
307746 found_all = found_all && found;
307747 }
307748
307749 return found_all;
307750}
ExpressionType FlipComparisonExpression(ExpressionType type)
Flip a comparison expression, turning e.g. < into >, or = into =.
Definition duckdb.cpp:68052

◆ TrySwitchSingleToLeft()

void duckdb::Deliminator::TrySwitchSingleToLeft ( LogicalComparisonJoin & delim_join)
private
307752 {
307753 D_ASSERT(delim_join.join_type == JoinType::SINGLE);
307754
307755 // Collect RHS bindings
307756 vector<ColumnBinding> join_bindings;
307757 for (const auto &cond : delim_join.conditions) {
307758 if (!IsEqualityJoinCondition(cond)) {
307759 return;
307760 }
307761 if (cond.right->GetExpressionType() != ExpressionType::BOUND_COLUMN_REF) {
307762 return;
307763 }
307764 auto &colref = cond.right->Cast<BoundColumnRefExpression>();
307765 join_bindings.emplace_back(colref.binding);
307766 }
307767
307768 // Now try to find an aggr in the RHS such that the join_column_bindings is a superset of the groups
307769 reference<LogicalOperator> current_op = *delim_join.children[1];
307770 while (current_op.get().type != LogicalOperatorType::LOGICAL_AGGREGATE_AND_GROUP_BY) {
307771 if (current_op.get().children.size() != 1) {
307772 return;
307773 }
307774
307775 switch (current_op.get().type) {
307776 case LogicalOperatorType::LOGICAL_PROJECTION:
307777 FindAndReplaceBindings(join_bindings, current_op.get().expressions, current_op.get().GetColumnBindings());
307778 break;
307779 case LogicalOperatorType::LOGICAL_FILTER:
307780 break; // Doesn't change bindings
307781 default:
307782 return;
307783 }
307784 current_op = *current_op.get().children[0];
307785 }
307786
307787 D_ASSERT(current_op.get().type == LogicalOperatorType::LOGICAL_AGGREGATE_AND_GROUP_BY);
307788 const auto &aggr = current_op.get().Cast<LogicalAggregate>();
307789 if (!aggr.grouping_functions.empty()) {
307790 return;
307791 }
307792
307793 for (idx_t group_idx = 0; group_idx < aggr.groups.size(); group_idx++) {
307794 if (std::find(join_bindings.begin(), join_bindings.end(), ColumnBinding(aggr.group_index, group_idx)) ==
307795 join_bindings.end()) {
307796 return;
307797 }
307798 }
307799
307800 delim_join.join_type = JoinType::LEFT;
307801}

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