Repository navigation
Description
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):
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():
datafusion/datafusion/datasource/src/file_scan_config/mod.rs
Lines 1334 to 1342 in 6135ebb
| pub fn statistics(&self) -> Statistics { | |
| let filter_may_change_row_count = self.file_source.filter().is_some() | |
| && self.statistics.num_rows != Precision::Exact(0); | |
| if filter_may_change_row_count { | |
| self.statistics.clone().to_inexact() | |
| } else { | |
| self.statistics.clone() | |
| } | |
| } |
A couple of design points seem worth confirming before changing this:
self.file_source.filter()can hold a pruning only filter whileFilterExecremains aboveDataSourceExec(for example whenpushdown_filters = false). Applying selectivity to pruning only filters would double count selectivity with the parentFilterExec, so estimation should apply only to filters actually absorbed into the scan (overlapping with Prototype: physical filter API (PhysicalFilter / FilterConjunct) with optional conjuncts #25760 and feat: keep optional filters pruning-only in the Parquet post-scan path #25722).FilterExecselectivity estimation currently lives indatafusion-physical-plan, abovedatafusion-datasource, so either the shared selectivity logic would need to live lower orFilterPushdownwould need to propagate the adjusted statistics when absorbingFilterExec.
I would be happy to work on a fix once maintainers confirm the preferred direction.
Activity