Skip to main content

vortex_layout/plan/plans/
segment_scan.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use vortex_array::EmptyMetadata;
5use vortex_array::dtype::DType;
6use vortex_buffer::ByteBuffer;
7use vortex_error::VortexResult;
8use vortex_session::registry::CachedId;
9use vortex_session::registry::ReadContext;
10
11use crate::plan::Plan;
12use crate::plan::PlanChildren;
13use crate::plan::PlanId;
14use crate::plan::PlanParts;
15use crate::plan::PlanVTable;
16use crate::plan::check_child_count;
17use crate::segments::SegmentId;
18
19/// Reads one serialized array segment.
20#[derive(Clone, Debug)]
21pub struct SegmentScan;
22
23/// Data needed to read and decode a single segment.
24#[derive(Clone, Debug)]
25pub struct SegmentScanData {
26    segment_id: SegmentId,
27    array_ctx: ReadContext,
28    array_tree: Option<ByteBuffer>,
29}
30
31/// A plan that reads one serialized array segment.
32pub type SegmentScanPlan = Plan<SegmentScan>;
33
34impl SegmentScanPlan {
35    /// Creates a segment scan over `segment_id`.
36    pub fn new(
37        dtype: DType,
38        row_count: u64,
39        segment_id: SegmentId,
40        array_ctx: ReadContext,
41        array_tree: Option<ByteBuffer>,
42    ) -> Self {
43        PlanParts {
44            vtable: SegmentScan,
45            dtype,
46            row_count,
47            children: PlanChildren::default(),
48            data: SegmentScanData {
49                segment_id,
50                array_ctx,
51                array_tree,
52            },
53        }
54        .into_typed()
55    }
56
57    /// Returns the segment this plan reads.
58    pub fn segment_id(&self) -> SegmentId {
59        self.data().segment_id
60    }
61
62    /// Returns the read context for the serialized array.
63    pub fn array_ctx(&self) -> &ReadContext {
64        &self.data().array_ctx
65    }
66
67    /// Returns the serialized array encoding tree, when it is stored out of line.
68    pub fn array_tree(&self) -> Option<&ByteBuffer> {
69        self.data().array_tree.as_ref()
70    }
71}
72
73impl PlanVTable for SegmentScan {
74    type PlanData = SegmentScanData;
75    type Metadata = EmptyMetadata;
76
77    fn id(&self) -> PlanId {
78        static ID: CachedId = CachedId::new("vortex.plan.segment_scan");
79        *ID
80    }
81
82    fn metadata(_plan: &Plan<Self>) -> Option<Self::Metadata> {
83        // The segment ID and read context are not yet covered by a metadata codec.
84        None
85    }
86
87    fn with_children(
88        _plan: &Plan<Self>,
89        children: &PlanChildren,
90        _data: &mut Self::PlanData,
91    ) -> VortexResult<()> {
92        check_child_count("SegmentScan", children, 0)?;
93        Ok(())
94    }
95}