Skip to main content

vortex_scan/plan/
vtable.rs

1// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright the Vortex contributors
3
4use std::borrow::Cow;
5use std::fmt;
6use std::fmt::Debug;
7use std::ops::Range;
8
9use vortex_array::DeserializeMetadata;
10use vortex_array::SerializeMetadata;
11use vortex_error::VortexResult;
12use vortex_error::vortex_bail;
13use vortex_mask::Mask;
14use vortex_session::VortexSession;
15use vortex_session::registry::Id;
16
17use crate::execution::pipeline::Chain;
18use crate::execution::pipeline::Compiler;
19use crate::plan::PlanRef;
20use crate::plan::PlanSlots;
21use crate::plan::typed::Plan;
22
23/// A unique identifier for a plan operator.
24pub type PlanId = Id;
25
26/// Operator-specific behavior for a typed [`Plan`].
27///
28/// Common fields — dtype, row count, and child slots — are stored outside the erased operator
29/// data. Implementations own only their operator-specific data, its metadata codec, and a callback
30/// for refreshing cached data after generic slot replacement.
31///
32/// Each operator fixes what every slot position holds. An absent child is a `None` slot, so the
33/// positions of the others never depend on which children are present.
34///
35/// Operators describe physical work over a row domain. Their identity and operator-specific data
36/// do not depend on the source layout kind. A lazy slot may nevertheless own a hidden
37/// source-layout reference used for on-demand lowering.
38pub trait PlanVTable: 'static + Clone + Sized + Send + Sync + Debug {
39    /// Operator-specific data, excluding children.
40    ///
41    /// Children belong in [`PlanParts::slots`](crate::plan::PlanParts::slots) so that
42    /// traversal and rewriting can discover them generically.
43    type PlanData: 'static + Send + Sync + Clone + Debug;
44
45    /// Serialized form of [`PlanData`](Self::PlanData).
46    type Metadata: SerializeMetadata + DeserializeMetadata + Debug;
47
48    /// Returns the globally unique operator ID.
49    fn id(&self) -> PlanId;
50
51    /// Writes operator-specific fields after the plan's standard display summary.
52    fn fmt(plan: &Plan<Self>, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
53        let _ = (plan, formatter);
54        Ok(())
55    }
56
57    /// Returns the serializable metadata for this operator.
58    ///
59    /// Returns `None` when the operator holds state that cannot be serialized.
60    fn metadata(plan: &Plan<Self>) -> Option<Self::Metadata>;
61
62    /// Refreshes cloned operator data after the slots are replaced.
63    ///
64    /// The plan layer clones [`PlanData`](Self::PlanData), replaces the slots externally, and
65    /// invokes this callback. Implementations validate the new slots and update any derived
66    /// values in `data`; they do not rebuild the plan itself.
67    fn with_slots(
68        plan: &Plan<Self>,
69        slots: &PlanSlots,
70        data: &mut Self::PlanData,
71    ) -> VortexResult<()> {
72        let _ = (plan, slots, data);
73        Ok(())
74    }
75
76    /// Returns the display name of `slot`.
77    fn slot_name(plan: &Plan<Self>, slot: usize) -> Cow<'_, str> {
78        let _ = plan;
79        Cow::Owned(format!("slot[{slot}]"))
80    }
81
82    /// Whether this node reads payload data and can apply a row mask at the source.
83    ///
84    /// Metadata-only pruning rejects these nodes outside pruning subplans. A Filter directly
85    /// above such a node forwards its mask to the node's compiler instead of adding a mask stage.
86    fn is_data_source(_plan: &Plan<Self>) -> bool {
87        false
88    }
89
90    /// Applies an adapter-owned rewrite before optimizing this node's children.
91    fn reduce(_plan: &Plan<Self>, _session: &VortexSession) -> VortexResult<Option<PlanRef>> {
92        Ok(None)
93    }
94
95    /// Rewrites a parent using format-specific knowledge of this child.
96    fn reduce_parent(
97        _plan: &Plan<Self>,
98        _parent: &PlanRef,
99        _slot: usize,
100        _session: &VortexSession,
101    ) -> VortexResult<Option<PlanRef>> {
102        Ok(None)
103    }
104
105    /// Compiles this plan over `rows` of its row domain, restricted to `mask`, into a chain:
106    /// a source and the stages after it, producing the rows `mask` selects in row order, or
107    /// `None` when that is no row. `rows` lies within the plan's row domain and `mask` is as long
108    /// as `rows`.
109    ///
110    /// A plan compiles its children through `compiler` and either adds stages to a child's
111    /// chain or joins several children's chains with a source of its own. Compiling does no IO.
112    ///
113    /// Pipelines have no absent inputs: a plan that joins its children compiles
114    /// [`Plan::children`], the present slots in slot order, so inlet `i` of its source reads the
115    /// `i`-th present slot.
116    fn compile(
117        plan: &Plan<Self>,
118        rows: Range<u64>,
119        mask: &Mask,
120        compiler: &mut Compiler<'_>,
121    ) -> VortexResult<Option<Chain>> {
122        let _ = (rows, mask, compiler);
123        vortex_bail!("Plan {} has no pipeline implementation", plan.vtable().id())
124    }
125}