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

Public Member Functions

 DistributivityRule (ExpressionRewriter &rewriter)
unique_ptr< Expression > Apply (LogicalOperator &op, vector< reference< Expression > > &bindings, bool &changes_made, bool is_root) override
Public Member Functions inherited from duckdb::Rule
 Rule (ExpressionRewriter &rewriter)
ClientContext & GetContext () const

Private Member Functions

void AddExpressionSet (Expression &expr, expression_set_t &set)
unique_ptr< Expression > ExtractExpression (BoundConjunctionExpression &conj, idx_t idx, Expression &expr)

Additional Inherited Members

Public Attributes inherited from duckdb::Rule
ExpressionRewriter & rewriter
 The expression rewriter this rule belongs to.
unique_ptr< ExpressionMatcher > root
 The expression matcher of the rule.

Constructor & Destructor Documentation

◆ DistributivityRule()

duckdb::DistributivityRule::DistributivityRule ( ExpressionRewriter & rewriter)
explicit
320757 : Rule(rewriter) {
320758 // we match on an OR expression within a LogicalFilter node
320759 root = make_uniq<ExpressionMatcher>();
320760 root->expr_type = make_uniq<SpecificExpressionTypeMatcher>(ExpressionType::CONJUNCTION_OR);
320761}
ExpressionRewriter & rewriter
The expression rewriter this rule belongs to.
Definition duckdb.cpp:69564
unique_ptr< ExpressionMatcher > root
The expression matcher of the rule.
Definition duckdb.cpp:69566

Member Function Documentation

◆ Apply()

unique_ptr< Expression > duckdb::DistributivityRule::Apply ( LogicalOperator & op,
vector< reference< Expression > > & bindings,
bool & changes_made,
bool is_root )
overridevirtual

Implements duckdb::Rule.

320804 {
320805 auto &initial_or = bindings[0].get().Cast<BoundConjunctionExpression>();
320806
320807 // we want to find expressions that occur in each of the children of the OR
320808 // i.e. (X AND A) OR (X AND B) => X occurs in all branches
320809 // first, for the initial child, we create an expression set of which expressions occur
320810 // this is our initial candidate set (in the example: [X, A])
320811 expression_set_t candidate_set;
320812 AddExpressionSet(*initial_or.children[0], candidate_set);
320813 // now for each of the remaining children, we create a set again and intersect them
320814 // in our example: the second set would be [X, B]
320815 // the intersection would leave [X]
320816 for (idx_t i = 1; i < initial_or.children.size(); i++) {
320817 expression_set_t next_set;
320818 AddExpressionSet(*initial_or.children[i], next_set);
320819 expression_set_t intersect_result;
320820 for (auto &expr : candidate_set) {
320821 if (next_set.find(expr) != next_set.end()) {
320822 intersect_result.insert(expr);
320823 }
320824 }
320825 candidate_set = intersect_result;
320826 }
320827 if (candidate_set.empty()) {
320828 // nothing found: abort
320829 return nullptr;
320830 }
320831 // now for each of the remaining expressions in the candidate set we know that it is contained in all branches of
320832 // the OR
320833 auto new_root = make_uniq<BoundConjunctionExpression>(ExpressionType::CONJUNCTION_AND);
320834 for (auto &expr : candidate_set) {
320835 D_ASSERT(initial_or.children.size() > 0);
320836
320837 // extract the expression from the first child of the OR
320838 auto result = ExtractExpression(initial_or, 0, expr.get());
320839 // now for the subsequent expressions, simply remove the expression
320840 for (idx_t i = 1; i < initial_or.children.size(); i++) {
320841 ExtractExpression(initial_or, i, *result);
320842 }
320843 // now we add the expression to the new root
320844 new_root->children.push_back(std::move(result));
320845 }
320846
320847 // check if we completely erased one of the children of the OR
320848 // this happens if we have an OR in the form of "X OR (X AND A)"
320849 // the left child will be completely empty, as it only contains common expressions
320850 // in this case, any other children are not useful:
320851 // X OR (X AND A) is the same as "X"
320852 // since (1) only tuples that do not qualify "X" will not pass this predicate
320853 // and (2) all tuples that qualify "X" will pass this predicate
320854 for (idx_t i = 0; i < initial_or.children.size(); i++) {
320855 if (!initial_or.children[i]) {
320856 if (new_root->children.size() <= 1) {
320857 return std::move(new_root->children[0]);
320858 } else {
320859 return std::move(new_root);
320860 }
320861 }
320862 }
320863 // finally we need to add the remaining expressions in the OR to the new root
320864 if (initial_or.children.size() == 1) {
320865 // one child: skip the OR entirely and only add the single child
320866 new_root->children.push_back(std::move(initial_or.children[0]));
320867 } else if (initial_or.children.size() > 1) {
320868 // multiple children still remain: push them into a new OR and add that to the new root
320869 auto new_or = make_uniq<BoundConjunctionExpression>(ExpressionType::CONJUNCTION_OR);
320870 for (auto &child : initial_or.children) {
320871 new_or->children.push_back(std::move(child));
320872 }
320873 new_root->children.push_back(std::move(new_or));
320874 }
320875 // finally return the new root
320876 if (new_root->children.size() == 1) {
320877 return std::move(new_root->children[0]);
320878 }
320879 return std::move(new_root);
320880}
uint64_t idx_t
a saner size_t for loop indices etc
Definition duckdb.hpp:237

◆ AddExpressionSet()

void duckdb::DistributivityRule::AddExpressionSet ( Expression & expr,
expression_set_t & set )
private
320763 {
320764 if (expr.GetExpressionType() == ExpressionType::CONJUNCTION_AND) {
320765 auto &and_expr = expr.Cast<BoundConjunctionExpression>();
320766 for (auto &child : and_expr.children) {
320767 set.insert(*child);
320768 }
320769 } else {
320770 set.insert(expr);
320771 }
320772}

◆ ExtractExpression()

unique_ptr< Expression > duckdb::DistributivityRule::ExtractExpression ( BoundConjunctionExpression & conj,
idx_t idx,
Expression & expr )
private
320775 {
320776 auto &child = conj.children[idx];
320777 unique_ptr<Expression> result;
320778 if (child->GetExpressionType() == ExpressionType::CONJUNCTION_AND) {
320779 // AND, remove expression from the list
320780 auto &and_expr = child->Cast<BoundConjunctionExpression>();
320781 for (idx_t i = 0; i < and_expr.children.size(); i++) {
320782 if (and_expr.children[i]->Equals(expr)) {
320783 result = std::move(and_expr.children[i]);
320784 and_expr.children.erase_at(i);
320785 break;
320786 }
320787 }
320788 if (and_expr.children.size() == 1) {
320789 conj.children[idx] = std::move(and_expr.children[0]);
320790 }
320791 }
320792 // not an AND node(e.g. (X AND B) OR X) or this is the last expr,
320793 // remove the entire expression
320794 if (!result) {
320795 D_ASSERT(child->Equals(expr));
320796 result = std::move(child);
320797 conj.children[idx] = nullptr;
320798 }
320799 D_ASSERT(result);
320800 return result;
320801}

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