Skip to content

Commit

Permalink
Apply to all nodes and update tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
viirya committed Nov 20, 2021
1 parent 8d715de commit eec7cbe
Showing 1 changed file with 15 additions and 26 deletions.
41 changes: 15 additions & 26 deletions datafusion/src/optimizer/constant_folding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,20 +108,8 @@ impl OptimizerRule for ConstantFolding {

let new_name = &new_e.name(plan.schema());

// Some plans will be candidates in projection pushdown rule to
// trim expressions based on expression names. We need to keep
// expression name for them.
let is_plan_for_projection_pushdown = matches!(
plan,
LogicalPlan::Window { .. }
| LogicalPlan::Aggregate { .. }
| LogicalPlan::Union { .. }
);

if let (Ok(expr_name), Ok(new_expr_name)) = (name, new_name) {
if expr_name != new_expr_name
&& is_plan_for_projection_pushdown
{
if expr_name != new_expr_name {
Ok(new_e.alias(expr_name))
} else {
Ok(new_e)
Expand Down Expand Up @@ -653,8 +641,8 @@ mod tests {

let expected = "\
Projection: #test.a\
\n Filter: NOT #test.c\
\n Filter: #test.b\
\n Filter: NOT #test.c AS test.c = Boolean(false)\
\n Filter: #test.b AS test.b = Boolean(true)\
\n TableScan: test projection=None";

assert_optimized_plan_eq(&plan, expected);
Expand All @@ -674,8 +662,8 @@ mod tests {
let expected = "\
Projection: #test.a\
\n Limit: 1\
\n Filter: #test.c\
\n Filter: NOT #test.b\
\n Filter: #test.c AS test.c != Boolean(false)\
\n Filter: NOT #test.b AS test.b != Boolean(true)\
\n TableScan: test projection=None";

assert_optimized_plan_eq(&plan, expected);
Expand All @@ -692,7 +680,7 @@ mod tests {

let expected = "\
Projection: #test.a\
\n Filter: NOT #test.b AND #test.c\
\n Filter: NOT #test.b AND #test.c AS test.b != Boolean(true) AND test.c = Boolean(true)\
\n TableScan: test projection=None";

assert_optimized_plan_eq(&plan, expected);
Expand All @@ -709,7 +697,7 @@ mod tests {

let expected = "\
Projection: #test.a\
\n Filter: NOT #test.b OR NOT #test.c\
\n Filter: NOT #test.b OR NOT #test.c AS test.b != Boolean(true) OR test.c = Boolean(false)\
\n TableScan: test projection=None";

assert_optimized_plan_eq(&plan, expected);
Expand All @@ -726,7 +714,7 @@ mod tests {

let expected = "\
Projection: #test.a\
\n Filter: #test.b\
\n Filter: #test.b AS NOT test.b = Boolean(false)\
\n TableScan: test projection=None";

assert_optimized_plan_eq(&plan, expected);
Expand All @@ -741,7 +729,7 @@ mod tests {
.build()?;

let expected = "\
Projection: #test.a, #test.d, NOT #test.b\
Projection: #test.a, #test.d, NOT #test.b AS test.b = Boolean(false)\
\n TableScan: test projection=None";

assert_optimized_plan_eq(&plan, expected);
Expand Down Expand Up @@ -816,7 +804,7 @@ mod tests {
.build()
.unwrap();

let expected = "Projection: TimestampNanosecond(1599566400000000000)\
let expected = "Projection: TimestampNanosecond(1599566400000000000) AS totimestamp(Utf8(\"2020-09-08T12:00:00+00:00\"))\
\n TableScan: test projection=None"
.to_string();
let actual = get_optimized_plan_formatted(&plan, &Utc::now());
Expand Down Expand Up @@ -851,7 +839,7 @@ mod tests {
.build()
.unwrap();

let expected = "Projection: Int32(0)\
let expected = "Projection: Int32(0) AS CAST(Utf8(\"0\") AS Int32)\
\n TableScan: test projection=None";
let actual = get_optimized_plan_formatted(&plan, &Utc::now());
assert_eq!(expected, actual);
Expand Down Expand Up @@ -900,7 +888,7 @@ mod tests {
// expect the same timestamp appears in both exprs
let actual = get_optimized_plan_formatted(&plan, &time);
let expected = format!(
"Projection: TimestampNanosecond({}), TimestampNanosecond({}) AS t2\
"Projection: TimestampNanosecond({}) AS now(), TimestampNanosecond({}) AS t2\
\n TableScan: test projection=None",
time.timestamp_nanos(),
time.timestamp_nanos()
Expand All @@ -924,7 +912,8 @@ mod tests {
.unwrap();

let actual = get_optimized_plan_formatted(&plan, &time);
let expected = "Projection: NOT #test.a\
let expected =
"Projection: NOT #test.a AS Boolean(true) OR Boolean(false) != test.a\
\n TableScan: test projection=None";

assert_eq!(actual, expected);
Expand Down Expand Up @@ -956,7 +945,7 @@ mod tests {

// Note that constant folder runs and folds the entire
// expression down to a single constant (true)
let expected = "Filter: Boolean(true)\
let expected = "Filter: Boolean(true) AS CAST(now() AS Int64) < CAST(totimestamp(Utf8(\"2020-09-08T12:05:00+00:00\")) AS Int64) + Int32(50000)\
\n TableScan: test projection=None";
let actual = get_optimized_plan_formatted(&plan, &time);

Expand Down

0 comments on commit eec7cbe

Please sign in to comment.