This is an automated email from the ASF dual-hosted git repository.
github-bot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git
The following commit(s) were added to refs/heads/main by this push:
new 44dfa7b8a5 Clean up date_part preimage implementation (#20350)
44dfa7b8a5 is described below
commit 44dfa7b8a5b908d6d82233cc6aaea1137b1d54b5
Author: Konstantin Tarasov <[email protected]>
AuthorDate: Mon Mar 9 16:52:43 2026 -0400
Clean up date_part preimage implementation (#20350)
## 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 #123` indicates that this PR will close issue #123.
-->
- Closes #20349.
## Rationale for this change
Check issue
<!--
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.
-->
## What changes are included in this PR?
Replaced manual timestamp tz calculation with a new
`ArrowTimeStampType::from_naive_datetime` API.
<!--
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.
-->
## 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
2. 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)?
-->
## Are there any user-facing changes?
Yes
<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->
No
<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->
---------
Co-authored-by: Andrew Lamb <[email protected]>
---
datafusion/functions/src/datetime/date_part.rs | 46 ++++++++++++--------------
1 file changed, 21 insertions(+), 25 deletions(-)
diff --git a/datafusion/functions/src/datetime/date_part.rs
b/datafusion/functions/src/datetime/date_part.rs
index f3d676f11d..8e93b238b2 100644
--- a/datafusion/functions/src/datetime/date_part.rs
+++ b/datafusion/functions/src/datetime/date_part.rs
@@ -28,10 +28,11 @@ use arrow::datatypes::DataType::{
};
use arrow::datatypes::TimeUnit::{Microsecond, Millisecond, Nanosecond, Second};
use arrow::datatypes::{
- DataType, Date32Type, Date64Type, Field, FieldRef, IntervalUnit as
ArrowIntervalUnit,
- TimeUnit,
+ ArrowTimestampType, DataType, Date32Type, Date64Type, Field, FieldRef,
+ IntervalUnit as ArrowIntervalUnit, TimeUnit, TimestampMicrosecondType,
+ TimestampMillisecondType, TimestampNanosecondType, TimestampSecondType,
};
-use chrono::{Datelike, NaiveDate, TimeZone, Utc};
+use chrono::{Datelike, NaiveDate};
use datafusion_common::types::{NativeType, logical_date};
use datafusion_common::{
@@ -337,37 +338,32 @@ fn date_to_scalar(date: NaiveDate, target_type:
&DataType) -> Option<ScalarValue
Timestamp(unit, tz_opt) => {
let naive_midnight = date.and_hms_opt(0, 0, 0)?;
-
- let utc_dt = if let Some(tz_str) = tz_opt {
- let tz: Tz = tz_str.parse().ok()?;
-
- let local = tz.from_local_datetime(&naive_midnight);
-
- let local_dt = match local {
- chrono::offset::LocalResult::Single(dt) => dt,
- chrono::offset::LocalResult::Ambiguous(dt1, _dt2) => dt1,
- chrono::offset::LocalResult::None => local.earliest()?,
- };
-
- local_dt.with_timezone(&Utc)
- } else {
- Utc.from_utc_datetime(&naive_midnight)
- };
+ let tz: Option<Tz> = tz_opt.clone().and_then(|s| s.parse().ok());
match unit {
- Second => {
- ScalarValue::TimestampSecond(Some(utc_dt.timestamp()),
tz_opt.clone())
- }
+ Second => ScalarValue::TimestampSecond(
+ TimestampSecondType::from_naive_datetime(naive_midnight,
tz.as_ref()),
+ tz_opt.clone(),
+ ),
Millisecond => ScalarValue::TimestampMillisecond(
- Some(utc_dt.timestamp_millis()),
+ TimestampMillisecondType::from_naive_datetime(
+ naive_midnight,
+ tz.as_ref(),
+ ),
tz_opt.clone(),
),
Microsecond => ScalarValue::TimestampMicrosecond(
- Some(utc_dt.timestamp_micros()),
+ TimestampMicrosecondType::from_naive_datetime(
+ naive_midnight,
+ tz.as_ref(),
+ ),
tz_opt.clone(),
),
Nanosecond => ScalarValue::TimestampNanosecond(
- Some(utc_dt.timestamp_nanos_opt()?),
+ TimestampNanosecondType::from_naive_datetime(
+ naive_midnight,
+ tz.as_ref(),
+ ),
tz_opt.clone(),
),
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]