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