-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Format Date32
to string given timestamp specifiers
#15361
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
Format Date32
to string given timestamp specifiers
#15361
Conversation
52e1d29
to
1e57ed1
Compare
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.
Thanks @friendlymatthew -- something seems strange to me about this PR in that it is catching errors and reformatting. It seems like the allowable format options should be known based on the input data type 🤔
88d304e
to
ec2373a
Compare
|
||
// eagerly cast Date32 values to Date64 to support date formatting with time-related specifiers | ||
// without error. | ||
if data_type == &Date32 { |
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.
as @Omega359 says, this will now penalize performance for all existing Date32 columns
Is there any way we can check if the format string contains any time related specifiers before doing this conversion?
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.
It is possible to check for time formats in the format string however I'm not sure that cost would be any less than just doing the conversion up front. I think we need a benchmark to have quantified data vs assumptions.
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.
Some data from a quick little benchmark I wrote
test_date32_to_date64_cast_array_1000
time: [311.06 ns 314.48 ns 318.16 ns]
Found 4 outliers among 100 measurements (4.00%)
1 (1.00%) high mild
3 (3.00%) high severe
test_parse_performance_with_time_specifiers_array_1000
time: [343.79 ns 351.64 ns 359.98 ns]
Found 14 outliers among 100 measurements (14.00%)
10 (10.00%) low mild
3 (3.00%) high mild
1 (1.00%) high severe
test_parse_performance_without_time_specifiers_array_1000
time: [196.59 µs 201.06 µs 206.45 µs]
Found 10 outliers among 100 measurements (10.00%)
2 (2.00%) high mild
8 (8.00%) high severe
test_date32_to_date64_cast_array_1000: just casts from date32 to date64
test_parse_performance_with_time_specifiers_array_1000: parses the formats looking for time specifiers and when found does the cast from date32 to date64
test_parse_performance_without_time_specifiers_array_1000: parses the formats looking for time specifiers (doesn't find anything), no cast
Note that results on my machine will vary +-10% between runs. The check for time specifiers in the format is simple and could be improved but I think is enough to show general performance. Note that the list of time specifiers is not complete
const TIME_SPECIFIERS: &[&str] = &[
"%H", "%M", "%S", "%c", "%f", "%k", "%I", "%l", "%p", "%R", "%T", "%x", "%r", "%Z",
];
fn has_time_specifiers(str: &str) -> bool {
for specifier in TIME_SPECIFIERS {
if str.contains(specifier) {
return true;
}
}
false
}
A couple of takeaways from this. casting is not as cheap as I thought however parsing is seems to be more expensive than that but perhaps with some really good optimizations it could be reduced.
I don't see a good way to make this feature have no cost with Date32 && no time specifiers in the format.
I went ahead and did a quick poc using format parsing, you can see it @ https://github.com/Omega359/arrow-datafusion/tree/to_char_date32_with_time_formats (main...Omega359:arrow-datafusion:to_char_date32_with_time_formats) I haven't done a comparison with the benchmark results from main yet. |
I was planning on getting to this during the weekend, but wow, this is super awesome @Omega359! I love how Let me know how you want to proceed, I'd be happy to help out with benchmarking. |
@friendlymatthew - Running the benchmarks between main and the sidebranch would be very helpful - it should give us an idea as to the overhead of the format parsing for date32 data. |
Sounds great. I'll have some free time tomorrow to work on this. |
@Omega359 would you be open to committing to this branch? This way, I could base the benchmarking work on top of your work. If we decide to on a different approach than the experimented one, we could just roll back to a prior commit. |
Your branch you mean? I can try and push a PR to your branch once I figure out how :) |
I just sent an invite to collaborate on my fork. From there you should be able to directly push a commit to this branch! |
I messed up and pushed directly to your branch vs a PR, sorry about that @friendlymatthew |
Not at all, thank you and happy to collaborate on this together! |
Some benchmark results when comparing main with this branch: (TLDR) This branch will make the existing I compared two benchmark functions: These functions are concerned with formatting dates with date-only specifiers. From main: to_char_array_date_only_patterns_1000
time: [136.39 µs 136.68 µs 137.03 µs]
to_char_scalar_date_only_pattern_1000
time: [92.084 µs 95.999 µs 100.12 µs] And when compared to this branch: to_char_array_date_only_patterns_1000
time: [173.46 µs 174.21 µs 175.10 µs]
change: [+25.159% +25.881% +26.651%] (p = 0.00 < 0.05)
Performance has regressed.
Found 6 outliers among 100 measurements (6.00%)
6 (6.00%) high mild
to_char_scalar_date_only_pattern_1000
time: [105.04 µs 109.46 µs 113.80 µs]
change: [+6.6648% +11.410% +16.429%] (p = 0.00 < 0.05)
Performance has regressed. To reproduce:You can run the two benchmark functions by: cargo bench --package datafusion-functions "to_char_array_date_only_patterns_1000|to_char_scalar_date_only_pattern_1000" Since the benchmark function names clash between main and this branch, you can consider this branch as main. (This branch only contains the refactor benchmark logic). cc/ @Omega359 |
Slowing down existing functionality for current users for some new functionality doesn't sound like a great tradeoff to me -- if users want this behavior they can always override the to_char implementation If we can retain the same performance for existing to_char but add the new feature I think that is much more reasonable |
It's only slower when date32 is the input type. It's going to be hard to have this functionality for formats with timestamp specifiers but not have some sort of impact on those with just date specifiers. The only way I can think of to handle that is via catching the error when the parsing fails and retrying with date64. To put some context on this:
|
It may be beneficial to leave the existing code paths undisturbed and decide whether to retry on err. It avoids the overhead of custom to_char_scalar let formatter = ArrayFormatter::try_new(array.as_ref(), &format_options)?;
let formatted: Result<Vec<Option<String>>, ArrowError> = (0..array.len())
.map(|i| {
if array.is_null(i) {
Ok(None)
} else {
formatter.value(i).try_to_string().map(Some)
}
})
.collect(); /*
will stop iterating upon the first err and
since we're dealing with one format string,
it'll halt after the first format attempt
*/
if let Ok(formatted) = formatted {
if is_scalar_expression {
Ok(ColumnarValue::Scalar(ScalarValue::Utf8(
formatted.first().unwrap().clone(),
)))
} else {
Ok(ColumnarValue::Array(
Arc::new(StringArray::from(formatted)) as ArrayRef
))
}
} else {
// if the format attempt is with a Date32, it's possible the attempt failed because
// the format string contained time-specifiers, so we'll retry as Date64s
if data_type == &Date32 {
return to_char_scalar(expression.clone().cast_to(&Date64, None)?, format);
}
exec_err!("{}", formatted.unwrap_err())
}
FWIW, it's worth implementing the retry logic and benchmarking the performance. I'm happy to do the work. The decision between eager casting and selective retry likely comes down to whether we're willing to slightly slow down existing behavior in exchange for better performance in the new case. But I'm just spitballing. |
a855bb0
to
c69180b
Compare
I'll go ahead and close this |
@friendlymatthew - I think this actually should be reopened and just be updated to the previous version of the code then merged in - I think it's a worthy addition to DF. I think this issue just unfortunately dropped off the radar at the end :( |
Sure |
Thanks @Omega359 and @friendlymatthew -- when you are happy with it, please ping me and I will give it a final review |
@friendlymatthew - Are you be able to update this with the previous version of the code or would you mind if I made the changes either against your branch or a new branch and PR (giving you credit for it of course since you did the work!) ? |
Hi, please feel free to take over. You should have write access to my fork; you can also make a new PR if you'd like. I'm a bit swamped with work-- I need to close out some projects and go back to work related to arrow |
This reverts commit b379fe1.
…e32-with-ts-format # Conflicts: # datafusion/functions/benches/to_char.rs
🤖 |
🤖: Benchmark completed Details
|
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.
Thank you @friendlymatthew and @Omega359
While I suspect there is more performance to be had with this implementation, this looks like a step forward to me.
Thank you
// if the data type was a Date32, formatting could have failed because the format string | ||
// contained datetime specifiers, so we'll retry by casting the date array as a timestamp array | ||
if data_type == &Date32 { | ||
return to_char_scalar(expression.clone().cast_to(&Date64, None)?, format); |
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.
this clone seems unecessary. I'll make a PR to try and avoid some clones as a follow up
BTW, that script looks like it could be interesting but I don't see it in your repo. Any chance you could push that one up? |
Soryr -- I think the link is wrong. The script is here: https://github.com/alamb/datafusion-benchmarking/blob/main/scripts/gh_compare_branch_bench.sh I will update the checkout |
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.
I also filed a ticket to track improving the performance even more
Thanks again for helping get this over the line @Omega359 |
to_char(date, timstamp format)
#14536Rationale for this change
Datafusion currently errs when attempting to format a date using time-related specifiers.
However, Postgres supports this feature as it implicitly treats the date as a timestamp.
Rather than eagerly casting every
Date32
to aDate64
when callingto_char
, this commit attempts to first format aDate32
with the supplied format string. If the formatting fails, we try to reformat as aDate64
. This way, only format strings with time-related specifiers endure the intermediary cast.All changes are tested, specifically for two different call sites:
_to_char_scalar
and_to_char_array
.