gabotechs commented on code in PR #27574:
URL: https://github.com/apache/datafusion/pull/27574#discussion_r4671055660


##########
datafusion/physical-expr/src/projection.rs:
##########
@@ -3322,6 +3437,115 @@ pub(crate) mod tests {
         Ok(())
     }
 
+    #[test]
+    fn test_expr_column_statistics_bounds() {

Review Comment:
   This test is a bit hard to follow, any chance we could split it in multiple 
more scoped ones?



##########
datafusion/physical-plan/src/aggregates/mod.rs:
##########
@@ -9148,6 +9264,306 @@ mod tests {
         Ok(())
     }
 
+    #[test]
+    fn test_aggregate_stats_integer_bounds() -> Result<()> {
+        use datafusion_common::ColumnStatistics;
+
+        fn bounds(min: i32, max: i32) -> ColumnStatistics {
+            ColumnStatistics::new_unknown()
+                
.with_min_value(Precision::Inexact(ScalarValue::Int32(Some(min))))
+                
.with_max_value(Precision::Inexact(ScalarValue::Int32(Some(max))))
+        }
+
+        struct TestCase {
+            name: &'static str,
+            input_rows: Precision<usize>,
+            col_a_stats: ColumnStatistics,
+            col_b_stats: ColumnStatistics,
+            group_by_cols: Vec<&'static str>,
+            expected_num_rows: Precision<usize>,
+        }
+        let case = |name, col_a_stats, expected_num_rows| TestCase {
+            name,
+            input_rows: Precision::Exact(1_000_000),
+            col_a_stats,
+            col_b_stats: ColumnStatistics::new_unknown(),
+            group_by_cols: vec!["a"],
+            expected_num_rows,
+        };
+
+        let cases = vec![
+            case(
+                "nullable key with unknown NULL count adds a NULL group",
+                bounds(1992, 1998),
+                Precision::Inexact(8),
+            ),

Review Comment:
   This is pretty much like the Go-style of testing, but in Rust, I recommend 
to split these in individual tests instead.
   
   The issue with this setup is that the Cargo toolchain does not report well 
which test failing amongst all the cases, and re-running just a single failed 
test becomes very hard.



##########
datafusion/physical-plan/src/aggregates/mod.rs:
##########
@@ -9148,6 +9264,306 @@ mod tests {
         Ok(())
     }
 
+    #[test]
+    fn test_aggregate_stats_integer_bounds() -> Result<()> {
+        use datafusion_common::ColumnStatistics;
+
+        fn bounds(min: i32, max: i32) -> ColumnStatistics {
+            ColumnStatistics::new_unknown()
+                
.with_min_value(Precision::Inexact(ScalarValue::Int32(Some(min))))
+                
.with_max_value(Precision::Inexact(ScalarValue::Int32(Some(max))))
+        }
+
+        struct TestCase {
+            name: &'static str,
+            input_rows: Precision<usize>,
+            col_a_stats: ColumnStatistics,
+            col_b_stats: ColumnStatistics,
+            group_by_cols: Vec<&'static str>,
+            expected_num_rows: Precision<usize>,
+        }
+        let case = |name, col_a_stats, expected_num_rows| TestCase {
+            name,
+            input_rows: Precision::Exact(1_000_000),
+            col_a_stats,
+            col_b_stats: ColumnStatistics::new_unknown(),
+            group_by_cols: vec!["a"],
+            expected_num_rows,
+        };
+
+        let cases = vec![
+            case(
+                "nullable key with unknown NULL count adds a NULL group",
+                bounds(1992, 1998),
+                Precision::Inexact(8),
+            ),
+            case(
+                "key without NULLs has no NULL group",
+                bounds(1992, 1998).with_null_count(Precision::Exact(0)),
+                Precision::Inexact(7),
+            ),
+            case(
+                "NDV is preferred to bounds",
+                bounds(1, 100).with_distinct_count(Precision::Inexact(3)),
+                Precision::Inexact(3),
+            ),
+            case(
+                "bounds cap the NDV",
+                bounds(1, 7).with_distinct_count(Precision::Inexact(1_000)),
+                Precision::Inexact(7),
+            ),
+            TestCase {
+                name: "bounds do not replace unknown input rows",
+                input_rows: Precision::Absent,
+                ..case("", bounds(1, i32::MAX), Precision::Absent)
+            },
+            TestCase {
+                name: "NDV capped by bounds is used without input rows",
+                input_rows: Precision::Absent,
+                ..case(
+                    "",
+                    bounds(1, 
7).with_distinct_count(Precision::Inexact(1_000)),
+                    Precision::Inexact(7),
+                )
+            },
+            case(
+                "range larger than the input is capped by input rows",
+                bounds(0, i32::MAX),
+                Precision::Inexact(1_000_000),
+            ),
+            case(
+                "string bounds do not count values",
+                ColumnStatistics::new_unknown()
+                    .with_min_value(Precision::Exact(ScalarValue::from("a")))
+                    .with_max_value(Precision::Exact(ScalarValue::from("b"))),
+                Precision::Inexact(1_000_000),
+            ),
+            case(
+                "NULL bound does not count values",
+                ColumnStatistics::new_unknown()
+                    .with_min_value(Precision::Exact(ScalarValue::Int32(None)))
+                    
.with_max_value(Precision::Exact(ScalarValue::Int32(Some(10)))),
+                Precision::Inexact(1_000_000),
+            ),
+            case(
+                "bounds out of order do not count values",
+                bounds(10, 1),
+                Precision::Inexact(1_000_000),
+            ),
+            TestCase {
+                name: "bounds and NDV multiply across keys",
+                col_b_stats: ColumnStatistics::new_unknown()
+                    .with_distinct_count(Precision::Exact(5)),
+                group_by_cols: vec!["a", "b"],
+                ..case(
+                    "",
+                    bounds(1, 10).with_null_count(Precision::Exact(0)),
+                    Precision::Inexact(50),
+                )
+            },
+            TestCase {
+                name: "key without NDV or bounds keeps input rows",
+                group_by_cols: vec!["a", "b"],
+                ..case("", bounds(1, 10), Precision::Inexact(1_000_000))
+            },
+        ];
+
+        for case in cases {
+            let schema = Arc::new(Schema::new(vec![
+                Field::new("a", DataType::Int32, true),
+                Field::new("b", DataType::Int32, true),
+            ]));
+            let input_stats = Statistics {
+                num_rows: case.input_rows,
+                total_byte_size: Precision::Inexact(1_000_000),
+                column_statistics: vec![case.col_a_stats, case.col_b_stats],
+            };
+            let agg = build_test_aggregate(
+                &schema,
+                input_stats,
+                simple_group_by(&schema, &case.group_by_cols),
+                None,
+            )?;
+
+            let stats = StatisticsContext::new().compute(&agg, 
&StatisticsArgs::new())?;
+            assert_eq!(stats.num_rows, case.expected_num_rows, "{}", 
case.name);
+        }
+
+        // Grouping sets keep the NDV-only estimate.
+        let schema = Arc::new(Schema::new(vec![
+            Field::new("a", DataType::Int32, true),
+            Field::new("b", DataType::Int32, true),
+        ]));
+        let grouping_set = PhysicalGroupBy::new(
+            vec![(col("a", &schema)?, "a".to_string())],
+            vec![(lit(ScalarValue::Int32(None)), "a".to_string())],
+            vec![vec![false], vec![true]],
+            true,
+        );
+        let input_stats = Statistics {
+            num_rows: Precision::Exact(1_000_000),
+            total_byte_size: Precision::Inexact(1_000_000),
+            column_statistics: vec![bounds(1, 10), 
ColumnStatistics::new_unknown()],
+        };
+        let agg = build_test_aggregate(&schema, input_stats, grouping_set, 
None)?;
+        let stats = StatisticsContext::new().compute(&agg, 
&StatisticsArgs::new())?;
+        assert_eq!(stats.num_rows, Precision::Inexact(1_000_000));
+
+        Ok(())
+    }
+
+    /// The bounds of an expression key reach the final aggregate through the
+    /// partial aggregate output.
+    #[test]
+    fn test_aggregate_stats_expression_bounds_through_partial() -> Result<()> {

Review Comment:
   I see overall the ratio of tests VS actual code for this PR is really high. 
Have not reviewed in depth every test, but if you see opportunities for 
condensing them and removing overlap so that we can have fewer that would be 
awesome.



-- 
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]

Reply via email to