Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move code to fold Stable functions like now() from Simplifier to ConstEvaluator #1176

Merged
merged 1 commit into from
Oct 28, 2021

Conversation

alamb
Copy link
Contributor

@alamb alamb commented Oct 25, 2021

Which issue does this PR close?

Resolves #1175

Rationale for this change

This is a follow on suggestion from @rdettai on #1153 https://github.com/apache/arrow-datafusion/pull/1153/files#r735437280

Namely the substitution of now() for the current value is better described as "constant evaluation" rather than "algebraic simplification" so it should be done in the ConstEvaluator code.

It also has the very nice property that expressions that include now() can also be more completely evaluated (will comment inline)

What changes are included in this PR?

  1. Move code to fold Stable functions like now() from Simplifier() to ConstEvaluator
  2. Fix code that did not apply const evaluator to filter predicates

Are there any user-facing changes?

MOAR constant folding!

@github-actions github-actions bot added the datafusion Changes in the datafusion crate label Oct 25, 2021
@alamb alamb marked this pull request as draft October 25, 2021 19:53

match plan {
LogicalPlan::Filter { predicate, input } => Ok(LogicalPlan::Filter {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no reason to special case LogicalPlan::Filter as the predicate is handled by LogicalPlan::expressions -- and if you look carefully this doesn't call rewrite using the const_evaluator (I totally missed this in #1153 ) but found it while updating tests in this PR

@@ -228,15 +220,6 @@ impl<'a> ExprRewriter for Simplifier<'a> {
Expr::Not(inner)
}
}
// convert now() --> the time in `ExecutionProps`
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point of this PR is to remove this code (it is now handled by ConstEvaluator)

@@ -753,27 +732,6 @@ mod tests {
}
}

#[test]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

covered in the tests for ConstEvaluator

let expected = "Filter: TimestampNanosecond(1599566400000000000) < CAST(totimestamp(Utf8(\"2020-09-08T12:05:00+00:00\")) AS Int64) + Int32(50000)\
// Note that constant folder runs and folds the entire
// expression down to a single constant (true)
let expected = "Filter: Boolean(true)\
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The filter expression has been totally simplified 🎉

@@ -838,17 +796,16 @@ mod tests {
// now() < cast(to_timestamp(...) as int) + 5000000000
let plan = LogicalPlanBuilder::from(table_scan)
.filter(
now_expr()
cast_to_int64_expr(now_expr())
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It will be pretty awesome when we get #194 so casting to do timestamp arithmetic is no longer needed

// To evaluate stable functions, need ExecutionProps, see
// Simplifier for code that does that.
Volatility::Stable => false,
// Values for functions such as now() are taken from ExecutionProps
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the actual change that allows the const evaluator to replace now() with a constant.

@alamb alamb changed the title Move code to fold Stable functions like now() from Simplifier to ConstEvaluator (WIP) Move code to fold Stable functions like now() from Simplifier to ConstEvaluator Oct 26, 2021
@alamb alamb changed the title (WIP) Move code to fold Stable functions like now() from Simplifier to ConstEvaluator Move code to fold Stable functions like now() from Simplifier to ConstEvaluator Oct 27, 2021
@alamb alamb marked this pull request as ready for review October 27, 2021 23:28
@alamb
Copy link
Contributor Author

alamb commented Oct 27, 2021

This one is now ready for review @rdettai @houqp and @Dandandan

Copy link
Contributor

@rdettai rdettai left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! thanks Andrew 😃

@alamb alamb merged commit 2c8e65b into apache:master Oct 28, 2021
@houqp houqp added the enhancement New feature or request label Nov 6, 2021
@alamb alamb deleted the alamb/move_scalar branch August 8, 2023 20:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
datafusion Changes in the datafusion crate enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Move code to fold now() from Simplifier() to ConstEvaluator
3 participants