-
Notifications
You must be signed in to change notification settings - Fork 340
perf: optimize date_parser / cast string to date (up to 2x faster)
#4917
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
2
commits into
apache:main
Choose a base branch
from
andygrove:auto-opt/date_parser-datafusion-comet-20260714-043541
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.
+183
−64
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
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,84 @@ | ||
| // 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::{builder::StringBuilder, RecordBatch}; | ||
| use arrow::datatypes::{DataType, Field, Schema}; | ||
| use criterion::{criterion_group, criterion_main, Criterion}; | ||
| use datafusion::physical_expr::{expressions::Column, PhysicalExpr}; | ||
| use datafusion_comet_spark_expr::{Cast, EvalMode, SparkCastOptions}; | ||
| use std::sync::Arc; | ||
|
|
||
| const BATCH_SIZE: usize = 8192; | ||
|
|
||
| fn criterion_benchmark(c: &mut Criterion) { | ||
| let expr = Arc::new(Column::new("a", 0)); | ||
| let cast_to_date = Cast::new( | ||
| expr, | ||
| DataType::Date32, | ||
| SparkCastOptions::new(EvalMode::Legacy, "UTC", false), | ||
| None, | ||
| None, | ||
| ); | ||
|
|
||
| let canonical = | ||
| create_batch(|i| format!("{:04}-{:02}-{:02}", 1970 + i % 60, i % 12 + 1, i % 28 + 1)); | ||
| let mixed = create_batch(|i| match i % 5 { | ||
| 0 => format!("{:04}-{:02}-{:02}", 1970 + i % 60, i % 12 + 1, i % 28 + 1), | ||
| 1 => format!("{}-{}-{} 12:34:56", 1970 + i % 60, i % 12 + 1, i % 28 + 1), | ||
| 2 => format!( | ||
| " {:04}-{:02}-{:02} ", | ||
| 1900 + i % 200, | ||
| i % 12 + 1, | ||
| i % 28 + 1 | ||
| ), | ||
| 3 => format!("{:04}", 1970 + i % 60), | ||
| _ => "not a date".to_string(), | ||
| }); | ||
|
|
||
| let mut group = c.benchmark_group("cast_string_to_date"); | ||
| group.bench_function("canonical", |b| { | ||
| b.iter(|| cast_to_date.evaluate(&canonical).unwrap()); | ||
| }); | ||
| group.bench_function("mixed", |b| { | ||
| b.iter(|| cast_to_date.evaluate(&mixed).unwrap()); | ||
| }); | ||
| group.finish(); | ||
| } | ||
|
|
||
| fn create_batch(f: impl Fn(usize) -> String) -> RecordBatch { | ||
| let schema = Arc::new(Schema::new(vec![Field::new("a", DataType::Utf8, true)])); | ||
| let mut builder = StringBuilder::new(); | ||
| for i in 0..BATCH_SIZE { | ||
| if i % 17 == 0 { | ||
| builder.append_null(); | ||
| } else { | ||
| builder.append_value(f(i)); | ||
| } | ||
| } | ||
| RecordBatch::try_new(schema, vec![Arc::new(builder.finish())]).unwrap() | ||
| } | ||
|
|
||
| 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.
date_parser_test(native/spark-expr/src/conversion_funcs/string.rs:2712) never feeds a canonical-shapedddd-dd-ddstring that is an invalid calendar date, so the branch where the fast path callsymd_to_epoch_dayand getsNoneis untested. The existing invalid-date cases (2020-010-01,202,2020-\r8) all fall through to the general parser because they are not 10-chardddd-dd-dd. A regression that made the fast path skip calendar validation would pass the current suite.Suggested change: add to the invalid list in
date_parser_test(which assertsNonefor Legacy/Try andis_err()for Ansi is wrong here, see finding 2, so put these in a Legacy/Try-only null assertion) canonical invalid dates and one valid boundary: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.
Added canonical-form invalid dates (
2020-02-30,2021-02-29,2020-13-01,2020-00-15,2020-04-31,2020-01-00) assertingNonefor all three eval modes, plus a valid leap-day case (2020-02-29→18321) that exercises the successful fast path.