[fix] Add type coercion from NULL to Interval to make date_bin more postgres compatible (#20499)

## Which issue does this PR close?

<!--
We generally require a GitHub issue to be filed for all bug fixes and
enhancements and this helps us generate change logs for our releases.
You can link an issue to this PR using the GitHub syntax. For example
`Closes #123` indicates that this PR will close issue #123.
-->

- Closes https://github.com/apache/datafusion/issues/20502

## Rationale for this change

The following query is failing with the following error:

`SELECT date_bin(NULL, TIMESTAMP '2023-01-01 12:30:00', TIMESTAMP
'2023-01-01 12:00:00')
`

`Error: Error during planning: Failed to coerce arguments to satisfy a
call to 'date_bin' function: coercion from Null, Timestamp(ns),
Timestamp(ns) to the signature OneOf([....])`

## What changes are included in this PR?

Fix `date_bin(NULL, ...)` to return `NULL` instead of a planning error
by allowing Nulls to coerce to Interva.

## Are these changes tested?

I added a sqllogictest case to verify the query executes and returns
`NULL`.

## Are there any user-facing changes?

<!--
If there are user-facing changes then we may require documentation to be
updated before approving the PR.
-->

<!--
If there are any breaking changes to public APIs, please add the `api
change` label.
-->

Yes, previously `date_bin(NULL, ...) `returned a planning error. It now
returns NULL.
This commit is contained in:
Lía Adriana
2026-02-25 09:02:30 +01:00
committed by GitHub
parent d75fcb83e3
commit e937cadbcc
3 changed files with 19 additions and 1 deletions
@@ -883,7 +883,7 @@ fn coerced_from<'a>(
Timestamp(TimeUnit::Nanosecond, None),
Null | Timestamp(_, None) | Date32 | Utf8 | LargeUtf8,
) => Some(type_into.clone()),
(Interval(_), Utf8 | LargeUtf8) => Some(type_into.clone()),
(Interval(_), Null | Utf8 | LargeUtf8) => Some(type_into.clone()),
// We can go into a Utf8View from a Utf8 or LargeUtf8
(Utf8View, Utf8 | LargeUtf8 | Null) => Some(type_into.clone()),
// Any type can be coerced into strings
@@ -421,6 +421,12 @@ fn date_bin_impl(
origin: &ColumnarValue,
) -> Result<ColumnarValue> {
let stride = match stride {
ColumnarValue::Scalar(s) if s.is_null() => {
// NULL stride -> NULL result (standard SQL NULL propagation)
return Ok(ColumnarValue::Scalar(ScalarValue::try_from(
array.data_type(),
)?));
}
ColumnarValue::Scalar(ScalarValue::IntervalDayTime(Some(v))) => {
let (days, ms) = IntervalDayTimeType::to_parts(*v);
let nanos = (TimeDelta::try_days(days as i64).unwrap()
@@ -771,6 +771,18 @@ select to_timestamp_seconds(cast (1 as int));
## test date_bin function
##########
# NULL stride should return NULL, not a planning error
query P
SELECT date_bin(NULL, TIMESTAMP '2023-01-01 12:30:00', TIMESTAMP '2023-01-01 12:00:00')
----
NULL
# NULL stride should return NULL, not a planning error
query P
SELECT date_bin(NULL, TIMESTAMP '2023-01-01 12:30:00')
----
NULL
# invalid second arg type
query error
SELECT DATE_BIN(INTERVAL '0 second', 25, TIMESTAMP '1970-01-01T00:00:00Z')