haohuaijin opened a new issue, #25524:
URL: https://github.com/apache/datafusion/issues/25524

   ### 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:
   
   ```sql
   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:
   
   ```text
   Arrow error: Cast error: Can't cast value 1000 to type Int8
   ```
   
   The lower endpoint reproduces the same problem:
   
   ```sql
   SELECT a
   FROM narrow_column
   WHERE CAST(CAST(a < 0 AS INT) * -1000 AS TINYINT) = z
   ORDER BY a;
   ```
   
   Actual result:
   
   ```text
   Arrow error: Cast error: Can't cast value -1000 to type Int8
   ```
   
   Both queries should return:
   
   ```text
   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:
   
   ```sql
   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:
   
   ```sql
   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.
   
   - #11297 fixed a related endpoint-overflow problem in reverse 
`propagate_constraints` by using safe casts. This reproducer exercises forward 
bound evaluation.
   - #22028 discusses the broader policy of avoiding query failures from 
interval/statistics inference.
   - #25407 and the discussion in #25234 already identify narrowing of inferred 
endpoints as a prerequisite for safe widening. This issue isolates that bug 
with standalone SQL reproducers that do not depend on widening. Fixing it would 
be a prerequisite/follow-up step toward #25407, not completion of that feature.
   
   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.
   


-- 
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]

Reply via email to