Skip to content

Commit 915a600

Browse files
committed
feat: Support Date32 to TimestampNanosecond coercion
1 parent 90f0168 commit 915a600

File tree

4 files changed

+34
-7
lines changed

4 files changed

+34
-7
lines changed

datafusion/core/src/physical_plan/functions.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,6 @@ fn signature(fun: &BuiltinScalarFunction) -> Signature {
516516
DataType::Utf8,
517517
DataType::Timestamp(TimeUnit::Nanosecond, Some("UTC".to_owned())),
518518
]),
519-
TypeSignature::Exact(vec![DataType::Utf8, DataType::Date32]),
520519
],
521520
fun.volatility(),
522521
),

datafusion/core/src/physical_plan/type_coercion.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,9 @@ pub fn can_coerce_from(type_into: &DataType, type_from: &DataType) -> bool {
201201
| Float32
202202
| Float64
203203
),
204-
Timestamp(TimeUnit::Nanosecond, None) => matches!(type_from, Timestamp(_, None)),
204+
Timestamp(TimeUnit::Nanosecond, None) => {
205+
matches!(type_from, Timestamp(_, None) | Date32)
206+
}
205207
Utf8 | LargeUtf8 => true,
206208
_ => false,
207209
}

datafusion/core/tests/sql/timestamp.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -846,3 +846,34 @@ async fn test_current_date() -> Result<()> {
846846

847847
Ok(())
848848
}
849+
850+
#[tokio::test]
851+
async fn date_trunc_date32_test() -> Result<()> {
852+
let ctx = SessionContext::new();
853+
854+
let sql = "select date_trunc('month', cast('2023-02-28' as date)) as dt";
855+
let results = execute_to_batches(&ctx, sql).await;
856+
857+
let expected = vec![
858+
"+---------------------+",
859+
"| dt |",
860+
"+---------------------+",
861+
"| 2023-02-01 00:00:00 |",
862+
"+---------------------+",
863+
];
864+
assert_batches_eq!(expected, &results);
865+
866+
let sql = "with w as (select cast('2023-02-28' as date) d) select date_trunc('month', d) as dt from w";
867+
let results = execute_to_batches(&ctx, sql).await;
868+
869+
let expected = vec![
870+
"+---------------------+",
871+
"| dt |",
872+
"+---------------------+",
873+
"| 2023-02-01 00:00:00 |",
874+
"+---------------------+",
875+
];
876+
assert_batches_eq!(expected, &results);
877+
878+
Ok(())
879+
}

datafusion/physical-expr/src/datetime_expressions.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -460,11 +460,6 @@ pub fn date_trunc(args: &[ColumnarValue]) -> Result<ColumnarValue> {
460460
tz_opt.clone(),
461461
))
462462
}
463-
ColumnarValue::Scalar(ScalarValue::Date32(_)) => {
464-
return Err(DataFusionError::Execution(
465-
"`date_trunc` does not accept Date32 type, it's a stub".to_string(),
466-
));
467-
}
468463
ColumnarValue::Array(array) => {
469464
let array = array
470465
.as_any()

0 commit comments

Comments
 (0)