1use 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#[derive(Debug)]
18pub enum Scalar {
19 Null(NullScalar),
21 Bool(BoolScalar),
23 Decimal(DecimalScalar),
25 Primitive(PrimitiveScalar),
27 String(StringScalar),
29 Binary(BinaryScalar),
31 List(ListViewScalar),
33 FixedSizeList(FixedSizeListScalar),
35 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 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 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 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 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 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 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 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 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 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}