Skip to content

Commit e4f43ff

Browse files
perf: Cast entire Date32 array to Date64 on 1st failure (apache#21948)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes apache#123` indicates that this PR will close issue apache#123. --> - Closes apache#17152 ## Rationale for this change <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> - [x] 1. `Vec<Option<String>>` extra allocations - Resolved in apache#20635 by using `StringBuilder` - [ ] 2. Per-row cast on fallback - Will be resolved in this PR ```rust // When `Date32` formatting fails (due to time specifiers), // the code called cast individually for each row instead of converting the entire array let date64_value = cast(&data_array.slice(idx, 1), &Date64)?; ``` ## What changes are included in this PR? <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> Cast the entire Date32 array to Date64 once on first failure, instead of per-row ```rust cast(data_array.as_ref(), &Date64 ``` ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 4. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> Yes - Add 2 benchmarks: - `Date32 + datetime` patterns (all rows trigger the fallback) - `Date32 + mixed patterns` (roughly half do) ```bash ┌─────────────────────────────────────┬───────────────────┬───────────┐ │ Benchmark │ Format │ Change │ ├─────────────────────────────────────┼───────────────────┼───────────┤ │ array_date_only_patterns_1000 │ array, date-only │ No change │ ├─────────────────────────────────────┼───────────────────┼───────────┤ │ array_datetime_patterns_1000 │ array, datetime │ -3.21% │ ├─────────────────────────────────────┼───────────────────┼───────────┤ │ array_mixed_patterns_1000 │ array, mixed │ +1.41% │ ├─────────────────────────────────────┼───────────────────┼───────────┤ │ scalar_date_only_pattern_1000 │ scalar, date-only │ No change │ ├─────────────────────────────────────┼───────────────────┼───────────┤ │ scalar_datetime_pattern_1000 │ scalar, datetime │ No change │ ├─────────────────────────────────────┼───────────────────┼───────────┤ │ array_date32_datetime_patterns_1000 │ array, datetime │ -20.24% │ ├─────────────────────────────────────┼───────────────────┼───────────┤ │ array_date32_mixed_patterns_1000 │ array, mixed │ -15.74% │ └─────────────────────────────────────┴───────────────────┴───────────┘ ``` <details> <summary>Detail</summary> ```bash cargo bench --bench to_char Compiling datafusion-functions v53.1.0 (/Users/qmac/Documents/GitHub/datafusion/datafusion/functions) Finished `bench` profile [optimized] target(s) in 1m 10s Running benches/to_char.rs (target/release/deps/to_char-d2acea4b7a3e2fba) Gnuplot not found, using plotters backend Benchmarking to_char_array_date_only_patterns_1000: Warming up for 3.00Benchmarking to_char_array_date_only_patterns_1000: Collecting 100 sampto_char_array_date_only_patterns_1000 time: [104.01 µs 104.37 µs 104.74 µs] change: [−0.5692% +0.3244% +1.0788%] (p = 0.47 > 0.05) No change in performance detected. Found 3 outliers among 100 measurements (3.00%) 1 (1.00%) low mild 2 (2.00%) high mild Benchmarking to_char_array_datetime_patterns_1000: Warming up for 3.000Benchmarking to_char_array_datetime_patterns_1000: Collecting 100 samplto_char_array_datetime_patterns_1000 time: [165.96 µs 166.43 µs 166.94 µs] change: [−4.1660% −3.2055% −2.3626%] (p = 0.00 < 0.05) Performance has improved. Found 6 outliers among 100 measurements (6.00%) 2 (2.00%) low mild 3 (3.00%) high mild 1 (1.00%) high severe Benchmarking to_char_array_mixed_patterns_1000: Collecting 100 samples to_char_array_mixed_patterns_1000 time: [139.58 µs 140.02 µs 140.49 µs] change: [+1.0116% +1.4122% +1.8178%] (p = 0.00 < 0.05) Performance has regressed. Found 3 outliers among 100 measurements (3.00%) 1 (1.00%) low mild 2 (2.00%) high mild Benchmarking to_char_scalar_date_only_pattern_1000: Warming up for 3.00Benchmarking to_char_scalar_date_only_pattern_1000: Collecting 100 sampto_char_scalar_date_only_pattern_1000 time: [60.073 µs 63.184 µs 66.309 µs] change: [−4.0228% +0.6839% +6.1581%] (p = 0.79 > 0.05) No change in performance detected. Benchmarking to_char_scalar_datetime_pattern_1000: Warming up for 3.000Benchmarking to_char_scalar_datetime_pattern_1000: Collecting 100 samplto_char_scalar_datetime_pattern_1000 time: [117.65 µs 124.76 µs 131.84 µs] change: [−3.3009% +2.6889% +8.6148%] (p = 0.37 > 0.05) No change in performance detected. Found 30 outliers among 100 measurements (30.00%) 16 (16.00%) low mild 14 (14.00%) high mild Benchmarking to_char_array_date32_datetime_patterns_1000: Warming up foBenchmarking to_char_array_date32_datetime_patterns_1000: Collecting 10to_char_array_date32_datetime_patterns_1000 time: [289.67 µs 290.77 µs 291.88 µs] change: [−20.661% −20.240% −19.863%] (p = 0.00 < 0.05) Performance has improved. Benchmarking to_char_array_date32_mixed_patterns_1000: Warming up for 3Benchmarking to_char_array_date32_mixed_patterns_1000: Collecting 100 sto_char_array_date32_mixed_patterns_1000 time: [194.47 µs 195.28 µs 196.11 µs] change: [−16.230% −15.738% −15.285%] (p = 0.00 < 0.05) Performance has improved. ``` </details> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. --> No --------- Co-authored-by: Kumar Ujjawal <ujjawalpathak6@gmail.com>
1 parent 739e147 commit e4f43ff

1 file changed

Lines changed: 30 additions & 10 deletions

File tree

datafusion/functions/src/datetime/to_char.rs

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ fn to_char_array(args: &[ColumnarValue]) -> Result<ColumnarValue> {
248248
StringBuilder::with_capacity(data_array.len(), data_array.len() * fmt_len);
249249
let mut buffer = String::with_capacity(fmt_len);
250250

251+
// Lazily computed Date64 cast of the entire array, used when a Date32
252+
// format string contains time specifiers that the Date32 formatter
253+
// cannot handle. Cast once and reuse for all subsequent rows
254+
let mut date64_array: Option<ArrayRef> = None;
255+
251256
for idx in 0..data_array.len() {
252257
if format_array.is_null(idx) || data_array.is_null(idx) {
253258
builder.append_null();
@@ -266,13 +271,18 @@ fn to_char_array(args: &[ColumnarValue]) -> Result<ColumnarValue> {
266271
// buffer and `append_value` on success.
267272
match formatter.value(idx).write(&mut buffer) {
268273
Ok(()) => builder.append_value(&buffer),
269-
// Retry with Date64 (see comment in to_char_scalar).
270274
Err(_) if data_type == &Date32 => {
271275
buffer.clear();
272-
let date64_value = cast(&data_array.slice(idx, 1), &Date64)?;
273-
let retry_fmt =
274-
ArrayFormatter::try_new(date64_value.as_ref(), &format_options)?;
275-
retry_fmt.value(0).write(&mut buffer)?;
276+
let date64_ref = match &date64_array {
277+
Some(arr) => arr.as_ref(),
278+
None => {
279+
date64_array = Some(cast(data_array.as_ref(), &Date64)?);
280+
date64_array.as_ref().unwrap().as_ref()
281+
}
282+
};
283+
let retry_options = build_format_options(&Date64, format)?;
284+
let retry_fmt = ArrayFormatter::try_new(date64_ref, &retry_options)?;
285+
retry_fmt.value(idx).write(&mut buffer)?;
276286
builder.append_value(&buffer);
277287
}
278288
Err(e) => return Err(e.into()),
@@ -307,11 +317,21 @@ mod tests {
307317

308318
#[test]
309319
fn test_array_array() {
310-
let array_array_data = vec![(
311-
Arc::new(Date32Array::from(vec![18506, 18507])) as ArrayRef,
312-
StringArray::from(vec!["%Y::%m::%d", "%Y::%m::%d %S::%M::%H %f"]),
313-
StringArray::from(vec!["2020::09::01", "2020::09::02 00::00::00 000000000"]),
314-
)];
320+
let array_array_data = vec![
321+
(
322+
Arc::new(Date32Array::from(vec![18506, 18507])) as ArrayRef,
323+
StringArray::from(vec!["%Y::%m::%d", "%Y::%m::%d %S::%M::%H %f"]),
324+
StringArray::from(vec![
325+
"2020::09::01",
326+
"2020::09::02 00::00::00 000000000",
327+
]),
328+
),
329+
(
330+
Arc::new(Date32Array::from(vec![18506, 18507])) as ArrayRef,
331+
StringArray::from(vec!["%Y::%m::%d %H:%M:%S", "%d-%m-%Y %H:%M"]),
332+
StringArray::from(vec!["2020::09::01 00:00:00", "02-09-2020 00:00"]),
333+
),
334+
];
315335

316336
for (value, format, expected) in array_array_data {
317337
let batch_len = value.len();

0 commit comments

Comments
 (0)