This is an automated email from the ASF dual-hosted git repository.
github-bot pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/datafusion.git
The following commit(s) were added to refs/heads/main by this push:
new e937cadbcc [fix] Add type coercion from NULL to Interval to make
date_bin more postgres compatible (#20499)
e937cadbcc is described below
commit e937cadbcceff6a42bee2c5fc8d03068fa0eb30c
Author: Lía Adriana <[email protected]>
AuthorDate: Wed Feb 25 09:02:30 2026 +0100
[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.
---
datafusion/expr/src/type_coercion/functions.rs | 2 +-
datafusion/functions/src/datetime/date_bin.rs | 6 ++++++
datafusion/sqllogictest/test_files/datetime/timestamps.slt | 12 ++++++++++++
3 files changed, 19 insertions(+), 1 deletion(-)
diff --git a/datafusion/expr/src/type_coercion/functions.rs
b/datafusion/expr/src/type_coercion/functions.rs
index 90c137de24..fe259fb8c9 100644
--- a/datafusion/expr/src/type_coercion/functions.rs
+++ b/datafusion/expr/src/type_coercion/functions.rs
@@ -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
diff --git a/datafusion/functions/src/datetime/date_bin.rs
b/datafusion/functions/src/datetime/date_bin.rs
index 7f123d214f..c0984c1ea6 100644
--- a/datafusion/functions/src/datetime/date_bin.rs
+++ b/datafusion/functions/src/datetime/date_bin.rs
@@ -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()
diff --git a/datafusion/sqllogictest/test_files/datetime/timestamps.slt
b/datafusion/sqllogictest/test_files/datetime/timestamps.slt
index 8ed32940e8..9526ccebfd 100644
--- a/datafusion/sqllogictest/test_files/datetime/timestamps.slt
+++ b/datafusion/sqllogictest/test_files/datetime/timestamps.slt
@@ -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')
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]