From 2ac741739c39e88f9441e116b6b1a2ec09ed612e Mon Sep 17 00:00:00 2001 From: Alex Qyoun-ae <4062971+MazterQyou@users.noreply.github.com> Date: Wed, 30 Aug 2023 21:42:44 +0400 Subject: [PATCH] feat: Support `Date32` to `TimestampNanosecond` coercion --- .../core/src/physical_plan/functions.rs | 1 - .../core/src/physical_plan/type_coercion.rs | 2 +- datafusion/core/tests/sql/timestamp.rs | 31 +++++++++++++++++++ .../physical-expr/src/datetime_expressions.rs | 5 --- 4 files changed, 32 insertions(+), 7 deletions(-) diff --git a/datafusion/core/src/physical_plan/functions.rs b/datafusion/core/src/physical_plan/functions.rs index 3f75e5346069..135ee1277243 100644 --- a/datafusion/core/src/physical_plan/functions.rs +++ b/datafusion/core/src/physical_plan/functions.rs @@ -516,7 +516,6 @@ fn signature(fun: &BuiltinScalarFunction) -> Signature { DataType::Utf8, DataType::Timestamp(TimeUnit::Nanosecond, Some("UTC".to_owned())), ]), - TypeSignature::Exact(vec![DataType::Utf8, DataType::Date32]), ], fun.volatility(), ), diff --git a/datafusion/core/src/physical_plan/type_coercion.rs b/datafusion/core/src/physical_plan/type_coercion.rs index b4133565aebf..b9722ead5358 100644 --- a/datafusion/core/src/physical_plan/type_coercion.rs +++ b/datafusion/core/src/physical_plan/type_coercion.rs @@ -201,7 +201,7 @@ pub fn can_coerce_from(type_into: &DataType, type_from: &DataType) -> bool { | Float32 | Float64 ), - Timestamp(TimeUnit::Nanosecond, None) => matches!(type_from, Timestamp(_, None)), + Timestamp(TimeUnit::Nanosecond, None) => matches!(type_from, Timestamp(_, None) | Date32), Utf8 | LargeUtf8 => true, _ => false, } diff --git a/datafusion/core/tests/sql/timestamp.rs b/datafusion/core/tests/sql/timestamp.rs index 1bde202d4376..912e0b072760 100644 --- a/datafusion/core/tests/sql/timestamp.rs +++ b/datafusion/core/tests/sql/timestamp.rs @@ -846,3 +846,34 @@ async fn test_current_date() -> Result<()> { Ok(()) } + +#[tokio::test] +async fn date_trunc_date32_test() -> Result<()> { + let ctx = SessionContext::new(); + + let sql = "select date_trunc('month', cast('2023-02-28' as date)) as dt"; + let results = execute_to_batches(&ctx, sql).await; + + let expected = vec![ + "+---------------------+", + "| dt |", + "+---------------------+", + "| 2023-02-01 00:00:00 |", + "+---------------------+", + ]; + assert_batches_eq!(expected, &results); + + let sql = "with w as (select cast('2023-02-28' as date) d) select date_trunc('month', d) as dt from w"; + let results = execute_to_batches(&ctx, sql).await; + + let expected = vec![ + "+---------------------+", + "| dt |", + "+---------------------+", + "| 2023-02-01 00:00:00 |", + "+---------------------+", + ]; + assert_batches_eq!(expected, &results); + + Ok(()) +} diff --git a/datafusion/physical-expr/src/datetime_expressions.rs b/datafusion/physical-expr/src/datetime_expressions.rs index b1f785e14e28..04fabf4f63d4 100644 --- a/datafusion/physical-expr/src/datetime_expressions.rs +++ b/datafusion/physical-expr/src/datetime_expressions.rs @@ -460,11 +460,6 @@ pub fn date_trunc(args: &[ColumnarValue]) -> Result { tz_opt.clone(), )) } - ColumnarValue::Scalar(ScalarValue::Date32(_)) => { - return Err(DataFusionError::Execution( - "`date_trunc` does not accept Date32 type, it's a stub".to_string(), - )); - } ColumnarValue::Array(array) => { let array = array .as_any()