vortex_vector/struct_/
scalar.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use crate::Scalar;
5use crate::ScalarOps;
6use crate::VectorMut;
7use crate::VectorOps;
8use crate::struct_::StructVector;
9use vortex_mask::Mask;
10
11/// Represents a struct scalar value.
12///
13/// The inner value is a StructVector with length 1.
14#[derive(Clone, Debug)]
15pub struct StructScalar(StructVector);
16
17impl StructScalar {
18    /// Creates a new StructScalar from a length-1 StructVector.
19    ///
20    /// # Panics
21    ///
22    /// Panics if the input vector does not have length 1.
23    pub fn new(vector: StructVector) -> Self {
24        assert_eq!(vector.len(), 1);
25        Self(vector)
26    }
27
28    /// Returns the inner length-1 vector representing the struct scalar.
29    pub fn value(&self) -> &StructVector {
30        &self.0
31    }
32
33    /// Returns the nth field scalar of the struct.
34    pub fn field(&self, field_idx: usize) -> Scalar {
35        self.0.fields()[field_idx].scalar_at(0)
36    }
37}
38
39impl ScalarOps for StructScalar {
40    fn is_valid(&self) -> bool {
41        self.0.validity().value(0)
42    }
43
44    fn mask_validity(&mut self, mask: bool) {
45        if !mask {
46            self.0.mask_validity(&Mask::new_false(1))
47        }
48    }
49
50    fn repeat(&self, _n: usize) -> VectorMut {
51        todo!()
52    }
53}
54
55impl From<StructScalar> for Scalar {
56    fn from(val: StructScalar) -> Self {
57        Scalar::Struct(val)
58    }
59}