vortex-scan 0.95.0

Scanning operations for Vortex
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

use std::any::Any;
use std::borrow::Cow;
use std::fmt;
use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Formatter;
use std::hash::Hash;
use std::hash::Hasher;
use std::marker::PhantomData;
use std::ops::Deref;
use std::ops::Range;
use std::sync::Arc;

use vortex_array::SerializeMetadata;
use vortex_array::dtype::DType;
use vortex_error::VortexExpect;
use vortex_error::VortexResult;
use vortex_error::vortex_ensure;
use vortex_mask::Mask;
use vortex_session::VortexSession;

use crate::execution::pipeline::Chain;
use crate::execution::pipeline::Compiler;
use crate::plan::PlanId;
use crate::plan::PlanSlots;
use crate::plan::PlanVTable;
use crate::plan::display::PlanTreeDisplay;

/// The combined allocation behind [`PlanRef`].
///
/// Common plan state is stored before the unsized `data` tail, so reading the operator ID, dtype,
/// row count, or slots does not dispatch through the operator vtable. Only `PlanData<V>` is
/// erased to [`DynPlan`].
struct PlanInner<D: ?Sized> {
    id: PlanId,
    dtype: DType,
    row_count: u64,
    slots: PlanSlots,
    data: D, // must be last for unsized coercion
}

/// Shared, erased handle to a plan operator.
#[derive(Clone)]
pub struct PlanRef(Arc<PlanInner<dyn DynPlan>>);

impl PlanRef {
    fn from_inner<V: PlanVTable>(inner: Arc<PlanInner<PlanData<V>>>) -> Self {
        let inner: Arc<PlanInner<dyn DynPlan>> = inner;
        Self(inner)
    }

    fn dyn_plan(&self) -> &dyn DynPlan {
        &self.0.data
    }

    /// Whether this node reads payload data.
    pub(crate) fn is_data_source(&self) -> bool {
        self.dyn_plan().dyn_is_data_source(self)
    }

    pub(crate) fn reduce(&self, session: &VortexSession) -> VortexResult<Option<PlanRef>> {
        self.dyn_plan().dyn_reduce(self, session)
    }

    pub(crate) fn reduce_parent(
        &self,
        parent: &PlanRef,
        slot: usize,
        session: &VortexSession,
    ) -> VortexResult<Option<PlanRef>> {
        self.dyn_plan()
            .dyn_reduce_parent(self, parent, slot, session)
    }

    /// Returns whether two references point at the same plan.
    pub fn ptr_eq(lhs: &Self, rhs: &Self) -> bool {
        Arc::ptr_eq(&lhs.0, &rhs.0)
    }

    /// Returns the operator ID.
    pub fn id(&self) -> PlanId {
        self.0.id
    }

    /// Returns the dtype produced by this plan.
    pub fn dtype(&self) -> &DType {
        &self.0.dtype
    }

    /// Returns the number of rows in this plan's row domain.
    pub fn row_count(&self) -> u64 {
        self.0.row_count
    }

    /// Returns the child slots without building any child.
    pub fn slots(&self) -> &PlanSlots {
        &self.0.slots
    }

    /// Returns the number of slots, present or absent, without building any child.
    pub fn nslots(&self) -> usize {
        self.0.slots.len()
    }

    /// Returns the plan in `slot`, building it on first access, or `None` when the slot is
    /// absent. Errors when `slot` is out of bounds.
    pub fn slot(&self, slot: usize) -> VortexResult<Option<PlanRef>> {
        Ok(self.0.slots.get(slot)?.cloned())
    }

    /// Returns the plan in `slot`, or an error when the slot is absent or out of bounds.
    pub fn slot_required(&self, slot: usize) -> VortexResult<PlanRef> {
        self.0.slots.required(slot).cloned()
    }

    /// The plans of the present slots in slot order, building each as it is visited. See
    /// [`PlanSlots::children`].
    pub fn children(&self) -> impl Iterator<Item = VortexResult<&PlanRef>> + '_ {
        self.0.slots.children()
    }

    /// Rebuilds this plan with `slots` stored outside its erased operator data.
    pub fn with_slots(&self, slots: impl Into<PlanSlots>) -> VortexResult<PlanRef> {
        self.dyn_plan().dyn_with_slots(self, slots.into())
    }

    /// Returns the display name of `slot`.
    pub fn slot_name(&self, slot: usize) -> Cow<'_, str> {
        self.dyn_plan().dyn_slot_name(self, slot)
    }

    /// Serializes operator-specific metadata, or `None` when the operator is not serializable.
    pub fn metadata(&self) -> Option<Vec<u8>> {
        self.dyn_plan().dyn_metadata(self)
    }

    /// Returns whether this plan uses vtable `V`.
    pub fn is<V: PlanVTable>(&self) -> bool {
        self.dyn_plan().as_any().is::<PlanData<V>>()
    }

    /// Downcasts this plan to vtable `V`.
    pub fn as_<V: PlanVTable>(&self) -> &Plan<V> {
        self.as_opt::<V>().vortex_expect("Failed to downcast")
    }

    /// Attempts to borrow this plan as a typed handle for vtable `V`.
    pub fn as_opt<V: PlanVTable>(&self) -> Option<&Plan<V>> {
        if !self.is::<V>() {
            return None;
        }

        // SAFETY: Plan<V> is transparent over PlanRef, and the type check above proves that its
        // erased tail contains PlanData<V>.
        Some(unsafe { &*(std::ptr::from_ref(self).cast::<Plan<V>>()) })
    }

    /// Compiles this plan over `rows` of its row domain, restricted to `mask`. See
    /// [`PlanVTable::compile`].
    pub fn compile(
        &self,
        rows: Range<u64>,
        mask: &Mask,
        compiler: &mut Compiler<'_>,
    ) -> VortexResult<Option<Chain>> {
        vortex_ensure!(
            rows.start <= rows.end && rows.end <= self.row_count(),
            "Compile rows {rows:?} exceed plan row count {}",
            self.row_count()
        );
        vortex_ensure!(
            mask.len() as u64 == rows.end - rows.start,
            "Compile mask length {} does not match rows {rows:?}",
            mask.len()
        );
        self.dyn_plan().dyn_compile(self, rows, mask, compiler)
    }

    /// Displays this plan and its descendants with the default plan extractors.
    pub fn display_tree(&self) -> PlanTreeDisplay<'_> {
        PlanTreeDisplay::default_display(self)
    }

    /// Creates a composable tree display with no extractors.
    pub fn tree_display_builder(&self) -> PlanTreeDisplay<'_> {
        PlanTreeDisplay::new(self)
    }
}

impl Display for PlanRef {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
        write!(
            formatter,
            "{}({}, rows={})",
            self.id(),
            self.dtype(),
            self.row_count()
        )?;
        self.dyn_plan().dyn_fmt(self, formatter)
    }
}

impl Debug for PlanRef {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("Plan")
            .field("id", &self.0.id)
            .field("dtype", &self.0.dtype)
            .field("row_count", &self.0.row_count)
            .field("slots", &self.0.slots)
            .field("data", &&self.0.data)
            .finish()
    }
}

/// Pieces used to construct a typed plan.
pub struct PlanParts<V: PlanVTable> {
    /// The vtable identifying the operator.
    pub vtable: V,
    /// Logical dtype produced by the operator.
    pub dtype: DType,
    /// Number of rows in the operator's row domain.
    pub row_count: u64,
    /// Child slots, at the positions the operator defines.
    pub slots: PlanSlots,
    /// Operator-specific, non-child data.
    pub data: V::PlanData,
}

impl<V: PlanVTable> PlanParts<V> {
    /// Converts these parts into a typed plan.
    pub fn into_typed(self) -> Plan<V> {
        Plan::from_parts(self)
    }

    /// Erases these parts into a plan reference.
    pub fn into_plan(self) -> PlanRef {
        self.into_typed().into_plan()
    }
}

/// A plan keyed by identity: two keys are equal only when they hold the same plan, as
/// [`PlanRef::ptr_eq`] decides.
///
/// The key holds the plan, so its address cannot be freed and reused by another plan while the
/// key exists.
#[derive(Clone, Debug)]
pub(crate) struct PlanByPtr(pub(crate) PlanRef);

impl PartialEq for PlanByPtr {
    fn eq(&self, other: &Self) -> bool {
        PlanRef::ptr_eq(&self.0, &other.0)
    }
}

impl Eq for PlanByPtr {}

impl Hash for PlanByPtr {
    fn hash<H: Hasher>(&self, state: &mut H) {
        Arc::as_ptr(&self.0.0).cast::<()>().hash(state);
    }
}

/// A typed, shared handle to a plan operator.
#[repr(transparent)]
pub struct Plan<V: PlanVTable> {
    inner: PlanRef,
    _vtable: PhantomData<V>,
}

impl<V: PlanVTable> Plan<V> {
    /// Constructs a plan from explicit parts.
    pub fn from_parts(parts: PlanParts<V>) -> Self {
        let inner = Arc::new(PlanInner {
            id: parts.vtable.id(),
            dtype: parts.dtype,
            row_count: parts.row_count,
            slots: parts.slots,
            data: PlanData {
                vtable: parts.vtable,
                data: parts.data,
            },
        });
        Self {
            inner: PlanRef::from_inner(inner),
            _vtable: PhantomData,
        }
    }

    fn typed_data(&self) -> &PlanData<V> {
        self.inner
            .dyn_plan()
            .as_any()
            .downcast_ref::<PlanData<V>>()
            .vortex_expect("Typed plan contains the wrong vtable")
    }

    /// Returns the vtable.
    pub fn vtable(&self) -> &V {
        &self.typed_data().vtable
    }

    /// Returns operator-specific data.
    pub fn data(&self) -> &V::PlanData {
        &self.typed_data().data
    }

    /// Returns the dtype produced by this plan.
    pub fn dtype(&self) -> &DType {
        self.inner.dtype()
    }

    /// Returns the number of rows in this plan's row domain.
    pub fn row_count(&self) -> u64 {
        self.inner.row_count()
    }

    /// Returns the child slots without building any child.
    pub fn slots(&self) -> &PlanSlots {
        self.inner.slots()
    }

    /// Returns the number of slots, present or absent, without building any child.
    pub fn nslots(&self) -> usize {
        self.inner.nslots()
    }

    /// Returns the plan in `slot`, building it on first access, or `None` when the slot is
    /// absent.
    pub fn slot(&self, slot: usize) -> VortexResult<Option<PlanRef>> {
        self.inner.slot(slot)
    }

    /// Returns the plan in `slot`, or an error when the slot is absent or out of bounds.
    pub fn slot_required(&self, slot: usize) -> VortexResult<PlanRef> {
        self.inner.slot_required(slot)
    }

    /// The plans of the present slots in slot order. See [`PlanSlots::children`].
    pub fn children(&self) -> impl Iterator<Item = VortexResult<&PlanRef>> + '_ {
        self.inner.children()
    }

    /// Erases this typed plan into a shared reference.
    pub fn to_plan(&self) -> PlanRef {
        self.inner.clone()
    }

    /// Erases this typed plan into a shared reference.
    pub fn into_plan(self) -> PlanRef {
        self.inner
    }
}

impl<V: PlanVTable> Clone for Plan<V> {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
            _vtable: PhantomData,
        }
    }
}

impl<V: PlanVTable> Debug for Plan<V> {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
        Debug::fmt(&self.inner, formatter)
    }
}

impl<V: PlanVTable> Display for Plan<V> {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
        Display::fmt(&self.inner, formatter)
    }
}

impl<V: PlanVTable> Deref for Plan<V> {
    type Target = V::PlanData;

    fn deref(&self) -> &Self::Target {
        self.data()
    }
}

impl<V: PlanVTable> From<Plan<V>> for PlanRef {
    fn from(value: Plan<V>) -> Self {
        value.into_plan()
    }
}

/// A vtable value paired with its operator-specific plan data.
struct PlanData<V: PlanVTable> {
    vtable: V,
    data: V::PlanData,
}

impl<V: PlanVTable> Debug for PlanData<V> {
    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
        formatter
            .debug_struct("PlanData")
            .field("vtable", &self.vtable)
            .field("data", &self.data)
            .finish()
    }
}

/// Erased operator-specific behavior stored in the unsized tail of a [`PlanRef`].
#[doc(hidden)]
pub trait DynPlan: 'static + Send + Sync + Debug {
    /// Returns this operator data as [`Any`] for downcasting.
    fn as_any(&self) -> &dyn Any;

    /// Whether the node reads payload data.
    fn dyn_is_data_source(&self, plan: &PlanRef) -> bool;

    /// Applies an adapter-owned rewrite.
    fn dyn_reduce(&self, plan: &PlanRef, session: &VortexSession) -> VortexResult<Option<PlanRef>>;

    /// Applies an adapter-owned parent rewrite.
    fn dyn_reduce_parent(
        &self,
        plan: &PlanRef,
        parent: &PlanRef,
        slot: usize,
        session: &VortexSession,
    ) -> VortexResult<Option<PlanRef>>;

    /// Formats operator-specific fields.
    fn dyn_fmt(&self, plan: &PlanRef, formatter: &mut Formatter<'_>) -> fmt::Result;

    /// Clones operator data, runs its slot-replacement callback, and rebuilds the common node.
    fn dyn_with_slots(&self, plan: &PlanRef, slots: PlanSlots) -> VortexResult<PlanRef>;

    /// Returns the display name of `slot`.
    fn dyn_slot_name<'a>(&'a self, plan: &'a PlanRef, slot: usize) -> Cow<'a, str>;

    /// Serializes operator-specific metadata, or `None` when the operator is not serializable.
    fn dyn_metadata(&self, plan: &PlanRef) -> Option<Vec<u8>>;

    /// Compiles this operator. See [`PlanVTable::compile`].
    fn dyn_compile(
        &self,
        plan: &PlanRef,
        rows: Range<u64>,
        mask: &Mask,
        compiler: &mut Compiler<'_>,
    ) -> VortexResult<Option<Chain>>;
}

impl<V: PlanVTable> DynPlan for PlanData<V> {
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn dyn_is_data_source(&self, plan: &PlanRef) -> bool {
        V::is_data_source(plan.as_::<V>())
    }

    fn dyn_reduce(&self, plan: &PlanRef, session: &VortexSession) -> VortexResult<Option<PlanRef>> {
        V::reduce(plan.as_::<V>(), session)
    }

    fn dyn_reduce_parent(
        &self,
        plan: &PlanRef,
        parent: &PlanRef,
        slot: usize,
        session: &VortexSession,
    ) -> VortexResult<Option<PlanRef>> {
        V::reduce_parent(plan.as_::<V>(), parent, slot, session)
    }

    fn dyn_fmt(&self, plan: &PlanRef, formatter: &mut Formatter<'_>) -> fmt::Result {
        <V as PlanVTable>::fmt(plan.as_::<V>(), formatter)
    }

    fn dyn_with_slots(&self, plan: &PlanRef, slots: PlanSlots) -> VortexResult<PlanRef> {
        let mut data = self.data.clone();
        V::with_slots(plan.as_::<V>(), &slots, &mut data)?;
        Ok(PlanParts {
            vtable: self.vtable.clone(),
            dtype: plan.dtype().clone(),
            row_count: plan.row_count(),
            slots,
            data,
        }
        .into_plan())
    }

    fn dyn_slot_name<'a>(&'a self, plan: &'a PlanRef, slot: usize) -> Cow<'a, str> {
        V::slot_name(plan.as_::<V>(), slot)
    }

    fn dyn_metadata(&self, plan: &PlanRef) -> Option<Vec<u8>> {
        V::metadata(plan.as_::<V>()).map(SerializeMetadata::serialize)
    }

    fn dyn_compile(
        &self,
        plan: &PlanRef,
        rows: Range<u64>,
        mask: &Mask,
        compiler: &mut Compiler<'_>,
    ) -> VortexResult<Option<Chain>> {
        V::compile(plan.as_::<V>(), rows, mask, compiler)
    }
}