vortex_vector/binaryview/
scalar.rs1use crate::Scalar;
5use crate::ScalarOps;
6use crate::VectorMut;
7use crate::VectorMutOps;
8use crate::binaryview::BinaryType;
9use crate::binaryview::BinaryViewType;
10use crate::binaryview::BinaryViewTypeUpcast;
11use crate::binaryview::BinaryViewVectorMut;
12use crate::binaryview::StringType;
13
14#[derive(Clone, Debug, PartialEq)]
16pub struct BinaryViewScalar<T: BinaryViewType>(Option<T::Scalar>);
17
18impl<T: BinaryViewType> BinaryViewScalar<T> {
19 pub fn new(value: Option<T::Scalar>) -> Self {
21 Self(value)
22 }
23}
24
25impl<T: BinaryViewType> BinaryViewScalar<T> {
26 pub fn value(&self) -> Option<&T::Scalar> {
28 self.0.as_ref()
29 }
30
31 pub fn zero() -> Self {
33 Self::new(Some(T::empty_scalar()))
34 }
35
36 pub fn null() -> Self {
38 Self::new(None)
39 }
40}
41
42impl<T: BinaryViewType> ScalarOps for BinaryViewScalar<T> {
43 fn is_valid(&self) -> bool {
44 self.0.is_some()
45 }
46
47 fn mask_validity(&mut self, mask: bool) {
48 if !mask {
49 self.0 = None;
50 }
51 }
52
53 fn repeat(&self, n: usize) -> VectorMut {
54 let mut vec = BinaryViewVectorMut::<T>::with_capacity(n);
55 match self.value() {
56 None => vec.append_nulls(n),
57 Some(buf) => vec.append_owned_values(buf.clone(), n),
58 }
59 vec.into()
60 }
61}
62
63impl BinaryViewTypeUpcast for Scalar {
64 type Input<T: BinaryViewType> = BinaryViewScalar<T>;
65
66 fn from_binary(input: Self::Input<BinaryType>) -> Self {
67 Scalar::Binary(input)
68 }
69
70 fn from_string(input: Self::Input<StringType>) -> Self {
71 Scalar::String(input)
72 }
73}
74
75impl<T: BinaryViewType> From<BinaryViewScalar<T>> for Scalar {
76 fn from(val: BinaryViewScalar<T>) -> Self {
77 T::upcast(val)
78 }
79}