Skip to content

Apache DataFusion SQL Query Engine

Clean up date_part preimage implementation (#20350)

Konstantin Tarasov44dfa7b8a5b9
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(),
),
}