vortex_vector/primitive/
scalar.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::ops::Deref;
5
6use vortex_dtype::half::f16;
7use vortex_dtype::{NativePType, PTypeUpcast};
8
9use crate::primitive::{PVectorMut, PrimitiveVectorMut};
10use crate::{Scalar, ScalarOps, VectorMut, VectorMutOps};
11
12/// Represents a primitive scalar value.
13#[derive(Debug)]
14pub enum PrimitiveScalar {
15    /// 8-bit signed integer scalar
16    I8(PScalar<i8>),
17    /// 16-bit signed integer scalar
18    I16(PScalar<i16>),
19    /// 32-bit signed integer scalar
20    I32(PScalar<i32>),
21    /// 64-bit signed integer scalar
22    I64(PScalar<i64>),
23    /// 8-bit unsigned integer scalar
24    U8(PScalar<u8>),
25    /// 16-bit unsigned integer scalar
26    U16(PScalar<u16>),
27    /// 32-bit unsigned integer scalar
28    U32(PScalar<u32>),
29    /// 64-bit unsigned integer scalar
30    U64(PScalar<u64>),
31    /// 16-bit floating point scalar
32    F16(PScalar<f16>),
33    /// 32-bit floating point scalar
34    F32(PScalar<f32>),
35    /// 64-bit floating point scalar
36    F64(PScalar<f64>),
37}
38
39impl ScalarOps for PrimitiveScalar {
40    fn is_valid(&self) -> bool {
41        match self {
42            PrimitiveScalar::I8(v) => v.is_some(),
43            PrimitiveScalar::I16(v) => v.is_some(),
44            PrimitiveScalar::I32(v) => v.is_some(),
45            PrimitiveScalar::I64(v) => v.is_some(),
46            PrimitiveScalar::U8(v) => v.is_some(),
47            PrimitiveScalar::U16(v) => v.is_some(),
48            PrimitiveScalar::U32(v) => v.is_some(),
49            PrimitiveScalar::U64(v) => v.is_some(),
50            PrimitiveScalar::F16(v) => v.is_some(),
51            PrimitiveScalar::F32(v) => v.is_some(),
52            PrimitiveScalar::F64(v) => v.is_some(),
53        }
54    }
55
56    fn repeat(&self, _n: usize) -> VectorMut {
57        todo!()
58    }
59}
60
61impl From<PrimitiveScalar> for Scalar {
62    fn from(val: PrimitiveScalar) -> Self {
63        Scalar::Primitive(val)
64    }
65}
66
67/// Represents a primitive scalar value with a specific native primitive type.
68#[derive(Clone, Debug, PartialEq, Eq, Hash)]
69pub struct PScalar<T>(Option<T>);
70
71impl<T: NativePType> PScalar<T> {
72    /// Creates a new primitive scalar with the given value.
73    pub fn new(value: Option<T>) -> Self {
74        Self(value)
75    }
76}
77
78impl<T: NativePType> From<PScalar<T>> for PrimitiveScalar {
79    fn from(value: PScalar<T>) -> Self {
80        T::upcast(value)
81    }
82}
83
84impl<T: NativePType> ScalarOps for PScalar<T> {
85    fn is_valid(&self) -> bool {
86        self.0.is_some()
87    }
88
89    fn repeat(&self, n: usize) -> VectorMut {
90        let mut vec = PVectorMut::<T>::with_capacity(n);
91        match self.0 {
92            None => vec.append_nulls(n),
93            Some(v) => vec.append_values(v, n),
94        }
95        PrimitiveVectorMut::from(vec).into()
96    }
97}
98
99impl<T: NativePType> From<PScalar<T>> for Scalar {
100    fn from(value: PScalar<T>) -> Self {
101        Scalar::Primitive(T::upcast(value))
102    }
103}
104
105impl PTypeUpcast for PrimitiveScalar {
106    type Input<T: NativePType> = PScalar<T>;
107
108    fn from_u8(input: Self::Input<u8>) -> Self {
109        PrimitiveScalar::U8(input)
110    }
111
112    fn from_u16(input: Self::Input<u16>) -> Self {
113        PrimitiveScalar::U16(input)
114    }
115
116    fn from_u32(input: Self::Input<u32>) -> Self {
117        PrimitiveScalar::U32(input)
118    }
119
120    fn from_u64(input: Self::Input<u64>) -> Self {
121        PrimitiveScalar::U64(input)
122    }
123
124    fn from_i8(input: Self::Input<i8>) -> Self {
125        PrimitiveScalar::I8(input)
126    }
127
128    fn from_i16(input: Self::Input<i16>) -> Self {
129        PrimitiveScalar::I16(input)
130    }
131
132    fn from_i32(input: Self::Input<i32>) -> Self {
133        PrimitiveScalar::I32(input)
134    }
135
136    fn from_i64(input: Self::Input<i64>) -> Self {
137        PrimitiveScalar::I64(input)
138    }
139
140    fn from_f16(input: Self::Input<f16>) -> Self {
141        PrimitiveScalar::F16(input)
142    }
143
144    fn from_f32(input: Self::Input<f32>) -> Self {
145        PrimitiveScalar::F32(input)
146    }
147
148    fn from_f64(input: Self::Input<f64>) -> Self {
149        PrimitiveScalar::F64(input)
150    }
151}
152
153impl<T: NativePType> Deref for PScalar<T> {
154    type Target = Option<T>;
155
156    fn deref(&self) -> &Self::Target {
157        &self.0
158    }
159}