|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +use std::collections::HashMap; |
| 19 | + |
| 20 | +use datafusion::common::stats::Precision; |
| 21 | +use datafusion::common::{ColumnStatistics, Statistics}; |
| 22 | +use iceberg::spec::ManifestStatus; |
| 23 | +use iceberg::table::Table; |
| 24 | +use iceberg::Result; |
| 25 | + |
| 26 | +use crate::physical_plan::expr_to_predicate::datum_to_scalar_value; |
| 27 | + |
| 28 | +// Compute DataFusion table statistics for a given table/snapshot |
| 29 | +pub async fn compute_statistics(table: &Table, snapshot_id: Option<i64>) -> Result<Statistics> { |
| 30 | + let file_io = table.file_io(); |
| 31 | + let metadata = table.metadata(); |
| 32 | + let snapshot = table.snapshot(snapshot_id)?; |
| 33 | + |
| 34 | + let mut num_rows = 0; |
| 35 | + let mut lower_bounds = HashMap::new(); |
| 36 | + let mut upper_bounds = HashMap::new(); |
| 37 | + let mut null_counts = HashMap::new(); |
| 38 | + |
| 39 | + let manifest_list = snapshot.load_manifest_list(file_io, metadata).await?; |
| 40 | + |
| 41 | + // For each existing/added manifest in the snapshot aggregate the row count, as well as null |
| 42 | + // count and min/max values. |
| 43 | + for manifest_file in manifest_list.entries() { |
| 44 | + let manifest = manifest_file.load_manifest(file_io).await?; |
| 45 | + manifest.entries().iter().for_each(|manifest_entry| { |
| 46 | + if manifest_entry.status() != ManifestStatus::Deleted { |
| 47 | + let data_file = manifest_entry.data_file(); |
| 48 | + num_rows += data_file.record_count(); |
| 49 | + data_file.lower_bounds().iter().for_each(|(col_id, min)| { |
| 50 | + lower_bounds |
| 51 | + .entry(*col_id) |
| 52 | + .and_modify(|col_min| { |
| 53 | + if min < col_min { |
| 54 | + *col_min = min.clone() |
| 55 | + } |
| 56 | + }) |
| 57 | + .or_insert(min.clone()); |
| 58 | + }); |
| 59 | + data_file.upper_bounds().iter().for_each(|(col_id, max)| { |
| 60 | + upper_bounds |
| 61 | + .entry(*col_id) |
| 62 | + .and_modify(|col_max| { |
| 63 | + if max > col_max { |
| 64 | + *col_max = max.clone() |
| 65 | + } |
| 66 | + }) |
| 67 | + .or_insert(max.clone()); |
| 68 | + }); |
| 69 | + data_file |
| 70 | + .null_value_counts() |
| 71 | + .iter() |
| 72 | + .for_each(|(col_id, null_count)| { |
| 73 | + null_counts |
| 74 | + .entry(*col_id) |
| 75 | + .and_modify(|col_null_count| *col_null_count += *null_count) |
| 76 | + .or_insert(*null_count); |
| 77 | + }); |
| 78 | + } |
| 79 | + }) |
| 80 | + } |
| 81 | + |
| 82 | + // Construct the DataFusion `Statistics` object, leaving any missing info as `Precision::Absent` |
| 83 | + let schema = snapshot.schema(metadata)?; |
| 84 | + let col_stats = schema |
| 85 | + .as_struct() |
| 86 | + .fields() |
| 87 | + .iter() |
| 88 | + .map(|field| { |
| 89 | + ColumnStatistics { |
| 90 | + null_count: null_counts |
| 91 | + .get(&field.id) |
| 92 | + .map(|nc| Precision::Inexact(*nc as usize)) |
| 93 | + .unwrap_or(Precision::Absent), |
| 94 | + max_value: upper_bounds |
| 95 | + .get(&field.id) |
| 96 | + .and_then(|datum| datum_to_scalar_value(datum).map(Precision::Inexact)) |
| 97 | + .unwrap_or(Precision::Absent), |
| 98 | + min_value: lower_bounds |
| 99 | + .get(&field.id) |
| 100 | + .and_then(|datum| datum_to_scalar_value(datum).map(Precision::Inexact)) |
| 101 | + .unwrap_or(Precision::Absent), |
| 102 | + distinct_count: Precision::Absent, // will be picked up after #417 |
| 103 | + } |
| 104 | + }) |
| 105 | + .collect(); |
| 106 | + |
| 107 | + Ok(Statistics { |
| 108 | + num_rows: Precision::Inexact(num_rows as usize), |
| 109 | + total_byte_size: Precision::Absent, |
| 110 | + column_statistics: col_stats, |
| 111 | + }) |
| 112 | +} |
0 commit comments