vortex_vector/binaryview/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4//! Definition and implementation of variable-length binary types.
5//!
6//! All types are specializations of the [`BinaryViewVector`] type, which is represented internally
7//! by `BinaryView`s. `BinaryView`s are identical to the `BinaryView` type defined by the Arrow
8//! [specification](https://arrow.apache.org/docs/format/Columnar.html#variable-size-binary-view-layout),
9//! which are inspired by "German" strings.
10
11pub use scalar::*;
12pub use types::*;
13pub use vector::*;
14pub use vector_mut::*;
15pub use view::*;
16use vortex_error::vortex_panic;
17
18use crate::Vector;
19use crate::VectorMut;
20
21mod scalar;
22mod types;
23mod vector;
24mod vector_mut;
25mod view;
26
27/// Type alias for non-utf8 variable-length binary vectors.
28pub type BinaryVector = BinaryViewVector<BinaryType>;
29/// Type alias for mutable non-utf8 variable-length binary vectors.
30pub type BinaryVectorMut = BinaryViewVectorMut<BinaryType>;
31/// Type alias for UTF-8 variable-length string vectors.
32pub type StringVector = BinaryViewVector<StringType>;
33/// Type alias for mutable UTF-8 variable-length string vectors.
34pub type StringVectorMut = BinaryViewVectorMut<StringType>;
35/// Type alias for non-utf8 variable-length binary scalars.
36pub type BinaryScalar = BinaryViewScalar<BinaryType>;
37/// Type alias for UTF-8 variable-length string scalars.
38pub type StringScalar = BinaryViewScalar<StringType>;
39
40impl BinaryViewDowncast for Vector {
41    type Output<T: BinaryViewType> = BinaryViewVector<T>;
42
43    fn into_binary(self) -> Self::Output<BinaryType> {
44        if let Vector::Binary(v) = self {
45            return v;
46        }
47        vortex_panic!("Expected BinaryVector, got {self:?}");
48    }
49
50    fn into_string(self) -> Self::Output<StringType> {
51        if let Vector::String(v) = self {
52            return v;
53        }
54        vortex_panic!("Expected StringVector, got {self:?}");
55    }
56}
57
58impl BinaryViewTypeUpcast for Vector {
59    type Input<T: BinaryViewType> = BinaryViewVector<T>;
60
61    fn from_binary(input: Self::Input<BinaryType>) -> Self {
62        Vector::Binary(input)
63    }
64
65    fn from_string(input: Self::Input<StringType>) -> Self {
66        Vector::String(input)
67    }
68}
69
70impl BinaryViewDowncast for VectorMut {
71    type Output<T: BinaryViewType> = BinaryViewVectorMut<T>;
72
73    fn into_binary(self) -> Self::Output<BinaryType> {
74        if let VectorMut::Binary(v) = self {
75            return v;
76        }
77        vortex_panic!("Expected BinaryVector, got {self:?}");
78    }
79
80    fn into_string(self) -> Self::Output<StringType> {
81        if let VectorMut::String(v) = self {
82            return v;
83        }
84        vortex_panic!("Expected StringVector, got {self:?}");
85    }
86}
87
88impl BinaryViewTypeUpcast for VectorMut {
89    type Input<T: BinaryViewType> = BinaryViewVectorMut<T>;
90
91    fn from_binary(input: Self::Input<BinaryType>) -> Self {
92        VectorMut::Binary(input)
93    }
94
95    fn from_string(input: Self::Input<StringType>) -> Self {
96        VectorMut::String(input)
97    }
98}