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

   ### Describe the bug
   
   When a `WHERE` clause simplifies to `NULL AND <expr>` (for example `WHERE s 
NOT IN ('val_1', NULL) AND s ILIKE 'val_%'`), `PruningPredicate` rewrites the 
`Literal(Boolean(None))` conjunct to `true` via `unhandled_hook` instead of 
pruning all row groups, so the scan decodes 100% of Parquet row groups and 
filters out every row afterward.
   
   ### To Reproduce
   
   I reproduced this on `main` (`57.0.0-dev`) with default settings:
   
   ```sql
   SET datafusion.execution.target_partitions = 1;
   
   COPY (
     SELECT i AS id, 'val_' || CAST(i % 10 AS VARCHAR) AS s
     FROM generate_series(1, 5000) AS t(i)
   ) TO 't.parquet' STORED AS PARQUET OPTIONS (max_row_group_size 1000);
   
   CREATE EXTERNAL TABLE t STORED AS PARQUET LOCATION 't.parquet';
   
   -- Simplifies to EmptyExec (0/5 row groups scanned)
   EXPLAIN ANALYZE SELECT id FROM t WHERE s NOT IN ('val_1', NULL);
   
   -- Keeps FilterExec: NULL AND s@1 ILIKE val_% and scans 5/5 row groups 
(5,000 rows decoded, 0 output)
   EXPLAIN ANALYZE SELECT id FROM t WHERE s NOT IN ('val_1', NULL) AND s ILIKE 
'val_%';
   ```
   
   ### Expected behavior
   
   `WHERE s NOT IN ('val_1', NULL) AND s ILIKE 'val_%'` cannot match any rows 
and should avoid scanning all five Parquet row groups (either folded to 
`EmptyExec` during optimization or pruned by `PruningPredicate`).
   
   ### Additional context
   
   Two layers interact here:
   
   1. In `EliminateFilter`, `s NOT IN ('val_1', NULL)` simplifies to 
`Literal(Boolean(None))` (#26542), while `Expr::Like` is not listed in 
`is_safe_to_discard`, so `NULL AND s ILIKE 'val_%'` reaches physical planning:
      
https://github.com/apache/datafusion/blob/76aa1b4e636b1f88bd8adb8dcbb3ce488c5d3241/datafusion/optimizer/src/eliminate_filter.rs#L230-L260
   2. In `PruningPredicate::build_predicate_expression`, `is_always_false` 
checks only `ScalarValue::Boolean(Some(false))` and ignores 
`ScalarValue::Boolean(None)`, so `NULL` falls through to `unhandled_hook` 
(`true`):
      
https://github.com/apache/datafusion/blob/76aa1b4e636b1f88bd8adb8dcbb3ce488c5d3241/datafusion/pruning/src/pruning_predicate.rs#L838-L841
      
https://github.com/apache/datafusion/blob/76aa1b4e636b1f88bd8adb8dcbb3ce488c5d3241/datafusion/pruning/src/pruning_predicate.rs#L1772-L1776
   
   Handling `Boolean(None)` inside `PruningPredicate` has two subtleties worth 
maintainer input:
   
   - **Error semantics:** unlike `FALSE AND (1 / x = 1)`, which short circuits 
in physical evaluation, `NULL AND (1 / x = 1)` evaluates the right side and 
raises a division by zero error, so pruning `NULL AND <fallible>` in 
`PruningPredicate` would suppress runtime errors (whereas adding infallible 
expressions like `Expr::Like` to `EliminateFilter::is_safe_to_discard` 
preserves them).
   - **Full match inversion:** `build_inverted_predicate` checks 
`can_be_inverted_for_full_match()` only on the forward predicate, so a `NULL` 
hidden under `NOT(CASE WHEN ... THEN NULL ELSE b END)` in the forward pass 
could become `false` in the inverted predicate and falsely mark a row group 
fully matched unless inversion also rejects `has_filter_semantics_only` on the 
inverse.
   
   I would be happy to contribute a fix once maintainers confirm which layer 
should handle this.
   


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