|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// SPDX-FileCopyrightText: Copyright the Vortex contributors |
| 3 | + |
| 4 | +use async_trait::async_trait; |
| 5 | +use vortex_array::aggregate_fn::AggregateFnRef; |
| 6 | +use vortex_array::dtype::DType; |
| 7 | +use vortex_array::dtype::FieldPath; |
| 8 | +use vortex_array::expr::stats::Precision; |
| 9 | +use vortex_array::scalar::Scalar; |
| 10 | +use vortex_array::stats::StatsSet; |
| 11 | +use vortex_error::VortexResult; |
| 12 | +use vortex_layout::scan::layout::LayoutReaderDataSource; |
| 13 | +use vortex_scan::DataSource; |
| 14 | +use vortex_scan::DataSourceScanRef; |
| 15 | +use vortex_scan::PartitionRef; |
| 16 | +use vortex_scan::ScanRequest; |
| 17 | +use vortex_session::VortexSession; |
| 18 | + |
| 19 | +use crate::FileStatistics; |
| 20 | + |
| 21 | +pub(crate) struct FileDataSource { |
| 22 | + source: LayoutReaderDataSource, |
| 23 | + statistics: Option<FileStatistics>, |
| 24 | +} |
| 25 | + |
| 26 | +impl FileDataSource { |
| 27 | + pub(crate) fn new(source: LayoutReaderDataSource, statistics: Option<FileStatistics>) -> Self { |
| 28 | + Self { source, statistics } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +#[async_trait] |
| 33 | +impl DataSource for FileDataSource { |
| 34 | + fn dtype(&self) -> &DType { |
| 35 | + self.source.dtype() |
| 36 | + } |
| 37 | + |
| 38 | + fn row_count(&self) -> Precision<u64> { |
| 39 | + self.source.row_count() |
| 40 | + } |
| 41 | + |
| 42 | + fn byte_size(&self) -> Precision<u64> { |
| 43 | + self.source.byte_size() |
| 44 | + } |
| 45 | + |
| 46 | + fn deserialize_partition( |
| 47 | + &self, |
| 48 | + data: &[u8], |
| 49 | + session: &VortexSession, |
| 50 | + ) -> VortexResult<PartitionRef> { |
| 51 | + self.source.deserialize_partition(data, session) |
| 52 | + } |
| 53 | + |
| 54 | + async fn scan(&self, request: ScanRequest) -> VortexResult<DataSourceScanRef> { |
| 55 | + self.source.scan(request).await |
| 56 | + } |
| 57 | + |
| 58 | + async fn field_statistics(&self, field_path: &FieldPath) -> VortexResult<StatsSet> { |
| 59 | + Ok(self |
| 60 | + .statistics |
| 61 | + .as_ref() |
| 62 | + .and_then(|statistics| statistics.get_by_path(field_path)) |
| 63 | + .map(|(statistics, _)| statistics.to_stats_set()) |
| 64 | + .unwrap_or_default()) |
| 65 | + } |
| 66 | + |
| 67 | + async fn field_statistic( |
| 68 | + &self, |
| 69 | + field_path: &FieldPath, |
| 70 | + aggregate_fn: &AggregateFnRef, |
| 71 | + ) -> VortexResult<Precision<Scalar>> { |
| 72 | + let Some((statistics, _)) = self |
| 73 | + .statistics |
| 74 | + .as_ref() |
| 75 | + .and_then(|statistics| statistics.get_by_path(field_path)) |
| 76 | + else { |
| 77 | + return Ok(Precision::Absent); |
| 78 | + }; |
| 79 | + Ok(statistics |
| 80 | + .iter() |
| 81 | + .find(|statistic| statistic.aggregate_fn() == aggregate_fn) |
| 82 | + .and_then(|statistic| statistic.partial()) |
| 83 | + .cloned() |
| 84 | + .map_or(Precision::Absent, Precision::Exact)) |
| 85 | + } |
| 86 | +} |
0 commit comments