vortex_vector/
scalar.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_error::vortex_panic;
5
6use crate::ScalarOps;
7use crate::VectorMut;
8use crate::binaryview::BinaryScalar;
9use crate::binaryview::StringScalar;
10use crate::bool::BoolScalar;
11use crate::decimal::DecimalScalar;
12use crate::fixed_size_list::FixedSizeListScalar;
13use crate::listview::ListViewScalar;
14use crate::match_each_scalar;
15use crate::null::NullScalar;
16use crate::primitive::PrimitiveScalar;
17use crate::struct_::StructScalar;
18
19/// Represents a scalar value of any supported type.
20#[derive(Clone, Debug)]
21pub enum Scalar {
22    /// Null scalars are always null.
23    Null(NullScalar),
24    /// Boolean scalars hold the boolean value in an Option, where None represents null.
25    Bool(BoolScalar),
26    /// Decimal scalars hold the decimal value in a DScalar, or else None for null.
27    Decimal(DecimalScalar),
28    /// Primitive scalars hold the primitive value in a PScalar, or else None for null.
29    Primitive(PrimitiveScalar),
30    /// String scalars hold the string data in a BufferString, or else None for null.
31    String(StringScalar),
32    /// Binary scalars hold the binary data in a ByteBuffer, or else None for null.
33    Binary(BinaryScalar),
34    /// Variable-size list scalars hold the list elements in a vector, or else None for null.
35    List(ListViewScalar),
36    /// Fixed-size list scalars hold the list elements in a vector, or else None for null.
37    FixedSizeList(FixedSizeListScalar),
38    /// Struct scalars are represented as a length-1 struct vector.
39    Struct(StructScalar),
40}
41
42impl ScalarOps for Scalar {
43    fn is_valid(&self) -> bool {
44        match_each_scalar!(self, |v| { v.is_valid() })
45    }
46
47    fn is_invalid(&self) -> bool {
48        !self.is_valid()
49    }
50
51    fn mask_validity(&mut self, mask: bool) {
52        match_each_scalar!(self, |v| { v.mask_validity(mask) })
53    }
54
55    fn repeat(&self, n: usize) -> VectorMut {
56        match_each_scalar!(self, |v| { v.repeat(n) })
57    }
58}
59
60impl Scalar {
61    /// Converts the `Scalar` into a `NullScalar`.
62    pub fn into_null(self) -> NullScalar {
63        if let Scalar::Null(scalar) = self {
64            return scalar;
65        }
66        vortex_panic!("Cannot convert non-null Scalar into NullScalar");
67    }
68
69    /// Converts the `Scalar` into a `BoolScalar`.
70    pub fn into_bool(self) -> BoolScalar {
71        if let Scalar::Bool(scalar) = self {
72            return scalar;
73        }
74        vortex_panic!("Cannot convert non-bool Scalar into BoolScalar");
75    }
76
77    /// Converts the `Scalar` into a `BoolScalar`.
78    pub fn to_bool(&self) -> &BoolScalar {
79        if let Scalar::Bool(scalar) = self {
80            return scalar;
81        }
82        vortex_panic!("Cannot convert non-bool Scalar into BoolScalar");
83    }
84
85    /// Converts the `Scalar` into a `DecimalScalar`.
86    pub fn into_decimal(self) -> DecimalScalar {
87        if let Scalar::Decimal(scalar) = self {
88            return scalar;
89        }
90        vortex_panic!("Cannot convert non-decimal Scalar into DecimalScalar");
91    }
92
93    /// Converts the `Scalar` into a `PrimitiveScalar`.
94    pub fn into_primitive(self) -> PrimitiveScalar {
95        if let Scalar::Primitive(scalar) = self {
96            return scalar;
97        }
98        vortex_panic!("Cannot convert non-primitive Scalar into PrimitiveScalar");
99    }
100
101    /// Converts the `Scalar` into a `StringScalar`.
102    pub fn into_string(self) -> StringScalar {
103        if let Scalar::String(scalar) = self {
104            return scalar;
105        }
106        vortex_panic!("Cannot convert non-string Scalar into StringScalar");
107    }
108
109    /// Converts the `Scalar` into a `BinaryScalar`.
110    pub fn into_binary(self) -> BinaryScalar {
111        if let Scalar::Binary(scalar) = self {
112            return scalar;
113        }
114        vortex_panic!("Cannot convert non-binary Scalar into BinaryScalar");
115    }
116
117    /// Converts the `Scalar` into a `ListViewScalar`.
118    pub fn into_list(self) -> ListViewScalar {
119        if let Scalar::List(scalar) = self {
120            return scalar;
121        }
122        vortex_panic!("Cannot convert non-list Scalar into ListViewScalar");
123    }
124
125    /// Converts the `Scalar` into a `FixedSizeListScalar`.
126    pub fn into_fixed_size_list(self) -> FixedSizeListScalar {
127        if let Scalar::FixedSizeList(scalar) = self {
128            return scalar;
129        }
130        vortex_panic!("Cannot convert non-fixed-size-list Scalar into FixedSizeListScalar");
131    }
132
133    /// Converts the `Scalar` into a `StructScalar`.
134    pub fn into_struct(self) -> StructScalar {
135        if let Scalar::Struct(scalar) = self {
136            return scalar;
137        }
138        vortex_panic!("Cannot convert non-struct Scalar into StructScalar");
139    }
140}
141
142impl Scalar {
143    /// Converts the `Scalar` into a `NullScalar`.
144    pub fn as_null(&self) -> &NullScalar {
145        if let Scalar::Null(scalar) = self {
146            return scalar;
147        }
148        vortex_panic!("Cannot convert non-null Scalar into NullScalar");
149    }
150
151    /// Converts the `Scalar` into a `BoolScalar`.
152    pub fn as_bool(&self) -> &BoolScalar {
153        if let Scalar::Bool(scalar) = self {
154            return scalar;
155        }
156        vortex_panic!("Cannot convert non-bool Scalar into BoolScalar");
157    }
158
159    /// Converts the `Scalar` into a `DecimalScalar`.
160    pub fn as_decimal(&self) -> &DecimalScalar {
161        if let Scalar::Decimal(scalar) = self {
162            return scalar;
163        }
164        vortex_panic!("Cannot convert non-decimal Scalar into DecimalScalar");
165    }
166
167    /// Converts the `Scalar` into a `PrimitiveScalar`.
168    pub fn as_primitive(&self) -> &PrimitiveScalar {
169        if let Scalar::Primitive(scalar) = self {
170            return scalar;
171        }
172        vortex_panic!("Cannot convert non-primitive Scalar into PrimitiveScalar");
173    }
174
175    /// Converts the `Scalar` into a `StringScalar`.
176    pub fn as_string(&self) -> &StringScalar {
177        if let Scalar::String(scalar) = self {
178            return scalar;
179        }
180        vortex_panic!("Cannot convert non-string Scalar into StringScalar");
181    }
182
183    /// Converts the `Scalar` into a `BinaryScalar`.
184    pub fn as_binary(&self) -> &BinaryScalar {
185        if let Scalar::Binary(scalar) = self {
186            return scalar;
187        }
188        vortex_panic!("Cannot convert non-binary Scalar into BinaryScalar");
189    }
190
191    /// Converts the `Scalar` into a `ListViewScalar`.
192    pub fn as_list(&self) -> &ListViewScalar {
193        if let Scalar::List(scalar) = self {
194            return scalar;
195        }
196        vortex_panic!("Cannot convert non-list Scalar into ListViewScalar");
197    }
198
199    /// Converts the `Scalar` into a `FixedSizeListScalar`.
200    pub fn as_fixed_size_list(&self) -> &FixedSizeListScalar {
201        if let Scalar::FixedSizeList(scalar) = self {
202            return scalar;
203        }
204        vortex_panic!("Cannot convert non-fixed-size-list Scalar into FixedSizeListScalar");
205    }
206
207    /// Converts the `Scalar` into a `StructScalar`.
208    pub fn as_struct(&self) -> &StructScalar {
209        if let Scalar::Struct(scalar) = self {
210            return scalar;
211        }
212        vortex_panic!("Cannot convert non-struct Scalar into StructScalar");
213    }
214}