vortex_scan/plan/builtins/row_idx/
mod.rs1pub mod expr;
5
6use std::ops::Range;
7
8use vortex_array::EmptyMetadata;
9use vortex_array::IntoArray;
10use vortex_array::dtype::DType;
11use vortex_array::dtype::Nullability;
12use vortex_array::dtype::PType;
13use vortex_array::dtype::StructFields;
14use vortex_buffer::Buffer;
15use vortex_error::VortexResult;
16use vortex_expr::BoundExpression;
17use vortex_expr::Expression;
18use vortex_mask::Mask;
19use vortex_session::VortexSession;
20use vortex_session::registry::CachedId;
21
22use self::expr::RowIdx as RowIdxFn;
23use crate::execution::operators::OnceSource;
24use crate::execution::pipeline::Chain;
25use crate::execution::pipeline::Compiler;
26use crate::plan::EvalPlan;
27use crate::plan::PackPlan;
28use crate::plan::Plan;
29use crate::plan::PlanId;
30use crate::plan::PlanParts;
31use crate::plan::PlanRef;
32use crate::plan::PlanSlots;
33use crate::plan::PlanVTable;
34use crate::plan::partition::PartitionRule;
35use crate::plan::partition::PartitionSession;
36use crate::plan::partition::PartitionedExpression;
37use crate::plan::partition::PartitionedPlan;
38use crate::plan::partition::Partitions;
39use crate::plan::partition::PlanSlot;
40use crate::plan::partition::Slots;
41use crate::plan::partition::positional_name;
42
43#[derive(Clone, Debug)]
45pub struct RowIdx;
46
47#[derive(Clone, Debug)]
49pub struct RowIdxData;
50
51pub type RowIdxPlan = Plan<RowIdx>;
53
54impl RowIdxPlan {
55 pub fn new(row_count: u64) -> Self {
57 PlanParts {
58 vtable: RowIdx,
59 dtype: row_idx_dtype(),
60 row_count,
61 slots: PlanSlots::default(),
62 data: RowIdxData,
63 }
64 .into_typed()
65 }
66}
67
68fn row_idx_dtype() -> DType {
70 DType::Primitive(PType::U64, Nullability::NonNullable)
71}
72
73impl PlanVTable for RowIdx {
74 type PlanData = RowIdxData;
75 type Metadata = EmptyMetadata;
76
77 fn id(&self) -> PlanId {
78 static ID: CachedId = CachedId::new("vortex.plan.row_idx");
79 *ID
80 }
81
82 fn metadata(_plan: &Plan<Self>) -> Option<Self::Metadata> {
83 Some(EmptyMetadata)
84 }
85
86 fn with_slots(
87 _plan: &Plan<Self>,
88 slots: &PlanSlots,
89 _data: &mut Self::PlanData,
90 ) -> VortexResult<()> {
91 slots.check_len("RowIdx", 0)
92 }
93
94 fn compile(
95 plan: &Plan<Self>,
96 rows: Range<u64>,
97 mask: &Mask,
98 compiler: &mut Compiler<'_>,
99 ) -> VortexResult<Option<Chain>> {
100 let _ = plan;
101 if mask.all_false() {
102 return Ok(None);
103 }
104 let offset = compiler.row_offset();
105 let indices = Buffer::from_iter(rows.start + offset..rows.end + offset).into_array();
106 let indices = if mask.all_true() {
107 indices
108 } else {
109 indices.filter(mask.clone())?
110 };
111 Ok(Some(Chain::new(OnceSource::new(indices))))
112 }
113}
114
115#[derive(Clone, Copy)]
116pub enum RowIdxSlot {
117 Child,
118 RowIdx,
119}
120impl From<RowIdxSlot> for PlanSlot {
121 fn from(slot: RowIdxSlot) -> Self {
122 match slot {
123 RowIdxSlot::Child => 0,
124 RowIdxSlot::RowIdx => 1,
125 }
126 }
127}
128
129impl PartitionedPlan for RowIdx {
130 type Slot = RowIdxSlot;
131 fn slot_dtype(scope: &DType, slot: RowIdxSlot) -> VortexResult<DType> {
132 Ok(match slot {
133 RowIdxSlot::Child => scope.clone(),
134 RowIdxSlot::RowIdx => row_idx_dtype(),
135 })
136 }
137}
138
139pub fn register_row_idx_partition_rules(session: &PartitionSession) {
141 session.register_root_rule(RowIdx, &RootRule);
142 session.register_rule(RowIdx, RowIdxFn, &RowIdxFnRule);
143}
144
145struct RootRule;
147impl PartitionRule for RootRule {
148 type Plan = RowIdx;
149 fn apply(
150 &self,
151 slots: &Slots<'_, RowIdx>,
152 _node: &BoundExpression,
153 ) -> VortexResult<Option<Expression>> {
154 Ok(Some(slots.read(RowIdxSlot::Child)))
155 }
156}
157
158struct RowIdxFnRule;
160impl PartitionRule for RowIdxFnRule {
161 type Plan = RowIdx;
162 fn apply(
163 &self,
164 slots: &Slots<'_, RowIdx>,
165 _node: &BoundExpression,
166 ) -> VortexResult<Option<Expression>> {
167 Ok(Some(slots.read(RowIdxSlot::RowIdx)))
168 }
169}
170
171pub(crate) fn plan_row_idx_expression(
177 expression: BoundExpression,
178 child: PlanRef,
179 session: &VortexSession,
180) -> VortexResult<PlanRef> {
181 let row_count = child.row_count();
182 let partitioned = match RowIdx.partition(child.dtype(), &expression, session)? {
183 PartitionedExpression::One(slot, expr) => {
184 let source = if slot == PlanSlot::from(RowIdxSlot::RowIdx) {
185 RowIdxPlan::new(row_count).into_plan()
186 } else {
187 child
188 };
189 return Ok(EvalPlan::try_new(expr, source)?.into_plan());
190 }
191 PartitionedExpression::Any(expr) => {
192 let source = PackPlan::try_new(
193 StructFields::empty(),
194 Nullability::NonNullable,
195 row_count,
196 Vec::new(),
197 None,
198 )?;
199 return Ok(EvalPlan::try_new(expr, source.into_plan())?.into_plan());
200 }
201 PartitionedExpression::Many(partitioned) => partitioned,
202 };
203 let Partitions {
204 combiner: root,
205 partitions,
206 } = partitioned;
207
208 let mut plans = Vec::with_capacity(partitions.len());
209 for partition in partitions {
210 let source = if partition.slot == PlanSlot::from(RowIdxSlot::RowIdx) {
211 RowIdxPlan::new(row_count).into_plan()
212 } else {
213 child.clone()
214 };
215 plans.push(EvalPlan::try_new(partition.expr, source)?.into_plan());
216 }
217
218 let fields = StructFields::from_iter(
219 plans
220 .iter()
221 .enumerate()
222 .map(|(position, plan)| (positional_name(position), plan.dtype().clone())),
223 );
224 let partitions = PackPlan::try_new(fields, Nullability::NonNullable, row_count, plans, None)?;
225
226 Ok(EvalPlan::try_new(root, partitions.into_plan())?.into_plan())
227}