Apache DataFusion SQL Query Engine
Refactor Statistics, introduce precision estimates (`Exact`, `Inexact`, `Absent`) (#7793)
Berkay Şahinc9330bc09e58
diff --git a/datafusion-examples/examples/csv_opener.rs b/datafusion-examples/examples/csv_opener.rsindex 5126666d5e..15fb07ded4 100644--- a/datafusion-examples/examples/csv_opener.rs+++ b/datafusion-examples/examples/csv_opener.rs@@ -17,6 +17,7 @@use std::{sync::Arc, vec};+use datafusion::common::Statistics;use datafusion::{assert_batches_eq,datasource::{@@ -29,6 +30,7 @@ use datafusion::{physical_plan::metrics::ExecutionPlanMetricsSet,test_util::aggr_test_schema,};+use futures::StreamExt;use object_store::local::LocalFileSystem;@@ -60,7 +62,7 @@ async fn main() -> Result<()> {object_store_url: ObjectStoreUrl::local_filesystem(),file_schema: schema.clone(),file_groups: vec![vec![PartitionedFile::new(path.display().to_string(), 10)]],- statistics: Default::default(),+ statistics: Statistics::new_unknown(&schema),projection: Some(vec![12, 0]),limit: Some(5),table_partition_cols: vec![],diff --git a/datafusion-examples/examples/custom_datasource.rs b/datafusion-examples/examples/custom_datasource.rsindex a24573c860..dd36665a93 100644--- a/datafusion-examples/examples/custom_datasource.rs+++ b/datafusion-examples/examples/custom_datasource.rs@@ -15,13 +15,17 @@// specific language governing permissions and limitations// under the License.-use async_trait::async_trait;+use std::any::Any;+use std::collections::{BTreeMap, HashMap};+use std::fmt::{self, Debug, Formatter};+use std::sync::{Arc, Mutex};+use std::time::Duration;+use datafusion::arrow::array::{UInt64Builder, UInt8Builder};use datafusion::arrow::datatypes::{DataType, Field, Schema, SchemaRef};use datafusion::arrow::record_batch::RecordBatch;use datafusion::dataframe::DataFrame;-use datafusion::datasource::provider_as_source;-use datafusion::datasource::{TableProvider, TableType};+use datafusion::datasource::{provider_as_source, TableProvider, TableType};use datafusion::error::Result;use datafusion::execution::context::{SessionState, TaskContext};use datafusion::physical_plan::expressions::PhysicalSortExpr;@@ -32,11 +36,8 @@ use datafusion::physical_plan::{};use datafusion::prelude::*;use datafusion_expr::{Expr, LogicalPlanBuilder};-use std::any::Any;-use std::collections::{BTreeMap, HashMap};-use std::fmt::{self, Debug, Formatter};-use std::sync::{Arc, Mutex};-use std::time::Duration;++use async_trait::async_trait;use tokio::time::timeout;/// This example demonstrates executing a simple query against a custom datasource@@ -270,7 +271,7 @@ impl ExecutionPlan for CustomExec {)?))}- fn statistics(&self) -> Statistics {- Statistics::default()+ fn statistics(&self) -> Result<Statistics> {+ Ok(Statistics::new_unknown(&self.schema()))}}diff --git a/datafusion-examples/examples/json_opener.rs b/datafusion-examples/examples/json_opener.rsindex 74ba6f3852..1a3dbe57be 100644--- a/datafusion-examples/examples/json_opener.rs+++ b/datafusion-examples/examples/json_opener.rs@@ -29,6 +29,8 @@ use datafusion::{error::Result,physical_plan::metrics::ExecutionPlanMetricsSet,};+use datafusion_common::Statistics;+use futures::StreamExt;use object_store::ObjectStore;@@ -63,7 +65,7 @@ async fn main() -> Result<()> {object_store_url: ObjectStoreUrl::local_filesystem(),file_schema: schema.clone(),file_groups: vec![vec![PartitionedFile::new(path.to_string(), 10)]],- statistics: Default::default(),+ statistics: Statistics::new_unknown(&schema),projection: Some(vec![1, 0]),limit: Some(5),table_partition_cols: vec![],diff --git a/datafusion/common/src/stats.rs b/datafusion/common/src/stats.rsindex 279ad434a8..fbf639a321 100644--- a/datafusion/common/src/stats.rs+++ b/datafusion/common/src/stats.rs@@ -17,10 +17,11 @@//! This module provides data structures to represent statistics+use std::fmt::{self, Debug, Display};+use crate::ScalarValue;-use arrow::datatypes::DataType;-use std::fmt::{self, Debug, Display};+use arrow_schema::Schema;/// Represents a value with a degree of certainty. `Precision` is used to/// propagate information the precision of statistical values.@@ -203,70 +204,95 @@ impl<T: fmt::Debug + Clone + PartialEq + Eq + PartialOrd> Display for Precision</// Fields are optional and can be inexact because the sources/// sometimes provide approximate estimates for performance reasons/// and the transformations output are not always predictable.-#[derive(Debug, Clone, Default, PartialEq, Eq)]+#[derive(Debug, Clone, PartialEq, Eq)]pub struct Statistics {- /// The number of table rows- pub num_rows: Option<usize>,- /// total bytes of the table rows- pub total_byte_size: Option<usize>,- /// Statistics on a column level- pub column_statistics: Option<Vec<ColumnStatistics>>,- /// If true, any field that is `Some(..)` is the actual value in the data provided by the operator (it is not- /// an estimate). Any or all other fields might still be None, in which case no information is known.- /// if false, any field that is `Some(..)` may contain an inexact estimate and may not be the actual value.- pub is_exact: bool,+ /// The number of table rows.+ pub num_rows: Precision<usize>,+ /// Total bytes of the table rows.+ pub total_byte_size: Precision<usize>,+ /// Statistics on a column level. It contains a [`ColumnStatistics`] for+ /// each field in the schema of the the table to which the [`Statistics`] refer.+ pub column_statistics: Vec<ColumnStatistics>,}-impl Display for Statistics {- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {- if self.num_rows.is_none() && self.total_byte_size.is_none() && !self.is_exact {- return Ok(());+impl Statistics {+ /// Returns a [`Statistics`] instance for the given schema by assigning+ /// unknown statistics to each column in the schema.+ pub fn new_unknown(schema: &Schema) -> Self {+ Self {+ num_rows: Precision::Absent,+ total_byte_size: Precision::Absent,+ column_statistics: Statistics::unknown_column(schema),}+ }- let rows = self- .num_rows- .map_or_else(|| "None".to_string(), |v| v.to_string());- let bytes = self- .total_byte_size- .map_or_else(|| "None".to_string(), |v| v.to_string());+ /// Returns an unbounded `ColumnStatistics` for each field in the schema.+ pub fn unknown_column(schema: &Schema) -> Vec<ColumnStatistics> {+ schema+ .fields()+ .iter()+ .map(|_| ColumnStatistics::new_unknown())+ .collect()+ }- write!(f, "rows={}, bytes={}, exact={}", rows, bytes, self.is_exact)?;+ /// If the exactness of a [`Statistics`] instance is lost, this function relaxes+ /// the exactness of all information by converting them [`Precision::Inexact`].+ pub fn into_inexact(self) -> Self {+ Statistics {+ num_rows: self.num_rows.to_inexact(),+ total_byte_size: self.total_byte_size.to_inexact(),+ column_statistics: self+ .column_statistics+ .into_iter()+ .map(|cs| ColumnStatistics {+ null_count: cs.null_count.to_inexact(),+ max_value: cs.max_value.to_inexact(),+ min_value: cs.min_value.to_inexact(),+ distinct_count: cs.distinct_count.to_inexact(),+ })+ .collect::<Vec<_>>(),+ }+ }+}++impl Display for Statistics {+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ write!(f, "Rows={}, Bytes={}", self.num_rows, self.total_byte_size)?;Ok(())}}/// Statistics for a column within a relation-#[derive(Clone, Debug, Default, PartialEq, Eq)]+#[derive(Clone, Debug, PartialEq, Eq, Default)]pub struct ColumnStatistics {/// Number of null values on column- pub null_count: Option<usize>,+ pub null_count: Precision<usize>,/// Maximum value of column- pub max_value: Option<ScalarValue>,+ pub max_value: Precision<ScalarValue>,/// Minimum value of column- pub min_value: Option<ScalarValue>,+ pub min_value: Precision<ScalarValue>,/// Number of distinct values- pub distinct_count: Option<usize>,+ pub distinct_count: Precision<usize>,}impl ColumnStatistics {/// Column contains a single non null value (e.g constant).pub fn is_singleton(&self) -> bool {- match (&self.min_value, &self.max_value) {+ match (self.min_value.get_value(), self.max_value.get_value()) {// Min and max values are the same and not infinity.(Some(min), Some(max)) => !min.is_null() && !max.is_null() && (min == max),(_, _) => false,}}- /// Returns the [`ColumnStatistics`] corresponding to the given datatype by assigning infinite bounds.- pub fn new_with_unbounded_column(dt: &DataType) -> ColumnStatistics {- let null = ScalarValue::try_from(dt.clone()).ok();+ /// Returns a [`ColumnStatistics`] instance having all [`Precision::Absent`] parameters.+ pub fn new_unknown() -> ColumnStatistics {ColumnStatistics {- null_count: None,- max_value: null.clone(),- min_value: null,- distinct_count: None,+ null_count: Precision::Absent,+ max_value: Precision::Absent,+ min_value: Precision::Absent,+ distinct_count: Precision::Absent,}}}diff --git a/datafusion/core/src/datasource/file_format/arrow.rs b/datafusion/core/src/datasource/file_format/arrow.rsindex 45ecdd6083..16ae4411d1 100644--- a/datafusion/core/src/datasource/file_format/arrow.rs+++ b/datafusion/core/src/datasource/file_format/arrow.rs@@ -19,20 +19,24 @@//!//! Works with files following the [Arrow IPC format](https://arrow.apache.org/docs/format/Columnar.html#ipc-file-format)+use std::any::Any;+use std::io::{Read, Seek};+use std::sync::Arc;+use crate::datasource::file_format::FileFormat;use crate::datasource::physical_plan::{ArrowExec, FileScanConfig};use crate::error::Result;use crate::execution::context::SessionState;use crate::physical_plan::ExecutionPlan;+use arrow::ipc::reader::FileReader;use arrow_schema::{Schema, SchemaRef};-use async_trait::async_trait;+use datafusion_common::{FileType, Statistics};use datafusion_physical_expr::PhysicalExpr;++use async_trait::async_trait;use object_store::{GetResultPayload, ObjectMeta, ObjectStore};-use std::any::Any;-use std::io::{Read, Seek};-use std::sync::Arc;/// Arrow `FileFormat` implementation.#[derive(Default, Debug)]@@ -74,10 +78,10 @@ impl FileFormat for ArrowFormat {&self,_state: &SessionState,_store: &Arc<dyn ObjectStore>,- _table_schema: SchemaRef,+ table_schema: SchemaRef,_object: &ObjectMeta,) -> Result<Statistics> {- Ok(Statistics::default())+ Ok(Statistics::new_unknown(&table_schema))}async fn create_physical_plan(diff --git a/datafusion/core/src/datasource/file_format/avro.rs b/datafusion/core/src/datasource/file_format/avro.rsindex cc6a03ba3f..a24a28ad6f 100644--- a/datafusion/core/src/datasource/file_format/avro.rs+++ b/datafusion/core/src/datasource/file_format/avro.rs@@ -74,10 +74,10 @@ impl FileFormat for AvroFormat {&self,_state: &SessionState,_store: &Arc<dyn ObjectStore>,- _table_schema: SchemaRef,+ table_schema: SchemaRef,_object: &ObjectMeta,) -> Result<Statistics> {- Ok(Statistics::default())+ Ok(Statistics::new_unknown(&table_schema))}async fn create_physical_plan(diff --git a/datafusion/core/src/datasource/file_format/csv.rs b/datafusion/core/src/datasource/file_format/csv.rsindex e77382ad9c..41265ede7f 100644--- a/datafusion/core/src/datasource/file_format/csv.rs+++ b/datafusion/core/src/datasource/file_format/csv.rs@@ -23,21 +23,6 @@ use std::fmt;use std::fmt::Debug;use std::sync::Arc;-use arrow::csv::WriterBuilder;-use arrow::datatypes::{DataType, Field, Fields, Schema};-use arrow::{self, datatypes::SchemaRef};-use arrow_array::RecordBatch;-use datafusion_common::{exec_err, not_impl_err, DataFusionError, FileType};-use datafusion_execution::TaskContext;-use datafusion_physical_expr::{PhysicalExpr, PhysicalSortRequirement};--use async_trait::async_trait;-use bytes::{Buf, Bytes};-use datafusion_physical_plan::metrics::MetricsSet;-use futures::stream::BoxStream;-use futures::{pin_mut, Stream, StreamExt, TryStreamExt};-use object_store::{delimited::newline_delimited_stream, ObjectMeta, ObjectStore};-use super::{FileFormat, DEFAULT_SCHEMA_INFER_MAX_RECORD};use crate::datasource::file_format::file_compression_type::FileCompressionType;use crate::datasource::file_format::write::{@@ -51,6 +36,21 @@ use crate::execution::context::SessionState;use crate::physical_plan::insert::{DataSink, FileSinkExec};use crate::physical_plan::{DisplayAs, DisplayFormatType, Statistics};use crate::physical_plan::{ExecutionPlan, SendableRecordBatchStream};++use arrow::csv::WriterBuilder;+use arrow::datatypes::{DataType, Field, Fields, Schema};+use arrow::{self, datatypes::SchemaRef};+use arrow_array::RecordBatch;+use datafusion_common::{exec_err, not_impl_err, DataFusionError, FileType};+use datafusion_execution::TaskContext;+use datafusion_physical_expr::{PhysicalExpr, PhysicalSortRequirement};+use datafusion_physical_plan::metrics::MetricsSet;++use async_trait::async_trait;+use bytes::{Buf, Bytes};+use futures::stream::BoxStream;+use futures::{pin_mut, Stream, StreamExt, TryStreamExt};+use object_store::{delimited::newline_delimited_stream, ObjectMeta, ObjectStore};use rand::distributions::{Alphanumeric, DistString};/// Character Separated Value `FileFormat` implementation.@@ -236,10 +236,10 @@ impl FileFormat for CsvFormat {&self,_state: &SessionState,_store: &Arc<dyn ObjectStore>,- _table_schema: SchemaRef,+ table_schema: SchemaRef,_object: &ObjectMeta,) -> Result<Statistics> {- Ok(Statistics::default())+ Ok(Statistics::new_unknown(&table_schema))}async fn create_physical_plan(@@ -625,6 +625,7 @@ mod tests {use chrono::DateTime;use datafusion_common::cast::as_string_array;use datafusion_common::internal_err;+ use datafusion_common::stats::Precision;use datafusion_common::FileType;use datafusion_common::GetExt;use datafusion_expr::{col, lit};@@ -657,8 +658,8 @@ mod tests {assert_eq!(tt_batches, 50 /* 100/2 */);// test metadata- assert_eq!(exec.statistics().num_rows, None);- assert_eq!(exec.statistics().total_byte_size, None);+ assert_eq!(exec.statistics()?.num_rows, Precision::Absent);+ assert_eq!(exec.statistics()?.total_byte_size, Precision::Absent);Ok(())}diff --git a/datafusion/core/src/datasource/file_format/json.rs b/datafusion/core/src/datasource/file_format/json.rsindex fa8fb5a723..dc5b24b2ea 100644--- a/datafusion/core/src/datasource/file_format/json.rs+++ b/datafusion/core/src/datasource/file_format/json.rs@@ -18,51 +18,38 @@//! Line delimited JSON format abstractionsuse std::any::Any;--use bytes::Bytes;-use datafusion_common::not_impl_err;-use datafusion_common::DataFusionError;-use datafusion_common::FileType;-use datafusion_execution::TaskContext;-use datafusion_physical_expr::PhysicalSortRequirement;-use datafusion_physical_plan::metrics::MetricsSet;-use rand::distributions::Alphanumeric;-use rand::distributions::DistString;use std::fmt;use std::fmt::Debug;use std::io::BufReader;use std::sync::Arc;-use arrow::datatypes::Schema;-use arrow::datatypes::SchemaRef;-use arrow::json;-use arrow::json::reader::infer_json_schema_from_iterator;-use arrow::json::reader::ValueIter;-use arrow_array::RecordBatch;-use async_trait::async_trait;-use bytes::Buf;--use datafusion_physical_expr::PhysicalExpr;-use object_store::{GetResultPayload, ObjectMeta, ObjectStore};--use crate::datasource::physical_plan::FileGroupDisplay;-use crate::physical_plan::insert::DataSink;-use crate::physical_plan::insert::FileSinkExec;-use crate::physical_plan::SendableRecordBatchStream;-use crate::physical_plan::{DisplayAs, DisplayFormatType, Statistics};--use super::FileFormat;-use super::FileScanConfig;+use super::{FileFormat, FileScanConfig};use crate::datasource::file_format::file_compression_type::FileCompressionType;use crate::datasource::file_format::write::{create_writer, stateless_serialize_and_write_files, BatchSerializer, FileWriterMode,};use crate::datasource::file_format::DEFAULT_SCHEMA_INFER_MAX_RECORD;-use crate::datasource::physical_plan::FileSinkConfig;-use crate::datasource::physical_plan::NdJsonExec;+use crate::datasource::physical_plan::{FileGroupDisplay, FileSinkConfig, NdJsonExec};use crate::error::Result;use crate::execution::context::SessionState;-use crate::physical_plan::ExecutionPlan;+use crate::physical_plan::insert::{DataSink, FileSinkExec};+use crate::physical_plan::{+ DisplayAs, DisplayFormatType, ExecutionPlan, SendableRecordBatchStream, Statistics,+};++use arrow::datatypes::{Schema, SchemaRef};+use arrow::json;+use arrow::json::reader::{infer_json_schema_from_iterator, ValueIter};+use arrow_array::RecordBatch;+use datafusion_common::{not_impl_err, DataFusionError, FileType};+use datafusion_execution::TaskContext;+use datafusion_physical_expr::{PhysicalExpr, PhysicalSortRequirement};+use datafusion_physical_plan::metrics::MetricsSet;++use async_trait::async_trait;+use bytes::{Buf, Bytes};+use object_store::{GetResultPayload, ObjectMeta, ObjectStore};+use rand::distributions::{Alphanumeric, DistString};/// New line delimited JSON `FileFormat` implementation.#[derive(Debug)]@@ -154,10 +141,10 @@ impl FileFormat for JsonFormat {&self,_state: &SessionState,_store: &Arc<dyn ObjectStore>,- _table_schema: SchemaRef,+ table_schema: SchemaRef,_object: &ObjectMeta,) -> Result<Statistics> {- Ok(Statistics::default())+ Ok(Statistics::new_unknown(&table_schema))}async fn create_physical_plan(@@ -394,6 +381,7 @@ impl DataSink for JsonSink {mod tests {use super::super::test_util::scan_format;use datafusion_common::cast::as_int64_array;+ use datafusion_common::stats::Precision;use futures::StreamExt;use object_store::local::LocalFileSystem;@@ -424,8 +412,8 @@ mod tests {assert_eq!(tt_batches, 6 /* 12/2 */);// test metadata- assert_eq!(exec.statistics().num_rows, None);- assert_eq!(exec.statistics().total_byte_size, None);+ assert_eq!(exec.statistics()?.num_rows, Precision::Absent);+ assert_eq!(exec.statistics()?.total_byte_size, Precision::Absent);Ok(())}diff --git a/datafusion/core/src/datasource/file_format/parquet.rs b/datafusion/core/src/datasource/file_format/parquet.rsindex d946bfb0b9..859bff7ae4 100644--- a/datafusion/core/src/datasource/file_format/parquet.rs+++ b/datafusion/core/src/datasource/file_format/parquet.rs@@ -17,51 +17,22 @@//! Parquet format abstractions-use datafusion_physical_plan::metrics::MetricsSet;-use parquet::column::writer::ColumnCloseResult;-use parquet::file::writer::SerializedFileWriter;-use rand::distributions::DistString;use std::any::Any;use std::fmt;use std::fmt::Debug;use std::io::Write;use std::sync::Arc;-use tokio::io::{AsyncWrite, AsyncWriteExt};-use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};-use tokio::task::{JoinHandle, JoinSet};--use crate::datasource::file_format::file_compression_type::FileCompressionType;-use arrow::datatypes::SchemaRef;-use arrow::datatypes::{Fields, Schema};-use async_trait::async_trait;-use bytes::{BufMut, BytesMut};-use datafusion_common::{exec_err, not_impl_err, plan_err, DataFusionError, FileType};-use datafusion_execution::TaskContext;-use datafusion_physical_expr::{PhysicalExpr, PhysicalSortRequirement};-use futures::{StreamExt, TryStreamExt};-use hashbrown::HashMap;-use object_store::{ObjectMeta, ObjectStore};-use parquet::arrow::{parquet_to_arrow_schema, AsyncArrowWriter};-use parquet::file::footer::{decode_footer, decode_metadata};-use parquet::file::metadata::ParquetMetaData;-use parquet::file::properties::WriterProperties;-use parquet::file::statistics::Statistics as ParquetStatistics;-use rand::distributions::Alphanumeric;use super::write::{create_writer, AbortableWrite, FileWriterMode};-use super::FileFormat;-use super::FileScanConfig;-use crate::arrow::array::{- BooleanArray, Float32Array, Float64Array, Int32Array, Int64Array,-};-use crate::arrow::datatypes::DataType;-use crate::config::ConfigOptions;+use super::{FileFormat, FileScanConfig};+use crate::config::ConfigOptions;+use crate::datasource::file_format::file_compression_type::FileCompressionType;+use crate::datasource::get_col_stats;use crate::datasource::physical_plan::{FileGroupDisplay, FileMeta, FileSinkConfig, ParquetExec, SchemaAdapter,};--use crate::datasource::{create_max_min_accs, get_col_stats};+use crate::datasource::statistics::create_max_min_accs;use crate::error::Result;use crate::execution::context::SessionState;use crate::physical_plan::expressions::{MaxAccumulator, MinAccumulator};@@ -71,6 +42,31 @@ use crate::physical_plan::{Statistics,};+use arrow::array::{BooleanArray, Float32Array, Float64Array, Int32Array, Int64Array};+use arrow::datatypes::{DataType, Fields, Schema, SchemaRef};+use datafusion_common::stats::Precision;+use datafusion_common::{exec_err, not_impl_err, plan_err, DataFusionError, FileType};+use datafusion_execution::TaskContext;+use datafusion_physical_expr::{PhysicalExpr, PhysicalSortRequirement};+use datafusion_physical_plan::metrics::MetricsSet;++use async_trait::async_trait;+use bytes::{BufMut, BytesMut};+use futures::{StreamExt, TryStreamExt};+use hashbrown::HashMap;+use object_store::{ObjectMeta, ObjectStore};+use parquet::arrow::{parquet_to_arrow_schema, AsyncArrowWriter};+use parquet::column::writer::ColumnCloseResult;+use parquet::file::footer::{decode_footer, decode_metadata};+use parquet::file::metadata::ParquetMetaData;+use parquet::file::properties::WriterProperties;+use parquet::file::statistics::Statistics as ParquetStatistics;+use parquet::file::writer::SerializedFileWriter;+use rand::distributions::{Alphanumeric, DistString};+use tokio::io::{AsyncWrite, AsyncWriteExt};+use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};+use tokio::task::{JoinHandle, JoinSet};+/// The Apache Parquet `FileFormat` implementation////// Note it is recommended these are instead configured on the [`ConfigOptions`]@@ -515,7 +511,7 @@ async fn fetch_statistics(let mut num_rows = 0;let mut total_byte_size = 0;- let mut null_counts = vec![0; num_fields];+ let mut null_counts = vec![Precision::Exact(0); num_fields];let mut has_statistics = false;let schema_adapter = SchemaAdapter::new(table_schema.clone());@@ -541,7 +537,7 @@ async fn fetch_statistics(schema_adapter.map_column_index(table_idx, &file_schema){if let Some((null_count, stats)) = column_stats.get(&file_idx) {- *null_cnt += *null_count as usize;+ *null_cnt = null_cnt.add(&Precision::Exact(*null_count as usize));summarize_min_max(&mut max_values,&mut min_values,@@ -555,28 +551,22 @@ async fn fetch_statistics(min_values[table_idx] = None;}} else {- *null_cnt += num_rows as usize;+ *null_cnt = null_cnt.add(&Precision::Exact(num_rows as usize));}}}}let column_stats = if has_statistics {- Some(get_col_stats(- &table_schema,- null_counts,- &mut max_values,- &mut min_values,- ))+ get_col_stats(&table_schema, null_counts, &mut max_values, &mut min_values)} else {- None+ Statistics::unknown_column(&table_schema)};let statistics = Statistics {- num_rows: Some(num_rows as usize),- total_byte_size: Some(total_byte_size as usize),+ num_rows: Precision::Exact(num_rows as usize),+ total_byte_size: Precision::Exact(total_byte_size as usize),column_statistics: column_stats,- is_exact: true,};Ok(statistics)@@ -1186,20 +1176,26 @@ mod tests {let stats =fetch_statistics(store.as_ref(), schema.clone(), &meta[0], None).await?;- assert_eq!(stats.num_rows, Some(3));- let c1_stats = &stats.column_statistics.as_ref().expect("missing c1 stats")[0];- let c2_stats = &stats.column_statistics.as_ref().expect("missing c2 stats")[1];- assert_eq!(c1_stats.null_count, Some(1));- assert_eq!(c2_stats.null_count, Some(3));+ assert_eq!(stats.num_rows, Precision::Exact(3));+ let c1_stats = &stats.column_statistics[0];+ let c2_stats = &stats.column_statistics[1];+ assert_eq!(c1_stats.null_count, Precision::Exact(1));+ assert_eq!(c2_stats.null_count, Precision::Exact(3));let stats = fetch_statistics(store.as_ref(), schema, &meta[1], None).await?;- assert_eq!(stats.num_rows, Some(3));- let c1_stats = &stats.column_statistics.as_ref().expect("missing c1 stats")[0];- let c2_stats = &stats.column_statistics.as_ref().expect("missing c2 stats")[1];- assert_eq!(c1_stats.null_count, Some(3));- assert_eq!(c2_stats.null_count, Some(1));- assert_eq!(c2_stats.max_value, Some(ScalarValue::Int64(Some(2))));- assert_eq!(c2_stats.min_value, Some(ScalarValue::Int64(Some(1))));+ assert_eq!(stats.num_rows, Precision::Exact(3));+ let c1_stats = &stats.column_statistics[0];+ let c2_stats = &stats.column_statistics[1];+ assert_eq!(c1_stats.null_count, Precision::Exact(3));+ assert_eq!(c2_stats.null_count, Precision::Exact(1));+ assert_eq!(+ c2_stats.max_value,+ Precision::Exact(ScalarValue::Int64(Some(2)))+ );+ assert_eq!(+ c2_stats.min_value,+ Precision::Exact(ScalarValue::Int64(Some(1)))+ );Ok(())}@@ -1335,11 +1331,11 @@ mod tests {fetch_statistics(store.upcast().as_ref(), schema.clone(), &meta[0], Some(9)).await?;- assert_eq!(stats.num_rows, Some(3));- let c1_stats = &stats.column_statistics.as_ref().expect("missing c1 stats")[0];- let c2_stats = &stats.column_statistics.as_ref().expect("missing c2 stats")[1];- assert_eq!(c1_stats.null_count, Some(1));- assert_eq!(c2_stats.null_count, Some(3));+ assert_eq!(stats.num_rows, Precision::Exact(3));+ let c1_stats = &stats.column_statistics[0];+ let c2_stats = &stats.column_statistics[1];+ assert_eq!(c1_stats.null_count, Precision::Exact(1));+ assert_eq!(c2_stats.null_count, Precision::Exact(3));let store = Arc::new(RequestCountingObjectStore::new(Arc::new(LocalFileSystem::new(),@@ -1368,11 +1364,11 @@ mod tests {).await?;- assert_eq!(stats.num_rows, Some(3));- let c1_stats = &stats.column_statistics.as_ref().expect("missing c1 stats")[0];- let c2_stats = &stats.column_statistics.as_ref().expect("missing c2 stats")[1];- assert_eq!(c1_stats.null_count, Some(1));- assert_eq!(c2_stats.null_count, Some(3));+ assert_eq!(stats.num_rows, Precision::Exact(3));+ let c1_stats = &stats.column_statistics[0];+ let c2_stats = &stats.column_statistics[1];+ assert_eq!(c1_stats.null_count, Precision::Exact(1));+ assert_eq!(c2_stats.null_count, Precision::Exact(3));let store = Arc::new(RequestCountingObjectStore::new(Arc::new(LocalFileSystem::new(),@@ -1412,8 +1408,8 @@ mod tests {assert_eq!(tt_batches, 4 /* 8/2 */);// test metadata- assert_eq!(exec.statistics().num_rows, Some(8));- assert_eq!(exec.statistics().total_byte_size, Some(671));+ assert_eq!(exec.statistics()?.num_rows, Precision::Exact(8));+ assert_eq!(exec.statistics()?.total_byte_size, Precision::Exact(671));Ok(())}@@ -1454,9 +1450,8 @@ mod tests {get_exec(&state, "alltypes_plain.parquet", projection, Some(1)).await?;// note: even if the limit is set, the executor rounds up to the batch size- assert_eq!(exec.statistics().num_rows, Some(8));- assert_eq!(exec.statistics().total_byte_size, Some(671));- assert!(exec.statistics().is_exact);+ assert_eq!(exec.statistics()?.num_rows, Precision::Exact(8));+ assert_eq!(exec.statistics()?.total_byte_size, Precision::Exact(671));let batches = collect(exec, task_ctx).await?;assert_eq!(1, batches.len());assert_eq!(11, batches[0].num_columns());diff --git a/datafusion/core/src/datasource/listing/table.rs b/datafusion/core/src/datasource/listing/table.rsindex 6ef214c97f..ca86e3e3c7 100644--- a/datafusion/core/src/datasource/listing/table.rs+++ b/datafusion/core/src/datasource/listing/table.rs@@ -962,7 +962,7 @@ impl ListingTable {let store = if let Some(url) = self.table_paths.get(0) {ctx.runtime_env().object_store(url)?} else {- return Ok((vec![], Statistics::default()));+ return Ok((vec![], Statistics::new_unknown(&self.file_schema)));};// list files (with partitions)let file_list = future::try_join_all(self.table_paths.iter().map(|table_path| {@@ -976,14 +976,12 @@ impl ListingTable {)})).await?;-let file_list = stream::iter(file_list).flatten();-// collect the statistics if required by the configlet files = file_list.map(|part_file| async {let part_file = part_file?;- let mut statistics_result = Statistics::default();+ let mut statistics_result = Statistics::new_unknown(&self.file_schema);if self.options.collect_stat {let statistics_cache = self.collected_statistics.clone();match statistics_cache.get_with_extra(@@ -1051,9 +1049,9 @@ mod tests {use arrow::datatypes::{DataType, Schema};use arrow::record_batch::RecordBatch;+ use datafusion_common::stats::Precision;use datafusion_common::{assert_contains, GetExt, ScalarValue};use datafusion_expr::{BinaryExpr, LogicalPlanBuilder, Operator};-use rstest::*;use tempfile::TempDir;@@ -1100,8 +1098,8 @@ mod tests {assert_eq!(exec.output_partitioning().partition_count(), 1);// test metadata- assert_eq!(exec.statistics().num_rows, Some(8));- assert_eq!(exec.statistics().total_byte_size, Some(671));+ assert_eq!(exec.statistics()?.num_rows, Precision::Exact(8));+ assert_eq!(exec.statistics()?.total_byte_size, Precision::Exact(671));Ok(())}@@ -1123,8 +1121,8 @@ mod tests {let table = ListingTable::try_new(config)?;let exec = table.scan(&state, None, &[], None).await?;- assert_eq!(exec.statistics().num_rows, Some(8));- assert_eq!(exec.statistics().total_byte_size, Some(671));+ assert_eq!(exec.statistics()?.num_rows, Precision::Exact(8));+ assert_eq!(exec.statistics()?.total_byte_size, Precision::Exact(671));Ok(())}@@ -1147,8 +1145,8 @@ mod tests {let table = ListingTable::try_new(config)?;let exec = table.scan(&state, None, &[], None).await?;- assert_eq!(exec.statistics().num_rows, None);- assert_eq!(exec.statistics().total_byte_size, None);+ assert_eq!(exec.statistics()?.num_rows, Precision::Absent);+ assert_eq!(exec.statistics()?.total_byte_size, Precision::Absent);Ok(())}diff --git a/datafusion/core/src/datasource/mod.rs b/datafusion/core/src/datasource/mod.rsindex 35f5653651..455818056f 100644--- a/datafusion/core/src/datasource/mod.rs+++ b/datafusion/core/src/datasource/mod.rs@@ -42,5 +42,5 @@ pub use self::memory::MemTable;pub use self::provider::TableProvider;pub use self::view::ViewTable;pub use crate::logical_expr::TableType;+pub(crate) use statistics::get_col_stats;pub use statistics::get_statistics_with_limit;-pub(crate) use statistics::{create_max_min_accs, get_col_stats};diff --git a/datafusion/core/src/datasource/physical_plan/arrow_file.rs b/datafusion/core/src/datasource/physical_plan/arrow_file.rsindex a47376248e..e00e8aea0a 100644--- a/datafusion/core/src/datasource/physical_plan/arrow_file.rs+++ b/datafusion/core/src/datasource/physical_plan/arrow_file.rs@@ -16,6 +16,10 @@// under the License.//! Execution plan for reading Arrow files++use std::any::Any;+use std::sync::Arc;+use crate::datasource::physical_plan::{FileMeta, FileOpenFuture, FileOpener, FileScanConfig,};@@ -24,6 +28,7 @@ use crate::physical_plan::metrics::{ExecutionPlanMetricsSet, MetricsSet};use crate::physical_plan::{DisplayAs, DisplayFormatType, ExecutionPlan, Partitioning, SendableRecordBatchStream,};+use arrow_schema::SchemaRef;use datafusion_common::Statistics;use datafusion_execution::TaskContext;@@ -31,10 +36,9 @@ use datafusion_physical_expr::{ordering_equivalence_properties_helper, LexOrdering, OrderingEquivalenceProperties,PhysicalSortExpr,};+use futures::StreamExt;use object_store::{GetResultPayload, ObjectStore};-use std::any::Any;-use std::sync::Arc;/// Execution plan for scanning Arrow data source#[derive(Debug, Clone)]@@ -143,8 +147,8 @@ impl ExecutionPlan for ArrowExec {Some(self.metrics.clone_inner())}- fn statistics(&self) -> Statistics {- self.projected_statistics.clone()+ fn statistics(&self) -> Result<Statistics> {+ Ok(self.projected_statistics.clone())}}diff --git a/datafusion/core/src/datasource/physical_plan/avro.rs b/datafusion/core/src/datasource/physical_plan/avro.rsindex 93655e8665..f08bc9b8df 100644--- a/datafusion/core/src/datasource/physical_plan/avro.rs+++ b/datafusion/core/src/datasource/physical_plan/avro.rs@@ -16,6 +16,11 @@// under the License.//! Execution plan for reading line-delimited Avro files++use std::any::Any;+use std::sync::Arc;++use super::FileScanConfig;use crate::error::Result;use crate::physical_plan::expressions::PhysicalSortExpr;use crate::physical_plan::metrics::{ExecutionPlanMetricsSet, MetricsSet};@@ -23,18 +28,13 @@ use crate::physical_plan::{DisplayAs, DisplayFormatType, ExecutionPlan, Partitioning, SendableRecordBatchStream,Statistics,};-use datafusion_execution::TaskContext;use arrow::datatypes::SchemaRef;+use datafusion_execution::TaskContext;use datafusion_physical_expr::{ordering_equivalence_properties_helper, LexOrdering, OrderingEquivalenceProperties,};-use std::any::Any;-use std::sync::Arc;--use super::FileScanConfig;-/// Execution plan for scanning Avro data source#[derive(Debug, Clone)]#[allow(dead_code)]@@ -154,8 +154,8 @@ impl ExecutionPlan for AvroExec {Ok(Box::pin(stream))}- fn statistics(&self) -> Statistics {- self.projected_statistics.clone()+ fn statistics(&self) -> Result<Statistics> {+ Ok(self.projected_statistics.clone())}fn metrics(&self) -> Option<MetricsSet> {@@ -272,8 +272,8 @@ mod tests {let avro_exec = AvroExec::new(FileScanConfig {object_store_url: ObjectStoreUrl::local_filesystem(),file_groups: vec![vec![meta.into()]],+ statistics: Statistics::new_unknown(&file_schema),file_schema,- statistics: Statistics::default(),projection: Some(vec![0, 1, 2]),limit: None,table_partition_cols: vec![],@@ -344,8 +344,8 @@ mod tests {let avro_exec = AvroExec::new(FileScanConfig {object_store_url,file_groups: vec![vec![meta.into()]],+ statistics: Statistics::new_unknown(&file_schema),file_schema,- statistics: Statistics::default(),projection,limit: None,table_partition_cols: vec![],@@ -417,8 +417,8 @@ mod tests {projection: Some(vec![0, 1, file_schema.fields().len(), 2]),object_store_url,file_groups: vec![vec![partitioned_file]],+ statistics: Statistics::new_unknown(&file_schema),file_schema,- statistics: Statistics::default(),limit: None,table_partition_cols: vec![("date".to_owned(), DataType::Utf8)],output_ordering: vec![],diff --git a/datafusion/core/src/datasource/physical_plan/csv.rs b/datafusion/core/src/datasource/physical_plan/csv.rsindex 8277836a24..f3b2fa9de7 100644--- a/datafusion/core/src/datasource/physical_plan/csv.rs+++ b/datafusion/core/src/datasource/physical_plan/csv.rs@@ -17,6 +17,13 @@//! Execution plan for reading CSV files+use std::any::Any;+use std::io::{Read, Seek, SeekFrom};+use std::ops::Range;+use std::sync::Arc;+use std::task::Poll;++use super::FileScanConfig;use crate::datasource::file_format::file_compression_type::FileCompressionType;use crate::datasource::listing::{FileRange, ListingTableUrl};use crate::datasource::physical_plan::file_stream::{@@ -30,25 +37,18 @@ use crate::physical_plan::{DisplayAs, DisplayFormatType, ExecutionPlan, Partitioning, SendableRecordBatchStream,Statistics,};+use arrow::csv;use arrow::datatypes::SchemaRef;use datafusion_execution::TaskContext;use datafusion_physical_expr::{ordering_equivalence_properties_helper, LexOrdering, OrderingEquivalenceProperties,};-use tokio::io::AsyncWriteExt;--use super::FileScanConfig;use bytes::{Buf, Bytes};-use futures::ready;-use futures::{StreamExt, TryStreamExt};+use futures::{ready, StreamExt, TryStreamExt};use object_store::{GetOptions, GetResultPayload, ObjectStore};-use std::any::Any;-use std::io::{Read, Seek, SeekFrom};-use std::ops::Range;-use std::sync::Arc;-use std::task::Poll;+use tokio::io::AsyncWriteExt;use tokio::task::JoinSet;/// Execution plan for scanning a CSV file@@ -234,8 +234,8 @@ impl ExecutionPlan for CsvExec {Ok(Box::pin(stream) as SendableRecordBatchStream)}- fn statistics(&self) -> Statistics {- self.projected_statistics.clone()+ fn statistics(&self) -> Result<Statistics> {+ Ok(self.projected_statistics.clone())}fn metrics(&self) -> Option<MetricsSet> {diff --git a/datafusion/core/src/datasource/physical_plan/file_scan_config.rs b/datafusion/core/src/datasource/physical_plan/file_scan_config.rsindex 819bfabae2..c1a19b745b 100644--- a/datafusion/core/src/datasource/physical_plan/file_scan_config.rs+++ b/datafusion/core/src/datasource/physical_plan/file_scan_config.rs@@ -18,6 +18,12 @@//! [`FileScanConfig`] to configure scanning of possibly partitioned//! file sources.+use std::{+ borrow::Cow, cmp::min, collections::HashMap, fmt::Debug, marker::PhantomData,+ sync::Arc, vec,+};++use super::get_projected_output_ordering;use crate::datasource::{listing::{FileRange, PartitionedFile},object_store::ObjectStoreUrl,@@ -32,18 +38,12 @@ use arrow::buffer::Buffer;use arrow::datatypes::{ArrowNativeType, UInt16Type};use arrow_array::{ArrayRef, DictionaryArray, RecordBatch};use arrow_schema::{DataType, Field, Schema, SchemaRef};-use datafusion_common::exec_err;-use datafusion_common::{ColumnStatistics, Statistics};+use datafusion_common::stats::Precision;+use datafusion_common::{exec_err, ColumnStatistics, Statistics};use datafusion_physical_expr::LexOrdering;use itertools::Itertools;use log::warn;-use std::{- borrow::Cow, cmp::min, collections::HashMap, fmt::Debug, marker::PhantomData,- sync::Arc, vec,-};--use super::get_projected_output_ordering;/// Convert type to a type suitable for use as a [`ListingTable`]/// partition column. Returns `Dictionary(UInt16, val_type)`, which is@@ -130,30 +130,23 @@ impl FileScanConfig {let mut table_cols_stats = vec![];for idx in proj_iter {if idx < self.file_schema.fields().len() {- table_fields.push(self.file_schema.field(idx).clone());- if let Some(file_cols_stats) = &self.statistics.column_statistics {- table_cols_stats.push(file_cols_stats[idx].clone())- } else {- table_cols_stats.push(ColumnStatistics::default())- }+ let field = self.file_schema.field(idx);+ table_fields.push(field.clone());+ table_cols_stats.push(self.statistics.column_statistics[idx].clone())} else {let partition_idx = idx - self.file_schema.fields().len();- table_fields.push(Field::new(- &self.table_partition_cols[partition_idx].0,- self.table_partition_cols[partition_idx].1.to_owned(),- false,- ));+ let (name, dtype) = &self.table_partition_cols[partition_idx];+ table_fields.push(Field::new(name, dtype.to_owned(), false));// TODO provide accurate stat for partition column (#1186)- table_cols_stats.push(ColumnStatistics::default())+ table_cols_stats.push(ColumnStatistics::new_unknown())}}let table_stats = Statistics {- num_rows: self.statistics.num_rows,- is_exact: self.statistics.is_exact,+ num_rows: self.statistics.num_rows.clone(),// TODO correct byte size?- total_byte_size: None,- column_statistics: Some(table_cols_stats),+ total_byte_size: Precision::Absent,+ column_statistics: table_cols_stats,};let table_schema = Arc::new(@@ -507,7 +500,7 @@ mod tests {let conf = config_for_projection(Arc::clone(&file_schema),None,- Statistics::default(),+ Statistics::new_unknown(&file_schema),vec![("date".to_owned(),wrap_partition_type_in_dict(DataType::Utf8),@@ -522,10 +515,7 @@ mod tests {"partition columns are the last columns");assert_eq!(- proj_statistics- .column_statistics- .expect("projection creates column statistics")- .len(),+ proj_statistics.column_statistics.len(),file_schema.fields().len() + 1);// TODO implement tests for partition column statistics once implemented@@ -544,18 +534,16 @@ mod tests {Arc::clone(&file_schema),Some(vec![file_schema.fields().len(), 0]),Statistics {- num_rows: Some(10),+ num_rows: Precision::Inexact(10),// assign the column index to distinct_count to help assert// the source statistic after the projection- column_statistics: Some(- (0..file_schema.fields().len())- .map(|i| ColumnStatistics {- distinct_count: Some(i),- ..Default::default()- })- .collect(),- ),- ..Default::default()+ column_statistics: (0..file_schema.fields().len())+ .map(|i| ColumnStatistics {+ distinct_count: Precision::Inexact(i),+ ..Default::default()+ })+ .collect(),+ total_byte_size: Precision::Absent,},vec![("date".to_owned(),@@ -568,13 +556,11 @@ mod tests {columns(&proj_schema),vec!["date".to_owned(), "c1".to_owned()]);- let proj_stat_cols = proj_statistics- .column_statistics- .expect("projection creates column statistics");+ let proj_stat_cols = proj_statistics.column_statistics;assert_eq!(proj_stat_cols.len(), 2);// TODO implement tests for proj_stat_cols[0] once partition column// statistics are implemented- assert_eq!(proj_stat_cols[1].distinct_count, Some(0));+ assert_eq!(proj_stat_cols[1].distinct_count, Precision::Inexact(0));let col_names = conf.projected_file_column_names();assert_eq!(col_names, Some(vec!["c1".to_owned()]));@@ -615,7 +601,7 @@ mod tests {file_batch.schema().fields().len(),file_batch.schema().fields().len() + 2,]),- Statistics::default(),+ Statistics::new_unknown(&file_batch.schema()),partition_cols.clone(),);let (proj_schema, ..) = conf.project();diff --git a/datafusion/core/src/datasource/physical_plan/file_stream.rs b/datafusion/core/src/datasource/physical_plan/file_stream.rsindex af304f40dd..487d17d9c1 100644--- a/datafusion/core/src/datasource/physical_plan/file_stream.rs+++ b/datafusion/core/src/datasource/physical_plan/file_stream.rs@@ -521,6 +521,7 @@ mod tests {use arrow_schema::Schema;use datafusion_common::internal_err;use datafusion_common::DataFusionError;+ use datafusion_common::Statistics;use super::*;use crate::datasource::file_format::write::BatchSerializer;@@ -659,9 +660,9 @@ mod tests {let config = FileScanConfig {object_store_url: ObjectStoreUrl::parse("test:///").unwrap(),+ statistics: Statistics::new_unknown(&file_schema),file_schema,file_groups: vec![file_group],- statistics: Default::default(),projection: None,limit: self.limit,table_partition_cols: vec![],diff --git a/datafusion/core/src/datasource/physical_plan/json.rs b/datafusion/core/src/datasource/physical_plan/json.rsindex 4931104041..1ba8e47a52 100644--- a/datafusion/core/src/datasource/physical_plan/json.rs+++ b/datafusion/core/src/datasource/physical_plan/json.rs@@ -16,6 +16,13 @@// under the License.//! Execution plan for reading line-delimited JSON files++use std::any::Any;+use std::io::BufReader;+use std::sync::Arc;+use std::task::Poll;++use super::FileScanConfig;use crate::datasource::file_format::file_compression_type::FileCompressionType;use crate::datasource::listing::ListingTableUrl;use crate::datasource::physical_plan::file_stream::{@@ -29,10 +36,10 @@ use crate::physical_plan::{DisplayAs, DisplayFormatType, ExecutionPlan, Partitioning, SendableRecordBatchStream,Statistics,};-use datafusion_execution::TaskContext;use arrow::json::ReaderBuilder;use arrow::{datatypes::SchemaRef, json};+use datafusion_execution::TaskContext;use datafusion_physical_expr::{ordering_equivalence_properties_helper, LexOrdering, OrderingEquivalenceProperties,};@@ -41,15 +48,9 @@ use bytes::{Buf, Bytes};use futures::{ready, stream, StreamExt, TryStreamExt};use object_store;use object_store::{GetResultPayload, ObjectStore};-use std::any::Any;-use std::io::BufReader;-use std::sync::Arc;-use std::task::Poll;use tokio::io::AsyncWriteExt;use tokio::task::JoinSet;-use super::FileScanConfig;-/// Execution plan for scanning NdJson data source#[derive(Debug, Clone)]pub struct NdJsonExec {@@ -163,8 +164,8 @@ impl ExecutionPlan for NdJsonExec {Ok(Box::pin(stream) as SendableRecordBatchStream)}- fn statistics(&self) -> Statistics {- self.projected_statistics.clone()+ fn statistics(&self) -> Result<Statistics> {+ Ok(self.projected_statistics.clone())}fn metrics(&self) -> Option<MetricsSet> {@@ -457,8 +458,8 @@ mod tests {FileScanConfig {object_store_url,file_groups,+ statistics: Statistics::new_unknown(&file_schema),file_schema,- statistics: Statistics::default(),projection: None,limit: Some(3),table_partition_cols: vec![],@@ -536,8 +537,8 @@ mod tests {FileScanConfig {object_store_url,file_groups,+ statistics: Statistics::new_unknown(&file_schema),file_schema,- statistics: Statistics::default(),projection: None,limit: Some(3),table_partition_cols: vec![],@@ -584,8 +585,8 @@ mod tests {FileScanConfig {object_store_url,file_groups,+ statistics: Statistics::new_unknown(&file_schema),file_schema,- statistics: Statistics::default(),projection: Some(vec![0, 2]),limit: None,table_partition_cols: vec![],@@ -637,8 +638,8 @@ mod tests {FileScanConfig {object_store_url,file_groups,+ statistics: Statistics::new_unknown(&file_schema),file_schema,- statistics: Statistics::default(),projection: Some(vec![3, 0, 2]),limit: None,table_partition_cols: vec![],diff --git a/datafusion/core/src/datasource/physical_plan/mod.rs b/datafusion/core/src/datasource/physical_plan/mod.rsindex d8ae6b3c04..57844aac51 100644--- a/datafusion/core/src/datasource/physical_plan/mod.rs+++ b/datafusion/core/src/datasource/physical_plan/mod.rs@@ -20,32 +20,33 @@mod arrow_file;mod avro;mod csv;+mod file_scan_config;mod file_stream;mod json;pub mod parquet;pub(crate) use self::csv::plan_to_csv;pub use self::csv::{CsvConfig, CsvExec, CsvOpener};+pub(crate) use self::file_scan_config::PartitionColumnProjector;+pub(crate) use self::json::plan_to_json;pub(crate) use self::parquet::plan_to_parquet;pub use self::parquet::{ParquetExec, ParquetFileMetrics, ParquetFileReaderFactory};-use arrow::{- array::new_null_array,- compute::can_cast_types,- datatypes::{DataType, Schema, SchemaRef},- record_batch::{RecordBatch, RecordBatchOptions},-};+pub use arrow_file::ArrowExec;pub use avro::AvroExec;-use datafusion_physical_expr::PhysicalSortExpr;-pub use file_stream::{FileOpenFuture, FileOpener, FileStream, OnError};-pub(crate) use json::plan_to_json;-pub use json::{JsonOpener, NdJsonExec};-mod file_scan_config;-pub(crate) use file_scan_config::PartitionColumnProjector;pub use file_scan_config::{wrap_partition_type_in_dict, wrap_partition_value_in_dict, FileScanConfig,};+pub use file_stream::{FileOpenFuture, FileOpener, FileStream, OnError};+pub use json::{JsonOpener, NdJsonExec};+use std::{+ fmt::{Debug, Formatter, Result as FmtResult},+ sync::Arc,+ vec,+};++use super::listing::ListingTableUrl;use crate::error::{DataFusionError, Result};use crate::{datasource::file_format::write::FileWriterMode,@@ -59,21 +60,20 @@ use crate::{physical_plan::display::{OutputOrderingDisplay, ProjectSchemaDisplay},};+use arrow::{+ array::new_null_array,+ compute::{can_cast_types, cast},+ datatypes::{DataType, Schema, SchemaRef},+ record_batch::{RecordBatch, RecordBatchOptions},+};use datafusion_common::{file_options::FileTypeWriterOptions, plan_err};use datafusion_physical_expr::expressions::Column;--use arrow::compute::cast;+use datafusion_physical_expr::PhysicalSortExpr;use datafusion_physical_plan::ExecutionPlan;+use log::debug;use object_store::path::Path;use object_store::ObjectMeta;-use std::{- fmt::{Debug, Formatter, Result as FmtResult},- sync::Arc,- vec,-};--use super::listing::ListingTableUrl;/// The base configurations to provide when creating a physical plan for/// writing to any given file format.@@ -815,7 +815,7 @@ mod tests {object_store_url: ObjectStoreUrl::local_filesystem(),file_groups: file_group,file_schema: Arc::new(Schema::empty()),- statistics: Statistics::default(),+ statistics: Statistics::new_unknown(&Schema::empty()),projection: None,limit: None,table_partition_cols: vec![],@@ -878,7 +878,9 @@ mod tests {object_store_url: ObjectStoreUrl::local_filesystem(),file_groups: fg.clone(),file_schema: Arc::new(Schema::empty()),- statistics: Statistics::default(),+ statistics: Statistics::new_unknown(&Arc::new(+ Schema::empty(),+ )),projection: None,limit: None,table_partition_cols: vec![],@@ -912,7 +914,7 @@ mod tests {object_store_url: ObjectStoreUrl::local_filesystem(),file_groups: single_partition,file_schema: Arc::new(Schema::empty()),- statistics: Statistics::default(),+ statistics: Statistics::new_unknown(&Schema::empty()),projection: None,limit: None,table_partition_cols: vec![],@@ -949,7 +951,7 @@ mod tests {object_store_url: ObjectStoreUrl::local_filesystem(),file_groups: single_partition,file_schema: Arc::new(Schema::empty()),- statistics: Statistics::default(),+ statistics: Statistics::new_unknown(&Schema::empty()),projection: None,limit: None,table_partition_cols: vec![],@@ -992,7 +994,7 @@ mod tests {object_store_url: ObjectStoreUrl::local_filesystem(),file_groups: source_partitions,file_schema: Arc::new(Schema::empty()),- statistics: Statistics::default(),+ statistics: Statistics::new_unknown(&Schema::empty()),projection: None,limit: None,table_partition_cols: vec![],@@ -1031,7 +1033,7 @@ mod tests {object_store_url: ObjectStoreUrl::local_filesystem(),file_groups: source_partitions,file_schema: Arc::new(Schema::empty()),- statistics: Statistics::default(),+ statistics: Statistics::new_unknown(&Schema::empty()),projection: None,limit: None,table_partition_cols: vec![],@@ -1071,7 +1073,7 @@ mod tests {object_store_url: ObjectStoreUrl::local_filesystem(),file_groups: source_partitions,file_schema: Arc::new(Schema::empty()),- statistics: Statistics::default(),+ statistics: Statistics::new_unknown(&Schema::empty()),projection: None,limit: None,table_partition_cols: vec![],@@ -1100,7 +1102,7 @@ mod tests {object_store_url: ObjectStoreUrl::local_filesystem(),file_groups: single_partition,file_schema: Arc::new(Schema::empty()),- statistics: Statistics::default(),+ statistics: Statistics::new_unknown(&Schema::empty()),projection: None,limit: None,table_partition_cols: vec![],diff --git a/datafusion/core/src/datasource/physical_plan/parquet.rs b/datafusion/core/src/datasource/physical_plan/parquet.rsindex 6f27acfe7f..c781ad81c1 100644--- a/datafusion/core/src/datasource/physical_plan/parquet.rs+++ b/datafusion/core/src/datasource/physical_plan/parquet.rs@@ -17,6 +17,11 @@//! Execution plan for reading Parquet files+use std::any::Any;+use std::fmt::Debug;+use std::ops::Range;+use std::sync::Arc;+use crate::datasource::physical_plan::file_stream::{FileOpenFuture, FileOpener, FileStream,};@@ -36,27 +41,19 @@ use crate::{Statistics,},};-use datafusion_physical_expr::{- ordering_equivalence_properties_helper, PhysicalSortExpr,-};-use fmt::Debug;-use object_store::path::Path;-use std::any::Any;-use std::fmt;-use std::ops::Range;-use std::sync::Arc;-use tokio::task::JoinSet;use arrow::datatypes::{DataType, SchemaRef};use arrow::error::ArrowError;use datafusion_physical_expr::{- LexOrdering, OrderingEquivalenceProperties, PhysicalExpr,+ ordering_equivalence_properties_helper, LexOrdering, OrderingEquivalenceProperties,+ PhysicalExpr, PhysicalSortExpr,};use bytes::Bytes;use futures::future::BoxFuture;use futures::{StreamExt, TryStreamExt};use log::debug;+use object_store::path::Path;use object_store::ObjectStore;use parquet::arrow::arrow_reader::ArrowReaderOptions;use parquet::arrow::async_reader::{AsyncFileReader, ParquetObjectReader};@@ -64,6 +61,7 @@ use parquet::arrow::{AsyncArrowWriter, ParquetRecordBatchStreamBuilder, Projectiuse parquet::basic::{ConvertedType, LogicalType};use parquet::file::{metadata::ParquetMetaData, properties::WriterProperties};use parquet::schema::types::ColumnDescriptor;+use tokio::task::JoinSet;mod metrics;pub mod page_filter;@@ -387,8 +385,8 @@ impl ExecutionPlan for ParquetExec {Some(self.metrics.clone_inner())}- fn statistics(&self) -> Statistics {- self.projected_statistics.clone()+ fn statistics(&self) -> Result<Statistics> {+ Ok(self.projected_statistics.clone())}}@@ -860,8 +858,8 @@ mod tests {FileScanConfig {object_store_url: ObjectStoreUrl::local_filesystem(),file_groups: vec![file_groups],+ statistics: Statistics::new_unknown(&file_schema),file_schema,- statistics: Statistics::default(),projection,limit: None,table_partition_cols: vec![],@@ -1517,8 +1515,8 @@ mod tests {FileScanConfig {object_store_url: ObjectStoreUrl::local_filesystem(),file_groups,+ statistics: Statistics::new_unknown(&file_schema),file_schema,- statistics: Statistics::default(),projection: None,limit: None,table_partition_cols: vec![],@@ -1620,8 +1618,8 @@ mod tests {FileScanConfig {object_store_url,file_groups: vec![vec![partitioned_file]],- file_schema: schema,- statistics: Statistics::default(),+ file_schema: schema.clone(),+ statistics: Statistics::new_unknown(&schema),// file has 10 cols so index 12 should be month and 13 should be dayprojection: Some(vec![0, 1, 2, 12, 13]),limit: None,@@ -1695,7 +1693,7 @@ mod tests {object_store_url: ObjectStoreUrl::local_filesystem(),file_groups: vec![vec![partitioned_file]],file_schema: Arc::new(Schema::empty()),- statistics: Statistics::default(),+ statistics: Statistics::new_unknown(&Schema::empty()),projection: None,limit: None,table_partition_cols: vec![],diff --git a/datafusion/core/src/datasource/statistics.rs b/datafusion/core/src/datasource/statistics.rsindex 1b6a03e15c..3d8248dfde 100644--- a/datafusion/core/src/datasource/statistics.rs+++ b/datafusion/core/src/datasource/statistics.rs@@ -15,14 +15,18 @@// specific language governing permissions and limitations// under the License.+use super::listing::PartitionedFile;use crate::arrow::datatypes::{Schema, SchemaRef};use crate::error::Result;use crate::physical_plan::expressions::{MaxAccumulator, MinAccumulator};use crate::physical_plan::{Accumulator, ColumnStatistics, Statistics};-use futures::Stream;-use futures::StreamExt;-use super::listing::PartitionedFile;+use datafusion_common::stats::Precision;+use datafusion_common::ScalarValue;++use futures::{Stream, StreamExt};+use itertools::izip;+use itertools::multiunzip;/// Get all files as well as the file level summary statistics (no statistic for partition columns)./// If the optional `limit` is provided, includes only sufficient files.@@ -33,101 +37,105 @@ pub async fn get_statistics_with_limit(limit: Option<usize>,) -> Result<(Vec<PartitionedFile>, Statistics)> {let mut result_files = vec![];+ // These statistics can be calculated as long as at least one file provides+ // useful information. If none of the files provides any information, then+ // they will end up having `Precision::Absent` values. Throughout calculations,+ // missing values will be imputed as:+ // - zero for summations, and+ // - neutral element for extreme points.+ let size = file_schema.fields().len();+ let mut null_counts: Vec<Precision<usize>> = vec![Precision::Absent; size];+ let mut max_values: Vec<Precision<ScalarValue>> = vec![Precision::Absent; size];+ let mut min_values: Vec<Precision<ScalarValue>> = vec![Precision::Absent; size];+ let mut num_rows = Precision::<usize>::Absent;+ let mut total_byte_size = Precision::<usize>::Absent;- let mut null_counts = vec![0; file_schema.fields().len()];- let mut has_statistics = false;- let (mut max_values, mut min_values) = create_max_min_accs(&file_schema);-- let mut is_exact = true;-- // The number of rows and the total byte size can be calculated as long as- // at least one file has them. If none of the files provide them, then they- // will be omitted from the statistics. The missing values will be counted- // as zero.- let mut num_rows = None;- let mut total_byte_size = None;-- // fusing the stream allows us to call next safely even once it is finished+ // Fusing the stream allows us to call next safely even once it is finished.let mut all_files = Box::pin(all_files.fuse());- while let Some(res) = all_files.next().await {- let (file, file_stats) = res?;++ if let Some(first_file) = all_files.next().await {+ let (file, file_stats) = first_file?;result_files.push(file);- is_exact &= file_stats.is_exact;- num_rows = if let Some(num_rows) = num_rows {- Some(num_rows + file_stats.num_rows.unwrap_or(0))- } else {- file_stats.num_rows- };- total_byte_size = if let Some(total_byte_size) = total_byte_size {- Some(total_byte_size + file_stats.total_byte_size.unwrap_or(0))- } else {- file_stats.total_byte_size- };- if let Some(vec) = &file_stats.column_statistics {- has_statistics = true;- for (i, cs) in vec.iter().enumerate() {- null_counts[i] += cs.null_count.unwrap_or(0);-- if let Some(max_value) = &mut max_values[i] {- if let Some(file_max) = cs.max_value.clone() {- match max_value.update_batch(&[file_max.to_array()]) {- Ok(_) => {}- Err(_) => {- max_values[i] = None;- }- }- } else {- max_values[i] = None;- }- }- if let Some(min_value) = &mut min_values[i] {- if let Some(file_min) = cs.min_value.clone() {- match min_value.update_batch(&[file_min.to_array()]) {- Ok(_) => {}- Err(_) => {- min_values[i] = None;- }- }- } else {- min_values[i] = None;- }- }- }+ // First file, we set them directly from the file statistics.+ num_rows = file_stats.num_rows;+ total_byte_size = file_stats.total_byte_size;+ for (index, file_column) in file_stats.column_statistics.into_iter().enumerate() {+ null_counts[index] = file_column.null_count;+ max_values[index] = file_column.max_value;+ min_values[index] = file_column.min_value;}// If the number of rows exceeds the limit, we can stop processing// files. This only applies when we know the number of rows. It also// currently ignores tables that have no statistics regarding the// number of rows.- if num_rows.unwrap_or(usize::MIN) > limit.unwrap_or(usize::MAX) {- break;- }- }- // if we still have files in the stream, it means that the limit kicked- // in and that the statistic could have been different if we processed- // the files in a different order.- if all_files.next().await.is_some() {- is_exact = false;- }+ if num_rows.get_value().unwrap_or(&usize::MIN) <= &limit.unwrap_or(usize::MAX) {+ while let Some(current) = all_files.next().await {+ let (file, file_stats) = current?;+ result_files.push(file);++ // We accumulate the number of rows, total byte size and null+ // counts across all the files in question. If any file does not+ // provide any information or provides an inexact value, we demote+ // the statistic precision to inexact.+ num_rows = add_row_stats(file_stats.num_rows, num_rows);- let column_stats = if has_statistics {- Some(get_col_stats(- &file_schema,- null_counts,- &mut max_values,- &mut min_values,- ))- } else {- None+ total_byte_size =+ add_row_stats(file_stats.total_byte_size, total_byte_size);++ (null_counts, max_values, min_values) = multiunzip(+ izip!(+ file_stats.column_statistics.into_iter(),+ null_counts.into_iter(),+ max_values.into_iter(),+ min_values.into_iter()+ )+ .map(+ |(+ ColumnStatistics {+ null_count: file_nc,+ max_value: file_max,+ min_value: file_min,+ distinct_count: _,+ },+ null_count,+ max_value,+ min_value,+ )| {+ (+ add_row_stats(file_nc, null_count),+ set_max_if_greater(file_max, max_value),+ set_min_if_lesser(file_min, min_value),+ )+ },+ ),+ );++ // If the number of rows exceeds the limit, we can stop processing+ // files. This only applies when we know the number of rows. It also+ // currently ignores tables that have no statistics regarding the+ // number of rows.+ if num_rows.get_value().unwrap_or(&usize::MIN)+ > &limit.unwrap_or(usize::MAX)+ {+ break;+ }+ }+ }};- let statistics = Statistics {+ let mut statistics = Statistics {num_rows,total_byte_size,- column_statistics: column_stats,- is_exact,+ column_statistics: get_col_stats_vec(null_counts, max_values, min_values),};+ if all_files.next().await.is_some() {+ // If we still have files in the stream, it means that the limit kicked+ // in, and the statistic could have been different had we processed the+ // files in a different order.+ statistics = statistics.into_inexact()+ }Ok((result_files, statistics))}@@ -139,18 +147,44 @@ pub(crate) fn create_max_min_accs(.fields().iter().map(|field| MaxAccumulator::try_new(field.data_type()).ok())- .collect::<Vec<_>>();+ .collect();let min_values: Vec<Option<MinAccumulator>> = schema.fields().iter().map(|field| MinAccumulator::try_new(field.data_type()).ok())- .collect::<Vec<_>>();+ .collect();(max_values, min_values)}+fn add_row_stats(+ file_num_rows: Precision<usize>,+ num_rows: Precision<usize>,+) -> Precision<usize> {+ match (file_num_rows, &num_rows) {+ (Precision::Absent, _) => num_rows.to_inexact(),+ (lhs, Precision::Absent) => lhs.to_inexact(),+ (lhs, rhs) => lhs.add(rhs),+ }+}++pub(crate) fn get_col_stats_vec(+ null_counts: Vec<Precision<usize>>,+ max_values: Vec<Precision<ScalarValue>>,+ min_values: Vec<Precision<ScalarValue>>,+) -> Vec<ColumnStatistics> {+ izip!(null_counts, max_values, min_values)+ .map(|(null_count, max_value, min_value)| ColumnStatistics {+ null_count,+ max_value,+ min_value,+ distinct_count: Precision::Absent,+ })+ .collect()+}+pub(crate) fn get_col_stats(schema: &Schema,- null_counts: Vec<usize>,+ null_counts: Vec<Precision<usize>>,max_values: &mut [Option<MaxAccumulator>],min_values: &mut [Option<MinAccumulator>],) -> Vec<ColumnStatistics> {@@ -165,11 +199,57 @@ pub(crate) fn get_col_stats(None => None,};ColumnStatistics {- null_count: Some(null_counts[i]),- max_value,- min_value,- distinct_count: None,+ null_count: null_counts[i].clone(),+ max_value: max_value.map(Precision::Exact).unwrap_or(Precision::Absent),+ min_value: min_value.map(Precision::Exact).unwrap_or(Precision::Absent),+ distinct_count: Precision::Absent,}}).collect()}++/// If the given value is numerically greater than the original maximum value,+/// return the new maximum value with appropriate exactness information.+fn set_max_if_greater(+ max_nominee: Precision<ScalarValue>,+ max_values: Precision<ScalarValue>,+) -> Precision<ScalarValue> {+ match (&max_values, &max_nominee) {+ (Precision::Exact(val1), Precision::Exact(val2)) if val1 < val2 => max_nominee,+ (Precision::Exact(val1), Precision::Inexact(val2))+ | (Precision::Inexact(val1), Precision::Inexact(val2))+ | (Precision::Inexact(val1), Precision::Exact(val2))+ if val1 < val2 =>+ {+ max_nominee.to_inexact()+ }+ (Precision::Exact(_), Precision::Absent) => max_values.to_inexact(),+ (Precision::Absent, Precision::Exact(_)) => max_nominee.to_inexact(),+ (Precision::Absent, Precision::Inexact(_)) => max_nominee,+ (Precision::Absent, Precision::Absent) => Precision::Absent,+ _ => max_values,+ }+}++/// If the given value is numerically lesser than the original minimum value,+/// return the new minimum value with appropriate exactness information.+fn set_min_if_lesser(+ min_nominee: Precision<ScalarValue>,+ min_values: Precision<ScalarValue>,+) -> Precision<ScalarValue> {+ match (&min_values, &min_nominee) {+ (Precision::Exact(val1), Precision::Exact(val2)) if val1 > val2 => min_nominee,+ (Precision::Exact(val1), Precision::Inexact(val2))+ | (Precision::Inexact(val1), Precision::Inexact(val2))+ | (Precision::Inexact(val1), Precision::Exact(val2))+ if val1 > val2 =>+ {+ min_nominee.to_inexact()+ }+ (Precision::Exact(_), Precision::Absent) => min_values.to_inexact(),+ (Precision::Absent, Precision::Exact(_)) => min_nominee.to_inexact(),+ (Precision::Absent, Precision::Inexact(_)) => min_nominee,+ (Precision::Absent, Precision::Absent) => Precision::Absent,+ _ => min_values,+ }+}diff --git a/datafusion/core/src/physical_optimizer/aggregate_statistics.rs b/datafusion/core/src/physical_optimizer/aggregate_statistics.rsindex 52fd60aa94..43def5d73f 100644--- a/datafusion/core/src/physical_optimizer/aggregate_statistics.rs+++ b/datafusion/core/src/physical_optimizer/aggregate_statistics.rs@@ -18,20 +18,18 @@//! Utilizing exact statistics from sources to avoid scanning datause std::sync::Arc;+use super::optimizer::PhysicalOptimizerRule;use crate::config::ConfigOptions;-use datafusion_common::tree_node::TreeNode;-use datafusion_expr::utils::COUNT_STAR_EXPANSION;--use crate::physical_plan::aggregates::{AggregateExec, AggregateMode};+use crate::error::Result;+use crate::physical_plan::aggregates::AggregateExec;use crate::physical_plan::empty::EmptyExec;use crate::physical_plan::projection::ProjectionExec;-use crate::physical_plan::{- expressions, AggregateExpr, ColumnStatistics, ExecutionPlan, Statistics,-};+use crate::physical_plan::{expressions, AggregateExpr, ExecutionPlan, Statistics};use crate::scalar::ScalarValue;-use super::optimizer::PhysicalOptimizerRule;-use crate::error::Result;+use datafusion_common::stats::Precision;+use datafusion_common::tree_node::TreeNode;+use datafusion_expr::utils::COUNT_STAR_EXPANSION;/// Optimizer that uses available statistics for aggregate functions#[derive(Default)]@@ -58,7 +56,7 @@ impl PhysicalOptimizerRule for AggregateStatistics {.as_any().downcast_ref::<AggregateExec>().expect("take_optimizable() ensures that this is a AggregateExec");- let stats = partial_agg_exec.input().statistics();+ let stats = partial_agg_exec.input().statistics()?;let mut projections = vec![];for expr in partial_agg_exec.aggr_expr() {if let Some((non_null_rows, name)) =@@ -107,13 +105,12 @@ impl PhysicalOptimizerRule for AggregateStatistics {/// assert if the node passed as argument is a final `AggregateExec` node that can be optimized:/// - its child (with possible intermediate layers) is a partial `AggregateExec` node/// - they both have no grouping expression-/// - the statistics are exact/// If this is the case, return a ref to the partial `AggregateExec`, else `None`./// We would have preferred to return a casted ref to AggregateExec but the recursion requires/// the `ExecutionPlan.children()` method that returns an owned reference.fn take_optimizable(node: &dyn ExecutionPlan) -> Option<Arc<dyn ExecutionPlan>> {if let Some(final_agg_exec) = node.as_any().downcast_ref::<AggregateExec>() {- if final_agg_exec.mode() == &AggregateMode::Final+ if !final_agg_exec.mode().is_first_stage()&& final_agg_exec.group_expr().is_empty(){let mut child = Arc::clone(final_agg_exec.input());@@ -121,14 +118,11 @@ fn take_optimizable(node: &dyn ExecutionPlan) -> Option<Arc<dyn ExecutionPlan>>if let Some(partial_agg_exec) =child.as_any().downcast_ref::<AggregateExec>(){- if partial_agg_exec.mode() == &AggregateMode::Partial+ if partial_agg_exec.mode().is_first_stage()&& partial_agg_exec.group_expr().is_empty()&& partial_agg_exec.filter_expr().iter().all(|e| e.is_none()){- let stats = partial_agg_exec.input().statistics();- if stats.is_exact {- return Some(child);- }+ return Some(child);}}if let [ref childrens_child] = child.children().as_slice() {@@ -142,13 +136,13 @@ fn take_optimizable(node: &dyn ExecutionPlan) -> Option<Arc<dyn ExecutionPlan>>None}-/// If this agg_expr is a count that is defined in the statistics, return it+/// If this agg_expr is a count that is exactly defined in the statistics, return it.fn take_optimizable_table_count(agg_expr: &dyn AggregateExpr,stats: &Statistics,) -> Option<(ScalarValue, &'static str)> {- if let (Some(num_rows), Some(casted_expr)) = (- stats.num_rows,+ if let (&Precision::Exact(num_rows), Some(casted_expr)) = (+ &stats.num_rows,agg_expr.as_any().downcast_ref::<expressions::Count>(),) {// TODO implementing Eq on PhysicalExpr would help a lot here@@ -169,14 +163,14 @@ fn take_optimizable_table_count(None}-/// If this agg_expr is a count that can be derived from the statistics, return it+/// If this agg_expr is a count that can be exactly derived from the statistics, return it.fn take_optimizable_column_count(agg_expr: &dyn AggregateExpr,stats: &Statistics,) -> Option<(ScalarValue, String)> {- if let (Some(num_rows), Some(col_stats), Some(casted_expr)) = (- stats.num_rows,- &stats.column_statistics,+ let col_stats = &stats.column_statistics;+ if let (&Precision::Exact(num_rows), Some(casted_expr)) = (+ &stats.num_rows,agg_expr.as_any().downcast_ref::<expressions::Count>(),) {if casted_expr.expressions().len() == 1 {@@ -185,11 +179,8 @@ fn take_optimizable_column_count(.as_any().downcast_ref::<expressions::Column>(){- if let ColumnStatistics {- null_count: Some(val),- ..- } = &col_stats[col_expr.index()]- {+ let current_val = &col_stats[col_expr.index()].null_count;+ if let &Precision::Exact(val) = current_val {return Some((ScalarValue::Int64(Some((num_rows - val) as i64)),casted_expr.name().to_string(),@@ -201,27 +192,23 @@ fn take_optimizable_column_count(None}-/// If this agg_expr is a min that is defined in the statistics, return it+/// If this agg_expr is a min that is exactly defined in the statistics, return it.fn take_optimizable_min(agg_expr: &dyn AggregateExpr,stats: &Statistics,) -> Option<(ScalarValue, String)> {- if let (Some(col_stats), Some(casted_expr)) = (- &stats.column_statistics,- agg_expr.as_any().downcast_ref::<expressions::Min>(),- ) {+ let col_stats = &stats.column_statistics;+ if let Some(casted_expr) = agg_expr.as_any().downcast_ref::<expressions::Min>() {if casted_expr.expressions().len() == 1 {// TODO optimize with exprs other than Columnif let Some(col_expr) = casted_expr.expressions()[0].as_any().downcast_ref::<expressions::Column>(){- if let ColumnStatistics {- min_value: Some(val),- ..- } = &col_stats[col_expr.index()]- {- return Some((val.clone(), casted_expr.name().to_string()));+ if let Precision::Exact(val) = &col_stats[col_expr.index()].min_value {+ if !val.is_null() {+ return Some((val.clone(), casted_expr.name().to_string()));+ }}}}@@ -229,27 +216,23 @@ fn take_optimizable_min(None}-/// If this agg_expr is a max that is defined in the statistics, return it+/// If this agg_expr is a max that is exactly defined in the statistics, return it.fn take_optimizable_max(agg_expr: &dyn AggregateExpr,stats: &Statistics,) -> Option<(ScalarValue, String)> {- if let (Some(col_stats), Some(casted_expr)) = (- &stats.column_statistics,- agg_expr.as_any().downcast_ref::<expressions::Max>(),- ) {+ let col_stats = &stats.column_statistics;+ if let Some(casted_expr) = agg_expr.as_any().downcast_ref::<expressions::Max>() {if casted_expr.expressions().len() == 1 {// TODO optimize with exprs other than Columnif let Some(col_expr) = casted_expr.expressions()[0].as_any().downcast_ref::<expressions::Column>(){- if let ColumnStatistics {- max_value: Some(val),- ..- } = &col_stats[col_expr.index()]- {- return Some((val.clone(), casted_expr.name().to_string()));+ if let Precision::Exact(val) = &col_stats[col_expr.index()].max_value {+ if !val.is_null() {+ return Some((val.clone(), casted_expr.name().to_string()));+ }}}}@@ -259,16 +242,9 @@ fn take_optimizable_max(#[cfg(test)]mod tests {- use super::*;use std::sync::Arc;- use arrow::array::Int32Array;- use arrow::datatypes::{DataType, Field, Schema};- use arrow::record_batch::RecordBatch;- use datafusion_common::cast::as_int64_array;- use datafusion_physical_expr::expressions::cast;- use datafusion_physical_expr::PhysicalExpr;-+ use super::*;use crate::error::Result;use crate::logical_expr::Operator;use crate::physical_plan::aggregates::{AggregateExec, PhysicalGroupBy};@@ -279,6 +255,14 @@ mod tests {use crate::physical_plan::memory::MemoryExec;use crate::prelude::SessionContext;+ use arrow::array::Int32Array;+ use arrow::datatypes::{DataType, Field, Schema};+ use arrow::record_batch::RecordBatch;+ use datafusion_common::cast::as_int64_array;+ use datafusion_physical_expr::expressions::cast;+ use datafusion_physical_expr::PhysicalExpr;+ use datafusion_physical_plan::aggregates::AggregateMode;+/// Mock data using a MemoryExec which has an exact count statisticfn mock_data() -> Result<Arc<MemoryExec>> {let schema = Arc::new(Schema::new(vec![@@ -308,7 +292,8 @@ mod tests {) -> Result<()> {let session_ctx = SessionContext::new();let state = session_ctx.state();- let plan = Arc::new(plan) as _;+ let plan: Arc<dyn ExecutionPlan> = Arc::new(plan);+let optimized = AggregateStatistics::new().optimize(Arc::clone(&plan), state.config_options())?;diff --git a/datafusion/core/src/physical_optimizer/combine_partial_final_agg.rs b/datafusion/core/src/physical_optimizer/combine_partial_final_agg.rsindex 40b2bcc3e1..838ae61368 100644--- a/datafusion/core/src/physical_optimizer/combine_partial_final_agg.rs+++ b/datafusion/core/src/physical_optimizer/combine_partial_final_agg.rs@@ -17,13 +17,15 @@//! CombinePartialFinalAggregate optimizer rule checks the adjacent Partial and Final AggregateExecs//! and try to combine them if necessary++use std::sync::Arc;+use crate::error::Result;use crate::physical_optimizer::PhysicalOptimizerRule;use crate::physical_plan::aggregates::{AggregateExec, AggregateMode, PhysicalGroupBy};use crate::physical_plan::ExecutionPlan;-use datafusion_common::config::ConfigOptions;-use std::sync::Arc;+use datafusion_common::config::ConfigOptions;use datafusion_common::tree_node::{Transformed, TreeNode};use datafusion_physical_expr::expressions::Column;use datafusion_physical_expr::{AggregateExpr, PhysicalExpr};@@ -191,10 +193,6 @@ fn discard_column_index(group_expr: Arc<dyn PhysicalExpr>) -> Arc<dyn PhysicalEx#[cfg(test)]mod tests {- use arrow::datatypes::{DataType, Field, Schema, SchemaRef};- use datafusion_physical_expr::expressions::{col, Count, Sum};- use datafusion_physical_expr::AggregateExpr;-use super::*;use crate::datasource::listing::PartitionedFile;use crate::datasource::object_store::ObjectStoreUrl;@@ -206,6 +204,10 @@ mod tests {use crate::physical_plan::repartition::RepartitionExec;use crate::physical_plan::{displayable, Partitioning, Statistics};+ use arrow::datatypes::{DataType, Field, Schema, SchemaRef};+ use datafusion_physical_expr::expressions::{col, Count, Sum};+ use datafusion_physical_expr::AggregateExpr;+/// Runs the CombinePartialFinalAggregate optimizer and asserts the plan against the expectedmacro_rules! assert_optimized {($EXPECTED_LINES: expr, $PLAN: expr) => {@@ -248,7 +250,7 @@ mod tests {object_store_url: ObjectStoreUrl::parse("test:///").unwrap(),file_schema: schema.clone(),file_groups: vec![vec![PartitionedFile::new("x".to_string(), 100)]],- statistics: Statistics::default(),+ statistics: Statistics::new_unknown(schema),projection: None,limit: None,table_partition_cols: vec![],diff --git a/datafusion/core/src/physical_optimizer/enforce_distribution.rs b/datafusion/core/src/physical_optimizer/enforce_distribution.rsindex 52525d1fc4..89036e9f8c 100644--- a/datafusion/core/src/physical_optimizer/enforce_distribution.rs+++ b/datafusion/core/src/physical_optimizer/enforce_distribution.rs@@ -40,13 +40,14 @@ use crate::physical_plan::joins::{};use crate::physical_plan::projection::ProjectionExec;use crate::physical_plan::repartition::RepartitionExec;-use crate::physical_plan::sorts::sort::SortOptions;use crate::physical_plan::sorts::sort_preserving_merge::SortPreservingMergeExec;use crate::physical_plan::union::{can_interleave, InterleaveExec, UnionExec};use crate::physical_plan::windows::WindowAggExec;-use crate::physical_plan::Partitioning;-use crate::physical_plan::{with_new_children_if_necessary, Distribution, ExecutionPlan};+use crate::physical_plan::{+ with_new_children_if_necessary, Distribution, ExecutionPlan, Partitioning,+};+use arrow::compute::SortOptions;use datafusion_common::tree_node::{Transformed, TreeNode, VisitRecursion};use datafusion_expr::logical_plan::JoinType;use datafusion_physical_expr::equivalence::EquivalenceProperties;@@ -58,8 +59,8 @@ use datafusion_physical_expr::{expr_list_eq_strict_order, PhysicalExpr, PhysicalSortRequirement,};use datafusion_physical_plan::unbounded_output;-use datafusion_physical_plan::windows::{get_best_fitting_window, BoundedWindowAggExec};+use itertools::izip;/// The `EnforceDistribution` rule ensures that distribution requirements are@@ -1289,11 +1290,11 @@ fn ensure_distribution(child_idx,)| {// Don't need to apply when the returned row count is not greater than 1:- let stats = child.statistics();- let repartition_beneficial_stats = if stats.is_exact {- stats- .num_rows- .map(|num_rows| num_rows > batch_size)+ let num_rows = child.statistics()?.num_rows;+ let repartition_beneficial_stats = if num_rows.is_exact().unwrap_or(false) {+ num_rows+ .get_value()+ .map(|value| value > &batch_size).unwrap_or(true)} else {true@@ -1808,7 +1809,7 @@ mod tests {unreachable!();}- fn statistics(&self) -> Statistics {+ fn statistics(&self) -> Result<Statistics> {self.input.statistics()}}@@ -1835,7 +1836,7 @@ mod tests {object_store_url: ObjectStoreUrl::parse("test:///").unwrap(),file_schema: schema(),file_groups: vec![vec![PartitionedFile::new("x".to_string(), 100)]],- statistics: Statistics::default(),+ statistics: Statistics::new_unknown(&schema()),projection: None,limit: None,table_partition_cols: vec![],@@ -1863,7 +1864,7 @@ mod tests {vec![PartitionedFile::new("x".to_string(), 100)],vec![PartitionedFile::new("y".to_string(), 100)],],- statistics: Statistics::default(),+ statistics: Statistics::new_unknown(&schema()),projection: None,limit: None,table_partition_cols: vec![],@@ -1885,7 +1886,7 @@ mod tests {object_store_url: ObjectStoreUrl::parse("test:///").unwrap(),file_schema: schema(),file_groups: vec![vec![PartitionedFile::new("x".to_string(), 100)]],- statistics: Statistics::default(),+ statistics: Statistics::new_unknown(&schema()),projection: None,limit: None,table_partition_cols: vec![],@@ -1916,7 +1917,7 @@ mod tests {vec![PartitionedFile::new("x".to_string(), 100)],vec![PartitionedFile::new("y".to_string(), 100)],],- statistics: Statistics::default(),+ statistics: Statistics::new_unknown(&schema()),projection: None,limit: None,table_partition_cols: vec![],@@ -3953,7 +3954,7 @@ mod tests {"x".to_string(),100,)]],- statistics: Statistics::default(),+ statistics: Statistics::new_unknown(&schema()),projection: None,limit: None,table_partition_cols: vec![],diff --git a/datafusion/core/src/physical_optimizer/join_selection.rs b/datafusion/core/src/physical_optimizer/join_selection.rsindex 3d5c93d4b3..876a464257 100644--- a/datafusion/core/src/physical_optimizer/join_selection.rs+++ b/datafusion/core/src/physical_optimizer/join_selection.rs@@ -60,21 +60,29 @@ impl JoinSelection {// TODO: We need some performance test for Right Semi/Right Join swap to Left Semi/Left Join in case that the right side is smaller but not much smaller.// TODO: In PrestoSQL, the optimizer flips join sides only if one side is much smaller than the other by more than SIZE_DIFFERENCE_THRESHOLD times, by default is is 8 times./// Checks statistics for join swap.-fn should_swap_join_order(left: &dyn ExecutionPlan, right: &dyn ExecutionPlan) -> bool {+fn should_swap_join_order(+ left: &dyn ExecutionPlan,+ right: &dyn ExecutionPlan,+) -> Result<bool> {// Get the left and right table's total bytes// If both the left and right tables contain total_byte_size statistics,// use `total_byte_size` to determine `should_swap_join_order`, else use `num_rows`- let (left_size, right_size) = match (- left.statistics().total_byte_size,- right.statistics().total_byte_size,+ let left_stats = left.statistics()?;+ let right_stats = right.statistics()?;+ // First compare `total_byte_size` of left and right side,+ // if information in this field is insufficient fallback to the `num_rows`+ match (+ left_stats.total_byte_size.get_value(),+ right_stats.total_byte_size.get_value(),) {- (Some(l), Some(r)) => (Some(l), Some(r)),- _ => (left.statistics().num_rows, right.statistics().num_rows),- };-- match (left_size, right_size) {- (Some(l), Some(r)) => l > r,- _ => false,+ (Some(l), Some(r)) => Ok(l > r),+ _ => match (+ left_stats.num_rows.get_value(),+ right_stats.num_rows.get_value(),+ ) {+ (Some(l), Some(r)) => Ok(l > r),+ _ => Ok(false),+ },}}@@ -84,10 +92,13 @@ fn supports_collect_by_size() -> bool {// Currently we do not trust the 0 value from stats, due to stats collection might have bug// TODO check the logic in datasource::get_statistics_with_limit()- if let Some(size) = plan.statistics().total_byte_size {- size != 0 && size < collection_size_threshold- } else if let Some(row_count) = plan.statistics().num_rows {- row_count != 0 && row_count < collection_size_threshold+ let Ok(stats) = plan.statistics() else {+ return false;+ };+ if let Some(size) = stats.total_byte_size.get_value() {+ *size != 0 && *size < collection_size_threshold+ } else if let Some(row_count) = stats.num_rows.get_value() {+ *row_count != 0 && *row_count < collection_size_threshold} else {false}@@ -294,7 +305,7 @@ fn try_collect_left(};match (left_can_collect, right_can_collect) {(true, true) => {- if should_swap_join_order(&**left, &**right)+ if should_swap_join_order(&**left, &**right)?&& supports_swap(*hash_join.join_type()){Ok(Some(swap_hash_join(hash_join, PartitionMode::CollectLeft)?))@@ -333,7 +344,7 @@ fn try_collect_left(fn partitioned_hash_join(hash_join: &HashJoinExec) -> Result<Arc<dyn ExecutionPlan>> {let left = hash_join.left();let right = hash_join.right();- if should_swap_join_order(&**left, &**right) && supports_swap(*hash_join.join_type())+ if should_swap_join_order(&**left, &**right)? && supports_swap(*hash_join.join_type()){swap_hash_join(hash_join, PartitionMode::Partitioned)} else {@@ -373,7 +384,7 @@ fn statistical_join_selection_subrule(PartitionMode::Partitioned => {let left = hash_join.left();let right = hash_join.right();- if should_swap_join_order(&**left, &**right)+ if should_swap_join_order(&**left, &**right)?&& supports_swap(*hash_join.join_type()){swap_hash_join(hash_join, PartitionMode::Partitioned).map(Some)?@@ -385,7 +396,7 @@ fn statistical_join_selection_subrule(} else if let Some(cross_join) = plan.as_any().downcast_ref::<CrossJoinExec>() {let left = cross_join.left();let right = cross_join.right();- if should_swap_join_order(&**left, &**right) {+ if should_swap_join_order(&**left, &**right)? {let new_join = CrossJoinExec::new(Arc::clone(right), Arc::clone(left));// TODO avoid adding ProjectionExec again and again, only adding Final Projectionlet proj: Arc<dyn ExecutionPlan> = Arc::new(ProjectionExec::try_new(@@ -579,6 +590,8 @@ fn apply_subrules(#[cfg(test)]mod tests_statistical {+ use std::sync::Arc;+use super::*;use crate::{physical_plan::{@@ -587,28 +600,26 @@ mod tests_statistical {test::StatisticsExec,};- use std::sync::Arc;-use arrow::datatypes::{DataType, Field, Schema};- use datafusion_common::{JoinType, ScalarValue};+ use datafusion_common::{stats::Precision, JoinType, ScalarValue};use datafusion_physical_expr::expressions::Column;use datafusion_physical_expr::PhysicalExpr;fn create_big_and_small() -> (Arc<dyn ExecutionPlan>, Arc<dyn ExecutionPlan>) {let big = Arc::new(StatisticsExec::new(Statistics {- num_rows: Some(10),- total_byte_size: Some(100000),- ..Default::default()+ num_rows: Precision::Inexact(10),+ total_byte_size: Precision::Inexact(100000),+ column_statistics: vec![ColumnStatistics::new_unknown()],},Schema::new(vec![Field::new("big_col", DataType::Int32, false)]),));let small = Arc::new(StatisticsExec::new(Statistics {- num_rows: Some(100000),- total_byte_size: Some(10),- ..Default::default()+ num_rows: Precision::Inexact(100000),+ total_byte_size: Precision::Inexact(10),+ column_statistics: vec![ColumnStatistics::new_unknown()],},Schema::new(vec![Field::new("small_col", DataType::Int32, false)]),));@@ -624,13 +635,19 @@ mod tests_statistical {min: Option<u64>,max: Option<u64>,distinct_count: Option<usize>,- ) -> Option<Vec<ColumnStatistics>> {- Some(vec![ColumnStatistics {- distinct_count,- min_value: min.map(|size| ScalarValue::UInt64(Some(size))),- max_value: max.map(|size| ScalarValue::UInt64(Some(size))),+ ) -> Vec<ColumnStatistics> {+ vec![ColumnStatistics {+ distinct_count: distinct_count+ .map(Precision::Inexact)+ .unwrap_or(Precision::Absent),+ min_value: min+ .map(|size| Precision::Inexact(ScalarValue::UInt64(Some(size))))+ .unwrap_or(Precision::Absent),+ max_value: max+ .map(|size| Precision::Inexact(ScalarValue::UInt64(Some(size))))+ .unwrap_or(Precision::Absent),..Default::default()- }])+ }]}/// Returns three plans with statistics of (min, max, distinct_count)@@ -644,39 +661,39 @@ mod tests_statistical {) {let big = Arc::new(StatisticsExec::new(Statistics {- num_rows: Some(100_000),+ num_rows: Precision::Inexact(100_000),column_statistics: create_column_stats(Some(0),Some(50_000),Some(50_000),),- ..Default::default()+ total_byte_size: Precision::Absent,},Schema::new(vec![Field::new("big_col", DataType::Int32, false)]),));let medium = Arc::new(StatisticsExec::new(Statistics {- num_rows: Some(10_000),+ num_rows: Precision::Inexact(10_000),column_statistics: create_column_stats(Some(1000),Some(5000),Some(1000),),- ..Default::default()+ total_byte_size: Precision::Absent,},Schema::new(vec![Field::new("medium_col", DataType::Int32, false)]),));let small = Arc::new(StatisticsExec::new(Statistics {- num_rows: Some(1000),+ num_rows: Precision::Inexact(1000),column_statistics: create_column_stats(Some(0),Some(100_000),Some(1000),),- ..Default::default()+ total_byte_size: Precision::Absent,},Schema::new(vec![Field::new("small_col", DataType::Int32, false)]),));@@ -725,10 +742,13 @@ mod tests_statistical {.downcast_ref::<HashJoinExec>().expect("The type of the plan should not be changed");- assert_eq!(swapped_join.left().statistics().total_byte_size, Some(10));assert_eq!(- swapped_join.right().statistics().total_byte_size,- Some(100000)+ swapped_join.left().statistics().unwrap().total_byte_size,+ Precision::Inexact(10)+ );+ assert_eq!(+ swapped_join.right().statistics().unwrap().total_byte_size,+ Precision::Inexact(100000));}@@ -774,10 +794,13 @@ mod tests_statistical {.expect("The type of the plan should not be changed");assert_eq!(- swapped_join.left().statistics().total_byte_size,- Some(100000)+ swapped_join.left().statistics().unwrap().total_byte_size,+ Precision::Inexact(100000)+ );+ assert_eq!(+ swapped_join.right().statistics().unwrap().total_byte_size,+ Precision::Inexact(10));- assert_eq!(swapped_join.right().statistics().total_byte_size, Some(10));}#[tokio::test]@@ -815,10 +838,13 @@ mod tests_statistical {assert_eq!(swapped_join.schema().fields().len(), 1);- assert_eq!(swapped_join.left().statistics().total_byte_size, Some(10));assert_eq!(- swapped_join.right().statistics().total_byte_size,- Some(100000)+ swapped_join.left().statistics().unwrap().total_byte_size,+ Precision::Inexact(10)+ );+ assert_eq!(+ swapped_join.right().statistics().unwrap().total_byte_size,+ Precision::Inexact(100000));assert_eq!(original_schema, swapped_join.schema());@@ -893,9 +919,9 @@ mod tests_statistical {" HashJoinExec: mode=CollectLeft, join_type=Right, on=[(small_col@1, medium_col@0)]"," ProjectionExec: expr=[big_col@1 as big_col, small_col@0 as small_col]"," HashJoinExec: mode=CollectLeft, join_type=Inner, on=[(small_col@0, big_col@0)]",- " StatisticsExec: col_count=1, row_count=Some(1000)",- " StatisticsExec: col_count=1, row_count=Some(100000)",- " StatisticsExec: col_count=1, row_count=Some(10000)",+ " StatisticsExec: col_count=1, row_count=Inexact(1000)",+ " StatisticsExec: col_count=1, row_count=Inexact(100000)",+ " StatisticsExec: col_count=1, row_count=Inexact(10000)","",];assert_optimized!(expected, join);@@ -927,10 +953,13 @@ mod tests_statistical {.downcast_ref::<HashJoinExec>().expect("The type of the plan should not be changed");- assert_eq!(swapped_join.left().statistics().total_byte_size, Some(10));assert_eq!(- swapped_join.right().statistics().total_byte_size,- Some(100000)+ swapped_join.left().statistics().unwrap().total_byte_size,+ Precision::Inexact(10)+ );+ assert_eq!(+ swapped_join.right().statistics().unwrap().total_byte_size,+ Precision::Inexact(100000));}@@ -973,27 +1002,27 @@ mod tests_statistical {async fn test_join_selection_collect_left() {let big = Arc::new(StatisticsExec::new(Statistics {- num_rows: Some(10000000),- total_byte_size: Some(10000000),- ..Default::default()+ num_rows: Precision::Inexact(10000000),+ total_byte_size: Precision::Inexact(10000000),+ column_statistics: vec![ColumnStatistics::new_unknown()],},Schema::new(vec![Field::new("big_col", DataType::Int32, false)]),));let small = Arc::new(StatisticsExec::new(Statistics {- num_rows: Some(10),- total_byte_size: Some(10),- ..Default::default()+ num_rows: Precision::Inexact(10),+ total_byte_size: Precision::Inexact(10),+ column_statistics: vec![ColumnStatistics::new_unknown()],},Schema::new(vec![Field::new("small_col", DataType::Int32, false)]),));let empty = Arc::new(StatisticsExec::new(Statistics {- num_rows: None,- total_byte_size: None,- ..Default::default()+ num_rows: Precision::Absent,+ total_byte_size: Precision::Absent,+ column_statistics: vec![ColumnStatistics::new_unknown()],},Schema::new(vec![Field::new("empty_col", DataType::Int32, false)]),));@@ -1051,27 +1080,27 @@ mod tests_statistical {async fn test_join_selection_partitioned() {let big1 = Arc::new(StatisticsExec::new(Statistics {- num_rows: Some(10000000),- total_byte_size: Some(10000000),- ..Default::default()+ num_rows: Precision::Inexact(10000000),+ total_byte_size: Precision::Inexact(10000000),+ column_statistics: vec![ColumnStatistics::new_unknown()],},Schema::new(vec![Field::new("big_col1", DataType::Int32, false)]),));let big2 = Arc::new(StatisticsExec::new(Statistics {- num_rows: Some(20000000),- total_byte_size: Some(20000000),- ..Default::default()+ num_rows: Precision::Inexact(20000000),+ total_byte_size: Precision::Inexact(20000000),+ column_statistics: vec![ColumnStatistics::new_unknown()],},Schema::new(vec![Field::new("big_col2", DataType::Int32, false)]),));let empty = Arc::new(StatisticsExec::new(Statistics {- num_rows: None,- total_byte_size: None,- ..Default::default()+ num_rows: Precision::Absent,+ total_byte_size: Precision::Absent,+ column_statistics: vec![ColumnStatistics::new_unknown()],},Schema::new(vec![Field::new("empty_col", DataType::Int32, false)]),));@@ -1173,12 +1202,13 @@ mod tests_statistical {#[cfg(test)]mod util_tests {+ use std::sync::Arc;+use arrow_schema::{DataType, Field, Schema};use datafusion_expr::Operator;use datafusion_physical_expr::expressions::{BinaryExpr, Column, NegativeExpr};use datafusion_physical_expr::intervals::utils::check_support;use datafusion_physical_expr::PhysicalExpr;- use std::sync::Arc;#[test]fn check_expr_supported() {diff --git a/datafusion/core/src/physical_optimizer/output_requirements.rs b/datafusion/core/src/physical_optimizer/output_requirements.rsindex 4b687d7f35..f5eacd5ee6 100644--- a/datafusion/core/src/physical_optimizer/output_requirements.rs+++ b/datafusion/core/src/physical_optimizer/output_requirements.rs@@ -179,7 +179,7 @@ impl ExecutionPlan for OutputRequirementExec {unreachable!();}- fn statistics(&self) -> Statistics {+ fn statistics(&self) -> Result<Statistics> {self.input.statistics()}}diff --git a/datafusion/core/src/physical_optimizer/pipeline_checker.rs b/datafusion/core/src/physical_optimizer/pipeline_checker.rsindex fbc9910179..43ae7dbfe7 100644--- a/datafusion/core/src/physical_optimizer/pipeline_checker.rs+++ b/datafusion/core/src/physical_optimizer/pipeline_checker.rs@@ -18,17 +18,19 @@//! The [PipelineChecker] rule ensures that a given plan can accommodate its//! infinite sources, if there are any. It will reject non-runnable query plans//! that use pipeline-breaking operators on infinite input(s).-//!++use std::sync::Arc;+use crate::config::ConfigOptions;use crate::error::Result;use crate::physical_optimizer::PhysicalOptimizerRule;use crate::physical_plan::joins::SymmetricHashJoinExec;use crate::physical_plan::{with_new_children_if_necessary, ExecutionPlan};+use datafusion_common::config::OptimizerOptions;use datafusion_common::tree_node::{Transformed, TreeNode, VisitRecursion};use datafusion_common::{plan_err, DataFusionError};use datafusion_physical_expr::intervals::utils::{check_support, is_datatype_supported};-use std::sync::Arc;/// The PipelineChecker rule rejects non-runnable query plans that use/// pipeline-breaking operators on infinite input(s).diff --git a/datafusion/core/src/physical_optimizer/replace_with_order_preserving_variants.rs b/datafusion/core/src/physical_optimizer/replace_with_order_preserving_variants.rsindex f4b3608d00..fb1a50e18d 100644--- a/datafusion/core/src/physical_optimizer/replace_with_order_preserving_variants.rs+++ b/datafusion/core/src/physical_optimizer/replace_with_order_preserving_variants.rs@@ -903,7 +903,7 @@ mod tests {"file_path".to_string(),100,)]],- statistics: Statistics::default(),+ statistics: Statistics::new_unknown(schema),projection: Some(projection),limit: None,table_partition_cols: vec![],diff --git a/datafusion/core/src/physical_optimizer/test_utils.rs b/datafusion/core/src/physical_optimizer/test_utils.rsindex ed73456514..53401751b6 100644--- a/datafusion/core/src/physical_optimizer/test_utils.rs+++ b/datafusion/core/src/physical_optimizer/test_utils.rs@@ -267,7 +267,7 @@ pub fn parquet_exec(schema: &SchemaRef) -> Arc<ParquetExec> {object_store_url: ObjectStoreUrl::parse("test:///").unwrap(),file_schema: schema.clone(),file_groups: vec![vec![PartitionedFile::new("x".to_string(), 100)]],- statistics: Statistics::default(),+ statistics: Statistics::new_unknown(schema),projection: None,limit: None,table_partition_cols: vec![],@@ -291,7 +291,7 @@ pub fn parquet_exec_sorted(object_store_url: ObjectStoreUrl::parse("test:///").unwrap(),file_schema: schema.clone(),file_groups: vec![vec![PartitionedFile::new("x".to_string(), 100)]],- statistics: Statistics::default(),+ statistics: Statistics::new_unknown(schema),projection: None,limit: None,table_partition_cols: vec![],diff --git a/datafusion/core/src/physical_planner.rs b/datafusion/core/src/physical_planner.rsindex 4055f9f4eb..5a1fdcaee5 100644--- a/datafusion/core/src/physical_planner.rs+++ b/datafusion/core/src/physical_planner.rs@@ -17,6 +17,10 @@//! Planner for [`LogicalPlan`] to [`ExecutionPlan`]+use std::collections::HashMap;+use std::fmt::Write;+use std::sync::Arc;+use crate::datasource::file_format::arrow::ArrowFormat;use crate::datasource::file_format::avro::AvroFormat;use crate::datasource::file_format::csv::CsvFormat;@@ -27,6 +31,7 @@ use crate::datasource::file_format::FileFormat;use crate::datasource::listing::ListingTableUrl;use crate::datasource::physical_plan::FileSinkConfig;use crate::datasource::source_as_provider;+use crate::error::{DataFusionError, Result};use crate::execution::context::{ExecutionProps, SessionState};use crate::logical_expr::utils::generate_sort_key;use crate::logical_expr::{@@ -37,49 +42,45 @@ use crate::logical_expr::{CrossJoin, Expr, LogicalPlan, Partitioning as LogicalPartitioning, PlanType,Repartition, Union, UserDefinedLogicalNode,};-use crate::physical_plan::memory::MemoryExec;-use arrow_array::builder::StringBuilder;-use arrow_array::RecordBatch;-use datafusion_common::display::ToStringifiedPlan;-use datafusion_common::file_options::FileTypeWriterOptions;-use datafusion_common::FileType;-use datafusion_expr::dml::{CopyOptions, CopyTo};-use crate::logical_expr::{Limit, Values};use crate::physical_expr::create_physical_expr;use crate::physical_optimizer::optimizer::PhysicalOptimizerRule;use crate::physical_plan::aggregates::{AggregateExec, AggregateMode, PhysicalGroupBy};use crate::physical_plan::analyze::AnalyzeExec;+use crate::physical_plan::empty::EmptyExec;use crate::physical_plan::explain::ExplainExec;use crate::physical_plan::expressions::{Column, PhysicalSortExpr};use crate::physical_plan::filter::FilterExec;-use crate::physical_plan::joins::HashJoinExec;-use crate::physical_plan::joins::SortMergeJoinExec;-use crate::physical_plan::joins::{CrossJoinExec, NestedLoopJoinExec};+use crate::physical_plan::joins::utils as join_utils;+use crate::physical_plan::joins::{+ CrossJoinExec, HashJoinExec, NestedLoopJoinExec, PartitionMode, SortMergeJoinExec,+};use crate::physical_plan::limit::{GlobalLimitExec, LocalLimitExec};+use crate::physical_plan::memory::MemoryExec;use crate::physical_plan::projection::ProjectionExec;use crate::physical_plan::repartition::RepartitionExec;use crate::physical_plan::sorts::sort::SortExec;+use crate::physical_plan::union::UnionExec;use crate::physical_plan::unnest::UnnestExec;+use crate::physical_plan::values::ValuesExec;use crate::physical_plan::windows::{BoundedWindowAggExec, PartitionSearchMode, WindowAggExec,};use crate::physical_plan::{- aggregates, empty::EmptyExec, joins::PartitionMode, udaf, union::UnionExec,- values::ValuesExec, windows,-};-use crate::physical_plan::{joins::utils as join_utils, Partitioning};-use crate::physical_plan::{AggregateExpr, ExecutionPlan, PhysicalExpr, WindowExpr};-use crate::{- error::{DataFusionError, Result},- physical_plan::displayable,+ aggregates, displayable, udaf, windows, AggregateExpr, ExecutionPlan, Partitioning,+ PhysicalExpr, WindowExpr,};+use arrow::compute::SortOptions;use arrow::datatypes::{Schema, SchemaRef};-use async_trait::async_trait;+use arrow_array::builder::StringBuilder;+use arrow_array::RecordBatch;+use datafusion_common::display::ToStringifiedPlan;+use datafusion_common::file_options::FileTypeWriterOptions;use datafusion_common::{- exec_err, internal_err, not_impl_err, plan_err, DFSchema, ScalarValue,+ exec_err, internal_err, not_impl_err, plan_err, DFSchema, FileType, ScalarValue,};+use datafusion_expr::dml::{CopyOptions, CopyTo};use datafusion_expr::expr::{self, AggregateFunction, AggregateUDF, Alias, Between, BinaryExpr, Cast,GetFieldAccess, GetIndexedField, GroupingSet, InList, Like, ScalarUDF, TryCast,@@ -87,17 +88,17 @@ use datafusion_expr::expr::{};use datafusion_expr::expr_rewriter::{unalias, unnormalize_cols};use datafusion_expr::logical_plan::builder::wrap_projection_for_join_if_necessary;-use datafusion_expr::{DescribeTable, DmlStatement, StringifiedPlan, WriteOp};-use datafusion_expr::{WindowFrame, WindowFrameBound};+use datafusion_expr::{+ DescribeTable, DmlStatement, StringifiedPlan, WindowFrame, WindowFrameBound, WriteOp,+};use datafusion_physical_expr::expressions::Literal;use datafusion_sql::utils::window_expr_common_partition_keys;++use async_trait::async_trait;use futures::future::BoxFuture;use futures::{FutureExt, StreamExt, TryStreamExt};use itertools::{multiunzip, Itertools};use log::{debug, trace};-use std::collections::HashMap;-use std::fmt::Write;-use std::sync::Arc;fn create_function_physical_name(fun: &str,@@ -2670,7 +2671,7 @@ mod tests {unimplemented!("NoOpExecutionPlan::execute");}- fn statistics(&self) -> Statistics {+ fn statistics(&self) -> Result<Statistics> {unimplemented!("NoOpExecutionPlan::statistics");}}diff --git a/datafusion/core/src/test/mod.rs b/datafusion/core/src/test/mod.rsindex 903542ca3f..aad5c19044 100644--- a/datafusion/core/src/test/mod.rs+++ b/datafusion/core/src/test/mod.rs@@ -17,6 +17,13 @@//! Common unit test utility methods+use std::any::Any;+use std::fs::File;+use std::io::prelude::*;+use std::io::{BufReader, BufWriter};+use std::path::Path;+use std::sync::Arc;+use crate::datasource::file_format::file_compression_type::{FileCompressionType, FileTypeExt,};@@ -29,29 +36,23 @@ use crate::logical_expr::LogicalPlan;use crate::physical_plan::ExecutionPlan;use crate::test::object_store::local_unpartitioned_file;use crate::test_util::{aggr_test_schema, arrow_test_data};-use array::ArrayRef;-use arrow::array::{self, Array, Decimal128Builder, Int32Array};++use arrow::array::{self, Array, ArrayRef, Decimal128Builder, Int32Array};use arrow::datatypes::{DataType, Field, Schema, SchemaRef};use arrow::record_batch::RecordBatch;+use datafusion_common::{DataFusionError, FileType, Statistics};+use datafusion_execution::{SendableRecordBatchStream, TaskContext};+use datafusion_physical_expr::{Partitioning, PhysicalSortExpr};+use datafusion_physical_plan::{DisplayAs, DisplayFormatType};+#[cfg(feature = "compression")]use bzip2::write::BzEncoder;#[cfg(feature = "compression")]use bzip2::Compression as BzCompression;-use datafusion_common::FileType;-use datafusion_common::{DataFusionError, Statistics};-use datafusion_execution::{SendableRecordBatchStream, TaskContext};-use datafusion_physical_expr::{Partitioning, PhysicalSortExpr};-use datafusion_physical_plan::{DisplayAs, DisplayFormatType};#[cfg(feature = "compression")]use flate2::write::GzEncoder;#[cfg(feature = "compression")]use flate2::Compression as GzCompression;-use std::any::Any;-use std::fs::File;-use std::io::prelude::*;-use std::io::{BufReader, BufWriter};-use std::path::Path;-use std::sync::Arc;#[cfg(feature = "compression")]use xz2::write::XzEncoder;#[cfg(feature = "compression")]@@ -195,9 +196,9 @@ pub fn partitioned_csv_config() -> Result<FileScanConfig> {Ok(FileScanConfig {object_store_url: ObjectStoreUrl::local_filesystem(),- file_schema: schema,+ file_schema: schema.clone(),file_groups,- statistics: Default::default(),+ statistics: Statistics::new_unknown(&schema),projection: None,limit: None,table_partition_cols: vec![],@@ -285,7 +286,7 @@ pub fn csv_exec_sorted(object_store_url: ObjectStoreUrl::parse("test:///").unwrap(),file_schema: schema.clone(),file_groups: vec![vec![PartitionedFile::new("x".to_string(), 100)]],- statistics: Statistics::default(),+ statistics: Statistics::new_unknown(schema),projection: None,limit: None,table_partition_cols: vec![],@@ -308,12 +309,8 @@ pub struct StatisticsExec {}impl StatisticsExec {pub fn new(stats: Statistics, schema: Schema) -> Self {- assert!(- stats- .column_statistics- .as_ref()- .map(|cols| cols.len() == schema.fields().len())- .unwrap_or(true),+ assert_eq!(+ stats.column_statistics.len(), schema.fields().len(),"if defined, the column statistics vector length should be the number of fields");Self {@@ -378,8 +375,8 @@ impl ExecutionPlan for StatisticsExec {unimplemented!("This plan only serves for testing statistics")}- fn statistics(&self) -> Statistics {- self.stats.clone()+ fn statistics(&self) -> Result<Statistics> {+ Ok(self.stats.clone())}}diff --git a/datafusion/core/src/test_util/mod.rs b/datafusion/core/src/test_util/mod.rsindex bd52c3eeda..d826ec8bfb 100644--- a/datafusion/core/src/test_util/mod.rs+++ b/datafusion/core/src/test_util/mod.rs@@ -37,12 +37,14 @@ use crate::physical_plan::{SendableRecordBatchStream,};use crate::prelude::{CsvReadOptions, SessionContext};+use arrow::datatypes::{DataType, Field, Schema, SchemaRef};use arrow::record_batch::RecordBatch;-use async_trait::async_trait;use datafusion_common::{Statistics, TableReference};use datafusion_expr::{CreateExternalTable, Expr, TableType};use datafusion_physical_expr::PhysicalSortExpr;++use async_trait::async_trait;use futures::Stream;// backwards compatibility@@ -50,8 +52,7 @@ pub use datafusion_common::test_util::{arrow_test_data, get_data_dir, parquet_test_data,};-pub use datafusion_common::assert_batches_eq;-pub use datafusion_common::assert_batches_sorted_eq;+pub use datafusion_common::{assert_batches_eq, assert_batches_sorted_eq};/// Scan an empty data source, mainly used in testspub fn scan_empty(@@ -238,8 +239,8 @@ impl ExecutionPlan for UnboundedExec {}))}- fn statistics(&self) -> Statistics {- Statistics::default()+ fn statistics(&self) -> Result<Statistics> {+ Ok(Statistics::new_unknown(&self.schema()))}}diff --git a/datafusion/core/src/test_util/parquet.rs b/datafusion/core/src/test_util/parquet.rsindex d3a1f9c1ef..0d11526703 100644--- a/datafusion/core/src/test_util/parquet.rs+++ b/datafusion/core/src/test_util/parquet.rs@@ -35,6 +35,9 @@ use crate::physical_plan::filter::FilterExec;use crate::physical_plan::metrics::MetricsSet;use crate::physical_plan::ExecutionPlan;use crate::prelude::{Expr, SessionConfig};++use datafusion_common::Statistics;+use object_store::path::Path;use object_store::ObjectMeta;use parquet::arrow::ArrowWriter;@@ -147,7 +150,7 @@ impl TestParquetFile {range: None,extensions: None,}]],- statistics: Default::default(),+ statistics: Statistics::new_unknown(&self.schema),projection: None,limit: None,table_partition_cols: vec![],diff --git a/datafusion/core/tests/custom_sources.rs b/datafusion/core/tests/custom_sources.rsindex 377c2ab2ab..daf1ef41a2 100644--- a/datafusion/core/tests/custom_sources.rs+++ b/datafusion/core/tests/custom_sources.rs@@ -15,10 +15,17 @@// specific language governing permissions and limitations// under the License.+use std::any::Any;+use std::pin::Pin;+use std::sync::Arc;+use std::task::{Context, Poll};+use arrow::array::{Int32Array, Int64Array};use arrow::compute::kernels::aggregate;use arrow::datatypes::{DataType, Field, Int32Type, Schema, SchemaRef};use arrow::record_batch::RecordBatch;+use datafusion::datasource::{TableProvider, TableType};+use datafusion::error::Result;use datafusion::execution::context::{SessionContext, SessionState, TaskContext};use datafusion::logical_expr::{col, Expr, LogicalPlan, LogicalPlanBuilder, TableScan, UNNAMED_TABLE,@@ -26,29 +33,20 @@ use datafusion::logical_expr::{use datafusion::physical_plan::empty::EmptyExec;use datafusion::physical_plan::expressions::PhysicalSortExpr;use datafusion::physical_plan::{- ColumnStatistics, DisplayAs, ExecutionPlan, Partitioning, RecordBatchStream,- SendableRecordBatchStream, Statistics,+ collect, ColumnStatistics, DisplayAs, DisplayFormatType, ExecutionPlan, Partitioning,+ RecordBatchStream, SendableRecordBatchStream, Statistics,};use datafusion::scalar::ScalarValue;-use datafusion::{- datasource::{TableProvider, TableType},- physical_plan::collect,-};-use datafusion::{error::Result, physical_plan::DisplayFormatType};-use datafusion_common::cast::as_primitive_array;use datafusion_common::project_schema;+use datafusion_common::stats::Precision;++use async_trait::async_trait;use futures::stream::Stream;-use std::any::Any;-use std::pin::Pin;-use std::sync::Arc;-use std::task::{Context, Poll};/// Also run all tests that are found in the `custom_sources_cases` directorymod custom_sources_cases;-use async_trait::async_trait;-//--- Custom source dataframe tests ---//struct CustomTableProvider;@@ -153,30 +151,28 @@ impl ExecutionPlan for CustomExecutionPlan {Ok(Box::pin(TestCustomRecordBatchStream { nb_batch: 1 }))}- fn statistics(&self) -> Statistics {+ fn statistics(&self) -> Result<Statistics> {let batch = TEST_CUSTOM_RECORD_BATCH!().unwrap();- Statistics {- is_exact: true,- num_rows: Some(batch.num_rows()),- total_byte_size: None,- column_statistics: Some(- self.projection- .clone()- .unwrap_or_else(|| (0..batch.columns().len()).collect())- .iter()- .map(|i| ColumnStatistics {- null_count: Some(batch.column(*i).null_count()),- min_value: Some(ScalarValue::Int32(aggregate::min(- as_primitive_array::<Int32Type>(batch.column(*i)).unwrap(),- ))),- max_value: Some(ScalarValue::Int32(aggregate::max(- as_primitive_array::<Int32Type>(batch.column(*i)).unwrap(),- ))),- ..Default::default()- })- .collect(),- ),- }+ Ok(Statistics {+ num_rows: Precision::Exact(batch.num_rows()),+ total_byte_size: Precision::Absent,+ column_statistics: self+ .projection+ .clone()+ .unwrap_or_else(|| (0..batch.columns().len()).collect())+ .iter()+ .map(|i| ColumnStatistics {+ null_count: Precision::Exact(batch.column(*i).null_count()),+ min_value: Precision::Exact(ScalarValue::Int32(aggregate::min(+ as_primitive_array::<Int32Type>(batch.column(*i)).unwrap(),+ ))),+ max_value: Precision::Exact(ScalarValue::Int32(aggregate::max(+ as_primitive_array::<Int32Type>(batch.column(*i)).unwrap(),+ ))),+ ..Default::default()+ })+ .collect(),+ })}}diff --git a/datafusion/core/tests/custom_sources_cases/provider_filter_pushdown.rs b/datafusion/core/tests/custom_sources_cases/provider_filter_pushdown.rsindex 73085937cb..4679ca6d07 100644--- a/datafusion/core/tests/custom_sources_cases/provider_filter_pushdown.rs+++ b/datafusion/core/tests/custom_sources_cases/provider_filter_pushdown.rs@@ -119,10 +119,10 @@ impl ExecutionPlan for CustomPlan {)))}- fn statistics(&self) -> Statistics {+ fn statistics(&self) -> Result<Statistics> {// here we could provide more accurate statistics// but we want to test the filter pushdown not the CBOs- Statistics::default()+ Ok(Statistics::new_unknown(&self.schema()))}}diff --git a/datafusion/core/tests/custom_sources_cases/statistics.rs b/datafusion/core/tests/custom_sources_cases/statistics.rsindex 43e6c8851e..f0985f5546 100644--- a/datafusion/core/tests/custom_sources_cases/statistics.rs+++ b/datafusion/core/tests/custom_sources_cases/statistics.rs@@ -34,7 +34,7 @@ use datafusion::{use async_trait::async_trait;use datafusion::execution::context::{SessionState, TaskContext};-use datafusion_common::project_schema;+use datafusion_common::{project_schema, stats::Precision};/// This is a testing structure for statistics/// It will act both as a table provider and execution plan@@ -46,13 +46,10 @@ struct StatisticsValidation {impl StatisticsValidation {fn new(stats: Statistics, schema: SchemaRef) -> Self {- assert!(- stats- .column_statistics- .as_ref()- .map(|cols| cols.len() == schema.fields().len())- .unwrap_or(true),- "if defined, the column statistics vector length should be the number of fields"+ assert_eq!(+ stats.column_statistics.len(),+ schema.fields().len(),+ "the column statistics vector length should be the number of fields");Self { stats, schema }}@@ -94,17 +91,16 @@ impl TableProvider for StatisticsValidation {let current_stat = self.stats.clone();- let proj_col_stats = current_stat- .column_statistics- .map(|col_stat| projection.iter().map(|i| col_stat[*i].clone()).collect());-+ let proj_col_stats = projection+ .iter()+ .map(|i| current_stat.column_statistics[*i].clone())+ .collect();Ok(Arc::new(Self::new(Statistics {- is_exact: current_stat.is_exact,num_rows: current_stat.num_rows,column_statistics: proj_col_stats,// TODO stats: knowing the type of the new columns we can guess the output size- total_byte_size: None,+ total_byte_size: Precision::Absent,},projected_schema,)))@@ -166,8 +162,8 @@ impl ExecutionPlan for StatisticsValidation {unimplemented!("This plan only serves for testing statistics")}- fn statistics(&self) -> Statistics {- self.stats.clone()+ fn statistics(&self) -> Result<Statistics> {+ Ok(self.stats.clone())}}@@ -182,23 +178,22 @@ fn init_ctx(stats: Statistics, schema: Schema) -> Result<SessionContext> {fn fully_defined() -> (Statistics, Schema) {(Statistics {- num_rows: Some(13),- is_exact: true,- total_byte_size: None, // ignore byte size for now- column_statistics: Some(vec![+ num_rows: Precision::Exact(13),+ total_byte_size: Precision::Absent, // ignore byte size for now+ column_statistics: vec![ColumnStatistics {- distinct_count: Some(2),- max_value: Some(ScalarValue::Int32(Some(1023))),- min_value: Some(ScalarValue::Int32(Some(-24))),- null_count: Some(0),+ distinct_count: Precision::Exact(2),+ max_value: Precision::Exact(ScalarValue::Int32(Some(1023))),+ min_value: Precision::Exact(ScalarValue::Int32(Some(-24))),+ null_count: Precision::Exact(0),},ColumnStatistics {- distinct_count: Some(13),- max_value: Some(ScalarValue::Int64(Some(5486))),- min_value: Some(ScalarValue::Int64(Some(-6783))),- null_count: Some(5),+ distinct_count: Precision::Exact(13),+ max_value: Precision::Exact(ScalarValue::Int64(Some(5486))),+ min_value: Precision::Exact(ScalarValue::Int64(Some(-6783))),+ null_count: Precision::Exact(5),},- ]),+ ],},Schema::new(vec![Field::new("c1", DataType::Int32, false),@@ -216,7 +211,7 @@ async fn sql_basic() -> Result<()> {let physical_plan = df.create_physical_plan().await.unwrap();// the statistics should be those of the source- assert_eq!(stats, physical_plan.statistics());+ assert_eq!(stats, physical_plan.statistics()?);Ok(())}@@ -232,10 +227,8 @@ async fn sql_filter() -> Result<()> {.unwrap();let physical_plan = df.create_physical_plan().await.unwrap();-- let stats = physical_plan.statistics();- assert!(!stats.is_exact);- assert_eq!(stats.num_rows, Some(1));+ let stats = physical_plan.statistics()?;+ assert_eq!(stats.num_rows, Precision::Inexact(1));Ok(())}@@ -243,6 +236,7 @@ async fn sql_filter() -> Result<()> {#[tokio::test]async fn sql_limit() -> Result<()> {let (stats, schema) = fully_defined();+ let col_stats = Statistics::unknown_column(&schema);let ctx = init_ctx(stats.clone(), schema)?;let df = ctx.sql("SELECT * FROM stats_table LIMIT 5").await.unwrap();@@ -251,11 +245,11 @@ async fn sql_limit() -> Result<()> {// we loose all statistics except the for number of rows which becomes the limitassert_eq!(Statistics {- num_rows: Some(5),- is_exact: true,- ..Default::default()+ num_rows: Precision::Exact(5),+ column_statistics: col_stats,+ total_byte_size: Precision::Absent},- physical_plan.statistics()+ physical_plan.statistics()?);let df = ctx@@ -264,7 +258,7 @@ async fn sql_limit() -> Result<()> {.unwrap();let physical_plan = df.create_physical_plan().await.unwrap();// when the limit is larger than the original number of lines, statistics remain unchanged- assert_eq!(stats, physical_plan.statistics());+ assert_eq!(stats, physical_plan.statistics()?);Ok(())}@@ -281,13 +275,12 @@ async fn sql_window() -> Result<()> {let physical_plan = df.create_physical_plan().await.unwrap();- let result = physical_plan.statistics();+ let result = physical_plan.statistics()?;assert_eq!(stats.num_rows, result.num_rows);- assert!(result.column_statistics.is_some());- let col_stats = result.column_statistics.unwrap();+ let col_stats = result.column_statistics;assert_eq!(2, col_stats.len());- assert_eq!(stats.column_statistics.unwrap()[1], col_stats[0]);+ assert_eq!(stats.column_statistics[1], col_stats[0]);Ok(())}diff --git a/datafusion/core/tests/parquet/custom_reader.rs b/datafusion/core/tests/parquet/custom_reader.rsindex 75ff56a265..37481b936d 100644--- a/datafusion/core/tests/parquet/custom_reader.rs+++ b/datafusion/core/tests/parquet/custom_reader.rs@@ -15,6 +15,11 @@// specific language governing permissions and limitations// under the License.+use std::io::Cursor;+use std::ops::Range;+use std::sync::Arc;+use std::time::SystemTime;+use arrow::array::{ArrayRef, Int64Array, Int8Array, StringArray};use arrow::datatypes::{Field, Schema, SchemaBuilder};use arrow::record_batch::RecordBatch;@@ -30,6 +35,7 @@ use datafusion::physical_plan::metrics::ExecutionPlanMetricsSet;use datafusion::physical_plan::{collect, Statistics};use datafusion::prelude::SessionContext;use datafusion_common::Result;+use futures::future::BoxFuture;use futures::{FutureExt, TryFutureExt};use object_store::memory::InMemory;@@ -39,10 +45,6 @@ use parquet::arrow::async_reader::AsyncFileReader;use parquet::arrow::ArrowWriter;use parquet::errors::ParquetError;use parquet::file::metadata::ParquetMetaData;-use std::io::Cursor;-use std::ops::Range;-use std::sync::Arc;-use std::time::SystemTime;const EXPECTED_USER_DEFINED_METADATA: &str = "some-user-defined-metadata";@@ -77,8 +79,8 @@ async fn route_data_access_ops_to_parquet_file_reader_factory() {// just any url that doesn't point to in memory object storeobject_store_url: ObjectStoreUrl::local_filesystem(),file_groups: vec![file_groups],+ statistics: Statistics::new_unknown(&file_schema),file_schema,- statistics: Statistics::default(),projection: None,limit: None,table_partition_cols: vec![],diff --git a/datafusion/core/tests/parquet/file_statistics.rs b/datafusion/core/tests/parquet/file_statistics.rsindex ac4a91720e..1ea154303d 100644--- a/datafusion/core/tests/parquet/file_statistics.rs+++ b/datafusion/core/tests/parquet/file_statistics.rs@@ -15,6 +15,9 @@// specific language governing permissions and limitations// under the License.+use std::fs;+use std::sync::Arc;+use datafusion::datasource::file_format::parquet::ParquetFormat;use datafusion::datasource::listing::{ListingOptions, ListingTable, ListingTableConfig, ListingTableUrl,@@ -23,6 +26,7 @@ use datafusion::datasource::physical_plan::ParquetExec;use datafusion::datasource::TableProvider;use datafusion::execution::context::SessionState;use datafusion::prelude::SessionContext;+use datafusion_common::stats::Precision;use datafusion_execution::cache::cache_manager::CacheManagerConfig;use datafusion_execution::cache::cache_unit;use datafusion_execution::cache::cache_unit::{@@ -30,8 +34,7 @@ use datafusion_execution::cache::cache_unit::{};use datafusion_execution::config::SessionConfig;use datafusion_execution::runtime_env::{RuntimeConfig, RuntimeEnv};-use std::fs;-use std::sync::Arc;+use tempfile::tempdir;#[tokio::test]@@ -54,24 +57,33 @@ async fn load_table_stats_with_session_level_cache() {assert_eq!(get_static_cache_size(&state1), 0);let exec1 = table1.scan(&state1, None, &[], None).await.unwrap();- assert_eq!(exec1.statistics().num_rows, Some(8));- assert_eq!(exec1.statistics().total_byte_size, Some(671));+ assert_eq!(exec1.statistics().unwrap().num_rows, Precision::Exact(8));+ assert_eq!(+ exec1.statistics().unwrap().total_byte_size,+ Precision::Exact(671)+ );assert_eq!(get_static_cache_size(&state1), 1);//Session 2 first time list files//check session 1 cache result not show in session 2assert_eq!(get_static_cache_size(&state2), 0);let exec2 = table2.scan(&state2, None, &[], None).await.unwrap();- assert_eq!(exec2.statistics().num_rows, Some(8));- assert_eq!(exec2.statistics().total_byte_size, Some(671));+ assert_eq!(exec2.statistics().unwrap().num_rows, Precision::Exact(8));+ assert_eq!(+ exec2.statistics().unwrap().total_byte_size,+ Precision::Exact(671)+ );assert_eq!(get_static_cache_size(&state2), 1);//Session 1 second time list files//check session 1 cache result not show in session 2assert_eq!(get_static_cache_size(&state1), 1);let exec3 = table1.scan(&state1, None, &[], None).await.unwrap();- assert_eq!(exec3.statistics().num_rows, Some(8));- assert_eq!(exec3.statistics().total_byte_size, Some(671));+ assert_eq!(exec3.statistics().unwrap().num_rows, Precision::Exact(8));+ assert_eq!(+ exec3.statistics().unwrap().total_byte_size,+ Precision::Exact(671)+ );// List same file no increaseassert_eq!(get_static_cache_size(&state1), 1);}diff --git a/datafusion/core/tests/parquet/page_pruning.rs b/datafusion/core/tests/parquet/page_pruning.rsindex 4337259c1e..b77643c35e 100644--- a/datafusion/core/tests/parquet/page_pruning.rs+++ b/datafusion/core/tests/parquet/page_pruning.rs@@ -17,6 +17,7 @@use crate::parquet::Unit::Page;use crate::parquet::{ContextWithParquet, Scenario};+use datafusion::datasource::file_format::parquet::ParquetFormat;use datafusion::datasource::file_format::FileFormat;use datafusion::datasource::listing::PartitionedFile;@@ -30,6 +31,7 @@ use datafusion_common::{ScalarValue, Statistics, ToDFSchema};use datafusion_expr::{col, lit, Expr};use datafusion_physical_expr::create_physical_expr;use datafusion_physical_expr::execution_props::ExecutionProps;+use futures::StreamExt;use object_store::path::Path;use object_store::ObjectMeta;@@ -71,8 +73,8 @@ async fn get_parquet_exec(state: &SessionState, filter: Expr) -> ParquetExec {FileScanConfig {object_store_url,file_groups: vec![vec![partitioned_file]],- file_schema: schema,- statistics: Statistics::default(),+ file_schema: schema.clone(),+ statistics: Statistics::new_unknown(&schema),// file has 10 cols so index 12 should be monthprojection: None,limit: None,@@ -240,10 +242,11 @@ async fn test_prune(expected_row_pages_pruned: Option<usize>,expected_results: usize,) {- let output = ContextWithParquet::new(case_data_type, Page)- .await- .query(sql)- .await;+ let output: crate::parquet::TestOutput =+ ContextWithParquet::new(case_data_type, Page)+ .await+ .query(sql)+ .await;println!("{}", output.description());assert_eq!(output.predicate_evaluation_errors(), expected_errors);diff --git a/datafusion/core/tests/parquet/schema_coercion.rs b/datafusion/core/tests/parquet/schema_coercion.rsindex f7dace9930..b3134d470b 100644--- a/datafusion/core/tests/parquet/schema_coercion.rs+++ b/datafusion/core/tests/parquet/schema_coercion.rs@@ -15,6 +15,8 @@// specific language governing permissions and limitations// under the License.+use std::sync::Arc;+use arrow::datatypes::{Field, Schema};use arrow::record_batch::RecordBatch;use arrow_array::types::Int32Type;@@ -24,14 +26,13 @@ use datafusion::assert_batches_sorted_eq;use datafusion::datasource::physical_plan::{FileScanConfig, ParquetExec};use datafusion::physical_plan::collect;use datafusion::prelude::SessionContext;-use datafusion_common::Result;-use datafusion_common::Statistics;+use datafusion_common::{Result, Statistics};use datafusion_execution::object_store::ObjectStoreUrl;+use object_store::path::Path;use object_store::ObjectMeta;use parquet::arrow::ArrowWriter;use parquet::file::properties::WriterProperties;-use std::sync::Arc;use tempfile::NamedTempFile;/// Test for reading data from multiple parquet files with different schemas and coercing them into a single schema.@@ -62,8 +63,8 @@ async fn multi_parquet_coercion() {FileScanConfig {object_store_url: ObjectStoreUrl::local_filesystem(),file_groups: vec![file_groups],+ statistics: Statistics::new_unknown(&file_schema),file_schema,- statistics: Statistics::default(),projection: None,limit: None,table_partition_cols: vec![],@@ -126,8 +127,8 @@ async fn multi_parquet_coercion_projection() {FileScanConfig {object_store_url: ObjectStoreUrl::local_filesystem(),file_groups: vec![file_groups],+ statistics: Statistics::new_unknown(&file_schema),file_schema,- statistics: Statistics::default(),projection: Some(vec![1, 0, 2]),limit: None,table_partition_cols: vec![],diff --git a/datafusion/core/tests/path_partition.rs b/datafusion/core/tests/path_partition.rsindex 1c08733e3d..27d146de79 100644--- a/datafusion/core/tests/path_partition.rs+++ b/datafusion/core/tests/path_partition.rs@@ -17,16 +17,13 @@//! Test queries on partitioned datasets-use arrow::datatypes::DataType;use std::collections::BTreeSet;use std::fs::File;use std::io::{Read, Seek, SeekFrom};use std::ops::Range;use std::sync::Arc;-use async_trait::async_trait;-use bytes::Bytes;-use chrono::{TimeZone, Utc};+use arrow::datatypes::DataType;use datafusion::datasource::listing::ListingTableUrl;use datafusion::{assert_batches_sorted_eq,@@ -39,7 +36,12 @@ use datafusion::{prelude::SessionContext,test_util::{self, arrow_test_data, parquet_test_data},};+use datafusion_common::stats::Precision;use datafusion_common::ScalarValue;++use async_trait::async_trait;+use bytes::Bytes;+use chrono::{TimeZone, Utc};use futures::stream;use futures::stream::BoxStream;use object_store::{@@ -458,34 +460,30 @@ async fn parquet_statistics() -> Result<()> {//// NO PROJECTION ////let dataframe = ctx.sql("SELECT * FROM t").await?;let physical_plan = dataframe.create_physical_plan().await?;- assert_eq!(physical_plan.schema().fields().len(), 4);+ let schema = physical_plan.schema();+ assert_eq!(schema.fields().len(), 4);- let stat_cols = physical_plan- .statistics()- .column_statistics- .expect("col stats should be defined");+ let stat_cols = physical_plan.statistics()?.column_statistics;assert_eq!(stat_cols.len(), 4);// stats for the first col are read from the parquet file- assert_eq!(stat_cols[0].null_count, Some(3));+ assert_eq!(stat_cols[0].null_count, Precision::Exact(3));// TODO assert partition column (1,2,3) stats once implemented (#1186)- assert_eq!(stat_cols[1], ColumnStatistics::default());- assert_eq!(stat_cols[2], ColumnStatistics::default());- assert_eq!(stat_cols[3], ColumnStatistics::default());+ assert_eq!(stat_cols[1], ColumnStatistics::new_unknown(),);+ assert_eq!(stat_cols[2], ColumnStatistics::new_unknown(),);+ assert_eq!(stat_cols[3], ColumnStatistics::new_unknown(),);//// WITH PROJECTION ////let dataframe = ctx.sql("SELECT mycol, day FROM t WHERE day='28'").await?;let physical_plan = dataframe.create_physical_plan().await?;- assert_eq!(physical_plan.schema().fields().len(), 2);+ let schema = physical_plan.schema();+ assert_eq!(schema.fields().len(), 2);- let stat_cols = physical_plan- .statistics()- .column_statistics- .expect("col stats should be defined");+ let stat_cols = physical_plan.statistics()?.column_statistics;assert_eq!(stat_cols.len(), 2);// stats for the first col are read from the parquet file- assert_eq!(stat_cols[0].null_count, Some(1));+ assert_eq!(stat_cols[0].null_count, Precision::Exact(1));// TODO assert partition column stats once implemented (#1186)- assert_eq!(stat_cols[1], ColumnStatistics::default());+ assert_eq!(stat_cols[1], ColumnStatistics::new_unknown(),);Ok(())}diff --git a/datafusion/core/tests/sql/explain_analyze.rs b/datafusion/core/tests/sql/explain_analyze.rsindex de7ed19d3f..c328f46be7 100644--- a/datafusion/core/tests/sql/explain_analyze.rs+++ b/datafusion/core/tests/sql/explain_analyze.rs@@ -16,6 +16,7 @@// under the License.use super::*;+use datafusion::config::ConfigOptions;use datafusion::physical_plan::display::DisplayableExecutionPlan;use datafusion::physical_plan::metrics::Timestamp;@@ -826,5 +827,5 @@ async fn csv_explain_analyze_with_statistics() {.to_string();// should contain scan statistics- assert_contains!(&formatted, ", statistics=[]");+ assert_contains!(&formatted, ", statistics=[Rows=Absent, Bytes=Absent]");}diff --git a/datafusion/core/tests/user_defined/user_defined_plan.rs b/datafusion/core/tests/user_defined/user_defined_plan.rsindex d8c4bea174..d4a8842c0a 100644--- a/datafusion/core/tests/user_defined/user_defined_plan.rs+++ b/datafusion/core/tests/user_defined/user_defined_plan.rs@@ -58,7 +58,9 @@//! N elements, reducing the total amount of required buffer memory.//!-use futures::{Stream, StreamExt};+use std::fmt::Debug;+use std::task::{Context, Poll};+use std::{any::Any, collections::BTreeMap, fmt, sync::Arc};use arrow::{array::{Int64Array, StringArray},@@ -68,8 +70,7 @@ use arrow::{};use datafusion::{common::cast::{as_int64_array, as_string_array},- common::internal_err,- common::DFSchemaRef,+ common::{internal_err, DFSchemaRef},error::{DataFusionError, Result},execution::{context::{QueryPlanner, SessionState, TaskContext},@@ -89,11 +90,8 @@ use datafusion::{prelude::{SessionConfig, SessionContext},};-use fmt::Debug;-use std::task::{Context, Poll};-use std::{any::Any, collections::BTreeMap, fmt, sync::Arc};-use async_trait::async_trait;+use futures::{Stream, StreamExt};/// Execute the specified sql and return the resulting record batches/// pretty printed as a String.@@ -490,10 +488,10 @@ impl ExecutionPlan for TopKExec {}))}- fn statistics(&self) -> Statistics {+ fn statistics(&self) -> Result<Statistics> {// to improve the optimizability of this plan// better statistics inference could be provided- Statistics::default()+ Ok(Statistics::new_unknown(&self.schema()))}}diff --git a/datafusion/execution/src/cache/cache_unit.rs b/datafusion/execution/src/cache/cache_unit.rsindex c432a67587..4a21dc02bd 100644--- a/datafusion/execution/src/cache/cache_unit.rs+++ b/datafusion/execution/src/cache/cache_unit.rs@@ -15,12 +15,15 @@// specific language governing permissions and limitations// under the License.+use std::sync::Arc;+use crate::cache::CacheAccessor;-use dashmap::DashMap;+use datafusion_common::Statistics;++use dashmap::DashMap;use object_store::path::Path;use object_store::ObjectMeta;-use std::sync::Arc;/// Collected statistics for files/// Cache is invalided when file size or last modification has changed@@ -158,6 +161,7 @@ impl CacheAccessor<Path, Arc<Vec<ObjectMeta>>> for DefaultListFilesCache {mod tests {use crate::cache::cache_unit::{DefaultFileStatisticsCache, DefaultListFilesCache};use crate::cache::CacheAccessor;+ use arrow::datatypes::{DataType, Field, Schema, TimeUnit};use chrono::DateTime;use datafusion_common::Statistics;use object_store::path::Path;@@ -173,11 +177,19 @@ mod tests {size: 1024,e_tag: None,};-let cache = DefaultFileStatisticsCache::default();assert!(cache.get_with_extra(&meta.location, &meta).is_none());- cache.put_with_extra(&meta.location, Statistics::default().into(), &meta);+ cache.put_with_extra(+ &meta.location,+ Statistics::new_unknown(&Schema::new(vec![Field::new(+ "test_column",+ DataType::Timestamp(TimeUnit::Second, None),+ false,+ )]))+ .into(),+ &meta,+ );assert!(cache.get_with_extra(&meta.location, &meta).is_some());// file size changeddiff --git a/datafusion/physical-expr/src/analysis.rs b/datafusion/physical-expr/src/analysis.rsindex 990c643c6b..93c24014fd 100644--- a/datafusion/physical-expr/src/analysis.rs+++ b/datafusion/physical-expr/src/analysis.rs@@ -17,6 +17,9 @@//! Interval and selectivity in [`AnalysisContext`]+use std::fmt::Debug;+use std::sync::Arc;+use crate::expressions::Column;use crate::intervals::cp_solver::PropagationResult;use crate::intervals::{cardinality_ratio, ExprIntervalGraph, Interval, IntervalBound};@@ -24,20 +27,18 @@ use crate::utils::collect_columns;use crate::PhysicalExpr;use arrow::datatypes::Schema;+use datafusion_common::stats::Precision;use datafusion_common::{internal_err, ColumnStatistics, DataFusionError, Result, ScalarValue,};-use std::fmt::Debug;-use std::sync::Arc;-/// The shared context used during the analysis of an expression. Includes/// the boundaries for all known columns.#[derive(Clone, Debug, PartialEq)]pub struct AnalysisContext {// A list of known column boundaries, ordered by the index// of the column in the current schema.- pub boundaries: Option<Vec<ExprBoundaries>>,+ pub boundaries: Vec<ExprBoundaries>,/// The estimated percentage of rows that this expression would select, if/// it were to be used as a boolean predicate on a filter. The value will be/// between 0.0 (selects nothing) and 1.0 (selects everything).@@ -47,7 +48,7 @@ pub struct AnalysisContext {impl AnalysisContext {pub fn new(boundaries: Vec<ExprBoundaries>) -> Self {Self {- boundaries: Some(boundaries),+ boundaries,selectivity: None,}}@@ -58,19 +59,16 @@ impl AnalysisContext {}/// Create a new analysis context from column statistics.- pub fn from_statistics(+ pub fn try_from_statistics(input_schema: &Schema,statistics: &[ColumnStatistics],- ) -> Self {- let mut column_boundaries = vec![];- for (idx, stats) in statistics.iter().enumerate() {- column_boundaries.push(ExprBoundaries::from_column(- stats,- input_schema.fields()[idx].name().clone(),- idx,- ));- }- Self::new(column_boundaries)+ ) -> Result<Self> {+ statistics+ .iter()+ .enumerate()+ .map(|(idx, stats)| ExprBoundaries::try_from_column(input_schema, stats, idx))+ .collect::<Result<Vec<_>>>()+ .map(Self::new)}}@@ -82,24 +80,40 @@ pub struct ExprBoundaries {/// Minimum and maximum values this expression can have.pub interval: Interval,/// Maximum number of distinct values this expression can produce, if known.- pub distinct_count: Option<usize>,+ pub distinct_count: Precision<usize>,}impl ExprBoundaries {/// Create a new `ExprBoundaries` object from column level statistics.- pub fn from_column(stats: &ColumnStatistics, col: String, index: usize) -> Self {- Self {- column: Column::new(&col, index),- interval: Interval::new(- IntervalBound::new_closed(- stats.min_value.clone().unwrap_or(ScalarValue::Null),- ),- IntervalBound::new_closed(- stats.max_value.clone().unwrap_or(ScalarValue::Null),- ),+ pub fn try_from_column(+ schema: &Schema,+ col_stats: &ColumnStatistics,+ col_index: usize,+ ) -> Result<Self> {+ let field = &schema.fields()[col_index];+ let empty_field = ScalarValue::try_from(field.data_type())?;+ let interval = Interval::new(+ IntervalBound::new_closed(+ col_stats+ .min_value+ .get_value()+ .cloned()+ .unwrap_or(empty_field.clone()),),- distinct_count: stats.distinct_count,- }+ IntervalBound::new_closed(+ col_stats+ .max_value+ .get_value()+ .cloned()+ .unwrap_or(empty_field),+ ),+ );+ let column = Column::new(field.name(), col_index);+ Ok(ExprBoundaries {+ column,+ interval,+ distinct_count: col_stats.distinct_count.clone(),+ })}}@@ -122,9 +136,7 @@ pub fn analyze(expr: &Arc<dyn PhysicalExpr>,context: AnalysisContext,) -> Result<AnalysisContext> {- let target_boundaries = context.boundaries.ok_or_else(|| {- DataFusionError::Internal("No column exists at the input to filter".to_string())- })?;+ let target_boundaries = context.boundaries;let mut graph = ExprIntervalGraph::try_new(expr.clone())?;@@ -148,18 +160,22 @@ pub fn analyze(})}).collect();-- match graph.update_ranges(&mut target_indices_and_boundaries)? {- PropagationResult::Success => {- shrink_boundaries(expr, graph, target_boundaries, target_expr_and_indices)- }- PropagationResult::Infeasible => {- Ok(AnalysisContext::new(target_boundaries).with_selectivity(0.0))- }- PropagationResult::CannotPropagate => {- Ok(AnalysisContext::new(target_boundaries).with_selectivity(1.0))- }- }+ Ok(+ match graph.update_ranges(&mut target_indices_and_boundaries)? {+ PropagationResult::Success => shrink_boundaries(+ expr,+ graph,+ target_boundaries,+ target_expr_and_indices,+ )?,+ PropagationResult::Infeasible => {+ AnalysisContext::new(target_boundaries).with_selectivity(0.0)+ }+ PropagationResult::CannotPropagate => {+ AnalysisContext::new(target_boundaries).with_selectivity(1.0)+ }+ },+ )}/// If the `PropagationResult` indicates success, this function calculates the@@ -184,24 +200,19 @@ fn shrink_boundaries(}});let graph_nodes = graph.gather_node_indices(&[expr.clone()]);- let (_, root_index) = graph_nodes.first().ok_or_else(|| {- DataFusionError::Internal("Error in constructing predicate graph".to_string())- })?;+ let Some((_, root_index)) = graph_nodes.get(0) else {+ return internal_err!(+ "The ExprIntervalGraph under investigation does not have any nodes."+ );+ };let final_result = graph.get_interval(*root_index);- // If during selectivity calculation we encounter an error, use 1.0 as cardinality estimate- // safest estimate(e.q largest possible value).let selectivity = calculate_selectivity(&final_result.lower.value,&final_result.upper.value,&target_boundaries,&initial_boundaries,- )- .unwrap_or(1.0);-- if !(0.0..=1.0).contains(&selectivity) {- return internal_err!("Selectivity is out of limit: {}", selectivity);- }+ )?;Ok(AnalysisContext::new(target_boundaries).with_selectivity(selectivity))}diff --git a/datafusion/physical-expr/src/expressions/in_list.rs b/datafusion/physical-expr/src/expressions/in_list.rsindex bdc476f5b3..643bbfd820 100644--- a/datafusion/physical-expr/src/expressions/in_list.rs+++ b/datafusion/physical-expr/src/expressions/in_list.rs@@ -17,8 +17,6 @@//! Implementation of `InList` expressions: [`InListExpr`]-use ahash::RandomState;-use datafusion_common::exec_err;use std::any::Any;use std::fmt::Debug;use std::hash::{Hash, Hasher};@@ -27,6 +25,7 @@ use std::sync::Arc;use crate::physical_expr::down_cast_any_ref;use crate::utils::expr_list_eq_any_order;use crate::PhysicalExpr;+use arrow::array::*;use arrow::buffer::BooleanBuffer;use arrow::compute::kernels::boolean::{not, or_kleene};@@ -36,12 +35,16 @@ use arrow::datatypes::*;use arrow::record_batch::RecordBatch;use arrow::util::bit_iterator::BitIndexIterator;use arrow::{downcast_dictionary_array, downcast_primitive_array};+use datafusion_common::cast::{+ as_boolean_array, as_generic_binary_array, as_string_array,+};use datafusion_common::hash_utils::HashValue;use datafusion_common::{- cast::{as_boolean_array, as_generic_binary_array, as_string_array},- internal_err, not_impl_err, DataFusionError, Result, ScalarValue,+ exec_err, internal_err, not_impl_err, DataFusionError, Result, ScalarValue,};use datafusion_expr::ColumnarValue;++use ahash::RandomState;use hashbrown::hash_map::RawEntryMut;use hashbrown::HashMap;diff --git a/datafusion/physical-expr/src/expressions/negative.rs b/datafusion/physical-expr/src/expressions/negative.rsindex c66c91ef31..86b000e76a 100644--- a/datafusion/physical-expr/src/expressions/negative.rs+++ b/datafusion/physical-expr/src/expressions/negative.rs@@ -106,7 +106,7 @@ impl PhysicalExpr for NegativeExpr {/// Given the child interval of a NegativeExpr, it calculates the NegativeExpr's interval./// It replaces the upper and lower bounds after multiplying them with -1.- /// Ex: `(a, b] => [-b, -a)``+ /// Ex: `(a, b]` => `[-b, -a)`fn evaluate_bounds(&self, children: &[&Interval]) -> Result<Interval> {Ok(Interval::new(children[0].upper.negate()?,@@ -171,7 +171,6 @@ mod tests {expressions::{col, Column},intervals::Interval,};- #[allow(unused_imports)]use arrow::array::*;use arrow::datatypes::*;use arrow_schema::DataType::{Float32, Float64, Int16, Int32, Int64, Int8};diff --git a/datafusion/physical-expr/src/intervals/interval_aritmetic.rs b/datafusion/physical-expr/src/intervals/interval_aritmetic.rsindex 277124ea22..1ea9b2d9ae 100644--- a/datafusion/physical-expr/src/intervals/interval_aritmetic.rs+++ b/datafusion/physical-expr/src/intervals/interval_aritmetic.rs@@ -17,20 +17,20 @@//! Interval arithmetic library+use std::borrow::Borrow;+use std::fmt::{self, Display, Formatter};+use std::ops::{AddAssign, SubAssign};+use crate::aggregate::min_max::{max, min};use crate::intervals::rounding::{alter_fp_rounding_mode, next_down, next_up};+use arrow::compute::{cast_with_options, CastOptions};use arrow::datatypes::DataType;use arrow_array::ArrowNativeTypeOp;-use datafusion_common::{exec_err, internal_err, DataFusionError, Result, ScalarValue};+use datafusion_common::{internal_err, DataFusionError, Result, ScalarValue};use datafusion_expr::type_coercion::binary::get_result_type;use datafusion_expr::Operator;-use std::borrow::Borrow;-use std::fmt;-use std::fmt::{Display, Formatter};-use std::ops::{AddAssign, SubAssign};-/// This type represents a single endpoint of an [`Interval`]. An/// endpoint can be open (does not include the endpoint) or closed/// (includes the endpoint).@@ -87,7 +87,7 @@ impl IntervalBound {}/// Returns a new bound with a negated value, if any, and the same open/closed.- /// For example negating `[5` would return `[-5`, or `-1)` would return `1)`+ /// For example negating `[5` would return `[-5`, or `-1)` would return `1)`.pub fn negate(&self) -> Result<IntervalBound> {self.value.arithmetic_negate().map(|value| IntervalBound {value,@@ -535,60 +535,77 @@ impl Interval {};/// Returns the cardinality of this interval, which is the number of all- /// distinct points inside it.- pub fn cardinality(&self) -> Result<u64> {- match self.get_datatype() {- Ok(data_type) if data_type.is_integer() => {- if let Some(diff) = self.upper.value.distance(&self.lower.value) {- Ok(calculate_cardinality_based_on_bounds(+ /// distinct points inside it. This function returns `None` if:+ /// - The interval is unbounded from either side, or+ /// - Cardinality calculations for the datatype in question is not+ /// implemented yet, or+ /// - An overflow occurs during the calculation.+ ///+ /// This function returns an error if the given interval is malformed.+ pub fn cardinality(&self) -> Result<Option<u64>> {+ let data_type = self.get_datatype()?;+ if data_type.is_integer() {+ Ok(self.upper.value.distance(&self.lower.value).map(|diff| {+ calculate_cardinality_based_on_bounds(+ self.lower.open,+ self.upper.open,+ diff as u64,+ )+ }))+ }+ // Ordering floating-point numbers according to their binary representations+ // coincide with their natural ordering. Therefore, we can consider their+ // binary representations as "indices" and subtract them. For details, see:+ // https://stackoverflow.com/questions/8875064/how-many-distinct-floating-point-numbers-in-a-specific-range+ else if data_type.is_floating() {+ match (&self.lower.value, &self.upper.value) {+ (+ ScalarValue::Float32(Some(lower)),+ ScalarValue::Float32(Some(upper)),+ ) => {+ // Negative numbers are sorted in the reverse order. To always have a positive difference after the subtraction,+ // we perform following transformation:+ let lower_bits = lower.to_bits() as i32;+ let upper_bits = upper.to_bits() as i32;+ let transformed_lower =+ lower_bits ^ ((lower_bits >> 31) & 0x7fffffff);+ let transformed_upper =+ upper_bits ^ ((upper_bits >> 31) & 0x7fffffff);+ let Ok(count) = transformed_upper.sub_checked(transformed_lower)+ else {+ return Ok(None);+ };+ Ok(Some(calculate_cardinality_based_on_bounds(self.lower.open,self.upper.open,- diff as u64,- ))- } else {- exec_err!("Cardinality cannot be calculated for {:?}", self)+ count as u64,+ )))}- }- // Ordering floating-point numbers according to their binary representations- // coincide with their natural ordering. Therefore, we can consider their- // binary representations as "indices" and subtract them. For details, see:- // https://stackoverflow.com/questions/8875064/how-many-distinct-floating-point-numbers-in-a-specific-range- Ok(data_type) if data_type.is_floating() => {- // If the minimum value is a negative number, we need to- // switch sides to ensure an unsigned result.- let (min, max) = if self.lower.value- < ScalarValue::new_zero(&self.lower.value.data_type())?- {- (self.upper.value.clone(), self.lower.value.clone())- } else {- (self.lower.value.clone(), self.upper.value.clone())- };-- match (min, max) {- (- ScalarValue::Float32(Some(lower)),- ScalarValue::Float32(Some(upper)),- ) => Ok(calculate_cardinality_based_on_bounds(- self.lower.open,- self.upper.open,- (upper.to_bits().sub_checked(lower.to_bits()))? as u64,- )),- (- ScalarValue::Float64(Some(lower)),- ScalarValue::Float64(Some(upper)),- ) => Ok(calculate_cardinality_based_on_bounds(+ (+ ScalarValue::Float64(Some(lower)),+ ScalarValue::Float64(Some(upper)),+ ) => {+ let lower_bits = lower.to_bits() as i64;+ let upper_bits = upper.to_bits() as i64;+ let transformed_lower =+ lower_bits ^ ((lower_bits >> 63) & 0x7fffffffffffffff);+ let transformed_upper =+ upper_bits ^ ((upper_bits >> 63) & 0x7fffffffffffffff);+ let Ok(count) = transformed_upper.sub_checked(transformed_lower)+ else {+ return Ok(None);+ };+ Ok(Some(calculate_cardinality_based_on_bounds(self.lower.open,self.upper.open,- upper.to_bits().sub_checked(lower.to_bits())?,- )),- _ => exec_err!(- "Cardinality cannot be calculated for the datatype {:?}",- data_type- ),+ count as u64,+ )))}+ _ => Ok(None),}- // If the cardinality cannot be calculated anyway, give an error.- _ => exec_err!("Cardinality cannot be calculated for {:?}", self),+ } else {+ // Cardinality calculations are not implemented for this data type yet:+ Ok(None)}}@@ -691,12 +708,25 @@ fn next_value<const INC: bool>(value: ScalarValue) -> ScalarValue {}}-/// This function computes the cardinality ratio of the given intervals.+/// This function computes the selectivity of an operation by computing the+/// cardinality ratio of the given input/output intervals. If this can not be+/// calculated for some reason, it returns `1.0` meaning fullly selective (no+/// filtering).pub fn cardinality_ratio(initial_interval: &Interval,final_interval: &Interval,) -> Result<f64> {- Ok(final_interval.cardinality()? as f64 / initial_interval.cardinality()? as f64)+ Ok(+ match (+ final_interval.cardinality()?,+ initial_interval.cardinality()?,+ ) {+ (Some(final_interval), Some(initial_interval)) => {+ final_interval as f64 / initial_interval as f64+ }+ _ => 1.0,+ },+ )}pub fn apply_operator(op: &Operator, lhs: &Interval, rhs: &Interval) -> Result<Interval> {@@ -1018,7 +1048,6 @@ impl NullableInterval {mod tests {use super::next_value;use crate::intervals::{Interval, IntervalBound};-use arrow_schema::DataType;use datafusion_common::{Result, ScalarValue};@@ -1716,7 +1745,7 @@ mod tests {),];for interval in intervals {- assert_eq!(interval.cardinality()?, distinct_f64);+ assert_eq!(interval.cardinality()?.unwrap(), distinct_f64);}let intervals = [@@ -1730,20 +1759,26 @@ mod tests {),];for interval in intervals {- assert_eq!(interval.cardinality()?, distinct_f32);+ assert_eq!(interval.cardinality()?.unwrap(), distinct_f32);}+ // The regular logarithmic distribution of floating-point numbers are+ // only applicable outside of the `(-phi, phi)` interval where `phi`+ // denotes the largest positive subnormal floating-point number. Since+ // the following intervals include such subnormal points, we cannot use+ // a simple powers-of-two type formula for our expectations. Therefore,+ // we manually supply the actual expected cardinality.let interval = Interval::new(IntervalBound::new(ScalarValue::from(-0.0625), false),IntervalBound::new(ScalarValue::from(0.0625), true),);- assert_eq!(interval.cardinality()?, distinct_f64 * 2_048);+ assert_eq!(interval.cardinality()?.unwrap(), 9178336040581070849);let interval = Interval::new(IntervalBound::new(ScalarValue::from(-0.0625_f32), false),IntervalBound::new(ScalarValue::from(0.0625_f32), true),);- assert_eq!(interval.cardinality()?, distinct_f32 * 256);+ assert_eq!(interval.cardinality()?.unwrap(), 2063597569);Ok(())}diff --git a/datafusion/physical-expr/src/intervals/utils.rs b/datafusion/physical-expr/src/intervals/utils.rsindex 2ddbca4073..7a4ccff950 100644--- a/datafusion/physical-expr/src/intervals/utils.rs+++ b/datafusion/physical-expr/src/intervals/utils.rs@@ -17,17 +17,18 @@//! Utility functions for the interval arithmetic library+use std::sync::Arc;+use super::{Interval, IntervalBound};use crate::{expressions::{BinaryExpr, CastExpr, Column, Literal, NegativeExpr},PhysicalExpr,};+use arrow_schema::{DataType, SchemaRef};use datafusion_common::{DataFusionError, Result, ScalarValue};use datafusion_expr::Operator;-use std::sync::Arc;-const MDN_DAY_MASK: i128 = 0xFFFF_FFFF_0000_0000_0000_0000;const MDN_NS_MASK: i128 = 0xFFFF_FFFF_FFFF_FFFF;const DT_MS_MASK: i64 = 0xFFFF_FFFF;diff --git a/datafusion/physical-expr/src/lib.rs b/datafusion/physical-expr/src/lib.rsindex 48d5f4e130..e670380e59 100644--- a/datafusion/physical-expr/src/lib.rs+++ b/datafusion/physical-expr/src/lib.rs@@ -48,9 +48,6 @@ pub mod utils;pub mod var_provider;pub mod window;-// For backwards compatibility-pub use datafusion_common::hash_utils;-pub use aggregate::groups_accumulator::{EmitTo, GroupsAccumulator, GroupsAccumulatorAdapter,};diff --git a/datafusion/physical-plan/src/aggregates/mod.rs b/datafusion/physical-plan/src/aggregates/mod.rsindex 3c9dabf605..7191d51fb7 100644--- a/datafusion/physical-plan/src/aggregates/mod.rs+++ b/datafusion/physical-plan/src/aggregates/mod.rs@@ -17,8 +17,14 @@//! Aggregates functionalities+use std::any::Any;+use std::collections::HashMap;+use std::sync::Arc;++use super::DisplayAs;use crate::aggregates::{no_grouping::AggregateStream, row_hash::GroupedHashAggregateStream,+ topk_stream::GroupedTopKAggregateStream,};use crate::metrics::{ExecutionPlanMetricsSet, MetricsSet};use crate::{@@ -29,23 +35,25 @@ use crate::{use arrow::array::ArrayRef;use arrow::datatypes::{Field, Schema, SchemaRef};use arrow::record_batch::RecordBatch;+use datafusion_common::stats::Precision;use datafusion_common::utils::longest_consecutive_prefix;use datafusion_common::{not_impl_err, plan_err, DataFusionError, Result};use datafusion_execution::TaskContext;use datafusion_expr::Accumulator;+use datafusion_physical_expr::utils::{+ convert_to_expr, get_finer_ordering, get_indices_of_matching_exprs,+ ordering_satisfy_requirement_concrete,+};use datafusion_physical_expr::{+ aggregate::is_order_sensitive,equivalence::project_equivalence_properties,- expressions::Column,+ expressions::{Column, Max, Min},normalize_out_expr_with_columns_map, physical_exprs_contains, reverse_order_bys,- utils::{convert_to_expr, get_indices_of_matching_exprs},AggregateExpr, LexOrdering, LexOrderingReq, OrderingEquivalenceProperties,PhysicalExpr, PhysicalSortExpr, PhysicalSortRequirement,};use itertools::Itertools;-use std::any::Any;-use std::collections::HashMap;-use std::sync::Arc;mod group_values;mod no_grouping;@@ -54,16 +62,8 @@ mod row_hash;mod topk;mod topk_stream;-use crate::aggregates::topk_stream::GroupedTopKAggregateStream;pub use datafusion_expr::AggregateFunction;-use datafusion_physical_expr::aggregate::is_order_sensitive;pub use datafusion_physical_expr::expressions::create_aggregate_expr;-use datafusion_physical_expr::expressions::{Max, Min};-use datafusion_physical_expr::utils::{- get_finer_ordering, ordering_satisfy_requirement_concrete,-};--use super::DisplayAs;/// Hash aggregate modes#[derive(Debug, Copy, Clone, PartialEq, Eq)]@@ -1005,36 +1005,49 @@ impl ExecutionPlan for AggregateExec {Some(self.metrics.clone_inner())}- fn statistics(&self) -> Statistics {+ fn statistics(&self) -> Result<Statistics> {// TODO stats: group expressions:// - once expressions will be able to compute their own stats, use it here// - case where we group by on a column for which with have the `distinct` stat// TODO stats: aggr expression:// - aggregations somtimes also preserve invariants such as min, max...+ let column_statistics = Statistics::unknown_column(&self.schema());match self.mode {AggregateMode::Final | AggregateMode::FinalPartitionedif self.group_by.expr.is_empty() =>{- Statistics {- num_rows: Some(1),- is_exact: true,- ..Default::default()- }+ Ok(Statistics {+ num_rows: Precision::Exact(1),+ column_statistics,+ total_byte_size: Precision::Absent,+ })}_ => {- let input_stats = self.input.statistics();- // Input statistics is exact and number of rows not greater than 1:- let is_exact = input_stats.is_exact- && (input_stats- .num_rows- .map(|num_rows| num_rows == 1)- .unwrap_or(false));- Statistics {- // the output row count is surely not larger than its input row count- num_rows: self.input.statistics().num_rows,- is_exact,- ..Default::default()- }+ // When the input row count is 0 or 1, we can adopt that statistic keeping its reliability.+ // When it is larger than 1, we degrade the precision since it may decrease after aggregation.+ let num_rows = if let Some(value) =+ self.input().statistics()?.num_rows.get_value()+ {+ if *value > 1 {+ self.input().statistics()?.num_rows.to_inexact()+ } else if *value == 0 {+ // Aggregation on an empty table creates a null row.+ self.input()+ .statistics()?+ .num_rows+ .add(&Precision::Exact(1))+ } else {+ // num_rows = 1 case+ self.input().statistics()?.num_rows+ }+ } else {+ Precision::Absent+ };+ Ok(Statistics {+ num_rows,+ column_statistics,+ total_byte_size: Precision::Absent,+ })}}}@@ -1835,9 +1848,13 @@ mod tests {Ok(Box::pin(stream))}- fn statistics(&self) -> Statistics {+ fn statistics(&self) -> Result<Statistics> {let (_, batches) = some_data();- common::compute_record_batch_statistics(&[batches], &self.schema(), None)+ Ok(common::compute_record_batch_statistics(+ &[batches],+ &self.schema(),+ None,+ ))}}diff --git a/datafusion/physical-plan/src/analyze.rs b/datafusion/physical-plan/src/analyze.rsindex 3a2cac59cf..bce2425135 100644--- a/datafusion/physical-plan/src/analyze.rs+++ b/datafusion/physical-plan/src/analyze.rs@@ -20,19 +20,19 @@use std::sync::Arc;use std::{any::Any, time::Instant};-use crate::{- display::DisplayableExecutionPlan, DisplayFormatType, ExecutionPlan, Partitioning,- Statistics,-};-use arrow::{array::StringBuilder, datatypes::SchemaRef, record_batch::RecordBatch};-use datafusion_common::{internal_err, DataFusionError, Result};-use futures::StreamExt;-use super::expressions::PhysicalSortExpr;use super::stream::{RecordBatchReceiverStream, RecordBatchStreamAdapter};use super::{DisplayAs, Distribution, SendableRecordBatchStream};++use crate::display::DisplayableExecutionPlan;+use crate::{DisplayFormatType, ExecutionPlan, Partitioning, Statistics};++use arrow::{array::StringBuilder, datatypes::SchemaRef, record_batch::RecordBatch};+use datafusion_common::{internal_err, DataFusionError, Result};use datafusion_execution::TaskContext;+use futures::StreamExt;+/// `EXPLAIN ANALYZE` execution plan operator. This operator runs its input,/// discards the results, and then prints out an annotated plan with metrics#[derive(Debug, Clone)]@@ -196,9 +196,9 @@ impl ExecutionPlan for AnalyzeExec {)))}- fn statistics(&self) -> Statistics {+ fn statistics(&self) -> Result<Statistics> {// Statistics an an ANALYZE plan are not relevant- Statistics::default()+ Ok(Statistics::new_unknown(&self.schema()))}}diff --git a/datafusion/physical-plan/src/coalesce_batches.rs b/datafusion/physical-plan/src/coalesce_batches.rsindex f46a228064..df9e8a8a2b 100644--- a/datafusion/physical-plan/src/coalesce_batches.rs+++ b/datafusion/physical-plan/src/coalesce_batches.rs@@ -23,6 +23,9 @@ use std::pin::Pin;use std::sync::Arc;use std::task::{Context, Poll};+use super::expressions::PhysicalSortExpr;+use super::metrics::{BaselineMetrics, ExecutionPlanMetricsSet, MetricsSet};+use super::{DisplayAs, Statistics};use crate::{DisplayFormatType, EquivalenceProperties, ExecutionPlan, Partitioning,RecordBatchStream, SendableRecordBatchStream,@@ -35,11 +38,6 @@ use datafusion_common::Result;use datafusion_execution::TaskContext;use datafusion_physical_expr::OrderingEquivalenceProperties;-use super::expressions::PhysicalSortExpr;-use super::metrics::{BaselineMetrics, MetricsSet};-use super::DisplayAs;-use super::{metrics::ExecutionPlanMetricsSet, Statistics};-use futures::stream::{Stream, StreamExt};use log::trace;@@ -174,7 +172,7 @@ impl ExecutionPlan for CoalesceBatchesExec {Some(self.metrics.clone_inner())}- fn statistics(&self) -> Statistics {+ fn statistics(&self) -> Result<Statistics> {self.input.statistics()}}diff --git a/datafusion/physical-plan/src/coalesce_partitions.rs b/datafusion/physical-plan/src/coalesce_partitions.rsindex 646d42795b..f09b33e3f3 100644--- a/datafusion/physical-plan/src/coalesce_partitions.rs+++ b/datafusion/physical-plan/src/coalesce_partitions.rs@@ -163,7 +163,7 @@ impl ExecutionPlan for CoalescePartitionsExec {Some(self.metrics.clone_inner())}- fn statistics(&self) -> Statistics {+ fn statistics(&self) -> Result<Statistics> {self.input.statistics()}}diff --git a/datafusion/physical-plan/src/common.rs b/datafusion/physical-plan/src/common.rsindex c6cfbbfbba..649f3a31aa 100644--- a/datafusion/physical-plan/src/common.rs+++ b/datafusion/physical-plan/src/common.rs@@ -17,24 +17,28 @@//! Defines common code used in execution plans+use std::fs;+use std::fs::{metadata, File};+use std::path::{Path, PathBuf};+use std::sync::Arc;+use std::task::{Context, Poll};+use super::SendableRecordBatchStream;use crate::stream::RecordBatchReceiverStream;use crate::{ColumnStatistics, ExecutionPlan, Statistics};+use arrow::datatypes::Schema;use arrow::ipc::writer::{FileWriter, IpcWriteOptions};use arrow::record_batch::RecordBatch;+use datafusion_common::stats::Precision;use datafusion_common::{plan_err, DataFusionError, Result};use datafusion_execution::memory_pool::MemoryReservation;use datafusion_physical_expr::expressions::{BinaryExpr, Column};use datafusion_physical_expr::{PhysicalExpr, PhysicalSortExpr};+use futures::{Future, StreamExt, TryStreamExt};use parking_lot::Mutex;use pin_project_lite::pin_project;-use std::fs;-use std::fs::{metadata, File};-use std::path::{Path, PathBuf};-use std::sync::Arc;-use std::task::{Context, Poll};use tokio::task::JoinHandle;/// [`MemoryReservation`] used across query execution streams@@ -146,22 +150,21 @@ pub fn compute_record_batch_statistics(None => (0..schema.fields().len()).collect(),};- let mut column_statistics = vec![ColumnStatistics::default(); projection.len()];+ let mut column_statistics = vec![ColumnStatistics::new_unknown(); projection.len()];for partition in batches.iter() {for batch in partition {for (stat_index, col_index) in projection.iter().enumerate() {- *column_statistics[stat_index].null_count.get_or_insert(0) +=- batch.column(*col_index).null_count();+ column_statistics[stat_index].null_count =+ Precision::Exact(batch.column(*col_index).null_count());}}}Statistics {- num_rows: Some(nb_rows),- total_byte_size: Some(total_byte_size),- column_statistics: Some(column_statistics),- is_exact: true,+ num_rows: Precision::Exact(nb_rows),+ total_byte_size: Precision::Exact(total_byte_size),+ column_statistics,}}@@ -378,6 +381,7 @@ mod tests {use crate::memory::MemoryExec;use crate::sorts::sort::SortExec;use crate::union::UnionExec;+use arrow::compute::SortOptions;use arrow::{array::{Float32Array, Float64Array},@@ -671,9 +675,8 @@ mod tests {]));let stats = compute_record_batch_statistics(&[], &schema, Some(vec![0, 1]));- assert_eq!(stats.num_rows, Some(0));- assert!(stats.is_exact);- assert_eq!(stats.total_byte_size, Some(0));+ assert_eq!(stats.num_rows, Precision::Exact(0));+ assert_eq!(stats.total_byte_size, Precision::Exact(0));Ok(())}@@ -694,27 +697,26 @@ mod tests {compute_record_batch_statistics(&[vec![batch]], &schema, Some(vec![0, 1]));let mut expected = Statistics {- is_exact: true,- num_rows: Some(3),- total_byte_size: Some(464), // this might change a bit if the way we compute the size changes- column_statistics: Some(vec![+ num_rows: Precision::Exact(3),+ total_byte_size: Precision::Exact(464), // this might change a bit if the way we compute the size changes+ column_statistics: vec![ColumnStatistics {- distinct_count: None,- max_value: None,- min_value: None,- null_count: Some(0),+ distinct_count: Precision::Absent,+ max_value: Precision::Absent,+ min_value: Precision::Absent,+ null_count: Precision::Exact(0),},ColumnStatistics {- distinct_count: None,- max_value: None,- min_value: None,- null_count: Some(0),+ distinct_count: Precision::Absent,+ max_value: Precision::Absent,+ min_value: Precision::Absent,+ null_count: Precision::Exact(0),},- ]),+ ],};// Prevent test flakiness due to undefined / changing implementation details- expected.total_byte_size = actual.total_byte_size;+ expected.total_byte_size = actual.total_byte_size.clone();assert_eq!(actual, expected);Ok(())diff --git a/datafusion/physical-plan/src/display.rs b/datafusion/physical-plan/src/display.rsindex e4a4e113eb..aa368251eb 100644--- a/datafusion/physical-plan/src/display.rs+++ b/datafusion/physical-plan/src/display.rs@@ -20,13 +20,12 @@use std::fmt;+use super::{accept, ExecutionPlan, ExecutionPlanVisitor};+use arrow_schema::SchemaRef;-use datafusion_common::display::StringifiedPlan;+use datafusion_common::display::{GraphvizBuilder, PlanType, StringifiedPlan};use datafusion_physical_expr::PhysicalSortExpr;-use super::{accept, ExecutionPlan, ExecutionPlanVisitor};-use datafusion_common::display::{GraphvizBuilder, PlanType};-/// Options for controlling how each [`ExecutionPlan`] should format itself#[derive(Debug, Clone, Copy)]pub enum DisplayFormatType {@@ -261,8 +260,9 @@ impl<'a, 'b> ExecutionPlanVisitor for IndentVisitor<'a, 'b> {}}}+ let stats = plan.statistics().map_err(|_e| fmt::Error)?;if self.show_statistics {- write!(self.f, ", statistics=[{}]", plan.statistics())?;+ write!(self.f, ", statistics=[{}]", stats)?;}writeln!(self.f)?;self.indent += 1;@@ -341,8 +341,9 @@ impl ExecutionPlanVisitor for GraphvizVisitor<'_, '_> {}};+ let stats = plan.statistics().map_err(|_e| fmt::Error)?;let statistics = if self.show_statistics {- format!("statistics=[{}]", plan.statistics())+ format!("statistics=[{}]", stats)} else {"".to_string()};diff --git a/datafusion/physical-plan/src/empty.rs b/datafusion/physical-plan/src/empty.rsindex 675dac9ad2..a3e1fb79ed 100644--- a/datafusion/physical-plan/src/empty.rs+++ b/datafusion/physical-plan/src/empty.rs@@ -20,18 +20,18 @@use std::any::Any;use std::sync::Arc;+use super::expressions::PhysicalSortExpr;+use super::{common, DisplayAs, SendableRecordBatchStream, Statistics};use crate::{memory::MemoryStream, DisplayFormatType, ExecutionPlan, Partitioning};+use arrow::array::{ArrayRef, NullArray};use arrow::datatypes::{DataType, Field, Fields, Schema, SchemaRef};use arrow::record_batch::RecordBatch;use datafusion_common::{internal_err, DataFusionError, Result};-use log::trace;--use super::expressions::PhysicalSortExpr;-use super::{common, DisplayAs, SendableRecordBatchStream, Statistics};-use datafusion_execution::TaskContext;+use log::trace;+/// Execution plan for empty relation (produces no rows)#[derive(Debug)]pub struct EmptyExec {@@ -161,11 +161,15 @@ impl ExecutionPlan for EmptyExec {)?))}- fn statistics(&self) -> Statistics {+ fn statistics(&self) -> Result<Statistics> {let batch = self.data().expect("Create empty RecordBatch should not fail");- common::compute_record_batch_statistics(&[batch], &self.schema, None)+ Ok(common::compute_record_batch_statistics(+ &[batch],+ &self.schema,+ None,+ ))}}diff --git a/datafusion/physical-plan/src/explain.rs b/datafusion/physical-plan/src/explain.rsindex 8d6bf4105f..81b8f99441 100644--- a/datafusion/physical-plan/src/explain.rs+++ b/datafusion/physical-plan/src/explain.rs@@ -20,19 +20,18 @@use std::any::Any;use std::sync::Arc;-use datafusion_common::display::StringifiedPlan;+use super::expressions::PhysicalSortExpr;+use super::{DisplayAs, SendableRecordBatchStream};+use crate::stream::RecordBatchStreamAdapter;+use crate::{DisplayFormatType, ExecutionPlan, Partitioning, Statistics};+use arrow::{array::StringBuilder, datatypes::SchemaRef, record_batch::RecordBatch};+use datafusion_common::display::StringifiedPlan;use datafusion_common::{internal_err, DataFusionError, Result};+use datafusion_execution::TaskContext;-use crate::{DisplayFormatType, ExecutionPlan, Partitioning, Statistics};-use arrow::{array::StringBuilder, datatypes::SchemaRef, record_batch::RecordBatch};use log::trace;-use super::DisplayAs;-use super::{expressions::PhysicalSortExpr, SendableRecordBatchStream};-use crate::stream::RecordBatchStreamAdapter;-use datafusion_execution::TaskContext;-/// Explain execution plan operator. This operator contains the string/// values of the various plans it has when it is created, and passes/// them to its output.@@ -169,9 +168,9 @@ impl ExecutionPlan for ExplainExec {)))}- fn statistics(&self) -> Statistics {+ fn statistics(&self) -> Result<Statistics> {// Statistics an EXPLAIN plan are not relevant- Statistics::default()+ Ok(Statistics::new_unknown(&self.schema()))}}diff --git a/datafusion/physical-plan/src/filter.rs b/datafusion/physical-plan/src/filter.rsindex ccd0bd525f..59f9928d0e 100644--- a/datafusion/physical-plan/src/filter.rs+++ b/datafusion/physical-plan/src/filter.rs@@ -37,6 +37,7 @@ use arrow::compute::filter_record_batch;use arrow::datatypes::{DataType, SchemaRef};use arrow::record_batch::RecordBatch;use datafusion_common::cast::as_boolean_array;+use datafusion_common::stats::Precision;use datafusion_common::{plan_err, DataFusionError, Result};use datafusion_execution::TaskContext;use datafusion_expr::Operator;@@ -48,6 +49,7 @@ use datafusion_physical_expr::{use datafusion_physical_expr::intervals::utils::check_support;use datafusion_physical_expr::utils::collect_columns;+use futures::stream::{Stream, StreamExt};use log::trace;@@ -154,19 +156,17 @@ impl ExecutionPlan for FilterExec {}fn ordering_equivalence_properties(&self) -> OrderingEquivalenceProperties {- let stats = self.statistics();+ let stats = self+ .statistics()+ .expect("Ordering equivalences need to handle the error case of statistics");// Add the columns that have only one value (singleton) after filtering to constants.- if let Some(col_stats) = stats.column_statistics {- let constants = collect_columns(self.predicate())- .into_iter()- .filter(|column| col_stats[column.index()].is_singleton())- .map(|column| Arc::new(column) as Arc<dyn PhysicalExpr>)- .collect::<Vec<_>>();- let filter_oeq = self.input.ordering_equivalence_properties();- filter_oeq.with_constants(constants)- } else {- self.input.ordering_equivalence_properties()- }+ let constants = collect_columns(self.predicate())+ .into_iter()+ .filter(|column| stats.column_statistics[column.index()].is_singleton())+ .map(|column| Arc::new(column) as Arc<dyn PhysicalExpr>)+ .collect::<Vec<_>>();+ let filter_oeq = self.input.ordering_equivalence_properties();+ filter_oeq.with_constants(constants)}fn with_new_children(@@ -200,56 +200,43 @@ impl ExecutionPlan for FilterExec {/// The output statistics of a filtering operation can be estimated if the/// predicate's selectivity value can be determined for the incoming data.- fn statistics(&self) -> Statistics {+ fn statistics(&self) -> Result<Statistics> {let predicate = self.predicate();- if !check_support(predicate, &self.schema()) {- return Statistics::default();+ let schema = self.schema();+ if !check_support(predicate, &schema) {+ return Ok(Statistics::new_unknown(&schema));}+ let input_stats = self.input.statistics()?;- let input_stats = self.input.statistics();- let input_column_stats = match input_stats.column_statistics {- Some(stats) => stats,- None => self- .schema()- .fields- .iter()- .map(|field| {- ColumnStatistics::new_with_unbounded_column(field.data_type())- })- .collect::<Vec<_>>(),- };-- let starter_ctx =- AnalysisContext::from_statistics(&self.input.schema(), &input_column_stats);-- let analysis_ctx = match analyze(predicate, starter_ctx) {- Ok(ctx) => ctx,- Err(_) => return Statistics::default(),- };+ let num_rows = input_stats.num_rows;+ let total_byte_size = input_stats.total_byte_size;+ let input_analysis_ctx = AnalysisContext::try_from_statistics(+ &self.input.schema(),+ &input_stats.column_statistics,+ )?;+ let analysis_ctx = analyze(predicate, input_analysis_ctx)?;+ // Estimate (inexact) selectivity of predicatelet selectivity = analysis_ctx.selectivity.unwrap_or(1.0);-- let num_rows = input_stats- .num_rows- .map(|num| (num as f64 * selectivity).ceil() as usize);- let total_byte_size = input_stats- .total_byte_size- .map(|size| (size as f64 * selectivity).ceil() as usize);-- let column_statistics = if let Some(analysis_boundaries) = analysis_ctx.boundaries- {- collect_new_statistics(input_column_stats, selectivity, analysis_boundaries)- } else {- input_column_stats+ let num_rows = match num_rows.get_value() {+ Some(nr) => Precision::Inexact((*nr as f64 * selectivity).ceil() as usize),+ None => Precision::Absent,+ };+ let total_byte_size = match total_byte_size.get_value() {+ Some(tbs) => Precision::Inexact((*tbs as f64 * selectivity).ceil() as usize),+ None => Precision::Absent,};- Statistics {+ let column_statistics = collect_new_statistics(+ &input_stats.column_statistics,+ analysis_ctx.boundaries,+ );+ Ok(Statistics {num_rows,total_byte_size,- column_statistics: Some(column_statistics),- is_exact: Default::default(),- }+ column_statistics,+ })}}@@ -258,11 +245,9 @@ impl ExecutionPlan for FilterExec {/// is adjusted by using the next/previous value for its data type to convert/// it into a closed bound.fn collect_new_statistics(- input_column_stats: Vec<ColumnStatistics>,- selectivity: f64,+ input_column_stats: &[ColumnStatistics],analysis_boundaries: Vec<ExprBoundaries>,) -> Vec<ColumnStatistics> {- let nonempty_columns = selectivity > 0.0;analysis_boundaries.into_iter().enumerate()@@ -277,10 +262,16 @@ fn collect_new_statistics()| {let closed_interval = interval.close_bounds();ColumnStatistics {- null_count: input_column_stats[idx].null_count,- max_value: nonempty_columns.then_some(closed_interval.upper.value),- min_value: nonempty_columns.then_some(closed_interval.lower.value),- distinct_count,+ null_count: match input_column_stats[idx].null_count.get_value() {+ Some(nc) => Precision::Inexact(*nc),+ None => Precision::Absent,+ },+ max_value: Precision::Inexact(closed_interval.upper.value),+ min_value: Precision::Inexact(closed_interval.lower.value),+ distinct_count: match distinct_count.get_value() {+ Some(dc) => Precision::Inexact(*dc),+ None => Precision::Absent,+ },}},)@@ -396,18 +387,18 @@ pub type EqualAndNonEqual<'a> =#[cfg(test)]mod tests {+ use std::iter::Iterator;+ use std::sync::Arc;use super::*;use crate::expressions::*;use crate::test;use crate::test::exec::StatisticsExec;use crate::ExecutionPlan;+use arrow::datatypes::{DataType, Field, Schema};- use datafusion_common::ColumnStatistics;- use datafusion_common::ScalarValue;+ use datafusion_common::{ColumnStatistics, ScalarValue};use datafusion_expr::Operator;- use std::iter::Iterator;- use std::sync::Arc;#[tokio::test]async fn collect_columns_predicates() -> Result<()> {@@ -460,14 +451,13 @@ mod tests {let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]);let input = Arc::new(StatisticsExec::new(Statistics {- num_rows: Some(100),- total_byte_size: Some(100 * bytes_per_row),- column_statistics: Some(vec![ColumnStatistics {- min_value: Some(ScalarValue::Int32(Some(1))),- max_value: Some(ScalarValue::Int32(Some(100))),+ num_rows: Precision::Inexact(100),+ total_byte_size: Precision::Inexact(100 * bytes_per_row),+ column_statistics: vec![ColumnStatistics {+ min_value: Precision::Inexact(ScalarValue::Int32(Some(1))),+ max_value: Precision::Inexact(ScalarValue::Int32(Some(100))),..Default::default()- }]),- ..Default::default()+ }],},schema.clone(),));@@ -480,16 +470,19 @@ mod tests {let filter: Arc<dyn ExecutionPlan> =Arc::new(FilterExec::try_new(predicate, input)?);- let statistics = filter.statistics();- assert_eq!(statistics.num_rows, Some(25));- assert_eq!(statistics.total_byte_size, Some(25 * bytes_per_row));+ let statistics = filter.statistics()?;+ assert_eq!(statistics.num_rows, Precision::Inexact(25));+ assert_eq!(+ statistics.total_byte_size,+ Precision::Inexact(25 * bytes_per_row)+ );assert_eq!(statistics.column_statistics,- Some(vec![ColumnStatistics {- min_value: Some(ScalarValue::Int32(Some(1))),- max_value: Some(ScalarValue::Int32(Some(25))),+ vec![ColumnStatistics {+ min_value: Precision::Inexact(ScalarValue::Int32(Some(1))),+ max_value: Precision::Inexact(ScalarValue::Int32(Some(25))),..Default::default()- }])+ }]);Ok(())@@ -502,13 +495,13 @@ mod tests {let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]);let input = Arc::new(StatisticsExec::new(Statistics {- num_rows: Some(100),- column_statistics: Some(vec![ColumnStatistics {- min_value: Some(ScalarValue::Int32(Some(1))),- max_value: Some(ScalarValue::Int32(Some(100))),+ num_rows: Precision::Inexact(100),+ column_statistics: vec![ColumnStatistics {+ min_value: Precision::Inexact(ScalarValue::Int32(Some(1))),+ max_value: Precision::Inexact(ScalarValue::Int32(Some(100))),..Default::default()- }]),- ..Default::default()+ }],+ total_byte_size: Precision::Absent,},schema.clone(),));@@ -527,15 +520,15 @@ mod tests {sub_filter,)?);- let statistics = filter.statistics();- assert_eq!(statistics.num_rows, Some(16));+ let statistics = filter.statistics()?;+ assert_eq!(statistics.num_rows, Precision::Inexact(16));assert_eq!(statistics.column_statistics,- Some(vec![ColumnStatistics {- min_value: Some(ScalarValue::Int32(Some(10))),- max_value: Some(ScalarValue::Int32(Some(25))),+ vec![ColumnStatistics {+ min_value: Precision::Inexact(ScalarValue::Int32(Some(10))),+ max_value: Precision::Inexact(ScalarValue::Int32(Some(25))),..Default::default()- }])+ }]);Ok(())@@ -552,20 +545,20 @@ mod tests {]);let input = Arc::new(StatisticsExec::new(Statistics {- num_rows: Some(100),- column_statistics: Some(vec![+ num_rows: Precision::Inexact(100),+ column_statistics: vec![ColumnStatistics {- min_value: Some(ScalarValue::Int32(Some(1))),- max_value: Some(ScalarValue::Int32(Some(100))),+ min_value: Precision::Inexact(ScalarValue::Int32(Some(1))),+ max_value: Precision::Inexact(ScalarValue::Int32(Some(100))),..Default::default()},ColumnStatistics {- min_value: Some(ScalarValue::Int32(Some(1))),- max_value: Some(ScalarValue::Int32(Some(50))),+ min_value: Precision::Inexact(ScalarValue::Int32(Some(1))),+ max_value: Precision::Inexact(ScalarValue::Int32(Some(50))),..Default::default()},- ]),- ..Default::default()+ ],+ total_byte_size: Precision::Absent,},schema.clone(),));@@ -587,28 +580,28 @@ mod tests {binary(col("a", &schema)?, Operator::GtEq, lit(10i32), &schema)?,b_gt_5,)?);- let statistics = filter.statistics();+ let statistics = filter.statistics()?;// On a uniform distribution, only fifteen rows will satisfy the// filter that 'a' proposed (a >= 10 AND a <= 25) (15/100) and only// 5 rows will satisfy the filter that 'b' proposed (b > 45) (5/50).//// Which would result with a selectivity of '15/100 * 5/50' or 0.015// and that means about %1.5 of the all rows (rounded up to 2 rows).- assert_eq!(statistics.num_rows, Some(2));+ assert_eq!(statistics.num_rows, Precision::Inexact(2));assert_eq!(statistics.column_statistics,- Some(vec![+ vec![ColumnStatistics {- min_value: Some(ScalarValue::Int32(Some(10))),- max_value: Some(ScalarValue::Int32(Some(25))),+ min_value: Precision::Inexact(ScalarValue::Int32(Some(10))),+ max_value: Precision::Inexact(ScalarValue::Int32(Some(25))),..Default::default()},ColumnStatistics {- min_value: Some(ScalarValue::Int32(Some(46))),- max_value: Some(ScalarValue::Int32(Some(50))),+ min_value: Precision::Inexact(ScalarValue::Int32(Some(46))),+ max_value: Precision::Inexact(ScalarValue::Int32(Some(50))),..Default::default()}- ])+ ]);Ok(())@@ -620,12 +613,7 @@ mod tests {// a: min=???, max=??? (missing)let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]);let input = Arc::new(StatisticsExec::new(- Statistics {- column_statistics: Some(vec![ColumnStatistics {- ..Default::default()- }]),- ..Default::default()- },+ Statistics::new_unknown(&schema),schema.clone(),));@@ -637,8 +625,8 @@ mod tests {let filter: Arc<dyn ExecutionPlan> =Arc::new(FilterExec::try_new(predicate, input)?);- let statistics = filter.statistics();- assert_eq!(statistics.num_rows, None);+ let statistics = filter.statistics()?;+ assert_eq!(statistics.num_rows, Precision::Absent);Ok(())}@@ -656,26 +644,25 @@ mod tests {]);let input = Arc::new(StatisticsExec::new(Statistics {- num_rows: Some(1000),- total_byte_size: Some(4000),- column_statistics: Some(vec![+ num_rows: Precision::Inexact(1000),+ total_byte_size: Precision::Inexact(4000),+ column_statistics: vec![ColumnStatistics {- min_value: Some(ScalarValue::Int32(Some(1))),- max_value: Some(ScalarValue::Int32(Some(100))),+ min_value: Precision::Inexact(ScalarValue::Int32(Some(1))),+ max_value: Precision::Inexact(ScalarValue::Int32(Some(100))),..Default::default()},ColumnStatistics {- min_value: Some(ScalarValue::Int32(Some(1))),- max_value: Some(ScalarValue::Int32(Some(3))),+ min_value: Precision::Inexact(ScalarValue::Int32(Some(1))),+ max_value: Precision::Inexact(ScalarValue::Int32(Some(3))),..Default::default()},ColumnStatistics {- min_value: Some(ScalarValue::Float32(Some(1000.0))),- max_value: Some(ScalarValue::Float32(Some(1100.0))),+ min_value: Precision::Inexact(ScalarValue::Float32(Some(1000.0))),+ max_value: Precision::Inexact(ScalarValue::Float32(Some(1100.0))),..Default::default()},- ]),- ..Default::default()+ ],},schema,));@@ -711,47 +698,51 @@ mod tests {));let filter: Arc<dyn ExecutionPlan> =Arc::new(FilterExec::try_new(predicate, input)?);- let statistics = filter.statistics();+ let statistics = filter.statistics()?;// 0.5 (from a) * 0.333333... (from b) * 0.798387... (from c) ≈ 0.1330...// num_rows after ceil => 133.0... => 134// total_byte_size after ceil => 532.0... => 533- assert_eq!(statistics.num_rows, Some(134));- assert_eq!(statistics.total_byte_size, Some(533));+ assert_eq!(statistics.num_rows, Precision::Inexact(134));+ assert_eq!(statistics.total_byte_size, Precision::Inexact(533));let exp_col_stats = vec![ColumnStatistics {- min_value: Some(ScalarValue::Int32(Some(4))),- max_value: Some(ScalarValue::Int32(Some(53))),+ min_value: Precision::Inexact(ScalarValue::Int32(Some(4))),+ max_value: Precision::Inexact(ScalarValue::Int32(Some(53))),..Default::default()},ColumnStatistics {- min_value: Some(ScalarValue::Int32(Some(3))),- max_value: Some(ScalarValue::Int32(Some(3))),+ min_value: Precision::Inexact(ScalarValue::Int32(Some(3))),+ max_value: Precision::Inexact(ScalarValue::Int32(Some(3))),..Default::default()},ColumnStatistics {- min_value: Some(ScalarValue::Float32(Some(1000.0))),- max_value: Some(ScalarValue::Float32(Some(1075.0))),+ min_value: Precision::Inexact(ScalarValue::Float32(Some(1000.0))),+ max_value: Precision::Inexact(ScalarValue::Float32(Some(1075.0))),..Default::default()},];let _ = exp_col_stats.into_iter()- .zip(statistics.column_statistics.unwrap())+ .zip(statistics.column_statistics).map(|(expected, actual)| {- if actual.min_value.clone().unwrap().data_type().is_floating() {- // Windows rounds arithmetic operation results differently for floating point numbers.- // Therefore, we check if the actual values are in an epsilon range.- let actual_min = actual.min_value.unwrap();- let actual_max = actual.max_value.unwrap();- let expected_min = expected.min_value.unwrap();- let expected_max = expected.max_value.unwrap();- let eps = ScalarValue::Float32(Some(1e-6));-- assert!(actual_min.sub(&expected_min).unwrap() < eps);- assert!(actual_min.sub(&expected_min).unwrap() < eps);-- assert!(actual_max.sub(&expected_max).unwrap() < eps);- assert!(actual_max.sub(&expected_max).unwrap() < eps);+ if let Some(val) = actual.min_value.get_value() {+ if val.data_type().is_floating() {+ // Windows rounds arithmetic operation results differently for floating point numbers.+ // Therefore, we check if the actual values are in an epsilon range.+ let actual_min = actual.min_value.get_value().unwrap();+ let actual_max = actual.max_value.get_value().unwrap();+ let expected_min = expected.min_value.get_value().unwrap();+ let expected_max = expected.max_value.get_value().unwrap();+ let eps = ScalarValue::Float32(Some(1e-6));++ assert!(actual_min.sub(expected_min).unwrap() < eps);+ assert!(actual_min.sub(expected_min).unwrap() < eps);++ assert!(actual_max.sub(expected_max).unwrap() < eps);+ assert!(actual_max.sub(expected_max).unwrap() < eps);+ } else {+ assert_eq!(actual, expected);+ }} else {assert_eq!(actual, expected);}@@ -771,21 +762,20 @@ mod tests {]);let input = Arc::new(StatisticsExec::new(Statistics {- num_rows: Some(1000),- total_byte_size: Some(4000),- column_statistics: Some(vec![+ num_rows: Precision::Inexact(1000),+ total_byte_size: Precision::Inexact(4000),+ column_statistics: vec![ColumnStatistics {- min_value: Some(ScalarValue::Int32(Some(1))),- max_value: Some(ScalarValue::Int32(Some(100))),+ min_value: Precision::Inexact(ScalarValue::Int32(Some(1))),+ max_value: Precision::Inexact(ScalarValue::Int32(Some(100))),..Default::default()},ColumnStatistics {- min_value: Some(ScalarValue::Int32(Some(1))),- max_value: Some(ScalarValue::Int32(Some(3))),+ min_value: Precision::Inexact(ScalarValue::Int32(Some(1))),+ max_value: Precision::Inexact(ScalarValue::Int32(Some(3))),..Default::default()},- ]),- ..Default::default()+ ],},schema,));@@ -804,13 +794,13 @@ mod tests {)),));// Since filter predicate passes all entries, statistics after filter shouldn't change.- let expected = input.statistics().column_statistics;+ let expected = input.statistics()?.column_statistics;let filter: Arc<dyn ExecutionPlan> =Arc::new(FilterExec::try_new(predicate, input)?);- let statistics = filter.statistics();+ let statistics = filter.statistics()?;- assert_eq!(statistics.num_rows, Some(1000));- assert_eq!(statistics.total_byte_size, Some(4000));+ assert_eq!(statistics.num_rows, Precision::Inexact(1000));+ assert_eq!(statistics.total_byte_size, Precision::Inexact(4000));assert_eq!(statistics.column_statistics, expected);Ok(())@@ -827,21 +817,20 @@ mod tests {]);let input = Arc::new(StatisticsExec::new(Statistics {- num_rows: Some(1000),- total_byte_size: Some(4000),- column_statistics: Some(vec![+ num_rows: Precision::Inexact(1000),+ total_byte_size: Precision::Inexact(4000),+ column_statistics: vec![ColumnStatistics {- min_value: Some(ScalarValue::Int32(Some(1))),- max_value: Some(ScalarValue::Int32(Some(100))),+ min_value: Precision::Inexact(ScalarValue::Int32(Some(1))),+ max_value: Precision::Inexact(ScalarValue::Int32(Some(100))),..Default::default()},ColumnStatistics {- min_value: Some(ScalarValue::Int32(Some(1))),- max_value: Some(ScalarValue::Int32(Some(3))),+ min_value: Precision::Inexact(ScalarValue::Int32(Some(1))),+ max_value: Precision::Inexact(ScalarValue::Int32(Some(3))),..Default::default()},- ]),- ..Default::default()+ ],},schema,));@@ -861,24 +850,24 @@ mod tests {));let filter: Arc<dyn ExecutionPlan> =Arc::new(FilterExec::try_new(predicate, input)?);- let statistics = filter.statistics();+ let statistics = filter.statistics()?;- assert_eq!(statistics.num_rows, Some(0));- assert_eq!(statistics.total_byte_size, Some(0));+ assert_eq!(statistics.num_rows, Precision::Inexact(0));+ assert_eq!(statistics.total_byte_size, Precision::Inexact(0));assert_eq!(statistics.column_statistics,- Some(vec![+ vec![ColumnStatistics {- min_value: None,- max_value: None,+ min_value: Precision::Inexact(ScalarValue::Int32(Some(1))),+ max_value: Precision::Inexact(ScalarValue::Int32(Some(100))),..Default::default()},ColumnStatistics {- min_value: None,- max_value: None,+ min_value: Precision::Inexact(ScalarValue::Int32(Some(1))),+ max_value: Precision::Inexact(ScalarValue::Int32(Some(3))),..Default::default()},- ])+ ]);Ok(())@@ -892,21 +881,20 @@ mod tests {]);let input = Arc::new(StatisticsExec::new(Statistics {- num_rows: Some(1000),- total_byte_size: Some(4000),- column_statistics: Some(vec![+ num_rows: Precision::Inexact(1000),+ total_byte_size: Precision::Inexact(4000),+ column_statistics: vec![ColumnStatistics {- min_value: Some(ScalarValue::Int32(Some(1))),- max_value: Some(ScalarValue::Int32(Some(100))),+ min_value: Precision::Inexact(ScalarValue::Int32(Some(1))),+ max_value: Precision::Inexact(ScalarValue::Int32(Some(100))),..Default::default()},ColumnStatistics {- min_value: Some(ScalarValue::Int32(Some(1))),- max_value: Some(ScalarValue::Int32(Some(100))),+ min_value: Precision::Inexact(ScalarValue::Int32(Some(1))),+ max_value: Precision::Inexact(ScalarValue::Int32(Some(100))),..Default::default()},- ]),- ..Default::default()+ ],},schema,));@@ -918,26 +906,71 @@ mod tests {));let filter: Arc<dyn ExecutionPlan> =Arc::new(FilterExec::try_new(predicate, input)?);- let statistics = filter.statistics();+ let statistics = filter.statistics()?;- assert_eq!(statistics.num_rows, Some(490));- assert_eq!(statistics.total_byte_size, Some(1960));+ assert_eq!(statistics.num_rows, Precision::Inexact(490));+ assert_eq!(statistics.total_byte_size, Precision::Inexact(1960));assert_eq!(statistics.column_statistics,- Some(vec![+ vec![ColumnStatistics {- min_value: Some(ScalarValue::Int32(Some(1))),- max_value: Some(ScalarValue::Int32(Some(49))),+ min_value: Precision::Inexact(ScalarValue::Int32(Some(1))),+ max_value: Precision::Inexact(ScalarValue::Int32(Some(49))),..Default::default()},ColumnStatistics {- min_value: Some(ScalarValue::Int32(Some(1))),- max_value: Some(ScalarValue::Int32(Some(100))),+ min_value: Precision::Inexact(ScalarValue::Int32(Some(1))),+ max_value: Precision::Inexact(ScalarValue::Int32(Some(100))),..Default::default()},- ])+ ]);Ok(())}++ #[tokio::test]+ async fn test_empty_input_statistics() -> Result<()> {+ let schema = Schema::new(vec![Field::new("a", DataType::Int32, false)]);+ let input = Arc::new(StatisticsExec::new(+ Statistics::new_unknown(&schema),+ schema,+ ));+ // WHERE a <= 10 AND 0 <= a - 5+ let predicate = Arc::new(BinaryExpr::new(+ Arc::new(BinaryExpr::new(+ Arc::new(Column::new("a", 0)),+ Operator::LtEq,+ Arc::new(Literal::new(ScalarValue::Int32(Some(10)))),+ )),+ Operator::And,+ Arc::new(BinaryExpr::new(+ Arc::new(Literal::new(ScalarValue::Int32(Some(0)))),+ Operator::LtEq,+ Arc::new(BinaryExpr::new(+ Arc::new(Column::new("a", 0)),+ Operator::Minus,+ Arc::new(Literal::new(ScalarValue::Int32(Some(5)))),+ )),+ )),+ ));+ let filter: Arc<dyn ExecutionPlan> =+ Arc::new(FilterExec::try_new(predicate, input)?);+ let filter_statistics = filter.statistics()?;++ let expected_filter_statistics = Statistics {+ num_rows: Precision::Absent,+ total_byte_size: Precision::Absent,+ column_statistics: vec![ColumnStatistics {+ null_count: Precision::Absent,+ min_value: Precision::Inexact(ScalarValue::Int32(Some(5))),+ max_value: Precision::Inexact(ScalarValue::Int32(Some(10))),+ distinct_count: Precision::Absent,+ }],+ };++ assert_eq!(filter_statistics, expected_filter_statistics);++ Ok(())+ }}diff --git a/datafusion/physical-plan/src/insert.rs b/datafusion/physical-plan/src/insert.rsindex bff20e85b7..5b58a0a771 100644--- a/datafusion/physical-plan/src/insert.rs+++ b/datafusion/physical-plan/src/insert.rs@@ -17,28 +17,29 @@//! Execution plan for writing data to [`DataSink`]s+use std::any::Any;+use std::fmt;+use std::fmt::Debug;+use std::sync::Arc;+use super::expressions::PhysicalSortExpr;use super::{DisplayAs, DisplayFormatType, ExecutionPlan, Partitioning, SendableRecordBatchStream,Statistics,};+use crate::metrics::MetricsSet;+use crate::stream::RecordBatchStreamAdapter;+use arrow::datatypes::SchemaRef;use arrow::record_batch::RecordBatch;use arrow_array::{ArrayRef, UInt64Array};use arrow_schema::{DataType, Field, Schema};-use async_trait::async_trait;-use core::fmt;-use datafusion_common::Result;+use datafusion_common::{exec_err, internal_err, DataFusionError, Result};+use datafusion_execution::TaskContext;use datafusion_physical_expr::PhysicalSortRequirement;-use futures::StreamExt;-use std::any::Any;-use std::fmt::Debug;-use std::sync::Arc;-use crate::metrics::MetricsSet;-use crate::stream::RecordBatchStreamAdapter;-use datafusion_common::{exec_err, internal_err, DataFusionError};-use datafusion_execution::TaskContext;+use async_trait::async_trait;+use futures::StreamExt;/// `DataSink` implements writing streams of [`RecordBatch`]es to/// user defined destinations.@@ -284,8 +285,8 @@ impl ExecutionPlan for FileSinkExec {)))}- fn statistics(&self) -> Statistics {- Statistics::default()+ fn statistics(&self) -> Result<Statistics> {+ Ok(Statistics::new_unknown(&self.schema()))}}diff --git a/datafusion/physical-plan/src/joins/cross_join.rs b/datafusion/physical-plan/src/joins/cross_join.rsindex 4ba29524b3..1ffd9ad1c1 100644--- a/datafusion/physical-plan/src/joins/cross_join.rs+++ b/datafusion/physical-plan/src/joins/cross_join.rs@@ -18,13 +18,12 @@//! Defines the cross join plan for loading the left side of the cross join//! and producing batches in parallel for the right partitions-use futures::{ready, StreamExt};-use futures::{Stream, TryStreamExt};use std::{any::Any, sync::Arc, task::Poll};-use arrow::datatypes::{Fields, Schema, SchemaRef};-use arrow::record_batch::RecordBatch;-+use super::utils::{+ adjust_right_output_partitioning, cross_join_equivalence_properties,+ BuildProbeJoinMetrics, OnceAsync, OnceFut,+};use crate::metrics::{ExecutionPlanMetricsSet, MetricsSet};use crate::DisplayAs;use crate::{@@ -33,16 +32,17 @@ use crate::{ExecutionPlan, Partitioning, PhysicalSortExpr, RecordBatchStream,SendableRecordBatchStream, Statistics,};-use async_trait::async_trait;-use datafusion_common::{plan_err, DataFusionError};-use datafusion_common::{Result, ScalarValue};++use arrow::datatypes::{Fields, Schema, SchemaRef};+use arrow::record_batch::RecordBatch;+use datafusion_common::stats::Precision;+use datafusion_common::{plan_err, DataFusionError, Result, ScalarValue};use datafusion_execution::memory_pool::{MemoryConsumer, MemoryReservation};use datafusion_execution::TaskContext;-use super::utils::{- adjust_right_output_partitioning, cross_join_equivalence_properties,- BuildProbeJoinMetrics, OnceAsync, OnceFut,-};+use async_trait::async_trait;+use futures::{ready, StreamExt};+use futures::{Stream, TryStreamExt};/// Data of the left sidetype JoinLeftData = (RecordBatch, MemoryReservation);@@ -257,77 +257,55 @@ impl ExecutionPlan for CrossJoinExec {}))}- fn statistics(&self) -> Statistics {- stats_cartesian_product(- self.left.statistics(),- self.left.schema().fields().len(),- self.right.statistics(),- self.right.schema().fields().len(),- )+ fn statistics(&self) -> Result<Statistics> {+ Ok(stats_cartesian_product(+ self.left.statistics()?,+ self.right.statistics()?,+ ))}}/// [left/right]_col_count are required in case the column statistics are Nonefn stats_cartesian_product(left_stats: Statistics,- left_col_count: usize,right_stats: Statistics,- right_col_count: usize,) -> Statistics {let left_row_count = left_stats.num_rows;let right_row_count = right_stats.num_rows;// calculate global stats- let is_exact = left_stats.is_exact && right_stats.is_exact;- let num_rows = left_stats- .num_rows- .zip(right_stats.num_rows)- .map(|(a, b)| a * b);+ let num_rows = left_row_count.multiply(&right_row_count);// the result size is two times a*b because you have the columns of both left and rightlet total_byte_size = left_stats.total_byte_size- .zip(right_stats.total_byte_size)- .map(|(a, b)| 2 * a * b);-- // calculate column stats- let column_statistics =- // complete the column statistics if they are missing only on one side- match (left_stats.column_statistics, right_stats.column_statistics) {- (None, None) => None,- (None, Some(right_col_stat)) => Some((- vec![ColumnStatistics::default(); left_col_count],- right_col_stat,- )),- (Some(left_col_stat), None) => Some((- left_col_stat,- vec![ColumnStatistics::default(); right_col_count],- )),- (Some(left_col_stat), Some(right_col_stat)) => {- Some((left_col_stat, right_col_stat))- }- }- .map(|(left_col_stats, right_col_stats)| {- // the null counts must be multiplied by the row counts of the other side (if defined)- // Min, max and distinct_count on the other hand are invariants.- left_col_stats.into_iter().map(|s| ColumnStatistics{- null_count: s.null_count.zip(right_row_count).map(|(a, b)| a * b),- distinct_count: s.distinct_count,- min_value: s.min_value,- max_value: s.max_value,- }).chain(- right_col_stats.into_iter().map(|s| ColumnStatistics{- null_count: s.null_count.zip(left_row_count).map(|(a, b)| a * b),- distinct_count: s.distinct_count,- min_value: s.min_value,- max_value: s.max_value,- })).collect()- });+ .multiply(&right_stats.total_byte_size)+ .multiply(&Precision::Exact(2));++ let left_col_stats = left_stats.column_statistics;+ let right_col_stats = right_stats.column_statistics;++ // the null counts must be multiplied by the row counts of the other side (if defined)+ // Min, max and distinct_count on the other hand are invariants.+ let cross_join_stats = left_col_stats+ .into_iter()+ .map(|s| ColumnStatistics {+ null_count: s.null_count.multiply(&right_row_count),+ distinct_count: s.distinct_count,+ min_value: s.min_value,+ max_value: s.max_value,+ })+ .chain(right_col_stats.into_iter().map(|s| ColumnStatistics {+ null_count: s.null_count.multiply(&left_row_count),+ distinct_count: s.distinct_count,+ min_value: s.min_value,+ max_value: s.max_value,+ }))+ .collect();Statistics {- is_exact,num_rows,total_byte_size,- column_statistics,+ column_statistics: cross_join_stats,}}@@ -459,6 +437,7 @@ mod tests {use super::*;use crate::common;use crate::test::build_table_scan_i32;+use datafusion_common::{assert_batches_sorted_eq, assert_contains};use datafusion_execution::runtime_env::{RuntimeConfig, RuntimeEnv};@@ -484,63 +463,68 @@ mod tests {let right_bytes = 27;let left = Statistics {- is_exact: true,- num_rows: Some(left_row_count),- total_byte_size: Some(left_bytes),- column_statistics: Some(vec![+ num_rows: Precision::Exact(left_row_count),+ total_byte_size: Precision::Exact(left_bytes),+ column_statistics: vec![ColumnStatistics {- distinct_count: Some(5),- max_value: Some(ScalarValue::Int64(Some(21))),- min_value: Some(ScalarValue::Int64(Some(-4))),- null_count: Some(0),+ distinct_count: Precision::Exact(5),+ max_value: Precision::Exact(ScalarValue::Int64(Some(21))),+ min_value: Precision::Exact(ScalarValue::Int64(Some(-4))),+ null_count: Precision::Exact(0),},ColumnStatistics {- distinct_count: Some(1),- max_value: Some(ScalarValue::Utf8(Some(String::from("x")))),- min_value: Some(ScalarValue::Utf8(Some(String::from("a")))),- null_count: Some(3),+ distinct_count: Precision::Exact(1),+ max_value: Precision::Exact(ScalarValue::Utf8(Some(String::from(+ "x",+ )))),+ min_value: Precision::Exact(ScalarValue::Utf8(Some(String::from(+ "a",+ )))),+ null_count: Precision::Exact(3),},- ]),+ ],};let right = Statistics {- is_exact: true,- num_rows: Some(right_row_count),- total_byte_size: Some(right_bytes),- column_statistics: Some(vec![ColumnStatistics {- distinct_count: Some(3),- max_value: Some(ScalarValue::Int64(Some(12))),- min_value: Some(ScalarValue::Int64(Some(0))),- null_count: Some(2),- }]),+ num_rows: Precision::Exact(right_row_count),+ total_byte_size: Precision::Exact(right_bytes),+ column_statistics: vec![ColumnStatistics {+ distinct_count: Precision::Exact(3),+ max_value: Precision::Exact(ScalarValue::Int64(Some(12))),+ min_value: Precision::Exact(ScalarValue::Int64(Some(0))),+ null_count: Precision::Exact(2),+ }],};- let result = stats_cartesian_product(left, 3, right, 2);+ let result = stats_cartesian_product(left, right);let expected = Statistics {- is_exact: true,- num_rows: Some(left_row_count * right_row_count),- total_byte_size: Some(2 * left_bytes * right_bytes),- column_statistics: Some(vec![+ num_rows: Precision::Exact(left_row_count * right_row_count),+ total_byte_size: Precision::Exact(2 * left_bytes * right_bytes),+ column_statistics: vec![ColumnStatistics {- distinct_count: Some(5),- max_value: Some(ScalarValue::Int64(Some(21))),- min_value: Some(ScalarValue::Int64(Some(-4))),- null_count: Some(0),+ distinct_count: Precision::Exact(5),+ max_value: Precision::Exact(ScalarValue::Int64(Some(21))),+ min_value: Precision::Exact(ScalarValue::Int64(Some(-4))),+ null_count: Precision::Exact(0),},ColumnStatistics {- distinct_count: Some(1),- max_value: Some(ScalarValue::Utf8(Some(String::from("x")))),- min_value: Some(ScalarValue::Utf8(Some(String::from("a")))),- null_count: Some(3 * right_row_count),+ distinct_count: Precision::Exact(1),+ max_value: Precision::Exact(ScalarValue::Utf8(Some(String::from(+ "x",+ )))),+ min_value: Precision::Exact(ScalarValue::Utf8(Some(String::from(+ "a",+ )))),+ null_count: Precision::Exact(3 * right_row_count),},ColumnStatistics {- distinct_count: Some(3),- max_value: Some(ScalarValue::Int64(Some(12))),- min_value: Some(ScalarValue::Int64(Some(0))),- null_count: Some(2 * left_row_count),+ distinct_count: Precision::Exact(3),+ max_value: Precision::Exact(ScalarValue::Int64(Some(12))),+ min_value: Precision::Exact(ScalarValue::Int64(Some(0))),+ null_count: Precision::Exact(2 * left_row_count),},- ]),+ ],};assert_eq!(result, expected);@@ -551,63 +535,68 @@ mod tests {let left_row_count = 11;let left = Statistics {- is_exact: true,- num_rows: Some(left_row_count),- total_byte_size: Some(23),- column_statistics: Some(vec![+ num_rows: Precision::Exact(left_row_count),+ total_byte_size: Precision::Exact(23),+ column_statistics: vec![ColumnStatistics {- distinct_count: Some(5),- max_value: Some(ScalarValue::Int64(Some(21))),- min_value: Some(ScalarValue::Int64(Some(-4))),- null_count: Some(0),+ distinct_count: Precision::Exact(5),+ max_value: Precision::Exact(ScalarValue::Int64(Some(21))),+ min_value: Precision::Exact(ScalarValue::Int64(Some(-4))),+ null_count: Precision::Exact(0),},ColumnStatistics {- distinct_count: Some(1),- max_value: Some(ScalarValue::Utf8(Some(String::from("x")))),- min_value: Some(ScalarValue::Utf8(Some(String::from("a")))),- null_count: Some(3),+ distinct_count: Precision::Exact(1),+ max_value: Precision::Exact(ScalarValue::Utf8(Some(String::from(+ "x",+ )))),+ min_value: Precision::Exact(ScalarValue::Utf8(Some(String::from(+ "a",+ )))),+ null_count: Precision::Exact(3),},- ]),+ ],};let right = Statistics {- is_exact: true,- num_rows: None, // not defined!- total_byte_size: None, // not defined!- column_statistics: Some(vec![ColumnStatistics {- distinct_count: Some(3),- max_value: Some(ScalarValue::Int64(Some(12))),- min_value: Some(ScalarValue::Int64(Some(0))),- null_count: Some(2),- }]),+ num_rows: Precision::Absent,+ total_byte_size: Precision::Absent,+ column_statistics: vec![ColumnStatistics {+ distinct_count: Precision::Exact(3),+ max_value: Precision::Exact(ScalarValue::Int64(Some(12))),+ min_value: Precision::Exact(ScalarValue::Int64(Some(0))),+ null_count: Precision::Exact(2),+ }],};- let result = stats_cartesian_product(left, 3, right, 2);+ let result = stats_cartesian_product(left, right);let expected = Statistics {- is_exact: true,- num_rows: None,- total_byte_size: None,- column_statistics: Some(vec![+ num_rows: Precision::Absent,+ total_byte_size: Precision::Absent,+ column_statistics: vec![ColumnStatistics {- distinct_count: Some(5),- max_value: Some(ScalarValue::Int64(Some(21))),- min_value: Some(ScalarValue::Int64(Some(-4))),- null_count: None, // we don't know the row count on the right+ distinct_count: Precision::Exact(5),+ max_value: Precision::Exact(ScalarValue::Int64(Some(21))),+ min_value: Precision::Exact(ScalarValue::Int64(Some(-4))),+ null_count: Precision::Absent, // we don't know the row count on the right},ColumnStatistics {- distinct_count: Some(1),- max_value: Some(ScalarValue::Utf8(Some(String::from("x")))),- min_value: Some(ScalarValue::Utf8(Some(String::from("a")))),- null_count: None, // we don't know the row count on the right+ distinct_count: Precision::Exact(1),+ max_value: Precision::Exact(ScalarValue::Utf8(Some(String::from(+ "x",+ )))),+ min_value: Precision::Exact(ScalarValue::Utf8(Some(String::from(+ "a",+ )))),+ null_count: Precision::Absent, // we don't know the row count on the right},ColumnStatistics {- distinct_count: Some(3),- max_value: Some(ScalarValue::Int64(Some(12))),- min_value: Some(ScalarValue::Int64(Some(0))),- null_count: Some(2 * left_row_count),+ distinct_count: Precision::Exact(3),+ max_value: Precision::Exact(ScalarValue::Int64(Some(12))),+ min_value: Precision::Exact(ScalarValue::Int64(Some(0))),+ null_count: Precision::Exact(2 * left_row_count),},- ]),+ ],};assert_eq!(result, expected);diff --git a/datafusion/physical-plan/src/joins/hash_join.rs b/datafusion/physical-plan/src/joins/hash_join.rsindex 8e204634f3..4fd8882c01 100644--- a/datafusion/physical-plan/src/joins/hash_join.rs+++ b/datafusion/physical-plan/src/joins/hash_join.rs@@ -493,7 +493,7 @@ impl ExecutionPlan for HashJoinExec {Some(self.metrics.clone_inner())}- fn statistics(&self) -> Statistics {+ fn statistics(&self) -> Result<Statistics> {// TODO stats: it is not possible in general to know the output size of joins// There are some special cases though, for example:// - `A LEFT JOIN B ON A.col=B.col` with `COUNT_DISTINCT(B.col)=COUNT(B.col)`@@ -502,6 +502,7 @@ impl ExecutionPlan for HashJoinExec {self.right.clone(),self.on.clone(),&self.join_type,+ &self.schema,)}}diff --git a/datafusion/physical-plan/src/joins/nested_loop_join.rs b/datafusion/physical-plan/src/joins/nested_loop_join.rsindex c49c16dba3..944efb47f4 100644--- a/datafusion/physical-plan/src/joins/nested_loop_join.rs+++ b/datafusion/physical-plan/src/joins/nested_loop_join.rs@@ -19,6 +19,12 @@//! The nested loop join can execute in parallel by partitions and it is//! determined by the [`JoinType`].+use std::any::Any;+use std::fmt::Formatter;+use std::sync::Arc;+use std::task::Poll;++use crate::coalesce_batches::concat_batches;use crate::joins::utils::{append_right_indices, apply_join_filter_to_indices, build_batch_from_indices,build_join_schema, check_join_is_valid, combine_join_equivalence_properties,@@ -32,26 +38,20 @@ use crate::{DisplayAs, DisplayFormatType, Distribution, ExecutionPlan, Partitioning,RecordBatchStream, SendableRecordBatchStream,};+use arrow::array::{BooleanBufferBuilder, UInt32Array, UInt32Builder, UInt64Array, UInt64Builder,};use arrow::datatypes::{Schema, SchemaRef};use arrow::record_batch::RecordBatch;use arrow::util::bit_util;-use datafusion_common::{exec_err, DataFusionError, Statistics};-use datafusion_execution::memory_pool::MemoryReservation;+use datafusion_common::{exec_err, DataFusionError, Result, Statistics};+use datafusion_execution::memory_pool::{MemoryConsumer, MemoryReservation};+use datafusion_execution::TaskContext;use datafusion_expr::JoinType;use datafusion_physical_expr::{EquivalenceProperties, PhysicalSortExpr};-use futures::{ready, Stream, StreamExt, TryStreamExt};-use std::any::Any;-use std::fmt::Formatter;-use std::sync::Arc;-use std::task::Poll;-use crate::coalesce_batches::concat_batches;-use datafusion_common::Result;-use datafusion_execution::memory_pool::MemoryConsumer;-use datafusion_execution::TaskContext;+use futures::{ready, Stream, StreamExt, TryStreamExt};/// Data of the inner table sidetype JoinLeftData = (RecordBatch, MemoryReservation);@@ -282,12 +282,13 @@ impl ExecutionPlan for NestedLoopJoinExec {Some(self.metrics.clone_inner())}- fn statistics(&self) -> Statistics {+ fn statistics(&self) -> Result<Statistics> {estimate_join_statistics(self.left.clone(),self.right.clone(),vec![],&self.join_type,+ &self.schema,)}}@@ -739,21 +740,21 @@ impl RecordBatchStream for NestedLoopJoinStream {#[cfg(test)]mod tests {+ use std::sync::Arc;+use super::*;+ use crate::joins::utils::JoinSide;use crate::{common, expressions::Column, memory::MemoryExec, repartition::RepartitionExec,test::build_table_i32,};+use arrow::datatypes::{DataType, Field};+ use datafusion_common::{assert_batches_sorted_eq, assert_contains, ScalarValue};use datafusion_execution::runtime_env::{RuntimeConfig, RuntimeEnv};use datafusion_expr::Operator;- use datafusion_physical_expr::expressions::BinaryExpr;-- use crate::joins::utils::JoinSide;- use datafusion_common::{assert_batches_sorted_eq, assert_contains, ScalarValue};- use datafusion_physical_expr::expressions::Literal;+ use datafusion_physical_expr::expressions::{BinaryExpr, Literal};use datafusion_physical_expr::PhysicalExpr;- use std::sync::Arc;fn build_table(a: (&str, &Vec<i32>),diff --git a/datafusion/physical-plan/src/joins/sort_merge_join.rs b/datafusion/physical-plan/src/joins/sort_merge_join.rsindex 4de723ab73..61ac864bf2 100644--- a/datafusion/physical-plan/src/joins/sort_merge_join.rs+++ b/datafusion/physical-plan/src/joins/sort_merge_join.rs@@ -30,8 +30,7 @@ use std::pin::Pin;use std::sync::Arc;use std::task::{Context, Poll};-use crate::expressions::Column;-use crate::expressions::PhysicalSortExpr;+use crate::expressions::{Column, PhysicalSortExpr};use crate::joins::utils::{build_join_schema, calculate_join_output_ordering, check_join_is_valid,combine_join_equivalence_properties, combine_join_ordering_equivalence_properties,@@ -381,7 +380,7 @@ impl ExecutionPlan for SortMergeJoinExec {Some(self.metrics.clone_inner())}- fn statistics(&self) -> Statistics {+ fn statistics(&self) -> Result<Statistics> {// TODO stats: it is not possible in general to know the output size of joins// There are some special cases though, for example:// - `A LEFT JOIN B ON A.col=B.col` with `COUNT_DISTINCT(B.col)=COUNT(B.col)`@@ -390,6 +389,7 @@ impl ExecutionPlan for SortMergeJoinExec {self.right.clone(),self.on.clone(),&self.join_type,+ &self.schema,)}}@@ -1397,24 +1397,23 @@ fn is_join_arrays_equal(mod tests {use std::sync::Arc;- use arrow::array::{Date32Array, Date64Array, Int32Array};- use arrow::compute::SortOptions;- use arrow::datatypes::{DataType, Field, Schema};- use arrow::record_batch::RecordBatch;- use datafusion_execution::config::SessionConfig;- use datafusion_execution::TaskContext;-use crate::expressions::Column;use crate::joins::utils::JoinOn;use crate::joins::SortMergeJoinExec;use crate::memory::MemoryExec;use crate::test::build_table_i32;use crate::{common, ExecutionPlan};- use datafusion_common::Result;++ use arrow::array::{Date32Array, Date64Array, Int32Array};+ use arrow::compute::SortOptions;+ use arrow::datatypes::{DataType, Field, Schema};+ use arrow::record_batch::RecordBatch;use datafusion_common::{- assert_batches_eq, assert_batches_sorted_eq, assert_contains, JoinType,+ assert_batches_eq, assert_batches_sorted_eq, assert_contains, JoinType, Result,};+ use datafusion_execution::config::SessionConfig;use datafusion_execution::runtime_env::{RuntimeConfig, RuntimeEnv};+ use datafusion_execution::TaskContext;fn build_table(a: (&str, &Vec<i32>),diff --git a/datafusion/physical-plan/src/joins/symmetric_hash_join.rs b/datafusion/physical-plan/src/joins/symmetric_hash_join.rsindex e6eb5dd695..64128b6e3a 100644--- a/datafusion/physical-plan/src/joins/symmetric_hash_join.rs+++ b/datafusion/physical-plan/src/joins/symmetric_hash_join.rs@@ -18,11 +18,11 @@//! This file implements the symmetric hash join algorithm with range-based//! data pruning to join two (potentially infinite) streams.//!-//! A [SymmetricHashJoinExec] plan takes two children plan (with appropriate+//! A [`SymmetricHashJoinExec`] plan takes two children plan (with appropriate//! output ordering) and produces the join output according to the given join//! type and other options.//!-//! This plan uses the [OneSideHashJoiner] object to facilitate join calculations+//! This plan uses the [`OneSideHashJoiner`] object to facilitate join calculations//! for both its children.use std::fmt;@@ -38,23 +38,19 @@ use crate::joins::hash_join_utils::{calculate_filter_expr_intervals, combine_two_batches,convert_sort_expr_with_filter_schema, get_pruning_anti_indices,get_pruning_semi_indices, record_visited_indices, PruningJoinHashMap,+ SortedFilterExpr,+};+use crate::joins::utils::{+ build_batch_from_indices, build_join_schema, check_join_is_valid,+ combine_join_equivalence_properties, partitioned_join_output_partitioning,+ prepare_sorted_exprs, ColumnIndex, JoinFilter, JoinOn, JoinSide,};-use crate::joins::StreamJoinPartitionMode;-use crate::DisplayAs;use crate::{- expressions::Column,- expressions::PhysicalSortExpr,- joins::{- hash_join_utils::SortedFilterExpr,- utils::{- build_batch_from_indices, build_join_schema, check_join_is_valid,- combine_join_equivalence_properties, partitioned_join_output_partitioning,- ColumnIndex, JoinFilter, JoinOn, JoinSide,- },- },+ expressions::{Column, PhysicalSortExpr},+ joins::StreamJoinPartitionMode,metrics::{self, ExecutionPlanMetricsSet, MetricBuilder, MetricsSet},- DisplayFormatType, Distribution, EquivalenceProperties, ExecutionPlan, Partitioning,- RecordBatchStream, SendableRecordBatchStream, Statistics,+ DisplayAs, DisplayFormatType, Distribution, EquivalenceProperties, ExecutionPlan,+ Partitioning, RecordBatchStream, SendableRecordBatchStream, Statistics,};use arrow::array::{ArrowPrimitiveType, NativeAdapter, PrimitiveArray, PrimitiveBuilder};@@ -62,13 +58,11 @@ use arrow::compute::concat_batches;use arrow::datatypes::{Schema, SchemaRef};use arrow::record_batch::RecordBatch;use datafusion_common::utils::bisect;-use datafusion_common::{internal_err, plan_err, JoinType};-use datafusion_common::{DataFusionError, Result};+use datafusion_common::{internal_err, plan_err, DataFusionError, JoinType, Result};use datafusion_execution::memory_pool::MemoryConsumer;use datafusion_execution::TaskContext;use datafusion_physical_expr::intervals::ExprIntervalGraph;-use crate::joins::utils::prepare_sorted_exprs;use ahash::RandomState;use futures::stream::{select, BoxStream};use futures::{Stream, StreamExt};@@ -467,9 +461,9 @@ impl ExecutionPlan for SymmetricHashJoinExec {Some(self.metrics.clone_inner())}- fn statistics(&self) -> Statistics {+ fn statistics(&self) -> Result<Statistics> {// TODO stats: it is not possible in general to know the output size of joins- Statistics::default()+ Ok(Statistics::new_unknown(&self.schema()))}fn execute(@@ -1204,17 +1198,11 @@ impl SymmetricHashJoinStream {#[cfg(test)]mod tests {- use super::*;- use arrow::compute::SortOptions;- use arrow::datatypes::{DataType, Field, IntervalUnit, Schema, TimeUnit};- use datafusion_execution::config::SessionConfig;- use rstest::*;-- use datafusion_expr::Operator;- use datafusion_physical_expr::expressions::{binary, col, Column};+ use std::collections::HashMap;+ use std::sync::Mutex;+ use super::*;use crate::joins::hash_join_utils::tests::complicated_filter;-use crate::joins::test_utils::{build_sides_record_batches, compare_batches, create_memory_table,join_expr_tests_fixture_f64, join_expr_tests_fixture_i32,@@ -1222,11 +1210,16 @@ mod tests {partitioned_sym_join_with_filter, split_record_batches,};- const TABLE_SIZE: i32 = 30;+ use arrow::compute::SortOptions;+ use arrow::datatypes::{DataType, Field, IntervalUnit, Schema, TimeUnit};+ use datafusion_execution::config::SessionConfig;+ use datafusion_expr::Operator;+ use datafusion_physical_expr::expressions::{binary, col, Column};use once_cell::sync::Lazy;- use std::collections::HashMap;- use std::sync::Mutex;+ use rstest::*;++ const TABLE_SIZE: i32 = 30;type TableKey = (i32, i32, usize); // (cardinality.0, cardinality.1, batch_size)type TableValue = (Vec<RecordBatch>, Vec<RecordBatch>); // (left, right)diff --git a/datafusion/physical-plan/src/joins/utils.rs b/datafusion/physical-plan/src/joins/utils.rsindex 1dbcf76173..c523a8fe81 100644--- a/datafusion/physical-plan/src/joins/utils.rs+++ b/datafusion/physical-plan/src/joins/utils.rs@@ -17,7 +17,6 @@//! Join related functionality used both on logical and physical plans-use std::cmp::max;use std::collections::HashSet;use std::fmt::{Display, Formatter};use std::future::Future;@@ -25,10 +24,11 @@ use std::sync::Arc;use std::task::{Context, Poll};use std::usize;+use crate::joins::hash_join_utils::{build_filter_input_order, SortedFilterExpr};use crate::metrics::{self, ExecutionPlanMetricsSet, MetricBuilder};-use crate::SchemaRef;use crate::{- ColumnStatistics, EquivalenceProperties, ExecutionPlan, Partitioning, Statistics,+ ColumnStatistics, EquivalenceProperties, ExecutionPlan, Partitioning, SchemaRef,+ Statistics,};use arrow::array::{@@ -39,21 +39,21 @@ use arrow::compute;use arrow::datatypes::{Field, Schema, SchemaBuilder};use arrow::record_batch::{RecordBatch, RecordBatchOptions};use datafusion_common::cast::as_boolean_array;+use datafusion_common::stats::Precision;use datafusion_common::tree_node::{Transformed, TreeNode};use datafusion_common::{exec_err, plan_datafusion_err, plan_err, DataFusionError, JoinType, Result,- ScalarValue, SharedResult,+ SharedResult,};use datafusion_physical_expr::expressions::Column;+use datafusion_physical_expr::intervals::{ExprIntervalGraph, Interval, IntervalBound};+use datafusion_physical_expr::utils::merge_vectors;use datafusion_physical_expr::{add_offset_to_lex_ordering, EquivalentClass, LexOrdering, LexOrderingRef,OrderingEquivalenceProperties, OrderingEquivalentClass, PhysicalExpr,PhysicalSortExpr,};-use crate::joins::hash_join_utils::{build_filter_input_order, SortedFilterExpr};-use datafusion_physical_expr::intervals::ExprIntervalGraph;-use datafusion_physical_expr::utils::merge_vectors;use futures::future::{BoxFuture, Shared};use futures::{ready, FutureExt};use parking_lot::Mutex;@@ -723,21 +723,21 @@ pub(crate) fn estimate_join_statistics(right: Arc<dyn ExecutionPlan>,on: JoinOn,join_type: &JoinType,-) -> Statistics {- let left_stats = left.statistics();- let right_stats = right.statistics();+ schema: &Schema,+) -> Result<Statistics> {+ let left_stats = left.statistics()?;+ let right_stats = right.statistics()?;let join_stats = estimate_join_cardinality(join_type, left_stats, right_stats, &on);let (num_rows, column_statistics) = match join_stats {- Some(stats) => (Some(stats.num_rows), Some(stats.column_statistics)),- None => (None, None),+ Some(stats) => (Precision::Inexact(stats.num_rows), stats.column_statistics),+ None => (Precision::Absent, Statistics::unknown_column(schema)),};- Statistics {+ Ok(Statistics {num_rows,- total_byte_size: None,+ total_byte_size: Precision::Absent,column_statistics,- is_exact: false,- }+ })}// Estimate the cardinality for the given join with input statistics.@@ -749,29 +749,27 @@ fn estimate_join_cardinality() -> Option<PartialJoinStatistics> {match join_type {JoinType::Inner | JoinType::Left | JoinType::Right | JoinType::Full => {- let left_num_rows = left_stats.num_rows?;- let right_num_rows = right_stats.num_rows?;-- // Take the left_col_stats and right_col_stats using the index- // obtained from index() method of the each element of 'on'.- let all_left_col_stats = left_stats.column_statistics?;- let all_right_col_stats = right_stats.column_statistics?;let (left_col_stats, right_col_stats) = on.iter().map(|(left, right)| {(- all_left_col_stats[left.index()].clone(),- all_right_col_stats[right.index()].clone(),+ left_stats.column_statistics[left.index()].clone(),+ right_stats.column_statistics[right.index()].clone(),)}).unzip::<_, _, Vec<_>, Vec<_>>();let ij_cardinality = estimate_inner_join_cardinality(- left_num_rows,- right_num_rows,- left_col_stats,- right_col_stats,- left_stats.is_exact && right_stats.is_exact,+ Statistics {+ num_rows: left_stats.num_rows.clone(),+ total_byte_size: Precision::Absent,+ column_statistics: left_col_stats,+ },+ Statistics {+ num_rows: right_stats.num_rows.clone(),+ total_byte_size: Precision::Absent,+ column_statistics: right_col_stats,+ },)?;// The cardinality for inner join can also be used to estimate@@ -780,25 +778,25 @@ fn estimate_join_cardinality(// joins (so that we don't underestimate the cardinality).let cardinality = match join_type {JoinType::Inner => ij_cardinality,- JoinType::Left => max(ij_cardinality, left_num_rows),- JoinType::Right => max(ij_cardinality, right_num_rows),- JoinType::Full => {- max(ij_cardinality, left_num_rows)- + max(ij_cardinality, right_num_rows)- - ij_cardinality- }+ JoinType::Left => ij_cardinality.max(&left_stats.num_rows),+ JoinType::Right => ij_cardinality.max(&right_stats.num_rows),+ JoinType::Full => ij_cardinality+ .max(&left_stats.num_rows)+ .add(&ij_cardinality.max(&right_stats.num_rows))+ .sub(&ij_cardinality),_ => unreachable!(),};Some(PartialJoinStatistics {- num_rows: cardinality,+ num_rows: *cardinality.get_value()?,// We don't do anything specific here, just combine the existing// statistics which might yield subpar results (although it is// true, esp regarding min/max). For a better estimation, we need// filter selectivity analysis first.- column_statistics: all_left_col_stats+ column_statistics: left_stats+ .column_statistics.into_iter()- .chain(all_right_col_stats)+ .chain(right_stats.column_statistics).collect(),})}@@ -815,30 +813,47 @@ fn estimate_join_cardinality(/// a very conservative implementation that can quickly give up if there is not/// enough input statistics.fn estimate_inner_join_cardinality(- left_num_rows: usize,- right_num_rows: usize,- left_col_stats: Vec<ColumnStatistics>,- right_col_stats: Vec<ColumnStatistics>,- is_exact: bool,-) -> Option<usize> {+ left_stats: Statistics,+ right_stats: Statistics,+) -> Option<Precision<usize>> {// The algorithm here is partly based on the non-histogram selectivity estimation// from Spark's Catalyst optimizer.-- let mut join_selectivity = None;- for (left_stat, right_stat) in left_col_stats.iter().zip(right_col_stats.iter()) {- if (left_stat.min_value.clone()? > right_stat.max_value.clone()?)- || (left_stat.max_value.clone()? < right_stat.min_value.clone()?)- {- // If there is no overlap in any of the join columns, that means the join- // itself is disjoint and the cardinality is 0. Though we can only assume- // this when the statistics are exact (since it is a very strong assumption).- return if is_exact { Some(0) } else { None };+ let mut join_selectivity = Precision::Absent;+ for (left_stat, right_stat) in left_stats+ .column_statistics+ .iter()+ .zip(right_stats.column_statistics.iter())+ {+ // If there is no overlap in any of the join columns, this means the join+ // itself is disjoint and the cardinality is 0. Though we can only assume+ // this when the statistics are exact (since it is a very strong assumption).+ if left_stat.min_value.get_value()? > right_stat.max_value.get_value()? {+ return Some(+ if left_stat.min_value.is_exact().unwrap_or(false)+ && right_stat.max_value.is_exact().unwrap_or(false)+ {+ Precision::Exact(0)+ } else {+ Precision::Inexact(0)+ },+ );+ }+ if left_stat.max_value.get_value()? < right_stat.min_value.get_value()? {+ return Some(+ if left_stat.max_value.is_exact().unwrap_or(false)+ && right_stat.min_value.is_exact().unwrap_or(false)+ {+ Precision::Exact(0)+ } else {+ Precision::Inexact(0)+ },+ );}- let left_max_distinct = max_distinct_count(left_num_rows, left_stat.clone());- let right_max_distinct = max_distinct_count(right_num_rows, right_stat.clone());- let max_distinct = max(left_max_distinct, right_max_distinct);- if max_distinct > join_selectivity {+ let left_max_distinct = max_distinct_count(&left_stats.num_rows, left_stat)?;+ let right_max_distinct = max_distinct_count(&right_stats.num_rows, right_stat)?;+ let max_distinct = left_max_distinct.max(&right_max_distinct);+ if max_distinct.get_value().is_some() {// Seems like there are a few implementations of this algorithm that implement// exponential decay for the selectivity (like Hive's Optiq Optimizer). Needs// further exploration.@@ -849,9 +864,14 @@ fn estimate_inner_join_cardinality(// With the assumption that the smaller input's domain is generally represented in the bigger// input's domain, we can estimate the inner join's cardinality by taking the cartesian product// of the two inputs and normalizing it by the selectivity factor.+ let left_num_rows = left_stats.num_rows.get_value()?;+ let right_num_rows = right_stats.num_rows.get_value()?;match join_selectivity {- Some(selectivity) if selectivity > 0 => {- Some((left_num_rows * right_num_rows) / selectivity)+ Precision::Exact(value) if value > 0 => {+ Some(Precision::Exact((left_num_rows * right_num_rows) / value))+ }+ Precision::Inexact(value) if value > 0 => {+ Some(Precision::Inexact((left_num_rows * right_num_rows) / value))}// Since we don't have any information about the selectivity (which is derived// from the number of distinct rows information) we can give up here for now.@@ -867,44 +887,46 @@ fn estimate_inner_join_cardinality(/// If distinct_count is available, uses it directly. If the column numeric, and/// has min/max values, then they might be used as a fallback option. Otherwise,/// returns None.-fn max_distinct_count(num_rows: usize, stats: ColumnStatistics) -> Option<usize> {- match (stats.distinct_count, stats.max_value, stats.min_value) {- (Some(_), _, _) => stats.distinct_count,+fn max_distinct_count(+ num_rows: &Precision<usize>,+ stats: &ColumnStatistics,+) -> Option<Precision<usize>> {+ match (+ &stats.distinct_count,+ stats.max_value.get_value(),+ stats.min_value.get_value(),+ ) {+ (Precision::Exact(_), _, _) | (Precision::Inexact(_), _, _) => {+ Some(stats.distinct_count.clone())+ }(_, Some(max), Some(min)) => {- // Note that float support is intentionally omitted here, since the computation- // of a range between two float values is not trivial and the result would be- // highly inaccurate.- let numeric_range = get_int_range(min, max)?;+ let numeric_range = Interval::new(+ IntervalBound::new(min.clone(), false),+ IntervalBound::new(max.clone(), false),+ )+ .cardinality()+ .ok()+ .flatten()? as usize;// The number can never be greater than the number of rows we have (minus// the nulls, since they don't count as distinct values).- let ceiling = num_rows - stats.null_count.unwrap_or(0);- Some(numeric_range.min(ceiling))+ let ceiling =+ num_rows.get_value()? - stats.null_count.get_value().unwrap_or(&0);+ Some(+ if num_rows.is_exact().unwrap_or(false)+ && stats.max_value.is_exact().unwrap_or(false)+ && stats.min_value.is_exact().unwrap_or(false)+ {+ Precision::Exact(numeric_range.min(ceiling))+ } else {+ Precision::Inexact(numeric_range.min(ceiling))+ },+ )}_ => None,}}-/// Return the numeric range between the given min and max values.-fn get_int_range(min: ScalarValue, max: ScalarValue) -> Option<usize> {- let delta = &max.sub(&min).ok()?;- match delta {- ScalarValue::Int8(Some(delta)) if *delta >= 0 => Some(*delta as usize),- ScalarValue::Int16(Some(delta)) if *delta >= 0 => Some(*delta as usize),- ScalarValue::Int32(Some(delta)) if *delta >= 0 => Some(*delta as usize),- ScalarValue::Int64(Some(delta)) if *delta >= 0 => Some(*delta as usize),- ScalarValue::UInt8(Some(delta)) => Some(*delta as usize),- ScalarValue::UInt16(Some(delta)) => Some(*delta as usize),- ScalarValue::UInt32(Some(delta)) => Some(*delta as usize),- ScalarValue::UInt64(Some(delta)) => Some(*delta as usize),- _ => None,- }- // The delta (directly) is not the real range, since it does not include the- // first term.- // E.g. (min=2, max=4) -> (4 - 2) -> 2, but the actual result should be 3 (1, 2, 3).- .map(|open_ended_range| open_ended_range + 1)-}-enum OnceFutState<T> {Pending(OnceFutPending<T>),Ready(SharedResult<Arc<T>>),@@ -1380,13 +1402,15 @@ pub fn prepare_sorted_exprs(#[cfg(test)]mod tests {+ use std::pin::Pin;+use super::*;- use arrow::datatypes::Fields;- use arrow::error::Result as ArrowResult;- use arrow::{datatypes::DataType, error::ArrowError};++ use arrow::datatypes::{DataType, Fields};+ use arrow::error::{ArrowError, Result as ArrowResult};use arrow_schema::SortOptions;+use datafusion_common::ScalarValue;- use std::pin::Pin;fn check(left: &[Column], right: &[Column], on: &[(Column, Column)]) -> Result<()> {let left = left@@ -1539,14 +1563,18 @@ mod tests {fn create_stats(num_rows: Option<usize>,- column_stats: Option<Vec<ColumnStatistics>>,+ column_stats: Vec<ColumnStatistics>,is_exact: bool,) -> Statistics {Statistics {- num_rows,+ num_rows: if is_exact {+ num_rows.map(Precision::Exact)+ } else {+ num_rows.map(Precision::Inexact)+ }+ .unwrap_or(Precision::Absent),column_statistics: column_stats,- is_exact,- ..Default::default()+ total_byte_size: Precision::Absent,}}@@ -1556,9 +1584,15 @@ mod tests {distinct_count: Option<usize>,) -> ColumnStatistics {ColumnStatistics {- distinct_count,- min_value: min.map(|size| ScalarValue::Int64(Some(size))),- max_value: max.map(|size| ScalarValue::Int64(Some(size))),+ distinct_count: distinct_count+ .map(Precision::Inexact)+ .unwrap_or(Precision::Absent),+ min_value: min+ .map(|size| Precision::Inexact(ScalarValue::from(size)))+ .unwrap_or(Precision::Absent),+ max_value: max+ .map(|size| Precision::Inexact(ScalarValue::from(size)))+ .unwrap_or(Precision::Absent),..Default::default()}}@@ -1570,7 +1604,7 @@ mod tests {// over the expected output (since it depends on join type to join type).#[test]fn test_inner_join_cardinality_single_column() -> Result<()> {- let cases: Vec<(PartialStats, PartialStats, Option<usize>)> = vec![+ let cases: Vec<(PartialStats, PartialStats, Option<Precision<usize>>)> = vec![// -----------------------------------------------------------------------------// | left(rows, min, max, distinct), right(rows, min, max, distinct), expected |// -----------------------------------------------------------------------------@@ -1582,70 +1616,70 @@ mod tests {((10, Some(1), Some(10), None),(10, Some(1), Some(10), None),- Some(10),+ Some(Precision::Inexact(10)),),// range(left) > range(right)((10, Some(6), Some(10), None),(10, Some(8), Some(10), None),- Some(20),+ Some(Precision::Inexact(20)),),// range(right) > range(left)((10, Some(8), Some(10), None),(10, Some(6), Some(10), None),- Some(20),+ Some(Precision::Inexact(20)),),// range(left) > len(left), range(right) > len(right)((10, Some(1), Some(15), None),(20, Some(1), Some(40), None),- Some(10),+ Some(Precision::Inexact(10)),),// When we have distinct count.((10, Some(1), Some(10), Some(10)),(10, Some(1), Some(10), Some(10)),- Some(10),+ Some(Precision::Inexact(10)),),// distinct(left) > distinct(right)((10, Some(1), Some(10), Some(5)),(10, Some(1), Some(10), Some(2)),- Some(20),+ Some(Precision::Inexact(20)),),// distinct(right) > distinct(left)((10, Some(1), Some(10), Some(2)),(10, Some(1), Some(10), Some(5)),- Some(20),+ Some(Precision::Inexact(20)),),// min(left) < 0 (range(left) > range(right))((10, Some(-5), Some(5), None),(10, Some(1), Some(5), None),- Some(10),+ Some(Precision::Inexact(10)),),// min(right) < 0, max(right) < 0 (range(right) > range(left))((10, Some(-25), Some(-20), None),(10, Some(-25), Some(-15), None),- Some(10),+ Some(Precision::Inexact(10)),),// range(left) < 0, range(right) >= 0// (there isn't a case where both left and right ranges are negative// so one of them is always going to work, this just proves negative// ranges with bigger absolute values are not are not accidentally used).(- (10, Some(10), Some(0), None),+ (10, Some(-10), Some(0), None),(10, Some(0), Some(10), Some(5)),- Some(20), // It would have been ten if we have used abs(range(left))+ Some(Precision::Inexact(10)),),// range(left) = 1, range(right) = 1((10, Some(1), Some(1), None),(10, Some(1), Some(1), None),- Some(100),+ Some(Precision::Inexact(100)),),//// Edge cases@@ -1670,22 +1704,12 @@ mod tests {((10, Some(0), Some(10), None),(10, Some(11), Some(20), None),- None,+ Some(Precision::Inexact(0)),),((10, Some(11), Some(20), None),(10, Some(0), Some(10), None),- None,- ),- (- (10, Some(5), Some(10), Some(10)),- (10, Some(11), Some(3), Some(10)),- None,- ),- (- (10, Some(10), Some(5), Some(10)),- (10, Some(3), Some(7), Some(10)),- None,+ Some(Precision::Inexact(0)),),// distinct(left) = 0, distinct(right) = 0(@@ -1709,13 +1733,18 @@ mod tests {assert_eq!(estimate_inner_join_cardinality(- left_num_rows,- right_num_rows,- left_col_stats.clone(),- right_col_stats.clone(),- false,+ Statistics {+ num_rows: Precision::Inexact(left_num_rows),+ total_byte_size: Precision::Absent,+ column_statistics: left_col_stats.clone(),+ },+ Statistics {+ num_rows: Precision::Inexact(right_num_rows),+ total_byte_size: Precision::Absent,+ column_statistics: right_col_stats.clone(),+ },),- expected_cardinality+ expected_cardinality.clone());// We should also be able to use join_cardinality to get the same results@@ -1723,18 +1752,22 @@ mod tests {let join_on = vec![(Column::new("a", 0), Column::new("b", 0))];let partial_join_stats = estimate_join_cardinality(&join_type,- create_stats(Some(left_num_rows), Some(left_col_stats.clone()), false),- create_stats(Some(right_num_rows), Some(right_col_stats.clone()), false),+ create_stats(Some(left_num_rows), left_col_stats.clone(), false),+ create_stats(Some(right_num_rows), right_col_stats.clone(), false),&join_on,);assert_eq!(- partial_join_stats.clone().map(|s| s.num_rows),- expected_cardinality+ partial_join_stats+ .clone()+ .map(|s| Precision::Inexact(s.num_rows)),+ expected_cardinality.clone());assert_eq!(partial_join_stats.map(|s| s.column_statistics),- expected_cardinality.map(|_| [left_col_stats, right_col_stats].concat())+ expected_cardinality+ .clone()+ .map(|_| [left_col_stats, right_col_stats].concat()));}Ok(())@@ -1756,13 +1789,18 @@ mod tests {// count is 200, so we are going to pick it.assert_eq!(estimate_inner_join_cardinality(- 400,- 400,- left_col_stats,- right_col_stats,- false+ Statistics {+ num_rows: Precision::Inexact(400),+ total_byte_size: Precision::Absent,+ column_statistics: left_col_stats,+ },+ Statistics {+ num_rows: Precision::Inexact(400),+ total_byte_size: Precision::Absent,+ column_statistics: right_col_stats,+ },),- Some((400 * 400) / 200)+ Some(Precision::Inexact((400 * 400) / 200)));Ok(())}@@ -1770,26 +1808,31 @@ mod tests {#[test]fn test_inner_join_cardinality_decimal_range() -> Result<()> {let left_col_stats = vec![ColumnStatistics {- distinct_count: None,- min_value: Some(ScalarValue::Decimal128(Some(32500), 14, 4)),- max_value: Some(ScalarValue::Decimal128(Some(35000), 14, 4)),+ distinct_count: Precision::Absent,+ min_value: Precision::Inexact(ScalarValue::Decimal128(Some(32500), 14, 4)),+ max_value: Precision::Inexact(ScalarValue::Decimal128(Some(35000), 14, 4)),..Default::default()}];let right_col_stats = vec![ColumnStatistics {- distinct_count: None,- min_value: Some(ScalarValue::Decimal128(Some(33500), 14, 4)),- max_value: Some(ScalarValue::Decimal128(Some(34000), 14, 4)),+ distinct_count: Precision::Absent,+ min_value: Precision::Inexact(ScalarValue::Decimal128(Some(33500), 14, 4)),+ max_value: Precision::Inexact(ScalarValue::Decimal128(Some(34000), 14, 4)),..Default::default()}];assert_eq!(estimate_inner_join_cardinality(- 100,- 100,- left_col_stats,- right_col_stats,- false+ Statistics {+ num_rows: Precision::Inexact(100),+ total_byte_size: Precision::Absent,+ column_statistics: left_col_stats,+ },+ Statistics {+ num_rows: Precision::Inexact(100),+ total_byte_size: Precision::Absent,+ column_statistics: right_col_stats,+ },),None);@@ -1836,8 +1879,8 @@ mod tests {let partial_join_stats = estimate_join_cardinality(&join_type,- create_stats(Some(1000), Some(left_col_stats.clone()), false),- create_stats(Some(2000), Some(right_col_stats.clone()), false),+ create_stats(Some(1000), left_col_stats.clone(), false),+ create_stats(Some(2000), right_col_stats.clone(), false),&join_on,).unwrap();@@ -1901,8 +1944,8 @@ mod tests {for (join_type, expected_num_rows) in cases {let partial_join_stats = estimate_join_cardinality(&join_type,- create_stats(Some(1000), Some(left_col_stats.clone()), true),- create_stats(Some(2000), Some(right_col_stats.clone()), true),+ create_stats(Some(1000), left_col_stats.clone(), true),+ create_stats(Some(2000), right_col_stats.clone(), true),&join_on,).unwrap();diff --git a/datafusion/physical-plan/src/lib.rs b/datafusion/physical-plan/src/lib.rsindex 30ac2cb983..d7987ba95a 100644--- a/datafusion/physical-plan/src/lib.rs+++ b/datafusion/physical-plan/src/lib.rs@@ -17,39 +17,78 @@//! Traits for physical query plan, supporting parallel execution for partitioned relations.-mod topk;-mod visitor;-pub use self::metrics::Metric;-use self::metrics::MetricsSet;-use self::{- coalesce_partitions::CoalescePartitionsExec, display::DisplayableExecutionPlan,-};-pub use datafusion_common::{internal_err, ColumnStatistics, Statistics};-use datafusion_common::{plan_err, Result};-use datafusion_physical_expr::PhysicalSortExpr;-pub use topk::TopK;-pub use visitor::{accept, visit_execution_plan, ExecutionPlanVisitor};+use std::any::Any;+use std::fmt::Debug;+use std::sync::Arc;++use crate::coalesce_partitions::CoalescePartitionsExec;+use crate::display::DisplayableExecutionPlan;+use crate::metrics::MetricsSet;+use crate::repartition::RepartitionExec;+use crate::sorts::sort_preserving_merge::SortPreservingMergeExec;use arrow::datatypes::SchemaRef;use arrow::record_batch::RecordBatch;-+use datafusion_common::tree_node::Transformed;use datafusion_common::utils::DataPtr;-pub use datafusion_expr::Accumulator;-pub use datafusion_expr::ColumnarValue;+use datafusion_common::{plan_err, DataFusionError, Result};+use datafusion_execution::TaskContext;use datafusion_physical_expr::equivalence::OrderingEquivalenceProperties;-pub use display::{DefaultDisplay, DisplayAs, DisplayFormatType, VerboseDisplay};+use datafusion_physical_expr::expressions::Column;+use datafusion_physical_expr::{+ EquivalenceProperties, PhysicalSortExpr, PhysicalSortRequirement,+};+use futures::stream::TryStreamExt;-use std::fmt::Debug;use tokio::task::JoinSet;-use datafusion_common::tree_node::Transformed;-use datafusion_common::DataFusionError;-use std::any::Any;-use std::sync::Arc;+mod topk;+mod visitor;-// backwards compatibility+pub mod aggregates;+pub mod analyze;+pub mod coalesce_batches;+pub mod coalesce_partitions;+pub mod common;+pub mod display;+pub mod empty;+pub mod explain;+pub mod filter;+pub mod insert;+pub mod joins;+pub mod limit;+pub mod memory;+pub mod metrics;+pub mod projection;+pub mod repartition;+pub mod sorts;+pub mod stream;+pub mod streaming;+pub mod tree_node;+pub mod udaf;+pub mod union;+pub mod unnest;+pub mod values;+pub mod windows;++pub use crate::display::{DefaultDisplay, DisplayAs, DisplayFormatType, VerboseDisplay};+pub use crate::metrics::Metric;+pub use crate::topk::TopK;+pub use crate::visitor::{accept, visit_execution_plan, ExecutionPlanVisitor};++pub use datafusion_common::hash_utils;+pub use datafusion_common::utils::project_schema;+pub use datafusion_common::{internal_err, ColumnStatistics, Statistics};+pub use datafusion_expr::{Accumulator, ColumnarValue};+pub use datafusion_physical_expr::window::WindowExpr;+pub use datafusion_physical_expr::{+ expressions, functions, ordering_equivalence_properties_helper, udf, AggregateExpr,+ Distribution, Partitioning, PhysicalExpr,+};++// Backwards compatibility+pub use crate::stream::EmptyRecordBatchStream;pub use datafusion_execution::{RecordBatchStream, SendableRecordBatchStream};-pub use stream::EmptyRecordBatchStream;/// `ExecutionPlan` represent nodes in the DataFusion Physical Plan.///@@ -193,7 +232,7 @@ pub trait ExecutionPlan: Debug + DisplayAs + Send + Sync {}/// Returns the global output statistics for this `ExecutionPlan` node.- fn statistics(&self) -> Statistics;+ fn statistics(&self) -> Result<Statistics>;}/// Indicate whether a data exchange is needed for the input of `plan`, which will be very helpful@@ -343,46 +382,5 @@ pub fn unbounded_output(plan: &Arc<dyn ExecutionPlan>) -> bool {.unwrap_or(true)}-use datafusion_physical_expr::expressions::Column;-pub use datafusion_physical_expr::window::WindowExpr;-pub use datafusion_physical_expr::{AggregateExpr, PhysicalExpr};-pub use datafusion_physical_expr::{Distribution, Partitioning};-use datafusion_physical_expr::{EquivalenceProperties, PhysicalSortRequirement};--pub mod aggregates;-pub mod analyze;-pub mod coalesce_batches;-pub mod coalesce_partitions;-pub mod common;-pub mod display;-pub mod empty;-pub mod explain;-pub mod filter;-pub mod insert;-pub mod joins;-pub mod limit;-pub mod memory;-pub mod metrics;-pub mod projection;-pub mod repartition;-pub mod sorts;-pub mod stream;-pub mod streaming;-pub mod tree_node;-pub mod udaf;-pub mod union;-pub mod unnest;-pub mod values;-pub mod windows;--use crate::repartition::RepartitionExec;-use crate::sorts::sort_preserving_merge::SortPreservingMergeExec;-pub use datafusion_common::hash_utils;-pub use datafusion_common::utils::project_schema;-use datafusion_execution::TaskContext;-pub use datafusion_physical_expr::{- expressions, functions, ordering_equivalence_properties_helper, udf,-};-#[cfg(test)]pub mod test;diff --git a/datafusion/physical-plan/src/limit.rs b/datafusion/physical-plan/src/limit.rsindex 31ed08399c..9fe1eb0763 100644--- a/datafusion/physical-plan/src/limit.rs+++ b/datafusion/physical-plan/src/limit.rs@@ -22,23 +22,22 @@ use std::pin::Pin;use std::sync::Arc;use std::task::{Context, Poll};-use crate::{- DisplayFormatType, Distribution, EquivalenceProperties, ExecutionPlan, Partitioning,-};-use super::expressions::PhysicalSortExpr;use super::metrics::{BaselineMetrics, ExecutionPlanMetricsSet, MetricsSet};use super::{DisplayAs, RecordBatchStream, SendableRecordBatchStream, Statistics};+use crate::{+ DisplayFormatType, Distribution, EquivalenceProperties, ExecutionPlan, Partitioning,+};use arrow::array::ArrayRef;use arrow::datatypes::SchemaRef;use arrow::record_batch::{RecordBatch, RecordBatchOptions};+use datafusion_common::stats::Precision;use datafusion_common::{internal_err, DataFusionError, Result};use datafusion_execution::TaskContext;use datafusion_physical_expr::OrderingEquivalenceProperties;-use futures::stream::Stream;-use futures::stream::StreamExt;+use futures::stream::{Stream, StreamExt};use log::trace;/// Limit execution plan@@ -191,8 +190,8 @@ impl ExecutionPlan for GlobalLimitExec {Some(self.metrics.clone_inner())}- fn statistics(&self) -> Statistics {- let input_stats = self.input.statistics();+ fn statistics(&self) -> Result<Statistics> {+ let input_stats = self.input.statistics()?;let skip = self.skip;// the maximum row number needs to be fetchedlet max_row_num = self@@ -205,16 +204,29 @@ impl ExecutionPlan for GlobalLimitExec {}}).unwrap_or(usize::MAX);- match input_stats {+ let col_stats = Statistics::unknown_column(&self.schema());++ let fetched_row_number_stats = Statistics {+ num_rows: Precision::Exact(max_row_num),+ column_statistics: col_stats.clone(),+ total_byte_size: Precision::Absent,+ };++ let stats = match input_stats {Statistics {- num_rows: Some(nr), ..+ num_rows: Precision::Exact(nr),+ ..+ }+ | Statistics {+ num_rows: Precision::Inexact(nr),+ ..} => {if nr <= skip {// if all input data will be skipped, return 0Statistics {- num_rows: Some(0),- is_exact: input_stats.is_exact,- ..Default::default()+ num_rows: Precision::Exact(0),+ column_statistics: col_stats,+ total_byte_size: Precision::Absent,}} else if nr <= max_row_num {// if the input does not reach the "fetch" globally, return input stats@@ -222,20 +234,15 @@ impl ExecutionPlan for GlobalLimitExec {} else {// if the input is greater than the "fetch", the num_row will be the "fetch",// but we won't be able to predict the other statistics- Statistics {- num_rows: Some(max_row_num),- is_exact: input_stats.is_exact,- ..Default::default()- }+ fetched_row_number_stats}}- _ => Statistics {+ _ => {// the result output row number will always be no greater than the limit number- num_rows: Some(max_row_num),- is_exact: false,- ..Default::default()- },- }+ fetched_row_number_stats+ }+ };+ Ok(stats)}}@@ -361,32 +368,53 @@ impl ExecutionPlan for LocalLimitExec {Some(self.metrics.clone_inner())}- fn statistics(&self) -> Statistics {- let input_stats = self.input.statistics();- match input_stats {+ fn statistics(&self) -> Result<Statistics> {+ let input_stats = self.input.statistics()?;+ let col_stats = Statistics::unknown_column(&self.schema());+ let stats = match input_stats {// if the input does not reach the limit globally, return input statsStatistics {- num_rows: Some(nr), ..+ num_rows: Precision::Exact(nr),+ ..+ }+ | Statistics {+ num_rows: Precision::Inexact(nr),+ ..} if nr <= self.fetch => input_stats,// if the input is greater than the limit, the num_row will be greater// than the limit because the partitions will be limited separatly// the statisticStatistics {- num_rows: Some(nr), ..+ num_rows: Precision::Exact(nr),+ ..+ } if nr > self.fetch => Statistics {+ num_rows: Precision::Exact(self.fetch),+ // this is not actually exact, but will be when GlobalLimit is applied+ // TODO stats: find a more explicit way to vehiculate this information+ column_statistics: col_stats,+ total_byte_size: Precision::Absent,+ },+ Statistics {+ num_rows: Precision::Inexact(nr),+ ..} if nr > self.fetch => Statistics {- num_rows: Some(self.fetch),+ num_rows: Precision::Inexact(self.fetch),// this is not actually exact, but will be when GlobalLimit is applied// TODO stats: find a more explicit way to vehiculate this information- is_exact: input_stats.is_exact,- ..Default::default()+ column_statistics: col_stats,+ total_byte_size: Precision::Absent,},_ => Statistics {// the result output row number will always be no greater than the limit number- num_rows: Some(self.fetch * self.output_partitioning().partition_count()),- is_exact: false,- ..Default::default()+ num_rows: Precision::Inexact(+ self.fetch * self.output_partitioning().partition_count(),+ ),++ column_statistics: col_stats,+ total_byte_size: Precision::Absent,},- }+ };+ Ok(stats)}}@@ -528,14 +556,12 @@ impl RecordBatchStream for LimitStream {#[cfg(test)]mod tests {-- use arrow_schema::Schema;- use common::collect;-use super::*;use crate::coalesce_partitions::CoalescePartitionsExec;- use crate::common;- use crate::test;+ use crate::common::collect;+ use crate::{common, test};++ use arrow_schema::Schema;#[tokio::test]async fn limit() -> Result<()> {@@ -728,10 +754,10 @@ mod tests {#[tokio::test]async fn test_row_number_statistics_for_global_limit() -> Result<()> {let row_count = row_number_statistics_for_global_limit(0, Some(10)).await?;- assert_eq!(row_count, Some(10));+ assert_eq!(row_count, Precision::Exact(10));let row_count = row_number_statistics_for_global_limit(5, Some(10)).await?;- assert_eq!(row_count, Some(15));+ assert_eq!(row_count, Precision::Exact(15));Ok(())}@@ -739,7 +765,7 @@ mod tests {#[tokio::test]async fn test_row_number_statistics_for_local_limit() -> Result<()> {let row_count = row_number_statistics_for_local_limit(4, 10).await?;- assert_eq!(row_count, Some(10));+ assert_eq!(row_count, Precision::Exact(10));Ok(())}@@ -747,7 +773,7 @@ mod tests {async fn row_number_statistics_for_global_limit(skip: usize,fetch: Option<usize>,- ) -> Result<Option<usize>> {+ ) -> Result<Precision<usize>> {let num_partitions = 4;let csv = test::scan_partitioned(num_partitions);@@ -756,20 +782,20 @@ mod tests {let offset =GlobalLimitExec::new(Arc::new(CoalescePartitionsExec::new(csv)), skip, fetch);- Ok(offset.statistics().num_rows)+ Ok(offset.statistics()?.num_rows)}async fn row_number_statistics_for_local_limit(num_partitions: usize,fetch: usize,- ) -> Result<Option<usize>> {+ ) -> Result<Precision<usize>> {let csv = test::scan_partitioned(num_partitions);assert_eq!(csv.output_partitioning().partition_count(), num_partitions);let offset = LocalLimitExec::new(csv, fetch);- Ok(offset.statistics().num_rows)+ Ok(offset.statistics()?.num_rows)}/// Return a RecordBatch with a single array with row_count szdiff --git a/datafusion/physical-plan/src/memory.rs b/datafusion/physical-plan/src/memory.rsindex d919ded8d0..b535005792 100644--- a/datafusion/physical-plan/src/memory.rs+++ b/datafusion/physical-plan/src/memory.rs@@ -17,23 +17,24 @@//! Execution plan for reading in-memory batches of data+use std::any::Any;+use std::fmt;+use std::sync::Arc;+use std::task::{Context, Poll};+use super::expressions::PhysicalSortExpr;use super::{common, DisplayAs, DisplayFormatType, ExecutionPlan, Partitioning, RecordBatchStream,SendableRecordBatchStream, Statistics,};+use crate::ordering_equivalence_properties_helper;+use arrow::datatypes::SchemaRef;use arrow::record_batch::RecordBatch;-use core::fmt;-use datafusion_common::{internal_err, project_schema, Result};-use std::any::Any;-use std::sync::Arc;-use std::task::{Context, Poll};--use crate::ordering_equivalence_properties_helper;-use datafusion_common::DataFusionError;+use datafusion_common::{internal_err, project_schema, DataFusionError, Result};use datafusion_execution::TaskContext;use datafusion_physical_expr::{LexOrdering, OrderingEquivalenceProperties};+use futures::Stream;/// Execution plan for reading in-memory batches of data@@ -150,12 +151,12 @@ impl ExecutionPlan for MemoryExec {}/// We recompute the statistics dynamically from the arrow metadata as it is pretty cheap to do so- fn statistics(&self) -> Statistics {- common::compute_record_batch_statistics(+ fn statistics(&self) -> Result<Statistics> {+ Ok(common::compute_record_batch_statistics(&self.partitions,&self.schema,self.projection.clone(),- )+ ))}}@@ -266,12 +267,14 @@ impl RecordBatchStream for MemoryStream {#[cfg(test)]mod tests {+ use std::sync::Arc;+use crate::memory::MemoryExec;use crate::ExecutionPlan;+use arrow_schema::{DataType, Field, Schema, SortOptions};use datafusion_physical_expr::expressions::col;use datafusion_physical_expr::PhysicalSortExpr;- use std::sync::Arc;#[test]fn test_memory_order_eq() -> datafusion_common::Result<()> {diff --git a/datafusion/physical-plan/src/projection.rs b/datafusion/physical-plan/src/projection.rsindex f4a2cd339e..029dd24d7d 100644--- a/datafusion/physical-plan/src/projection.rs+++ b/datafusion/physical-plan/src/projection.rs@@ -36,15 +36,16 @@ use crate::{use arrow::datatypes::{Field, Schema, SchemaRef};use arrow::record_batch::{RecordBatch, RecordBatchOptions};+use datafusion_common::stats::Precision;use datafusion_common::Result;use datafusion_execution::TaskContext;use datafusion_physical_expr::expressions::{Literal, UnKnownColumn};+use datafusion_physical_expr::utils::find_orderings_of_exprs;use datafusion_physical_expr::{normalize_out_expr_with_columns_map, project_equivalence_properties,project_ordering_equivalence_properties, OrderingEquivalenceProperties,};-use datafusion_physical_expr::utils::find_orderings_of_exprs;use futures::stream::{Stream, StreamExt};use log::trace;@@ -330,12 +331,12 @@ impl ExecutionPlan for ProjectionExec {Some(self.metrics.clone_inner())}- fn statistics(&self) -> Statistics {- stats_projection(- self.input.statistics(),+ fn statistics(&self) -> Result<Statistics> {+ Ok(stats_projection(+ self.input.statistics()?,self.expr.iter().map(|(e, _)| Arc::clone(e)),self.schema.clone(),- )+ ))}}@@ -392,57 +393,37 @@ fn get_field_metadata(}fn stats_projection(- stats: Statistics,+ mut 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| {- inner_exprs- .clone()- .into_iter()- .map(|e| {- if let Some(col) = e.as_any().downcast_ref::<Column>() {- input_col_stats[col.index()].clone()- } else {- // TODO stats: estimate more statistics from expressions- // (expressions should compute their statistics themselves)- ColumnStatistics::default()- }- })- .collect()- });-- 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- total_byte_size: stats.total_byte_size,+ let mut primitive_row_size = 0;+ let mut primitive_row_size_possible = true;+ let mut column_statistics = vec![];+ for expr in exprs {+ let col_stats = if let Some(col) = expr.as_any().downcast_ref::<Column>() {+ stats.column_statistics[col.index()].clone()+ } else {+ // TODO stats: estimate more statistics from expressions+ // (expressions should compute their statistics themselves)+ ColumnStatistics::new_unknown()+ };+ column_statistics.push(col_stats);+ if let Ok(data_type) = expr.data_type(&schema) {+ if let Some(value) = data_type.primitive_width() {+ primitive_row_size += value;+ continue;}}+ primitive_row_size_possible = false;+ }++ if primitive_row_size_possible {+ stats.total_byte_size =+ Precision::Exact(primitive_row_size).multiply(&stats.num_rows);}+ stats.column_statistics = column_statistics;+ stats}impl ProjectionStream {@@ -529,29 +510,32 @@ mod tests {fn get_stats() -> Statistics {Statistics {- is_exact: true,- num_rows: Some(5),- total_byte_size: Some(23),- column_statistics: Some(vec![+ num_rows: Precision::Exact(5),+ total_byte_size: Precision::Exact(23),+ column_statistics: vec![ColumnStatistics {- distinct_count: Some(5),- max_value: Some(ScalarValue::Int64(Some(21))),- min_value: Some(ScalarValue::Int64(Some(-4))),- null_count: Some(0),+ distinct_count: Precision::Exact(5),+ max_value: Precision::Exact(ScalarValue::Int64(Some(21))),+ min_value: Precision::Exact(ScalarValue::Int64(Some(-4))),+ null_count: Precision::Exact(0),},ColumnStatistics {- distinct_count: Some(1),- max_value: Some(ScalarValue::Utf8(Some(String::from("x")))),- min_value: Some(ScalarValue::Utf8(Some(String::from("a")))),- null_count: Some(3),+ distinct_count: Precision::Exact(1),+ max_value: Precision::Exact(ScalarValue::Utf8(Some(String::from(+ "x",+ )))),+ min_value: Precision::Exact(ScalarValue::Utf8(Some(String::from(+ "a",+ )))),+ null_count: Precision::Exact(3),},ColumnStatistics {- distinct_count: None,- max_value: Some(ScalarValue::Float32(Some(1.1))),- min_value: Some(ScalarValue::Float32(Some(0.1))),- null_count: None,+ distinct_count: Precision::Absent,+ max_value: Precision::Exact(ScalarValue::Float32(Some(1.1))),+ min_value: Precision::Exact(ScalarValue::Float32(Some(0.1))),+ null_count: Precision::Absent,},- ]),+ ],}}@@ -574,23 +558,26 @@ mod tests {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(23),- column_statistics: Some(vec![+ num_rows: Precision::Exact(5),+ total_byte_size: Precision::Exact(23),+ column_statistics: vec![ColumnStatistics {- distinct_count: Some(1),- max_value: Some(ScalarValue::Utf8(Some(String::from("x")))),- min_value: Some(ScalarValue::Utf8(Some(String::from("a")))),- null_count: Some(3),+ distinct_count: Precision::Exact(1),+ max_value: Precision::Exact(ScalarValue::Utf8(Some(String::from(+ "x",+ )))),+ min_value: Precision::Exact(ScalarValue::Utf8(Some(String::from(+ "a",+ )))),+ null_count: Precision::Exact(3),},ColumnStatistics {- distinct_count: Some(5),- max_value: Some(ScalarValue::Int64(Some(21))),- min_value: Some(ScalarValue::Int64(Some(-4))),- null_count: Some(0),+ distinct_count: Precision::Exact(5),+ max_value: Precision::Exact(ScalarValue::Int64(Some(21))),+ min_value: Precision::Exact(ScalarValue::Int64(Some(-4))),+ null_count: Precision::Exact(0),},- ]),+ ],};assert_eq!(result, expected);@@ -609,23 +596,22 @@ mod tests {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![+ num_rows: Precision::Exact(5),+ total_byte_size: Precision::Exact(60),+ column_statistics: vec![ColumnStatistics {- distinct_count: None,- max_value: Some(ScalarValue::Float32(Some(1.1))),- min_value: Some(ScalarValue::Float32(Some(0.1))),- null_count: None,+ distinct_count: Precision::Absent,+ max_value: Precision::Exact(ScalarValue::Float32(Some(1.1))),+ min_value: Precision::Exact(ScalarValue::Float32(Some(0.1))),+ null_count: Precision::Absent,},ColumnStatistics {- distinct_count: Some(5),- max_value: Some(ScalarValue::Int64(Some(21))),- min_value: Some(ScalarValue::Int64(Some(-4))),- null_count: Some(0),+ distinct_count: Precision::Exact(5),+ max_value: Precision::Exact(ScalarValue::Int64(Some(21))),+ min_value: Precision::Exact(ScalarValue::Int64(Some(-4))),+ null_count: Precision::Exact(0),},- ]),+ ],};assert_eq!(result, expected);diff --git a/datafusion/physical-plan/src/repartition/mod.rs b/datafusion/physical-plan/src/repartition/mod.rsindex bcb9c3afee..d1638a0277 100644--- a/datafusion/physical-plan/src/repartition/mod.rs+++ b/datafusion/physical-plan/src/repartition/mod.rs@@ -620,7 +620,7 @@ impl ExecutionPlan for RepartitionExec {Some(self.metrics.clone_inner())}- fn statistics(&self) -> Statistics {+ fn statistics(&self) -> Result<Statistics> {self.input.statistics()}}diff --git a/datafusion/physical-plan/src/sorts/cursor.rs b/datafusion/physical-plan/src/sorts/cursor.rsindex 52de880bae..1ca41d4fe2 100644--- a/datafusion/physical-plan/src/sorts/cursor.rs+++ b/datafusion/physical-plan/src/sorts/cursor.rs@@ -15,14 +15,15 @@// specific language governing permissions and limitations// under the License.-use crate::sorts::sort::SortOptions;+use std::cmp::Ordering;+use arrow::buffer::ScalarBuffer;+use arrow::compute::SortOptions;use arrow::datatypes::ArrowNativeTypeOp;use arrow::row::{Row, Rows};use arrow_array::types::ByteArrayType;use arrow_array::{Array, ArrowPrimitiveType, GenericByteArray, PrimitiveArray};use datafusion_execution::memory_pool::MemoryReservation;-use std::cmp::Ordering;/// A [`Cursor`] for [`Rows`]pub struct RowCursor {diff --git a/datafusion/physical-plan/src/sorts/sort.rs b/datafusion/physical-plan/src/sorts/sort.rsindex fb6f460f1b..c7d676493f 100644--- a/datafusion/physical-plan/src/sorts/sort.rs+++ b/datafusion/physical-plan/src/sorts/sort.rs@@ -19,6 +19,14 @@//! It will do in-memory sorting if it has enough memory budget//! but spills to disk if needed.+use std::any::Any;+use std::fmt;+use std::fmt::{Debug, Formatter};+use std::fs::File;+use std::io::BufReader;+use std::path::{Path, PathBuf};+use std::sync::Arc;+use crate::common::{spawn_buffered, IPCWriter};use crate::expressions::PhysicalSortExpr;use crate::metrics::{@@ -31,7 +39,7 @@ use crate::{DisplayAs, DisplayFormatType, Distribution, EmptyRecordBatchStream, ExecutionPlan,Partitioning, SendableRecordBatchStream, Statistics,};-pub use arrow::compute::SortOptions;+use arrow::compute::{concat_batches, lexsort_to_indices, take};use arrow::datatypes::SchemaRef;use arrow::ipc::reader::FileReader;@@ -44,15 +52,9 @@ use datafusion_execution::memory_pool::{use datafusion_execution::runtime_env::RuntimeEnv;use datafusion_execution::TaskContext;use datafusion_physical_expr::EquivalenceProperties;+use futures::{StreamExt, TryStreamExt};use log::{debug, error, trace};-use std::any::Any;-use std::fmt;-use std::fmt::{Debug, Formatter};-use std::fs::File;-use std::io::BufReader;-use std::path::{Path, PathBuf};-use std::sync::Arc;use tokio::sync::mpsc::Sender;use tokio::task;@@ -908,13 +910,15 @@ impl ExecutionPlan for SortExec {Some(self.metrics_set.clone_inner())}- fn statistics(&self) -> Statistics {+ fn statistics(&self) -> Result<Statistics> {self.input.statistics()}}#[cfg(test)]mod tests {+ use std::collections::HashMap;+use super::*;use crate::coalesce_partitions::CoalescePartitionsExec;use crate::collect;@@ -923,14 +927,15 @@ mod tests {use crate::test;use crate::test::assert_is_pending;use crate::test::exec::{assert_strong_count_converges_to_zero, BlockingExec};+use arrow::array::*;use arrow::compute::SortOptions;use arrow::datatypes::*;use datafusion_common::cast::as_primitive_array;use datafusion_execution::config::SessionConfig;use datafusion_execution::runtime_env::RuntimeConfig;+use futures::FutureExt;- use std::collections::HashMap;#[tokio::test]async fn test_in_mem_sort() -> Result<()> {diff --git a/datafusion/physical-plan/src/sorts/sort_preserving_merge.rs b/datafusion/physical-plan/src/sorts/sort_preserving_merge.rsindex f7a9e193ee..ee044d9172 100644--- a/datafusion/physical-plan/src/sorts/sort_preserving_merge.rs+++ b/datafusion/physical-plan/src/sorts/sort_preserving_merge.rs@@ -28,10 +28,10 @@ use crate::{DisplayAs, DisplayFormatType, Distribution, ExecutionPlan, Partitioning,SendableRecordBatchStream, Statistics,};-use datafusion_execution::memory_pool::MemoryConsumer;use arrow::datatypes::SchemaRef;use datafusion_common::{internal_err, DataFusionError, Result};+use datafusion_execution::memory_pool::MemoryConsumer;use datafusion_execution::TaskContext;use datafusion_physical_expr::{EquivalenceProperties, OrderingEquivalenceProperties, PhysicalSortRequirement,@@ -264,22 +264,14 @@ impl ExecutionPlan for SortPreservingMergeExec {Some(self.metrics.clone_inner())}- fn statistics(&self) -> Statistics {+ fn statistics(&self) -> Result<Statistics> {self.input.statistics()}}#[cfg(test)]mod tests {- use std::iter::FromIterator;-- use arrow::array::ArrayRef;- use arrow::compute::SortOptions;- use arrow::datatypes::{DataType, Field, Schema};- use arrow::record_batch::RecordBatch;- use datafusion_execution::config::SessionConfig;- use futures::{FutureExt, StreamExt};-+ use super::*;use crate::coalesce_partitions::CoalescePartitionsExec;use crate::expressions::col;use crate::memory::MemoryExec;@@ -289,10 +281,16 @@ mod tests {use crate::test::exec::{assert_strong_count_converges_to_zero, BlockingExec};use crate::test::{self, assert_is_pending, make_partition};use crate::{collect, common};+ use arrow::array::ArrayRef;use arrow::array::{Int32Array, StringArray, TimestampNanosecondArray};+ use arrow::compute::SortOptions;+ use arrow::datatypes::{DataType, Field, Schema};+ use arrow::record_batch::RecordBatch;use datafusion_common::{assert_batches_eq, assert_contains};+ use datafusion_execution::config::SessionConfig;- use super::*;+ use futures::{FutureExt, StreamExt};+ use std::iter::FromIterator;#[tokio::test]async fn test_merge_interleave() {diff --git a/datafusion/physical-plan/src/streaming.rs b/datafusion/physical-plan/src/streaming.rsindex cb972fa41e..7bfa7e2cee 100644--- a/datafusion/physical-plan/src/streaming.rs+++ b/datafusion/physical-plan/src/streaming.rs@@ -20,20 +20,19 @@use std::any::Any;use std::sync::Arc;-use arrow::datatypes::SchemaRef;-use async_trait::async_trait;-use futures::stream::StreamExt;--use datafusion_common::{internal_err, plan_err, DataFusionError, Result, Statistics};-use datafusion_physical_expr::{LexOrdering, PhysicalSortExpr};-use log::debug;-+use super::{DisplayAs, DisplayFormatType};use crate::display::{OutputOrderingDisplay, ProjectSchemaDisplay};use crate::stream::RecordBatchStreamAdapter;use crate::{ExecutionPlan, Partitioning, SendableRecordBatchStream};++use arrow::datatypes::SchemaRef;+use datafusion_common::{internal_err, plan_err, DataFusionError, Result, Statistics};use datafusion_execution::TaskContext;+use datafusion_physical_expr::{LexOrdering, PhysicalSortExpr};-use super::{DisplayAs, DisplayFormatType};+use async_trait::async_trait;+use futures::stream::StreamExt;+use log::debug;/// A partition that can be converted into a [`SendableRecordBatchStream`]pub trait PartitionStream: Send + Sync {@@ -189,7 +188,7 @@ impl ExecutionPlan for StreamingTableExec {})}- fn statistics(&self) -> Statistics {- Default::default()+ fn statistics(&self) -> Result<Statistics> {+ Ok(Statistics::new_unknown(&self.schema()))}}diff --git a/datafusion/physical-plan/src/test/exec.rs b/datafusion/physical-plan/src/test/exec.rsindex a1f40c7ba9..f90f4231c6 100644--- a/datafusion/physical-plan/src/test/exec.rs+++ b/datafusion/physical-plan/src/test/exec.rs@@ -23,23 +23,21 @@ use std::{sync::{Arc, Weak},task::{Context, Poll},};-use tokio::sync::Barrier;--use arrow::{- datatypes::{DataType, Field, Schema, SchemaRef},- record_batch::RecordBatch,-};-use futures::Stream;+use crate::stream::{RecordBatchReceiverStream, RecordBatchStreamAdapter};use crate::{- common, stream::RecordBatchReceiverStream, stream::RecordBatchStreamAdapter,- DisplayAs, DisplayFormatType, ExecutionPlan, Partitioning, RecordBatchStream,+ common, DisplayAs, DisplayFormatType, ExecutionPlan, Partitioning, RecordBatchStream,SendableRecordBatchStream, Statistics,};-use datafusion_physical_expr::PhysicalSortExpr;+use arrow::datatypes::{DataType, Field, Schema, SchemaRef};+use arrow::record_batch::RecordBatch;use datafusion_common::{internal_err, DataFusionError, Result};use datafusion_execution::TaskContext;+use datafusion_physical_expr::PhysicalSortExpr;++use futures::Stream;+use tokio::sync::Barrier;/// Index into the data that has been returned so far#[derive(Debug, Default, Clone)]@@ -239,7 +237,7 @@ impl ExecutionPlan for MockExec {}// Panics if one of the batches is an error- fn statistics(&self) -> Statistics {+ fn statistics(&self) -> Result<Statistics> {let data: Result<Vec<_>> = self.data.iter()@@ -249,9 +247,13 @@ impl ExecutionPlan for MockExec {}).collect();- let data = data.unwrap();+ let data = data?;- common::compute_record_batch_statistics(&[data], &self.schema, None)+ Ok(common::compute_record_batch_statistics(+ &[data],+ &self.schema,+ None,+ ))}}@@ -369,8 +371,12 @@ impl ExecutionPlan for BarrierExec {Ok(builder.build())}- fn statistics(&self) -> Statistics {- common::compute_record_batch_statistics(&self.data, &self.schema, None)+ fn statistics(&self) -> Result<Statistics> {+ Ok(common::compute_record_batch_statistics(+ &self.data,+ &self.schema,+ None,+ ))}}@@ -448,8 +454,8 @@ impl ExecutionPlan for ErrorExec {internal_err!("ErrorExec, unsurprisingly, errored in partition {partition}")}- fn statistics(&self) -> Statistics {- Statistics::default()+ fn statistics(&self) -> Result<Statistics> {+ Ok(Statistics::new_unknown(&self.schema()))}}@@ -461,12 +467,9 @@ pub struct StatisticsExec {}impl StatisticsExec {pub fn new(stats: Statistics, schema: Schema) -> Self {- assert!(+ assert_eq!(stats- .column_statistics- .as_ref()- .map(|cols| cols.len() == schema.fields().len())- .unwrap_or(true),+ .column_statistics.len(), schema.fields().len(),"if defined, the column statistics vector length should be the number of fields");Self {@@ -531,8 +534,8 @@ impl ExecutionPlan for StatisticsExec {unimplemented!("This plan only serves for testing statistics")}- fn statistics(&self) -> Statistics {- self.stats.clone()+ fn statistics(&self) -> Result<Statistics> {+ Ok(self.stats.clone())}}@@ -625,7 +628,7 @@ impl ExecutionPlan for BlockingExec {}))}- fn statistics(&self) -> Statistics {+ fn statistics(&self) -> Result<Statistics> {unimplemented!()}}@@ -762,7 +765,7 @@ impl ExecutionPlan for PanicExec {}))}- fn statistics(&self) -> Statistics {+ fn statistics(&self) -> Result<Statistics> {unimplemented!()}}diff --git a/datafusion/physical-plan/src/union.rs b/datafusion/physical-plan/src/union.rsindex af765e257d..2727a6e161 100644--- a/datafusion/physical-plan/src/union.rs+++ b/datafusion/physical-plan/src/union.rs@@ -25,27 +25,25 @@ use std::pin::Pin;use std::task::{Context, Poll};use std::{any::Any, sync::Arc};-use arrow::{- datatypes::{Field, Schema, SchemaRef},- record_batch::RecordBatch,-};-use datafusion_common::{exec_err, internal_err, DFSchemaRef, DataFusionError};-use futures::Stream;-use itertools::Itertools;-use log::{debug, trace, warn};--use super::DisplayAs;use super::{expressions::PhysicalSortExpr,metrics::{ExecutionPlanMetricsSet, MetricsSet},- ColumnStatistics, DisplayFormatType, ExecutionPlan, Partitioning, RecordBatchStream,- SendableRecordBatchStream, Statistics,+ ColumnStatistics, DisplayAs, DisplayFormatType, ExecutionPlan, Partitioning,+ RecordBatchStream, SendableRecordBatchStream, Statistics,};use crate::common::get_meet_of_orderings;+use crate::metrics::BaselineMetrics;use crate::stream::ObservedStream;-use crate::{expressions, metrics::BaselineMetrics};-use datafusion_common::Result;++use arrow::datatypes::{Field, Schema, SchemaRef};+use arrow::record_batch::RecordBatch;+use datafusion_common::stats::Precision;+use datafusion_common::{exec_err, internal_err, DFSchemaRef, DataFusionError, Result};use datafusion_execution::TaskContext;++use futures::Stream;+use itertools::Itertools;+use log::{debug, trace, warn};use tokio::macros::support::thread_rng_n;/// `UnionExec`: `UNION ALL` execution plan.@@ -264,12 +262,17 @@ impl ExecutionPlan for UnionExec {Some(self.metrics.clone_inner())}- fn statistics(&self) -> Statistics {- self.inputs+ fn statistics(&self) -> Result<Statistics> {+ let stats = self+ .inputs.iter()- .map(|ep| ep.statistics())+ .map(|stat| stat.statistics())+ .collect::<Result<Vec<_>>>()?;++ Ok(stats+ .into_iter().reduce(stats_union)- .unwrap_or_default()+ .unwrap_or_else(|| Statistics::new_unknown(&self.schema())))}fn benefits_from_input_partitioning(&self) -> Vec<bool> {@@ -438,12 +441,17 @@ impl ExecutionPlan for InterleaveExec {Some(self.metrics.clone_inner())}- fn statistics(&self) -> Statistics {- self.inputs+ fn statistics(&self) -> Result<Statistics> {+ let stats = self+ .inputs.iter()- .map(|ep| ep.statistics())+ .map(|stat| stat.statistics())+ .collect::<Result<Vec<_>>>()?;++ Ok(stats+ .into_iter().reduce(stats_union)- .unwrap_or_default()+ .unwrap_or_else(|| Statistics::new_unknown(&self.schema())))}fn benefits_from_input_partitioning(&self) -> Vec<bool> {@@ -564,47 +572,32 @@ fn col_stats_union(mut left: ColumnStatistics,right: ColumnStatistics,) -> ColumnStatistics {- left.distinct_count = None;- left.min_value = left- .min_value- .zip(right.min_value)- .map(|(a, b)| expressions::helpers::min(&a, &b))- .and_then(Result::ok);- left.max_value = left- .max_value- .zip(right.max_value)- .map(|(a, b)| expressions::helpers::max(&a, &b))- .and_then(Result::ok);- left.null_count = left.null_count.zip(right.null_count).map(|(a, b)| a + b);+ left.distinct_count = Precision::Absent;+ left.min_value = left.min_value.min(&right.min_value);+ left.max_value = left.max_value.max(&right.max_value);+ left.null_count = left.null_count.add(&right.null_count);left}fn stats_union(mut left: Statistics, right: Statistics) -> Statistics {- left.is_exact = left.is_exact && right.is_exact;- left.num_rows = left.num_rows.zip(right.num_rows).map(|(a, b)| a + b);- left.total_byte_size = left- .total_byte_size- .zip(right.total_byte_size)- .map(|(a, b)| a + b);- left.column_statistics =- left.column_statistics- .zip(right.column_statistics)- .map(|(a, b)| {- a.into_iter()- .zip(b)- .map(|(ca, cb)| col_stats_union(ca, cb))- .collect()- });+ left.num_rows = left.num_rows.add(&right.num_rows);+ left.total_byte_size = left.total_byte_size.add(&right.total_byte_size);+ left.column_statistics = left+ .column_statistics+ .into_iter()+ .zip(right.column_statistics)+ .map(|(a, b)| col_stats_union(a, b))+ .collect::<Vec<_>>();left}#[cfg(test)]mod tests {use super::*;+ use crate::collect;use crate::test;- use crate::collect;use arrow::record_batch::RecordBatch;use datafusion_common::ScalarValue;@@ -630,82 +623,91 @@ mod tests {#[tokio::test]async fn test_stats_union() {let left = Statistics {- is_exact: true,- num_rows: Some(5),- total_byte_size: Some(23),- column_statistics: Some(vec![+ num_rows: Precision::Exact(5),+ total_byte_size: Precision::Exact(23),+ column_statistics: vec![ColumnStatistics {- distinct_count: Some(5),- max_value: Some(ScalarValue::Int64(Some(21))),- min_value: Some(ScalarValue::Int64(Some(-4))),- null_count: Some(0),+ distinct_count: Precision::Exact(5),+ max_value: Precision::Exact(ScalarValue::Int64(Some(21))),+ min_value: Precision::Exact(ScalarValue::Int64(Some(-4))),+ null_count: Precision::Exact(0),},ColumnStatistics {- distinct_count: Some(1),- max_value: Some(ScalarValue::Utf8(Some(String::from("x")))),- min_value: Some(ScalarValue::Utf8(Some(String::from("a")))),- null_count: Some(3),+ distinct_count: Precision::Exact(1),+ max_value: Precision::Exact(ScalarValue::Utf8(Some(String::from(+ "x",+ )))),+ min_value: Precision::Exact(ScalarValue::Utf8(Some(String::from(+ "a",+ )))),+ null_count: Precision::Exact(3),},ColumnStatistics {- distinct_count: None,- max_value: Some(ScalarValue::Float32(Some(1.1))),- min_value: Some(ScalarValue::Float32(Some(0.1))),- null_count: None,+ distinct_count: Precision::Absent,+ max_value: Precision::Exact(ScalarValue::Float32(Some(1.1))),+ min_value: Precision::Exact(ScalarValue::Float32(Some(0.1))),+ null_count: Precision::Absent,},- ]),+ ],};let right = Statistics {- is_exact: true,- num_rows: Some(7),- total_byte_size: Some(29),- column_statistics: Some(vec![+ num_rows: Precision::Exact(7),+ total_byte_size: Precision::Exact(29),+ column_statistics: vec![ColumnStatistics {- distinct_count: Some(3),- max_value: Some(ScalarValue::Int64(Some(34))),- min_value: Some(ScalarValue::Int64(Some(1))),- null_count: Some(1),+ distinct_count: Precision::Exact(3),+ max_value: Precision::Exact(ScalarValue::Int64(Some(34))),+ min_value: Precision::Exact(ScalarValue::Int64(Some(1))),+ null_count: Precision::Exact(1),},ColumnStatistics {- distinct_count: None,- max_value: Some(ScalarValue::Utf8(Some(String::from("c")))),- min_value: Some(ScalarValue::Utf8(Some(String::from("b")))),- null_count: None,+ distinct_count: Precision::Absent,+ max_value: Precision::Exact(ScalarValue::Utf8(Some(String::from(+ "c",+ )))),+ min_value: Precision::Exact(ScalarValue::Utf8(Some(String::from(+ "b",+ )))),+ null_count: Precision::Absent,},ColumnStatistics {- distinct_count: None,- max_value: None,- min_value: None,- null_count: None,+ distinct_count: Precision::Absent,+ max_value: Precision::Absent,+ min_value: Precision::Absent,+ null_count: Precision::Absent,},- ]),+ ],};let result = stats_union(left, right);let expected = Statistics {- is_exact: true,- num_rows: Some(12),- total_byte_size: Some(52),- column_statistics: Some(vec![+ num_rows: Precision::Exact(12),+ total_byte_size: Precision::Exact(52),+ column_statistics: vec![ColumnStatistics {- distinct_count: None,- max_value: Some(ScalarValue::Int64(Some(34))),- min_value: Some(ScalarValue::Int64(Some(-4))),- null_count: Some(1),+ distinct_count: Precision::Absent,+ max_value: Precision::Exact(ScalarValue::Int64(Some(34))),+ min_value: Precision::Exact(ScalarValue::Int64(Some(-4))),+ null_count: Precision::Exact(1),},ColumnStatistics {- distinct_count: None,- max_value: Some(ScalarValue::Utf8(Some(String::from("x")))),- min_value: Some(ScalarValue::Utf8(Some(String::from("a")))),- null_count: None,+ distinct_count: Precision::Absent,+ max_value: Precision::Exact(ScalarValue::Utf8(Some(String::from(+ "x",+ )))),+ min_value: Precision::Exact(ScalarValue::Utf8(Some(String::from(+ "a",+ )))),+ null_count: Precision::Absent,},ColumnStatistics {- distinct_count: None,- max_value: None,- min_value: None,- null_count: None,+ distinct_count: Precision::Absent,+ max_value: Precision::Absent,+ min_value: Precision::Absent,+ null_count: Precision::Absent,},- ]),+ ],};assert_eq!(result, expected);diff --git a/datafusion/physical-plan/src/unnest.rs b/datafusion/physical-plan/src/unnest.rsindex 410ea97887..ed64735e5a 100644--- a/datafusion/physical-plan/src/unnest.rs+++ b/datafusion/physical-plan/src/unnest.rs@@ -17,6 +17,17 @@//! Defines the unnest column plan for unnesting values in a column that contains a list//! type, conceptually is like joining each row with all the values in the list column.++use std::time::Instant;+use std::{any::Any, sync::Arc};++use super::DisplayAs;+use crate::{+ expressions::Column, DisplayFormatType, Distribution, EquivalenceProperties,+ ExecutionPlan, Partitioning, PhysicalExpr, PhysicalSortExpr, RecordBatchStream,+ SendableRecordBatchStream, Statistics,+};+use arrow::array::{Array, ArrayRef, ArrowPrimitiveType, FixedSizeListArray, LargeListArray, ListArray,PrimitiveArray,@@ -27,23 +38,12 @@ use arrow::datatypes::{};use arrow::record_batch::RecordBatch;use arrow_array::{GenericListArray, OffsetSizeTrait};-use async_trait::async_trait;-use datafusion_common::UnnestOptions;-use datafusion_common::{exec_err, DataFusionError, Result};+use datafusion_common::{exec_err, DataFusionError, Result, UnnestOptions};use datafusion_execution::TaskContext;-use futures::Stream;-use futures::StreamExt;-use log::trace;-use std::time::Instant;-use std::{any::Any, sync::Arc};--use crate::{- expressions::Column, DisplayFormatType, Distribution, EquivalenceProperties,- ExecutionPlan, Partitioning, PhysicalExpr, PhysicalSortExpr, RecordBatchStream,- SendableRecordBatchStream, Statistics,-};-use super::DisplayAs;+use async_trait::async_trait;+use futures::{Stream, StreamExt};+use log::trace;/// Unnest the given column by joining the row with each value in the/// nested type.@@ -160,8 +160,8 @@ impl ExecutionPlan for UnnestExec {}))}- fn statistics(&self) -> Statistics {- Default::default()+ fn statistics(&self) -> Result<Statistics> {+ Ok(Statistics::new_unknown(&self.schema()))}}diff --git a/datafusion/physical-plan/src/values.rs b/datafusion/physical-plan/src/values.rsindex 383726401c..b624fb362e 100644--- a/datafusion/physical-plan/src/values.rs+++ b/datafusion/physical-plan/src/values.rs@@ -17,20 +17,21 @@//! Values execution plan+use std::any::Any;+use std::sync::Arc;+use super::expressions::PhysicalSortExpr;use super::{common, DisplayAs, SendableRecordBatchStream, Statistics};use crate::{memory::MemoryStream, ColumnarValue, DisplayFormatType, ExecutionPlan, Partitioning,PhysicalExpr,};+use arrow::array::new_null_array;use arrow::datatypes::SchemaRef;use arrow::record_batch::RecordBatch;-use datafusion_common::{internal_err, plan_err, ScalarValue};-use datafusion_common::{DataFusionError, Result};+use datafusion_common::{internal_err, plan_err, DataFusionError, Result, ScalarValue};use datafusion_execution::TaskContext;-use std::any::Any;-use std::sync::Arc;/// Execution plan for values list based relation (produces constant rows)#[derive(Debug)]@@ -187,9 +188,13 @@ impl ExecutionPlan for ValuesExec {)?))}- fn statistics(&self) -> Statistics {+ fn statistics(&self) -> Result<Statistics> {let batch = self.data();- common::compute_record_batch_statistics(&[batch], &self.schema, None)+ Ok(common::compute_record_batch_statistics(+ &[batch],+ &self.schema,+ None,+ ))}}@@ -197,6 +202,7 @@ impl ExecutionPlan for ValuesExec {mod tests {use super::*;use crate::test::{self, make_partition};+use arrow_schema::{DataType, Field, Schema};#[tokio::test]diff --git a/datafusion/physical-plan/src/windows/bounded_window_agg_exec.rs b/datafusion/physical-plan/src/windows/bounded_window_agg_exec.rsindex 800ea42b35..eab47886c7 100644--- a/datafusion/physical-plan/src/windows/bounded_window_agg_exec.rs+++ b/datafusion/physical-plan/src/windows/bounded_window_agg_exec.rs@@ -43,8 +43,8 @@ use arrow::{datatypes::{Schema, SchemaBuilder, SchemaRef},record_batch::RecordBatch,};-use datafusion_common::hash_utils::create_hashes;+use datafusion_common::stats::Precision;use datafusion_common::utils::{evaluate_partition_ranges, get_arrayref_at_indices, get_at_indices,get_record_batch_at_indices, get_row_at_idx,@@ -320,24 +320,22 @@ impl ExecutionPlan for BoundedWindowAggExec {Some(self.metrics.clone_inner())}- fn statistics(&self) -> Statistics {- let input_stat = self.input.statistics();+ fn statistics(&self) -> Result<Statistics> {+ let input_stat = self.input.statistics()?;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...let mut column_statistics = Vec::with_capacity(win_cols + input_cols);- if let Some(input_col_stats) = input_stat.column_statistics {- column_statistics.extend(input_col_stats);- } else {- column_statistics.extend(vec![ColumnStatistics::default(); input_cols]);+ // copy stats of the input to the beginning of the schema.+ column_statistics.extend(input_stat.column_statistics);+ for _ in 0..win_cols {+ column_statistics.push(ColumnStatistics::new_unknown())}- column_statistics.extend(vec![ColumnStatistics::default(); win_cols]);- Statistics {- is_exact: input_stat.is_exact,+ Ok(Statistics {num_rows: input_stat.num_rows,- column_statistics: Some(column_statistics),- total_byte_size: None,- }+ column_statistics,+ total_byte_size: Precision::Absent,+ })}}diff --git a/datafusion/physical-plan/src/windows/window_agg_exec.rs b/datafusion/physical-plan/src/windows/window_agg_exec.rsindex b4dc8ec88c..4873778425 100644--- a/datafusion/physical-plan/src/windows/window_agg_exec.rs+++ b/datafusion/physical-plan/src/windows/window_agg_exec.rs@@ -42,6 +42,7 @@ use arrow::{datatypes::{Schema, SchemaRef},record_batch::RecordBatch,};+use datafusion_common::stats::Precision;use datafusion_common::utils::{evaluate_partition_ranges, get_at_indices};use datafusion_common::{internal_err, plan_err, DataFusionError, Result};use datafusion_execution::TaskContext;@@ -246,24 +247,22 @@ impl ExecutionPlan for WindowAggExec {Some(self.metrics.clone_inner())}- fn statistics(&self) -> Statistics {- let input_stat = self.input.statistics();+ fn statistics(&self) -> Result<Statistics> {+ let input_stat = self.input.statistics()?;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...let mut column_statistics = Vec::with_capacity(win_cols + input_cols);- if let Some(input_col_stats) = input_stat.column_statistics {- column_statistics.extend(input_col_stats);- } else {- column_statistics.extend(vec![ColumnStatistics::default(); input_cols]);+ // copy stats of the input to the beginning of the schema.+ column_statistics.extend(input_stat.column_statistics);+ for _ in 0..win_cols {+ column_statistics.push(ColumnStatistics::new_unknown())}- column_statistics.extend(vec![ColumnStatistics::default(); win_cols]);- Statistics {- is_exact: input_stat.is_exact,+ Ok(Statistics {num_rows: input_stat.num_rows,- column_statistics: Some(column_statistics),- total_byte_size: None,- }+ column_statistics,+ total_byte_size: Precision::Absent,+ })}}diff --git a/datafusion/proto/proto/datafusion.proto b/datafusion/proto/proto/datafusion.protoindex 1819d1a439..f4ab582593 100644--- a/datafusion/proto/proto/datafusion.proto+++ b/datafusion/proto/proto/datafusion.proto@@ -1542,18 +1542,28 @@ message PartitionStats {repeated ColumnStats column_stats = 4;}+message Precision{+ PrecisionInfo precision_info = 1;+ ScalarValue val = 2;+}++enum PrecisionInfo {+ EXACT = 0;+ INEXACT = 1;+ ABSENT = 2;+}+message Statistics {- int64 num_rows = 1;- int64 total_byte_size = 2;+ Precision num_rows = 1;+ Precision total_byte_size = 2;repeated ColumnStats column_stats = 3;- bool is_exact = 4;}message ColumnStats {- ScalarValue min_value = 1;- ScalarValue max_value = 2;- uint32 null_count = 3;- uint32 distinct_count = 4;+ Precision min_value = 1;+ Precision max_value = 2;+ Precision null_count = 3;+ Precision distinct_count = 4;}message NamedStructFieldExpr {diff --git a/datafusion/proto/src/generated/pbjson.rs b/datafusion/proto/src/generated/pbjson.rsindex 9aed987491..e9e2fd0c04 100644--- a/datafusion/proto/src/generated/pbjson.rs+++ b/datafusion/proto/src/generated/pbjson.rs@@ -3289,10 +3289,10 @@ impl serde::Serialize for ColumnStats {if self.max_value.is_some() {len += 1;}- if self.null_count != 0 {+ if self.null_count.is_some() {len += 1;}- if self.distinct_count != 0 {+ if self.distinct_count.is_some() {len += 1;}let mut struct_ser = serializer.serialize_struct("datafusion.ColumnStats", len)?;@@ -3302,11 +3302,11 @@ impl serde::Serialize for ColumnStats {if let Some(v) = self.max_value.as_ref() {struct_ser.serialize_field("maxValue", v)?;}- if self.null_count != 0 {- struct_ser.serialize_field("nullCount", &self.null_count)?;+ if let Some(v) = self.null_count.as_ref() {+ struct_ser.serialize_field("nullCount", v)?;}- if self.distinct_count != 0 {- struct_ser.serialize_field("distinctCount", &self.distinct_count)?;+ if let Some(v) = self.distinct_count.as_ref() {+ struct_ser.serialize_field("distinctCount", v)?;}struct_ser.end()}@@ -3400,25 +3400,21 @@ impl<'de> serde::Deserialize<'de> for ColumnStats {if null_count__.is_some() {return Err(serde::de::Error::duplicate_field("nullCount"));}- null_count__ =- Some(map_.next_value::<::pbjson::private::NumberDeserialize<_>>()?.0)- ;+ null_count__ = map_.next_value()?;}GeneratedField::DistinctCount => {if distinct_count__.is_some() {return Err(serde::de::Error::duplicate_field("distinctCount"));}- distinct_count__ =- Some(map_.next_value::<::pbjson::private::NumberDeserialize<_>>()?.0)- ;+ distinct_count__ = map_.next_value()?;}}}Ok(ColumnStats {min_value: min_value__,max_value: max_value__,- null_count: null_count__.unwrap_or_default(),- distinct_count: distinct_count__.unwrap_or_default(),+ null_count: null_count__,+ distinct_count: distinct_count__,})}}@@ -18320,6 +18316,191 @@ impl<'de> serde::Deserialize<'de> for PlanType {deserializer.deserialize_struct("datafusion.PlanType", FIELDS, GeneratedVisitor)}}+impl serde::Serialize for Precision {+ #[allow(deprecated)]+ fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>+ where+ S: serde::Serializer,+ {+ use serde::ser::SerializeStruct;+ let mut len = 0;+ if self.precision_info != 0 {+ len += 1;+ }+ if self.val.is_some() {+ len += 1;+ }+ let mut struct_ser = serializer.serialize_struct("datafusion.Precision", len)?;+ if self.precision_info != 0 {+ let v = PrecisionInfo::try_from(self.precision_info)+ .map_err(|_| serde::ser::Error::custom(format!("Invalid variant {}", self.precision_info)))?;+ struct_ser.serialize_field("precisionInfo", &v)?;+ }+ if let Some(v) = self.val.as_ref() {+ struct_ser.serialize_field("val", v)?;+ }+ struct_ser.end()+ }+}+impl<'de> serde::Deserialize<'de> for Precision {+ #[allow(deprecated)]+ fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>+ where+ D: serde::Deserializer<'de>,+ {+ const FIELDS: &[&str] = &[+ "precision_info",+ "precisionInfo",+ "val",+ ];++ #[allow(clippy::enum_variant_names)]+ enum GeneratedField {+ PrecisionInfo,+ Val,+ }+ impl<'de> serde::Deserialize<'de> for GeneratedField {+ fn deserialize<D>(deserializer: D) -> std::result::Result<GeneratedField, D::Error>+ where+ D: serde::Deserializer<'de>,+ {+ struct GeneratedVisitor;++ impl<'de> serde::de::Visitor<'de> for GeneratedVisitor {+ type Value = GeneratedField;++ fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ write!(formatter, "expected one of: {:?}", &FIELDS)+ }++ #[allow(unused_variables)]+ fn visit_str<E>(self, value: &str) -> std::result::Result<GeneratedField, E>+ where+ E: serde::de::Error,+ {+ match value {+ "precisionInfo" | "precision_info" => Ok(GeneratedField::PrecisionInfo),+ "val" => Ok(GeneratedField::Val),+ _ => Err(serde::de::Error::unknown_field(value, FIELDS)),+ }+ }+ }+ deserializer.deserialize_identifier(GeneratedVisitor)+ }+ }+ struct GeneratedVisitor;+ impl<'de> serde::de::Visitor<'de> for GeneratedVisitor {+ type Value = Precision;++ fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ formatter.write_str("struct datafusion.Precision")+ }++ fn visit_map<V>(self, mut map_: V) -> std::result::Result<Precision, V::Error>+ where+ V: serde::de::MapAccess<'de>,+ {+ let mut precision_info__ = None;+ let mut val__ = None;+ while let Some(k) = map_.next_key()? {+ match k {+ GeneratedField::PrecisionInfo => {+ if precision_info__.is_some() {+ return Err(serde::de::Error::duplicate_field("precisionInfo"));+ }+ precision_info__ = Some(map_.next_value::<PrecisionInfo>()? as i32);+ }+ GeneratedField::Val => {+ if val__.is_some() {+ return Err(serde::de::Error::duplicate_field("val"));+ }+ val__ = map_.next_value()?;+ }+ }+ }+ Ok(Precision {+ precision_info: precision_info__.unwrap_or_default(),+ val: val__,+ })+ }+ }+ deserializer.deserialize_struct("datafusion.Precision", FIELDS, GeneratedVisitor)+ }+}+impl serde::Serialize for PrecisionInfo {+ #[allow(deprecated)]+ fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>+ where+ S: serde::Serializer,+ {+ let variant = match self {+ Self::Exact => "EXACT",+ Self::Inexact => "INEXACT",+ Self::Absent => "ABSENT",+ };+ serializer.serialize_str(variant)+ }+}+impl<'de> serde::Deserialize<'de> for PrecisionInfo {+ #[allow(deprecated)]+ fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>+ where+ D: serde::Deserializer<'de>,+ {+ const FIELDS: &[&str] = &[+ "EXACT",+ "INEXACT",+ "ABSENT",+ ];++ struct GeneratedVisitor;++ impl<'de> serde::de::Visitor<'de> for GeneratedVisitor {+ type Value = PrecisionInfo;++ fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {+ write!(formatter, "expected one of: {:?}", &FIELDS)+ }++ fn visit_i64<E>(self, v: i64) -> std::result::Result<Self::Value, E>+ where+ E: serde::de::Error,+ {+ i32::try_from(v)+ .ok()+ .and_then(|x| x.try_into().ok())+ .ok_or_else(|| {+ serde::de::Error::invalid_value(serde::de::Unexpected::Signed(v), &self)+ })+ }++ fn visit_u64<E>(self, v: u64) -> std::result::Result<Self::Value, E>+ where+ E: serde::de::Error,+ {+ i32::try_from(v)+ .ok()+ .and_then(|x| x.try_into().ok())+ .ok_or_else(|| {+ serde::de::Error::invalid_value(serde::de::Unexpected::Unsigned(v), &self)+ })+ }++ fn visit_str<E>(self, value: &str) -> std::result::Result<Self::Value, E>+ where+ E: serde::de::Error,+ {+ match value {+ "EXACT" => Ok(PrecisionInfo::Exact),+ "INEXACT" => Ok(PrecisionInfo::Inexact),+ "ABSENT" => Ok(PrecisionInfo::Absent),+ _ => Err(serde::de::Error::unknown_variant(value, FIELDS)),+ }+ }+ }+ deserializer.deserialize_any(GeneratedVisitor)+ }+}impl serde::Serialize for PrepareNode {#[allow(deprecated)]fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>@@ -22240,33 +22421,25 @@ impl serde::Serialize for Statistics {{use serde::ser::SerializeStruct;let mut len = 0;- if self.num_rows != 0 {+ if self.num_rows.is_some() {len += 1;}- if self.total_byte_size != 0 {+ if self.total_byte_size.is_some() {len += 1;}if !self.column_stats.is_empty() {len += 1;}- if self.is_exact {- len += 1;- }let mut struct_ser = serializer.serialize_struct("datafusion.Statistics", len)?;- if self.num_rows != 0 {- #[allow(clippy::needless_borrow)]- struct_ser.serialize_field("numRows", ToString::to_string(&self.num_rows).as_str())?;+ if let Some(v) = self.num_rows.as_ref() {+ struct_ser.serialize_field("numRows", v)?;}- if self.total_byte_size != 0 {- #[allow(clippy::needless_borrow)]- struct_ser.serialize_field("totalByteSize", ToString::to_string(&self.total_byte_size).as_str())?;+ if let Some(v) = self.total_byte_size.as_ref() {+ struct_ser.serialize_field("totalByteSize", v)?;}if !self.column_stats.is_empty() {struct_ser.serialize_field("columnStats", &self.column_stats)?;}- if self.is_exact {- struct_ser.serialize_field("isExact", &self.is_exact)?;- }struct_ser.end()}}@@ -22283,8 +22456,6 @@ impl<'de> serde::Deserialize<'de> for Statistics {"totalByteSize","column_stats","columnStats",- "is_exact",- "isExact",];#[allow(clippy::enum_variant_names)]@@ -22292,7 +22463,6 @@ impl<'de> serde::Deserialize<'de> for Statistics {NumRows,TotalByteSize,ColumnStats,- IsExact,}impl<'de> serde::Deserialize<'de> for GeneratedField {fn deserialize<D>(deserializer: D) -> std::result::Result<GeneratedField, D::Error>@@ -22317,7 +22487,6 @@ impl<'de> serde::Deserialize<'de> for Statistics {"numRows" | "num_rows" => Ok(GeneratedField::NumRows),"totalByteSize" | "total_byte_size" => Ok(GeneratedField::TotalByteSize),"columnStats" | "column_stats" => Ok(GeneratedField::ColumnStats),- "isExact" | "is_exact" => Ok(GeneratedField::IsExact),_ => Err(serde::de::Error::unknown_field(value, FIELDS)),}}@@ -22340,24 +22509,19 @@ impl<'de> serde::Deserialize<'de> for Statistics {let mut num_rows__ = None;let mut total_byte_size__ = None;let mut column_stats__ = None;- let mut is_exact__ = None;while let Some(k) = map_.next_key()? {match k {GeneratedField::NumRows => {if num_rows__.is_some() {return Err(serde::de::Error::duplicate_field("numRows"));}- num_rows__ =- Some(map_.next_value::<::pbjson::private::NumberDeserialize<_>>()?.0)- ;+ num_rows__ = map_.next_value()?;}GeneratedField::TotalByteSize => {if total_byte_size__.is_some() {return Err(serde::de::Error::duplicate_field("totalByteSize"));}- total_byte_size__ =- Some(map_.next_value::<::pbjson::private::NumberDeserialize<_>>()?.0)- ;+ total_byte_size__ = map_.next_value()?;}GeneratedField::ColumnStats => {if column_stats__.is_some() {@@ -22365,19 +22529,12 @@ impl<'de> serde::Deserialize<'de> for Statistics {}column_stats__ = Some(map_.next_value()?);}- GeneratedField::IsExact => {- if is_exact__.is_some() {- return Err(serde::de::Error::duplicate_field("isExact"));- }- is_exact__ = Some(map_.next_value()?);- }}}Ok(Statistics {- num_rows: num_rows__.unwrap_or_default(),- total_byte_size: total_byte_size__.unwrap_or_default(),+ num_rows: num_rows__,+ total_byte_size: total_byte_size__,column_stats: column_stats__.unwrap_or_default(),- is_exact: is_exact__.unwrap_or_default(),})}}diff --git a/datafusion/proto/src/generated/prost.rs b/datafusion/proto/src/generated/prost.rsindex 883799b159..1c821708a9 100644--- a/datafusion/proto/src/generated/prost.rs+++ b/datafusion/proto/src/generated/prost.rs@@ -2205,27 +2205,33 @@ pub struct PartitionStats {}#[allow(clippy::derive_partial_eq_without_eq)]#[derive(Clone, PartialEq, ::prost::Message)]+pub struct Precision {+ #[prost(enumeration = "PrecisionInfo", tag = "1")]+ pub precision_info: i32,+ #[prost(message, optional, tag = "2")]+ pub val: ::core::option::Option<ScalarValue>,+}+#[allow(clippy::derive_partial_eq_without_eq)]+#[derive(Clone, PartialEq, ::prost::Message)]pub struct Statistics {- #[prost(int64, tag = "1")]- pub num_rows: i64,- #[prost(int64, tag = "2")]- pub total_byte_size: i64,+ #[prost(message, optional, tag = "1")]+ pub num_rows: ::core::option::Option<Precision>,+ #[prost(message, optional, tag = "2")]+ pub total_byte_size: ::core::option::Option<Precision>,#[prost(message, repeated, tag = "3")]pub column_stats: ::prost::alloc::vec::Vec<ColumnStats>,- #[prost(bool, tag = "4")]- pub is_exact: bool,}#[allow(clippy::derive_partial_eq_without_eq)]#[derive(Clone, PartialEq, ::prost::Message)]pub struct ColumnStats {#[prost(message, optional, tag = "1")]- pub min_value: ::core::option::Option<ScalarValue>,+ pub min_value: ::core::option::Option<Precision>,#[prost(message, optional, tag = "2")]- pub max_value: ::core::option::Option<ScalarValue>,- #[prost(uint32, tag = "3")]- pub null_count: u32,- #[prost(uint32, tag = "4")]- pub distinct_count: u32,+ pub max_value: ::core::option::Option<Precision>,+ #[prost(message, optional, tag = "3")]+ pub null_count: ::core::option::Option<Precision>,+ #[prost(message, optional, tag = "4")]+ pub distinct_count: ::core::option::Option<Precision>,}#[allow(clippy::derive_partial_eq_without_eq)]#[derive(Clone, PartialEq, ::prost::Message)]@@ -3157,3 +3163,32 @@ impl JoinSide {}}}+#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, ::prost::Enumeration)]+#[repr(i32)]+pub enum PrecisionInfo {+ Exact = 0,+ Inexact = 1,+ Absent = 2,+}+impl PrecisionInfo {+ /// String value of the enum field names used in the ProtoBuf definition.+ ///+ /// The values are not transformed in any way and thus are considered stable+ /// (if the ProtoBuf definition does not change) and safe for programmatic use.+ pub fn as_str_name(&self) -> &'static str {+ match self {+ PrecisionInfo::Exact => "EXACT",+ PrecisionInfo::Inexact => "INEXACT",+ PrecisionInfo::Absent => "ABSENT",+ }+ }+ /// Creates an enum from field names used in the ProtoBuf definition.+ pub fn from_str_name(value: &str) -> ::core::option::Option<Self> {+ match value {+ "EXACT" => Some(Self::Exact),+ "INEXACT" => Some(Self::Inexact),+ "ABSENT" => Some(Self::Absent),+ _ => None,+ }+ }+}diff --git a/datafusion/proto/src/physical_plan/from_proto.rs b/datafusion/proto/src/physical_plan/from_proto.rsindex bb38116e5d..ff02b80521 100644--- a/datafusion/proto/src/physical_plan/from_proto.rs+++ b/datafusion/proto/src/physical_plan/from_proto.rs@@ -17,10 +17,12 @@//! Serde code to convert from protocol buffers to Rust data structures.-use crate::protobuf;+use std::convert::{TryFrom, TryInto};+use std::ops::Deref;+use std::sync::Arc;++use arrow::compute::SortOptions;use arrow::datatypes::DataType;-use chrono::TimeZone;-use chrono::Utc;use datafusion::arrow::datatypes::Schema;use datafusion::datasource::listing::{FileRange, PartitionedFile};use datafusion::datasource::object_store::ObjectStoreUrl;@@ -29,31 +31,28 @@ use datafusion::execution::context::ExecutionProps;use datafusion::execution::FunctionRegistry;use datafusion::logical_expr::window_function::WindowFunction;use datafusion::physical_expr::{PhysicalSortExpr, ScalarFunctionExpr};-use datafusion::physical_plan::expressions::{in_list, LikeExpr};+use datafusion::physical_plan::expressions::{+ in_list, BinaryExpr, CaseExpr, CastExpr, Column, IsNotNullExpr, IsNullExpr, LikeExpr,+ Literal, NegativeExpr, NotExpr, TryCastExpr,+};use datafusion::physical_plan::expressions::{GetFieldAccessExpr, GetIndexedFieldExpr};+use datafusion::physical_plan::joins::utils::JoinSide;use datafusion::physical_plan::windows::create_window_expr;-use datafusion::physical_plan::WindowExpr;use datafusion::physical_plan::{- expressions::{- BinaryExpr, CaseExpr, CastExpr, Column, IsNotNullExpr, IsNullExpr, Literal,- NegativeExpr, NotExpr, TryCastExpr,- },- functions, Partitioning,+ functions, ColumnStatistics, Partitioning, PhysicalExpr, Statistics, WindowExpr,};-use datafusion::physical_plan::{ColumnStatistics, PhysicalExpr, Statistics};-use datafusion_common::{not_impl_err, DataFusionError, Result};-use object_store::path::Path;-use object_store::ObjectMeta;-use std::convert::{TryFrom, TryInto};-use std::ops::Deref;-use std::sync::Arc;+use datafusion_common::stats::Precision;+use datafusion_common::{not_impl_err, DataFusionError, Result, ScalarValue};use crate::common::proto_error;use crate::convert_required;use crate::logical_plan;+use crate::protobuf;use crate::protobuf::physical_expr_node::ExprType;-use datafusion::physical_plan::joins::utils::JoinSide;-use datafusion::physical_plan::sorts::sort::SortOptions;++use chrono::{TimeZone, Utc};+use object_store::path::Path;+use object_store::ObjectMeta;impl From<&protobuf::PhysicalColumn> for Column {fn from(c: &protobuf::PhysicalColumn) -> Column {@@ -581,10 +580,96 @@ impl TryFrom<&protobuf::FileGroup> for Vec<PartitionedFile> {impl From<&protobuf::ColumnStats> for ColumnStatistics {fn from(cs: &protobuf::ColumnStats) -> ColumnStatistics {ColumnStatistics {- null_count: Some(cs.null_count as usize),- max_value: cs.max_value.as_ref().map(|m| m.try_into().unwrap()),- min_value: cs.min_value.as_ref().map(|m| m.try_into().unwrap()),- distinct_count: Some(cs.distinct_count as usize),+ null_count: if let Some(nc) = &cs.null_count {+ nc.clone().into()+ } else {+ Precision::Absent+ },+ max_value: if let Some(max) = &cs.max_value {+ max.clone().into()+ } else {+ Precision::Absent+ },+ min_value: if let Some(min) = &cs.min_value {+ min.clone().into()+ } else {+ Precision::Absent+ },+ distinct_count: if let Some(dc) = &cs.distinct_count {+ dc.clone().into()+ } else {+ Precision::Absent+ },+ }+ }+}++impl From<protobuf::Precision> for Precision<usize> {+ fn from(s: protobuf::Precision) -> Self {+ let Ok(precision_type) = s.precision_info.try_into() else {+ return Precision::Absent;+ };+ match precision_type {+ protobuf::PrecisionInfo::Exact => {+ if let Some(val) = s.val {+ if let Ok(ScalarValue::UInt64(Some(val))) =+ ScalarValue::try_from(&val)+ {+ Precision::Exact(val as usize)+ } else {+ Precision::Absent+ }+ } else {+ Precision::Absent+ }+ }+ protobuf::PrecisionInfo::Inexact => {+ if let Some(val) = s.val {+ if let Ok(ScalarValue::UInt64(Some(val))) =+ ScalarValue::try_from(&val)+ {+ Precision::Inexact(val as usize)+ } else {+ Precision::Absent+ }+ } else {+ Precision::Absent+ }+ }+ protobuf::PrecisionInfo::Absent => Precision::Absent,+ }+ }+}++impl From<protobuf::Precision> for Precision<ScalarValue> {+ fn from(s: protobuf::Precision) -> Self {+ let Ok(precision_type) = s.precision_info.try_into() else {+ return Precision::Absent;+ };+ match precision_type {+ protobuf::PrecisionInfo::Exact => {+ if let Some(val) = s.val {+ if let Ok(val) = ScalarValue::try_from(&val) {+ Precision::Exact(val)+ } else {+ Precision::Absent+ }+ } else {+ Precision::Absent+ }+ }+ protobuf::PrecisionInfo::Inexact => {+ if let Some(val) = s.val {+ if let Ok(val) = ScalarValue::try_from(&val) {+ Precision::Inexact(val)+ } else {+ Precision::Absent+ }+ } else {+ Precision::Absent+ }+ }+ protobuf::PrecisionInfo::Absent => Precision::Absent,}}}@@ -603,27 +688,19 @@ impl TryFrom<&protobuf::Statistics> for Statistics {fn try_from(s: &protobuf::Statistics) -> Result<Self, Self::Error> {// Keep it sync with Statistics::to_proto- let none_value = -1_i64;- let column_statistics =- s.column_stats.iter().map(|s| s.into()).collect::<Vec<_>>();Ok(Statistics {- num_rows: if s.num_rows == none_value {- None+ num_rows: if let Some(nr) = &s.num_rows {+ nr.clone().into()} else {- Some(s.num_rows as usize)+ Precision::Absent},- total_byte_size: if s.total_byte_size == none_value {- None+ total_byte_size: if let Some(tbs) = &s.total_byte_size {+ tbs.clone().into()} else {- Some(s.total_byte_size as usize)+ Precision::Absent},// No column statistic (None) is encoded with empty array- column_statistics: if column_statistics.is_empty() {- None- } else {- Some(column_statistics)- },- is_exact: s.is_exact,+ column_statistics: s.column_stats.iter().map(|s| s.into()).collect(),})}}diff --git a/datafusion/proto/src/physical_plan/to_proto.rs b/datafusion/proto/src/physical_plan/to_proto.rsindex cf3dbe2619..db97c39325 100644--- a/datafusion/proto/src/physical_plan/to_proto.rs+++ b/datafusion/proto/src/physical_plan/to_proto.rs@@ -22,58 +22,37 @@ use std::{sync::Arc,};-use datafusion::physical_plan::{- expressions::{- ApproxDistinct, ApproxMedian, ApproxPercentileCont,- ApproxPercentileContWithWeight, ArrayAgg, Correlation, Covariance, CovariancePop,- DistinctArrayAgg, DistinctBitXor, DistinctSum, FirstValue, Grouping, LastValue,- Median, OrderSensitiveArrayAgg, Regr, RegrType, Stddev, StddevPop, Variance,- VariancePop,- },- windows::BuiltInWindowExpr,- ColumnStatistics,-};-use datafusion::{- physical_expr::window::NthValueKind,- physical_plan::{- expressions::{- CaseExpr, CumeDist, InListExpr, IsNotNullExpr, IsNullExpr, NegativeExpr,- NotExpr, NthValue, Ntile, Rank, RankType, RowNumber, WindowShift,- },- Statistics,- },-};-use datafusion::{- physical_expr::window::SlidingAggregateWindowExpr,- physical_plan::{- expressions::{CastExpr, TryCastExpr},- windows::PlainAggregateWindowExpr,- WindowExpr,- },-};--use datafusion::datasource::listing::{FileRange, PartitionedFile};-use datafusion::datasource::physical_plan::FileScanConfig;--use datafusion::physical_plan::expressions::{Count, DistinctCount, Literal};--use datafusion::physical_plan::expressions::{- Avg, BinaryExpr, BitAnd, BitOr, BitXor, BoolAnd, BoolOr, Column, LikeExpr, Max, Min,- Sum,-};-use datafusion::physical_plan::{AggregateExpr, PhysicalExpr};--use crate::protobuf::{self, physical_window_expr_node};+use crate::protobuf::{self, physical_window_expr_node, scalar_value::Value};use crate::protobuf::{physical_aggregate_expr_node, PhysicalSortExprNode, PhysicalSortExprNodeCollection,ScalarValue,};++use datafusion::datasource::listing::{FileRange, PartitionedFile};+use datafusion::datasource::physical_plan::FileScanConfig;use datafusion::logical_expr::BuiltinScalarFunction;use datafusion::physical_expr::expressions::{GetFieldAccessExpr, GetIndexedFieldExpr};+use datafusion::physical_expr::window::{NthValueKind, SlidingAggregateWindowExpr};use datafusion::physical_expr::{PhysicalSortExpr, ScalarFunctionExpr};+use datafusion::physical_plan::expressions::{+ ApproxDistinct, ApproxMedian, ApproxPercentileCont, ApproxPercentileContWithWeight,+ ArrayAgg, Avg, BinaryExpr, BitAnd, BitOr, BitXor, BoolAnd, BoolOr, CaseExpr,+ CastExpr, Column, Correlation, Count, Covariance, CovariancePop, CumeDist,+ DistinctArrayAgg, DistinctBitXor, DistinctCount, DistinctSum, FirstValue, Grouping,+ InListExpr, IsNotNullExpr, IsNullExpr, LastValue, LikeExpr, Literal, Max, Median,+ Min, NegativeExpr, NotExpr, NthValue, Ntile, OrderSensitiveArrayAgg, Rank, RankType,+ Regr, RegrType, RowNumber, Stddev, StddevPop, Sum, TryCastExpr, Variance,+ VariancePop, WindowShift,+};use datafusion::physical_plan::joins::utils::JoinSide;use datafusion::physical_plan::udaf::AggregateFunctionExpr;-use datafusion_common::{internal_err, not_impl_err, DataFusionError, Result};+use datafusion::physical_plan::windows::{BuiltInWindowExpr, PlainAggregateWindowExpr};+use datafusion::physical_plan::{+ AggregateExpr, ColumnStatistics, PhysicalExpr, Statistics, WindowExpr,+};+use datafusion_common::{+ internal_err, not_impl_err, stats::Precision, DataFusionError, Result,+};impl TryFrom<Arc<dyn AggregateExpr>> for protobuf::PhysicalExprNode {type Error = DataFusionError;@@ -644,29 +623,66 @@ impl TryFrom<&[PartitionedFile]> for protobuf::FileGroup {}}-impl From<&ColumnStatistics> for protobuf::ColumnStats {- fn from(cs: &ColumnStatistics) -> protobuf::ColumnStats {- protobuf::ColumnStats {- min_value: cs.min_value.as_ref().map(|m| m.try_into().unwrap()),- max_value: cs.max_value.as_ref().map(|m| m.try_into().unwrap()),- null_count: cs.null_count.map(|n| n as u32).unwrap_or(0),- distinct_count: cs.distinct_count.map(|n| n as u32).unwrap_or(0),+impl From<&Precision<usize>> for protobuf::Precision {+ fn from(s: &Precision<usize>) -> protobuf::Precision {+ match s {+ Precision::Exact(val) => protobuf::Precision {+ precision_info: protobuf::PrecisionInfo::Exact.into(),+ val: Some(ScalarValue {+ value: Some(Value::Uint64Value(*val as u64)),+ }),+ },+ Precision::Inexact(val) => protobuf::Precision {+ precision_info: protobuf::PrecisionInfo::Inexact.into(),+ val: Some(ScalarValue {+ value: Some(Value::Uint64Value(*val as u64)),+ }),+ },+ Precision::Absent => protobuf::Precision {+ precision_info: protobuf::PrecisionInfo::Absent.into(),+ val: Some(ScalarValue { value: None }),+ },+ }+ }+}++impl From<&Precision<datafusion_common::ScalarValue>> for protobuf::Precision {+ fn from(s: &Precision<datafusion_common::ScalarValue>) -> protobuf::Precision {+ match s {+ Precision::Exact(val) => protobuf::Precision {+ precision_info: protobuf::PrecisionInfo::Exact.into(),+ val: val.try_into().ok(),+ },+ Precision::Inexact(val) => protobuf::Precision {+ precision_info: protobuf::PrecisionInfo::Inexact.into(),+ val: val.try_into().ok(),+ },+ Precision::Absent => protobuf::Precision {+ precision_info: protobuf::PrecisionInfo::Absent.into(),+ val: Some(ScalarValue { value: None }),+ },}}}impl From<&Statistics> for protobuf::Statistics {fn from(s: &Statistics) -> protobuf::Statistics {- let none_value = -1_i64;- let column_stats = match &s.column_statistics {- None => vec![],- Some(column_stats) => column_stats.iter().map(|s| s.into()).collect(),- };+ let column_stats = s.column_statistics.iter().map(|s| s.into()).collect();protobuf::Statistics {- num_rows: s.num_rows.map(|n| n as i64).unwrap_or(none_value),- total_byte_size: s.total_byte_size.map(|n| n as i64).unwrap_or(none_value),+ num_rows: Some(protobuf::Precision::from(&s.num_rows)),+ total_byte_size: Some(protobuf::Precision::from(&s.total_byte_size)),column_stats,- is_exact: s.is_exact,+ }+ }+}++impl From<&ColumnStatistics> for protobuf::ColumnStats {+ fn from(s: &ColumnStatistics) -> protobuf::ColumnStats {+ protobuf::ColumnStats {+ min_value: Some(protobuf::Precision::from(&s.min_value)),+ max_value: Some(protobuf::Precision::from(&s.max_value)),+ null_count: Some(protobuf::Precision::from(&s.null_count)),+ distinct_count: Some(protobuf::Precision::from(&s.distinct_count)),}}}diff --git a/datafusion/proto/tests/cases/roundtrip_physical_plan.rs b/datafusion/proto/tests/cases/roundtrip_physical_plan.rsindex e30d416bdc..01a0916d8c 100644--- a/datafusion/proto/tests/cases/roundtrip_physical_plan.rs+++ b/datafusion/proto/tests/cases/roundtrip_physical_plan.rs@@ -25,20 +25,19 @@ use datafusion::datasource::listing::PartitionedFile;use datafusion::datasource::object_store::ObjectStoreUrl;use datafusion::datasource::physical_plan::{FileScanConfig, ParquetExec};use datafusion::execution::context::ExecutionProps;-use datafusion::logical_expr::create_udf;-use datafusion::logical_expr::{BuiltinScalarFunction, Volatility};-use datafusion::logical_expr::{JoinType, Operator};-use datafusion::physical_expr::expressions::GetFieldAccessExpr;-use datafusion::physical_expr::expressions::{cast, in_list};+use datafusion::logical_expr::{+ create_udf, BuiltinScalarFunction, JoinType, Operator, Volatility,+};use datafusion::physical_expr::window::SlidingAggregateWindowExpr;use datafusion::physical_expr::ScalarFunctionExpr;-use datafusion::physical_plan::aggregates::PhysicalGroupBy;-use datafusion::physical_plan::aggregates::{AggregateExec, AggregateMode};+use datafusion::physical_plan::aggregates::{+ AggregateExec, AggregateMode, PhysicalGroupBy,+};use datafusion::physical_plan::analyze::AnalyzeExec;use datafusion::physical_plan::empty::EmptyExec;use datafusion::physical_plan::expressions::{- binary, col, like, lit, Avg, BinaryExpr, Column, DistinctCount, GetIndexedFieldExpr,- NotExpr, NthValue, PhysicalSortExpr, Sum,+ binary, cast, col, in_list, like, lit, Avg, BinaryExpr, Column, DistinctCount,+ GetFieldAccessExpr, GetIndexedFieldExpr, NotExpr, NthValue, PhysicalSortExpr, Sum,};use datafusion::physical_plan::filter::FilterExec;use datafusion::physical_plan::functions::make_scalar_function;@@ -49,10 +48,12 @@ use datafusion::physical_plan::sorts::sort::SortExec;use datafusion::physical_plan::windows::{BuiltInWindowExpr, PlainAggregateWindowExpr, WindowAggExec,};-use datafusion::physical_plan::{functions, udaf};-use datafusion::physical_plan::{AggregateExpr, ExecutionPlan, PhysicalExpr, Statistics};+use datafusion::physical_plan::{+ functions, udaf, AggregateExpr, ExecutionPlan, PhysicalExpr, Statistics,+};use datafusion::prelude::SessionContext;use datafusion::scalar::ScalarValue;+use datafusion_common::stats::Precision;use datafusion_common::Result;use datafusion_expr::{Accumulator, AccumulatorFactoryFunction, AggregateUDF, ReturnTypeFunction, Signature,@@ -472,10 +473,11 @@ fn roundtrip_parquet_exec_with_pruning_predicate() -> Result<()> {1024,)]],statistics: Statistics {- num_rows: Some(100),- total_byte_size: Some(1024),- column_statistics: None,- is_exact: false,+ num_rows: Precision::Inexact(100),+ total_byte_size: Precision::Inexact(1024),+ column_statistics: Statistics::unknown_column(&Arc::new(Schema::new(vec![+ Field::new("col", DataType::Utf8, false),+ ]))),},projection: None,limit: None,diff --git a/datafusion/sqllogictest/test_files/explain.slt b/datafusion/sqllogictest/test_files/explain.sltindex 0e190a6acd..066a31590c 100644--- a/datafusion/sqllogictest/test_files/explain.slt+++ b/datafusion/sqllogictest/test_files/explain.slt@@ -273,8 +273,8 @@ query TTEXPLAIN SELECT a, b, c FROM simple_explain_test limit 10;----physical_plan-GlobalLimitExec: skip=0, fetch=10, statistics=[rows=10, bytes=None, exact=false]---CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/core/tests/data/example.csv]]}, projection=[a, b, c], limit=10, has_header=true, statistics=[]+GlobalLimitExec: skip=0, fetch=10, statistics=[Rows=Exact(10), Bytes=Absent]+--CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/datafusion/core/tests/data/example.csv]]}, projection=[a, b, c], limit=10, has_header=true, statistics=[Rows=Absent, Bytes=Absent]# Parquet scan with statistics collectedstatement ok@@ -287,8 +287,8 @@ query TTEXPLAIN SELECT * FROM alltypes_plain limit 10;----physical_plan-GlobalLimitExec: skip=0, fetch=10, statistics=[rows=8, bytes=None, exact=true]---ParquetExec: file_groups={1 group: [[WORKSPACE_ROOT/parquet-testing/data/alltypes_plain.parquet]]}, projection=[id, bool_col, tinyint_col, smallint_col, int_col, bigint_col, float_col, double_col, date_string_col, string_col, timestamp_col], limit=10, statistics=[rows=8, bytes=None, exact=true]+GlobalLimitExec: skip=0, fetch=10, statistics=[Rows=Exact(8), Bytes=Absent]+--ParquetExec: file_groups={1 group: [[WORKSPACE_ROOT/parquet-testing/data/alltypes_plain.parquet]]}, projection=[id, bool_col, tinyint_col, smallint_col, int_col, bigint_col, float_col, double_col, date_string_col, string_col, timestamp_col], limit=10, statistics=[Rows=Exact(8), Bytes=Absent]statement okset datafusion.execution.collect_statistics = false;diff --git a/datafusion/sqllogictest/test_files/window.slt b/datafusion/sqllogictest/test_files/window.sltindex aa7b3d5bb9..7226182cf3 100644--- a/datafusion/sqllogictest/test_files/window.slt+++ b/datafusion/sqllogictest/test_files/window.slt@@ -2010,14 +2010,10 @@ Projection: ARRAY_AGG(aggregate_test_100.c13) AS array_agg1--------TableScan: aggregate_test_100 projection=[c13]physical_planProjectionExec: expr=[ARRAY_AGG(aggregate_test_100.c13)@0 as array_agg1]---AggregateExec: mode=Final, gby=[], aggr=[ARRAY_AGG(aggregate_test_100.c13)]-----CoalescePartitionsExec-------AggregateExec: mode=Partial, gby=[], aggr=[ARRAY_AGG(aggregate_test_100.c13)]---------RepartitionExec: partitioning=RoundRobinBatch(2), input_partitions=1-----------GlobalLimitExec: skip=0, fetch=1-------------SortExec: TopK(fetch=1), expr=[c13@0 ASC NULLS LAST]---------------CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100.csv]]}, projection=[c13], has_header=true-+--AggregateExec: mode=Single, gby=[], aggr=[ARRAY_AGG(aggregate_test_100.c13)]+----GlobalLimitExec: skip=0, fetch=1+------SortExec: TopK(fetch=1), expr=[c13@0 ASC NULLS LAST]+--------CsvExec: file_groups={1 group: [[WORKSPACE_ROOT/testing/data/csv/aggregate_test_100.csv]]}, projection=[c13], has_header=truequery ?SELECT ARRAY_AGG(c13) as array_agg1 FROM (SELECT * FROM aggregate_test_100 ORDER BY c13 LIMIT 1)diff --git a/datafusion/substrait/src/physical_plan/consumer.rs b/datafusion/substrait/src/physical_plan/consumer.rsindex 8d25626a3b..7788ba0a69 100644--- a/datafusion/substrait/src/physical_plan/consumer.rs+++ b/datafusion/substrait/src/physical_plan/consumer.rs@@ -15,19 +15,21 @@// specific language governing permissions and limitations// under the License.-use async_recursion::async_recursion;-use chrono::DateTime;+use std::collections::HashMap;+use std::sync::Arc;+use datafusion::arrow::datatypes::Schema;use datafusion::common::not_impl_err;use datafusion::datasource::listing::PartitionedFile;use datafusion::datasource::object_store::ObjectStoreUrl;use datafusion::datasource::physical_plan::{FileScanConfig, ParquetExec};use datafusion::error::{DataFusionError, Result};-use datafusion::physical_plan::ExecutionPlan;+use datafusion::physical_plan::{ExecutionPlan, Statistics};use datafusion::prelude::SessionContext;++use async_recursion::async_recursion;+use chrono::DateTime;use object_store::ObjectMeta;-use std::collections::HashMap;-use std::sync::Arc;use substrait::proto::read_rel::local_files::file_or_files::PathType;use substrait::proto::{expression::MaskExpression, read_rel::ReadType, rel::RelType, Rel,@@ -104,7 +106,7 @@ pub async fn from_substrait_rel(object_store_url: ObjectStoreUrl::local_filesystem(),file_schema: Arc::new(Schema::empty()),file_groups,- statistics: Default::default(),+ statistics: Statistics::new_unknown(&Schema::empty()),projection: None,limit: None,table_partition_cols: vec![],diff --git a/datafusion/substrait/tests/cases/roundtrip_physical_plan.rs b/datafusion/substrait/tests/cases/roundtrip_physical_plan.rsindex 25d60471a9..3e5e757e4c 100644--- a/datafusion/substrait/tests/cases/roundtrip_physical_plan.rs+++ b/datafusion/substrait/tests/cases/roundtrip_physical_plan.rs@@ -15,16 +15,18 @@// specific language governing permissions and limitations// under the License.+use std::collections::HashMap;+use std::sync::Arc;+use datafusion::arrow::datatypes::Schema;use datafusion::datasource::listing::PartitionedFile;use datafusion::datasource::object_store::ObjectStoreUrl;use datafusion::datasource::physical_plan::{FileScanConfig, ParquetExec};use datafusion::error::Result;-use datafusion::physical_plan::{displayable, ExecutionPlan};+use datafusion::physical_plan::{displayable, ExecutionPlan, Statistics};use datafusion::prelude::SessionContext;use datafusion_substrait::physical_plan::{consumer, producer};-use std::collections::HashMap;-use std::sync::Arc;+use substrait::proto::extensions;#[tokio::test]@@ -42,7 +44,7 @@ async fn parquet_exec() -> Result<()> {123,)],],- statistics: Default::default(),+ statistics: Statistics::new_unknown(&Schema::empty()),projection: None,limit: None,table_partition_cols: vec![],