-
Notifications
You must be signed in to change notification settings - Fork 340
perf: optimize date_trunc (>2x faster)
#4915
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
Open
andygrove
wants to merge
1
commit into
apache:main
Choose a base branch
from
andygrove:auto-opt/date_trunc-datafusion-comet-20260714-032832
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+146
−24
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| use arrow::array::{ArrayRef, Date32Array, StringArray}; | ||
| use arrow::datatypes::{DataType, Field}; | ||
| use criterion::{criterion_group, criterion_main, Criterion}; | ||
| use datafusion::common::config::ConfigOptions; | ||
| use datafusion::logical_expr::{ColumnarValue, ScalarFunctionArgs, ScalarUDFImpl}; | ||
| use datafusion_comet_spark_expr::SparkDateTrunc; | ||
| use std::hint::black_box; | ||
| use std::sync::Arc; | ||
|
|
||
| const SIZE: usize = 8192; | ||
|
|
||
| fn create_date_array() -> ArrayRef { | ||
| let dates: Vec<i32> = (0..SIZE).map(|i| ((i * 2) % 19000) as i32).collect(); | ||
| Arc::new(Date32Array::from(dates)) | ||
| } | ||
|
|
||
| /// A constant format column, which is what a query like `trunc(d, fmt)` produces when the | ||
| /// format comes from a column rather than a literal. | ||
| fn create_constant_format_array(format: &str) -> ArrayRef { | ||
| Arc::new(StringArray::from(vec![format; SIZE])) | ||
| } | ||
|
|
||
| /// A low-cardinality format column mixing the supported formats. | ||
| fn create_mixed_format_array() -> ArrayRef { | ||
| let formats = ["YEAR", "quarter", "MONTH", "week"]; | ||
| let values: Vec<&str> = (0..SIZE).map(|i| formats[i % formats.len()]).collect(); | ||
| Arc::new(StringArray::from(values)) | ||
| } | ||
|
|
||
| fn invoke(udf: &SparkDateTrunc, dates: &ArrayRef, formats: &ArrayRef) -> ColumnarValue { | ||
| let args = ScalarFunctionArgs { | ||
| args: vec![ | ||
| ColumnarValue::Array(Arc::clone(dates)), | ||
| ColumnarValue::Array(Arc::clone(formats)), | ||
| ], | ||
| number_rows: SIZE, | ||
| return_field: Arc::new(Field::new("date_trunc", DataType::Date32, true)), | ||
| arg_fields: vec![], | ||
| config_options: Arc::new(ConfigOptions::default()), | ||
| }; | ||
| udf.invoke_with_args(args).unwrap() | ||
| } | ||
|
|
||
| fn criterion_benchmark(c: &mut Criterion) { | ||
| let udf = SparkDateTrunc::new(); | ||
| let dates = create_date_array(); | ||
|
|
||
| let mut group = c.benchmark_group("date_trunc_array_fmt"); | ||
|
|
||
| for format in ["YEAR", "QUARTER", "MONTH", "WEEK"] { | ||
| let formats = create_constant_format_array(format); | ||
| group.bench_function(format.to_lowercase(), |b| { | ||
| b.iter(|| black_box(invoke(&udf, &dates, &formats))) | ||
| }); | ||
| } | ||
|
|
||
| let formats = create_mixed_format_array(); | ||
| group.bench_function("mixed", |b| { | ||
| b.iter(|| black_box(invoke(&udf, &dates, &formats))) | ||
| }); | ||
|
|
||
| group.finish(); | ||
| } | ||
|
|
||
| fn config() -> Criterion { | ||
| Criterion::default() | ||
| } | ||
|
|
||
| criterion_group! { | ||
| name = benches; | ||
| config = config(); | ||
| targets = criterion_benchmark | ||
| } | ||
| criterion_main!(benches); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Every entry in
DATE_TRUNC_FORMATSis ASCII.eq_ignore_ascii_caseonly folds ASCII bytes, so a non-ASCIIkeycan never equal an ASCII key regardless of whether it wasto_uppercased first. Theelsebranch allocates aStringand then cannot change the outcome: a non-ASCII format always falls through to the error. The comment claiming non-ASCII "still needs full Unicode case folding to fold the same way Spark does" is incorrect for this table, because Spark also only matches ASCII literals after folding, so a non-ASCII input is invalid in Spark too.Suggested change: drop the
Cowand theis_ascii()split entirely and match on the borrowed&str:This removes the
use std::borrow::Cow;import, removes an allocation on the non-ASCII path, and is exactly as Spark-compatible.