Skip to content

Commit 871ab1b

Browse files
committed
VarBinVector
Signed-off-by: Nicholas Gates <[email protected]>
1 parent feb0905 commit 871ab1b

File tree

5 files changed

+111
-96
lines changed

5 files changed

+111
-96
lines changed

vortex-vector/src/lib.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,11 @@ mod primitive;
1717
mod struct_;
1818
mod varbin;
1919

20-
pub use bool::{BoolVector, BoolVectorMut};
21-
pub use null::{NullVector, NullVectorMut};
22-
pub use primitive::{PVector, PVectorMut, PrimitiveVector, PrimitiveVectorMut};
23-
pub use struct_::{StructVector, StructVectorMut};
24-
pub use varbin::{
25-
BinaryVector, BinaryVectorMut, StringVector, StringVectorMut, VarBinType, VarBinVector,
26-
VarBinVectorMut,
27-
};
20+
pub use bool::*;
21+
pub use null::*;
22+
pub use primitive::*;
23+
pub use struct_::*;
24+
pub use varbin::*;
2825

2926
mod ops;
3027
mod vector;

vortex-vector/src/varbin/mod.rs

Lines changed: 7 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

4+
pub use types::*;
5+
pub use vector::*;
6+
pub use vector_mut::*;
7+
use vortex_error::vortex_panic;
8+
49
use crate::{Vector, VectorMut};
5-
use std::fmt::Debug;
610

11+
mod types;
712
mod vector;
8-
pub use vector::VarBinVector;
9-
1013
mod vector_mut;
11-
pub use vector_mut::VarBinVectorMut;
12-
use vortex_error::vortex_panic;
13-
1414
mod view;
1515

1616
/// Type alias for non-utf8 variable-length binary vectors.
@@ -22,81 +22,14 @@ pub type StringVector = VarBinVector<StringType>;
2222
/// Type alias for mutable UTF-8 variable-length string vectors.
2323
pub type StringVectorMut = VarBinVectorMut<StringType>;
2424

25-
impl<T: VarBinType> From<VarBinVector<T>> for Vector {
26-
fn from(value: VarBinVector<T>) -> Self {
27-
T::upcast(value)
28-
}
29-
}
30-
31-
impl<T: VarBinType> From<VarBinVectorMut<T>> for VectorMut {
32-
fn from(value: VarBinVectorMut<T>) -> Self {
33-
T::upcast(value)
34-
}
35-
}
36-
37-
/// Trait to mark supported binary view types.
38-
pub trait VarBinType: Debug + Sized + private::Sealed {
39-
/// The slice type for this variable binary type.
40-
type Slice: ?Sized + AsRef<[u8]>;
41-
42-
/// Downcast the provided object to a type-specific instance.
43-
fn downcast<V: VarBinTypeDowncast>(visitor: V) -> V::Output<Self>;
44-
45-
/// Upcast a type-specific instance to a generic instance.
46-
fn upcast<V: VarBinTypeUpcast>(input: V::Input<Self>) -> V;
47-
}
48-
49-
/// [`BinaryType`] for UTF-8 strings.
50-
#[derive(Clone, Debug)]
51-
pub struct StringType;
52-
impl VarBinType for StringType {
53-
type Slice = str;
54-
55-
fn downcast<V: VarBinTypeDowncast>(visitor: V) -> V::Output<Self> {
56-
visitor.into_string()
57-
}
58-
59-
fn upcast<V: VarBinTypeUpcast>(input: V::Input<Self>) -> V {
60-
V::from_string(input)
61-
}
62-
}
63-
64-
/// [`BinaryType`] for raw binary data.
65-
#[derive(Clone, Debug)]
66-
pub struct BinaryType;
67-
impl VarBinType for BinaryType {
68-
type Slice = [u8];
69-
70-
fn downcast<V: VarBinTypeDowncast>(visitor: V) -> V::Output<Self> {
71-
visitor.into_binary()
72-
}
73-
74-
fn upcast<V: VarBinTypeUpcast>(input: V::Input<Self>) -> V {
75-
V::from_binary(input)
76-
}
77-
}
78-
79-
pub trait VarBinTypeDowncast {
80-
type Output<T: VarBinType>;
81-
82-
fn into_binary(self) -> Self::Output<BinaryType>;
83-
fn into_string(self) -> Self::Output<StringType>;
84-
}
85-
86-
pub trait VarBinTypeUpcast {
87-
type Input<T: VarBinType>;
88-
89-
fn from_binary(input: Self::Input<BinaryType>) -> Self;
90-
fn from_string(input: Self::Input<StringType>) -> Self;
91-
}
92-
9325
impl VarBinTypeDowncast for Vector {
9426
type Output<T: VarBinType> = VarBinVector<T>;
9527

9628
fn into_binary(self) -> Self::Output<BinaryType> {
9729
if let Vector::Binary(v) = self {
9830
return v;
9931
}
32+
10033
vortex_panic!("Expected BinaryVector, got {self:?}");
10134
}
10235

@@ -149,9 +82,3 @@ impl VarBinTypeUpcast for VectorMut {
14982
VectorMut::String(input)
15083
}
15184
}
152-
153-
mod private {
154-
pub trait Sealed {}
155-
impl Sealed for super::StringType {}
156-
impl Sealed for super::BinaryType {}
157-
}

vortex-vector/src/varbin/types.rs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
// SPDX-FileCopyrightText: Copyright the Vortex contributors
3+
4+
use std::fmt::Debug;
5+
6+
use crate::{VarBinVector, VarBinVectorMut, Vector, VectorMut};
7+
8+
impl<T: VarBinType> From<VarBinVector<T>> for Vector {
9+
fn from(value: VarBinVector<T>) -> Self {
10+
T::upcast(value)
11+
}
12+
}
13+
14+
impl<T: VarBinType> From<VarBinVectorMut<T>> for VectorMut {
15+
fn from(value: VarBinVectorMut<T>) -> Self {
16+
T::upcast(value)
17+
}
18+
}
19+
20+
/// Trait to mark supported binary view types.
21+
pub trait VarBinType: Debug + Sized + private::Sealed {
22+
/// The slice type for this variable binary type.
23+
type Slice: ?Sized + AsRef<[u8]>;
24+
25+
/// Downcast the provided object to a type-specific instance.
26+
fn downcast<V: VarBinTypeDowncast>(visitor: V) -> V::Output<Self>;
27+
28+
/// Upcast a type-specific instance to a generic instance.
29+
fn upcast<V: VarBinTypeUpcast>(input: V::Input<Self>) -> V;
30+
}
31+
32+
/// [`BinaryType`] for UTF-8 strings.
33+
#[derive(Clone, Debug)]
34+
pub struct StringType;
35+
impl VarBinType for StringType {
36+
type Slice = str;
37+
38+
fn downcast<V: VarBinTypeDowncast>(visitor: V) -> V::Output<Self> {
39+
visitor.into_string()
40+
}
41+
42+
fn upcast<V: VarBinTypeUpcast>(input: V::Input<Self>) -> V {
43+
V::from_string(input)
44+
}
45+
}
46+
47+
/// [`BinaryType`] for raw binary data.
48+
#[derive(Clone, Debug)]
49+
pub struct BinaryType;
50+
impl VarBinType for BinaryType {
51+
type Slice = [u8];
52+
53+
fn downcast<V: VarBinTypeDowncast>(visitor: V) -> V::Output<Self> {
54+
visitor.into_binary()
55+
}
56+
57+
fn upcast<V: VarBinTypeUpcast>(input: V::Input<Self>) -> V {
58+
V::from_binary(input)
59+
}
60+
}
61+
62+
/// Trait for downcasting generic variable binary types to specific types.
63+
pub trait VarBinTypeDowncast {
64+
/// The output type after downcasting.
65+
type Output<T: VarBinType>;
66+
67+
/// Downcast to a binary type.
68+
fn into_binary(self) -> Self::Output<BinaryType>;
69+
/// Downcast to a string type.
70+
fn into_string(self) -> Self::Output<StringType>;
71+
}
72+
73+
/// Trait for upcasting specific variable binary types to generic types.
74+
pub trait VarBinTypeUpcast {
75+
/// The input type for upcasting.
76+
type Input<T: VarBinType>;
77+
78+
/// Upcast from a binary type.
79+
fn from_binary(input: Self::Input<BinaryType>) -> Self;
80+
/// Upcast from a string type.
81+
fn from_string(input: Self::Input<StringType>) -> Self;
82+
}
83+
84+
mod private {
85+
pub trait Sealed {}
86+
impl Sealed for super::StringType {}
87+
impl Sealed for super::BinaryType {}
88+
}

vortex-vector/src/varbin/vector.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

4-
use crate::varbin::vector_mut::VarBinVectorMut;
5-
use crate::varbin::view::BinaryView;
6-
use crate::varbin::VarBinType;
7-
use crate::VectorOps;
84
use std::sync::Arc;
5+
96
use vortex_buffer::{Buffer, ByteBuffer};
107
use vortex_mask::Mask;
118

9+
use crate::VectorOps;
10+
use crate::varbin::VarBinType;
11+
use crate::varbin::vector_mut::VarBinVectorMut;
12+
use crate::varbin::view::BinaryView;
13+
1214
/// A variable-length binary vector.
1315
#[derive(Debug, Clone)]
1416
pub struct VarBinVector<T: VarBinType> {

vortex-vector/src/varbin/vector_mut.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// SPDX-License-Identifier: Apache-2.0
22
// SPDX-FileCopyrightText: Copyright the Vortex contributors
33

4-
use crate::varbin::vector::VarBinVector;
5-
use crate::varbin::view::BinaryView;
6-
use crate::varbin::VarBinType;
7-
use crate::VectorMutOps;
84
use vortex_buffer::{BufferMut, ByteBuffer};
95
use vortex_mask::MaskMut;
106

7+
use crate::VectorMutOps;
8+
use crate::varbin::VarBinType;
9+
use crate::varbin::vector::VarBinVector;
10+
use crate::varbin::view::BinaryView;
11+
1112
/// Mutable variable-length binary vector.
1213
#[derive(Clone, Debug)]
1314
pub struct VarBinVectorMut<T: VarBinType> {

0 commit comments

Comments
 (0)