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

Public Member Functions

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

Private Attributes

ClientContext & context

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

◆ TimeStampComparison()

duckdb::TimeStampComparison::TimeStampComparison ( ExpressionRewriter & rewriter)
explicit
323105 : Rule(rewriter), context(rewriter.context) {
323106 // match on a ComparisonExpression that is an Equality and has a VARCHAR and ENUM as its children
323107 auto op = make_uniq<ComparisonExpressionMatcher>();
323108 op->policy = SetMatcher::Policy::UNORDERED;
323109 // Enum requires expression to be root
323110 op->expr_type = make_uniq<SpecificExpressionTypeMatcher>(ExpressionType::COMPARE_EQUAL);
323111
323112 // one side is timestamp cast to date
323113 auto left = make_uniq<CastExpressionMatcher>();
323114 left->type = make_uniq<TypeMatcherId>(LogicalTypeId::DATE);
323115 left->matcher = make_uniq<ExpressionMatcher>();
323116 left->matcher->expr_class = ExpressionClass::BOUND_COLUMN_REF;
323117 left->matcher->type = make_uniq<TypeMatcherId>(LogicalTypeId::TIMESTAMP);
323118 op->matchers.push_back(std::move(left));
323119
323120 // other side is varchar to date?
323121 auto right = make_uniq<CastExpressionMatcher>();
323122 right->type = make_uniq<TypeMatcherId>(LogicalTypeId::DATE);
323123 right->matcher = make_uniq<FoldableConstantMatcher>();
323124 right->matcher->expr_class = ExpressionClass::BOUND_CONSTANT;
323125 right->matcher->type = make_uniq<TypeMatcherId>(LogicalTypeId::VARCHAR);
323126 op->matchers.push_back(std::move(right));
323127
323128 root = std::move(op);
323129}
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

Member Function Documentation

◆ Apply()

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

Implements duckdb::Rule.

323137 {
323138 auto cast_constant = bindings[3].get().Copy();
323139 auto cast_columnref = bindings[2].get().Copy();
323140 auto is_constant = true;
323141 ExpressionIsConstant(*cast_constant, is_constant);
323142 if (!is_constant) {
323143 // means the matchers are flipped, so we need to flip our bindings
323144 // for some reason an extra binding is added in this case.
323145 cast_constant = bindings[4].get().Copy();
323146 cast_columnref = bindings[3].get().Copy();
323147 }
323148 auto new_expr = make_uniq<BoundConjunctionExpression>(ExpressionType::CONJUNCTION_AND);
323149
323150 Value result;
323151 if (ExpressionExecutor::TryEvaluateScalar(context, *cast_constant, result)) {
323152 D_ASSERT(result.type() == LogicalType::DATE);
323153 auto original_val = result.GetValue<duckdb::date_t>();
323154 auto no_seconds = dtime_t(0);
323155
323156 // original date as timestamp with no seconds
323157 auto original_val_ts = Value::TIMESTAMP(original_val, no_seconds);
323158 auto original_val_for_comparison = make_uniq<BoundConstantExpression>(original_val_ts);
323159
323160 // add one day and validate the new date
323161 // code is inspired by AddOperator::Operation(date_t left, int32_t right). The function wasn't used directly
323162 // since it throws errors that I cannot catch here.
323163
323164 auto date_t_copy = result.GetValue<duckdb::date_t>();
323165 date_t date_t_result;
323166 // attempt to add 1 day
323167 if (!TryAddOperator::Operation<date_t, int32_t, date_t>(date_t_copy, 1, date_t_result)) {
323168 // don't rewrite the expression and let the expression executor handle the invalid date
323169 return nullptr;
323170 }
323171
323172 auto result_as_val = Value::DATE(date_t_result);
323173 auto original_val_plus_on_date_ts = Value::TIMESTAMP(result_as_val.GetValue<timestamp_t>());
323174
323175 auto val_for_comparison = make_uniq<BoundConstantExpression>(original_val_plus_on_date_ts);
323176
323177 auto left_copy = cast_columnref->Copy();
323178 auto right_copy = cast_columnref->Copy();
323179 auto lt_eq_expr = make_uniq_base<Expression, BoundComparisonExpression>(
323180 ExpressionType::COMPARE_LESSTHAN, std::move(right_copy), std::move(val_for_comparison));
323181 auto gt_eq_expr = make_uniq_base<Expression, BoundComparisonExpression>(
323182 ExpressionType::COMPARE_GREATERTHANOREQUALTO, std::move(left_copy), std::move(original_val_for_comparison));
323183 new_expr->children.push_back(std::move(gt_eq_expr));
323184 new_expr->children.push_back(std::move(lt_eq_expr));
323185 return std::move(new_expr);
323186 }
323187 return nullptr;
323188}
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
static DUCKDB_API Value TIMESTAMP(date_t date, dtime_t time)
Create a timestamp Value from a specified date/time combination.
Definition duckdb.cpp:132409
static DUCKDB_API Value DATE(date_t date)
Create a date Value from a specified date.
Definition duckdb.cpp:132338

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