-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Constant fold / optimize to_timestamp
function during planning
#387
Changes from 4 commits
ae0785a
ff2a20b
c32d2f5
43d01ef
6663671
21acdb5
2b9b9b7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ use crate::optimizer::optimizer::OptimizerRule; | |
use crate::optimizer::utils; | ||
use crate::physical_plan::functions::BuiltinScalarFunction; | ||
use crate::scalar::ScalarValue; | ||
use arrow::compute::kernels::cast_utils::string_to_timestamp_nanos; | ||
|
||
/// Optimizer that simplifies comparison expressions involving boolean literals. | ||
/// | ||
|
@@ -217,6 +218,35 @@ impl<'a> ExprRewriter for ConstantRewriter<'a> { | |
.query_execution_start_time | ||
.timestamp_nanos(), | ||
))), | ||
Expr::ScalarFunction { | ||
fun: BuiltinScalarFunction::ToTimestamp, | ||
args, | ||
} => { | ||
if !args.is_empty() { | ||
match &args[0] { | ||
Expr::Literal(ScalarValue::Utf8(Some(val))) => { | ||
match string_to_timestamp_nanos(val) { | ||
Ok(timestamp) => Expr::Literal( | ||
ScalarValue::TimestampNanosecond(Some(timestamp)), | ||
), | ||
_ => Expr::ScalarFunction { | ||
fun: BuiltinScalarFunction::ToTimestamp, | ||
args, | ||
}, | ||
} | ||
} | ||
_ => Expr::ScalarFunction { | ||
fun: BuiltinScalarFunction::ToTimestamp, | ||
args, | ||
}, | ||
} | ||
} else { | ||
Expr::ScalarFunction { | ||
fun: BuiltinScalarFunction::ToTimestamp, | ||
args, | ||
} | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What happen if the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In case of all error cases including empty, we don't optimise. So the default non-optimised flow continues. |
||
expr => { | ||
// no rewrite possible | ||
expr | ||
|
@@ -632,6 +662,29 @@ mod tests { | |
return format!("{:?}", optimized_plan); | ||
} | ||
|
||
#[test] | ||
fn to_timestamp_expr() { | ||
let table_scan = test_table_scan().unwrap(); | ||
let proj = vec![Expr::ScalarFunction { | ||
args: vec![Expr::Literal(ScalarValue::Utf8(Some( | ||
"2020-09-08T12:00:00+00:00".to_string(), | ||
)))], | ||
fun: BuiltinScalarFunction::ToTimestamp, | ||
}]; | ||
let plan = LogicalPlanBuilder::from(&table_scan) | ||
.project(proj) | ||
.unwrap() | ||
.build() | ||
.unwrap(); | ||
|
||
let expected = format!( | ||
"Projection: TimestampNanosecond(1599566400000000000)\ | ||
\n TableScan: test projection=None", | ||
); | ||
let actual = get_optimized_plan_formatted(&plan, &chrono::Utc::now()); | ||
assert_eq!(expected, actual); | ||
msathis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
msathis marked this conversation as resolved.
Show resolved
Hide resolved
|
||
#[test] | ||
fn single_now_expr() { | ||
let table_scan = test_table_scan().unwrap(); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Matching on the literal argument is good as the
rewrite
pass happens after all children are visited 👍