Skip to content

Commit d0304b3

Browse files
theirixJefffrey
andauthored
perf: optimisation for date_part with seconds (#23444)
## Which issue does this PR close? - Closes #23351. ## Rationale for this change Benches were added recently in #23350 . Improve and measure some optimisations. ## What changes are included in this PR? - Improve performance for `seconds_ns` and `seconds_as_i32`. A pretty rare code path. ## Are these changes tested? - Tests are passing - Bench-measured perf is improving ## 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. --> --------- Co-authored-by: Jeffrey Vo <[email protected]>
1 parent 61518a8 commit d0304b3

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

‎datafusion/functions/src/datetime/date_part.rs‎

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
// specific language governing permissions and limitations
1616
// under the License.
1717

18+
use std::iter::repeat_n;
1819
use std::str::FromStr;
1920
use std::sync::Arc;
2021

@@ -390,14 +391,27 @@ fn part_normalization(part: &str) -> &str {
390391

391392
/// Invoke [`date_part`] on an `array` (e.g. Timestamp) and convert the
392393
/// result to a total number of seconds, milliseconds, microseconds or
393-
/// nanoseconds
394+
/// nanoseconds as an `Int32Array`
394395
fn seconds_as_i32(array: &dyn Array, unit: TimeUnit) -> Result<ArrayRef> {
395396
// Nanosecond is neither supported in Postgres nor DuckDB, to avoid dealing
396397
// with overflow and precision issue we don't support nanosecond
397398
if unit == Nanosecond {
398399
return not_impl_err!("Date part {unit:?} not supported");
399400
}
400401

402+
// Fast path with seconds - no need to compute nanoseconds
403+
if unit == Second {
404+
return Ok(date_part(array, DatePart::Second)?);
405+
}
406+
407+
// Fast path for Date32 and Date64 - no seconds
408+
if array.data_type() == &Date32 || array.data_type() == &Date64 {
409+
return Ok(Arc::new(Int32Array::from_iter_values_with_nulls(
410+
repeat_n(0, array.len()),
411+
array.nulls().cloned(),
412+
)));
413+
}
414+
401415
let conversion_factor = match unit {
402416
Second => 1_000_000_000,
403417
Millisecond => 1_000_000,
@@ -539,6 +553,14 @@ fn epoch(array: &dyn Array) -> Result<ArrayRef> {
539553
/// `nanosecond`s in each second, so representing up to 60 seconds as
540554
/// nanoseconds can be values up to 60 billion, which does not fit in Int32.
541555
fn seconds_ns(array: &dyn Array) -> Result<ArrayRef> {
556+
// Fast path for Date32 and Date64 - no nanoseconds
557+
if array.data_type() == &Date32 || array.data_type() == &Date64 {
558+
return Ok(Arc::new(Int64Array::from_iter_values_with_nulls(
559+
repeat_n(0, array.len()),
560+
array.nulls().cloned(),
561+
)));
562+
}
563+
542564
let secs = date_part(array, DatePart::Second)?;
543565
// This assumes array is primitive and not a dictionary
544566
let secs = as_int32_array(secs.as_ref())?;

‎datafusion/sqllogictest/test_files/datetime/date_part.slt‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,40 @@ SELECT extract(millisecond from arrow_cast('23:32:50.123456789'::time, 'Time64(N
838838
----
839839
50123
840840

841+
# date32 and date64
842+
843+
statement ok
844+
CREATE TABLE source_dt AS
845+
with t as (values
846+
('1970-01-01'),
847+
('2020-06-02'),
848+
('2026-02-28'),
849+
(NULL)
850+
)
851+
SELECT
852+
arrow_cast(column1, 'Date32') as date32,
853+
arrow_cast(column1, 'Date64') as date64,
854+
FROM t;
855+
856+
query IIIIIIIIII
857+
SELECT date_part('year', date32), date_part('month', date32), date_part('week', date32), date_part('day', date32), date_part('hour', date32), date_part('minute', date32), date_part('second', date32), date_part('millisecond', date32), date_part('microsecond', date32), date_part('nanosecond', date32) FROM source_dt;
858+
----
859+
1970 1 1 1 0 0 0 0 0 0
860+
2020 6 23 2 0 0 0 0 0 0
861+
2026 2 9 28 0 0 0 0 0 0
862+
NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
863+
864+
query IIIIIIIIII
865+
SELECT date_part('year', date64), date_part('month', date64), date_part('week', date64), date_part('day', date64), date_part('hour', date64), date_part('minute', date64), date_part('second', date64), date_part('millisecond', date64), date_part('microsecond', date64), date_part('nanosecond', date64) FROM source_dt;
866+
----
867+
1970 1 1 1 0 0 0 0 0 0
868+
2020 6 23 2 0 0 0 0 0 0
869+
2026 2 9 28 0 0 0 0 0 0
870+
NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL
871+
872+
statement ok
873+
drop table source_dt;
874+
841875
# just some floating point stuff happening in the result here
842876
query I
843877
SELECT date_part('microsecond', arrow_cast('23:32:50.123456789'::time, 'Time64(Nanosecond)'))

0 commit comments

Comments
 (0)