Skip to content

Commit 64a7ebd

Browse files
committed
chore(cubesql): Realias Int32 and Boolean on constant folding
1 parent 8260a6d commit 64a7ebd

File tree

3 files changed

+39
-15
lines changed

3 files changed

+39
-15
lines changed

rust/cubesql/cubesql/src/compile/rewrite/analysis.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1287,10 +1287,12 @@ impl Analysis<LogicalPlanLanguage> for LogicalPlanAnalysis {
12871287
ScalarValue::Date32(_)
12881288
| ScalarValue::Date64(_)
12891289
| ScalarValue::Int64(_)
1290+
| ScalarValue::Int32(_)
12901291
| ScalarValue::Float64(_)
12911292
| ScalarValue::IntervalYearMonth(_)
12921293
| ScalarValue::IntervalDayTime(_)
12931294
| ScalarValue::Utf8(_)
1295+
| ScalarValue::Boolean(_)
12941296
) {
12951297
egraph[id]
12961298
.data

rust/cubesql/cubesql/src/compile/rewrite/rules/filters.rs

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ impl RewriteRules for FilterRules {
101101
transforming_rewrite(
102102
"push-down-limit-filter",
103103
filter(
104-
literal_expr("?literal"),
104+
"?literal_false",
105105
cube_scan(
106106
"?source_table_name",
107107
"?members",
@@ -132,7 +132,7 @@ impl RewriteRules for FilterRules {
132132
),
133133
),
134134
self.push_down_limit_filter(
135-
"?literal",
135+
"?literal_false",
136136
"?new_limit",
137137
"?new_limit_skip",
138138
"?new_limit_fetch",
@@ -141,18 +141,19 @@ impl RewriteRules for FilterRules {
141141
// Transform Filter: Boolean(true)
142142
// It's safe to push down filter under projection, next filter-truncate-true will truncate it
143143
// TODO: Find a better solution how to drop filter node at all once
144-
rewrite(
144+
transforming_rewrite(
145145
"push-down-filter-projection",
146146
filter(
147-
literal_bool(true),
147+
"?literal_true",
148148
projection("?expr", "?input", "?alias", "?projection_split"),
149149
),
150150
projection(
151151
"?expr",
152-
filter(literal_bool(true), "?input"),
152+
filter("?literal_true", "?input"),
153153
"?alias",
154154
"?projection_split",
155155
),
156+
self.transform_literal_true("?literal_true"),
156157
),
157158
rewrite(
158159
"swap-limit-filter",
@@ -257,26 +258,28 @@ impl RewriteRules for FilterRules {
257258
),
258259
),
259260
// Transform Filter: Boolean(True) same as TRUE = TRUE, which is useless
260-
rewrite(
261+
transforming_rewrite(
261262
"filter-truncate-true",
262263
filter_replacer(
263-
literal_bool(true),
264+
"?literal_true",
264265
"?alias_to_cube",
265266
"?members",
266267
"?filter_aliases",
267268
),
268269
cube_scan_filters_empty_tail(),
270+
self.transform_literal_true("?literal_true"),
269271
),
270272
// We use this rule to transform: (?expr IN (?list..)) = TRUE and ((?expr IN (?list..)) = TRUE) = TRUE
271-
rewrite(
273+
transforming_rewrite(
272274
"filter-truncate-in-list-true",
273275
filter_replacer(
274-
binary_expr("?expr", "=", literal_bool(true)),
276+
binary_expr("?expr", "=", "?literal_true"),
275277
"?alias_to_cube",
276278
"?members",
277279
"?filter_aliases",
278280
),
279281
filter_replacer("?expr", "?alias_to_cube", "?members", "?filter_aliases"),
282+
self.transform_literal_true("?literal_true"),
280283
),
281284
transforming_rewrite(
282285
"filter-replacer",
@@ -2686,7 +2689,9 @@ impl FilterRules {
26862689
let new_limit_skip_var = var!(new_limit_skip_var);
26872690
let new_limit_fetch_var = var!(new_limit_fetch_var);
26882691
move |egraph, subst| {
2689-
for literal_value in var_iter!(egraph[subst[literal_var]], LiteralExprValue) {
2692+
if let Some(ConstantFolding::Scalar(literal_value)) =
2693+
&egraph[subst[literal_var]].data.constant
2694+
{
26902695
if let ScalarValue::Boolean(Some(false)) = literal_value {
26912696
subst.insert(
26922697
new_limit_var,
@@ -2707,6 +2712,23 @@ impl FilterRules {
27072712
}
27082713
}
27092714

2715+
fn transform_literal_true(
2716+
&self,
2717+
literal_var: &'static str,
2718+
) -> impl Fn(&mut EGraph<LogicalPlanLanguage, LogicalPlanAnalysis>, &mut Subst) -> bool {
2719+
let literal_var = var!(literal_var);
2720+
move |egraph, subst| {
2721+
if let Some(ConstantFolding::Scalar(literal)) =
2722+
&egraph[subst[literal_var]].data.constant
2723+
{
2724+
if let ScalarValue::Boolean(Some(true)) = literal {
2725+
return true;
2726+
}
2727+
}
2728+
false
2729+
}
2730+
}
2731+
27102732
fn push_down_limit_projection(
27112733
&self,
27122734
input_var: &'static str,

rust/cubesql/cubesql/src/compile/snapshots/cubesql__compile__tests__test_extract_string_field.snap

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
source: cubesql/src/compile/mod.rs
33
expression: "execute_query(\"SELECT EXTRACT('YEAR' FROM CAST ('2020-12-25 22:48:48.000' AS timestamptz))\".to_string(),\n DatabaseProtocol::PostgreSQL).await?"
44
---
5-
+-------------+
6-
| Int32(2020) |
7-
+-------------+
8-
| 2020 |
9-
+-------------+
5+
+---------------------------------------------------------------------------------------------+
6+
| datepart(Utf8("YEAR"),CAST(Utf8("2020-12-25 22:48:48.000") AS Timestamp(Nanosecond, None))) |
7+
+---------------------------------------------------------------------------------------------+
8+
| 2020 |
9+
+---------------------------------------------------------------------------------------------+

0 commit comments

Comments
 (0)