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

Public Member Functions

 ArithmeticSimplificationRule (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

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

◆ ArithmeticSimplificationRule()

duckdb::ArithmeticSimplificationRule::ArithmeticSimplificationRule ( ExpressionRewriter & rewriter)
explicit
319696 : Rule(rewriter) {
319697 // match on an OperatorExpression that has a ConstantExpression as child
319698 auto op = make_uniq<FunctionExpressionMatcher>();
319699 op->matchers.push_back(make_uniq<ConstantExpressionMatcher>());
319700 op->matchers.push_back(make_uniq<ExpressionMatcher>());
319701 op->policy = SetMatcher::Policy::SOME;
319702 // we only match on simple arithmetic expressions (+, -, *, /)
319703 op->function = make_uniq<ManyFunctionMatcher>(unordered_set<string> {"+", "-", "*", "//"});
319704 // and only with numeric results
319705 op->type = make_uniq<IntegerTypeMatcher>();
319706 op->matchers[0]->type = make_uniq<IntegerTypeMatcher>();
319707 op->matchers[1]->type = make_uniq<IntegerTypeMatcher>();
319708 root = std::move(op);
319709}
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
@ SOME
Only some entries have to be matched, the order of the matches does not matter.
Definition duckdb.cpp:69125

Member Function Documentation

◆ Apply()

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

Implements duckdb::Rule.

319712 {
319713 auto &root = bindings[0].get().Cast<BoundFunctionExpression>();
319714 auto &constant = bindings[1].get().Cast<BoundConstantExpression>();
319715 idx_t constant_child = root.children[0].get() == &constant ? 0 : 1;
319716 D_ASSERT(root.children.size() == 2);
319717 (void)root;
319718 // any arithmetic operator involving NULL is always NULL
319719 if (constant.value.IsNull()) {
319720 return make_uniq<BoundConstantExpression>(Value(root.return_type));
319721 }
319722 auto &func_name = root.function.name;
319723 if (func_name == "+") {
319724 if (constant.value == 0) {
319725 // addition with 0
319726 // we can remove the entire operator and replace it with the non-constant child
319727 return std::move(root.children[1 - constant_child]);
319728 }
319729 } else if (func_name == "-") {
319730 if (constant_child == 1 && constant.value == 0) {
319731 // subtraction by 0
319732 // we can remove the entire operator and replace it with the non-constant child
319733 return std::move(root.children[1 - constant_child]);
319734 }
319735 } else if (func_name == "*") {
319736 if (constant.value == 1) {
319737 // multiply with 1, replace with non-constant child
319738 return std::move(root.children[1 - constant_child]);
319739 } else if (constant.value == 0) {
319740 // multiply by zero: replace with constant or null
319741 return ExpressionRewriter::ConstantOrNull(std::move(root.children[1 - constant_child]),
319742 Value::Numeric(root.return_type, 0));
319743 }
319744 } else if (func_name == "//") {
319745 if (constant_child == 1) {
319746 if (constant.value == 1) {
319747 // divide by 1, replace with non-constant child
319748 return std::move(root.children[1 - constant_child]);
319749 } else if (constant.value == 0) {
319750 // divide by 0, replace with NULL
319751 return make_uniq<BoundConstantExpression>(Value(root.return_type));
319752 }
319753 }
319754 } else {
319755 throw InternalException("Unrecognized function name in ArithmeticSimplificationRule");
319756 }
319757 return nullptr;
319758}
static DUCKDB_API Value Numeric(const LogicalType &type, int64_t value)
Create a Numeric value of the specified type with the specified value.
Definition duckdb.cpp:133053
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: