Repository navigation
Expand file tree
/
Copy pathdate_part_simplification.cpp
More file actions
103 lines (96 loc) · 3.46 KB
/
Copy pathdate_part_simplification.cpp
File metadata and controls
103 lines (96 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "duckdb/optimizer/rule/date_part_simplification.hpp"
#include "duckdb/common/exception.hpp"
#include "duckdb/planner/expression/bound_constant_expression.hpp"
#include "duckdb/planner/expression/bound_function_expression.hpp"
#include "duckdb/optimizer/matcher/expression_matcher.hpp"
#include "duckdb/optimizer/matcher/type_matcher.hpp"
#include "duckdb/optimizer/expression_rewriter.hpp"
#include "duckdb/common/enums/date_part_specifier.hpp"
#include "duckdb/function/function.hpp"
#include "duckdb/function/function_binder.hpp"
namespace duckdb {
DatePartSimplificationRule::DatePartSimplificationRule(ExpressionRewriter &rewriter) : Rule(rewriter) {
auto func = make_uniq<FunctionExpressionMatcher>();
func->function = make_uniq<SpecificFunctionMatcher>("date_part");
auto specifier = make_uniq<ConstantExpressionMatcher>();
// only the single-part overload is simplified - the struct overload takes a constant LIST of part names
specifier->type = make_uniq<SpecificTypeMatcher>(LogicalType::VARCHAR);
func->matchers.push_back(std::move(specifier));
func->matchers.push_back(make_uniq<ExpressionMatcher>());
func->policy = SetMatcher::Policy::ORDERED;
root = std::move(func);
}
unique_ptr<Expression> DatePartSimplificationRule::Apply(LogicalOperator &op, vector<reference<Expression>> &bindings,
bool &changes_made, bool is_root) {
auto &date_part = bindings[0].get().Cast<BoundFunctionExpression>();
auto &constant_expr = bindings[1].get().Cast<BoundConstantExpression>();
const auto &constant = constant_expr.GetValue();
if (constant.IsNull()) {
// NULL specifier: return constant NULL
return make_uniq<BoundConstantExpression>(Value(date_part.GetReturnType()));
}
// otherwise check the specifier
auto specifier = GetDatePartSpecifier(StringValue::Get(constant));
Identifier new_function_name;
switch (specifier) {
case DatePartSpecifier::YEAR:
new_function_name = "year";
break;
case DatePartSpecifier::MONTH:
new_function_name = "month";
break;
case DatePartSpecifier::DAY:
new_function_name = "day";
break;
case DatePartSpecifier::DECADE:
new_function_name = "decade";
break;
case DatePartSpecifier::CENTURY:
new_function_name = "century";
break;
case DatePartSpecifier::MILLENNIUM:
new_function_name = "millennium";
break;
case DatePartSpecifier::QUARTER:
new_function_name = "quarter";
break;
case DatePartSpecifier::WEEK:
new_function_name = "week";
break;
case DatePartSpecifier::YEARWEEK:
new_function_name = "yearweek";
break;
case DatePartSpecifier::DOW:
new_function_name = "dayofweek";
break;
case DatePartSpecifier::ISODOW:
new_function_name = "isodow";
break;
case DatePartSpecifier::DOY:
new_function_name = "dayofyear";
break;
case DatePartSpecifier::MICROSECONDS:
new_function_name = "microsecond";
break;
case DatePartSpecifier::MILLISECONDS:
new_function_name = "millisecond";
break;
case DatePartSpecifier::SECOND:
new_function_name = "second";
break;
case DatePartSpecifier::MINUTE:
new_function_name = "minute";
break;
case DatePartSpecifier::HOUR:
new_function_name = "hour";
break;
default:
return nullptr;
}
// found a replacement function: bind it
vector<unique_ptr<Expression>> children;
children.push_back(std::move(date_part.GetChildrenMutable()[1]));
FunctionBinder binder(rewriter.context);
return binder.BindScalarFunction(Identifier::DefaultSchema(), new_function_name, std::move(children));
}
} // namespace duckdb