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::sync::Arc;
8
9use vortex_array::{ArrayContext, DeserializeMetadata, EmptyMetadata};
10use vortex_dtype::DType;
11use vortex_error::{VortexResult, vortex_bail, vortex_panic};
12
13use crate::children::LayoutChildren;
14use crate::layouts::flat::reader::FlatReader;
15use crate::segments::{SegmentId, SegmentSource};
16use crate::{
17    LayoutChildType, LayoutEncodingRef, LayoutId, LayoutReaderRef, LayoutRef, VTable, vtable,
18};
19
20vtable!(Flat);
21
22impl VTable for FlatVTable {
23    type Layout = FlatLayout;
24    type Encoding = FlatLayoutEncoding;
25    type Metadata = EmptyMetadata;
26
27    fn id(_encoding: &Self::Encoding) -> LayoutId {
28        LayoutId::new_ref("vortex.flat")
29    }
30
31    fn encoding(_layout: &Self::Layout) -> LayoutEncodingRef {
32        LayoutEncodingRef::new_ref(FlatLayoutEncoding.as_ref())
33    }
34
35    fn row_count(layout: &Self::Layout) -> u64 {
36        layout.row_count
37    }
38
39    fn dtype(layout: &Self::Layout) -> &DType {
40        &layout.dtype
41    }
42
43    fn metadata(_layout: &Self::Layout) -> Self::Metadata {
44        EmptyMetadata
45    }
46
47    fn segment_ids(layout: &Self::Layout) -> Vec<SegmentId> {
48        vec![layout.segment_id]
49    }
50
51    fn nchildren(_layout: &Self::Layout) -> usize {
52        0
53    }
54
55    fn child(_layout: &Self::Layout, _idx: usize) -> VortexResult<LayoutRef> {
56        vortex_bail!("Flat layout has no children");
57    }
58
59    fn child_type(_layout: &Self::Layout, _idx: usize) -> LayoutChildType {
60        vortex_panic!("Flat layout has no children");
61    }
62
63    fn new_reader(
64        layout: &Self::Layout,
65        name: Arc<str>,
66        segment_source: Arc<dyn SegmentSource>,
67    ) -> VortexResult<LayoutReaderRef> {
68        Ok(Arc::new(FlatReader::new(
69            layout.clone(),
70            name,
71            segment_source,
72        )))
73    }
74
75    fn build(
76        _encoding: &Self::Encoding,
77        dtype: &DType,
78        row_count: u64,
79        _metadata: &<Self::Metadata as DeserializeMetadata>::Output,
80        segment_ids: Vec<SegmentId>,
81        _children: &dyn LayoutChildren,
82        ctx: ArrayContext,
83    ) -> VortexResult<Self::Layout> {
84        if segment_ids.len() != 1 {
85            vortex_bail!("Flat layout must have exactly one segment ID");
86        }
87        Ok(FlatLayout {
88            row_count,
89            dtype: dtype.clone(),
90            segment_id: segment_ids[0],
91            ctx,
92        })
93    }
94}
95
96#[derive(Debug)]
97pub struct FlatLayoutEncoding;
98
99#[derive(Clone, Debug)]
100pub struct FlatLayout {
101    row_count: u64,
102    dtype: DType,
103    segment_id: SegmentId,
104    ctx: ArrayContext,
105}
106
107impl FlatLayout {
108    pub fn new(row_count: u64, dtype: DType, segment_id: SegmentId, ctx: ArrayContext) -> Self {
109        Self {
110            row_count,
111            dtype,
112            segment_id,
113            ctx,
114        }
115    }
116
117    pub fn segment_id(&self) -> SegmentId {
118        self.segment_id
119    }
120}