morrySnow opened a new pull request, #67730:
URL: https://github.com/apache/doris/pull/67730

   ## Problem
   
   A comparison whose left side is an overflowing `TRY_CAST` can be simplified 
to a non-null boolean constant even though the cast evaluates to `NULL`. For 
example, with a non-null `BIGINT` value of `128`, this predicate must produce 
`NULL`:
   
   ```sql
   TRY_CAST(k AS TINYINT) > 127
   ```
   
   The incorrect simplification also makes `(... > 127) IS NULL` false and can 
filter out rows that should match.
   
   ## Root cause
   
   The type-range comparison simplifier unwraps a cast to inspect the source 
type and tighten the possible numeric range. It then reuses that unwrapped 
child to decide whether an always-true or always-false result can be null. A 
non-nullable child does not capture `TRY_CAST` semantics: a failed conversion 
can introduce `NULL` independently of child nullability.
   
   ## Reproduction
   
   ```sql
   CREATE TABLE t (
       id INT NOT NULL,
       k BIGINT NOT NULL
   )
   DUPLICATE KEY(id)
   DISTRIBUTED BY HASH(id) BUCKETS 1
   PROPERTIES ("replication_num" = "1");
   
   INSERT INTO t VALUES (1, 128), (2, 1);
   
   SELECT id,
          TRY_CAST(k AS TINYINT),
          TRY_CAST(k AS TINYINT) > 127,
          (TRY_CAST(k AS TINYINT) > 127) IS NULL
   FROM t
   ORDER BY id;
   ```
   
   Before this change, the comparison for `id = 1` is simplified to `FALSE`. 
The correct result is `NULL`, and the final `IS NULL` expression is `TRUE`.
   
   ## Fix
   
   Separate the expression used for numeric range inference from the expression 
used for nullability. The cast child remains the range source, but an original 
`TRY_CAST` is retained when producing null-aware boolean constants. Regular 
`CAST` expressions keep their existing child-based normalization.
   
   ## Tests
   
   - Extended `SimplifyComparisonPredicateTest` with out-of-range comparisons 
over a non-nullable source wrapped by `TRY_CAST`, covering both true-or-null 
and false-or-null simplifications.
   - Ran the complete test class: 14 tests passed.
   - Deployed the FE to a local sandbox and verified the SQL reproduction 
returns `NULL` for the overflow comparison and returns the row through an `IS 
NULL` filter.
   


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