Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 91 additions & 0 deletions datafusion/core/tests/physical_optimizer/partition_statistics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ mod test {
use datafusion_common::{ColumnStatistics, ScalarValue, Statistics};
use datafusion_execution::config::SessionConfig;
use datafusion_execution::TaskContext;
use datafusion_expr::{WindowFrame, WindowFunctionDefinition};
use datafusion_expr_common::operator::Operator;
use datafusion_functions_aggregate::count::count_udaf;
use datafusion_physical_expr::aggregate::AggregateExprBuilder;
Expand All @@ -52,6 +53,7 @@ mod test {
use datafusion_physical_plan::repartition::RepartitionExec;
use datafusion_physical_plan::sorts::sort::SortExec;
use datafusion_physical_plan::union::{InterleaveExec, UnionExec};
use datafusion_physical_plan::windows::{create_window_expr, WindowAggExec};
use datafusion_physical_plan::{
execute_stream_partitioned, get_plan_string, ExecutionPlan,
ExecutionPlanProperties,
Expand Down Expand Up @@ -980,4 +982,93 @@ mod test {

Ok(())
}

#[tokio::test]
async fn test_statistic_by_partition_of_window_agg() -> Result<()> {
let scan = create_scan_exec_with_statistics(None, Some(2)).await;

let window_expr = create_window_expr(
&WindowFunctionDefinition::AggregateUDF(count_udaf()),
"count".to_owned(),
&[col("id", &scan.schema())?],
&[], // no partition by
&[PhysicalSortExpr::new(
col("id", &scan.schema())?,
SortOptions::default(),
)],
Arc::new(WindowFrame::new(Some(false))),
scan.schema(),
false,
false,
None,
)?;

let window_agg: Arc<dyn ExecutionPlan> =
Arc::new(WindowAggExec::try_new(vec![window_expr], scan, true)?);

// Verify partition statistics are properly propagated (not unknown)
let statistics = (0..window_agg.output_partitioning().partition_count())
.map(|idx| window_agg.partition_statistics(Some(idx)))
.collect::<Result<Vec<_>>>()?;

assert_eq!(statistics.len(), 2);

// Window functions preserve input row counts and first column statistics
// but add unknown statistics for the new window column
let expected_statistic_partition_1 = Statistics {
num_rows: Precision::Exact(2),
total_byte_size: Precision::Absent,
column_statistics: vec![
ColumnStatistics {
null_count: Precision::Exact(0),
max_value: Precision::Exact(ScalarValue::Int32(Some(4))),
min_value: Precision::Exact(ScalarValue::Int32(Some(3))),
sum_value: Precision::Absent,
distinct_count: Precision::Absent,
},
ColumnStatistics {
null_count: Precision::Absent,
max_value: Precision::Absent,
min_value: Precision::Absent,
sum_value: Precision::Absent,
distinct_count: Precision::Absent,
},
ColumnStatistics::new_unknown(), // window column
],
};

let expected_statistic_partition_2 = Statistics {
num_rows: Precision::Exact(2),
total_byte_size: Precision::Absent,
column_statistics: vec![
ColumnStatistics {
null_count: Precision::Exact(0),
max_value: Precision::Exact(ScalarValue::Int32(Some(2))),
min_value: Precision::Exact(ScalarValue::Int32(Some(1))),
sum_value: Precision::Absent,
distinct_count: Precision::Absent,
},
ColumnStatistics {
null_count: Precision::Absent,
max_value: Precision::Absent,
min_value: Precision::Absent,
sum_value: Precision::Absent,
distinct_count: Precision::Absent,
},
ColumnStatistics::new_unknown(), // window column
],
};

assert_eq!(statistics[0], expected_statistic_partition_1);
assert_eq!(statistics[1], expected_statistic_partition_2);

// Verify the statistics match actual execution results
let expected_stats = vec![
ExpectedStatistics::NonEmpty(3, 4, 2),
ExpectedStatistics::NonEmpty(1, 2, 2),
];
validate_statistics_with_data(window_agg, expected_stats, 0).await?;

Ok(())
}
}
12 changes: 4 additions & 8 deletions datafusion/physical-plan/src/windows/window_agg_exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ impl WindowAggExec {
}
}

fn statistics_inner(&self) -> Result<Statistics> {
let input_stat = self.input.partition_statistics(None)?;
fn statistics_inner(&self, partition: Option<usize>) -> Result<Statistics> {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We may want to move the code directly into the partition_statistics and remove this inner function.

let input_stat = self.input.partition_statistics(partition)?;
let win_cols = self.window_expr.len();
let input_cols = self.input.schema().fields().len();
// TODO stats: some windowing function will maintain invariants such as min, max...
Expand Down Expand Up @@ -291,15 +291,11 @@ impl ExecutionPlan for WindowAggExec {
}

fn statistics(&self) -> Result<Statistics> {
self.statistics_inner()
self.statistics_inner(None)
}

fn partition_statistics(&self, partition: Option<usize>) -> Result<Statistics> {
if partition.is_none() {
self.statistics_inner()
} else {
Ok(Statistics::new_unknown(&self.schema()))
}
self.statistics_inner(partition)
}
}

Expand Down