-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix(physical-plan): preserve Exact(0) in FilterExec for null_count, distinct_count and total_byte_size upon empty input #24000
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
asolimando
wants to merge
2
commits into
apache:main
Choose a base branch
from
asolimando:asolimando/filter-exact-empty-counts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+70
−13
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -401,8 +401,11 @@ impl FilterExec { | |
| } | ||
| }; | ||
|
|
||
| let total_byte_size = | ||
| input_total_byte_size.with_estimated_selectivity(selectivity); | ||
| let total_byte_size = if is_infeasible { | ||
| Precision::Exact(0) | ||
| } else { | ||
| input_total_byte_size.with_estimated_selectivity(selectivity) | ||
| }; | ||
|
|
||
| Ok(Statistics { | ||
| num_rows, | ||
|
|
@@ -1032,15 +1035,17 @@ fn interval_bound_to_precision( | |
| } | ||
|
|
||
| /// Caps a row-bounded column statistic (a null count or distinct count) at the | ||
| /// filtered row estimate, since a column cannot have more nulls or distinct | ||
| /// values than it has rows. Known counts are demoted to inexact because the | ||
| /// filtered row count is itself an estimate. | ||
| /// filtered row count, since a column cannot have more nulls or distinct values | ||
| /// than it has rows. Known counts are demoted to inexact because a | ||
| /// filter-derived row bound is normally an estimate, the exception being an | ||
| /// exact zero, which proves the column is empty. | ||
| fn cap_at_rows( | ||
| value: Precision<usize>, | ||
| filtered_num_rows: Precision<usize>, | ||
| ) -> Precision<usize> { | ||
| match filtered_num_rows { | ||
| Precision::Absent => value.to_inexact(), | ||
| Precision::Exact(0) => Precision::Exact(0), | ||
| rows => value.to_inexact().min(&rows), | ||
| } | ||
| } | ||
|
|
@@ -2904,24 +2909,37 @@ mod tests { | |
| #[tokio::test] | ||
| async fn test_filter_statistics_preserves_exactly_empty_input() -> Result<()> { | ||
| // A satisfiable predicate over an exactly empty input: the filter cannot | ||
| // produce rows, so the whole estimate stays exact. | ||
| let schema = Schema::new(vec![Field::new("a", DataType::Int32, true)]); | ||
| // produce rows, so the whole estimate stays exact. Column `b` is not | ||
| // mentioned by the predicate, so its null and distinct counts go through | ||
| // the generic row cap. | ||
| let schema = Schema::new(vec![ | ||
| Field::new("a", DataType::Int32, true), | ||
| Field::new("b", DataType::Int32, true), | ||
| ]); | ||
| let input_stats = Statistics { | ||
| num_rows: Precision::Exact(0), | ||
| total_byte_size: Precision::Exact(0), | ||
| column_statistics: vec![ColumnStatistics { | ||
| null_count: Precision::Exact(0), | ||
| byte_size: Precision::Exact(0), | ||
| ..Default::default() | ||
| }], | ||
| column_statistics: vec![ | ||
| ColumnStatistics { | ||
| null_count: Precision::Exact(0), | ||
| byte_size: Precision::Exact(0), | ||
| ..Default::default() | ||
| }, | ||
| ColumnStatistics { | ||
| null_count: Precision::Exact(3), | ||
| distinct_count: Precision::Exact(7), | ||
| byte_size: Precision::Exact(0), | ||
| ..Default::default() | ||
| }, | ||
| ], | ||
| }; | ||
| let predicate = Arc::new(BinaryExpr::new( | ||
| Arc::new(Column::new("a", 0)), | ||
| Operator::Gt, | ||
| Arc::new(Literal::new(ScalarValue::Int32(Some(5)))), | ||
| )); | ||
|
|
||
| let input = Arc::new(StatisticsExec::new(input_stats, schema)); | ||
| let input = Arc::new(StatisticsExec::new(input_stats, schema.clone())); | ||
| let filter: Arc<dyn ExecutionPlan> = | ||
| Arc::new(FilterExec::try_new(predicate, input)?); | ||
| let statistics = | ||
|
|
@@ -2933,6 +2951,45 @@ mod tests { | |
| statistics.column_statistics[0].byte_size, | ||
| Precision::Exact(0) | ||
| ); | ||
| assert_eq!( | ||
| statistics.column_statistics[1].null_count, | ||
| Precision::Exact(0) | ||
| ); | ||
| assert_eq!( | ||
| statistics.column_statistics[1].distinct_count, | ||
| Precision::Exact(0) | ||
| ); | ||
|
|
||
| // A contradictory predicate (`a = 1 AND a = 2`) proves the same for an | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "proves the same" was a bit confusing for me. Maybe we can rephrase that. |
||
| // input of any size, so the byte size is exact too. | ||
| let input = Arc::new(StatisticsExec::new( | ||
| Statistics { | ||
| num_rows: Precision::Inexact(1000), | ||
| total_byte_size: Precision::Inexact(8000), | ||
| column_statistics: vec![ColumnStatistics::new_unknown(); 2], | ||
| }, | ||
| schema, | ||
| )); | ||
| let contradiction = Arc::new(BinaryExpr::new( | ||
| Arc::new(BinaryExpr::new( | ||
| Arc::new(Column::new("a", 0)), | ||
| Operator::Eq, | ||
| Arc::new(Literal::new(ScalarValue::Int32(Some(1)))), | ||
| )), | ||
| Operator::And, | ||
| Arc::new(BinaryExpr::new( | ||
| Arc::new(Column::new("a", 0)), | ||
| Operator::Eq, | ||
| Arc::new(Literal::new(ScalarValue::Int32(Some(2)))), | ||
| )), | ||
| )); | ||
| let filter: Arc<dyn ExecutionPlan> = | ||
| Arc::new(FilterExec::try_new(contradiction, input)?); | ||
| let statistics = | ||
| StatisticsContext::new().compute(filter.as_ref(), &StatisticsArgs::new())?; | ||
|
|
||
| assert_eq!(statistics.num_rows, Precision::Exact(0)); | ||
| assert_eq!(statistics.total_byte_size, Precision::Exact(0)); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice catch making
total_byte_sizeexact for the infeasible case. I think there's still one path that misses the same invariant though.FilterExeccan also prove an empty result when the input already hasnum_rows: Exact(0). For example, with a satisfiable predicate,num_rowsstaysExact(0)andcap_at_rowsnow correctly produces exact zero row bounded column stats. However, this line still returnsinput_total_byte_size.with_estimated_selectivity(selectivity), sototal_byte_sizeremainsAbsentorInexact.Could we key this off the computed
num_rows == Precision::Exact(0)(or another shared empty output predicate) instead of onlyis_infeasible? That would preserve the exact empty result invariant regardless of how the filter proves the output is empty.Could you also add a regression covering an input with
num_rows: Exact(0)andtotal_byte_size: AbsentorInexact?