xudong963 commented on code in PR #20292:
URL: https://github.com/apache/datafusion/pull/20292#discussion_r2916947709


##########
datafusion/core/tests/statistics/mod.rs:
##########
@@ -0,0 +1,161 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use datafusion::common::Result;
+use datafusion::physical_plan::ExecutionPlan;
+use datafusion_common::Statistics;
+use datafusion_common::stats::Precision;
+use datafusion_physical_expr_common::metrics::MetricValue;
+use std::fmt::{Debug, Display, Formatter};
+use std::sync::Arc;
+
+mod tpcds;
+
+#[derive(Debug, Default, Copy, Clone)]
+struct StatsVsMetricsDisplayOptions {
+    display_output_rows: bool,
+    display_output_bytes: bool,
+}
+
+struct Node {
+    name: String,
+    stats: Statistics,
+    output_rows: Option<usize>,
+    output_bytes: Option<usize>,
+    children: Vec<Node>,
+    opts: StatsVsMetricsDisplayOptions,
+}
+
+impl Debug for Node {
+    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
+        fn fmt(f: &mut Formatter<'_>, node: &Node, depth: usize) -> 
std::fmt::Result {
+            for _ in 0..depth {
+                write!(f, "  ")?;
+            }
+            write!(f, "{}:", node.name)?;
+            fn display_opt<T: Display>(opt: Option<T>) -> impl Display {
+                match opt {
+                    None => "?".to_string(),
+                    Some(v) => v.to_string(),
+                }
+            }
+            if node.opts.display_output_rows {
+                write!(
+                    f,
+                    " output_rows={} vs {} ({}%)",
+                    node.stats.num_rows,
+                    display_opt(node.output_rows),
+                    display_opt(accuracy_percent(node.stats.num_rows, 
node.output_rows))
+                )?;
+            }
+            if node.opts.display_output_bytes {
+                write!(
+                    f,
+                    " output_bytes={} vs {} ({}%)",
+                    node.stats.total_byte_size,
+                    display_opt(node.output_bytes),
+                    display_opt(accuracy_percent(
+                        node.stats.total_byte_size,
+                        node.output_bytes
+                    ))
+                )?;
+            }
+            writeln!(f)?;
+            for c in &node.children {
+                fmt(f, c, depth + 1)?;
+            }
+            Ok(())
+        }
+
+        fmt(f, self, 0)
+    }
+}
+
+impl Node {
+    fn from_plan(
+        plan: &Arc<dyn ExecutionPlan>,
+        opts: StatsVsMetricsDisplayOptions,
+    ) -> Result<Self> {
+        let mut children = vec![];
+        for child in plan.children() {
+            children.push(Node::from_plan(child, opts)?);
+        }
+
+        let mut node = Node {
+            name: plan.name().to_string(),
+            stats: plan.partition_statistics(None)?,
+            output_rows: None,
+            output_bytes: None,
+            children,
+            opts,
+        };
+        if let Some(metrics) = plan.metrics() {
+            node.output_rows = metrics.output_rows();
+            node.output_bytes = metrics
+                .sum(|v| matches!(v.value(), MetricValue::OutputBytes(_)))
+                .map(|v| v.as_usize());
+        }
+
+        Ok(node)
+    }
+
+    fn avg_row_accuracy(&self) -> usize {
+        fn collect_accuracy(node: &Node) -> Vec<usize> {
+            let mut results = vec![];
+            for child in &node.children {
+                results.extend(collect_accuracy(child));
+            }
+            if let Some(accuracy) =
+                accuracy_percent(node.stats.num_rows, node.output_rows)
+            {
+                results.push(accuracy);
+            }
+            results
+        }
+        let accuracy = collect_accuracy(self);
+        accuracy.iter().sum::<usize>() / accuracy.len()
+    }
+
+    fn avg_byte_accuracy(&self) -> usize {
+        fn collect_accuracy(node: &Node) -> Vec<usize> {
+            let mut results = vec![];
+            for child in &node.children {
+                results.extend(collect_accuracy(child));
+            }
+            if let Some(accuracy) =
+                accuracy_percent(node.stats.total_byte_size, node.output_bytes)
+            {
+                results.push(accuracy);
+            }
+            results
+        }
+        let accuracy = collect_accuracy(self);
+        accuracy.iter().sum::<usize>() / accuracy.len()

Review Comment:
   is it possible that `accuracy.len()` is zero?



##########
datafusion/core/tests/statistics/tpcds.rs:
##########
@@ -0,0 +1,876 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#![allow(clippy::absurd_extreme_comparisons)]
+#![allow(unused_comparisons)]
+
+use crate::statistics::StatsVsMetricsDisplayOptions;
+use datafusion::common::Result;
+use datafusion::prelude::{ParquetReadOptions, SessionContext};
+use datafusion_physical_plan::collect;
+use std::fs;
+
+#[cfg(test)]
+mod tests {
+    use super::*;
+    use crate::statistics::Node;
+
+    #[tokio::test]
+    async fn tpcds_1() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(1).await?;
+        assert!(result.row_estimation_accuracy >= 20);
+        assert!(result.byte_estimation_accuracy >= 8);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_2() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(2).await?;
+        assert!(result.row_estimation_accuracy >= 34);
+        assert!(result.byte_estimation_accuracy >= 16);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_3() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(3).await?;
+        assert!(result.row_estimation_accuracy >= 44);
+        assert!(result.byte_estimation_accuracy >= 10);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_4() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(4).await?;
+        assert!(result.row_estimation_accuracy >= 22);
+        assert!(result.byte_estimation_accuracy >= 4);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_5() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(5).await?;
+        assert!(result.row_estimation_accuracy >= 21);
+        assert!(result.byte_estimation_accuracy >= 12);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_6() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(6).await?;
+        assert!(result.row_estimation_accuracy >= 28);
+        assert!(result.byte_estimation_accuracy >= 5);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_7() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(7).await?;
+        assert!(result.row_estimation_accuracy >= 34);
+        assert!(result.byte_estimation_accuracy >= 4);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_8() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(8).await?;
+        assert!(result.row_estimation_accuracy >= 34);
+        assert!(result.byte_estimation_accuracy >= 2);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_9() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(9).await?;
+        assert!(result.row_estimation_accuracy >= 55);
+        assert!(result.byte_estimation_accuracy >= 30);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_10() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(10).await?;
+        assert!(result.row_estimation_accuracy >= 33);
+        assert!(result.byte_estimation_accuracy >= 7);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_11() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(11).await?;
+        assert!(result.row_estimation_accuracy >= 23);
+        assert!(result.byte_estimation_accuracy >= 6);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_12() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(12).await?;
+        assert!(result.row_estimation_accuracy >= 22);
+        assert!(result.byte_estimation_accuracy >= 5);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_13() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(13).await?;
+        assert!(result.row_estimation_accuracy >= 54);
+        assert!(result.byte_estimation_accuracy >= 9);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_14() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(14).await?;
+        assert!(result.row_estimation_accuracy >= 37);
+        assert!(result.byte_estimation_accuracy >= 16);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_15() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(15).await?;
+        assert!(result.row_estimation_accuracy >= 15);
+        assert!(result.byte_estimation_accuracy >= 8);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_16() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(16).await?;
+        assert!(result.row_estimation_accuracy >= 24);
+        assert!(result.byte_estimation_accuracy >= 9);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_17() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(17).await?;
+        assert!(result.row_estimation_accuracy >= 11);
+        assert!(result.byte_estimation_accuracy >= 6);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_18() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(18).await?;
+        assert!(result.row_estimation_accuracy >= 36);
+        assert!(result.byte_estimation_accuracy >= 6);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_19() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(19).await?;
+        assert!(result.row_estimation_accuracy >= 36);
+        assert!(result.byte_estimation_accuracy >= 7);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_20() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(20).await?;
+        assert!(result.row_estimation_accuracy >= 22);
+        assert!(result.byte_estimation_accuracy >= 5);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_21() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(21).await?;
+        assert!(result.row_estimation_accuracy >= 31);
+        assert!(result.byte_estimation_accuracy >= 3);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_22() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(22).await?;
+        assert!(result.row_estimation_accuracy >= 18);
+        assert!(result.byte_estimation_accuracy >= 2);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_23() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(23).await?;
+        assert!(result.row_estimation_accuracy >= 18);
+        assert!(result.byte_estimation_accuracy >= 12);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_24() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(24).await?;
+        assert!(result.row_estimation_accuracy >= 33);
+        assert!(result.byte_estimation_accuracy >= 7);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_25() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(25).await?;
+        assert!(result.row_estimation_accuracy >= 18);
+        assert!(result.byte_estimation_accuracy >= 6);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_26() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(26).await?;
+        assert!(result.row_estimation_accuracy >= 31);
+        assert!(result.byte_estimation_accuracy >= 2);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_27() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(27).await?;
+        assert!(result.row_estimation_accuracy >= 20);
+        assert!(result.byte_estimation_accuracy >= 3);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_28() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(28).await?;
+        assert!(result.row_estimation_accuracy >= 72);
+        assert!(result.byte_estimation_accuracy >= 39);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_29() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(29).await?;
+        assert!(result.row_estimation_accuracy >= 25);
+        assert!(result.byte_estimation_accuracy >= 8);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_30() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(30).await?;
+        assert!(result.row_estimation_accuracy >= 26);
+        assert!(result.byte_estimation_accuracy >= 3);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_31() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(31).await?;
+        assert!(result.row_estimation_accuracy >= 18);
+        assert!(result.byte_estimation_accuracy >= 6);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_32() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(32).await?;
+        assert!(result.row_estimation_accuracy >= 27);
+        assert!(result.byte_estimation_accuracy >= 13);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_33() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(33).await?;
+        assert!(result.row_estimation_accuracy >= 41);
+        assert!(result.byte_estimation_accuracy >= 12);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_34() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(34).await?;
+        assert!(result.row_estimation_accuracy >= 17);
+        assert!(result.byte_estimation_accuracy >= 3);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_35() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(35).await?;
+        assert!(result.row_estimation_accuracy >= 33);
+        assert!(result.byte_estimation_accuracy >= 8);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_36() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(36).await?;
+        assert!(result.row_estimation_accuracy >= 10);
+        assert!(result.byte_estimation_accuracy >= 3);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_37() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(37).await?;
+        assert!(result.row_estimation_accuracy >= 27);
+        assert!(result.byte_estimation_accuracy >= 9);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_38() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(38).await?;
+        assert!(result.row_estimation_accuracy >= 27);
+        assert!(result.byte_estimation_accuracy >= 7);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_39() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(39).await?;
+        assert!(result.row_estimation_accuracy >= 15);
+        assert!(result.byte_estimation_accuracy >= 9);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_40() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(40).await?;
+        assert!(result.row_estimation_accuracy >= 35);
+        assert!(result.byte_estimation_accuracy >= 9);
+        Ok(())
+    }
+
+    #[tokio::test]
+    async fn tpcds_41() -> Result<()> {
+        let result = tpcds_stats_vs_metrics(41).await?;
+        assert!(result.row_estimation_accuracy >= 33);
+        assert!(result.byte_estimation_accuracy >= 0);

Review Comment:
   this assert is alway true, right?



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