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, VectorMut};
19
20mod scalar;
21mod types;
22mod vector;
23mod vector_mut;
24mod view;
25
26/// Type alias for non-utf8 variable-length binary vectors.
27pub type BinaryVector = BinaryViewVector<BinaryType>;
28/// Type alias for mutable non-utf8 variable-length binary vectors.
29pub type BinaryVectorMut = BinaryViewVectorMut<BinaryType>;
30/// Type alias for UTF-8 variable-length string vectors.
31pub type StringVector = BinaryViewVector<StringType>;
32/// Type alias for mutable UTF-8 variable-length string vectors.
33pub type StringVectorMut = BinaryViewVectorMut<StringType>;
34/// Type alias for non-utf8 variable-length binary scalars.
35pub type BinaryScalar = BinaryViewScalar<BinaryType>;
36/// Type alias for UTF-8 variable-length string scalars.
37pub type StringScalar = BinaryViewScalar<StringType>;
38
39impl BinaryViewDowncast for Vector {
40    type Output<T: BinaryViewType> = BinaryViewVector<T>;
41
42    fn into_binary(self) -> Self::Output<BinaryType> {
43        if let Vector::Binary(v) = self {
44            return v;
45        }
46        vortex_panic!("Expected BinaryVector, got {self:?}");
47    }
48
49    fn into_string(self) -> Self::Output<StringType> {
50        if let Vector::String(v) = self {
51            return v;
52        }
53        vortex_panic!("Expected StringVector, got {self:?}");
54    }
55}
56
57impl BinaryViewTypeUpcast for Vector {
58    type Input<T: BinaryViewType> = BinaryViewVector<T>;
59
60    fn from_binary(input: Self::Input<BinaryType>) -> Self {
61        Vector::Binary(input)
62    }
63
64    fn from_string(input: Self::Input<StringType>) -> Self {
65        Vector::String(input)
66    }
67}
68
69impl BinaryViewDowncast for VectorMut {
70    type Output<T: BinaryViewType> = BinaryViewVectorMut<T>;
71
72    fn into_binary(self) -> Self::Output<BinaryType> {
73        if let VectorMut::Binary(v) = self {
74            return v;
75        }
76        vortex_panic!("Expected BinaryVector, got {self:?}");
77    }
78
79    fn into_string(self) -> Self::Output<StringType> {
80        if let VectorMut::String(v) = self {
81            return v;
82        }
83        vortex_panic!("Expected StringVector, got {self:?}");
84    }
85}
86
87impl BinaryViewTypeUpcast for VectorMut {
88    type Input<T: BinaryViewType> = BinaryViewVectorMut<T>;
89
90    fn from_binary(input: Self::Input<BinaryType>) -> Self {
91        VectorMut::Binary(input)
92    }
93
94    fn from_string(input: Self::Input<StringType>) -> Self {
95        VectorMut::String(input)
96    }
97}