Skip to content

Commit

Permalink
feat: Support Date32 to TimestampNanosecond coercion
Browse files Browse the repository at this point in the history
  • Loading branch information
MazterQyou committed Aug 30, 2023
1 parent 90f0168 commit 332ba8b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
1 change: 0 additions & 1 deletion datafusion/core/src/physical_plan/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
),
Expand Down
4 changes: 3 additions & 1 deletion datafusion/core/src/physical_plan/type_coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,9 @@ 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,
}
Expand Down
31 changes: 31 additions & 0 deletions datafusion/core/tests/sql/timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
5 changes: 0 additions & 5 deletions datafusion/physical-expr/src/datetime_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,11 +460,6 @@ pub fn date_trunc(args: &[ColumnarValue]) -> Result<ColumnarValue> {
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()
Expand Down

0 comments on commit 332ba8b

Please sign in to comment.