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

Public Member Functions

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

◆ MoveConstantsRule()

duckdb::MoveConstantsRule::MoveConstantsRule ( ExpressionRewriter & rewriter)
explicit
321519 : Rule(rewriter) {
321520 auto op = make_uniq<ComparisonExpressionMatcher>();
321521 op->matchers.push_back(make_uniq<ConstantExpressionMatcher>());
321522 op->policy = SetMatcher::Policy::UNORDERED;
321523
321524 auto arithmetic = make_uniq<FunctionExpressionMatcher>();
321525 // we handle multiplication, addition and subtraction because those are "easy"
321526 // integer division makes the division case difficult
321527 // e.g. [x / 2 = 3] means [x = 6 OR x = 7] because of truncation -> no clean rewrite rules
321528 arithmetic->function = make_uniq<ManyFunctionMatcher>(unordered_set<string> {"+", "-", "*"});
321529 // we match only on integral numeric types
321530 arithmetic->type = make_uniq<IntegerTypeMatcher>();
321531 auto child_constant_matcher = make_uniq<ConstantExpressionMatcher>();
321532 auto child_expression_matcher = make_uniq<ExpressionMatcher>();
321533 child_constant_matcher->type = make_uniq<IntegerTypeMatcher>();
321534 child_expression_matcher->type = make_uniq<IntegerTypeMatcher>();
321535 arithmetic->matchers.push_back(std::move(child_constant_matcher));
321536 arithmetic->matchers.push_back(std::move(child_expression_matcher));
321537 arithmetic->policy = SetMatcher::Policy::SOME;
321538 op->matchers.push_back(std::move(arithmetic));
321539 root = std::move(op);
321540}
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
@ UNORDERED
All entries have to be matched, but the order of the matches does not matter.
Definition duckdb.cpp:69123
@ 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::MoveConstantsRule::Apply ( LogicalOperator & op,
vector< reference< Expression > > & bindings,
bool & changes_made,
bool is_root )
overridevirtual

Implements duckdb::Rule.

321543 {
321544 auto &comparison = bindings[0].get().Cast<BoundComparisonExpression>();
321545 auto &outer_constant = bindings[1].get().Cast<BoundConstantExpression>();
321546 auto &arithmetic = bindings[2].get().Cast<BoundFunctionExpression>();
321547 auto &inner_constant = bindings[3].get().Cast<BoundConstantExpression>();
321548 D_ASSERT(arithmetic.return_type.IsIntegral());
321549 D_ASSERT(arithmetic.children[0]->return_type.IsIntegral());
321550 if (inner_constant.value.IsNull() || outer_constant.value.IsNull()) {
321551 if (comparison.GetExpressionType() == ExpressionType::COMPARE_DISTINCT_FROM ||
321552 comparison.GetExpressionType() == ExpressionType::COMPARE_NOT_DISTINCT_FROM) {
321553 return nullptr;
321554 }
321555 return make_uniq<BoundConstantExpression>(Value(comparison.return_type));
321556 }
321557 auto &constant_type = outer_constant.return_type;
321558 hugeint_t outer_value = IntegralValue::Get(outer_constant.value);
321559 hugeint_t inner_value = IntegralValue::Get(inner_constant.value);
321560
321561 idx_t arithmetic_child_index = arithmetic.children[0].get() == &inner_constant ? 1 : 0;
321562 auto &op_type = arithmetic.function.name;
321563 if (op_type == "+") {
321564 // [x + 1 COMP 10] OR [1 + x COMP 10]
321565 // order does not matter in addition:
321566 // simply change right side to 10-1 (outer_constant - inner_constant)
321567 if (!Hugeint::TrySubtractInPlace(outer_value, inner_value)) {
321568 return nullptr;
321569 }
321570 auto result_value = Value::HUGEINT(outer_value);
321571 if (!result_value.DefaultTryCastAs(constant_type)) {
321572 if (comparison.GetExpressionType() != ExpressionType::COMPARE_EQUAL) {
321573 return nullptr;
321574 }
321575 // if the cast is not possible then the comparison is not possible
321576 // for example, if we have x + 5 = 3, where x is an unsigned number, we will get x = -2
321577 // since this is not possible we can remove the entire branch here
321578 return ExpressionRewriter::ConstantOrNull(std::move(arithmetic.children[arithmetic_child_index]),
321579 Value::BOOLEAN(false));
321580 }
321581 outer_constant.value = std::move(result_value);
321582 } else if (op_type == "-") {
321583 // [x - 1 COMP 10] O R [1 - x COMP 10]
321584 // order matters in subtraction:
321585 if (arithmetic_child_index == 0) {
321586 // [x - 1 COMP 10]
321587 // change right side to 10+1 (outer_constant + inner_constant)
321588 if (!Hugeint::TryAddInPlace(outer_value, inner_value)) {
321589 return nullptr;
321590 }
321591 auto result_value = Value::HUGEINT(outer_value);
321592 if (!result_value.DefaultTryCastAs(constant_type)) {
321593 // if the cast is not possible then an equality comparison is not possible
321594 if (comparison.GetExpressionType() != ExpressionType::COMPARE_EQUAL) {
321595 return nullptr;
321596 }
321597 return ExpressionRewriter::ConstantOrNull(std::move(arithmetic.children[arithmetic_child_index]),
321598 Value::BOOLEAN(false));
321599 }
321600 outer_constant.value = std::move(result_value);
321601 } else {
321602 // [1 - x COMP 10]
321603 // change right side to 1-10=-9
321604 if (!Hugeint::TrySubtractInPlace(inner_value, outer_value)) {
321605 return nullptr;
321606 }
321607 auto result_value = Value::HUGEINT(inner_value);
321608 if (!result_value.DefaultTryCastAs(constant_type)) {
321609 // if the cast is not possible then an equality comparison is not possible
321610 if (comparison.GetExpressionType() != ExpressionType::COMPARE_EQUAL) {
321611 return nullptr;
321612 }
321613 return ExpressionRewriter::ConstantOrNull(std::move(arithmetic.children[arithmetic_child_index]),
321614 Value::BOOLEAN(false));
321615 }
321616 outer_constant.value = std::move(result_value);
321617 // in this case, we should also flip the comparison
321618 // e.g. if we have [4 - x < 2] then we should have [x > 2]
321619 comparison.SetExpressionTypeUnsafe(FlipComparisonExpression(comparison.GetExpressionType()));
321620 }
321621 } else {
321622 D_ASSERT(op_type == "*");
321623 // [x * 2 COMP 10] OR [2 * x COMP 10]
321624 // order does not matter in multiplication:
321625 // change right side to 10/2 (outer_constant / inner_constant)
321626 // but ONLY if outer_constant is cleanly divisible by the inner_constant
321627 if (inner_value == 0) {
321628 // x * 0, the result is either 0 or NULL
321629 // we let the arithmetic_simplification rule take care of simplifying this first
321630 return nullptr;
321631 }
321632 // check out of range for HUGEINT or not cleanly divisible
321633 // HUGEINT is not cleanly divisible when outer_value == minimum and inner value == -1. (modulo overflow)
321634 if ((outer_value == NumericLimits<hugeint_t>::Minimum() && inner_value == -1) ||
321635 outer_value % inner_value != 0) {
321636 bool is_equality = comparison.GetExpressionType() == ExpressionType::COMPARE_EQUAL;
321637 bool is_inequality = comparison.GetExpressionType() == ExpressionType::COMPARE_NOTEQUAL;
321638 if (is_equality || is_inequality) {
321639 // we know the values are not equal
321640 // the result will be either FALSE or NULL (if COMPARE_EQUAL)
321641 // or TRUE or NULL (if COMPARE_NOTEQUAL)
321642 return ExpressionRewriter::ConstantOrNull(std::move(arithmetic.children[arithmetic_child_index]),
321643 Value::BOOLEAN(is_inequality));
321644 } else {
321645 // not cleanly divisible and we are doing > >= < <=, skip the simplification for now
321646 return nullptr;
321647 }
321648 }
321649 if (inner_value < 0) {
321650 // multiply by negative value, need to flip expression
321651 comparison.SetExpressionTypeUnsafe(FlipComparisonExpression(comparison.GetExpressionType()));
321652 }
321653 // else divide the RHS by the LHS
321654 // we need to do a range check on the cast even though we do a division
321655 // because e.g. -128 / -1 = 128, which is out of range
321656 auto result_value = Value::HUGEINT(outer_value / inner_value);
321657 if (!result_value.DefaultTryCastAs(constant_type)) {
321658 return ExpressionRewriter::ConstantOrNull(std::move(arithmetic.children[arithmetic_child_index]),
321659 Value::BOOLEAN(false));
321660 }
321661 outer_constant.value = std::move(result_value);
321662 }
321663 // replace left side with x
321664 // first extract x from the arithmetic expression
321665 auto arithmetic_child = std::move(arithmetic.children[arithmetic_child_index]);
321666 // then place in the comparison
321667 if (comparison.left.get() == &outer_constant) {
321668 comparison.right = std::move(arithmetic_child);
321669 } else {
321670 comparison.left = std::move(arithmetic_child);
321671 }
321672 changes_made = true;
321673 return nullptr;
321674}
static DUCKDB_API Value HUGEINT(hugeint_t value)
Create a hugeint Value from a specified value.
Definition duckdb.cpp:132153
static DUCKDB_API Value BOOLEAN(bool value)
Create a boolean Value from a specified value.
Definition duckdb.cpp:132118
ExpressionType FlipComparisonExpression(ExpressionType type)
Flip a comparison expression, turning e.g. < into >, or = into =.
Definition duckdb.cpp:68052
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: