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

Public Member Functions

 LikeOptimizationRule (ExpressionRewriter &rewriter)
unique_ptr< Expression > Apply (LogicalOperator &op, vector< reference< Expression > > &bindings, bool &changes_made, bool is_root) override
unique_ptr< Expression > ApplyRule (BoundFunctionExpression &expr, ScalarFunction function, string pattern, bool is_not_like)
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

◆ LikeOptimizationRule()

duckdb::LikeOptimizationRule::LikeOptimizationRule ( ExpressionRewriter & rewriter)
explicit
321356 : Rule(rewriter) {
321357 // match on a FunctionExpression that has a foldable ConstantExpression
321358 auto func = make_uniq<FunctionExpressionMatcher>();
321359 func->matchers.push_back(make_uniq<ExpressionMatcher>());
321360 func->matchers.push_back(make_uniq<ConstantExpressionMatcher>());
321361 func->policy = SetMatcher::Policy::ORDERED;
321362 // we match on LIKE ("~~") and NOT LIKE ("!~~")
321363 func->function = make_uniq<ManyFunctionMatcher>(unordered_set<string> {"!~~", "~~"});
321364 root = std::move(func);
321365}
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
@ ORDERED
All entries have to be matched, and the matches have to be ordered.
Definition duckdb.cpp:69121

Member Function Documentation

◆ Apply()

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

Implements duckdb::Rule.

321448 {
321449 auto &root = bindings[0].get().Cast<BoundFunctionExpression>();
321450 auto &constant_expr = bindings[2].get().Cast<BoundConstantExpression>();
321451 D_ASSERT(root.children.size() == 2);
321452
321453 if (constant_expr.value.IsNull()) {
321454 return make_uniq<BoundConstantExpression>(Value(root.return_type));
321455 }
321456
321457 // the constant_expr is a scalar expression that we have to fold
321458 if (!constant_expr.IsFoldable()) {
321459 return nullptr;
321460 }
321461
321462 auto constant_value = ExpressionExecutor::EvaluateScalar(GetContext(), constant_expr);
321463 D_ASSERT(constant_value.type() == constant_expr.return_type);
321464 auto &patt_str = StringValue::Get(constant_value);
321465
321466 bool is_not_like = root.function.name == "!~~";
321467 if (PatternIsConstant(patt_str)) {
321468 // Pattern is constant
321469 return make_uniq<BoundComparisonExpression>(is_not_like ? ExpressionType::COMPARE_NOTEQUAL
321470 : ExpressionType::COMPARE_EQUAL,
321471 std::move(root.children[0]), std::move(root.children[1]));
321472 } else if (PatternIsPrefix(patt_str)) {
321473 // Prefix LIKE pattern : [^%_]*[%]+, ignoring underscore
321474 return ApplyRule(root, PrefixFun::GetFunction(), patt_str, is_not_like);
321475 } else if (PatternIsSuffix(patt_str)) {
321476 // Suffix LIKE pattern: [%]+[^%_]*, ignoring underscore
321477 return ApplyRule(root, SuffixFun::GetFunction(), patt_str, is_not_like);
321478 } else if (PatternIsContains(patt_str)) {
321479 // Contains LIKE pattern: [%]+[^%_]*[%]+, ignoring underscore
321480 return ApplyRule(root, GetStringContains(), patt_str, is_not_like);
321481 }
321482 return nullptr;
321483}
static DUCKDB_API Value EvaluateScalar(ClientContext &context, const Expression &expr, bool allow_unfoldable=false)
Evaluate a scalar expression and fold it into a single value.
Definition duckdb.cpp:149993

◆ ApplyRule()

unique_ptr< Expression > duckdb::LikeOptimizationRule::ApplyRule ( BoundFunctionExpression & expr,
ScalarFunction function,
string pattern,
bool is_not_like )
321486 {
321487 // replace LIKE by an optimized function
321488 unique_ptr<Expression> result;
321489 auto new_function =
321490 make_uniq<BoundFunctionExpression>(expr.return_type, std::move(function), std::move(expr.children), nullptr);
321491
321492 // removing "%" from the pattern
321493 pattern.erase(std::remove(pattern.begin(), pattern.end(), '%'), pattern.end());
321494
321495 new_function->children[1] = make_uniq<BoundConstantExpression>(Value(std::move(pattern)));
321496
321497 result = std::move(new_function);
321498 if (is_not_like) {
321499 auto negation = make_uniq<BoundOperatorExpression>(ExpressionType::OPERATOR_NOT, LogicalType::BOOLEAN);
321500 negation->children.push_back(std::move(result));
321501 result = std::move(negation);
321502 }
321503
321504 return result;
321505}

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