-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose DataFusion statistics on an IcebergTableScan
- Loading branch information
Showing
10 changed files
with
252 additions
and
27 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use datafusion::common::stats::Precision; | ||
use datafusion::common::{ColumnStatistics, ScalarValue, Statistics}; | ||
use iceberg::{Catalog, Result, TableIdent}; | ||
use iceberg_datafusion::compute_statistics; | ||
use iceberg_integration_tests::set_test_fixture; | ||
|
||
#[tokio::test] | ||
async fn test_statistics() -> Result<()> { | ||
let fixture = set_test_fixture("datafusion_statistics").await; | ||
|
||
let catalog = fixture.rest_catalog; | ||
|
||
let table = catalog | ||
.load_table( | ||
&TableIdent::from_strs(["default", "test_positional_merge_on_read_double_deletes"]) | ||
.unwrap(), | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
let stats = compute_statistics(&table, None).await?; | ||
|
||
assert_eq!(stats, Statistics { | ||
num_rows: Precision::Inexact(14), | ||
total_byte_size: Precision::Absent, | ||
column_statistics: vec![ | ||
ColumnStatistics { | ||
null_count: Precision::Inexact(0), | ||
max_value: Precision::Inexact(ScalarValue::Date32(Some(19428))), | ||
min_value: Precision::Inexact(ScalarValue::Date32(Some(19417))), | ||
distinct_count: Precision::Absent, | ||
}, | ||
ColumnStatistics { | ||
null_count: Precision::Inexact(0), | ||
max_value: Precision::Inexact(ScalarValue::Int32(Some(12))), | ||
min_value: Precision::Inexact(ScalarValue::Int32(Some(1))), | ||
distinct_count: Precision::Absent, | ||
}, | ||
ColumnStatistics { | ||
null_count: Precision::Inexact(0), | ||
max_value: Precision::Inexact(ScalarValue::Utf8View(Some("l".to_string()))), | ||
min_value: Precision::Inexact(ScalarValue::Utf8View(Some("a".to_string()))), | ||
distinct_count: Precision::Absent, | ||
}, | ||
], | ||
}); | ||
|
||
Ok(()) | ||
} |
This file contains 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 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 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 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 |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Licensed to the Apache Software Foundation (ASF) under one | ||
// or more contributor license agreements. See the NOTICE file | ||
// distributed with this work for additional information | ||
// regarding copyright ownership. The ASF licenses this file | ||
// to you under the Apache License, Version 2.0 (the | ||
// "License"); you may not use this file except in compliance | ||
// with the License. You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use std::collections::HashMap; | ||
|
||
use datafusion::common::stats::Precision; | ||
use datafusion::common::{ColumnStatistics, Statistics}; | ||
use iceberg::spec::ManifestStatus; | ||
use iceberg::table::Table; | ||
use iceberg::Result; | ||
|
||
use crate::physical_plan::expr_to_predicate::datum_to_scalar_value; | ||
|
||
// Compute DataFusion table statistics for a given table/snapshot | ||
pub async fn compute_statistics(table: &Table, snapshot_id: Option<i64>) -> Result<Statistics> { | ||
let file_io = table.file_io(); | ||
let metadata = table.metadata(); | ||
let snapshot = table.snapshot(snapshot_id)?; | ||
|
||
let mut num_rows = 0; | ||
let mut lower_bounds = HashMap::new(); | ||
let mut upper_bounds = HashMap::new(); | ||
let mut null_counts = HashMap::new(); | ||
|
||
let manifest_list = snapshot.load_manifest_list(file_io, metadata).await?; | ||
|
||
// For each existing/added manifest in the snapshot aggregate the row count, as well as null | ||
// count and min/max values. | ||
for manifest_file in manifest_list.entries() { | ||
let manifest = manifest_file.load_manifest(file_io).await?; | ||
manifest.entries().iter().for_each(|manifest_entry| { | ||
if manifest_entry.status() != ManifestStatus::Deleted { | ||
let data_file = manifest_entry.data_file(); | ||
num_rows += data_file.record_count(); | ||
data_file.lower_bounds().iter().for_each(|(col_id, min)| { | ||
lower_bounds | ||
.entry(*col_id) | ||
.and_modify(|col_min| { | ||
if min < col_min { | ||
*col_min = min.clone() | ||
} | ||
}) | ||
.or_insert(min.clone()); | ||
}); | ||
data_file.upper_bounds().iter().for_each(|(col_id, max)| { | ||
upper_bounds | ||
.entry(*col_id) | ||
.and_modify(|col_max| { | ||
if max > col_max { | ||
*col_max = max.clone() | ||
} | ||
}) | ||
.or_insert(max.clone()); | ||
}); | ||
data_file | ||
.null_value_counts() | ||
.iter() | ||
.for_each(|(col_id, null_count)| { | ||
null_counts | ||
.entry(*col_id) | ||
.and_modify(|col_null_count| *col_null_count += *null_count) | ||
.or_insert(*null_count); | ||
}); | ||
} | ||
}) | ||
} | ||
|
||
// Construct the DataFusion `Statistics` object, leaving any missing info as `Precision::Absent` | ||
let schema = snapshot.schema(metadata)?; | ||
let col_stats = schema | ||
.as_struct() | ||
.fields() | ||
.iter() | ||
.map(|field| { | ||
ColumnStatistics { | ||
null_count: null_counts | ||
.get(&field.id) | ||
.map(|nc| Precision::Inexact(*nc as usize)) | ||
.unwrap_or(Precision::Absent), | ||
max_value: upper_bounds | ||
.get(&field.id) | ||
.and_then(|datum| datum_to_scalar_value(datum).map(Precision::Inexact)) | ||
.unwrap_or(Precision::Absent), | ||
min_value: lower_bounds | ||
.get(&field.id) | ||
.and_then(|datum| datum_to_scalar_value(datum).map(Precision::Inexact)) | ||
.unwrap_or(Precision::Absent), | ||
distinct_count: Precision::Absent, // will be picked up after #417 | ||
} | ||
}) | ||
.collect(); | ||
|
||
Ok(Statistics { | ||
num_rows: Precision::Inexact(num_rows as usize), | ||
total_byte_size: Precision::Absent, | ||
column_statistics: col_stats, | ||
}) | ||
} |
Oops, something went wrong.