-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Derive filter statistic estimates from the predicate expression #4162
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -38,7 +38,7 @@ use arrow::error::Result as ArrowResult; | |
| use arrow::record_batch::RecordBatch; | ||
| use datafusion_expr::Operator; | ||
| use datafusion_physical_expr::expressions::BinaryExpr; | ||
| use datafusion_physical_expr::split_conjunction; | ||
| use datafusion_physical_expr::{split_conjunction, AnalysisContext}; | ||
|
|
||
| use log::debug; | ||
|
|
||
|
|
@@ -168,9 +168,27 @@ impl ExecutionPlan for FilterExec { | |
| Some(self.metrics.clone_inner()) | ||
| } | ||
|
|
||
| /// The output statistics of a filtering operation are unknown | ||
| /// The output statistics of a filtering operation can be estimated if the | ||
| /// predicate's selectivity value can be determined for the incoming data. | ||
| fn statistics(&self) -> Statistics { | ||
| Statistics::default() | ||
| let input_stats = self.input.statistics(); | ||
| let analysis_ctx = | ||
| AnalysisContext::from_statistics(self.input.schema().as_ref(), &input_stats); | ||
|
|
||
| let predicate_selectivity = self | ||
| .predicate | ||
| .boundaries(&analysis_ctx) | ||
| .and_then(|bounds| bounds.selectivity); | ||
|
|
||
| match predicate_selectivity { | ||
| Some(selectivity) => Statistics { | ||
| num_rows: input_stats | ||
| .num_rows | ||
| .map(|num_rows| (num_rows as f64 * selectivity).ceil() as usize), | ||
| ..Default::default() | ||
| }, | ||
| None => Statistics::default(), | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -282,9 +300,14 @@ mod tests { | |
| use crate::physical_plan::{collect, with_new_children_if_necessary}; | ||
| use crate::prelude::SessionContext; | ||
| use crate::test; | ||
| use crate::test::exec::StatisticsExec; | ||
| use crate::test_util; | ||
| use arrow::datatypes::{DataType, Field, Schema}; | ||
| use datafusion_common::ColumnStatistics; | ||
| use datafusion_common::ScalarValue; | ||
| use datafusion_expr::Operator; | ||
| use std::iter::Iterator; | ||
| use std::sync::Arc; | ||
|
|
||
| #[tokio::test] | ||
| async fn simple_predicate() -> Result<()> { | ||
|
|
@@ -380,4 +403,108 @@ mod tests { | |
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_filter_statistics_basic_expr() -> Result<()> { | ||
| // Table: | ||
| // a: min=1, max=100 | ||
| let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]); | ||
| let input = Arc::new(StatisticsExec::new( | ||
| Statistics { | ||
| num_rows: Some(100), | ||
| column_statistics: Some(vec![ColumnStatistics { | ||
| min_value: Some(ScalarValue::Int32(Some(1))), | ||
| max_value: Some(ScalarValue::Int32(Some(100))), | ||
| ..Default::default() | ||
| }]), | ||
| ..Default::default() | ||
| }, | ||
| schema.clone(), | ||
| )); | ||
|
|
||
| // a <= 25 | ||
| let predicate: Arc<dyn PhysicalExpr> = | ||
| binary(col("a", &schema)?, Operator::LtEq, lit(25i32), &schema)?; | ||
|
|
||
| // WHERE a <= 25 | ||
| let filter: Arc<dyn ExecutionPlan> = | ||
| Arc::new(FilterExec::try_new(predicate, input)?); | ||
|
|
||
| let statistics = filter.statistics(); | ||
| assert_eq!(statistics.num_rows, Some(25)); | ||
|
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. 👨🍳 👌 Very nice |
||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| #[ignore] | ||
| // This test requires propagation of column boundaries from the comparison analysis | ||
| // to the analysis context. This is not yet implemented. | ||
| async fn test_filter_statistics_column_level_basic_expr() -> Result<()> { | ||
|
Contributor
Author
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. @alamb while working on this, I've noticed the initial application of propagation of new column limits. Since we don't have an API to represent changes to the boundaries during an expression's analysis (like This doesn't mean it is completely ineffecttive as is, since we can at least find the cardinality of filter itself and do the local filter <-> table switch in the case below. But I think it might make sense to at least investigate potential ways to deal with this.
Contributor
Author
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. I have a simple solution for this problem (isidentical#5) that essentially implements a much more narrow-scoped version of the
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. I don't understand what about this test requires column level analysis -- your figure has a join in it, but thietest just seems to be the same as |
||
| // Table: | ||
| // a: min=1, max=100 | ||
| let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]); | ||
| let input = Arc::new(StatisticsExec::new( | ||
| Statistics { | ||
| num_rows: Some(100), | ||
| column_statistics: Some(vec![ColumnStatistics { | ||
| min_value: Some(ScalarValue::Int32(Some(1))), | ||
| max_value: Some(ScalarValue::Int32(Some(100))), | ||
| ..Default::default() | ||
| }]), | ||
| ..Default::default() | ||
| }, | ||
| schema.clone(), | ||
| )); | ||
|
|
||
| // a <= 25 | ||
| let predicate: Arc<dyn PhysicalExpr> = | ||
| binary(col("a", &schema)?, Operator::LtEq, lit(25i32), &schema)?; | ||
|
|
||
| // WHERE a <= 25 | ||
| let filter: Arc<dyn ExecutionPlan> = | ||
| Arc::new(FilterExec::try_new(predicate, input)?); | ||
|
|
||
| let statistics = filter.statistics(); | ||
| assert_eq!(statistics.num_rows, Some(25)); | ||
| assert_eq!( | ||
| statistics.column_statistics, | ||
| Some(vec![ColumnStatistics { | ||
| min_value: Some(ScalarValue::Int32(Some(1))), | ||
| max_value: Some(ScalarValue::Int32(Some(25))), | ||
| ..Default::default() | ||
| }]) | ||
| ); | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| async fn test_filter_statistics_when_input_stats_missing() -> Result<()> { | ||
| // Table: | ||
| // a: min=???, max=??? (missing) | ||
| let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]); | ||
| let input = Arc::new(StatisticsExec::new( | ||
| Statistics { | ||
| column_statistics: Some(vec![ColumnStatistics { | ||
| ..Default::default() | ||
| }]), | ||
| ..Default::default() | ||
| }, | ||
| schema.clone(), | ||
| )); | ||
|
|
||
| // a <= 25 | ||
| let predicate: Arc<dyn PhysicalExpr> = | ||
| binary(col("a", &schema)?, Operator::LtEq, lit(25i32), &schema)?; | ||
|
|
||
| // WHERE a <= 25 | ||
| let filter: Arc<dyn ExecutionPlan> = | ||
| Arc::new(FilterExec::try_new(predicate, input)?); | ||
|
|
||
| let statistics = filter.statistics(); | ||
| assert_eq!(statistics.num_rows, None); | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -238,8 +238,9 @@ async fn sql_filter() -> Result<()> { | |
| .await | ||
| .unwrap(); | ||
|
|
||
| // with a filtering condition we loose all knowledge about the statistics | ||
| assert_eq!(Statistics::default(), physical_plan.statistics()); | ||
| let stats = physical_plan.statistics(); | ||
| assert!(!stats.is_exact); | ||
| assert_eq!(stats.num_rows, Some(1)); | ||
|
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. Nice |
||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||

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.
I wonder if we should explicitly list out
is_exact: falsehere?Default::default()gets the same result but maybe being explicit would be better 🤔