Apache DataFusion SQL Query Engine
Move date_part, date_trunc, date_bin functions to datafusion-functions (#9435)
Bruce Ritchieacddecbb5671
diff --git a/datafusion/expr/src/built_in_function.rs b/datafusion/expr/src/built_in_function.rsindex f6789ea596..991963a1bc 100644--- a/datafusion/expr/src/built_in_function.rs+++ b/datafusion/expr/src/built_in_function.rs@@ -28,7 +28,7 @@ use crate::type_coercion::binary::get_wider_type;use crate::type_coercion::functions::data_types;use crate::{FuncMonotonicity, Signature, TypeSignature, Volatility};-use arrow::datatypes::{DataType, Field, Fields, IntervalUnit, TimeUnit};+use arrow::datatypes::{DataType, Field, Fields, TimeUnit};use datafusion_common::{plan_err, DataFusionError, Result};use strum::IntoEnumIterator;@@ -180,12 +180,6 @@ pub enum BuiltinScalarFunction {Concat,/// concat_wsConcatWithSeparator,- /// date_part- DatePart,- /// date_trunc- DateTrunc,- /// date_bin- DateBin,/// ends_withEndsWith,/// initcap@@ -382,9 +376,6 @@ impl BuiltinScalarFunction {BuiltinScalarFunction::Chr => Volatility::Immutable,BuiltinScalarFunction::Concat => Volatility::Immutable,BuiltinScalarFunction::ConcatWithSeparator => Volatility::Immutable,- BuiltinScalarFunction::DatePart => Volatility::Immutable,- BuiltinScalarFunction::DateTrunc => Volatility::Immutable,- BuiltinScalarFunction::DateBin => Volatility::Immutable,BuiltinScalarFunction::EndsWith => Volatility::Immutable,BuiltinScalarFunction::InitCap => Volatility::Immutable,BuiltinScalarFunction::Left => Volatility::Immutable,@@ -592,27 +583,6 @@ impl BuiltinScalarFunction {}BuiltinScalarFunction::Concat => Ok(Utf8),BuiltinScalarFunction::ConcatWithSeparator => Ok(Utf8),- BuiltinScalarFunction::DatePart => Ok(Float64),- BuiltinScalarFunction::DateBin | BuiltinScalarFunction::DateTrunc => {- match &input_expr_types[1] {- Timestamp(Nanosecond, None) | Utf8 | Null => {- Ok(Timestamp(Nanosecond, None))- }- Timestamp(Nanosecond, tz_opt) => {- Ok(Timestamp(Nanosecond, tz_opt.clone()))- }- Timestamp(Microsecond, tz_opt) => {- Ok(Timestamp(Microsecond, tz_opt.clone()))- }- Timestamp(Millisecond, tz_opt) => {- Ok(Timestamp(Millisecond, tz_opt.clone()))- }- Timestamp(Second, tz_opt) => Ok(Timestamp(Second, tz_opt.clone())),- _ => plan_err!(- "The {self} function can only accept timestamp as the second arg."- ),- }- }BuiltinScalarFunction::InitCap => {utf8_to_str_type(&input_expr_types[0], "initcap")}@@ -784,7 +754,6 @@ impl BuiltinScalarFunction {/// Return the argument [`Signature`] supported by this functionpub fn signature(&self) -> Signature {use DataType::*;- use IntervalUnit::*;use TimeUnit::*;use TypeSignature::*;// note: the physical expression must accept the type returned by this function or the execution panics.@@ -947,108 +916,6 @@ impl BuiltinScalarFunction {],self.volatility(),),- BuiltinScalarFunction::DateTrunc => Signature::one_of(- vec![- Exact(vec![Utf8, Timestamp(Nanosecond, None)]),- Exact(vec![- Utf8,- Timestamp(Nanosecond, Some(TIMEZONE_WILDCARD.into())),- ]),- Exact(vec![Utf8, Timestamp(Microsecond, None)]),- Exact(vec![- Utf8,- Timestamp(Microsecond, Some(TIMEZONE_WILDCARD.into())),- ]),- Exact(vec![Utf8, Timestamp(Millisecond, None)]),- Exact(vec![- Utf8,- Timestamp(Millisecond, Some(TIMEZONE_WILDCARD.into())),- ]),- Exact(vec![Utf8, Timestamp(Second, None)]),- Exact(vec![- Utf8,- Timestamp(Second, Some(TIMEZONE_WILDCARD.into())),- ]),- ],- self.volatility(),- ),- BuiltinScalarFunction::DateBin => {- let base_sig = |array_type: TimeUnit| {- vec![- Exact(vec![- Interval(MonthDayNano),- Timestamp(array_type.clone(), None),- Timestamp(Nanosecond, None),- ]),- Exact(vec![- Interval(MonthDayNano),- Timestamp(array_type.clone(), Some(TIMEZONE_WILDCARD.into())),- Timestamp(Nanosecond, Some(TIMEZONE_WILDCARD.into())),- ]),- Exact(vec![- Interval(DayTime),- Timestamp(array_type.clone(), None),- Timestamp(Nanosecond, None),- ]),- Exact(vec![- Interval(DayTime),- Timestamp(array_type.clone(), Some(TIMEZONE_WILDCARD.into())),- Timestamp(Nanosecond, Some(TIMEZONE_WILDCARD.into())),- ]),- Exact(vec![- Interval(MonthDayNano),- Timestamp(array_type.clone(), None),- ]),- Exact(vec![- Interval(MonthDayNano),- Timestamp(array_type.clone(), Some(TIMEZONE_WILDCARD.into())),- ]),- Exact(vec![- Interval(DayTime),- Timestamp(array_type.clone(), None),- ]),- Exact(vec![- Interval(DayTime),- Timestamp(array_type, Some(TIMEZONE_WILDCARD.into())),- ]),- ]- };-- let full_sig = [Nanosecond, Microsecond, Millisecond, Second]- .into_iter()- .map(base_sig)- .collect::<Vec<_>>()- .concat();-- Signature::one_of(full_sig, self.volatility())- }- BuiltinScalarFunction::DatePart => Signature::one_of(- vec![- Exact(vec![Utf8, Timestamp(Nanosecond, None)]),- Exact(vec![- Utf8,- Timestamp(Nanosecond, Some(TIMEZONE_WILDCARD.into())),- ]),- Exact(vec![Utf8, Timestamp(Millisecond, None)]),- Exact(vec![- Utf8,- Timestamp(Millisecond, Some(TIMEZONE_WILDCARD.into())),- ]),- Exact(vec![Utf8, Timestamp(Microsecond, None)]),- Exact(vec![- Utf8,- Timestamp(Microsecond, Some(TIMEZONE_WILDCARD.into())),- ]),- Exact(vec![Utf8, Timestamp(Second, None)]),- Exact(vec![- Utf8,- Timestamp(Second, Some(TIMEZONE_WILDCARD.into())),- ]),- Exact(vec![Utf8, Date64]),- Exact(vec![Utf8, Date32]),- ],- self.volatility(),- ),BuiltinScalarFunction::SplitPart => Signature::one_of(vec![Exact(vec![Utf8, Utf8, Int64]),@@ -1240,11 +1107,6 @@ impl BuiltinScalarFunction {| BuiltinScalarFunction::Pi) {Some(vec![Some(true)])- } else if matches!(- &self,- BuiltinScalarFunction::DateTrunc | BuiltinScalarFunction::DateBin- ) {- Some(vec![None, Some(true)])} else if *self == BuiltinScalarFunction::Log {Some(vec![Some(true), Some(false)])} else {@@ -1337,9 +1199,6 @@ impl BuiltinScalarFunction {BuiltinScalarFunction::CurrentDate => &["current_date", "today"],BuiltinScalarFunction::CurrentTime => &["current_time"],BuiltinScalarFunction::MakeDate => &["make_date"],- BuiltinScalarFunction::DateBin => &["date_bin"],- BuiltinScalarFunction::DateTrunc => &["date_trunc", "datetrunc"],- BuiltinScalarFunction::DatePart => &["date_part", "datepart"],BuiltinScalarFunction::ToChar => &["to_char", "date_format"],BuiltinScalarFunction::FromUnixtime => &["from_unixtime"],diff --git a/datafusion/expr/src/expr.rs b/datafusion/expr/src/expr.rsindex e83d2f1a65..0da05d96f6 100644--- a/datafusion/expr/src/expr.rs+++ b/datafusion/expr/src/expr.rs@@ -419,6 +419,11 @@ impl ScalarFunction {args,}}++ /// Create a new ScalarFunction expression with a user-defined function (UDF)+ pub fn new_func_def(func_def: ScalarFunctionDefinition, args: Vec<Expr>) -> Self {+ Self { func_def, args }+ }}/// Access a sub field of a nested type, such as `Field` or `List`diff --git a/datafusion/expr/src/expr_fn.rs b/datafusion/expr/src/expr_fn.rsindex 4090f9da6e..1e934267e9 100644--- a/datafusion/expr/src/expr_fn.rs+++ b/datafusion/expr/src/expr_fn.rs@@ -819,9 +819,6 @@ nary_scalar_expr!();// date functions-scalar_expr!(DatePart, date_part, part date, "extracts a subfield from the date");-scalar_expr!(DateTrunc, date_trunc, part date, "truncates the date to a specified level of precision");-scalar_expr!(DateBin, date_bin, stride source origin, "coerces an arbitrary timestamp to the start of the nearest specified interval");scalar_expr!(ToChar,to_char,@@ -1309,9 +1306,6 @@ mod test {test_scalar_expr!(Trim, trim, string);test_scalar_expr!(Upper, upper, string);- test_scalar_expr!(DatePart, date_part, part, date);- test_scalar_expr!(DateTrunc, date_trunc, part, date);- test_scalar_expr!(DateBin, date_bin, stride, source, origin);test_scalar_expr!(FromUnixtime, from_unixtime, unixtime);test_scalar_expr!(ArrayAppend, array_append, array, element);diff --git a/datafusion/functions/src/datetime/date_bin.rs b/datafusion/functions/src/datetime/date_bin.rsnew file mode 100644index 0000000000..8472c346f1--- /dev/null+++ b/datafusion/functions/src/datetime/date_bin.rs@@ -0,0 +1,766 @@+// Licensed to the Apache Software Foundation (ASF) under one+// or more contributor license agreements. See the NOTICE file+// distributed with this work for additional information+// regarding copyright ownership. The ASF licenses this file+// to you under the Apache License, Version 2.0 (the+// "License"); you may not use this file except in compliance+// with the License. You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing,+// software distributed under the License is distributed on an+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY+// KIND, either express or implied. See the License for the+// specific language governing permissions and limitations+// under the License.++use std::any::Any;+use std::sync::Arc;++use arrow::datatypes::DataType::{Null, Timestamp, Utf8};+use arrow::datatypes::IntervalUnit::{DayTime, MonthDayNano};+use arrow::datatypes::TimeUnit::{Microsecond, Millisecond, Nanosecond, Second};+use arrow::datatypes::{DataType, TimeUnit};+use arrow_array::temporal_conversions::NANOSECONDS;+use arrow_array::types::{+ ArrowTimestampType, IntervalDayTimeType, IntervalMonthDayNanoType,+ TimestampMicrosecondType, TimestampMillisecondType, TimestampNanosecondType,+ TimestampSecondType,+};+use arrow_array::{ArrayRef, PrimitiveArray};+use chrono::{DateTime, Datelike, Duration, Months, TimeDelta, Utc};++use datafusion_common::cast::as_primitive_array;+use datafusion_common::{exec_err, not_impl_err, plan_err, Result, ScalarValue};+use datafusion_expr::TypeSignature::Exact;+use datafusion_expr::{+ ColumnarValue, FuncMonotonicity, ScalarUDFImpl, Signature, Volatility,+ TIMEZONE_WILDCARD,+};++#[derive(Debug)]+pub(super) struct DateBinFunc {+ signature: Signature,+}++impl DateBinFunc {+ pub fn new() -> Self {+ let base_sig = |array_type: TimeUnit| {+ vec![+ Exact(vec![+ DataType::Interval(MonthDayNano),+ Timestamp(array_type.clone(), None),+ Timestamp(Nanosecond, None),+ ]),+ Exact(vec![+ DataType::Interval(MonthDayNano),+ Timestamp(array_type.clone(), Some(TIMEZONE_WILDCARD.into())),+ Timestamp(Nanosecond, Some(TIMEZONE_WILDCARD.into())),+ ]),+ Exact(vec![+ DataType::Interval(DayTime),+ Timestamp(array_type.clone(), None),+ Timestamp(Nanosecond, None),+ ]),+ Exact(vec![+ DataType::Interval(DayTime),+ Timestamp(array_type.clone(), Some(TIMEZONE_WILDCARD.into())),+ Timestamp(Nanosecond, Some(TIMEZONE_WILDCARD.into())),+ ]),+ Exact(vec![+ DataType::Interval(MonthDayNano),+ Timestamp(array_type.clone(), None),+ ]),+ Exact(vec![+ DataType::Interval(MonthDayNano),+ Timestamp(array_type.clone(), Some(TIMEZONE_WILDCARD.into())),+ ]),+ Exact(vec![+ DataType::Interval(DayTime),+ Timestamp(array_type.clone(), None),+ ]),+ Exact(vec![+ DataType::Interval(DayTime),+ Timestamp(array_type, Some(TIMEZONE_WILDCARD.into())),+ ]),+ ]+ };++ let full_sig = [Nanosecond, Microsecond, Millisecond, Second]+ .into_iter()+ .map(base_sig)+ .collect::<Vec<_>>()+ .concat();++ Self {+ signature: Signature::one_of(full_sig, Volatility::Immutable),+ }+ }+}++impl ScalarUDFImpl for DateBinFunc {+ fn as_any(&self) -> &dyn Any {+ self+ }++ fn name(&self) -> &str {+ "date_bin"+ }++ fn signature(&self) -> &Signature {+ &self.signature+ }++ fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {+ match &arg_types[1] {+ Timestamp(Nanosecond, None) | Utf8 | Null => Ok(Timestamp(Nanosecond, None)),+ Timestamp(Nanosecond, tz_opt) => Ok(Timestamp(Nanosecond, tz_opt.clone())),+ Timestamp(Microsecond, tz_opt) => Ok(Timestamp(Microsecond, tz_opt.clone())),+ Timestamp(Millisecond, tz_opt) => Ok(Timestamp(Millisecond, tz_opt.clone())),+ Timestamp(Second, tz_opt) => Ok(Timestamp(Second, tz_opt.clone())),+ _ => plan_err!(+ "The date_bin function can only accept timestamp as the second arg."+ ),+ }+ }++ fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {+ if args.len() == 2 {+ // Default to unix EPOCH+ let origin = ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(+ Some(0),+ Some("+00:00".into()),+ ));+ date_bin_impl(&args[0], &args[1], &origin)+ } else if args.len() == 3 {+ date_bin_impl(&args[0], &args[1], &args[2])+ } else {+ exec_err!("DATE_BIN expected two or three arguments")+ }+ }++ fn monotonicity(&self) -> Result<Option<FuncMonotonicity>> {+ Ok(Some(vec![None, Some(true)]))+ }+}++enum Interval {+ Nanoseconds(i64),+ Months(i64),+}++impl Interval {+ /// Returns (`stride_nanos`, `fn`) where+ ///+ /// 1. `stride_nanos` is a width, in nanoseconds+ /// 2. `fn` is a function that takes (stride_nanos, source, origin)+ ///+ /// `source` is the timestamp being binned+ ///+ /// `origin` is the time, in nanoseconds, where windows are measured from+ fn bin_fn(&self) -> (i64, fn(i64, i64, i64) -> i64) {+ match self {+ Interval::Nanoseconds(nanos) => (*nanos, date_bin_nanos_interval),+ Interval::Months(months) => (*months, date_bin_months_interval),+ }+ }+}++// return time in nanoseconds that the source timestamp falls into based on the stride and origin+fn date_bin_nanos_interval(stride_nanos: i64, source: i64, origin: i64) -> i64 {+ let time_diff = source - origin;++ // distance from origin to bin+ let time_delta = compute_distance(time_diff, stride_nanos);++ origin + time_delta+}++// distance from origin to bin+fn compute_distance(time_diff: i64, stride: i64) -> i64 {+ let time_delta = time_diff - (time_diff % stride);++ if time_diff < 0 && stride > 1 {+ // The origin is later than the source timestamp, round down to the previous bin+ time_delta - stride+ } else {+ time_delta+ }+}++// return time in nanoseconds that the source timestamp falls into based on the stride and origin+fn date_bin_months_interval(stride_months: i64, source: i64, origin: i64) -> i64 {+ // convert source and origin to DateTime<Utc>+ let source_date = to_utc_date_time(source);+ let origin_date = to_utc_date_time(origin);++ // calculate the number of months between the source and origin+ let month_diff = (source_date.year() - origin_date.year()) * 12+ + source_date.month() as i32+ - origin_date.month() as i32;++ // distance from origin to bin+ let month_delta = compute_distance(month_diff as i64, stride_months);++ let mut bin_time = if month_delta < 0 {+ origin_date - Months::new(month_delta.unsigned_abs() as u32)+ } else {+ origin_date + Months::new(month_delta as u32)+ };++ // If origin is not midnight of first date of the month, the bin_time may be larger than the source+ // In this case, we need to move back to previous bin+ if bin_time > source_date {+ let month_delta = month_delta - stride_months;+ bin_time = if month_delta < 0 {+ origin_date - Months::new(month_delta.unsigned_abs() as u32)+ } else {+ origin_date + Months::new(month_delta as u32)+ };+ }++ bin_time.timestamp_nanos_opt().unwrap()+}++fn to_utc_date_time(nanos: i64) -> DateTime<Utc> {+ let secs = nanos / 1_000_000_000;+ let nsec = (nanos % 1_000_000_000) as u32;+ DateTime::from_timestamp(secs, nsec).unwrap()+}++// Supported intervals:+// 1. IntervalDayTime: this means that the stride is in days, hours, minutes, seconds and milliseconds+// We will assume month interval won't be converted into this type+// TODO (my next PR): without `INTERVAL` keyword, the stride was converted into ScalarValue::IntervalDayTime somwhere+// for month interval. I need to find that and make it ScalarValue::IntervalMonthDayNano instead+// 2. IntervalMonthDayNano+fn date_bin_impl(+ stride: &ColumnarValue,+ array: &ColumnarValue,+ origin: &ColumnarValue,+) -> Result<ColumnarValue> {+ let stride = match stride {+ ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(v))) => {+ let (days, ms) = IntervalDayTimeType::to_parts(*v);+ let nanos = (TimeDelta::try_days(days as i64).unwrap()+ + TimeDelta::try_milliseconds(ms as i64).unwrap())+ .num_nanoseconds();++ match nanos {+ Some(v) => Interval::Nanoseconds(v),+ _ => return exec_err!("DATE_BIN stride argument is too large"),+ }+ }+ ColumnarValue::Scalar(ScalarValue::IntervalMonthDayNano(Some(v))) => {+ let (months, days, nanos) = IntervalMonthDayNanoType::to_parts(*v);++ // If interval is months, its origin must be midnight of first date of the month+ if months != 0 {+ // Return error if days or nanos is not zero+ if days != 0 || nanos != 0 {+ return not_impl_err!(+ "DATE_BIN stride does not support combination of month, day and nanosecond intervals"+ );+ } else {+ Interval::Months(months as i64)+ }+ } else {+ let nanos = (TimeDelta::try_days(days as i64).unwrap()+ + Duration::nanoseconds(nanos))+ .num_nanoseconds();+ match nanos {+ Some(v) => Interval::Nanoseconds(v),+ _ => return exec_err!("DATE_BIN stride argument is too large"),+ }+ }+ }+ ColumnarValue::Scalar(v) => {+ return exec_err!(+ "DATE_BIN expects stride argument to be an INTERVAL but got {}",+ v.data_type()+ );+ }+ ColumnarValue::Array(_) => {+ return not_impl_err!(+ "DATE_BIN only supports literal values for the stride argument, not arrays"+ );+ }+ };++ let origin = match origin {+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(v), _)) => *v,+ ColumnarValue::Scalar(v) => {+ return exec_err!(+ "DATE_BIN expects origin argument to be a TIMESTAMP with nanosecond precision but got {}",+ v.data_type()+ );+ }+ ColumnarValue::Array(_) => {+ return not_impl_err!(+ "DATE_BIN only supports literal values for the origin argument, not arrays"+ );+ }+ };++ let (stride, stride_fn) = stride.bin_fn();++ // Return error if stride is 0+ if stride == 0 {+ return exec_err!("DATE_BIN stride must be non-zero");+ }++ fn stride_map_fn<T: ArrowTimestampType>(+ origin: i64,+ stride: i64,+ stride_fn: fn(i64, i64, i64) -> i64,+ ) -> impl Fn(Option<i64>) -> Option<i64> {+ let scale = match T::UNIT {+ Nanosecond => 1,+ Microsecond => NANOSECONDS / 1_000_000,+ Millisecond => NANOSECONDS / 1_000,+ Second => NANOSECONDS,+ };+ move |x: Option<i64>| x.map(|x| stride_fn(stride, x * scale, origin) / scale)+ }++ Ok(match array {+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(v, tz_opt)) => {+ let apply_stride_fn =+ stride_map_fn::<TimestampNanosecondType>(origin, stride, stride_fn);+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(+ apply_stride_fn(*v),+ tz_opt.clone(),+ ))+ }+ ColumnarValue::Scalar(ScalarValue::TimestampMicrosecond(v, tz_opt)) => {+ let apply_stride_fn =+ stride_map_fn::<TimestampMicrosecondType>(origin, stride, stride_fn);+ ColumnarValue::Scalar(ScalarValue::TimestampMicrosecond(+ apply_stride_fn(*v),+ tz_opt.clone(),+ ))+ }+ ColumnarValue::Scalar(ScalarValue::TimestampMillisecond(v, tz_opt)) => {+ let apply_stride_fn =+ stride_map_fn::<TimestampMillisecondType>(origin, stride, stride_fn);+ ColumnarValue::Scalar(ScalarValue::TimestampMillisecond(+ apply_stride_fn(*v),+ tz_opt.clone(),+ ))+ }+ ColumnarValue::Scalar(ScalarValue::TimestampSecond(v, tz_opt)) => {+ let apply_stride_fn =+ stride_map_fn::<TimestampSecondType>(origin, stride, stride_fn);+ ColumnarValue::Scalar(ScalarValue::TimestampSecond(+ apply_stride_fn(*v),+ tz_opt.clone(),+ ))+ }++ ColumnarValue::Array(array) => {+ fn transform_array_with_stride<T>(+ origin: i64,+ stride: i64,+ stride_fn: fn(i64, i64, i64) -> i64,+ array: &ArrayRef,+ tz_opt: &Option<Arc<str>>,+ ) -> Result<ColumnarValue>+ where+ T: ArrowTimestampType,+ {+ let array = as_primitive_array::<T>(array)?;+ let apply_stride_fn = stride_map_fn::<T>(origin, stride, stride_fn);+ let array = array+ .iter()+ .map(apply_stride_fn)+ .collect::<PrimitiveArray<T>>()+ .with_timezone_opt(tz_opt.clone());++ Ok(ColumnarValue::Array(Arc::new(array)))+ }+ match array.data_type() {+ Timestamp(Nanosecond, tz_opt) => {+ transform_array_with_stride::<TimestampNanosecondType>(+ origin, stride, stride_fn, array, tz_opt,+ )?+ }+ Timestamp(Microsecond, tz_opt) => {+ transform_array_with_stride::<TimestampMicrosecondType>(+ origin, stride, stride_fn, array, tz_opt,+ )?+ }+ Timestamp(Millisecond, tz_opt) => {+ transform_array_with_stride::<TimestampMillisecondType>(+ origin, stride, stride_fn, array, tz_opt,+ )?+ }+ Timestamp(Second, tz_opt) => {+ transform_array_with_stride::<TimestampSecondType>(+ origin, stride, stride_fn, array, tz_opt,+ )?+ }+ _ => {+ return exec_err!(+ "DATE_BIN expects source argument to be a TIMESTAMP but got {}",+ array.data_type()+ );+ }+ }+ }+ _ => {+ return exec_err!(+ "DATE_BIN expects source argument to be a TIMESTAMP scalar or array"+ );+ }+ })+}++#[cfg(test)]+mod tests {+ use std::sync::Arc;++ use arrow::compute::kernels::cast_utils::string_to_timestamp_nanos;+ use arrow::datatypes::{DataType, TimeUnit};+ use arrow_array::types::TimestampNanosecondType;+ use arrow_array::{IntervalDayTimeArray, TimestampNanosecondArray};+ use chrono::TimeDelta;++ use datafusion_common::ScalarValue;+ use datafusion_expr::{ColumnarValue, ScalarUDFImpl};++ use crate::datetime::date_bin::{date_bin_nanos_interval, DateBinFunc};++ #[test]+ fn test_date_bin() {+ let res = DateBinFunc::new().invoke(&[+ ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(1))),+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),+ ]);+ assert!(res.is_ok());++ let timestamps = Arc::new((1..6).map(Some).collect::<TimestampNanosecondArray>());+ let res = DateBinFunc::new().invoke(&[+ ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(1))),+ ColumnarValue::Array(timestamps),+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),+ ]);+ assert!(res.is_ok());++ let res = DateBinFunc::new().invoke(&[+ ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(1))),+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),+ ]);+ assert!(res.is_ok());++ // stride supports month-day-nano+ let res = DateBinFunc::new().invoke(&[+ ColumnarValue::Scalar(ScalarValue::IntervalMonthDayNano(Some(1))),+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),+ ]);+ assert!(res.is_ok());++ //+ // Fallible test cases+ //++ // invalid number of arguments+ let res = DateBinFunc::new()+ .invoke(&[ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(1)))]);+ assert_eq!(+ res.err().unwrap().strip_backtrace(),+ "Execution error: DATE_BIN expected two or three arguments"+ );++ // stride: invalid type+ let res = DateBinFunc::new().invoke(&[+ ColumnarValue::Scalar(ScalarValue::IntervalYearMonth(Some(1))),+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),+ ]);+ assert_eq!(+ res.err().unwrap().strip_backtrace(),+ "Execution error: DATE_BIN expects stride argument to be an INTERVAL but got Interval(YearMonth)"+ );++ // stride: invalid value+ let res = DateBinFunc::new().invoke(&[+ ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(0))),+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),+ ]);+ assert_eq!(+ res.err().unwrap().strip_backtrace(),+ "Execution error: DATE_BIN stride must be non-zero"+ );++ // stride: overflow of day-time interval+ let res = DateBinFunc::new().invoke(&[+ ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(i64::MAX))),+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),+ ]);+ assert_eq!(+ res.err().unwrap().strip_backtrace(),+ "Execution error: DATE_BIN stride argument is too large"+ );++ // stride: overflow of month-day-nano interval+ let res = DateBinFunc::new().invoke(&[+ ColumnarValue::Scalar(ScalarValue::new_interval_mdn(0, i32::MAX, 1)),+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),+ ]);+ assert_eq!(+ res.err().unwrap().strip_backtrace(),+ "Execution error: DATE_BIN stride argument is too large"+ );++ // stride: month intervals+ let res = DateBinFunc::new().invoke(&[+ ColumnarValue::Scalar(ScalarValue::new_interval_mdn(1, 1, 1)),+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),+ ]);+ assert_eq!(+ res.err().unwrap().strip_backtrace(),+ "This feature is not implemented: DATE_BIN stride does not support combination of month, day and nanosecond intervals"+ );++ // origin: invalid type+ let res = DateBinFunc::new().invoke(&[+ ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(1))),+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),+ ColumnarValue::Scalar(ScalarValue::TimestampMicrosecond(Some(1), None)),+ ]);+ assert_eq!(+ res.err().unwrap().strip_backtrace(),+ "Execution error: DATE_BIN expects origin argument to be a TIMESTAMP with nanosecond precision but got Timestamp(Microsecond, None)"+ );++ let res = DateBinFunc::new().invoke(&[+ ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(1))),+ ColumnarValue::Scalar(ScalarValue::TimestampMicrosecond(Some(1), None)),+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),+ ]);+ assert!(res.is_ok());++ // unsupported array type for stride+ let intervals = Arc::new((1..6).map(Some).collect::<IntervalDayTimeArray>());+ let res = DateBinFunc::new().invoke(&[+ ColumnarValue::Array(intervals),+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),+ ]);+ assert_eq!(+ res.err().unwrap().strip_backtrace(),+ "This feature is not implemented: DATE_BIN only supports literal values for the stride argument, not arrays"+ );++ // unsupported array type for origin+ let timestamps = Arc::new((1..6).map(Some).collect::<TimestampNanosecondArray>());+ let res = DateBinFunc::new().invoke(&[+ ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(1))),+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),+ ColumnarValue::Array(timestamps),+ ]);+ assert_eq!(+ res.err().unwrap().strip_backtrace(),+ "This feature is not implemented: DATE_BIN only supports literal values for the origin argument, not arrays"+ );+ }++ #[test]+ fn test_date_bin_timezones() {+ let cases = vec![+ (+ vec![+ "2020-09-08T00:00:00Z",+ "2020-09-08T01:00:00Z",+ "2020-09-08T02:00:00Z",+ "2020-09-08T03:00:00Z",+ "2020-09-08T04:00:00Z",+ ],+ Some("+00".into()),+ "1970-01-01T00:00:00Z",+ vec![+ "2020-09-08T00:00:00Z",+ "2020-09-08T00:00:00Z",+ "2020-09-08T00:00:00Z",+ "2020-09-08T00:00:00Z",+ "2020-09-08T00:00:00Z",+ ],+ ),+ (+ vec![+ "2020-09-08T00:00:00Z",+ "2020-09-08T01:00:00Z",+ "2020-09-08T02:00:00Z",+ "2020-09-08T03:00:00Z",+ "2020-09-08T04:00:00Z",+ ],+ None,+ "1970-01-01T00:00:00Z",+ vec![+ "2020-09-08T00:00:00Z",+ "2020-09-08T00:00:00Z",+ "2020-09-08T00:00:00Z",+ "2020-09-08T00:00:00Z",+ "2020-09-08T00:00:00Z",+ ],+ ),+ (+ vec![+ "2020-09-08T00:00:00Z",+ "2020-09-08T01:00:00Z",+ "2020-09-08T02:00:00Z",+ "2020-09-08T03:00:00Z",+ "2020-09-08T04:00:00Z",+ ],+ Some("-02".into()),+ "1970-01-01T00:00:00Z",+ vec![+ "2020-09-08T00:00:00Z",+ "2020-09-08T00:00:00Z",+ "2020-09-08T00:00:00Z",+ "2020-09-08T00:00:00Z",+ "2020-09-08T00:00:00Z",+ ],+ ),+ (+ vec![+ "2020-09-08T00:00:00+05",+ "2020-09-08T01:00:00+05",+ "2020-09-08T02:00:00+05",+ "2020-09-08T03:00:00+05",+ "2020-09-08T04:00:00+05",+ ],+ Some("+05".into()),+ "1970-01-01T00:00:00+05",+ vec![+ "2020-09-08T00:00:00+05",+ "2020-09-08T00:00:00+05",+ "2020-09-08T00:00:00+05",+ "2020-09-08T00:00:00+05",+ "2020-09-08T00:00:00+05",+ ],+ ),+ (+ vec![+ "2020-09-08T00:00:00+08",+ "2020-09-08T01:00:00+08",+ "2020-09-08T02:00:00+08",+ "2020-09-08T03:00:00+08",+ "2020-09-08T04:00:00+08",+ ],+ Some("+08".into()),+ "1970-01-01T00:00:00+08",+ vec![+ "2020-09-08T00:00:00+08",+ "2020-09-08T00:00:00+08",+ "2020-09-08T00:00:00+08",+ "2020-09-08T00:00:00+08",+ "2020-09-08T00:00:00+08",+ ],+ ),+ ];++ cases+ .iter()+ .for_each(|(original, tz_opt, origin, expected)| {+ let input = original+ .iter()+ .map(|s| Some(string_to_timestamp_nanos(s).unwrap()))+ .collect::<TimestampNanosecondArray>()+ .with_timezone_opt(tz_opt.clone());+ let right = expected+ .iter()+ .map(|s| Some(string_to_timestamp_nanos(s).unwrap()))+ .collect::<TimestampNanosecondArray>()+ .with_timezone_opt(tz_opt.clone());+ let result = DateBinFunc::new()+ .invoke(&[+ ColumnarValue::Scalar(ScalarValue::new_interval_dt(1, 0)),+ ColumnarValue::Array(Arc::new(input)),+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(+ Some(string_to_timestamp_nanos(origin).unwrap()),+ tz_opt.clone(),+ )),+ ])+ .unwrap();+ if let ColumnarValue::Array(result) = result {+ assert_eq!(+ result.data_type(),+ &DataType::Timestamp(TimeUnit::Nanosecond, tz_opt.clone())+ );+ let left = arrow_array::cast::as_primitive_array::<+ TimestampNanosecondType,+ >(&result);+ assert_eq!(left, &right);+ } else {+ panic!("unexpected column type");+ }+ });+ }++ #[test]+ fn test_date_bin_single() {+ let cases = vec![+ (+ (+ TimeDelta::try_minutes(15),+ "2004-04-09T02:03:04.123456789Z",+ "2001-01-01T00:00:00",+ ),+ "2004-04-09T02:00:00Z",+ ),+ (+ (+ TimeDelta::try_minutes(15),+ "2004-04-09T02:03:04.123456789Z",+ "2001-01-01T00:02:30",+ ),+ "2004-04-09T02:02:30Z",+ ),+ (+ (+ TimeDelta::try_minutes(15),+ "2004-04-09T02:03:04.123456789Z",+ "2005-01-01T00:02:30",+ ),+ "2004-04-09T02:02:30Z",+ ),+ (+ (+ TimeDelta::try_hours(1),+ "2004-04-09T02:03:04.123456789Z",+ "2001-01-01T00:00:00",+ ),+ "2004-04-09T02:00:00Z",+ ),+ (+ (+ TimeDelta::try_seconds(10),+ "2004-04-09T02:03:11.123456789Z",+ "2001-01-01T00:00:00",+ ),+ "2004-04-09T02:03:10Z",+ ),+ ];++ cases+ .iter()+ .for_each(|((stride, source, origin), expected)| {+ let stride = stride.unwrap();+ let stride1 = stride.num_nanoseconds().unwrap();+ let source1 = string_to_timestamp_nanos(source).unwrap();+ let origin1 = string_to_timestamp_nanos(origin).unwrap();++ let expected1 = string_to_timestamp_nanos(expected).unwrap();+ let result = date_bin_nanos_interval(stride1, source1, origin1);+ assert_eq!(result, expected1, "{source} = {expected}");+ })+ }+}diff --git a/datafusion/functions/src/datetime/date_part.rs b/datafusion/functions/src/datetime/date_part.rsnew file mode 100644index 0000000000..200dcff6d4--- /dev/null+++ b/datafusion/functions/src/datetime/date_part.rs@@ -0,0 +1,269 @@+// Licensed to the Apache Software Foundation (ASF) under one+// or more contributor license agreements. See the NOTICE file+// distributed with this work for additional information+// regarding copyright ownership. The ASF licenses this file+// to you under the Apache License, Version 2.0 (the+// "License"); you may not use this file except in compliance+// with the License. You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing,+// software distributed under the License is distributed on an+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY+// KIND, either express or implied. See the License for the+// specific language governing permissions and limitations+// under the License.++use std::any::Any;+use std::sync::Arc;++use arrow::compute::cast;+use arrow::compute::kernels::temporal;+use arrow::datatypes::DataType::{Date32, Date64, Float64, Timestamp, Utf8};+use arrow::datatypes::TimeUnit::{Microsecond, Millisecond, Nanosecond, Second};+use arrow::datatypes::{DataType, TimeUnit};+use arrow_array::types::ArrowTemporalType;+use arrow_array::{Array, ArrayRef, ArrowNumericType, Float64Array, PrimitiveArray};++use datafusion_common::cast::{+ as_date32_array, as_date64_array, as_timestamp_microsecond_array,+ as_timestamp_millisecond_array, as_timestamp_nanosecond_array,+ as_timestamp_second_array,+};+use datafusion_common::{exec_err, Result, ScalarValue};+use datafusion_expr::TypeSignature::Exact;+use datafusion_expr::{+ ColumnarValue, ScalarUDFImpl, Signature, Volatility, TIMEZONE_WILDCARD,+};++#[derive(Debug)]+pub(super) struct DatePartFunc {+ signature: Signature,+ aliases: Vec<String>,+}++impl DatePartFunc {+ pub fn new() -> Self {+ Self {+ signature: Signature::one_of(+ vec![+ Exact(vec![Utf8, Timestamp(Nanosecond, None)]),+ Exact(vec![+ Utf8,+ Timestamp(Nanosecond, Some(TIMEZONE_WILDCARD.into())),+ ]),+ Exact(vec![Utf8, Timestamp(Millisecond, None)]),+ Exact(vec![+ Utf8,+ Timestamp(Millisecond, Some(TIMEZONE_WILDCARD.into())),+ ]),+ Exact(vec![Utf8, Timestamp(Microsecond, None)]),+ Exact(vec![+ Utf8,+ Timestamp(Microsecond, Some(TIMEZONE_WILDCARD.into())),+ ]),+ Exact(vec![Utf8, Timestamp(Second, None)]),+ Exact(vec![+ Utf8,+ Timestamp(Second, Some(TIMEZONE_WILDCARD.into())),+ ]),+ Exact(vec![Utf8, Date64]),+ Exact(vec![Utf8, Date32]),+ ],+ Volatility::Immutable,+ ),+ aliases: vec![String::from("datepart")],+ }+ }+}++macro_rules! extract_date_part {+ ($ARRAY: expr, $FN:expr) => {+ match $ARRAY.data_type() {+ DataType::Date32 => {+ let array = as_date32_array($ARRAY)?;+ Ok($FN(array)+ .map(|v| cast(&(Arc::new(v) as ArrayRef), &DataType::Float64))?)+ }+ DataType::Date64 => {+ let array = as_date64_array($ARRAY)?;+ Ok($FN(array)+ .map(|v| cast(&(Arc::new(v) as ArrayRef), &DataType::Float64))?)+ }+ DataType::Timestamp(time_unit, _) => match time_unit {+ TimeUnit::Second => {+ let array = as_timestamp_second_array($ARRAY)?;+ Ok($FN(array)+ .map(|v| cast(&(Arc::new(v) as ArrayRef), &DataType::Float64))?)+ }+ TimeUnit::Millisecond => {+ let array = as_timestamp_millisecond_array($ARRAY)?;+ Ok($FN(array)+ .map(|v| cast(&(Arc::new(v) as ArrayRef), &DataType::Float64))?)+ }+ TimeUnit::Microsecond => {+ let array = as_timestamp_microsecond_array($ARRAY)?;+ Ok($FN(array)+ .map(|v| cast(&(Arc::new(v) as ArrayRef), &DataType::Float64))?)+ }+ TimeUnit::Nanosecond => {+ let array = as_timestamp_nanosecond_array($ARRAY)?;+ Ok($FN(array)+ .map(|v| cast(&(Arc::new(v) as ArrayRef), &DataType::Float64))?)+ }+ },+ datatype => exec_err!("Extract does not support datatype {:?}", datatype),+ }+ };+}++impl ScalarUDFImpl for DatePartFunc {+ fn as_any(&self) -> &dyn Any {+ self+ }++ fn name(&self) -> &str {+ "date_part"+ }++ fn signature(&self) -> &Signature {+ &self.signature+ }++ fn return_type(&self, _arg_types: &[DataType]) -> Result<DataType> {+ Ok(Float64)+ }++ fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {+ if args.len() != 2 {+ return exec_err!("Expected two arguments in DATE_PART");+ }+ let (date_part, array) = (&args[0], &args[1]);++ let date_part =+ if let ColumnarValue::Scalar(ScalarValue::Utf8(Some(v))) = date_part {+ v+ } else {+ return exec_err!(+ "First argument of `DATE_PART` must be non-null scalar Utf8"+ );+ };++ let is_scalar = matches!(array, ColumnarValue::Scalar(_));++ let array = match array {+ ColumnarValue::Array(array) => array.clone(),+ ColumnarValue::Scalar(scalar) => scalar.to_array()?,+ };++ let arr = match date_part.to_lowercase().as_str() {+ "year" => extract_date_part!(&array, temporal::year),+ "quarter" => extract_date_part!(&array, temporal::quarter),+ "month" => extract_date_part!(&array, temporal::month),+ "week" => extract_date_part!(&array, temporal::week),+ "day" => extract_date_part!(&array, temporal::day),+ "doy" => extract_date_part!(&array, temporal::doy),+ "dow" => extract_date_part!(&array, temporal::num_days_from_sunday),+ "hour" => extract_date_part!(&array, temporal::hour),+ "minute" => extract_date_part!(&array, temporal::minute),+ "second" => extract_date_part!(&array, seconds),+ "millisecond" => extract_date_part!(&array, millis),+ "microsecond" => extract_date_part!(&array, micros),+ "nanosecond" => extract_date_part!(&array, nanos),+ "epoch" => extract_date_part!(&array, epoch),+ _ => exec_err!("Date part '{date_part}' not supported"),+ }?;++ Ok(if is_scalar {+ ColumnarValue::Scalar(ScalarValue::try_from_array(&arr?, 0)?)+ } else {+ ColumnarValue::Array(arr?)+ })+ }++ fn aliases(&self) -> &[String] {+ &self.aliases+ }+}++fn to_ticks<T>(array: &PrimitiveArray<T>, frac: i32) -> Result<Float64Array>+where+ T: ArrowTemporalType + ArrowNumericType,+ i64: From<T::Native>,+{+ let zipped = temporal::second(array)?+ .values()+ .iter()+ .zip(temporal::nanosecond(array)?.values().iter())+ .map(|o| (*o.0 as f64 + (*o.1 as f64) / 1_000_000_000.0) * (frac as f64))+ .collect::<Vec<f64>>();++ Ok(Float64Array::from(zipped))+}++fn seconds<T>(array: &PrimitiveArray<T>) -> Result<Float64Array>+where+ T: ArrowTemporalType + ArrowNumericType,+ i64: From<T::Native>,+{+ to_ticks(array, 1)+}++fn millis<T>(array: &PrimitiveArray<T>) -> Result<Float64Array>+where+ T: ArrowTemporalType + ArrowNumericType,+ i64: From<T::Native>,+{+ to_ticks(array, 1_000)+}++fn micros<T>(array: &PrimitiveArray<T>) -> Result<Float64Array>+where+ T: ArrowTemporalType + ArrowNumericType,+ i64: From<T::Native>,+{+ to_ticks(array, 1_000_000)+}++fn nanos<T>(array: &PrimitiveArray<T>) -> Result<Float64Array>+where+ T: ArrowTemporalType + ArrowNumericType,+ i64: From<T::Native>,+{+ to_ticks(array, 1_000_000_000)+}++fn epoch<T>(array: &PrimitiveArray<T>) -> Result<Float64Array>+where+ T: ArrowTemporalType + ArrowNumericType,+ i64: From<T::Native>,+{+ let b = match array.data_type() {+ Timestamp(tu, _) => {+ let scale = match tu {+ Second => 1,+ Millisecond => 1_000,+ Microsecond => 1_000_000,+ Nanosecond => 1_000_000_000,+ } as f64;+ array.unary(|n| {+ let n: i64 = n.into();+ n as f64 / scale+ })+ }+ Date32 => {+ let seconds_in_a_day = 86400_f64;+ array.unary(|n| {+ let n: i64 = n.into();+ n as f64 * seconds_in_a_day+ })+ }+ Date64 => array.unary(|n| {+ let n: i64 = n.into();+ n as f64 / 1_000_f64+ }),+ _ => return exec_err!("Can not convert {:?} to epoch", array.data_type()),+ };+ Ok(b)+}diff --git a/datafusion/functions/src/datetime/date_trunc.rs b/datafusion/functions/src/datetime/date_trunc.rsnew file mode 100644index 0000000000..3075107126--- /dev/null+++ b/datafusion/functions/src/datetime/date_trunc.rs@@ -0,0 +1,827 @@+// Licensed to the Apache Software Foundation (ASF) under one+// or more contributor license agreements. See the NOTICE file+// distributed with this work for additional information+// regarding copyright ownership. The ASF licenses this file+// to you under the Apache License, Version 2.0 (the+// "License"); you may not use this file except in compliance+// with the License. You may obtain a copy of the License at+//+// http://www.apache.org/licenses/LICENSE-2.0+//+// Unless required by applicable law or agreed to in writing,+// software distributed under the License is distributed on an+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY+// KIND, either express or implied. See the License for the+// specific language governing permissions and limitations+// under the License.++use std::any::Any;+use std::ops::{Add, Sub};+use std::str::FromStr;+use std::sync::Arc;++use arrow::datatypes::DataType::{Null, Timestamp, Utf8};+use arrow::datatypes::TimeUnit::{Microsecond, Millisecond, Nanosecond, Second};+use arrow::datatypes::{DataType, TimeUnit};+use arrow_array::temporal_conversions::{+ as_datetime_with_timezone, timestamp_ns_to_datetime,+};+use arrow_array::timezone::Tz;+use arrow_array::types::{+ ArrowTimestampType, TimestampMicrosecondType, TimestampMillisecondType,+ TimestampNanosecondType, TimestampSecondType,+};+use arrow_array::{Array, PrimitiveArray};+use chrono::{+ DateTime, Datelike, Duration, LocalResult, NaiveDateTime, Offset, TimeDelta, Timelike,+};++use datafusion_common::cast::as_primitive_array;+use datafusion_common::{exec_err, plan_err, DataFusionError, Result, ScalarValue};+use datafusion_expr::TypeSignature::Exact;+use datafusion_expr::{+ ColumnarValue, FuncMonotonicity, ScalarUDFImpl, Signature, Volatility,+ TIMEZONE_WILDCARD,+};++#[derive(Debug)]+pub(super) struct DateTruncFunc {+ signature: Signature,+ aliases: Vec<String>,+}++impl DateTruncFunc {+ pub fn new() -> Self {+ Self {+ signature: Signature::one_of(+ vec![+ Exact(vec![Utf8, Timestamp(Nanosecond, None)]),+ Exact(vec![+ Utf8,+ Timestamp(Nanosecond, Some(TIMEZONE_WILDCARD.into())),+ ]),+ Exact(vec![Utf8, Timestamp(Microsecond, None)]),+ Exact(vec![+ Utf8,+ Timestamp(Microsecond, Some(TIMEZONE_WILDCARD.into())),+ ]),+ Exact(vec![Utf8, Timestamp(Millisecond, None)]),+ Exact(vec![+ Utf8,+ Timestamp(Millisecond, Some(TIMEZONE_WILDCARD.into())),+ ]),+ Exact(vec![Utf8, Timestamp(Second, None)]),+ Exact(vec![+ Utf8,+ Timestamp(Second, Some(TIMEZONE_WILDCARD.into())),+ ]),+ ],+ Volatility::Immutable,+ ),+ aliases: vec![String::from("datetrunc")],+ }+ }+}++impl ScalarUDFImpl for DateTruncFunc {+ fn as_any(&self) -> &dyn Any {+ self+ }++ fn name(&self) -> &str {+ "date_trunc"+ }++ fn signature(&self) -> &Signature {+ &self.signature+ }++ fn return_type(&self, arg_types: &[DataType]) -> Result<DataType> {+ match &arg_types[1] {+ Timestamp(Nanosecond, None) | Utf8 | Null => Ok(Timestamp(Nanosecond, None)),+ Timestamp(Nanosecond, tz_opt) => Ok(Timestamp(Nanosecond, tz_opt.clone())),+ Timestamp(Microsecond, tz_opt) => Ok(Timestamp(Microsecond, tz_opt.clone())),+ Timestamp(Millisecond, tz_opt) => Ok(Timestamp(Millisecond, tz_opt.clone())),+ Timestamp(Second, tz_opt) => Ok(Timestamp(Second, tz_opt.clone())),+ _ => plan_err!(+ "The date_trunc function can only accept timestamp as the second arg."+ ),+ }+ }++ fn invoke(&self, args: &[ColumnarValue]) -> Result<ColumnarValue> {+ let (granularity, array) = (&args[0], &args[1]);++ let granularity = if let ColumnarValue::Scalar(ScalarValue::Utf8(Some(v))) =+ granularity+ {+ v.to_lowercase()+ } else {+ return exec_err!("Granularity of `date_trunc` must be non-null scalar Utf8");+ };++ fn process_array<T: ArrowTimestampType>(+ array: &dyn Array,+ granularity: String,+ tz_opt: &Option<Arc<str>>,+ ) -> Result<ColumnarValue> {+ let parsed_tz = parse_tz(tz_opt)?;+ let array = as_primitive_array::<T>(array)?;+ let array = array+ .iter()+ .map(|x| general_date_trunc(T::UNIT, &x, parsed_tz, granularity.as_str()))+ .collect::<Result<PrimitiveArray<T>>>()?+ .with_timezone_opt(tz_opt.clone());+ Ok(ColumnarValue::Array(Arc::new(array)))+ }++ fn process_scalar<T: ArrowTimestampType>(+ v: &Option<i64>,+ granularity: String,+ tz_opt: &Option<Arc<str>>,+ ) -> Result<ColumnarValue> {+ let parsed_tz = parse_tz(tz_opt)?;+ let value = general_date_trunc(T::UNIT, v, parsed_tz, granularity.as_str())?;+ let value = ScalarValue::new_timestamp::<T>(value, tz_opt.clone());+ Ok(ColumnarValue::Scalar(value))+ }++ Ok(match array {+ ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(v, tz_opt)) => {+ process_scalar::<TimestampNanosecondType>(v, granularity, tz_opt)?+ }+ ColumnarValue::Scalar(ScalarValue::TimestampMicrosecond(v, tz_opt)) => {+ process_scalar::<TimestampMicrosecondType>(v, granularity, tz_opt)?+ }+ ColumnarValue::Scalar(ScalarValue::TimestampMillisecond(v, tz_opt)) => {+ process_scalar::<TimestampMillisecondType>(v, granularity, tz_opt)?+ }+ ColumnarValue::Scalar(ScalarValue::TimestampSecond(v, tz_opt)) => {+ process_scalar::<TimestampSecondType>(v, granularity, tz_opt)?+ }+ ColumnarValue::Array(array) => {+ let array_type = array.data_type();+ match array_type {+ Timestamp(Second, tz_opt) => {+ process_array::<TimestampSecondType>(array, granularity, tz_opt)?+ }+ Timestamp(Millisecond, tz_opt) => process_array::<+ TimestampMillisecondType,+ >(+ array, granularity, tz_opt+ )?,+ Timestamp(Microsecond, tz_opt) => process_array::<+ TimestampMicrosecondType,+ >(+ array, granularity, tz_opt+ )?,+ Timestamp(Nanosecond, tz_opt) => process_array::<+ TimestampNanosecondType,+ >(+ array, granularity, tz_opt+ )?,+ _ => process_array::<TimestampNanosecondType>(+ array,+ granularity,+ &None,+ )?,+ }+ }+ _ => {+ return exec_err!(+ "second argument of `date_trunc` must be nanosecond timestamp scalar or array"+ );+ }+ })+ }++ fn aliases(&self) -> &[String] {+ &self.aliases+ }++ fn monotonicity(&self) -> Result<Option<FuncMonotonicity>> {+ Ok(Some(vec![None, Some(true)]))+ }+}++fn _date_trunc_coarse<T>(granularity: &str, value: Option<T>) -> Result<Option<T>>+where+ T: Datelike + Timelike + Sub<Duration, Output = T> + Copy,+{+ let value = match granularity {+ "millisecond" => value,+ "microsecond" => value,+ "second" => value.and_then(|d| d.with_nanosecond(0)),+ "minute" => value+ .and_then(|d| d.with_nanosecond(0))+ .and_then(|d| d.with_second(0)),+ "hour" => value+ .and_then(|d| d.with_nanosecond(0))+ .and_then(|d| d.with_second(0))+ .and_then(|d| d.with_minute(0)),+ "day" => value+ .and_then(|d| d.with_nanosecond(0))+ .and_then(|d| d.with_second(0))+ .and_then(|d| d.with_minute(0))+ .and_then(|d| d.with_hour(0)),+ "week" => value+ .and_then(|d| d.with_nanosecond(0))+ .and_then(|d| d.with_second(0))+ .and_then(|d| d.with_minute(0))+ .and_then(|d| d.with_hour(0))+ .map(|d| {+ d - TimeDelta::try_seconds(60 * 60 * 24 * d.weekday() as i64).unwrap()+ }),+ "month" => value+ .and_then(|d| d.with_nanosecond(0))+ .and_then(|d| d.with_second(0))+ .and_then(|d| d.with_minute(0))+ .and_then(|d| d.with_hour(0))+ .and_then(|d| d.with_day0(0)),+ "quarter" => value+ .and_then(|d| d.with_nanosecond(0))+ .and_then(|d| d.with_second(0))+ .and_then(|d| d.with_minute(0))+ .and_then(|d| d.with_hour(0))+ .and_then(|d| d.with_day0(0))+ .and_then(|d| d.with_month(quarter_month(&d))),+ "year" => value+ .and_then(|d| d.with_nanosecond(0))+ .and_then(|d| d.with_second(0))+ .and_then(|d| d.with_minute(0))+ .and_then(|d| d.with_hour(0))+ .and_then(|d| d.with_day0(0))+ .and_then(|d| d.with_month0(0)),+ unsupported => {+ return exec_err!("Unsupported date_trunc granularity: {unsupported}");+ }+ };+ Ok(value)+}++fn quarter_month<T>(date: &T) -> u32+where+ T: Datelike,+{+ 1 + 3 * ((date.month() - 1) / 3)+}++fn _date_trunc_coarse_with_tz(+ granularity: &str,+ value: Option<DateTime<Tz>>,+) -> Result<Option<i64>> {+ if let Some(value) = value {+ let local = value.naive_local();+ let truncated = _date_trunc_coarse::<NaiveDateTime>(granularity, Some(local))?;+ let truncated = truncated.and_then(|truncated| {+ match truncated.and_local_timezone(value.timezone()) {+ LocalResult::None => {+ // This can happen if the date_trunc operation moves the time into+ // an hour that doesn't exist due to daylight savings. On known example where+ // this can happen is with historic dates in the America/Sao_Paulo time zone.+ // To account for this adjust the time by a few hours, convert to local time,+ // and then adjust the time back.+ truncated+ .sub(TimeDelta::try_hours(3).unwrap())+ .and_local_timezone(value.timezone())+ .single()+ .map(|v| v.add(TimeDelta::try_hours(3).unwrap()))+ }+ LocalResult::Single(datetime) => Some(datetime),+ LocalResult::Ambiguous(datetime1, datetime2) => {+ // Because we are truncating from an equally or more specific time+ // the original time must have been within the ambiguous local time+ // period. Therefore the offset of one of these times should match the+ // offset of the original time.+ if datetime1.offset().fix() == value.offset().fix() {+ Some(datetime1)+ } else {+ Some(datetime2)+ }+ }+ }+ });+ Ok(truncated.and_then(|value| value.timestamp_nanos_opt()))+ } else {+ _date_trunc_coarse::<NaiveDateTime>(granularity, None)?;+ Ok(None)+ }+}++fn _date_trunc_coarse_without_tz(+ granularity: &str,+ value: Option<NaiveDateTime>,+) -> Result<Option<i64>> {+ let value = _date_trunc_coarse::<NaiveDateTime>(granularity, value)?;+ Ok(value.and_then(|value| value.and_utc().timestamp_nanos_opt()))+}++/// Truncates the single `value`, expressed in nanoseconds since the+/// epoch, for granularities greater than 1 second, in taking into+/// account that some granularities are not uniform durations of time+/// (e.g. months are not always the same lengths, leap seconds, etc)+fn date_trunc_coarse(granularity: &str, value: i64, tz: Option<Tz>) -> Result<i64> {+ let value = match tz {+ Some(tz) => {+ // Use chrono DateTime<Tz> to clear the various fields because need to clear per timezone,+ // and NaiveDateTime (ISO 8601) has no concept of timezones+ let value = as_datetime_with_timezone::<TimestampNanosecondType>(value, tz)+ .ok_or(DataFusionError::Execution(format!(+ "Timestamp {value} out of range"+ )))?;+ _date_trunc_coarse_with_tz(granularity, Some(value))+ }+ None => {+ // Use chrono NaiveDateTime to clear the various fields, if we don't have a timezone.+ let value = timestamp_ns_to_datetime(value).ok_or_else(|| {+ DataFusionError::Execution(format!("Timestamp {value} out of range"))+ })?;+ _date_trunc_coarse_without_tz(granularity, Some(value))+ }+ }?;++ // `with_x(0)` are infallible because `0` are always a valid+ Ok(value.unwrap())+}++// truncates a single value with the given timeunit to the specified granularity+fn general_date_trunc(+ tu: TimeUnit,+ value: &Option<i64>,+ tz: Option<Tz>,+ granularity: &str,+) -> Result<Option<i64>, DataFusionError> {+ let scale = match tu {+ Second => 1_000_000_000,+ Millisecond => 1_000_000,+ Microsecond => 1_000,+ Nanosecond => 1,+ };++ let Some(value) = value else {+ return Ok(None);+ };++ // convert to nanoseconds+ let nano = date_trunc_coarse(granularity, scale * value, tz)?;++ let result = match tu {+ Second => match granularity {+ "minute" => Some(nano / 1_000_000_000 / 60 * 60),+ _ => Some(nano / 1_000_000_000),+ },+ Millisecond => match granularity {+ "minute" => Some(nano / 1_000_000 / 1_000 / 60 * 1_000 * 60),+ "second" => Some(nano / 1_000_000 / 1_000 * 1_000),+ _ => Some(nano / 1_000_000),+ },+ Microsecond => match granularity {+ "minute" => Some(nano / 1_000 / 1_000_000 / 60 * 60 * 1_000_000),+ "second" => Some(nano / 1_000 / 1_000_000 * 1_000_000),+ "millisecond" => Some(nano / 1_000 / 1_000 * 1_000),+ _ => Some(nano / 1_000),+ },+ _ => match granularity {+ "minute" => Some(nano / 1_000_000_000 / 60 * 1_000_000_000 * 60),+ "second" => Some(nano / 1_000_000_000 * 1_000_000_000),+ "millisecond" => Some(nano / 1_000_000 * 1_000_000),+ "microsecond" => Some(nano / 1_000 * 1_000),+ _ => Some(nano),+ },+ };+ Ok(result)+}++fn parse_tz(tz: &Option<Arc<str>>) -> Result<Option<Tz>> {+ tz.as_ref()+ .map(|tz| {+ Tz::from_str(tz).map_err(|op| {+ DataFusionError::Execution(format!("failed on timezone {tz}: {:?}", op))+ })+ })+ .transpose()+}++#[cfg(test)]+mod tests {+ use crate::datetime::date_trunc::{date_trunc_coarse, DateTruncFunc};+ use arrow::compute::kernels::cast_utils::string_to_timestamp_nanos;+ use arrow::datatypes::{DataType, TimeUnit};+ use arrow_array::cast::as_primitive_array;+ use arrow_array::types::TimestampNanosecondType;+ use arrow_array::TimestampNanosecondArray;+ use datafusion_common::ScalarValue;+ use datafusion_expr::{ColumnarValue, ScalarUDFImpl};+ use std::sync::Arc;++ #[test]+ fn date_trunc_test() {+ let cases = vec![+ (+ "2020-09-08T13:42:29.190855Z",+ "second",+ "2020-09-08T13:42:29.000000Z",+ ),+ (+ "2020-09-08T13:42:29.190855Z",+ "minute",+ "2020-09-08T13:42:00.000000Z",+ ),+ (+ "2020-09-08T13:42:29.190855Z",+ "hour",+ "2020-09-08T13:00:00.000000Z",+ ),+ (+ "2020-09-08T13:42:29.190855Z",+ "day",+ "2020-09-08T00:00:00.000000Z",+ ),+ (+ "2020-09-08T13:42:29.190855Z",+ "week",+ "2020-09-07T00:00:00.000000Z",+ ),+ (+ "2020-09-08T13:42:29.190855Z",+ "month",+ "2020-09-01T00:00:00.000000Z",+ ),+ (+ "2020-09-08T13:42:29.190855Z",+ "year",+ "2020-01-01T00:00:00.000000Z",+ ),+ // week+ (+ "2021-01-01T13:42:29.190855Z",+ "week",+ "2020-12-28T00:00:00.000000Z",+ ),+ (+ "2020-01-01T13:42:29.190855Z",+ "week",+ "2019-12-30T00:00:00.000000Z",+ ),+ // quarter+ (+ "2020-01-01T13:42:29.190855Z",+ "quarter",+ "2020-01-01T00:00:00.000000Z",+ ),+ (+ "2020-02-01T13:42:29.190855Z",+ "quarter",+ "2020-01-01T00:00:00.000000Z",+ ),+ (+ "2020-03-01T13:42:29.190855Z",+ "quarter",+ "2020-01-01T00:00:00.000000Z",+ ),+ (+ "2020-04-01T13:42:29.190855Z",+ "quarter",+ "2020-04-01T00:00:00.000000Z",+ ),+ (+ "2020-08-01T13:42:29.190855Z",+ "quarter",+ "2020-07-01T00:00:00.000000Z",+ ),+ (+ "2020-11-01T13:42:29.190855Z",+ "quarter",+ "2020-10-01T00:00:00.000000Z",+ ),+ (+ "2020-12-01T13:42:29.190855Z",+ "quarter",+ "2020-10-01T00:00:00.000000Z",+ ),+ ];++ cases.iter().for_each(|(original, granularity, expected)| {+ let left = string_to_timestamp_nanos(original).unwrap();+ let right = string_to_timestamp_nanos(expected).unwrap();+ let result = date_trunc_coarse(granularity, left, None).unwrap();+ assert_eq!(result, right, "{original} = {expected}");+ });+ }++ #[test]+ fn test_date_trunc_timezones() {+ let cases = vec![+ (+ vec![+ "2020-09-08T00:00:00Z",+ "2020-09-08T01:00:00Z",+ "2020-09-08T02:00:00Z",+ "2020-09-08T03:00:00Z",+ "2020-09-08T04:00:00Z",+ ],+ Some("+00".into()),+ vec![+ "2020-09-08T00:00:00Z",+ "2020-09-08T00:00:00Z",+ "2020-09-08T00:00:00Z",+ "2020-09-08T00:00:00Z",+ "2020-09-08T00:00:00Z",+ ],+ ),+ (+ vec![+ "2020-09-08T00:00:00Z",+ "2020-09-08T01:00:00Z",+ "2020-09-08T02:00:00Z",+ "2020-09-08T03:00:00Z",+ "2020-09-08T04:00:00Z",+ ],+ None,+ vec![+ "2020-09-08T00:00:00Z",+ "2020-09-08T00:00:00Z",+ "2020-09-08T00:00:00Z",+ "2020-09-08T00:00:00Z",+ "2020-09-08T00:00:00Z",+ ],+ ),+ (+ vec![+ "2020-09-08T00:00:00Z",+ "2020-09-08T01:00:00Z",+ "2020-09-08T02:00:00Z",+ "2020-09-08T03:00:00Z",+ "2020-09-08T04:00:00Z",+ ],+ Some("-02".into()),+ vec![+ "2020-09-07T02:00:00Z",+ "2020-09-07T02:00:00Z",+ "2020-09-08T02:00:00Z",+ "2020-09-08T02:00:00Z",+ "2020-09-08T02:00:00Z",+ ],+ ),+ (+ vec![+ "2020-09-08T00:00:00+05",+ "2020-09-08T01:00:00+05",+ "2020-09-08T02:00:00+05",+ "2020-09-08T03:00:00+05",+ "2020-09-08T04:00:00+05",+ ],+ Some("+05".into()),+ vec![+ "2020-09-08T00:00:00+05",+ "2020-09-08T00:00:00+05",+ "2020-09-08T00:00:00+05",+ "2020-09-08T00:00:00+05",+ "2020-09-08T00:00:00+05",+ ],+ ),+ (+ vec![+ "2020-09-08T00:00:00+08",+ "2020-09-08T01:00:00+08",+ "2020-09-08T02:00:00+08",+ "2020-09-08T03:00:00+08",+ "2020-09-08T04:00:00+08",+ ],+ Some("+08".into()),+ vec![+ "2020-09-08T00:00:00+08",+ "2020-09-08T00:00:00+08",+ "2020-09-08T00:00:00+08",+ "2020-09-08T00:00:00+08",+ "2020-09-08T00:00:00+08",+ ],+ ),+ (+ vec![+ "2024-10-26T23:00:00Z",+ "2024-10-27T00:00:00Z",+ "2024-10-27T01:00:00Z",+ "2024-10-27T02:00:00Z",+ ],+ Some("Europe/Berlin".into()),+ vec![+ "2024-10-27T00:00:00+02",+ "2024-10-27T00:00:00+02",+ "2024-10-27T00:00:00+02",+ "2024-10-27T00:00:00+02",+ ],+ ),+ (+ vec![+ "2018-02-18T00:00:00Z",+ "2018-02-18T01:00:00Z",+ "2018-02-18T02:00:00Z",+ "2018-02-18T03:00:00Z",+ "2018-11-04T01:00:00Z",+ "2018-11-04T02:00:00Z",+ "2018-11-04T03:00:00Z",+ "2018-11-04T04:00:00Z",+ ],+ Some("America/Sao_Paulo".into()),+ vec![+ "2018-02-17T00:00:00-02",+ "2018-02-17T00:00:00-02",+ "2018-02-17T00:00:00-02",+ "2018-02-18T00:00:00-03",+ "2018-11-03T00:00:00-03",+ "2018-11-03T00:00:00-03",+ "2018-11-04T01:00:00-02",+ "2018-11-04T01:00:00-02",+ ],+ ),+ ];++ cases.iter().for_each(|(original, tz_opt, expected)| {+ let input = original+ .iter()+ .map(|s| Some(string_to_timestamp_nanos(s).unwrap()))+ .collect::<TimestampNanosecondArray>()+ .with_timezone_opt(tz_opt.clone());+ let right = expected+ .iter()+ .map(|s| Some(string_to_timestamp_nanos(s).unwrap()))+ .collect::<TimestampNanosecondArray>()+ .with_timezone_opt(tz_opt.clone());+ let result = DateTruncFunc::new()+ .invoke(&[+ ColumnarValue::Scalar(ScalarValue::from("day")),+ ColumnarValue::Array(Arc::new(input)),+ ])+ .unwrap();+ if let ColumnarValue::Array(result) = result {+ assert_eq!(+ result.data_type(),+ &DataType::Timestamp(TimeUnit::Nanosecond, tz_opt.clone())+ );+ let left = as_primitive_array::<TimestampNanosecondType>(&result);+ assert_eq!(left, &right);+ } else {+ panic!("unexpected column type");+ }+ });+ }++ #[test]+ fn test_date_trunc_hour_timezones() {+ let cases = vec![+ (+ vec![+ "2020-09-08T00:30:00Z",+ "2020-09-08T01:30:00Z",+ "2020-09-08T02:30:00Z",+ "2020-09-08T03:30:00Z",+ "2020-09-08T04:30:00Z",+ ],+ Some("+00".into()),+ vec![+ "2020-09-08T00:00:00Z",+ "2020-09-08T01:00:00Z",+ "2020-09-08T02:00:00Z",+ "2020-09-08T03:00:00Z",+ "2020-09-08T04:00:00Z",+ ],+ ),+ (+ vec![+ "2020-09-08T00:30:00Z",+ "2020-09-08T01:30:00Z",+ "2020-09-08T02:30:00Z",+ "2020-09-08T03:30:00Z",+ "2020-09-08T04:30:00Z",+ ],+ None,+ vec![+ "2020-09-08T00:00:00Z",+ "2020-09-08T01:00:00Z",+ "2020-09-08T02:00:00Z",+ "2020-09-08T03:00:00Z",+ "2020-09-08T04:00:00Z",+ ],+ ),+ (+ vec![+ "2020-09-08T00:30:00Z",+ "2020-09-08T01:30:00Z",+ "2020-09-08T02:30:00Z",+ "2020-09-08T03:30:00Z",+ "2020-09-08T04:30:00Z",+ ],+ Some("-02".into()),+ vec![+ "2020-09-08T00:00:00Z",+ "2020-09-08T01:00:00Z",+ "2020-09-08T02:00:00Z",+ "2020-09-08T03:00:00Z",+ "2020-09-08T04:00:00Z",+ ],+ ),+ (+ vec![+ "2020-09-08T00:30:00+05",+ "2020-09-08T01:30:00+05",+ "2020-09-08T02:30:00+05",+ "2020-09-08T03:30:00+05",+ "2020-09-08T04:30:00+05",+ ],+ Some("+05".into()),+ vec![+ "2020-09-08T00:00:00+05",+ "2020-09-08T01:00:00+05",+ "2020-09-08T02:00:00+05",+ "2020-09-08T03:00:00+05",+ "2020-09-08T04:00:00+05",+ ],+ ),+ (+ vec![+ "2020-09-08T00:30:00+08",+ "2020-09-08T01:30:00+08",+ "2020-09-08T02:30:00+08",+ "2020-09-08T03:30:00+08",+ "2020-09-08T04:30:00+08",+ ],+ Some("+08".into()),+ vec![+ "2020-09-08T00:00:00+08",+ "2020-09-08T01:00:00+08",+ "2020-09-08T02:00:00+08",+ "2020-09-08T03:00:00+08",+ "2020-09-08T04:00:00+08",+ ],+ ),+ (+ vec![+ "2024-10-26T23:30:00Z",+ "2024-10-27T00:30:00Z",+ "2024-10-27T01:30:00Z",+ "2024-10-27T02:30:00Z",+ ],+ Some("Europe/Berlin".into()),+ vec![+ "2024-10-27T01:00:00+02",+ "2024-10-27T02:00:00+02",+ "2024-10-27T02:00:00+01",+ "2024-10-27T03:00:00+01",+ ],+ ),+ (+ vec![+ "2018-02-18T00:30:00Z",+ "2018-02-18T01:30:00Z",+ "2018-02-18T02:30:00Z",+ "2018-02-18T03:30:00Z",+ "2018-11-04T01:00:00Z",+ "2018-11-04T02:00:00Z",+ "2018-11-04T03:00:00Z",+ "2018-11-04T04:00:00Z",+ ],+ Some("America/Sao_Paulo".into()),+ vec![+ "2018-02-17T22:00:00-02",+ "2018-02-17T23:00:00-02",+ "2018-02-17T23:00:00-03",+ "2018-02-18T00:00:00-03",+ "2018-11-03T22:00:00-03",+ "2018-11-03T23:00:00-03",+ "2018-11-04T01:00:00-02",+ "2018-11-04T02:00:00-02",+ ],+ ),+ ];++ cases.iter().for_each(|(original, tz_opt, expected)| {+ let input = original+ .iter()+ .map(|s| Some(string_to_timestamp_nanos(s).unwrap()))+ .collect::<TimestampNanosecondArray>()+ .with_timezone_opt(tz_opt.clone());+ let right = expected+ .iter()+ .map(|s| Some(string_to_timestamp_nanos(s).unwrap()))+ .collect::<TimestampNanosecondArray>()+ .with_timezone_opt(tz_opt.clone());+ let result = DateTruncFunc::new()+ .invoke(&[+ ColumnarValue::Scalar(ScalarValue::from("hour")),+ ColumnarValue::Array(Arc::new(input)),+ ])+ .unwrap();+ if let ColumnarValue::Array(result) = result {+ assert_eq!(+ result.data_type(),+ &DataType::Timestamp(TimeUnit::Nanosecond, tz_opt.clone())+ );+ let left = as_primitive_array::<TimestampNanosecondType>(&result);+ assert_eq!(left, &right);+ } else {+ panic!("unexpected column type");+ }+ });+ }+}diff --git a/datafusion/functions/src/datetime/mod.rs b/datafusion/functions/src/datetime/mod.rsindex 6c9ea1944b..7792174a76 100644--- a/datafusion/functions/src/datetime/mod.rs+++ b/datafusion/functions/src/datetime/mod.rs@@ -22,11 +22,17 @@ use std::sync::Arc;use datafusion_expr::ScalarUDF;mod common;+mod date_bin;+mod date_part;+mod date_trunc;mod to_date;mod to_timestamp;mod to_unixtime;// create UDFs+make_udf_function!(date_bin::DateBinFunc, DATE_BIN, date_bin);+make_udf_function!(date_part::DatePartFunc, DATE_PART, date_part);+make_udf_function!(date_trunc::DateTruncFunc, DATE_TRUNC, date_trunc);make_udf_function!(to_date::ToDateFunc, TO_DATE, to_date);make_udf_function!(to_unixtime::ToUnixtimeFunc, TO_UNIXTIME, to_unixtime);make_udf_function!(to_timestamp::ToTimestampFunc, TO_TIMESTAMP, to_timestamp);@@ -57,6 +63,21 @@ make_udf_function!(pub mod expr_fn {use datafusion_expr::Expr;+ #[doc = "coerces an arbitrary timestamp to the start of the nearest specified interval"]+ pub fn date_bin(stride: Expr, source: Expr, origin: Expr) -> Expr {+ super::date_bin().call(vec![stride, source, origin])+ }++ #[doc = "extracts a subfield from the date"]+ pub fn date_part(part: Expr, date: Expr) -> Expr {+ super::date_part().call(vec![part, date])+ }++ #[doc = "truncates the date to a specified level of precision"]+ pub fn date_trunc(part: Expr, date: Expr) -> Expr {+ super::date_trunc().call(vec![part, date])+ }+/// ```ignore/// # use std::sync::Arc;///@@ -141,6 +162,9 @@ pub mod expr_fn {/// Return a list of all functions in this packagepub fn functions() -> Vec<Arc<ScalarUDF>> {vec![+ date_bin(),+ date_part(),+ date_trunc(),to_date(),to_unixtime(),to_timestamp(),diff --git a/datafusion/optimizer/src/optimize_projections.rs b/datafusion/optimizer/src/optimize_projections.rsindex d8d7f71d71..08ee38f64a 100644--- a/datafusion/optimizer/src/optimize_projections.rs+++ b/datafusion/optimizer/src/optimize_projections.rs@@ -33,7 +33,7 @@ use arrow::datatypes::SchemaRef;use datafusion_common::{get_required_group_by_exprs_indices, Column, DFSchema, DFSchemaRef, JoinType, Result,};-use datafusion_expr::expr::{Alias, ScalarFunction, ScalarFunctionDefinition};+use datafusion_expr::expr::{Alias, ScalarFunction};use datafusion_expr::{logical_plan::LogicalPlan, projection_schema, Aggregate, BinaryExpr, Cast, Distinct,Expr, Projection, TableScan, Window,@@ -558,17 +558,16 @@ fn rewrite_expr(expr: &Expr, input: &Projection) -> Result<Option<Expr>> {Expr::Cast(Cast::new(Box::new(new_expr), cast.data_type.clone()))}Expr::ScalarFunction(scalar_fn) => {- // TODO: Support UDFs.- let ScalarFunctionDefinition::BuiltIn(fun) = scalar_fn.func_def else {- return Ok(None);- };return Ok(scalar_fn.args.iter().map(|expr| rewrite_expr(expr, input)).collect::<Result<Option<_>>>()?.map(|new_args| {- Expr::ScalarFunction(ScalarFunction::new(fun, new_args))+ Expr::ScalarFunction(ScalarFunction::new_func_def(+ scalar_fn.func_def.clone(),+ new_args,+ ))}));}// Unsupported type for consecutive projection merge analysis.diff --git a/datafusion/physical-expr/src/datetime_expressions.rs b/datafusion/physical-expr/src/datetime_expressions.rsindex e2e74a97d5..6a9a03bec0 100644--- a/datafusion/physical-expr/src/datetime_expressions.rs+++ b/datafusion/physical-expr/src/datetime_expressions.rs@@ -17,40 +17,20 @@//! DateTime expressions-use std::ops::{Add, Sub};-use std::str::FromStr;use std::sync::Arc;-use arrow::compute::cast;+use arrow::datatypes::TimeUnit;use arrow::util::display::{ArrayFormatter, DurationFormat, FormatOptions};use arrow::{- array::{Array, ArrayRef, Float64Array, PrimitiveArray},- datatypes::{- ArrowNumericType, ArrowTemporalType, DataType, IntervalDayTimeType,- IntervalMonthDayNanoType, TimestampMicrosecondType, TimestampMillisecondType,- TimestampNanosecondType, TimestampSecondType,- },-};-use arrow::{- compute::kernels::temporal,- datatypes::TimeUnit,- temporal_conversions::{as_datetime_with_timezone, timestamp_ns_to_datetime},+ array::{Array, ArrayRef, PrimitiveArray},+ datatypes::DataType,};use arrow_array::builder::PrimitiveBuilder;use arrow_array::cast::AsArray;-use arrow_array::temporal_conversions::NANOSECONDS;-use arrow_array::timezone::Tz;-use arrow_array::types::{ArrowTimestampType, Date32Type, Int32Type};+use arrow_array::types::{Date32Type, Int32Type};use arrow_array::StringArray;-use chrono::{prelude::*, TimeDelta};-use chrono::{Duration, LocalResult, Months, NaiveDate};--use datafusion_common::cast::{- as_date32_array, as_date64_array, as_primitive_array, as_timestamp_microsecond_array,- as_timestamp_millisecond_array, as_timestamp_nanosecond_array,- as_timestamp_second_array,-};-use datafusion_common::{exec_err, not_impl_err, DataFusionError, Result, ScalarValue};+use chrono::{DateTime, Datelike, NaiveDate, Utc};+use datafusion_common::{exec_err, Result, ScalarValue};use datafusion_expr::ColumnarValue;/// Create an implementation of `now()` that always returns the@@ -419,731 +399,6 @@ pub fn make_date(args: &[ColumnarValue]) -> Result<ColumnarValue> {}}-fn quarter_month<T>(date: &T) -> u32-where- T: Datelike,-{- 1 + 3 * ((date.month() - 1) / 3)-}--fn _date_trunc_coarse<T>(granularity: &str, value: Option<T>) -> Result<Option<T>>-where- T: Datelike + Timelike + Sub<Duration, Output = T> + Copy,-{- let value = match granularity {- "millisecond" => value,- "microsecond" => value,- "second" => value.and_then(|d| d.with_nanosecond(0)),- "minute" => value- .and_then(|d| d.with_nanosecond(0))- .and_then(|d| d.with_second(0)),- "hour" => value- .and_then(|d| d.with_nanosecond(0))- .and_then(|d| d.with_second(0))- .and_then(|d| d.with_minute(0)),- "day" => value- .and_then(|d| d.with_nanosecond(0))- .and_then(|d| d.with_second(0))- .and_then(|d| d.with_minute(0))- .and_then(|d| d.with_hour(0)),- "week" => value- .and_then(|d| d.with_nanosecond(0))- .and_then(|d| d.with_second(0))- .and_then(|d| d.with_minute(0))- .and_then(|d| d.with_hour(0))- .map(|d| {- d - TimeDelta::try_seconds(60 * 60 * 24 * d.weekday() as i64).unwrap()- }),- "month" => value- .and_then(|d| d.with_nanosecond(0))- .and_then(|d| d.with_second(0))- .and_then(|d| d.with_minute(0))- .and_then(|d| d.with_hour(0))- .and_then(|d| d.with_day0(0)),- "quarter" => value- .and_then(|d| d.with_nanosecond(0))- .and_then(|d| d.with_second(0))- .and_then(|d| d.with_minute(0))- .and_then(|d| d.with_hour(0))- .and_then(|d| d.with_day0(0))- .and_then(|d| d.with_month(quarter_month(&d))),- "year" => value- .and_then(|d| d.with_nanosecond(0))- .and_then(|d| d.with_second(0))- .and_then(|d| d.with_minute(0))- .and_then(|d| d.with_hour(0))- .and_then(|d| d.with_day0(0))- .and_then(|d| d.with_month0(0)),- unsupported => {- return exec_err!("Unsupported date_trunc granularity: {unsupported}");- }- };- Ok(value)-}--fn _date_trunc_coarse_with_tz(- granularity: &str,- value: Option<DateTime<Tz>>,-) -> Result<Option<i64>> {- if let Some(value) = value {- let local = value.naive_local();- let truncated = _date_trunc_coarse::<NaiveDateTime>(granularity, Some(local))?;- let truncated = truncated.and_then(|truncated| {- match truncated.and_local_timezone(value.timezone()) {- LocalResult::None => {- // This can happen if the date_trunc operation moves the time into- // an hour that doesn't exist due to daylight savings. On known example where- // this can happen is with historic dates in the America/Sao_Paulo time zone.- // To account for this adjust the time by a few hours, convert to local time,- // and then adjust the time back.- truncated- .sub(TimeDelta::try_hours(3)?)- .and_local_timezone(value.timezone())- .single()- .map(|v| v.add(TimeDelta::try_hours(3).unwrap()))- }- LocalResult::Single(datetime) => Some(datetime),- LocalResult::Ambiguous(datetime1, datetime2) => {- // Because we are truncating from an equally or more specific time- // the original time must have been within the ambiguous local time- // period. Therefore the offset of one of these times should match the- // offset of the original time.- if datetime1.offset().fix() == value.offset().fix() {- Some(datetime1)- } else {- Some(datetime2)- }- }- }- });- Ok(truncated.and_then(|value| value.timestamp_nanos_opt()))- } else {- _date_trunc_coarse::<NaiveDateTime>(granularity, None)?;- Ok(None)- }-}--fn _date_trunc_coarse_without_tz(- granularity: &str,- value: Option<NaiveDateTime>,-) -> Result<Option<i64>> {- let value = _date_trunc_coarse::<NaiveDateTime>(granularity, value)?;- Ok(value.and_then(|value| value.and_utc().timestamp_nanos_opt()))-}--/// Tuncates the single `value`, expressed in nanoseconds since the-/// epoch, for granularities greater than 1 second, in taking into-/// account that some granularities are not uniform durations of time-/// (e.g. months are not always the same lengths, leap seconds, etc)-fn date_trunc_coarse(granularity: &str, value: i64, tz: Option<Tz>) -> Result<i64> {- let value = match tz {- Some(tz) => {- // Use chrono DateTime<Tz> to clear the various fields because need to clear per timezone,- // and NaiveDateTime (ISO 8601) has no concept of timezones- let value = as_datetime_with_timezone::<TimestampNanosecondType>(value, tz)- .ok_or(DataFusionError::Execution(format!(- "Timestamp {value} out of range"- )))?;- _date_trunc_coarse_with_tz(granularity, Some(value))- }- None => {- // Use chrono NaiveDateTime to clear the various fields, if we don't have a timezone.- let value = timestamp_ns_to_datetime(value).ok_or_else(|| {- DataFusionError::Execution(format!("Timestamp {value} out of range"))- })?;- _date_trunc_coarse_without_tz(granularity, Some(value))- }- }?;-- // `with_x(0)` are infallible because `0` are always a valid- Ok(value.unwrap())-}--// truncates a single value with the given timeunit to the specified granularity-fn general_date_trunc(- tu: TimeUnit,- value: &Option<i64>,- tz: Option<Tz>,- granularity: &str,-) -> Result<Option<i64>, DataFusionError> {- let scale = match tu {- TimeUnit::Second => 1_000_000_000,- TimeUnit::Millisecond => 1_000_000,- TimeUnit::Microsecond => 1_000,- TimeUnit::Nanosecond => 1,- };-- let Some(value) = value else {- return Ok(None);- };-- // convert to nanoseconds- let nano = date_trunc_coarse(granularity, scale * value, tz)?;-- let result = match tu {- TimeUnit::Second => match granularity {- "minute" => Some(nano / 1_000_000_000 / 60 * 60),- _ => Some(nano / 1_000_000_000),- },- TimeUnit::Millisecond => match granularity {- "minute" => Some(nano / 1_000_000 / 1_000 / 60 * 1_000 * 60),- "second" => Some(nano / 1_000_000 / 1_000 * 1_000),- _ => Some(nano / 1_000_000),- },- TimeUnit::Microsecond => match granularity {- "minute" => Some(nano / 1_000 / 1_000_000 / 60 * 60 * 1_000_000),- "second" => Some(nano / 1_000 / 1_000_000 * 1_000_000),- "millisecond" => Some(nano / 1_000 / 1_000 * 1_000),- _ => Some(nano / 1_000),- },- _ => match granularity {- "minute" => Some(nano / 1_000_000_000 / 60 * 1_000_000_000 * 60),- "second" => Some(nano / 1_000_000_000 * 1_000_000_000),- "millisecond" => Some(nano / 1_000_000 * 1_000_000),- "microsecond" => Some(nano / 1_000 * 1_000),- _ => Some(nano),- },- };- Ok(result)-}--fn parse_tz(tz: &Option<Arc<str>>) -> Result<Option<Tz>> {- tz.as_ref()- .map(|tz| {- Tz::from_str(tz).map_err(|op| {- DataFusionError::Execution(format!("failed on timezone {tz}: {:?}", op))- })- })- .transpose()-}--/// date_trunc SQL function-pub fn date_trunc(args: &[ColumnarValue]) -> Result<ColumnarValue> {- let (granularity, array) = (&args[0], &args[1]);-- let granularity =- if let ColumnarValue::Scalar(ScalarValue::Utf8(Some(v))) = granularity {- v.to_lowercase()- } else {- return exec_err!("Granularity of `date_trunc` must be non-null scalar Utf8");- };-- fn process_array<T: ArrowTimestampType>(- array: &dyn Array,- granularity: String,- tz_opt: &Option<Arc<str>>,- ) -> Result<ColumnarValue> {- let parsed_tz = parse_tz(tz_opt)?;- let array = as_primitive_array::<T>(array)?;- let array = array- .iter()- .map(|x| general_date_trunc(T::UNIT, &x, parsed_tz, granularity.as_str()))- .collect::<Result<PrimitiveArray<T>>>()?- .with_timezone_opt(tz_opt.clone());- Ok(ColumnarValue::Array(Arc::new(array)))- }-- fn process_scalar<T: ArrowTimestampType>(- v: &Option<i64>,- granularity: String,- tz_opt: &Option<Arc<str>>,- ) -> Result<ColumnarValue> {- let parsed_tz = parse_tz(tz_opt)?;- let value = general_date_trunc(T::UNIT, v, parsed_tz, granularity.as_str())?;- let value = ScalarValue::new_timestamp::<T>(value, tz_opt.clone());- Ok(ColumnarValue::Scalar(value))- }-- Ok(match array {- ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(v, tz_opt)) => {- process_scalar::<TimestampNanosecondType>(v, granularity, tz_opt)?- }- ColumnarValue::Scalar(ScalarValue::TimestampMicrosecond(v, tz_opt)) => {- process_scalar::<TimestampMicrosecondType>(v, granularity, tz_opt)?- }- ColumnarValue::Scalar(ScalarValue::TimestampMillisecond(v, tz_opt)) => {- process_scalar::<TimestampMillisecondType>(v, granularity, tz_opt)?- }- ColumnarValue::Scalar(ScalarValue::TimestampSecond(v, tz_opt)) => {- process_scalar::<TimestampSecondType>(v, granularity, tz_opt)?- }- ColumnarValue::Array(array) => {- let array_type = array.data_type();- match array_type {- DataType::Timestamp(TimeUnit::Second, tz_opt) => {- process_array::<TimestampSecondType>(array, granularity, tz_opt)?- }- DataType::Timestamp(TimeUnit::Millisecond, tz_opt) => {- process_array::<TimestampMillisecondType>(array, granularity, tz_opt)?- }- DataType::Timestamp(TimeUnit::Microsecond, tz_opt) => {- process_array::<TimestampMicrosecondType>(array, granularity, tz_opt)?- }- DataType::Timestamp(TimeUnit::Nanosecond, tz_opt) => {- process_array::<TimestampNanosecondType>(array, granularity, tz_opt)?- }- _ => process_array::<TimestampNanosecondType>(array, granularity, &None)?,- }- }- _ => {- return exec_err!(- "second argument of `date_trunc` must be nanosecond timestamp scalar or array"- );- }- })-}--// return time in nanoseconds that the source timestamp falls into based on the stride and origin-fn date_bin_nanos_interval(stride_nanos: i64, source: i64, origin: i64) -> i64 {- let time_diff = source - origin;-- // distance from origin to bin- let time_delta = compute_distance(time_diff, stride_nanos);-- origin + time_delta-}--// distance from origin to bin-fn compute_distance(time_diff: i64, stride: i64) -> i64 {- let time_delta = time_diff - (time_diff % stride);-- if time_diff < 0 && stride > 1 {- // The origin is later than the source timestamp, round down to the previous bin- time_delta - stride- } else {- time_delta- }-}--// return time in nanoseconds that the source timestamp falls into based on the stride and origin-fn date_bin_months_interval(stride_months: i64, source: i64, origin: i64) -> i64 {- // convert source and origin to DateTime<Utc>- let source_date = to_utc_date_time(source);- let origin_date = to_utc_date_time(origin);-- // calculate the number of months between the source and origin- let month_diff = (source_date.year() - origin_date.year()) * 12- + source_date.month() as i32- - origin_date.month() as i32;-- // distance from origin to bin- let month_delta = compute_distance(month_diff as i64, stride_months);-- let mut bin_time = if month_delta < 0 {- origin_date - Months::new(month_delta.unsigned_abs() as u32)- } else {- origin_date + Months::new(month_delta as u32)- };-- // If origin is not midnight of first date of the month, the bin_time may be larger than the source- // In this case, we need to move back to previous bin- if bin_time > source_date {- let month_delta = month_delta - stride_months;- bin_time = if month_delta < 0 {- origin_date - Months::new(month_delta.unsigned_abs() as u32)- } else {- origin_date + Months::new(month_delta as u32)- };- }-- bin_time.timestamp_nanos_opt().unwrap()-}--fn to_utc_date_time(nanos: i64) -> DateTime<Utc> {- let secs = nanos / 1_000_000_000;- let nsec = (nanos % 1_000_000_000) as u32;- DateTime::from_timestamp(secs, nsec).unwrap()-}--/// DATE_BIN sql function-pub fn date_bin(args: &[ColumnarValue]) -> Result<ColumnarValue> {- if args.len() == 2 {- // Default to unix EPOCH- let origin = ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(- Some(0),- Some("+00:00".into()),- ));- date_bin_impl(&args[0], &args[1], &origin)- } else if args.len() == 3 {- date_bin_impl(&args[0], &args[1], &args[2])- } else {- exec_err!("DATE_BIN expected two or three arguments")- }-}--enum Interval {- Nanoseconds(i64),- Months(i64),-}--impl Interval {- /// Returns (`stride_nanos`, `fn`) where- ///- /// 1. `stride_nanos` is a width, in nanoseconds- /// 2. `fn` is a function that takes (stride_nanos, source, origin)- ///- /// `source` is the timestamp being binned- ///- /// `origin` is the time, in nanoseconds, where windows are measured from- fn bin_fn(&self) -> (i64, fn(i64, i64, i64) -> i64) {- match self {- Interval::Nanoseconds(nanos) => (*nanos, date_bin_nanos_interval),- Interval::Months(months) => (*months, date_bin_months_interval),- }- }-}--// Supported intervals:-// 1. IntervalDayTime: this means that the stride is in days, hours, minutes, seconds and milliseconds-// We will assume month interval won't be converted into this type-// TODO (my next PR): without `INTERVAL` keyword, the stride was converted into ScalarValue::IntervalDayTime somwhere-// for month interval. I need to find that and make it ScalarValue::IntervalMonthDayNano instead-// 2. IntervalMonthDayNano-fn date_bin_impl(- stride: &ColumnarValue,- array: &ColumnarValue,- origin: &ColumnarValue,-) -> Result<ColumnarValue> {- let stride = match stride {- ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(v))) => {- let (days, ms) = IntervalDayTimeType::to_parts(*v);- let nanos = (TimeDelta::try_days(days as i64).unwrap()- + TimeDelta::try_milliseconds(ms as i64).unwrap())- .num_nanoseconds();-- match nanos {- Some(v) => Interval::Nanoseconds(v),- _ => return exec_err!("DATE_BIN stride argument is too large"),- }- }- ColumnarValue::Scalar(ScalarValue::IntervalMonthDayNano(Some(v))) => {- let (months, days, nanos) = IntervalMonthDayNanoType::to_parts(*v);-- // If interval is months, its origin must be midnight of first date of the month- if months != 0 {- // Return error if days or nanos is not zero- if days != 0 || nanos != 0 {- return not_impl_err!(- "DATE_BIN stride does not support combination of month, day and nanosecond intervals"- );- } else {- Interval::Months(months as i64)- }- } else {- let nanos = (TimeDelta::try_days(days as i64).unwrap()- + Duration::nanoseconds(nanos))- .num_nanoseconds();- match nanos {- Some(v) => Interval::Nanoseconds(v),- _ => return exec_err!("DATE_BIN stride argument is too large"),- }- }- }- ColumnarValue::Scalar(v) => {- return exec_err!(- "DATE_BIN expects stride argument to be an INTERVAL but got {}",- v.data_type()- )- }- ColumnarValue::Array(_) => {- return not_impl_err!(- "DATE_BIN only supports literal values for the stride argument, not arrays"- )- }- };-- let origin = match origin {- ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(v), _)) => *v,- ColumnarValue::Scalar(v) => {- return exec_err!(- "DATE_BIN expects origin argument to be a TIMESTAMP with nanosececond precision but got {}",- v.data_type()- )- }- ColumnarValue::Array(_) => return not_impl_err!(- "DATE_BIN only supports literal values for the origin argument, not arrays"- ),- };-- let (stride, stride_fn) = stride.bin_fn();-- // Return error if stride is 0- if stride == 0 {- return exec_err!("DATE_BIN stride must be non-zero");- }-- fn stride_map_fn<T: ArrowTimestampType>(- origin: i64,- stride: i64,- stride_fn: fn(i64, i64, i64) -> i64,- ) -> impl Fn(Option<i64>) -> Option<i64> {- let scale = match T::UNIT {- TimeUnit::Nanosecond => 1,- TimeUnit::Microsecond => NANOSECONDS / 1_000_000,- TimeUnit::Millisecond => NANOSECONDS / 1_000,- TimeUnit::Second => NANOSECONDS,- };- move |x: Option<i64>| x.map(|x| stride_fn(stride, x * scale, origin) / scale)- }-- Ok(match array {- ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(v, tz_opt)) => {- let apply_stride_fn =- stride_map_fn::<TimestampNanosecondType>(origin, stride, stride_fn);- ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(- apply_stride_fn(*v),- tz_opt.clone(),- ))- }- ColumnarValue::Scalar(ScalarValue::TimestampMicrosecond(v, tz_opt)) => {- let apply_stride_fn =- stride_map_fn::<TimestampMicrosecondType>(origin, stride, stride_fn);- ColumnarValue::Scalar(ScalarValue::TimestampMicrosecond(- apply_stride_fn(*v),- tz_opt.clone(),- ))- }- ColumnarValue::Scalar(ScalarValue::TimestampMillisecond(v, tz_opt)) => {- let apply_stride_fn =- stride_map_fn::<TimestampMillisecondType>(origin, stride, stride_fn);- ColumnarValue::Scalar(ScalarValue::TimestampMillisecond(- apply_stride_fn(*v),- tz_opt.clone(),- ))- }- ColumnarValue::Scalar(ScalarValue::TimestampSecond(v, tz_opt)) => {- let apply_stride_fn =- stride_map_fn::<TimestampSecondType>(origin, stride, stride_fn);- ColumnarValue::Scalar(ScalarValue::TimestampSecond(- apply_stride_fn(*v),- tz_opt.clone(),- ))- }-- ColumnarValue::Array(array) => {- fn transform_array_with_stride<T>(- origin: i64,- stride: i64,- stride_fn: fn(i64, i64, i64) -> i64,- array: &ArrayRef,- tz_opt: &Option<Arc<str>>,- ) -> Result<ColumnarValue>- where- T: ArrowTimestampType,- {- let array = as_primitive_array::<T>(array)?;- let apply_stride_fn = stride_map_fn::<T>(origin, stride, stride_fn);- let array = array- .iter()- .map(apply_stride_fn)- .collect::<PrimitiveArray<T>>()- .with_timezone_opt(tz_opt.clone());-- Ok(ColumnarValue::Array(Arc::new(array)))- }- match array.data_type() {- DataType::Timestamp(TimeUnit::Nanosecond, tz_opt) => {- transform_array_with_stride::<TimestampNanosecondType>(- origin, stride, stride_fn, array, tz_opt,- )?- }- DataType::Timestamp(TimeUnit::Microsecond, tz_opt) => {- transform_array_with_stride::<TimestampMicrosecondType>(- origin, stride, stride_fn, array, tz_opt,- )?- }- DataType::Timestamp(TimeUnit::Millisecond, tz_opt) => {- transform_array_with_stride::<TimestampMillisecondType>(- origin, stride, stride_fn, array, tz_opt,- )?- }- DataType::Timestamp(TimeUnit::Second, tz_opt) => {- transform_array_with_stride::<TimestampSecondType>(- origin, stride, stride_fn, array, tz_opt,- )?- }- _ => {- return exec_err!(- "DATE_BIN expects source argument to be a TIMESTAMP but got {}",- array.data_type()- )- }- }- }- _ => {- return exec_err!(- "DATE_BIN expects source argument to be a TIMESTAMP scalar or array"- );- }- })-}--macro_rules! extract_date_part {- ($ARRAY: expr, $FN:expr) => {- match $ARRAY.data_type() {- DataType::Date32 => {- let array = as_date32_array($ARRAY)?;- Ok($FN(array)- .map(|v| cast(&(Arc::new(v) as ArrayRef), &DataType::Float64))?)- }- DataType::Date64 => {- let array = as_date64_array($ARRAY)?;- Ok($FN(array)- .map(|v| cast(&(Arc::new(v) as ArrayRef), &DataType::Float64))?)- }- DataType::Timestamp(time_unit, _) => match time_unit {- TimeUnit::Second => {- let array = as_timestamp_second_array($ARRAY)?;- Ok($FN(array)- .map(|v| cast(&(Arc::new(v) as ArrayRef), &DataType::Float64))?)- }- TimeUnit::Millisecond => {- let array = as_timestamp_millisecond_array($ARRAY)?;- Ok($FN(array)- .map(|v| cast(&(Arc::new(v) as ArrayRef), &DataType::Float64))?)- }- TimeUnit::Microsecond => {- let array = as_timestamp_microsecond_array($ARRAY)?;- Ok($FN(array)- .map(|v| cast(&(Arc::new(v) as ArrayRef), &DataType::Float64))?)- }- TimeUnit::Nanosecond => {- let array = as_timestamp_nanosecond_array($ARRAY)?;- Ok($FN(array)- .map(|v| cast(&(Arc::new(v) as ArrayRef), &DataType::Float64))?)- }- },- datatype => exec_err!("Extract does not support datatype {:?}", datatype),- }- };-}--/// DATE_PART SQL function-pub fn date_part(args: &[ColumnarValue]) -> Result<ColumnarValue> {- if args.len() != 2 {- return exec_err!("Expected two arguments in DATE_PART");- }- let (date_part, array) = (&args[0], &args[1]);-- let date_part = if let ColumnarValue::Scalar(ScalarValue::Utf8(Some(v))) = date_part {- v- } else {- return exec_err!("First argument of `DATE_PART` must be non-null scalar Utf8");- };-- let is_scalar = matches!(array, ColumnarValue::Scalar(_));-- let array = match array {- ColumnarValue::Array(array) => array.clone(),- ColumnarValue::Scalar(scalar) => scalar.to_array()?,- };-- let arr = match date_part.to_lowercase().as_str() {- "year" => extract_date_part!(&array, temporal::year),- "quarter" => extract_date_part!(&array, temporal::quarter),- "month" => extract_date_part!(&array, temporal::month),- "week" => extract_date_part!(&array, temporal::week),- "day" => extract_date_part!(&array, temporal::day),- "doy" => extract_date_part!(&array, temporal::doy),- "dow" => extract_date_part!(&array, temporal::num_days_from_sunday),- "hour" => extract_date_part!(&array, temporal::hour),- "minute" => extract_date_part!(&array, temporal::minute),- "second" => extract_date_part!(&array, seconds),- "millisecond" => extract_date_part!(&array, millis),- "microsecond" => extract_date_part!(&array, micros),- "nanosecond" => extract_date_part!(&array, nanos),- "epoch" => extract_date_part!(&array, epoch),- _ => exec_err!("Date part '{date_part}' not supported"),- }?;-- Ok(if is_scalar {- ColumnarValue::Scalar(ScalarValue::try_from_array(&arr?, 0)?)- } else {- ColumnarValue::Array(arr?)- })-}--fn to_ticks<T>(array: &PrimitiveArray<T>, frac: i32) -> Result<Float64Array>-where- T: ArrowTemporalType + ArrowNumericType,- i64: From<T::Native>,-{- let zipped = temporal::second(array)?- .values()- .iter()- .zip(temporal::nanosecond(array)?.values().iter())- .map(|o| ((*o.0 as f64 + (*o.1 as f64) / 1_000_000_000.0) * (frac as f64)))- .collect::<Vec<f64>>();-- Ok(Float64Array::from(zipped))-}--fn seconds<T>(array: &PrimitiveArray<T>) -> Result<Float64Array>-where- T: ArrowTemporalType + ArrowNumericType,- i64: From<T::Native>,-{- to_ticks(array, 1)-}--fn millis<T>(array: &PrimitiveArray<T>) -> Result<Float64Array>-where- T: ArrowTemporalType + ArrowNumericType,- i64: From<T::Native>,-{- to_ticks(array, 1_000)-}--fn micros<T>(array: &PrimitiveArray<T>) -> Result<Float64Array>-where- T: ArrowTemporalType + ArrowNumericType,- i64: From<T::Native>,-{- to_ticks(array, 1_000_000)-}--fn nanos<T>(array: &PrimitiveArray<T>) -> Result<Float64Array>-where- T: ArrowTemporalType + ArrowNumericType,- i64: From<T::Native>,-{- to_ticks(array, 1_000_000_000)-}--fn epoch<T>(array: &PrimitiveArray<T>) -> Result<Float64Array>-where- T: ArrowTemporalType + ArrowNumericType,- i64: From<T::Native>,-{- let b = match array.data_type() {- DataType::Timestamp(tu, _) => {- let scale = match tu {- TimeUnit::Second => 1,- TimeUnit::Millisecond => 1_000,- TimeUnit::Microsecond => 1_000_000,- TimeUnit::Nanosecond => 1_000_000_000,- } as f64;- array.unary(|n| {- let n: i64 = n.into();- n as f64 / scale- })- }- DataType::Date32 => {- let seconds_in_a_day = 86400_f64;- array.unary(|n| {- let n: i64 = n.into();- n as f64 * seconds_in_a_day- })- }- DataType::Date64 => array.unary(|n| {- let n: i64 = n.into();- n as f64 / 1_000_f64- }),- _ => return exec_err!("Can not convert {:?} to epoch", array.data_type()),- };- Ok(b)-}-/// from_unixtime() SQL function implementationpub fn from_unixtime_invoke(args: &[ColumnarValue]) -> Result<ColumnarValue> {if args.len() != 1 {@@ -1170,756 +425,19 @@ pub fn from_unixtime_invoke(args: &[ColumnarValue]) -> Result<ColumnarValue> {mod tests {use std::sync::Arc;- use arrow::array::{as_primitive_array, ArrayRef, Int64Array, IntervalDayTimeArray};- use arrow::compute::kernels::cast_utils::string_to_timestamp_nanos;+ use arrow::array::{ArrayRef, Int64Array};use arrow_array::{Date32Array, Date64Array, Int32Array, Time32MillisecondArray, Time32SecondArray,Time64MicrosecondArray, Time64NanosecondArray, TimestampMicrosecondArray,TimestampMillisecondArray, TimestampNanosecondArray, TimestampSecondArray,UInt32Array,};+ use chrono::{NaiveDateTime, Timelike};+use datafusion_common::ScalarValue;use super::*;- #[test]- fn date_trunc_test() {- let cases = vec![- (- "2020-09-08T13:42:29.190855Z",- "second",- "2020-09-08T13:42:29.000000Z",- ),- (- "2020-09-08T13:42:29.190855Z",- "minute",- "2020-09-08T13:42:00.000000Z",- ),- (- "2020-09-08T13:42:29.190855Z",- "hour",- "2020-09-08T13:00:00.000000Z",- ),- (- "2020-09-08T13:42:29.190855Z",- "day",- "2020-09-08T00:00:00.000000Z",- ),- (- "2020-09-08T13:42:29.190855Z",- "week",- "2020-09-07T00:00:00.000000Z",- ),- (- "2020-09-08T13:42:29.190855Z",- "month",- "2020-09-01T00:00:00.000000Z",- ),- (- "2020-09-08T13:42:29.190855Z",- "year",- "2020-01-01T00:00:00.000000Z",- ),- // week- (- "2021-01-01T13:42:29.190855Z",- "week",- "2020-12-28T00:00:00.000000Z",- ),- (- "2020-01-01T13:42:29.190855Z",- "week",- "2019-12-30T00:00:00.000000Z",- ),- // quarter- (- "2020-01-01T13:42:29.190855Z",- "quarter",- "2020-01-01T00:00:00.000000Z",- ),- (- "2020-02-01T13:42:29.190855Z",- "quarter",- "2020-01-01T00:00:00.000000Z",- ),- (- "2020-03-01T13:42:29.190855Z",- "quarter",- "2020-01-01T00:00:00.000000Z",- ),- (- "2020-04-01T13:42:29.190855Z",- "quarter",- "2020-04-01T00:00:00.000000Z",- ),- (- "2020-08-01T13:42:29.190855Z",- "quarter",- "2020-07-01T00:00:00.000000Z",- ),- (- "2020-11-01T13:42:29.190855Z",- "quarter",- "2020-10-01T00:00:00.000000Z",- ),- (- "2020-12-01T13:42:29.190855Z",- "quarter",- "2020-10-01T00:00:00.000000Z",- ),- ];-- cases.iter().for_each(|(original, granularity, expected)| {- let left = string_to_timestamp_nanos(original).unwrap();- let right = string_to_timestamp_nanos(expected).unwrap();- let result = date_trunc_coarse(granularity, left, None).unwrap();- assert_eq!(result, right, "{original} = {expected}");- });- }-- #[test]- fn test_date_trunc_timezones() {- let cases = vec![- (- vec![- "2020-09-08T00:00:00Z",- "2020-09-08T01:00:00Z",- "2020-09-08T02:00:00Z",- "2020-09-08T03:00:00Z",- "2020-09-08T04:00:00Z",- ],- Some("+00".into()),- vec![- "2020-09-08T00:00:00Z",- "2020-09-08T00:00:00Z",- "2020-09-08T00:00:00Z",- "2020-09-08T00:00:00Z",- "2020-09-08T00:00:00Z",- ],- ),- (- vec![- "2020-09-08T00:00:00Z",- "2020-09-08T01:00:00Z",- "2020-09-08T02:00:00Z",- "2020-09-08T03:00:00Z",- "2020-09-08T04:00:00Z",- ],- None,- vec![- "2020-09-08T00:00:00Z",- "2020-09-08T00:00:00Z",- "2020-09-08T00:00:00Z",- "2020-09-08T00:00:00Z",- "2020-09-08T00:00:00Z",- ],- ),- (- vec![- "2020-09-08T00:00:00Z",- "2020-09-08T01:00:00Z",- "2020-09-08T02:00:00Z",- "2020-09-08T03:00:00Z",- "2020-09-08T04:00:00Z",- ],- Some("-02".into()),- vec![- "2020-09-07T02:00:00Z",- "2020-09-07T02:00:00Z",- "2020-09-08T02:00:00Z",- "2020-09-08T02:00:00Z",- "2020-09-08T02:00:00Z",- ],- ),- (- vec![- "2020-09-08T00:00:00+05",- "2020-09-08T01:00:00+05",- "2020-09-08T02:00:00+05",- "2020-09-08T03:00:00+05",- "2020-09-08T04:00:00+05",- ],- Some("+05".into()),- vec![- "2020-09-08T00:00:00+05",- "2020-09-08T00:00:00+05",- "2020-09-08T00:00:00+05",- "2020-09-08T00:00:00+05",- "2020-09-08T00:00:00+05",- ],- ),- (- vec![- "2020-09-08T00:00:00+08",- "2020-09-08T01:00:00+08",- "2020-09-08T02:00:00+08",- "2020-09-08T03:00:00+08",- "2020-09-08T04:00:00+08",- ],- Some("+08".into()),- vec![- "2020-09-08T00:00:00+08",- "2020-09-08T00:00:00+08",- "2020-09-08T00:00:00+08",- "2020-09-08T00:00:00+08",- "2020-09-08T00:00:00+08",- ],- ),- (- vec![- "2024-10-26T23:00:00Z",- "2024-10-27T00:00:00Z",- "2024-10-27T01:00:00Z",- "2024-10-27T02:00:00Z",- ],- Some("Europe/Berlin".into()),- vec![- "2024-10-27T00:00:00+02",- "2024-10-27T00:00:00+02",- "2024-10-27T00:00:00+02",- "2024-10-27T00:00:00+02",- ],- ),- (- vec![- "2018-02-18T00:00:00Z",- "2018-02-18T01:00:00Z",- "2018-02-18T02:00:00Z",- "2018-02-18T03:00:00Z",- "2018-11-04T01:00:00Z",- "2018-11-04T02:00:00Z",- "2018-11-04T03:00:00Z",- "2018-11-04T04:00:00Z",- ],- Some("America/Sao_Paulo".into()),- vec![- "2018-02-17T00:00:00-02",- "2018-02-17T00:00:00-02",- "2018-02-17T00:00:00-02",- "2018-02-18T00:00:00-03",- "2018-11-03T00:00:00-03",- "2018-11-03T00:00:00-03",- "2018-11-04T01:00:00-02",- "2018-11-04T01:00:00-02",- ],- ),- ];-- cases.iter().for_each(|(original, tz_opt, expected)| {- let input = original- .iter()- .map(|s| Some(string_to_timestamp_nanos(s).unwrap()))- .collect::<TimestampNanosecondArray>()- .with_timezone_opt(tz_opt.clone());- let right = expected- .iter()- .map(|s| Some(string_to_timestamp_nanos(s).unwrap()))- .collect::<TimestampNanosecondArray>()- .with_timezone_opt(tz_opt.clone());- let result = date_trunc(&[- ColumnarValue::Scalar(ScalarValue::from("day")),- ColumnarValue::Array(Arc::new(input)),- ])- .unwrap();- if let ColumnarValue::Array(result) = result {- assert_eq!(- result.data_type(),- &DataType::Timestamp(TimeUnit::Nanosecond, tz_opt.clone())- );- let left = as_primitive_array::<TimestampNanosecondType>(&result);- assert_eq!(left, &right);- } else {- panic!("unexpected column type");- }- });- }-- #[test]- fn test_date_trunc_hour_timezones() {- let cases = vec![- (- vec![- "2020-09-08T00:30:00Z",- "2020-09-08T01:30:00Z",- "2020-09-08T02:30:00Z",- "2020-09-08T03:30:00Z",- "2020-09-08T04:30:00Z",- ],- Some("+00".into()),- vec![- "2020-09-08T00:00:00Z",- "2020-09-08T01:00:00Z",- "2020-09-08T02:00:00Z",- "2020-09-08T03:00:00Z",- "2020-09-08T04:00:00Z",- ],- ),- (- vec![- "2020-09-08T00:30:00Z",- "2020-09-08T01:30:00Z",- "2020-09-08T02:30:00Z",- "2020-09-08T03:30:00Z",- "2020-09-08T04:30:00Z",- ],- None,- vec![- "2020-09-08T00:00:00Z",- "2020-09-08T01:00:00Z",- "2020-09-08T02:00:00Z",- "2020-09-08T03:00:00Z",- "2020-09-08T04:00:00Z",- ],- ),- (- vec![- "2020-09-08T00:30:00Z",- "2020-09-08T01:30:00Z",- "2020-09-08T02:30:00Z",- "2020-09-08T03:30:00Z",- "2020-09-08T04:30:00Z",- ],- Some("-02".into()),- vec![- "2020-09-08T00:00:00Z",- "2020-09-08T01:00:00Z",- "2020-09-08T02:00:00Z",- "2020-09-08T03:00:00Z",- "2020-09-08T04:00:00Z",- ],- ),- (- vec![- "2020-09-08T00:30:00+05",- "2020-09-08T01:30:00+05",- "2020-09-08T02:30:00+05",- "2020-09-08T03:30:00+05",- "2020-09-08T04:30:00+05",- ],- Some("+05".into()),- vec![- "2020-09-08T00:00:00+05",- "2020-09-08T01:00:00+05",- "2020-09-08T02:00:00+05",- "2020-09-08T03:00:00+05",- "2020-09-08T04:00:00+05",- ],- ),- (- vec![- "2020-09-08T00:30:00+08",- "2020-09-08T01:30:00+08",- "2020-09-08T02:30:00+08",- "2020-09-08T03:30:00+08",- "2020-09-08T04:30:00+08",- ],- Some("+08".into()),- vec![- "2020-09-08T00:00:00+08",- "2020-09-08T01:00:00+08",- "2020-09-08T02:00:00+08",- "2020-09-08T03:00:00+08",- "2020-09-08T04:00:00+08",- ],- ),- (- vec![- "2024-10-26T23:30:00Z",- "2024-10-27T00:30:00Z",- "2024-10-27T01:30:00Z",- "2024-10-27T02:30:00Z",- ],- Some("Europe/Berlin".into()),- vec![- "2024-10-27T01:00:00+02",- "2024-10-27T02:00:00+02",- "2024-10-27T02:00:00+01",- "2024-10-27T03:00:00+01",- ],- ),- (- vec![- "2018-02-18T00:30:00Z",- "2018-02-18T01:30:00Z",- "2018-02-18T02:30:00Z",- "2018-02-18T03:30:00Z",- "2018-11-04T01:00:00Z",- "2018-11-04T02:00:00Z",- "2018-11-04T03:00:00Z",- "2018-11-04T04:00:00Z",- ],- Some("America/Sao_Paulo".into()),- vec![- "2018-02-17T22:00:00-02",- "2018-02-17T23:00:00-02",- "2018-02-17T23:00:00-03",- "2018-02-18T00:00:00-03",- "2018-11-03T22:00:00-03",- "2018-11-03T23:00:00-03",- "2018-11-04T01:00:00-02",- "2018-11-04T02:00:00-02",- ],- ),- ];-- cases.iter().for_each(|(original, tz_opt, expected)| {- let input = original- .iter()- .map(|s| Some(string_to_timestamp_nanos(s).unwrap()))- .collect::<TimestampNanosecondArray>()- .with_timezone_opt(tz_opt.clone());- let right = expected- .iter()- .map(|s| Some(string_to_timestamp_nanos(s).unwrap()))- .collect::<TimestampNanosecondArray>()- .with_timezone_opt(tz_opt.clone());- let result = date_trunc(&[- ColumnarValue::Scalar(ScalarValue::from("hour")),- ColumnarValue::Array(Arc::new(input)),- ])- .unwrap();- if let ColumnarValue::Array(result) = result {- assert_eq!(- result.data_type(),- &DataType::Timestamp(TimeUnit::Nanosecond, tz_opt.clone())- );- let left = as_primitive_array::<TimestampNanosecondType>(&result);- assert_eq!(left, &right);- } else {- panic!("unexpected column type");- }- });- }-- #[test]- fn test_date_bin_single() {- let cases = vec![- (- (- TimeDelta::try_minutes(15).unwrap(),- "2004-04-09T02:03:04.123456789Z",- "2001-01-01T00:00:00",- ),- "2004-04-09T02:00:00Z",- ),- (- (- TimeDelta::try_minutes(15).unwrap(),- "2004-04-09T02:03:04.123456789Z",- "2001-01-01T00:02:30",- ),- "2004-04-09T02:02:30Z",- ),- (- (- TimeDelta::try_minutes(15).unwrap(),- "2004-04-09T02:03:04.123456789Z",- "2005-01-01T00:02:30",- ),- "2004-04-09T02:02:30Z",- ),- (- (- TimeDelta::try_hours(1).unwrap(),- "2004-04-09T02:03:04.123456789Z",- "2001-01-01T00:00:00",- ),- "2004-04-09T02:00:00Z",- ),- (- (- TimeDelta::try_seconds(10).unwrap(),- "2004-04-09T02:03:11.123456789Z",- "2001-01-01T00:00:00",- ),- "2004-04-09T02:03:10Z",- ),- ];-- cases- .iter()- .for_each(|((stride, source, origin), expected)| {- let stride1 = stride.num_nanoseconds().unwrap();- let source1 = string_to_timestamp_nanos(source).unwrap();- let origin1 = string_to_timestamp_nanos(origin).unwrap();-- let expected1 = string_to_timestamp_nanos(expected).unwrap();- let result = date_bin_nanos_interval(stride1, source1, origin1);- assert_eq!(result, expected1, "{source} = {expected}");- })- }-- #[test]- fn test_date_bin() {- let res = date_bin(&[- ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(1))),- ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),- ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),- ]);- assert!(res.is_ok());-- let timestamps = Arc::new((1..6).map(Some).collect::<TimestampNanosecondArray>());- let res = date_bin(&[- ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(1))),- ColumnarValue::Array(timestamps),- ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),- ]);- assert!(res.is_ok());-- let res = date_bin(&[- ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(1))),- ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),- ]);- assert!(res.is_ok());-- // stride supports month-day-nano- let res = date_bin(&[- ColumnarValue::Scalar(ScalarValue::IntervalMonthDayNano(Some(1))),- ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),- ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),- ]);- assert!(res.is_ok());-- //- // Fallible test cases- //-- // invalid number of arguments- let res =- date_bin(&[ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(1)))]);- assert_eq!(- res.err().unwrap().strip_backtrace(),- "Execution error: DATE_BIN expected two or three arguments"- );-- // stride: invalid type- let res = date_bin(&[- ColumnarValue::Scalar(ScalarValue::IntervalYearMonth(Some(1))),- ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),- ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),- ]);- assert_eq!(- res.err().unwrap().strip_backtrace(),- "Execution error: DATE_BIN expects stride argument to be an INTERVAL but got Interval(YearMonth)"- );-- // stride: invalid value- let res = date_bin(&[- ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(0))),- ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),- ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),- ]);- assert_eq!(- res.err().unwrap().strip_backtrace(),- "Execution error: DATE_BIN stride must be non-zero"- );-- // stride: overflow of day-time interval- let res = date_bin(&[- ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(i64::MAX))),- ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),- ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),- ]);- assert_eq!(- res.err().unwrap().strip_backtrace(),- "Execution error: DATE_BIN stride argument is too large"- );-- // stride: overflow of month-day-nano interval- let res = date_bin(&[- ColumnarValue::Scalar(ScalarValue::new_interval_mdn(0, i32::MAX, 1)),- ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),- ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),- ]);- assert_eq!(- res.err().unwrap().strip_backtrace(),- "Execution error: DATE_BIN stride argument is too large"- );-- // stride: month intervals- let res = date_bin(&[- ColumnarValue::Scalar(ScalarValue::new_interval_mdn(1, 1, 1)),- ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),- ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),- ]);- assert_eq!(- res.err().unwrap().strip_backtrace(),- "This feature is not implemented: DATE_BIN stride does not support combination of month, day and nanosecond intervals"- );-- // origin: invalid type- let res = date_bin(&[- ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(1))),- ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),- ColumnarValue::Scalar(ScalarValue::TimestampMicrosecond(Some(1), None)),- ]);- assert_eq!(- res.err().unwrap().strip_backtrace(),- "Execution error: DATE_BIN expects origin argument to be a TIMESTAMP with nanosececond precision but got Timestamp(Microsecond, None)"- );-- let res = date_bin(&[- ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(1))),- ColumnarValue::Scalar(ScalarValue::TimestampMicrosecond(Some(1), None)),- ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),- ]);- assert!(res.is_ok());-- // unsupported array type for stride- let intervals = Arc::new((1..6).map(Some).collect::<IntervalDayTimeArray>());- let res = date_bin(&[- ColumnarValue::Array(intervals),- ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),- ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),- ]);- assert_eq!(- res.err().unwrap().strip_backtrace(),- "This feature is not implemented: DATE_BIN only supports literal values for the stride argument, not arrays"- );-- // unsupported array type for origin- let timestamps = Arc::new((1..6).map(Some).collect::<TimestampNanosecondArray>());- let res = date_bin(&[- ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(1))),- ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(Some(1), None)),- ColumnarValue::Array(timestamps),- ]);- assert_eq!(- res.err().unwrap().strip_backtrace(),- "This feature is not implemented: DATE_BIN only supports literal values for the origin argument, not arrays"- );- }-- #[test]- fn test_date_bin_timezones() {- let cases = vec![- (- vec![- "2020-09-08T00:00:00Z",- "2020-09-08T01:00:00Z",- "2020-09-08T02:00:00Z",- "2020-09-08T03:00:00Z",- "2020-09-08T04:00:00Z",- ],- Some("+00".into()),- "1970-01-01T00:00:00Z",- vec![- "2020-09-08T00:00:00Z",- "2020-09-08T00:00:00Z",- "2020-09-08T00:00:00Z",- "2020-09-08T00:00:00Z",- "2020-09-08T00:00:00Z",- ],- ),- (- vec![- "2020-09-08T00:00:00Z",- "2020-09-08T01:00:00Z",- "2020-09-08T02:00:00Z",- "2020-09-08T03:00:00Z",- "2020-09-08T04:00:00Z",- ],- None,- "1970-01-01T00:00:00Z",- vec![- "2020-09-08T00:00:00Z",- "2020-09-08T00:00:00Z",- "2020-09-08T00:00:00Z",- "2020-09-08T00:00:00Z",- "2020-09-08T00:00:00Z",- ],- ),- (- vec![- "2020-09-08T00:00:00Z",- "2020-09-08T01:00:00Z",- "2020-09-08T02:00:00Z",- "2020-09-08T03:00:00Z",- "2020-09-08T04:00:00Z",- ],- Some("-02".into()),- "1970-01-01T00:00:00Z",- vec![- "2020-09-08T00:00:00Z",- "2020-09-08T00:00:00Z",- "2020-09-08T00:00:00Z",- "2020-09-08T00:00:00Z",- "2020-09-08T00:00:00Z",- ],- ),- (- vec![- "2020-09-08T00:00:00+05",- "2020-09-08T01:00:00+05",- "2020-09-08T02:00:00+05",- "2020-09-08T03:00:00+05",- "2020-09-08T04:00:00+05",- ],- Some("+05".into()),- "1970-01-01T00:00:00+05",- vec![- "2020-09-08T00:00:00+05",- "2020-09-08T00:00:00+05",- "2020-09-08T00:00:00+05",- "2020-09-08T00:00:00+05",- "2020-09-08T00:00:00+05",- ],- ),- (- vec![- "2020-09-08T00:00:00+08",- "2020-09-08T01:00:00+08",- "2020-09-08T02:00:00+08",- "2020-09-08T03:00:00+08",- "2020-09-08T04:00:00+08",- ],- Some("+08".into()),- "1970-01-01T00:00:00+08",- vec![- "2020-09-08T00:00:00+08",- "2020-09-08T00:00:00+08",- "2020-09-08T00:00:00+08",- "2020-09-08T00:00:00+08",- "2020-09-08T00:00:00+08",- ],- ),- ];-- cases- .iter()- .for_each(|(original, tz_opt, origin, expected)| {- let input = original- .iter()- .map(|s| Some(string_to_timestamp_nanos(s).unwrap()))- .collect::<TimestampNanosecondArray>()- .with_timezone_opt(tz_opt.clone());- let right = expected- .iter()- .map(|s| Some(string_to_timestamp_nanos(s).unwrap()))- .collect::<TimestampNanosecondArray>()- .with_timezone_opt(tz_opt.clone());- let result = date_bin(&[- ColumnarValue::Scalar(ScalarValue::new_interval_dt(1, 0)),- ColumnarValue::Array(Arc::new(input)),- ColumnarValue::Scalar(ScalarValue::TimestampNanosecond(- Some(string_to_timestamp_nanos(origin).unwrap()),- tz_opt.clone(),- )),- ])- .unwrap();- if let ColumnarValue::Array(result) = result {- assert_eq!(- result.data_type(),- &DataType::Timestamp(TimeUnit::Nanosecond, tz_opt.clone())- );- let left = as_primitive_array::<TimestampNanosecondType>(&result);- assert_eq!(left, &right);- } else {- panic!("unexpected column type");- }- });- }-#[test]fn test_make_date() {let res = make_date(&[diff --git a/datafusion/physical-expr/src/equivalence/projection.rs b/datafusion/physical-expr/src/equivalence/projection.rsindex 13c3414d66..b8231a74c2 100644--- a/datafusion/physical-expr/src/equivalence/projection.rs+++ b/datafusion/physical-expr/src/equivalence/projection.rs@@ -118,13 +118,13 @@ mod tests {output_schema,};use crate::equivalence::EquivalenceProperties;- use crate::expressions::{col, BinaryExpr, Literal};+ use crate::expressions::{col, BinaryExpr};use crate::functions::create_physical_expr;use crate::PhysicalSortExpr;use arrow::datatypes::{DataType, Field, Schema};use arrow_schema::{SortOptions, TimeUnit};- use datafusion_common::{Result, ScalarValue};+ use datafusion_common::Result;use datafusion_expr::execution_props::ExecutionProps;use datafusion_expr::{BuiltinScalarFunction, Operator};@@ -146,14 +146,6 @@ mod tests {let col_d = &col("d", &schema)?;let col_e = &col("e", &schema)?;let col_ts = &col("ts", &schema)?;- let interval = Arc::new(Literal::new(ScalarValue::IntervalDayTime(Some(2))))- as Arc<dyn PhysicalExpr>;- let date_bin_func = &create_physical_expr(- &BuiltinScalarFunction::DateBin,- &[interval, col_ts.clone()],- &schema,- &ExecutionProps::default(),- )?;let a_plus_b = Arc::new(BinaryExpr::new(col_a.clone(),Operator::Plus,@@ -225,12 +217,9 @@ mod tests {(col_b, "b_new".to_string()),(col_a, "a_new".to_string()),(col_ts, "ts_new".to_string()),- (date_bin_func, "date_bin_res".to_string()),],// expectedvec![- // [date_bin_res ASC]- vec![("date_bin_res", option_asc)],// [ts_new ASC]vec![("ts_new", option_asc)],],@@ -249,18 +238,13 @@ mod tests {(col_b, "b_new".to_string()),(col_a, "a_new".to_string()),(col_ts, "ts_new".to_string()),- (date_bin_func, "date_bin_res".to_string()),],// expectedvec![// [a_new ASC, ts_new ASC]vec![("a_new", option_asc), ("ts_new", option_asc)],- // [a_new ASC, date_bin_res ASC]- vec![("a_new", option_asc), ("date_bin_res", option_asc)],// [b_new ASC, ts_new ASC]vec![("b_new", option_asc), ("ts_new", option_asc)],- // [b_new ASC, date_bin_res ASC]- vec![("b_new", option_asc), ("date_bin_res", option_asc)],],),// ---------- TEST CASE 5 ------------@@ -661,14 +645,6 @@ mod tests {Operator::Plus,col_b.clone(),)) as Arc<dyn PhysicalExpr>;- let interval = Arc::new(Literal::new(ScalarValue::IntervalDayTime(Some(2))))- as Arc<dyn PhysicalExpr>;- let date_bin_ts = &create_physical_expr(- &BuiltinScalarFunction::DateBin,- &[interval, col_ts.clone()],- &schema,- &ExecutionProps::default(),- )?;let round_c = &create_physical_expr(&BuiltinScalarFunction::Round,@@ -686,7 +662,6 @@ mod tests {(col_b, "b_new".to_string()),(col_a, "a_new".to_string()),(col_c, "c_new".to_string()),- (date_bin_ts, "date_bin_res".to_string()),(round_c, "round_c_res".to_string()),];let proj_exprs = proj_exprs@@ -699,7 +674,6 @@ mod tests {let col_a_new = &col("a_new", &output_schema)?;let col_b_new = &col("b_new", &output_schema)?;let col_c_new = &col("c_new", &output_schema)?;- let col_date_bin_res = &col("date_bin_res", &output_schema)?;let col_round_c_res = &col("round_c_res", &output_schema)?;let a_new_plus_b_new = Arc::new(BinaryExpr::new(col_a_new.clone(),@@ -744,7 +718,7 @@ mod tests {// expectedvec![// [a_new ASC, date_bin_res ASC]- vec![(col_a_new, option_asc), (col_date_bin_res, option_asc)],+ vec![(col_a_new, option_asc)],],),// ---------- TEST CASE 4 ------------@@ -761,10 +735,7 @@ mod tests {// expectedvec![// [a_new ASC, date_bin_res ASC]- // Please note that result is not [a_new ASC, date_bin_res ASC, b_new ASC]- // because, datebin_res may not be 1-1 function. Hence without introducing ts- // dependency we cannot guarantee any ordering after date_bin_res column.- vec![(col_a_new, option_asc), (col_date_bin_res, option_asc)],+ vec![(col_a_new, option_asc)],],),// ---------- TEST CASE 5 ------------diff --git a/datafusion/physical-expr/src/functions.rs b/datafusion/physical-expr/src/functions.rsindex 12727920dd..2e1d48eb76 100644--- a/datafusion/physical-expr/src/functions.rs+++ b/datafusion/physical-expr/src/functions.rs@@ -439,9 +439,6 @@ pub fn create_physical_fun(BuiltinScalarFunction::ConcatWithSeparator => Arc::new(|args| {make_scalar_function_inner(string_expressions::concat_ws)(args)}),- BuiltinScalarFunction::DatePart => Arc::new(datetime_expressions::date_part),- BuiltinScalarFunction::DateTrunc => Arc::new(datetime_expressions::date_trunc),- BuiltinScalarFunction::DateBin => Arc::new(datetime_expressions::date_bin),BuiltinScalarFunction::Now => {// bind value for now at plan timeArc::new(datetime_expressions::make_now(diff --git a/datafusion/proto/proto/datafusion.proto b/datafusion/proto/proto/datafusion.protoindex 114b8f4f4d..a5287e2a57 100644--- a/datafusion/proto/proto/datafusion.proto+++ b/datafusion/proto/proto/datafusion.proto@@ -577,8 +577,8 @@ enum ScalarFunction {Chr = 25;Concat = 26;ConcatWithSeparator = 27;- DatePart = 28;- DateTrunc = 29;+ // 28 was DatePart+ // 29 was DateTruncInitCap = 30;Left = 31;Lpad = 32;@@ -617,7 +617,7 @@ enum ScalarFunction {StructFun = 65;FromUnixtime = 66;Atan2 = 67;- DateBin = 68;+ // 68 was DateBinArrowTypeof = 69;CurrentDate = 70;CurrentTime = 71;diff --git a/datafusion/proto/src/generated/pbjson.rs b/datafusion/proto/src/generated/pbjson.rsindex 82edeb01b1..787cd570b2 100644--- a/datafusion/proto/src/generated/pbjson.rs+++ b/datafusion/proto/src/generated/pbjson.rs@@ -22366,8 +22366,6 @@ impl serde::Serialize for ScalarFunction {Self::Chr => "Chr",Self::Concat => "Concat",Self::ConcatWithSeparator => "ConcatWithSeparator",- Self::DatePart => "DatePart",- Self::DateTrunc => "DateTrunc",Self::InitCap => "InitCap",Self::Left => "Left",Self::Lpad => "Lpad",@@ -22400,7 +22398,6 @@ impl serde::Serialize for ScalarFunction {Self::StructFun => "StructFun",Self::FromUnixtime => "FromUnixtime",Self::Atan2 => "Atan2",- Self::DateBin => "DateBin",Self::ArrowTypeof => "ArrowTypeof",Self::CurrentDate => "CurrentDate",Self::CurrentTime => "CurrentTime",@@ -22488,8 +22485,6 @@ impl<'de> serde::Deserialize<'de> for ScalarFunction {"Chr","Concat","ConcatWithSeparator",- "DatePart",- "DateTrunc","InitCap","Left","Lpad",@@ -22522,7 +22517,6 @@ impl<'de> serde::Deserialize<'de> for ScalarFunction {"StructFun","FromUnixtime","Atan2",- "DateBin","ArrowTypeof","CurrentDate","CurrentTime",@@ -22639,8 +22633,6 @@ impl<'de> serde::Deserialize<'de> for ScalarFunction {"Chr" => Ok(ScalarFunction::Chr),"Concat" => Ok(ScalarFunction::Concat),"ConcatWithSeparator" => Ok(ScalarFunction::ConcatWithSeparator),- "DatePart" => Ok(ScalarFunction::DatePart),- "DateTrunc" => Ok(ScalarFunction::DateTrunc),"InitCap" => Ok(ScalarFunction::InitCap),"Left" => Ok(ScalarFunction::Left),"Lpad" => Ok(ScalarFunction::Lpad),@@ -22673,7 +22665,6 @@ impl<'de> serde::Deserialize<'de> for ScalarFunction {"StructFun" => Ok(ScalarFunction::StructFun),"FromUnixtime" => Ok(ScalarFunction::FromUnixtime),"Atan2" => Ok(ScalarFunction::Atan2),- "DateBin" => Ok(ScalarFunction::DateBin),"ArrowTypeof" => Ok(ScalarFunction::ArrowTypeof),"CurrentDate" => Ok(ScalarFunction::CurrentDate),"CurrentTime" => Ok(ScalarFunction::CurrentTime),diff --git a/datafusion/proto/src/generated/prost.rs b/datafusion/proto/src/generated/prost.rsindex c1b81fe4ca..971775e24b 100644--- a/datafusion/proto/src/generated/prost.rs+++ b/datafusion/proto/src/generated/prost.rs@@ -2667,8 +2667,8 @@ pub enum ScalarFunction {Chr = 25,Concat = 26,ConcatWithSeparator = 27,- DatePart = 28,- DateTrunc = 29,+ /// 28 was DatePart+ /// 29 was DateTruncInitCap = 30,Left = 31,Lpad = 32,@@ -2707,7 +2707,7 @@ pub enum ScalarFunction {StructFun = 65,FromUnixtime = 66,Atan2 = 67,- DateBin = 68,+ /// 68 was DateBinArrowTypeof = 69,CurrentDate = 70,CurrentTime = 71,@@ -2810,8 +2810,6 @@ impl ScalarFunction {ScalarFunction::Chr => "Chr",ScalarFunction::Concat => "Concat",ScalarFunction::ConcatWithSeparator => "ConcatWithSeparator",- ScalarFunction::DatePart => "DatePart",- ScalarFunction::DateTrunc => "DateTrunc",ScalarFunction::InitCap => "InitCap",ScalarFunction::Left => "Left",ScalarFunction::Lpad => "Lpad",@@ -2844,7 +2842,6 @@ impl ScalarFunction {ScalarFunction::StructFun => "StructFun",ScalarFunction::FromUnixtime => "FromUnixtime",ScalarFunction::Atan2 => "Atan2",- ScalarFunction::DateBin => "DateBin",ScalarFunction::ArrowTypeof => "ArrowTypeof",ScalarFunction::CurrentDate => "CurrentDate",ScalarFunction::CurrentTime => "CurrentTime",@@ -2926,8 +2923,6 @@ impl ScalarFunction {"Chr" => Some(Self::Chr),"Concat" => Some(Self::Concat),"ConcatWithSeparator" => Some(Self::ConcatWithSeparator),- "DatePart" => Some(Self::DatePart),- "DateTrunc" => Some(Self::DateTrunc),"InitCap" => Some(Self::InitCap),"Left" => Some(Self::Left),"Lpad" => Some(Self::Lpad),@@ -2960,7 +2955,6 @@ impl ScalarFunction {"StructFun" => Some(Self::StructFun),"FromUnixtime" => Some(Self::FromUnixtime),"Atan2" => Some(Self::Atan2),- "DateBin" => Some(Self::DateBin),"ArrowTypeof" => Some(Self::ArrowTypeof),"CurrentDate" => Some(Self::CurrentDate),"CurrentTime" => Some(Self::CurrentTime),diff --git a/datafusion/proto/src/logical_plan/from_proto.rs b/datafusion/proto/src/logical_plan/from_proto.rsindex 8222be1fe9..cc5491b3f2 100644--- a/datafusion/proto/src/logical_plan/from_proto.rs+++ b/datafusion/proto/src/logical_plan/from_proto.rs@@ -53,8 +53,8 @@ use datafusion_expr::{array_repeat, array_replace, array_replace_all, array_replace_n, array_resize,array_slice, array_sort, array_union, arrow_typeof, ascii, asinh, atan, atan2, atanh,bit_length, btrim, cbrt, ceil, character_length, chr, coalesce, concat_expr,- concat_ws_expr, cos, cosh, cot, current_date, current_time, date_bin, date_part,- date_trunc, degrees, digest, ends_with, exp,+ concat_ws_expr, cos, cosh, cot, current_date, current_time, degrees, digest,+ ends_with, exp,expr::{self, InList, Sort, WindowFunction},factorial, find_in_set, floor, from_unixtime, gcd, initcap, iszero, lcm, left,levenshtein, ln, log, log10, log2,@@ -501,9 +501,6 @@ impl From<&protobuf::ScalarFunction> for BuiltinScalarFunction {ScalarFunction::ArrayUnion => Self::ArrayUnion,ScalarFunction::ArrayResize => Self::ArrayResize,ScalarFunction::Array => Self::MakeArray,- ScalarFunction::DatePart => Self::DatePart,- ScalarFunction::DateTrunc => Self::DateTrunc,- ScalarFunction::DateBin => Self::DateBin,ScalarFunction::Md5 => Self::MD5,ScalarFunction::Sha224 => Self::SHA224,ScalarFunction::Sha256 => Self::SHA256,@@ -1580,19 +1577,6 @@ pub fn parse_expr(ScalarFunction::Rtrim => {Ok(rtrim(parse_expr(&args[0], registry, codec)?))}- ScalarFunction::DatePart => Ok(date_part(- parse_expr(&args[0], registry, codec)?,- parse_expr(&args[1], registry, codec)?,- )),- ScalarFunction::DateTrunc => Ok(date_trunc(- parse_expr(&args[0], registry, codec)?,- parse_expr(&args[1], registry, codec)?,- )),- ScalarFunction::DateBin => Ok(date_bin(- parse_expr(&args[0], registry, codec)?,- parse_expr(&args[1], registry, codec)?,- parse_expr(&args[2], registry, codec)?,- )),ScalarFunction::Sha224 => {Ok(sha224(parse_expr(&args[0], registry, codec)?))}diff --git a/datafusion/proto/src/logical_plan/to_proto.rs b/datafusion/proto/src/logical_plan/to_proto.rsindex f28fb54667..77b576dcd3 100644--- a/datafusion/proto/src/logical_plan/to_proto.rs+++ b/datafusion/proto/src/logical_plan/to_proto.rs@@ -1480,9 +1480,6 @@ impl TryFrom<&BuiltinScalarFunction> for protobuf::ScalarFunction {BuiltinScalarFunction::ArrayIntersect => Self::ArrayIntersect,BuiltinScalarFunction::ArrayUnion => Self::ArrayUnion,BuiltinScalarFunction::MakeArray => Self::Array,- BuiltinScalarFunction::DatePart => Self::DatePart,- BuiltinScalarFunction::DateTrunc => Self::DateTrunc,- BuiltinScalarFunction::DateBin => Self::DateBin,BuiltinScalarFunction::MD5 => Self::Md5,BuiltinScalarFunction::SHA224 => Self::Sha224,BuiltinScalarFunction::SHA256 => Self::Sha256,diff --git a/datafusion/sql/src/expr/mod.rs b/datafusion/sql/src/expr/mod.rsindex aa0b619167..4d04d9ebfa 100644--- a/datafusion/sql/src/expr/mod.rs+++ b/datafusion/sql/src/expr/mod.rs@@ -31,7 +31,8 @@ use crate::planner::{ContextProvider, PlannerContext, SqlToRel};use arrow_schema::DataType;use arrow_schema::TimeUnit;use datafusion_common::{- internal_err, not_impl_err, plan_err, Column, DFSchema, Result, ScalarValue,+ internal_datafusion_err, internal_err, not_impl_err, plan_err, Column, DFSchema,+ Result, ScalarValue,};use datafusion_expr::expr::AggregateFunctionDefinition;use datafusion_expr::expr::InList;@@ -169,12 +170,20 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {self.parse_value(value, planner_context.prepare_param_data_types())}SQLExpr::Extract { field, expr } => {- Ok(Expr::ScalarFunction(ScalarFunction::new(- BuiltinScalarFunction::DatePart,- vec![- Expr::Literal(ScalarValue::from(format!("{field}"))),- self.sql_expr_to_logical_expr(*expr, schema, planner_context)?,- ],+ let date_part = self+ .context_provider+ .get_function_meta("date_part")+ .ok_or_else(|| {+ internal_datafusion_err!(+ "Unable to find expected 'date_part' function"+ )+ })?;+ let args = vec![+ Expr::Literal(ScalarValue::from(format!("{field}"))),+ self.sql_expr_to_logical_expr(*expr, schema, planner_context)?,+ ];+ Ok(Expr::ScalarFunction(ScalarFunction::new_udf(+ date_part, args,)))}@@ -889,8 +898,6 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {#[cfg(test)]mod tests {- use super::*;-use std::collections::HashMap;use std::sync::Arc;@@ -904,6 +911,8 @@ mod tests {use crate::TableReference;+ use super::*;+struct TestContextProvider {options: ConfigOptions,tables: HashMap<String, Arc<dyn TableSource>>,diff --git a/datafusion/sql/tests/sql_integration.rs b/datafusion/sql/tests/sql_integration.rsindex 57e2e1ef06..655eb63cc3 100644--- a/datafusion/sql/tests/sql_integration.rs+++ b/datafusion/sql/tests/sql_integration.rs@@ -20,6 +20,7 @@ use std::any::Any;use std::collections::HashMap;use std::{sync::Arc, vec};+use arrow_schema::TimeUnit::Nanosecond;use arrow_schema::*;use sqlparser::dialect::{Dialect, GenericDialect, HiveDialect, MySqlDialect};@@ -2662,12 +2663,17 @@ fn logical_plan_with_dialect_and_options(dialect: &dyn Dialect,options: ParserOptions,) -> Result<LogicalPlan> {- let context = MockContextProvider::default().with_udf(make_udf(- "nullif",- vec![DataType::Int32, DataType::Int32],- DataType::Int32,- ));-+ let context = MockContextProvider::default()+ .with_udf(make_udf(+ "nullif",+ vec![DataType::Int32, DataType::Int32],+ DataType::Int32,+ ))+ .with_udf(make_udf(+ "date_trunc",+ vec![DataType::Utf8, DataType::Timestamp(Nanosecond, None)],+ DataType::Int32,+ ));let planner = SqlToRel::new_with_options(&context, options);let result = DFParser::parse_sql_with_dialect(sql, dialect);let mut ast = result?;@@ -2720,8 +2726,7 @@ impl ScalarUDFImpl for DummyUDF {/// Create logical plan, write with formatter, compare to expected outputfn quick_test(sql: &str, expected: &str) {- let plan = logical_plan(sql).unwrap();- assert_eq!(format!("{plan:?}"), expected);+ quick_test_with_options(sql, expected, ParserOptions::default())}fn quick_test_with_options(sql: &str, expected: &str, options: ParserOptions) {diff --git a/datafusion/sqllogictest/test_files/group_by.slt b/datafusion/sqllogictest/test_files/group_by.sltindex 86c26b398d..9004566fcd 100644--- a/datafusion/sqllogictest/test_files/group_by.slt+++ b/datafusion/sqllogictest/test_files/group_by.slt@@ -4305,6 +4305,38 @@ SELECT extract(month from ts) as months1211+# create an unbounded table that contains name, timestamp.+# where table is ordered by name DESC, ts DESC+statement ok+CREATE UNBOUNDED EXTERNAL TABLE unbounded_csv_with_timestamps2 (+ name VARCHAR,+ ts TIMESTAMP+)+STORED AS CSV+WITH ORDER (name DESC, ts DESC)+LOCATION '../core/tests/data/timestamps.csv'++# result shouldn't have SortExec(sort_exprs=[name DESC, time_chunks DESC]) in the result.+# datafusion should deduce that given ordering: [name DESC, ts DESC] is satisfied+# ordering: [name DESC, date_bin('15 minutes', ts) DESC] is also valid.+query TT+EXPLAIN SELECT name, date_bin('15 minutes', ts) as time_chunks+ FROM unbounded_csv_with_timestamps2+ ORDER BY name DESC, time_chunks DESC+ LIMIT 5;+----+logical_plan+Limit: skip=0, fetch=5+--Sort: unbounded_csv_with_timestamps2.name DESC NULLS FIRST, time_chunks DESC NULLS FIRST, fetch=5+----Projection: unbounded_csv_with_timestamps2.name, date_bin(IntervalMonthDayNano("900000000000"), unbounded_csv_with_timestamps2.ts) AS time_chunks+------TableScan: unbounded_csv_with_timestamps2 projection=[name, ts]+physical_plan+GlobalLimitExec: skip=0, fetch=5+--SortPreservingMergeExec: [name@0 DESC,time_chunks@1 DESC], fetch=5+----ProjectionExec: expr=[name@0 as name, date_bin(900000000000, ts@1) as time_chunks]+------RepartitionExec: partitioning=RoundRobinBatch(8), input_partitions=1+--------StreamingTableExec: partition_sizes=1, projection=[name, ts], infinite_source=true, output_ordering=[name@0 DESC, ts@1 DESC]+statement okdrop table t1