Repository navigation
[minior fix]: adjust the projection statistics #7428
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 | ||||
|---|---|---|---|---|---|---|
|
|
@@ -336,6 +336,7 @@ impl ExecutionPlan for ProjectionExec { | |||||
| stats_projection( | ||||||
| self.input.statistics(), | ||||||
| self.expr.iter().map(|(e, _)| Arc::clone(e)), | ||||||
| self.schema.clone(), | ||||||
| ) | ||||||
| } | ||||||
| } | ||||||
|
|
@@ -395,9 +396,13 @@ fn get_field_metadata( | |||||
| fn stats_projection( | ||||||
| stats: Statistics, | ||||||
| exprs: impl Iterator<Item = Arc<dyn PhysicalExpr>>, | ||||||
| schema: SchemaRef, | ||||||
| ) -> Statistics { | ||||||
| let inner_exprs = exprs.collect::<Vec<_>>(); | ||||||
| let column_statistics = stats.column_statistics.map(|input_col_stats| { | ||||||
| exprs | ||||||
| inner_exprs | ||||||
| .clone() | ||||||
| .into_iter() | ||||||
| .map(|e| { | ||||||
| if let Some(col) = e.as_any().downcast_ref::<Column>() { | ||||||
| input_col_stats[col.index()].clone() | ||||||
|
|
@@ -410,12 +415,35 @@ fn stats_projection( | |||||
| .collect() | ||||||
| }); | ||||||
|
|
||||||
| Statistics { | ||||||
| is_exact: stats.is_exact, | ||||||
| num_rows: stats.num_rows, | ||||||
| column_statistics, | ||||||
| // TODO stats: knowing the type of the new columns we can guess the output size | ||||||
| total_byte_size: None, | ||||||
| let primitive_row_size = inner_exprs | ||||||
| .into_iter() | ||||||
| .map(|e| match e.data_type(schema.as_ref()) { | ||||||
| Ok(data_type) => data_type.primitive_width(), | ||||||
| Err(_) => None, | ||||||
| }) | ||||||
| .try_fold(0usize, |init, v| v.map(|value| init + value)); | ||||||
|
|
||||||
| match (primitive_row_size, stats.num_rows) { | ||||||
| (Some(row_size), Some(row_count)) => { | ||||||
| Statistics { | ||||||
| is_exact: stats.is_exact, | ||||||
| num_rows: stats.num_rows, | ||||||
| column_statistics, | ||||||
| // Use the row_size * row_count as the total byte size | ||||||
| total_byte_size: Some(row_size * row_count), | ||||||
| } | ||||||
| } | ||||||
| _ => { | ||||||
| Statistics { | ||||||
| is_exact: stats.is_exact, | ||||||
| num_rows: stats.num_rows, | ||||||
| column_statistics, | ||||||
| // TODO stats: knowing the type of the new columns we can guess the output size | ||||||
| // If we can't get the exact statistics for the project | ||||||
| // Before we get the exact result, we just use the child status | ||||||
|
Comment on lines
+441
to
+443
Member
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. we can add a future ticket about I'm busy recently, if I have time, I will investigate this problem. |
||||||
| total_byte_size: stats.total_byte_size, | ||||||
|
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. If type or output has changed
Member
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 think they are different things.
such as Presto:
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. Hm I think you're right.
Member
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.
Suggested change
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. Do you means we use the cc @jackwener
Member
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. Yes, because it will cause inaccuracy to use
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 think we might need to add exactness information (is_exact) to
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 think it is fine to leave it inaccurate for now, as AFAIK we don't rely on exact information about
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 think we can't set the
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. cc @Dandandan @jackwener any comments for this
Member
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. LGTM❤️ |
||||||
| } | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -479,12 +507,12 @@ impl RecordBatchStream for ProjectionStream { | |||||
|
|
||||||
| #[cfg(test)] | ||||||
| mod tests { | ||||||
|
|
||||||
| use super::*; | ||||||
| use crate::physical_plan::common::collect; | ||||||
| use crate::physical_plan::expressions::{self, col}; | ||||||
| use crate::test::{self}; | ||||||
| use crate::test_util; | ||||||
| use arrow_schema::DataType; | ||||||
| use datafusion_common::ScalarValue; | ||||||
| use datafusion_expr::Operator; | ||||||
| use datafusion_physical_expr::expressions::binary; | ||||||
|
|
@@ -591,9 +619,8 @@ mod tests { | |||||
| Ok(()) | ||||||
| } | ||||||
|
|
||||||
| #[tokio::test] | ||||||
| async fn test_stats_projection_columns_only() { | ||||||
| let source = Statistics { | ||||||
| fn get_stats() -> Statistics { | ||||||
| Statistics { | ||||||
| is_exact: true, | ||||||
| num_rows: Some(5), | ||||||
| total_byte_size: Some(23), | ||||||
|
|
@@ -617,19 +644,31 @@ mod tests { | |||||
| null_count: None, | ||||||
| }, | ||||||
| ]), | ||||||
| }; | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| fn get_schema() -> Schema { | ||||||
| let field_0 = Field::new("col0", DataType::Int64, false); | ||||||
| let field_1 = Field::new("col1", DataType::Utf8, false); | ||||||
| let field_2 = Field::new("col2", DataType::Float32, false); | ||||||
| Schema::new(vec![field_0, field_1, field_2]) | ||||||
| } | ||||||
| #[tokio::test] | ||||||
| async fn test_stats_projection_columns_only() { | ||||||
| let source = get_stats(); | ||||||
| let schema = get_schema(); | ||||||
|
|
||||||
| let exprs: Vec<Arc<dyn PhysicalExpr>> = vec![ | ||||||
| Arc::new(expressions::Column::new("col1", 1)), | ||||||
| Arc::new(expressions::Column::new("col0", 0)), | ||||||
| ]; | ||||||
|
|
||||||
| let result = stats_projection(source, exprs.into_iter()); | ||||||
| let result = stats_projection(source, exprs.into_iter(), Arc::new(schema)); | ||||||
|
|
||||||
| let expected = Statistics { | ||||||
| is_exact: true, | ||||||
| num_rows: Some(5), | ||||||
| total_byte_size: None, | ||||||
| total_byte_size: Some(23), | ||||||
| column_statistics: Some(vec![ | ||||||
| ColumnStatistics { | ||||||
| distinct_count: Some(1), | ||||||
|
|
@@ -648,4 +687,39 @@ mod tests { | |||||
|
|
||||||
| assert_eq!(result, expected); | ||||||
| } | ||||||
|
|
||||||
| #[tokio::test] | ||||||
| async fn test_stats_projection_column_with_primitive_width_only() { | ||||||
| let source = get_stats(); | ||||||
| let schema = get_schema(); | ||||||
|
|
||||||
| let exprs: Vec<Arc<dyn PhysicalExpr>> = vec![ | ||||||
| Arc::new(expressions::Column::new("col2", 2)), | ||||||
| Arc::new(expressions::Column::new("col0", 0)), | ||||||
| ]; | ||||||
|
|
||||||
| let result = stats_projection(source, exprs.into_iter(), Arc::new(schema)); | ||||||
|
|
||||||
| let expected = Statistics { | ||||||
| is_exact: true, | ||||||
| num_rows: Some(5), | ||||||
| total_byte_size: Some(60), | ||||||
| column_statistics: Some(vec![ | ||||||
| ColumnStatistics { | ||||||
| distinct_count: None, | ||||||
| max_value: Some(ScalarValue::Float32(Some(1.1))), | ||||||
| min_value: Some(ScalarValue::Float32(Some(0.1))), | ||||||
| null_count: None, | ||||||
| }, | ||||||
| ColumnStatistics { | ||||||
| distinct_count: Some(5), | ||||||
| max_value: Some(ScalarValue::Int64(Some(21))), | ||||||
| min_value: Some(ScalarValue::Int64(Some(-4))), | ||||||
| null_count: Some(0), | ||||||
| }, | ||||||
| ]), | ||||||
| }; | ||||||
|
|
||||||
| assert_eq!(result, expected); | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1500,15 +1500,16 @@ Projection: join_t1.t1_id, join_t2.t2_id, join_t1.t1_name | |
| physical_plan | ||
| ProjectionExec: expr=[t1_id@0 as t1_id, t2_id@2 as t2_id, t1_name@1 as t1_name] | ||
| --ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t2_id@3 as t2_id] | ||
| ----CoalesceBatchesExec: target_batch_size=4096 | ||
| ------HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(join_t1.t1_id + UInt32(12)@2, join_t2.t2_id + UInt32(1)@1)] | ||
| --------CoalescePartitionsExec | ||
| ----ProjectionExec: expr=[t1_id@2 as t1_id, t1_name@3 as t1_name, join_t1.t1_id + UInt32(12)@4 as join_t1.t1_id + UInt32(12), t2_id@0 as t2_id, join_t2.t2_id + UInt32(1)@1 as join_t2.t2_id + UInt32(1)] | ||
|
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. after this pr, the t1 and t2 has the statistics in the projection exe. The table of Collect the left in the join, use the |
||
| ------CoalesceBatchesExec: target_batch_size=4096 | ||
| --------HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(join_t2.t2_id + UInt32(1)@1, join_t1.t1_id + UInt32(12)@2)] | ||
| ----------CoalescePartitionsExec | ||
| ------------ProjectionExec: expr=[t2_id@0 as t2_id, t2_id@0 + 1 as join_t2.t2_id + UInt32(1)] | ||
| --------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
| ----------------MemoryExec: partitions=1, partition_sizes=[1] | ||
| ----------ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t1_id@0 + 12 as join_t1.t1_id + UInt32(12)] | ||
| ------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
| --------------MemoryExec: partitions=1, partition_sizes=[1] | ||
| --------ProjectionExec: expr=[t2_id@0 as t2_id, t2_id@0 + 1 as join_t2.t2_id + UInt32(1)] | ||
| ----------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
| ------------MemoryExec: partitions=1, partition_sizes=[1] | ||
|
|
||
| statement ok | ||
| set datafusion.optimizer.repartition_joins = true; | ||
|
|
@@ -1527,18 +1528,19 @@ Projection: join_t1.t1_id, join_t2.t2_id, join_t1.t1_name | |
| physical_plan | ||
| ProjectionExec: expr=[t1_id@0 as t1_id, t2_id@2 as t2_id, t1_name@1 as t1_name] | ||
| --ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t2_id@3 as t2_id] | ||
| ----CoalesceBatchesExec: target_batch_size=4096 | ||
| ------HashJoinExec: mode=Partitioned, join_type=Inner, on=[(join_t1.t1_id + UInt32(12)@2, join_t2.t2_id + UInt32(1)@1)] | ||
| --------CoalesceBatchesExec: target_batch_size=4096 | ||
| ----------RepartitionExec: partitioning=Hash([join_t1.t1_id + UInt32(12)@2], 2), input_partitions=2 | ||
| ------------ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t1_id@0 + 12 as join_t1.t1_id + UInt32(12)] | ||
| --------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
| ----------------MemoryExec: partitions=1, partition_sizes=[1] | ||
| --------CoalesceBatchesExec: target_batch_size=4096 | ||
| ----------RepartitionExec: partitioning=Hash([join_t2.t2_id + UInt32(1)@1], 2), input_partitions=2 | ||
| ------------ProjectionExec: expr=[t2_id@0 as t2_id, t2_id@0 + 1 as join_t2.t2_id + UInt32(1)] | ||
| --------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
| ----------------MemoryExec: partitions=1, partition_sizes=[1] | ||
| ----ProjectionExec: expr=[t1_id@2 as t1_id, t1_name@3 as t1_name, join_t1.t1_id + UInt32(12)@4 as join_t1.t1_id + UInt32(12), t2_id@0 as t2_id, join_t2.t2_id + UInt32(1)@1 as join_t2.t2_id + UInt32(1)] | ||
| ------CoalesceBatchesExec: target_batch_size=4096 | ||
| --------HashJoinExec: mode=Partitioned, join_type=Inner, on=[(join_t2.t2_id + UInt32(1)@1, join_t1.t1_id + UInt32(12)@2)] | ||
| ----------CoalesceBatchesExec: target_batch_size=4096 | ||
| ------------RepartitionExec: partitioning=Hash([join_t2.t2_id + UInt32(1)@1], 2), input_partitions=2 | ||
| --------------ProjectionExec: expr=[t2_id@0 as t2_id, t2_id@0 + 1 as join_t2.t2_id + UInt32(1)] | ||
| ----------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
| ------------------MemoryExec: partitions=1, partition_sizes=[1] | ||
| ----------CoalesceBatchesExec: target_batch_size=4096 | ||
| ------------RepartitionExec: partitioning=Hash([join_t1.t1_id + UInt32(12)@2], 2), input_partitions=2 | ||
| --------------ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t1_id@0 + 12 as join_t1.t1_id + UInt32(12)] | ||
| ----------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
| ------------------MemoryExec: partitions=1, partition_sizes=[1] | ||
|
|
||
| # Left side expr key inner join | ||
|
|
||
|
|
@@ -1619,10 +1621,13 @@ Projection: join_t1.t1_id, join_t2.t2_id, join_t1.t1_name | |
| physical_plan | ||
| ProjectionExec: expr=[t1_id@0 as t1_id, t2_id@2 as t2_id, t1_name@1 as t1_name] | ||
| --ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t2_id@2 as t2_id] | ||
| ----CoalesceBatchesExec: target_batch_size=4096 | ||
| ------HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(t1_id@0, join_t2.t2_id - UInt32(11)@1)] | ||
| --------MemoryExec: partitions=1, partition_sizes=[1] | ||
| --------ProjectionExec: expr=[t2_id@0 as t2_id, t2_id@0 - 11 as join_t2.t2_id - UInt32(11)] | ||
| ----ProjectionExec: expr=[t1_id@2 as t1_id, t1_name@3 as t1_name, t2_id@0 as t2_id, join_t2.t2_id - UInt32(11)@1 as join_t2.t2_id - UInt32(11)] | ||
| ------CoalesceBatchesExec: target_batch_size=4096 | ||
| --------HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(join_t2.t2_id - UInt32(11)@1, t1_id@0)] | ||
| ----------CoalescePartitionsExec | ||
| ------------ProjectionExec: expr=[t2_id@0 as t2_id, t2_id@0 - 11 as join_t2.t2_id - UInt32(11)] | ||
| --------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
| ----------------MemoryExec: partitions=1, partition_sizes=[1] | ||
| ----------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
| ------------MemoryExec: partitions=1, partition_sizes=[1] | ||
|
|
||
|
|
@@ -1644,15 +1649,16 @@ Projection: join_t1.t1_id, join_t2.t2_id, join_t1.t1_name | |
| physical_plan | ||
| ProjectionExec: expr=[t1_id@0 as t1_id, t2_id@2 as t2_id, t1_name@1 as t1_name] | ||
| --ProjectionExec: expr=[t1_id@0 as t1_id, t1_name@1 as t1_name, t2_id@2 as t2_id] | ||
| ----CoalesceBatchesExec: target_batch_size=4096 | ||
| ------HashJoinExec: mode=Partitioned, join_type=Inner, on=[(t1_id@0, join_t2.t2_id - UInt32(11)@1)] | ||
| --------CoalesceBatchesExec: target_batch_size=4096 | ||
| ----------RepartitionExec: partitioning=Hash([t1_id@0], 2), input_partitions=2 | ||
| ------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
| --------------MemoryExec: partitions=1, partition_sizes=[1] | ||
| --------CoalesceBatchesExec: target_batch_size=4096 | ||
| ----------RepartitionExec: partitioning=Hash([join_t2.t2_id - UInt32(11)@1], 2), input_partitions=2 | ||
| ------------ProjectionExec: expr=[t2_id@0 as t2_id, t2_id@0 - 11 as join_t2.t2_id - UInt32(11)] | ||
| ----ProjectionExec: expr=[t1_id@2 as t1_id, t1_name@3 as t1_name, t2_id@0 as t2_id, join_t2.t2_id - UInt32(11)@1 as join_t2.t2_id - UInt32(11)] | ||
| ------CoalesceBatchesExec: target_batch_size=4096 | ||
| --------HashJoinExec: mode=Partitioned, join_type=Inner, on=[(join_t2.t2_id - UInt32(11)@1, t1_id@0)] | ||
| ----------CoalesceBatchesExec: target_batch_size=4096 | ||
| ------------RepartitionExec: partitioning=Hash([join_t2.t2_id - UInt32(11)@1], 2), input_partitions=2 | ||
| --------------ProjectionExec: expr=[t2_id@0 as t2_id, t2_id@0 - 11 as join_t2.t2_id - UInt32(11)] | ||
| ----------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
| ------------------MemoryExec: partitions=1, partition_sizes=[1] | ||
| ----------CoalesceBatchesExec: target_batch_size=4096 | ||
| ------------RepartitionExec: partitioning=Hash([t1_id@0], 2), input_partitions=2 | ||
| --------------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1 | ||
| ----------------MemoryExec: partitions=1, partition_sizes=[1] | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.