hcrosse opened a new issue, #27626:
URL: https://github.com/apache/datafusion/issues/27626
### Describe the bug
`FilterExec` updates unrelated columns' `null_count` by capping at the
filtered row count (`cap_at_rows`) rather than scaling by the filter
selectivity, even though `byte_size` right beside it scales proportionally by
selectivity.
When a nullable join key passes through a selective filter on an unrelated
column, its estimated `null_count` stays near or equal to `filtered_num_rows`.
Downstream `max_distinct_count` computes `num_rows - null_count`, which
collapses toward `0` or `1` and causes `estimate_inner_join_cardinality` to
explode `HashJoinExec` row estimates by orders of magnitude.
### 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 <= 300 THEN NULL ELSE 'k_' || CAST(i AS VARCHAR) END AS
k,
CASE WHEN i % 5 = 0 THEN 'hit' ELSE 'miss' END AS tag
FROM generate_series(1, 1000) AS t(i)
) TO 't1.parquet' STORED AS PARQUET;
COPY (
SELECT i AS id,
CASE WHEN i <= 300 THEN NULL ELSE 'k_' || CAST(i AS VARCHAR) END AS
k,
CASE WHEN i <= 301 THEN NULL ELSE 1 END AS flag
FROM generate_series(1, 1000) AS t(i)
) TO 't2.parquet' STORED AS PARQUET;
CREATE EXTERNAL TABLE t1 STORED AS PARQUET LOCATION 't1.parquet';
CREATE EXTERNAL TABLE t2 STORED AS PARQUET LOCATION 't2.parquet';
-- Less filtered join: actual rows = 140, estimated rows = Inexact(285)
EXPLAIN SELECT t1.id, t2.id
FROM t1 INNER JOIN t2 ON t1.k = t2.k
WHERE t1.tag = 'hit';
-- Adding AND t2.flag IS NULL: actual rows = 0, estimated rows =
Inexact(60200) (211x higher)
EXPLAIN SELECT t1.id, t2.id
FROM t1 INNER JOIN t2 ON t1.k = t2.k
WHERE t1.tag = 'hit' AND t2.flag IS NULL;
```
In the second query, `FilterExec(t1.tag = 'hit')` (200 rows) reports
`k.null_count = Inexact(200)` (`0` non null values), and `FilterExec(t2.flag IS
NULL)` (301 rows) reports `k.null_count = Inexact(300)` (`1` non null value).
`estimate_inner_join_cardinality` then divides `200 * 301` by `1` to get
`Inexact(60200)`.
### Expected behavior
`FilterExec` should scale `null_count` on unrelated columns by the filter
selectivity (capped at `filtered_num_rows`, keeping `Exact(0)` exact), matching
how `byte_size` is scaled. In the query above, `t1.k` scales to `Inexact(60)`
and `t2.k` scales to `Inexact(91)`, keeping the `HashJoinExec` estimate at
`Inexact(286)` instead of `Inexact(60200)`.
### Additional context
The three filter selectivity paths in
`datafusion/physical-plan/src/filter.rs` all call `cap_at_rows` on `null_count`
right beside `scale_byte_size_at_rows`:
- `FilterExec::statistics_helper`:
https://github.com/apache/datafusion/blob/76aa1b4e636b1f88bd8adb8dcbb3ce488c5d3241/datafusion/physical-plan/src/filter.rs#L459-L468
- `null_check_statistics`:
https://github.com/apache/datafusion/blob/76aa1b4e636b1f88bd8adb8dcbb3ce488c5d3241/datafusion/physical-plan/src/filter.rs#L1745-L1749
- `collect_new_statistics`:
https://github.com/apache/datafusion/blob/76aa1b4e636b1f88bd8adb8dcbb3ce488c5d3241/datafusion/physical-plan/src/filter.rs#L1901-L1909
Related to #22716 and #26052 (which tracks `distinct_count` separately).
--
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]