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

Public Member Functions

 DateTruncSimplificationRule (ExpressionRewriter &rewriter)
unique_ptr< Expression > Apply (LogicalOperator &op, vector< reference< Expression > > &bindings, bool &changes_made, bool is_root) override
unique_ptr< Expression > CreateTrunc (const BoundConstantExpression &date_part, const BoundConstantExpression &rhs, const LogicalType &return_type)
unique_ptr< Expression > CreateTruncAdd (const BoundConstantExpression &date_part, const BoundConstantExpression &rhs, const LogicalType &return_type)
bool DateIsTruncated (const BoundConstantExpression &date_part, const BoundConstantExpression &rhs)
unique_ptr< Expression > CastAndEvaluate (unique_ptr< Expression > rhs, const LogicalType &return_type)
Public Member Functions inherited from duckdb::Rule
 Rule (ExpressionRewriter &rewriter)
ClientContext & GetContext () const

Static Public Member Functions

static string DatePartToFunc (const DatePartSpecifier &date_part)

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

◆ DateTruncSimplificationRule()

duckdb::DateTruncSimplificationRule::DateTruncSimplificationRule ( ExpressionRewriter & rewriter)
explicit
320249 : Rule(rewriter) {
320250 auto op = make_uniq<ComparisonExpressionMatcher>();
320251
320252 auto lhs = make_uniq<FunctionExpressionMatcher>();
320253 lhs->function = make_uniq<ManyFunctionMatcher>(unordered_set<string> {"date_trunc", "datetrunc"});
320254 lhs->matchers.push_back(make_uniq<ConstantExpressionMatcher>());
320255 lhs->matchers.push_back(make_uniq<ExpressionMatcher>());
320256 lhs->policy = SetMatcher::Policy::ORDERED;
320257
320258 auto rhs = make_uniq<ConstantExpressionMatcher>();
320259
320260 op->matchers.push_back(std::move(lhs));
320261 op->matchers.push_back(std::move(rhs));
320262 op->policy = SetMatcher::Policy::UNORDERED;
320263
320264 root = std::move(op);
320265}
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
@ 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::DateTruncSimplificationRule::Apply ( LogicalOperator & op,
vector< reference< Expression > > & bindings,
bool & changes_made,
bool is_root )
overridevirtual

Implements duckdb::Rule.

320268 {
320269 auto &expr = bindings[0].get().Cast<BoundComparisonExpression>();
320270 auto comparison_type = expr.GetExpressionType();
320271
320272 auto &date_part = bindings[2].get().Cast<BoundConstantExpression>();
320273 // We must have only a column on the LHS.
320274 if (bindings[3].get().GetExpressionType() != ExpressionType::BOUND_COLUMN_REF) {
320275 return nullptr;
320276 }
320277
320278 auto &column_part = bindings[3].get().Cast<BoundColumnRefExpression>();
320279 auto &rhs = bindings[4].get().Cast<BoundConstantExpression>();
320280
320281 // Determine whether or not the column name is on the lhs or rhs.
320282 const bool col_is_lhs = (expr.left->GetExpressionClass() == ExpressionClass::BOUND_FUNCTION);
320283
320284 // We want to treat rhs >= col equivalently to col <= rhs.
320285 // So, get the expression type if it was ordered such that the constant was actually on the right hand side.
320286 ExpressionType rhs_comparison_type = comparison_type;
320287 if (!col_is_lhs) {
320288 rhs_comparison_type = FlipComparisonExpression(comparison_type);
320289 }
320290
320291 // Check whether trunc(date_part, constant_rhs) = constant_rhs.
320292 const bool is_truncated = DateIsTruncated(date_part, rhs);
320293
320294 switch (rhs_comparison_type) {
320295 case ExpressionType::COMPARE_EQUAL:
320296 case ExpressionType::COMPARE_NOT_DISTINCT_FROM:
320297 // We handle two very similar optimizations here:
320298 //
320299 // date_trunc(part, column) = constant_rhs --> column >= date_trunc(part, constant_rhs) AND
320300 // column < date_trunc(part, date_add(constant_rhs,
320301 // INTERVAL 1 part))
320302 // or, if date_trunc(part, constant_rhs) <> constant_rhs, this is unsatisfiable
320303 //
320304 // ----
320305 //
320306 // date_trunc(part, column) IS NOT DISTINCT FROM constant_rhs
320307 //
320308 // Here we have two cases: when constant_rhs is NULL, this simplifies to:
320309 //
320310 // column IS NULL
320311 //
320312 // Otherwise, the expression becomes:
320313 //
320314 // (column >= date_trunc(part, constant_rhs) AND
320315 // column < date_trunc(part, date_add(constant_rhs, INTERVAL 1 part)) AND
320316 // column IS NOT NULL)
320317 //
320318 {
320319 // First check if we can just return `column IS NULL`.
320320 if (rhs_comparison_type == ExpressionType::COMPARE_NOT_DISTINCT_FROM && rhs.value.IsNull()) {
320321 auto op = make_uniq<BoundOperatorExpression>(ExpressionType::OPERATOR_IS_NULL, LogicalType::BOOLEAN);
320322 op->children.push_back(column_part.Copy());
320323 return std::move(op);
320324 } else {
320325 if (!is_truncated) {
320326 return make_uniq<BoundConstantExpression>(Value::BOOLEAN(false));
320327 }
320328
320329 auto trunc = CreateTrunc(date_part, rhs, column_part.return_type);
320330 if (!trunc) {
320331 return nullptr;
320332 }
320333
320334 auto trunc_add = CreateTruncAdd(date_part, rhs, column_part.return_type);
320335 if (!trunc_add) {
320336 return nullptr;
320337 }
320338
320339 auto gteq = make_uniq<BoundComparisonExpression>(ExpressionType::COMPARE_GREATERTHANOREQUALTO,
320340 column_part.Copy(), std::move(trunc));
320341 auto lt = make_uniq<BoundComparisonExpression>(ExpressionType::COMPARE_LESSTHAN, column_part.Copy(),
320342 std::move(trunc_add));
320343
320344 // For IS NOT DISTINCT FROM, we also have to add the extra NULL term.
320345 if (rhs_comparison_type == ExpressionType::COMPARE_NOT_DISTINCT_FROM) {
320346 auto comp = make_uniq<BoundConjunctionExpression>(ExpressionType::CONJUNCTION_AND, std::move(gteq),
320347 std::move(lt));
320348
320349 auto isnotnull =
320350 make_uniq<BoundOperatorExpression>(ExpressionType::OPERATOR_IS_NOT_NULL, LogicalType::BOOLEAN);
320351 isnotnull->children.push_back(column_part.Copy());
320352
320353 return make_uniq<BoundConjunctionExpression>(ExpressionType::CONJUNCTION_AND, std::move(comp),
320354 std::move(isnotnull));
320355 } else {
320356 return make_uniq<BoundConjunctionExpression>(ExpressionType::CONJUNCTION_AND, std::move(gteq),
320357 std::move(lt));
320358 }
320359 }
320360 }
320361
320362 case ExpressionType::COMPARE_NOTEQUAL:
320363 case ExpressionType::COMPARE_DISTINCT_FROM:
320364 // We handle two very similar optimizations here:
320365 //
320366 // date_trunc(part, column) <> constant_rhs --> column < date_trunc(part, constant_rhs) OR
320367 // column >= date_trunc(part, date_add(constant_rhs,
320368 // INTERVAL 1 part))
320369 // or, if date_trunc(part, constant_rhs) <> constant_rhs, this is always true
320370 //
320371 // ----
320372 //
320373 // date_trunc(part, column) IS DISTINCT FROM constant_rhs
320374 //
320375 // Here we have two cases: when constant_rhs is NULL, this simplifies to:
320376 //
320377 // column IS NOT NULL
320378 //
320379 // Otherwise, the expression becomes:
320380 //
320381 // (column < date_trunc(part, constant_rhs) OR
320382 // column >= date_trunc(part, date_add(constant_rhs, INTERVAL 1 part)) OR
320383 // column IS NULL)
320384 //
320385 {
320386 if (rhs_comparison_type == ExpressionType::COMPARE_DISTINCT_FROM && rhs.value.IsNull()) {
320387 // Return 'column IS NOT NULL'.
320388 auto op =
320389 make_uniq<BoundOperatorExpression>(ExpressionType::OPERATOR_IS_NOT_NULL, LogicalType::BOOLEAN);
320390 op->children.push_back(column_part.Copy());
320391 return std::move(op);
320392 } else {
320393 if (!is_truncated) {
320394 return make_uniq<BoundConstantExpression>(Value::BOOLEAN(true));
320395 }
320396
320397 auto trunc = CreateTrunc(date_part, rhs, column_part.return_type);
320398 if (!trunc) {
320399 return nullptr;
320400 }
320401
320402 auto trunc_add = CreateTruncAdd(date_part, rhs, column_part.return_type);
320403 if (!trunc_add) {
320404 return nullptr;
320405 }
320406
320407 auto lt = make_uniq<BoundComparisonExpression>(ExpressionType::COMPARE_LESSTHAN, column_part.Copy(),
320408 std::move(trunc));
320409 auto gteq = make_uniq<BoundComparisonExpression>(ExpressionType::COMPARE_GREATERTHANOREQUALTO,
320410 column_part.Copy(), std::move(trunc_add));
320411
320412 // If this is a DISTINCT FROM, we need to add the 'column IS NULL' term.
320413 if (rhs_comparison_type == ExpressionType::COMPARE_DISTINCT_FROM) {
320414 auto comp = make_uniq<BoundConjunctionExpression>(ExpressionType::CONJUNCTION_OR, std::move(gteq),
320415 std::move(lt));
320416
320417 auto isnull =
320418 make_uniq<BoundOperatorExpression>(ExpressionType::OPERATOR_IS_NULL, LogicalType::BOOLEAN);
320419 isnull->children.push_back(column_part.Copy());
320420
320421 return make_uniq<BoundConjunctionExpression>(ExpressionType::CONJUNCTION_OR, std::move(comp),
320422 std::move(isnull));
320423 } else {
320424 return make_uniq<BoundConjunctionExpression>(ExpressionType::CONJUNCTION_OR, std::move(gteq),
320425 std::move(lt));
320426 }
320427 }
320428 }
320429 return nullptr;
320430
320431 case ExpressionType::COMPARE_LESSTHAN:
320432 case ExpressionType::COMPARE_GREATERTHANOREQUALTO:
320433 // date_trunc(part, column) < constant_rhs --> column < date_trunc(part, date_add(constant_rhs,
320434 // INTERVAL 1 part))
320435 // date_trunc(part, column) >= constant_rhs --> column >= date_trunc(part, date_add(constant_rhs,
320436 // INTERVAL 1 part))
320437 {
320438 // The optimization for < and >= is a little tricky: if trunc(rhs) = rhs, then we need to just
320439 // use the rhs as-is, instead of using trunc(rhs + 1 date_part).
320440 if (!is_truncated) {
320441 // Create date_trunc(part, date_add(rhs, INTERVAL 1 part)) and fold the constant.
320442 auto trunc = CreateTruncAdd(date_part, rhs, column_part.return_type);
320443 if (!trunc) {
320444 return nullptr; // Something went wrong---don't do the optimization.
320445 }
320446
320447 if (col_is_lhs) {
320448 expr.left = column_part.Copy();
320449 expr.right = std::move(trunc);
320450 } else {
320451 expr.right = column_part.Copy();
320452 expr.left = std::move(trunc);
320453 }
320454 } else {
320455 // If the RHS is already truncated (i.e. date_trunc(part, rhs) = rhs), then we can use
320456 // it as-is.
320457 if (col_is_lhs) {
320458 expr.left = column_part.Copy();
320459 // Determine whether the RHS needs to be casted.
320460 if (rhs.return_type.id() != expr.left->return_type.id()) {
320461 expr.right = CastAndEvaluate(std::move(expr.right), expr.left->return_type);
320462 }
320463 } else {
320464 expr.right = column_part.Copy();
320465 // Determine whether the RHS needs to be casted.
320466 if (rhs.return_type.id() != expr.right->return_type.id()) {
320467 expr.left = CastAndEvaluate(std::move(expr.left), expr.right->return_type);
320468 }
320469 }
320470 }
320471
320472 changes_made = true;
320473 return nullptr;
320474 }
320475
320476 case ExpressionType::COMPARE_LESSTHANOREQUALTO:
320477 case ExpressionType::COMPARE_GREATERTHAN:
320478 // date_trunc(part, column) <= constant_rhs --> column < date_trunc(part, date_add(constant_rhs,
320479 // INTERVAL 1 part))
320480 // date_trunc(part, column) > constant_rhs --> column >= date_trunc(part, date_add(constant_rhs,
320481 // INTERVAL 1 part))
320482 {
320483 // Create date_trunc(part, date_add(rhs, INTERVAL 1 part)) and fold the constant.
320484 auto trunc = CreateTruncAdd(date_part, rhs, column_part.return_type);
320485 if (!trunc) {
320486 return nullptr; // Something went wrong---don't do the optimization.
320487 }
320488
320489 if (col_is_lhs) {
320490 expr.left = column_part.Copy();
320491 expr.right = std::move(trunc);
320492 } else {
320493 expr.right = column_part.Copy();
320494 expr.left = std::move(trunc);
320495 }
320496
320497 // > needs to become >=, and <= needs to become <.
320498 if (rhs_comparison_type == ExpressionType::COMPARE_GREATERTHAN) {
320499 if (col_is_lhs) {
320500 expr.SetExpressionTypeUnsafe(ExpressionType::COMPARE_GREATERTHANOREQUALTO);
320501 } else {
320502 expr.SetExpressionTypeUnsafe(ExpressionType::COMPARE_LESSTHANOREQUALTO);
320503 }
320504 } else {
320505 if (col_is_lhs) {
320506 expr.SetExpressionTypeUnsafe(ExpressionType::COMPARE_LESSTHAN);
320507 } else {
320508 expr.SetExpressionTypeUnsafe(ExpressionType::COMPARE_GREATERTHAN);
320509 }
320510 }
320511
320512 changes_made = true;
320513 return nullptr;
320514 }
320515
320516 default:
320517 return nullptr;
320518 }
320519}
static DUCKDB_API Value BOOLEAN(bool value)
Create a boolean Value from a specified value.
Definition duckdb.cpp:132118
__host__ cudaTextureObject_t get() const noexcept
ExpressionType FlipComparisonExpression(ExpressionType type)
Flip a comparison expression, turning e.g. < into >, or = into =.
Definition duckdb.cpp:68052

◆ DatePartToFunc()

string duckdb::DateTruncSimplificationRule::DatePartToFunc ( const DatePartSpecifier & date_part)
static
320521 {
320522 switch (date_part) {
320523 // These specifiers can be used as intervals.
320524 case DatePartSpecifier::YEAR:
320525 return "to_years";
320526 case DatePartSpecifier::MONTH:
320527 return "to_months";
320528 case DatePartSpecifier::DAY:
320529 return "to_days";
320530 case DatePartSpecifier::DECADE:
320531 return "to_decades";
320532 case DatePartSpecifier::CENTURY:
320533 return "to_centuries";
320534 case DatePartSpecifier::MILLENNIUM:
320535 return "to_millennia";
320536 case DatePartSpecifier::MICROSECONDS:
320537 return "to_microseconds";
320538 case DatePartSpecifier::MILLISECONDS:
320539 return "to_milliseconds";
320540 case DatePartSpecifier::SECOND:
320541 return "to_seconds";
320542 case DatePartSpecifier::MINUTE:
320543 return "to_minutes";
320544 case DatePartSpecifier::HOUR:
320545 return "to_hours";
320546 case DatePartSpecifier::WEEK:
320547 return "to_weeks";
320548 case DatePartSpecifier::QUARTER:
320549 return "to_quarters";
320550
320551 // These specifiers cannot be used as intervals and can only be used as
320552 // date parts.
320553 case DatePartSpecifier::DOW:
320554 case DatePartSpecifier::ISODOW:
320555 case DatePartSpecifier::DOY:
320556 case DatePartSpecifier::ISOYEAR:
320557 case DatePartSpecifier::YEARWEEK:
320558 case DatePartSpecifier::ERA:
320559 case DatePartSpecifier::TIMEZONE:
320560 case DatePartSpecifier::TIMEZONE_HOUR:
320561 case DatePartSpecifier::TIMEZONE_MINUTE:
320562 default:
320563 return "";
320564 }
320565}

◆ CreateTrunc()

unique_ptr< Expression > duckdb::DateTruncSimplificationRule::CreateTrunc ( const BoundConstantExpression & date_part,
const BoundConstantExpression & rhs,
const LogicalType & return_type )
320569 {
320570 FunctionBinder binder(rewriter.context);
320571 ErrorData error;
320572
320573 vector<unique_ptr<Expression>> args;
320574 args.emplace_back(date_part.Copy());
320575 args.emplace_back(rhs.Copy());
320576 auto trunc = binder.BindScalarFunction(DEFAULT_SCHEMA, "date_trunc", std::move(args), error);
320577
320578 // Ensure that the RHS type matches the column type.
320579 if (trunc->return_type.id() != return_type.id()) {
320580 trunc = BoundCastExpression::AddDefaultCastToType(std::move(trunc), return_type, true);
320581 }
320582
320583 if (trunc->IsFoldable()) {
320584 Value result;
320585 if (!ExpressionExecutor::TryEvaluateScalar(rewriter.context, *trunc, result)) {
320586 return trunc;
320587 }
320588
320589 return make_uniq<BoundConstantExpression>(result);
320590 }
320591
320592 return trunc;
320593}
static unique_ptr< Expression > AddDefaultCastToType(unique_ptr< Expression > expr, const LogicalType &target_type, bool try_cast=false)
Cast an expression to the specified SQL type, using only the built-in SQL casts.
Definition duckdb.cpp:375861
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

◆ CreateTruncAdd()

unique_ptr< Expression > duckdb::DateTruncSimplificationRule::CreateTruncAdd ( const BoundConstantExpression & date_part,
const BoundConstantExpression & rhs,
const LogicalType & return_type )
320597 {
320598 DatePartSpecifier part = GetDatePartSpecifier(StringValue::Get(date_part.value));
320599 const string interval_func_name = DatePartToFunc(part);
320600
320601 // If the date part cannot be represented as an interval, then we cannot
320602 // perform the optimization.
320603 if (interval_func_name.empty()) {
320604 return nullptr;
320605 }
320606
320607 FunctionBinder binder(rewriter.context);
320608 ErrorData error;
320609
320610 vector<unique_ptr<Expression>> args1;
320611 auto constant_param = make_uniq<BoundConstantExpression>(Value::INTEGER(1));
320612 args1.emplace_back(std::move(constant_param));
320613 auto interval = binder.BindScalarFunction(DEFAULT_SCHEMA, interval_func_name, std::move(args1), error);
320614 if (!interval) {
320615 return nullptr; // Something wrong---just don't do the optimization.
320616 }
320617
320618 vector<unique_ptr<Expression>> args2;
320619 args2.emplace_back(rhs.Copy());
320620 args2.emplace_back(std::move(interval));
320621 auto add = binder.BindScalarFunction(DEFAULT_SCHEMA, "+", std::move(args2), error);
320622
320623 vector<unique_ptr<Expression>> args3;
320624 args3.emplace_back(date_part.Copy());
320625 args3.emplace_back(std::move(add));
320626 auto trunc = binder.BindScalarFunction(DEFAULT_SCHEMA, "date_trunc", std::move(args3), error);
320627
320628 // Ensure that the RHS type matches the column type.
320629 if (trunc->return_type.id() != return_type.id()) {
320630 trunc = BoundCastExpression::AddDefaultCastToType(std::move(trunc), return_type, true);
320631 }
320632
320633 if (trunc->IsFoldable()) {
320634 Value result;
320635 if (!ExpressionExecutor::TryEvaluateScalar(rewriter.context, *trunc, result)) {
320636 return trunc;
320637 }
320638
320639 return make_uniq<BoundConstantExpression>(result);
320640 }
320641
320642 return trunc;
320643}
static DUCKDB_API Value INTEGER(int32_t value)
Create an integer Value from a specified value.
Definition duckdb.cpp:132139
void add(InputArray src1, InputArray src2, OutputArray dst, InputArray mask=noArray(), int dtype=-1)

◆ DateIsTruncated()

bool duckdb::DateTruncSimplificationRule::DateIsTruncated ( const BoundConstantExpression & date_part,
const BoundConstantExpression & rhs )
320646 {
320647 // If the rhs is null, then the date is "truncated" in the sense that date_trunc(..., NULL) is also NULL.
320648 if (rhs.value.IsNull()) {
320649 return true;
320650 }
320651
320652 // Create the node date_trunc(date_part, rhs).
320653 auto trunc = CreateTrunc(date_part, rhs, rhs.return_type);
320654
320655 Value trunc_result, result;
320656 if (!ExpressionExecutor::TryEvaluateScalar(rewriter.context, *trunc, trunc_result)) {
320657 return false;
320658 }
320659 if (!ExpressionExecutor::TryEvaluateScalar(rewriter.context, rhs, result)) {
320660 return false;
320661 }
320662
320663 return (result == trunc_result);
320664}

◆ CastAndEvaluate()

unique_ptr< Expression > duckdb::DateTruncSimplificationRule::CastAndEvaluate ( unique_ptr< Expression > rhs,
const LogicalType & return_type )
320667 {
320668 auto cast = BoundCastExpression::AddDefaultCastToType(std::move(rhs), return_type, true);
320669 if (cast->IsFoldable()) {
320670 Value result;
320671 if (!ExpressionExecutor::TryEvaluateScalar(rewriter.context, *cast, result)) {
320672 return cast;
320673 }
320674
320675 return make_uniq<BoundConstantExpression>(result);
320676 }
320677
320678 return cast;
320679}
Widget2D cast() const

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