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

Public Member Functions

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

◆ ComparisonSimplificationRule()

duckdb::ComparisonSimplificationRule::ComparisonSimplificationRule ( ExpressionRewriter & rewriter)
explicit
319816 : Rule(rewriter) {
319817 // match on a ComparisonExpression that has a ConstantExpression as a check
319818 auto op = make_uniq<ComparisonExpressionMatcher>();
319819 op->matchers.push_back(make_uniq<FoldableConstantMatcher>());
319820 op->policy = SetMatcher::Policy::SOME;
319821 root = std::move(op);
319822}
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::ComparisonSimplificationRule::Apply ( LogicalOperator & op,
vector< reference< Expression > > & bindings,
bool & changes_made,
bool is_root )
overridevirtual

Here we check if we can apply the expression on the constant side We can do this if the cast itself is invertible and casting the constant is invertible in practice.

We can cast, now we change our column_ref_expression from an operator cast to a column reference

Implements duckdb::Rule.

319825 {
319826 auto &expr = bindings[0].get().Cast<BoundComparisonExpression>();
319827 auto &constant_expr = bindings[1].get();
319828 bool column_ref_left = expr.left.get() != &constant_expr;
319829 auto column_ref_expr = !column_ref_left ? expr.right.get() : expr.left.get();
319830 // the constant_expr is a scalar expression that we have to fold
319831 // use an ExpressionExecutor to execute the expression
319832 D_ASSERT(constant_expr.IsFoldable());
319833 Value constant_value;
319834 if (!ExpressionExecutor::TryEvaluateScalar(GetContext(), constant_expr, constant_value)) {
319835 return nullptr;
319836 }
319837 if (constant_value.IsNull() && !(expr.GetExpressionType() == ExpressionType::COMPARE_NOT_DISTINCT_FROM ||
319838 expr.GetExpressionType() == ExpressionType::COMPARE_DISTINCT_FROM)) {
319839 // comparison with constant NULL, return NULL
319840 return make_uniq<BoundConstantExpression>(Value(LogicalType::BOOLEAN));
319841 }
319842 if (column_ref_expr->GetExpressionClass() == ExpressionClass::BOUND_CAST) {
319846 auto &cast_expression = column_ref_expr->Cast<BoundCastExpression>();
319847 auto target_type = cast_expression.source_type();
319848 if (!BoundCastExpression::CastIsInvertible(target_type, cast_expression.return_type)) {
319849 return nullptr;
319850 }
319851
319852 // Can we cast the constant at all?
319853 string error_message;
319854 Value cast_constant;
319855 auto new_constant =
319856 constant_value.TryCastAs(rewriter.context, target_type, cast_constant, &error_message, true);
319857 if (!new_constant) {
319858 return nullptr;
319859 }
319860
319861 // Is the constant cast invertible?
319862 if (!cast_constant.IsNull() &&
319863 !BoundCastExpression::CastIsInvertible(cast_expression.return_type, target_type)) {
319864 // Cast is not invertible, so we do not rewrite this expression to ensure that the cast is executed
319865 return nullptr;
319866 }
319867
319869 auto child_expression = std::move(cast_expression.child);
319870 auto new_constant_expr = make_uniq<BoundConstantExpression>(cast_constant);
319871 if (column_ref_left) {
319872 expr.left = std::move(child_expression);
319873 expr.right = std::move(new_constant_expr);
319874 } else {
319875 expr.left = std::move(new_constant_expr);
319876 expr.right = std::move(child_expression);
319877 }
319878 changes_made = true;
319879 }
319880 return nullptr;
319881}
static bool CastIsInvertible(const LogicalType &source_type, const LogicalType &target_type)
Definition duckdb.cpp:375885
static DUCKDB_API bool TryEvaluateScalar(ClientContext &context, const Expression &expr, Value &result)
Try to evaluate a scalar expression and fold it into a single value, returns false if an exception is...
Definition duckdb.cpp:150008
DUCKDB_API bool TryCastAs(CastFunctionSet &set, GetCastFunctionInput &get_input, const LogicalType &target_type, Value &new_value, string *error_message, bool strict=false) const
Tries to cast this value to another type, and stores the result in "new_value".
Definition duckdb.cpp:133682
Here is the call graph for this function:

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