-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Extract parquet statistics to its own module, add tests #8294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 13 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
d187e36
Extract parquet statistics to its own module, add tests
alamb 7512c8b
Merge remote-tracking branch 'apache/main' into alamb/extract_parquet…
alamb d4e660a
Update datafusion/core/src/datasource/physical_plan/parquet/statistic…
alamb fd2aebc
rename enum
alamb 96a42f9
Merge branch 'alamb/extract_parquet_statistics' of github.com:alamb/a…
alamb a128a20
Improve API
alamb b4009c2
Add test for reading struct array statistics
alamb ef79c42
Add test for column after statistics
alamb 9b914db
improve tests
alamb b95dea9
simplify
alamb cd3c042
clippy
alamb ab95453
Update datafusion/core/src/datasource/physical_plan/parquet/statistic…
alamb 0235a9e
Update datafusion/core/src/datasource/physical_plan/parquet/statistic…
alamb a601fbf
Add test showing incorrect statistics
alamb 5c55302
Merge remote-tracking branch 'upstream/main' into alamb/extract_parqu…
tustvold 06b5201
Rework statistics
tustvold 641142b
Merge pull request #16 from tustvold/tustvold/extract_parquet_statistics
alamb e5cd8cf
Fix clippy
alamb b1666c2
Merge remote-tracking branch 'apache/main' into alamb/extract_parquet…
alamb 7022691
Update documentation and make it clear the statistics are not publica…
alamb a5e235a
Add link to upstream arrow ticket
alamb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,31 +15,26 @@ | |
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| use arrow::{ | ||
| array::ArrayRef, | ||
| datatypes::{DataType, Schema}, | ||
| }; | ||
| use arrow::{array::ArrayRef, datatypes::Schema}; | ||
| use datafusion_common::tree_node::{TreeNode, VisitRecursion}; | ||
| use datafusion_common::{Column, DataFusionError, Result, ScalarValue}; | ||
| use parquet::{ | ||
| arrow::{async_reader::AsyncFileReader, ParquetRecordBatchStreamBuilder}, | ||
| bloom_filter::Sbbf, | ||
| file::{metadata::RowGroupMetaData, statistics::Statistics as ParquetStatistics}, | ||
| file::metadata::RowGroupMetaData, | ||
| }; | ||
| use std::{ | ||
| collections::{HashMap, HashSet}, | ||
| sync::Arc, | ||
| }; | ||
|
|
||
| use crate::datasource::{ | ||
| listing::FileRange, | ||
| physical_plan::parquet::{from_bytes_to_i128, parquet_to_arrow_decimal_type}, | ||
| }; | ||
| use crate::datasource::listing::FileRange; | ||
| use crate::logical_expr::Operator; | ||
| use crate::physical_expr::expressions as phys_expr; | ||
| use crate::physical_optimizer::pruning::{PruningPredicate, PruningStatistics}; | ||
| use crate::physical_plan::PhysicalExpr; | ||
|
|
||
| use super::statistics::RowGroupStatisticsConverter; | ||
| use super::ParquetFileMetrics; | ||
|
|
||
| /// Prune row groups based on statistics | ||
|
|
@@ -303,112 +298,6 @@ struct RowGroupPruningStatistics<'a> { | |
| parquet_schema: &'a Schema, | ||
| } | ||
|
|
||
| /// Extract the min/max statistics from a `ParquetStatistics` object | ||
| macro_rules! get_statistic { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This macro is moved, without modification, into |
||
| ($column_statistics:expr, $func:ident, $bytes_func:ident, $target_arrow_type:expr) => {{ | ||
| if !$column_statistics.has_min_max_set() { | ||
| return None; | ||
| } | ||
| match $column_statistics { | ||
| ParquetStatistics::Boolean(s) => Some(ScalarValue::Boolean(Some(*s.$func()))), | ||
| ParquetStatistics::Int32(s) => { | ||
| match $target_arrow_type { | ||
| // int32 to decimal with the precision and scale | ||
| Some(DataType::Decimal128(precision, scale)) => { | ||
| Some(ScalarValue::Decimal128( | ||
| Some(*s.$func() as i128), | ||
| precision, | ||
| scale, | ||
| )) | ||
| } | ||
| _ => Some(ScalarValue::Int32(Some(*s.$func()))), | ||
| } | ||
| } | ||
| ParquetStatistics::Int64(s) => { | ||
| match $target_arrow_type { | ||
| // int64 to decimal with the precision and scale | ||
| Some(DataType::Decimal128(precision, scale)) => { | ||
| Some(ScalarValue::Decimal128( | ||
| Some(*s.$func() as i128), | ||
| precision, | ||
| scale, | ||
| )) | ||
| } | ||
| _ => Some(ScalarValue::Int64(Some(*s.$func()))), | ||
| } | ||
| } | ||
| // 96 bit ints not supported | ||
| ParquetStatistics::Int96(_) => None, | ||
| ParquetStatistics::Float(s) => Some(ScalarValue::Float32(Some(*s.$func()))), | ||
| ParquetStatistics::Double(s) => Some(ScalarValue::Float64(Some(*s.$func()))), | ||
| ParquetStatistics::ByteArray(s) => { | ||
| match $target_arrow_type { | ||
| // decimal data type | ||
| Some(DataType::Decimal128(precision, scale)) => { | ||
| Some(ScalarValue::Decimal128( | ||
| Some(from_bytes_to_i128(s.$bytes_func())), | ||
| precision, | ||
| scale, | ||
| )) | ||
| } | ||
| _ => { | ||
| let s = std::str::from_utf8(s.$bytes_func()) | ||
| .map(|s| s.to_string()) | ||
| .ok(); | ||
| Some(ScalarValue::Utf8(s)) | ||
| } | ||
| } | ||
| } | ||
| // type not supported yet | ||
| ParquetStatistics::FixedLenByteArray(s) => { | ||
| match $target_arrow_type { | ||
| // just support the decimal data type | ||
| Some(DataType::Decimal128(precision, scale)) => { | ||
| Some(ScalarValue::Decimal128( | ||
| Some(from_bytes_to_i128(s.$bytes_func())), | ||
| precision, | ||
| scale, | ||
| )) | ||
| } | ||
| _ => None, | ||
| } | ||
| } | ||
| } | ||
| }}; | ||
| } | ||
|
|
||
| // Extract the min or max value calling `func` or `bytes_func` on the ParquetStatistics as appropriate | ||
| macro_rules! get_min_max_values { | ||
| ($self:expr, $column:expr, $func:ident, $bytes_func:ident) => {{ | ||
| let (_column_index, field) = | ||
| if let Some((v, f)) = $self.parquet_schema.column_with_name(&$column.name) { | ||
| (v, f) | ||
| } else { | ||
| // Named column was not present | ||
| return None; | ||
| }; | ||
|
|
||
| let data_type = field.data_type(); | ||
| // The result may be None, because DataFusion doesn't have support for ScalarValues of the column type | ||
| let null_scalar: ScalarValue = data_type.try_into().ok()?; | ||
|
|
||
| $self.row_group_metadata | ||
| .columns() | ||
| .iter() | ||
| .find(|c| c.column_descr().name() == &$column.name) | ||
| .and_then(|c| if c.statistics().is_some() {Some((c.statistics().unwrap(), c.column_descr()))} else {None}) | ||
| .map(|(stats, column_descr)| | ||
| { | ||
| let target_data_type = parquet_to_arrow_decimal_type(column_descr); | ||
| get_statistic!(stats, $func, $bytes_func, target_data_type) | ||
| }) | ||
| .flatten() | ||
| // column either didn't have statistics at all or didn't have min/max values | ||
| .or_else(|| Some(null_scalar.clone())) | ||
| .and_then(|s| s.to_array().ok()) | ||
| }} | ||
| } | ||
|
|
||
| // Extract the null count value on the ParquetStatistics | ||
| macro_rules! get_null_count_values { | ||
| ($self:expr, $column:expr) => {{ | ||
|
|
@@ -431,11 +320,17 @@ macro_rules! get_null_count_values { | |
|
|
||
| impl<'a> PruningStatistics for RowGroupPruningStatistics<'a> { | ||
| fn min_values(&self, column: &Column) -> Option<ArrayRef> { | ||
| get_min_max_values!(self, column, min, min_bytes) | ||
| RowGroupStatisticsConverter::try_new(self.parquet_schema, &column.name) | ||
| .and_then(|converter| converter.min([self.row_group_metadata])) | ||
| // ignore errors during conversion, and just use no statistics | ||
| .ok() | ||
| } | ||
|
|
||
| fn max_values(&self, column: &Column) -> Option<ArrayRef> { | ||
| get_min_max_values!(self, column, max, max_bytes) | ||
| RowGroupStatisticsConverter::try_new(self.parquet_schema, &column.name) | ||
| .and_then(|converter| converter.max([self.row_group_metadata])) | ||
| // ignore errors during conversion, and just use no statistics | ||
| .ok() | ||
| } | ||
|
|
||
| fn num_containers(&self) -> usize { | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved to statistics.rs