UBarney commented on code in PR #15954:
URL: https://github.com/apache/datafusion/pull/15954#discussion_r2093873182
##########
datafusion/core/tests/physical_optimizer/partition_statistics.rs:
##########
@@ -488,4 +520,155 @@ mod test {
assert_eq!(statistics[0], expected_statistic_partition);
Ok(())
}
+
+ #[tokio::test]
+ async fn test_statistic_by_partition_of_agg() -> Result<()> {
+ let scan = create_scan_exec_with_statistics(None, Some(2)).await;
+
+ let scan_schema = scan.schema();
+
+ let group_by = PhysicalGroupBy::new_single(vec![
+ (col("id", &scan_schema)?, "id".to_string()),
+ (
+ binary(
+ lit(1),
+ Operator::Plus,
+ col("id", &scan_schema)?,
+ &scan_schema,
+ )?,
+ "expr".to_string(),
+ ),
+ ]);
+
+ let aggr_expr = vec![AggregateExprBuilder::new(count_udaf(),
vec![lit(1)])
+ .schema(Arc::clone(&scan_schema))
+ .alias(String::from("COUNT(c)"))
+ .build()
+ .map(Arc::new)?];
+
+ let aggregate_exec_partial = Arc::new(AggregateExec::try_new(
+ AggregateMode::Partial,
+ group_by.clone(),
+ aggr_expr.clone(),
+ vec![None],
+ Arc::clone(&scan),
+ scan_schema.clone(),
+ )?);
+
+ let p0_statistics =
aggregate_exec_partial.partition_statistics(Some(0))?;
+
+ let expected_p0_statistics = Statistics {
+ num_rows: Precision::Inexact(2),
+ total_byte_size: Precision::Absent,
+ column_statistics: vec![
+ ColumnStatistics {
+ null_count: Precision::Absent,
+ 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::new_unknown(),
+ ColumnStatistics::new_unknown(),
+ ],
+ };
+
+ assert_eq!(&p0_statistics, &expected_p0_statistics);
+
+ let expected_p1_statistics = Statistics {
+ num_rows: Precision::Inexact(2),
+ total_byte_size: Precision::Absent,
+ column_statistics: vec![
+ ColumnStatistics {
+ null_count: Precision::Absent,
+ 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::new_unknown(),
+ ColumnStatistics::new_unknown(),
+ ],
+ };
+
+ let p1_statistics =
aggregate_exec_partial.partition_statistics(Some(1))?;
+ assert_eq!(&p1_statistics, &expected_p1_statistics);
+
+ validate_statistics_with_data(
+ aggregate_exec_partial.clone(),
+ vec![
+ ExpectedStatistics::NonEmpty(3, 4, 2),
+ ExpectedStatistics::NonEmpty(1, 2, 2),
+ ],
+ 0,
+ )
+ .await?;
+
+ let agg_final = Arc::new(AggregateExec::try_new(
+ AggregateMode::Final,
+ group_by.clone(),
+ aggr_expr.clone(),
+ vec![None],
+ aggregate_exec_partial.clone(),
+ aggregate_exec_partial.schema(),
+ )?);
+
+ let p0_statistics = agg_final.partition_statistics(Some(0))?;
+ assert_eq!(&p0_statistics, &expected_p0_statistics);
+
+ let p1_statistics = agg_final.partition_statistics(Some(1))?;
+ assert_eq!(&p1_statistics, &expected_p1_statistics);
+
+ validate_statistics_with_data(
+ agg_final.clone(),
+ vec![
+ ExpectedStatistics::NonEmpty(3, 4, 2),
+ ExpectedStatistics::NonEmpty(1, 2, 2),
+ ],
+ 0,
+ )
+ .await?;
+
+ let empty_table =
+ Arc::new(EmptyExec::new(scan_schema.clone()).with_partitions(2));
+
+ let agg_partial = Arc::new(AggregateExec::try_new(
+ AggregateMode::Partial,
+ group_by.clone(),
+ aggr_expr.clone(),
+ vec![None],
+ empty_table.clone(),
+ scan_schema.clone(),
+ )?);
+
+ let agg_final = Arc::new(AggregateExec::try_new(
+ AggregateMode::Final,
+ group_by.clone(),
+ aggr_expr.clone(),
+ vec![None],
+ agg_partial.clone(),
+ agg_partial.schema(),
+ )?);
+
+ let empty_stat = Statistics {
+ num_rows: Precision::Exact(0),
+ total_byte_size: Precision::Absent,
+ column_statistics: vec![
+ ColumnStatistics::new_unknown(),
+ ColumnStatistics::new_unknown(),
+ ColumnStatistics::new_unknown(),
+ ],
+ };
+ assert_eq!(&empty_stat, &agg_final.partition_statistics(Some(0))?);
+ assert_eq!(&empty_stat, &agg_final.partition_statistics(Some(1))?);
Review Comment:
> How about also adding `AggregateMode::Final` test
done. PTAL
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]