Skip to content

Apache DataFusion SQL Query Engine

fix: nvl2 result type should not depend on its first argument (#26498)

Yu-Chuan Hunga9129e758dea
diff --git a/datafusion/functions/src/core/nvl2.rs b/datafusion/functions/src/core/nvl2.rs
index d68296d9b8..671e1d74f5 100644
--- a/datafusion/functions/src/core/nvl2.rs
+++ b/datafusion/functions/src/core/nvl2.rs
@@ -16,7 +16,7 @@
// under the License.
use arrow::datatypes::{DataType, Field, FieldRef};
-use datafusion_common::{Result, internal_err, utils::take_function_args};
+use datafusion_common::{Result, internal_err, plan_err, utils::take_function_args};
use datafusion_expr::{
ColumnarValue, Documentation, Expr, ReturnFieldArgs, ScalarFunctionArgs,
ScalarUDFImpl, Signature, Volatility,
@@ -125,20 +125,15 @@ impl ScalarUDFImpl for NVL2Func {
fn coerce_types(&self, arg_types: &[DataType]) -> Result<Vec<DataType>> {
let [tested, if_non_null, if_null] = take_function_args(self.name(), arg_types)?;
- let new_type =
- [if_non_null, if_null]
- .iter()
- .try_fold(tested.clone(), |acc, x| {
- // `type_union_coercion` finds a loose common type; the actual
- // coercion is done by `maybe_data_types`.
- let coerced_type = type_union_coercion(&acc, x);
- if let Some(coerced_type) = coerced_type {
- Ok(coerced_type)
- } else {
- internal_err!("Coercion from {acc} to {x} failed.")
- }
- })?;
- Ok(vec![new_type; arg_types.len()])
+ // `type_union_coercion` is order-sensitive (e.g. for timezones); start from
+ // `if_null` to match `CASE`, which coerces starting from the ELSE type.
+ let Some(new_type) = type_union_coercion(if_null, if_non_null) else {
+ return plan_err!(
+ "nvl2 cannot coerce {if_non_null} and {if_null} to a common type"
+ );
+ };
+
+ Ok(vec![tested.clone(), new_type.clone(), new_type])
}
fn documentation(&self) -> Option<&Documentation> {
diff --git a/datafusion/sqllogictest/test_files/nvl2.slt b/datafusion/sqllogictest/test_files/nvl2.slt
index 0dcb82bdf2..75c007b801 100644
--- a/datafusion/sqllogictest/test_files/nvl2.slt
+++ b/datafusion/sqllogictest/test_files/nvl2.slt
@@ -118,3 +118,71 @@ query ?
SELECT NVL2(NULL, NULL, NULL);
----
NULL
+
+
+# issue 26024 - nvl2 result type should not depend on its first argument
+statement ok
+CREATE TABLE t AS SELECT
+ 1 AS i, 1.5 AS f, 'a' AS s,
+ CAST(1.23 AS DECIMAL(5,2)) AS d1, CAST(4.56 AS DECIMAL(5,2)) AS d2,
+ TIMESTAMP '2024-01-01 10:00:00' AS ts1, TIMESTAMP '2024-01-02 10:00:00' AS ts2;
+
+query T
+SELECT arrow_typeof(CASE WHEN i IS NOT NULL THEN d1 ELSE d2 END) AS case_type FROM t;
+----
+Decimal128(5, 2)
+
+query RT
+SELECT nvl2(i, d1, d2) AS v, arrow_typeof(nvl2(i, d1, d2)) AS v_type FROM t;
+----
+1.23 Decimal128(5, 2)
+
+query RT
+SELECT nvl2(f, d1, d2) AS v, arrow_typeof(nvl2(f, d1, d2)) AS v_type FROM t;
+----
+1.23 Decimal128(5, 2)
+
+query RT
+SELECT nvl2(s, d1, d2) AS v, arrow_typeof(nvl2(s, d1, d2)) AS v_type FROM t;
+----
+1.23 Decimal128(5, 2)
+
+query PT
+SELECT nvl2(i, ts1, ts2) AS v, arrow_typeof(nvl2(i, ts1, ts2)) AS v_type FROM t;
+----
+2024-01-01T10:00:00 Timestamp(ns)
+
+query PT
+SELECT nvl2('a', ts1, ts2) AS v, arrow_typeof(nvl2('a', ts1, ts2)) AS v_type FROM t;
+----
+2024-01-01T10:00:00 Timestamp(ns)
+
+statement ok
+DROP TABLE t;
+
+# nvl2 should pick the same timezone as the equivalent CASE expression
+# when its branches have different timezones
+statement ok
+CREATE TABLE t_tz AS
+SELECT i, a, arrow_cast(a, 'Timestamp(Nanosecond, Some("+05:00"))') AS b
+FROM (SELECT 1 AS i,
+ arrow_cast(TIMESTAMP '2024-01-01 22:00:00', 'Timestamp(Nanosecond, Some("+00:00"))') AS a);
+
+query PTD
+SELECT CASE WHEN i IS NOT NULL THEN a ELSE b END AS v,
+ arrow_typeof(CASE WHEN i IS NOT NULL THEN a ELSE b END) AS v_type,
+ CAST(CASE WHEN i IS NOT NULL THEN a ELSE b END AS DATE) AS v_date
+FROM t_tz;
+----
+2024-01-02T03:00:00+05:00 Timestamp(ns, "+05:00") 2024-01-02
+
+query PTD
+SELECT nvl2(i, a, b) AS v,
+ arrow_typeof(nvl2(i, a, b)) AS v_type,
+ CAST(nvl2(i, a, b) AS DATE) AS v_date
+FROM t_tz;
+----
+2024-01-02T03:00:00+05:00 Timestamp(ns, "+05:00") 2024-01-02
+
+statement ok
+DROP TABLE t_tz;