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:
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_bounds → Interval::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.
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
mainatccfe704806, 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:
Actual result:
The lower endpoint reproduces the same problem:
Actual result:
Both queries should return:
For every input row,
a < 0is false, its integer cast is0, and multiplying by either1000or-1000still yields0. The runtime narrowing cast is therefore valid, and its result equalsz.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:
The corresponding
TRY_CASTmust continue returning NULL for each row:Additional context
The relevant forward-bound path is
CastExpr::evaluate_bounds→Interval::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.propagate_constraintsby using safe casts. This reproducer exercises forward bound evaluation.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.