vortex_arrow/options/mod.rs
1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Per-export options, including options defined by external Arrow export plugins.
5
6use std::any::Any;
7use std::any::TypeId;
8use std::fmt;
9use std::sync::Arc;
10
11/// Typed options propagated to every nested array in an Arrow export.
12///
13/// Export plugins can define their own option types without modifying this crate. Each type has
14/// at most one value; absent options leave the exporter's default behavior unchanged.
15///
16/// ```
17/// use vortex_arrow::{ArrowExportOptions, CompactBuffers};
18///
19/// let options = ArrowExportOptions::default().with(CompactBuffers(false));
20/// assert_eq!(options.get::<CompactBuffers>(), Some(&CompactBuffers(false)));
21/// ```
22#[derive(Clone, Default)]
23pub struct ArrowExportOptions {
24 values: Vec<(TypeId, Arc<dyn Any + Send + Sync>)>,
25}
26
27impl ArrowExportOptions {
28 /// Set an option, replacing any previous value of the same type.
29 pub fn with<T: Any + Send + Sync>(mut self, option: T) -> Self {
30 let id = TypeId::of::<T>();
31 if let Some((_, value)) = self.values.iter_mut().find(|(key, _)| *key == id) {
32 *value = Arc::new(option);
33 } else {
34 self.values.push((id, Arc::new(option)));
35 }
36 self
37 }
38
39 /// Look up an option defined by this crate or an external exporter.
40 pub fn get<T: Any + Send + Sync>(&self) -> Option<&T> {
41 self.values
42 .iter()
43 .find(|(key, _)| *key == TypeId::of::<T>())
44 .and_then(|(_, value)| value.downcast_ref())
45 }
46
47 /// Look up an option, falling back to its default when unset.
48 pub fn get_or_default<T: Any + Send + Sync + Clone + Default>(&self) -> T {
49 self.get::<T>().cloned().unwrap_or_default()
50 }
51}
52
53impl fmt::Debug for ArrowExportOptions {
54 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
55 f.debug_struct("ArrowExportOptions")
56 .field("option_count", &self.values.len())
57 .finish_non_exhaustive()
58 }
59}
60
61/// Whether to compact string and binary view buffers during Arrow export.
62///
63/// Enabled by default. Disabling compaction avoids scans and copies, but the exported array may
64/// retain backing data outside the selected rows. Values and the Arrow type are unchanged.
65#[derive(Clone, Copy, Debug, PartialEq, Eq)]
66pub struct CompactBuffers(pub bool);
67
68impl Default for CompactBuffers {
69 fn default() -> Self {
70 Self(true)
71 }
72}
73
74#[cfg(test)]
75mod tests;