Skip to main content

vortex_layout/layouts/flat/
mod.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4mod reader;
5pub mod writer;
6
7use std::env;
8use std::sync::Arc;
9
10use vortex_array::ArrayContext;
11use vortex_array::DeserializeMetadata;
12use vortex_array::ProstMetadata;
13use vortex_buffer::ByteBuffer;
14use vortex_dtype::DType;
15use vortex_error::VortexResult;
16use vortex_error::vortex_bail;
17use vortex_error::vortex_panic;
18use vortex_session::VortexSession;
19
20use crate::LayoutChildType;
21use crate::LayoutEncodingRef;
22use crate::LayoutId;
23use crate::LayoutReaderRef;
24use crate::LayoutRef;
25use crate::VTable;
26use crate::children::LayoutChildren;
27use crate::layouts::flat::reader::FlatReader;
28use crate::segments::SegmentId;
29use crate::segments::SegmentSource;
30use crate::vtable;
31
32/// Check if inline array node is enabled.
33/// This checks the env var each time to allow tests to toggle the behavior.
34pub(super) fn flat_layout_inline_array_node() -> bool {
35    env::var("FLAT_LAYOUT_INLINE_ARRAY_NODE").is_ok()
36}
37
38vtable!(Flat);
39
40impl VTable for FlatVTable {
41    type Layout = FlatLayout;
42    type Encoding = FlatLayoutEncoding;
43    type Metadata = ProstMetadata<FlatLayoutMetadata>;
44
45    fn id(_encoding: &Self::Encoding) -> LayoutId {
46        LayoutId::new_ref("vortex.flat")
47    }
48
49    fn encoding(_layout: &Self::Layout) -> LayoutEncodingRef {
50        LayoutEncodingRef::new_ref(FlatLayoutEncoding.as_ref())
51    }
52
53    fn row_count(layout: &Self::Layout) -> u64 {
54        layout.row_count
55    }
56
57    fn dtype(layout: &Self::Layout) -> &DType {
58        &layout.dtype
59    }
60
61    fn metadata(layout: &Self::Layout) -> Self::Metadata {
62        ProstMetadata(FlatLayoutMetadata {
63            array_encoding_tree: layout.array_tree.as_ref().map(|bytes| bytes.to_vec()),
64        })
65    }
66
67    fn segment_ids(layout: &Self::Layout) -> Vec<SegmentId> {
68        vec![layout.segment_id]
69    }
70
71    fn nchildren(_layout: &Self::Layout) -> usize {
72        0
73    }
74
75    fn child(_layout: &Self::Layout, _idx: usize) -> VortexResult<LayoutRef> {
76        vortex_bail!("Flat layout has no children");
77    }
78
79    fn child_type(_layout: &Self::Layout, _idx: usize) -> LayoutChildType {
80        vortex_panic!("Flat layout has no children");
81    }
82
83    fn new_reader(
84        layout: &Self::Layout,
85        name: Arc<str>,
86        segment_source: Arc<dyn SegmentSource>,
87        session: &VortexSession,
88    ) -> VortexResult<LayoutReaderRef> {
89        Ok(Arc::new(FlatReader::new(
90            layout.clone(),
91            name,
92            segment_source,
93            session.clone(),
94        )))
95    }
96
97    fn build(
98        _encoding: &Self::Encoding,
99        dtype: &DType,
100        row_count: u64,
101        metadata: &<Self::Metadata as DeserializeMetadata>::Output,
102        segment_ids: Vec<SegmentId>,
103        _children: &dyn LayoutChildren,
104        ctx: &ArrayContext,
105    ) -> VortexResult<Self::Layout> {
106        if segment_ids.len() != 1 {
107            vortex_bail!("Flat layout must have exactly one segment ID");
108        }
109        Ok(FlatLayout::new_with_metadata(
110            row_count,
111            dtype.clone(),
112            segment_ids[0],
113            ctx.clone(),
114            metadata
115                .array_encoding_tree
116                .as_ref()
117                .map(|v| ByteBuffer::from(v.clone())),
118        ))
119    }
120
121    fn with_children(_layout: &mut Self::Layout, children: Vec<LayoutRef>) -> VortexResult<()> {
122        if !children.is_empty() {
123            vortex_bail!("Flat layout has no children, got {}", children.len());
124        }
125        Ok(())
126    }
127}
128
129#[derive(Debug)]
130pub struct FlatLayoutEncoding;
131
132#[derive(Clone, Debug)]
133pub struct FlatLayout {
134    row_count: u64,
135    dtype: DType,
136    segment_id: SegmentId,
137    ctx: ArrayContext,
138    array_tree: Option<ByteBuffer>,
139}
140
141impl FlatLayout {
142    pub fn new(row_count: u64, dtype: DType, segment_id: SegmentId, ctx: ArrayContext) -> Self {
143        Self {
144            row_count,
145            dtype,
146            segment_id,
147            ctx,
148            array_tree: None,
149        }
150    }
151
152    pub fn new_with_metadata(
153        row_count: u64,
154        dtype: DType,
155        segment_id: SegmentId,
156        ctx: ArrayContext,
157        metadata: Option<ByteBuffer>,
158    ) -> Self {
159        Self {
160            row_count,
161            dtype,
162            segment_id,
163            ctx,
164            array_tree: metadata,
165        }
166    }
167
168    #[inline]
169    pub fn segment_id(&self) -> SegmentId {
170        self.segment_id
171    }
172
173    #[inline]
174    pub fn array_ctx(&self) -> &ArrayContext {
175        &self.ctx
176    }
177
178    #[inline]
179    pub fn array_tree(&self) -> Option<&ByteBuffer> {
180        self.array_tree.as_ref()
181    }
182}
183
184#[derive(prost::Message)]
185pub struct FlatLayoutMetadata {
186    // We can optionally store the array encoding tree here to avoid needing to fetch the segment
187    // to plan array deserialization.
188    // This will be a `ArrayNode`.
189    #[prost(optional, bytes, tag = "1")]
190    pub array_encoding_tree: Option<Vec<u8>>,
191}