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::binaryview::{BinaryScalar, StringScalar};
7use crate::bool::BoolScalar;
8use crate::decimal::DecimalScalar;
9use crate::fixed_size_list::FixedSizeListScalar;
10use crate::listview::ListViewScalar;
11use crate::null::NullScalar;
12use crate::primitive::PrimitiveScalar;
13use crate::struct_::StructScalar;
14use crate::{ScalarOps, VectorMut, match_each_scalar};
15
16/// Represents a scalar value of any supported type.
17#[derive(Debug)]
18pub enum Scalar {
19    /// Null scalars are always null.
20    Null(NullScalar),
21    /// Boolean scalars hold the boolean value in an Option, where None represents null.
22    Bool(BoolScalar),
23    /// Decimal scalars hold the decimal value in a DScalar, or else None for null.
24    Decimal(DecimalScalar),
25    /// Primitive scalars hold the primitive value in a PScalar, or else None for null.
26    Primitive(PrimitiveScalar),
27    /// String scalars hold the string data in a BufferString, or else None for null.
28    String(StringScalar),
29    /// Binary scalars hold the binary data in a ByteBuffer, or else None for null.
30    Binary(BinaryScalar),
31    /// Variable-size list scalars hold the list elements in a vector, or else None for null.
32    List(ListViewScalar),
33    /// Fixed-size list scalars hold the list elements in a vector, or else None for null.
34    FixedSizeList(FixedSizeListScalar),
35    /// Struct scalars are represented as a length-1 struct vector.
36    Struct(StructScalar),
37}
38
39impl ScalarOps for Scalar {
40    fn is_valid(&self) -> bool {
41        match_each_scalar!(self, |v| { v.is_valid() })
42    }
43
44    fn repeat(&self, n: usize) -> VectorMut {
45        match_each_scalar!(self, |v| { v.repeat(n) })
46    }
47}
48
49impl Scalar {
50    /// Converts the `Scalar` into a `NullScalar`.
51    pub fn into_null(self) -> NullScalar {
52        if let Scalar::Null(scalar) = self {
53            return scalar;
54        }
55        vortex_panic!("Cannot convert non-null Scalar into NullScalar");
56    }
57
58    /// Converts the `Scalar` into a `BoolScalar`.
59    pub fn into_bool(self) -> BoolScalar {
60        if let Scalar::Bool(scalar) = self {
61            return scalar;
62        }
63        vortex_panic!("Cannot convert non-bool Scalar into BoolScalar");
64    }
65
66    /// Converts the `Scalar` into a `DecimalScalar`.
67    pub fn into_decimal(self) -> DecimalScalar {
68        if let Scalar::Decimal(scalar) = self {
69            return scalar;
70        }
71        vortex_panic!("Cannot convert non-decimal Scalar into DecimalScalar");
72    }
73
74    /// Converts the `Scalar` into a `PrimitiveScalar`.
75    pub fn into_primitive(self) -> PrimitiveScalar {
76        if let Scalar::Primitive(scalar) = self {
77            return scalar;
78        }
79        vortex_panic!("Cannot convert non-primitive Scalar into PrimitiveScalar");
80    }
81
82    /// Converts the `Scalar` into a `StringScalar`.
83    pub fn into_string(self) -> StringScalar {
84        if let Scalar::String(scalar) = self {
85            return scalar;
86        }
87        vortex_panic!("Cannot convert non-string Scalar into StringScalar");
88    }
89
90    /// Converts the `Scalar` into a `BinaryScalar`.
91    pub fn into_binary(self) -> BinaryScalar {
92        if let Scalar::Binary(scalar) = self {
93            return scalar;
94        }
95        vortex_panic!("Cannot convert non-binary Scalar into BinaryScalar");
96    }
97
98    /// Converts the `Scalar` into a `ListViewScalar`.
99    pub fn into_list(self) -> ListViewScalar {
100        if let Scalar::List(scalar) = self {
101            return scalar;
102        }
103        vortex_panic!("Cannot convert non-list Scalar into ListViewScalar");
104    }
105
106    /// Converts the `Scalar` into a `FixedSizeListScalar`.
107    pub fn into_fixed_size_list(self) -> FixedSizeListScalar {
108        if let Scalar::FixedSizeList(scalar) = self {
109            return scalar;
110        }
111        vortex_panic!("Cannot convert non-fixed-size-list Scalar into FixedSizeListScalar");
112    }
113
114    /// Converts the `Scalar` into a `StructScalar`.
115    pub fn into_struct(self) -> StructScalar {
116        if let Scalar::Struct(scalar) = self {
117            return scalar;
118        }
119        vortex_panic!("Cannot convert non-struct Scalar into StructScalar");
120    }
121}