Skip to content

nvl2 result type depends on the type of its first argument #26024

Description

@neilconway

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

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 2b1fcae, in both release and debug builds.

This bug report was generated by Claude.

No activity

Activity on this issue will appear here.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions