vortex_array/arrays/constant/vtable/
mod.rs1use std::fmt::Debug;
5
6use vortex_dtype::DType;
7use vortex_error::VortexResult;
8use vortex_error::vortex_bail;
9use vortex_error::vortex_ensure;
10use vortex_scalar::Scalar;
11use vortex_scalar::ScalarValue;
12use vortex_vector::ScalarOps;
13use vortex_vector::Vector;
14use vortex_vector::VectorMutOps;
15
16use crate::ArrayRef;
17use crate::EmptyMetadata;
18use crate::arrays::ConstantArray;
19use crate::arrays::constant::vtable::rules::PARENT_RULES;
20use crate::buffer::BufferHandle;
21use crate::executor::ExecutionCtx;
22use crate::serde::ArrayChildren;
23use crate::vtable;
24use crate::vtable::ArrayId;
25use crate::vtable::ArrayVTable;
26use crate::vtable::ArrayVTableExt;
27use crate::vtable::NotSupported;
28use crate::vtable::VTable;
29
30mod array;
31mod canonical;
32mod encode;
33mod operations;
34mod rules;
35mod validity;
36mod visitor;
37
38vtable!(Constant);
39
40#[derive(Debug)]
41pub struct ConstantVTable;
42
43impl VTable for ConstantVTable {
44 type Array = ConstantArray;
45
46 type Metadata = EmptyMetadata;
47
48 type ArrayVTable = Self;
49 type CanonicalVTable = Self;
50 type OperationsVTable = Self;
51 type ValidityVTable = Self;
52 type VisitorVTable = Self;
53 type ComputeVTable = NotSupported;
55 type EncodeVTable = Self;
56
57 fn id(&self) -> ArrayId {
58 ArrayId::new_ref("vortex.constant")
59 }
60
61 fn encoding(_array: &Self::Array) -> ArrayVTable {
62 ConstantVTable.as_vtable()
63 }
64
65 fn metadata(_array: &ConstantArray) -> VortexResult<Self::Metadata> {
66 Ok(EmptyMetadata)
67 }
68
69 fn serialize(_metadata: Self::Metadata) -> VortexResult<Option<Vec<u8>>> {
70 Ok(Some(vec![]))
71 }
72
73 fn deserialize(_buffer: &[u8]) -> VortexResult<Self::Metadata> {
74 Ok(EmptyMetadata)
75 }
76
77 fn build(
78 &self,
79 dtype: &DType,
80 len: usize,
81 _metadata: &Self::Metadata,
82 buffers: &[BufferHandle],
83 _children: &dyn ArrayChildren,
84 ) -> VortexResult<ConstantArray> {
85 if buffers.len() != 1 {
86 vortex_bail!("Expected 1 buffer, got {}", buffers.len());
87 }
88 let buffer = buffers[0].clone().try_to_bytes()?;
89 let sv = ScalarValue::from_protobytes(&buffer)?;
90 let scalar = Scalar::new(dtype.clone(), sv);
91 Ok(ConstantArray::new(scalar, len))
92 }
93
94 fn with_children(_array: &mut Self::Array, children: Vec<ArrayRef>) -> VortexResult<()> {
95 vortex_ensure!(
96 children.is_empty(),
97 "ConstantArray has no children, got {}",
98 children.len()
99 );
100 Ok(())
101 }
102
103 fn execute(array: &Self::Array, _ctx: &mut ExecutionCtx) -> VortexResult<Vector> {
104 Ok(array.scalar.to_vector_scalar().repeat(array.len).freeze())
105 }
106
107 fn reduce_parent(
108 array: &Self::Array,
109 parent: &ArrayRef,
110 child_idx: usize,
111 ) -> VortexResult<Option<ArrayRef>> {
112 PARENT_RULES.evaluate(array, parent, child_idx)
113 }
114}