Skip to content

Narrowing casts of inferred interval endpoints can reject valid queries #25524

Description

@haohuaijin

Describe the bug

A valid query can fail when interval analysis tries to cast an inferred endpoint that is outside the target integer type, even though every value actually evaluated by the query fits that type.

Reproduced on main at ccfe704806, without the integer widening changes proposed in #25407. Both lower- and upper-endpoint overflow can be triggered independently of widening.

To Reproduce

This example requires no external files:

SET datafusion.execution.target_partitions = 1;

CREATE TABLE narrow_column (a INT, z TINYINT)
AS VALUES (1, 0), (2, 0), (3, 0);

-- Upper inferred endpoint is out of range.
SELECT a
FROM narrow_column
WHERE CAST(CAST(a < 0 AS INT) * 1000 AS TINYINT) = z
ORDER BY a;

Actual result:

Arrow error: Cast error: Can't cast value 1000 to type Int8

The lower endpoint reproduces the same problem:

SELECT a
FROM narrow_column
WHERE CAST(CAST(a < 0 AS INT) * -1000 AS TINYINT) = z
ORDER BY a;

Actual result:

Arrow error: Cast error: Can't cast value -1000 to type Int8

Both queries should return:

a
1
2
3

For every input row, a < 0 is false, its integer cast is 0, and multiplying by either 1000 or -1000 still yields 0. The runtime narrowing cast is therefore valid, and its result equals z.

Expected behavior

Out-of-range estimated endpoints should cause interval inference to fall back conservatively rather than reject a valid query. The inferred interval must still cover all possible runtime results.

Actual runtime overflow semantics must remain unchanged. With the same table, these queries really do produce out-of-range values and must still fail:

SELECT CAST(CAST(a > 0 AS INT) * 1000 AS TINYINT)
FROM narrow_column;

SELECT CAST(CAST(a > 0 AS INT) * -1000 AS TINYINT)
FROM narrow_column;

The corresponding TRY_CAST must continue returning NULL for each row:

SELECT TRY_CAST(CAST(a > 0 AS INT) * 1000 AS TINYINT)
FROM narrow_column;

Additional context

The relevant forward-bound path is CastExpr::evaluate_boundsInterval::cast_to. Interval analysis estimates [0, 1000] or [-1000, 0]; converting these endpoints with the runtime cast options raises an error before valid rows can be returned.

Both valid-query reproducers were verified to fail as SLT tests on the baseline and pass with an isolated conservative numeric-endpoint conversion fix. The runtime CAST error and TRY_CAST NULL checks also pass with that fix.

Activity

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions