hcrosse opened a new issue, #27625:
URL: https://github.com/apache/datafusion/issues/27625
### Describe the bug
`FilterExec::statistics_helper` estimates `col IS NOT NULL OR p` using the
flat 20% `default_filter_selectivity` fallback, even when the `col IS NOT NULL`
branch alone has known non null statistics much higher than 20%.
For a column with 90% non null values, `WHERE s IS NOT NULL OR k > 2` (940
actual rows) gets estimated at `Inexact(200)`, which is 4.5 times lower than
the strictly tighter `WHERE s IS NOT NULL` (`Exact(900)` / `Inexact(900)`).
### To Reproduce
I reproduced this on `main` (`57.0.0-dev`) with default settings:
```sql
SET datafusion.execution.target_partitions = 1;
SET datafusion.explain.show_statistics = true;
COPY (
SELECT i AS id,
CASE WHEN i <= 100 THEN NULL ELSE 's' END AS s,
i % 5 AS k
FROM generate_series(1, 1000) AS t(i)
) TO 't.parquet' STORED AS PARQUET;
CREATE EXTERNAL TABLE t STORED AS PARQUET LOCATION 't.parquet';
-- Disjunction: actual rows = 940, estimated rows = Inexact(200)
EXPLAIN SELECT id FROM t WHERE s IS NOT NULL OR k > 2;
-- Tighter single branch: actual rows = 900, estimated rows = Exact(900)
EXPLAIN SELECT id FROM t WHERE s IS NOT NULL;
```
### Expected behavior
The cardinality estimate for `A OR B` should not fall below the known
selectivity of a supported branch such as `s IS NOT NULL` (for example
combining branch selectivities via inclusion exclusion or clamping at the
maximum branch selectivity).
### Additional context
In `FilterExec::statistics_helper`, `null_check_column` only matches a bare
top level `IS [NOT] NULL`:
https://github.com/apache/datafusion/blob/6135ebb57662fee8cd56b70ff5d253901f9f0aa4/datafusion/physical-plan/src/filter.rs#L364-L374
When `IS NOT NULL` appears inside an `OR`, interval analysis rejects the
predicate and `compute_fallback_selectivity` falls back to
`default_selectivity` (20%):
https://github.com/apache/datafusion/blob/6135ebb57662fee8cd56b70ff5d253901f9f0aa4/datafusion/physical-plan/src/filter.rs#L1230-L1270
This is the `OR` counterpart to #25612 (under #25610), which noted that `OR`
requires separate handling from `AND`. I would be happy to contribute a fix
once maintainers confirm the preferred approach.
--
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]