Skip to main content

vortex_arrow/extension/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Arrow interoperability for Vortex extension types.
5//!
6//! Implement [`ArrowExtVTable`] and register it with [`ArrowSession::register_extension`] to enable
7//! Arrow import and export. Registration wraps the callbacks in an adapter that handles Vortex
8//! metadata, while the session validates schema round trips. Plugins only implement conversions.
9//!
10//! A Vortex extension does not need an equivalent standard Arrow type. It can use a custom Arrow
11//! extension name, or decline native export so the session exports storage with the Vortex ID and
12//! metadata. When a plugin supplies an Arrow representation, its metadata may not describe the full
13//! Vortex dtype. Handling that gap in the adapter prevents plugins from silently losing metadata.
14//!
15//! Declare an [`ArrowMetadataPolicy`] in [`ArrowExtVTable::metadata_policy`] based on whether the
16//! Arrow representation preserves the full Vortex dtype on its own. Callbacks leave transport to
17//! the adapter and import arrays with the restored dtype they receive. Plugins remain responsible
18//! for preserving values and lengths.
19
20use arrow_array::ArrayRef as ArrowArrayRef;
21use arrow_schema::Field;
22use vortex_array::ArrayRef;
23use vortex_array::ExecutionCtx;
24use vortex_array::dtype::extension::ExtDType;
25use vortex_array::dtype::extension::ExtVTable;
26use vortex_error::VortexResult;
27use vortex_session::registry::Id;
28
29use crate::ArrowExport;
30use crate::ArrowExportOptions;
31use crate::ArrowImport;
32use crate::ArrowSession;
33
34/// Whether an Arrow representation needs separate Vortex metadata or not to restore the full dtype.
35///
36/// On schema export, the session imports the generated field and checks that it reproduces the
37/// original dtype, including metadata, storage, and nullability. On successful array import, the
38/// session checks that the returned array has the dtype inferred from the field.
39#[derive(Clone, Copy, Debug, Eq, PartialEq)]
40pub enum ArrowMetadataPolicy {
41    /// Rely on the Arrow representation alone to preserve the complete Vortex dtype.
42    ///
43    /// Choose this only when the plugin's Arrow field can reconstruct the dtype without additional
44    /// Vortex metadata. The adapter neither writes nor reads a separate Vortex metadata payload.
45    ArrowNative,
46
47    /// Preserve Vortex metadata separately when the Arrow representation alone is insufficient.
48    ///
49    /// If the exported field does not reconstruct the original dtype, the adapter adds a Vortex
50    /// metadata payload before the session checks the completed field. The Arrow extension's own
51    /// metadata stays unchanged. On import, the payload restores metadata over the plugin's inferred
52    /// Vortex storage. The restored metadata must declare that same storage dtype, including nested
53    /// nullability. A mismatch returns an error.
54    ///
55    /// An absent payload uses the native interpretation. A required empty payload is still
56    /// written, because a codec can distinguish it from absence.
57    PreserveVortex,
58}
59
60/// Native Arrow conversion callbacks for one Vortex extension type.
61///
62/// The array callbacks must follow the value contracts of [`crate::ArrowExportVTable`] and
63/// [`crate::ArrowImportVTable`]. The adapter carries Vortex metadata so that importing with the
64/// inferred Arrow field can recover the original dtype.
65///
66/// Register through [`ArrowSession::register_extension`], and use the session's conversion APIs.
67/// Calling these callbacks directly bypasses metadata transport and schema validation. Callbacks
68/// **must** describe only the native Arrow representation, leaving Vortex metadata transport to the
69/// adapter. Register the owning dtype in the Vortex session as well to enable its Vortex ID path.
70///
71/// Register the dtype and its Arrow conversions in the same session:
72///
73/// ```
74/// use vortex_array::dtype::session::DTypeSessionExt;
75/// use vortex_arrow::{ArrowExtVTable, ArrowSessionExt};
76/// use vortex_session::VortexSession;
77///
78/// fn register<V: ArrowExtVTable>(session: &VortexSession, vtable: V) {
79///     session.dtypes().register(vtable.clone());
80///     session.arrow().register_extension(vtable);
81/// }
82/// ```
83pub trait ArrowExtVTable: ExtVTable {
84    /// The metadata policy used by the adapter for this extension type.
85    ///
86    /// The result **must** remain the same across calls and sessions.
87    fn metadata_policy() -> ArrowMetadataPolicy;
88
89    /// The native Arrow extension ID used for both import and export.
90    fn arrow_ext_id(&self) -> Id;
91
92    /// Builds the native field, or declines so that the session uses the Vortex ID path.
93    ///
94    /// Use [`Field::try_with_extension_type`] for standard Arrow extension metadata, or
95    /// [`crate::ext_metadata::set_arrow_extension_name`] for a custom name. The adapter adds any
96    /// Vortex metadata before the session validates the completed field against the source dtype.
97    fn to_arrow_field(
98        &self,
99        name: &str,
100        ext_dtype: &ExtDType<Self>,
101        session: &ArrowSession,
102    ) -> VortexResult<Option<Field>>;
103
104    /// Converts an array of this extension type to the native target representation.
105    ///
106    /// The adapter checks the source extension type before calling this method. Return
107    /// [`ArrowExport::Unsupported`] when the target is unsupported. Child exports **must** receive
108    /// the same `options` through [`crate::ExecuteArrowExt::execute_arrow_with_options`].
109    ///
110    /// The export **must** follow the
111    /// [null and comparison rules](crate::ArrowExportVTable::execute_arrow).
112    fn execute_arrow(
113        &self,
114        array: ArrayRef,
115        target: &Field,
116        options: &ArrowExportOptions,
117        ctx: &mut ExecutionCtx,
118    ) -> VortexResult<ArrowExport>;
119
120    /// Interprets the native field, without applying Vortex side metadata.
121    ///
122    /// Infer the Vortex storage representation here, which can differ from Arrow's physical type.
123    /// Return `None` only to defer the field to another plugin under the same Arrow extension
124    /// name. A field that names this type but falls outside its domain **must** fail, as
125    /// [`ArrowImportVTable`](crate::ArrowImportVTable) states.
126    fn from_arrow_field(
127        field: &Field,
128        session: &ArrowSession,
129    ) -> VortexResult<Option<ExtDType<Self>>>;
130
131    /// Imports native values with the extension dtype restored by the adapter.
132    ///
133    /// `ext_dtype` includes any Vortex metadata restored by the adapter, so this callback does not
134    /// need to decode it. Inferring the dtype again from the native Arrow representation could lose
135    /// that metadata.
136    ///
137    /// A successful result **must** have exactly `ext_dtype` and the input length. Return
138    /// [`ArrowImport::Unsupported`] only to defer the array to another plugin under the same Arrow
139    /// extension name, as [`ArrowImportVTable`](crate::ArrowImportVTable) states.
140    ///
141    /// See the [importer contract](crate::ArrowImportVTable::from_arrow_array) for how to apply
142    /// the storage dtype's nullability.
143    fn from_arrow_array(
144        array: ArrowArrayRef,
145        field: &Field,
146        ext_dtype: &ExtDType<Self>,
147        session: &ArrowSession,
148    ) -> VortexResult<ArrowImport>;
149}
150
151mod adapter;
152
153#[cfg(test)]
154mod tests;