vortex_array/arrays/constant/vtable/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_buffer::BufferHandle;
5use vortex_dtype::DType;
6use vortex_error::VortexResult;
7use vortex_error::vortex_bail;
8use vortex_scalar::Scalar;
9use vortex_scalar::ScalarValue;
10use vortex_vector::Vector;
11use vortex_vector::VectorMutOps;
12
13use crate::EmptyMetadata;
14use crate::arrays::ConstantArray;
15use crate::arrays::constant::vector::to_vector;
16use crate::execution::ExecutionCtx;
17use crate::serde::ArrayChildren;
18use crate::vtable;
19use crate::vtable::ArrayId;
20use crate::vtable::ArrayVTable;
21use crate::vtable::ArrayVTableExt;
22use crate::vtable::NotSupported;
23use crate::vtable::VTable;
24
25mod array;
26mod canonical;
27mod encode;
28mod operations;
29mod validity;
30mod visitor;
31
32vtable!(Constant);
33
34#[derive(Debug)]
35pub struct ConstantVTable;
36
37impl VTable for ConstantVTable {
38    type Array = ConstantArray;
39
40    type Metadata = EmptyMetadata;
41
42    type ArrayVTable = Self;
43    type CanonicalVTable = Self;
44    type OperationsVTable = Self;
45    type ValidityVTable = Self;
46    type VisitorVTable = Self;
47    // TODO(ngates): implement a compute kernel for elementwise operations
48    type ComputeVTable = NotSupported;
49    type EncodeVTable = Self;
50
51    fn id(&self) -> ArrayId {
52        ArrayId::new_ref("vortex.constant")
53    }
54
55    fn encoding(_array: &Self::Array) -> ArrayVTable {
56        ConstantVTable.as_vtable()
57    }
58
59    fn metadata(_array: &ConstantArray) -> VortexResult<Self::Metadata> {
60        Ok(EmptyMetadata)
61    }
62
63    fn serialize(_metadata: Self::Metadata) -> VortexResult<Option<Vec<u8>>> {
64        Ok(Some(vec![]))
65    }
66
67    fn deserialize(_buffer: &[u8]) -> VortexResult<Self::Metadata> {
68        Ok(EmptyMetadata)
69    }
70
71    fn build(
72        &self,
73        dtype: &DType,
74        len: usize,
75        _metadata: &Self::Metadata,
76        buffers: &[BufferHandle],
77        _children: &dyn ArrayChildren,
78    ) -> VortexResult<ConstantArray> {
79        if buffers.len() != 1 {
80            vortex_bail!("Expected 1 buffer, got {}", buffers.len());
81        }
82        let buffer = buffers[0].clone().try_to_bytes()?;
83        let sv = ScalarValue::from_protobytes(&buffer)?;
84        let scalar = Scalar::new(dtype.clone(), sv);
85        Ok(ConstantArray::new(scalar, len))
86    }
87
88    fn batch_execute(array: &Self::Array, _ctx: &mut ExecutionCtx) -> VortexResult<Vector> {
89        Ok(to_vector(array.scalar().clone(), array.len()).freeze())
90    }
91}