neilconway opened a new issue, #26024:
URL: https://github.com/apache/datafusion/issues/26024
### Describe the bug
The result of `nvl2(x, a, b)` should depend only on `a` and `b`; `x` is only
checked for NULL. The type of `x` currently changes the result type, and some
combinations fail:
- Decimal results are widened to a larger precision and scale, or turned
into strings.
- Timestamp results fail to plan when `x` is an integer, and fail during
optimization when `x` is a string literal.
The equivalent `CASE WHEN x IS NOT NULL THEN a ELSE b END` works in every
case.
### To Reproduce
```sql
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;
SELECT arrow_typeof(CASE WHEN i IS NOT NULL THEN d1 ELSE d2 END) AS
case_type FROM t;
-- Decimal128(5, 2)
SELECT nvl2(i, d1, d2) AS v, arrow_typeof(nvl2(i, d1, d2)) AS v_type FROM t;
-- 1.23 | Decimal128(22, 2)
SELECT nvl2(f, d1, d2) AS v, arrow_typeof(nvl2(f, d1, d2)) AS v_type FROM t;
-- 1.230000000000000 | Decimal128(30, 15)
SELECT nvl2(s, d1, d2) AS v, arrow_typeof(nvl2(s, d1, d2)) AS v_type FROM t;
-- 1.23 | Utf8
SELECT nvl2(i, ts1, ts2) AS v FROM t;
-- Error during planning: Execution error: Function 'nvl2' user-defined
coercion failed with: Internal error: Coercion from Int64 to Timestamp(ns)
failed..
SELECT nvl2('a', ts1, ts2) AS v FROM t;
-- Optimizer rule 'simplify_expressions' failed
-- caused by
-- Failed to cast field 'lit' from Utf8 to Timestamp(ns)
-- caused by
-- Arrow error: Parser error: Error parsing timestamp from 'a': timestamp
must contain at least 10 characters
```
### Expected behavior
`nvl2` returns the same results as the equivalent `CASE` expression:
- The three decimal queries return `1.23` as `Decimal128(5, 2)`.
- The two timestamp queries return `2024-01-01T10:00:00`.
### Additional context
Reproduced on `main` at commit 2b1fcae86, in both release and debug builds.
*This bug report was generated by Claude.*
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]