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

   ### Describe the bug
   
   When `datafusion.execution.parquet.pushdown_filters = true`, 
`FileScanConfig::statistics()` ignores pushed down filters and reports the 
unfiltered file row count as `Inexact(N)`. Once `FilterPushdown` absorbs 
`FilterExec` into `DataSourceExec`, the scan reports 100% of the table rows.
   
   As a result, a tighter predicate that pushes completely into 
`DataSourceExec` can get a much higher row estimate than a looser predicate 
that stays in `FilterExec`, which can skew downstream join planning and build 
side selection.
   
   ### To Reproduce
   
   I reproduced this on `main` (`57.0.0-dev`):
   
   ```sql
   SET datafusion.execution.target_partitions = 1;
   SET datafusion.execution.parquet.pushdown_filters = true;
   SET datafusion.explain.show_statistics = true;
   
   COPY (
     SELECT i AS id,
            i % 10 AS k,
            CASE WHEN i % 2 = 0 THEN NULL ELSE named_struct('a', i) END AS st
     FROM generate_series(1, 1000) AS t(i)
   ) TO 't.parquet' STORED AS PARQUET;
   
   CREATE EXTERNAL TABLE t STORED AS PARQUET LOCATION 't.parquet';
   
   -- Looser predicate: FilterExec remains in the plan
   -- Actual rows: 550, estimated rows: Inexact(200)
   EXPLAIN SELECT id FROM t WHERE k = 1 OR st IS NULL;
   
   -- Tighter predicate: absorbed into DataSourceExec
   -- Actual rows: 100, estimated rows: Inexact(1000)
   EXPLAIN SELECT id FROM t WHERE k = 1;
   ```
   
   With `WHERE k = 1`, `FilterExec` is absorbed into `DataSourceExec` and the 
reported cardinality jumps to `Inexact(1000)` (all rows in the file), five 
times higher than the `Inexact(200)` estimate for the strictly looser `WHERE k 
= 1 OR st IS NULL`.
   
   ### Expected behavior
   
   When a filter is pushed down and absorbed into `DataSourceExec` (removing 
`FilterExec`), `DataSourceExec` statistics should reflect the selectivity of 
the absorbed filter rather than reporting 100% of the unfiltered table rows.
   
   ### Additional context
   
   `FileScanConfig::statistics()` currently only marks the unfiltered 
statistics as inexact when `self.file_source.filter().is_some()`:
   
   
https://github.com/apache/datafusion/blob/6135ebb57662fee8cd56b70ff5d253901f9f0aa4/datafusion/datasource/src/file_scan_config/mod.rs#L1334-L1342
   
   A couple of design points seem worth confirming before changing this:
   
   1. `self.file_source.filter()` can hold a pruning only filter while 
`FilterExec` remains above `DataSourceExec` (for example when `pushdown_filters 
= false`). Applying selectivity to pruning only filters would double count 
selectivity with the parent `FilterExec`, so estimation should apply only to 
filters actually absorbed into the scan (overlapping with #25760 and #25722).
   2. `FilterExec` selectivity estimation currently lives in 
`datafusion-physical-plan`, above `datafusion-datasource`, so either the shared 
selectivity logic would need to live lower or `FilterPushdown` would need to 
propagate the adjusted statistics when absorbing `FilterExec`.
   
   I would be happy to work on a fix once maintainers confirm the preferred 
direction.
   


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