vortex_file/
file.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! This module defines the [`VortexFile`] struct, which represents a Vortex file on disk or in memory.
5//!
6//! The `VortexFile` provides methods for accessing file metadata, creating segment sources for reading
7//! data from the file, and initiating scans to read the file's contents into memory as Vortex arrays.
8
9use std::ops::Range;
10use std::sync::Arc;
11
12use itertools::Itertools;
13use vortex_array::ArrayRef;
14use vortex_array::expr::Expression;
15use vortex_array::expr::pruning::checked_pruning_expr;
16use vortex_array::stats::StatsSet;
17use vortex_dtype::DType;
18use vortex_dtype::Field;
19use vortex_dtype::FieldMask;
20use vortex_dtype::FieldPath;
21use vortex_dtype::FieldPathSet;
22use vortex_error::VortexResult;
23use vortex_layout::LayoutReader;
24use vortex_layout::segments::SegmentSource;
25use vortex_metrics::VortexMetrics;
26use vortex_scan::ScanBuilder;
27use vortex_scan::SplitBy;
28use vortex_session::VortexSession;
29use vortex_utils::aliases::hash_map::HashMap;
30
31use crate::footer::Footer;
32use crate::pruning::extract_relevant_file_stats_as_struct_row;
33
34/// Represents a Vortex file, providing access to its metadata and content.
35///
36/// A `VortexFile` is created by opening a Vortex file using [`VortexOpenOptions`](crate::VortexOpenOptions).
37/// It provides methods for accessing file metadata (such as row count, data type, and statistics)
38/// and for initiating scans to read the file's contents.
39#[derive(Clone)]
40pub struct VortexFile {
41    /// The footer of the Vortex file, containing metadata and layout information.
42    pub(crate) footer: Footer,
43    /// The segment source used to read segments from this file.
44    pub(crate) segment_source: Arc<dyn SegmentSource>,
45    /// Metrics tied to the file.
46    pub(crate) metrics: VortexMetrics,
47    /// The Vortex session used to open this file
48    pub(crate) session: VortexSession,
49}
50
51impl VortexFile {
52    /// Returns a reference to the file's footer, which contains metadata and layout information.
53    pub fn footer(&self) -> &Footer {
54        &self.footer
55    }
56
57    /// Returns the number of rows in the file.
58    pub fn row_count(&self) -> u64 {
59        self.footer.row_count()
60    }
61
62    /// Returns the data type of the file's contents.
63    pub fn dtype(&self) -> &DType {
64        self.footer.dtype()
65    }
66
67    /// Returns the file's statistics, if available.
68    ///
69    /// Statistics can be used for query optimization and data exploration.
70    pub fn file_stats(&self) -> Option<&Arc<[StatsSet]>> {
71        self.footer.statistics()
72    }
73
74    /// Returns a reference to the file's metrics.
75    pub fn metrics(&self) -> &VortexMetrics {
76        &self.metrics
77    }
78
79    /// Create a new segment source for reading from the file.
80    ///
81    /// This may spawn a background I/O driver that will exit when the returned segment source
82    /// is dropped.
83    pub fn segment_source(&self) -> Arc<dyn SegmentSource> {
84        self.segment_source.clone()
85    }
86
87    /// Create a new layout reader for the file.
88    pub fn layout_reader(&self) -> VortexResult<Arc<dyn LayoutReader>> {
89        let segment_source = self.segment_source();
90        self.footer
91            .layout()
92            // TODO(ngates): we may want to allow the user pass in a name here?
93            .new_reader("".into(), segment_source, &self.session)
94    }
95
96    /// Initiate a scan of the file, returning a builder for configuring the scan.
97    pub fn scan(&self) -> VortexResult<ScanBuilder<ArrayRef>> {
98        Ok(
99            ScanBuilder::new(self.session.clone(), self.layout_reader()?)
100                .with_metrics(self.metrics.clone()),
101        )
102    }
103
104    #[cfg(gpu_unstable)]
105    pub fn gpu_scan(
106        &self,
107        ctx: Arc<cudarc::driver::CudaContext>,
108    ) -> VortexResult<vortex_scan::gpu::GpuScanBuilder<vortex_gpu::GpuVector>> {
109        let segment_source = self.segment_source();
110        let gpu_reader = self
111            .footer
112            .layout()
113            .new_gpu_reader("".into(), segment_source, ctx)?;
114
115        Ok(vortex_scan::gpu::GpuScanBuilder::new(
116            self.session.clone(),
117            gpu_reader,
118        ))
119    }
120
121    /// Returns true if the expression will never match any rows in the file.
122    pub fn can_prune(&self, filter: &Expression) -> VortexResult<bool> {
123        let Some((stats, fields)) = self
124            .footer
125            .statistics()
126            .zip(self.footer.dtype().as_struct_fields_opt())
127        else {
128            return Ok(false);
129        };
130
131        let set = FieldPathSet::from_iter(fields.names().iter().zip(stats.iter()).flat_map(
132            |(name, stats)| {
133                stats.iter().map(|(stat, _)| {
134                    FieldPath::from_iter([
135                        Field::Name(name.clone()),
136                        Field::Name(stat.name().into()),
137                    ])
138                })
139            },
140        ));
141
142        let Some((predicate, required_stats)) = checked_pruning_expr(filter, &set) else {
143            return Ok(false);
144        };
145
146        let required_file_stats = HashMap::from_iter(
147            required_stats
148                .map()
149                .iter()
150                .map(|(path, stats)| (path.clone(), stats.clone())),
151        );
152
153        let Some(file_stats) =
154            extract_relevant_file_stats_as_struct_row(&required_file_stats, stats, fields)?
155        else {
156            return Ok(false);
157        };
158
159        Ok(predicate
160            .evaluate(&file_stats)?
161            .as_constant()
162            .is_some_and(|result| result.as_bool().value() == Some(true)))
163    }
164
165    pub fn splits(&self) -> VortexResult<Vec<Range<u64>>> {
166        let reader = self.layout_reader()?;
167        Ok(SplitBy::Layout
168            .splits(reader.as_ref(), &(0..reader.row_count()), &[FieldMask::All])?
169            .into_iter()
170            .tuple_windows()
171            .map(|(start, end)| start..end)
172            .collect())
173    }
174}