vortex-layout 0.90.0

Vortex layouts provide a way to perform lazy push-down scans over abstract storage
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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright the Vortex contributors

//! Write-time assembly: forward chunks to the data child while feeding index builders, then write
//! each index's content after all data segments.

use std::num::NonZeroU64;
use std::sync::Arc;

use async_trait::async_trait;
use futures::StreamExt;
use futures::stream;
use parking_lot::Mutex;
use roaring::RoaringBitmap;
use tracing::trace;
use vortex_array::ArrayRef;
use vortex_array::ExecutionCtx;
use vortex_array::VortexSessionExecute;
use vortex_array::dtype::DType;
use vortex_array::stream::ArrayStreamAdapter;
use vortex_array::stream::ArrayStreamExt;
use vortex_array::stream::SendableArrayStream;
use vortex_error::VortexResult;
use vortex_error::vortex_bail;
use vortex_error::vortex_ensure;
use vortex_session::VortexSession;

use crate::BufferedBytesReservation;
use crate::BufferedBytesTracker;
use crate::LayoutRef;
use crate::LayoutStrategy;
use crate::LayoutWriterContext;
use crate::layouts::indexed::IndexPartitioning;
use crate::layouts::indexed::IndexSpec;
use crate::layouts::indexed::IndexedLayout;
use crate::layouts::indexed::index::IndexBuilder;
use crate::layouts::indexed::index::IndexVTable;
use crate::layouts::indexed::index::IndexVTableRef;
use crate::segments::SegmentSinkRef;
use crate::sequence::SendableSequentialStream;
use crate::sequence::SequencePointer;
use crate::sequence::SequentialArrayStreamExt;
use crate::sequence::SequentialStreamAdapter;
use crate::sequence::SequentialStreamExt;

/// An index to attach to a column, as configured on the write side.
#[derive(Clone, Debug)]
pub struct IndexConfig {
    vtable: IndexVTableRef,
    options: Vec<u8>,
    partition_len: Option<NonZeroU64>,
}

impl IndexConfig {
    /// Configure `vtable` with kind-defined options.
    pub fn new(vtable: IndexVTableRef, options: Vec<u8>) -> Self {
        Self {
            vtable,
            options,
            partition_len: None,
        }
    }

    /// Configure `vtable` with its default options.
    pub fn with_defaults(vtable: IndexVTableRef) -> Self {
        Self::new(vtable, Vec::new())
    }

    /// Build and probe this index in independent partitions of `partition_len` data rows, rather
    /// than as one index over the whole column.
    ///
    /// Partitions are independent of how the data child is chunked, so a scan over part of the
    /// column reads only the overlapping partitions' index rows. `partition_len` must be a
    /// multiple of the strategy's [`IndexedStrategy::with_data_block_len`], which must be set.
    pub fn with_partition_len(mut self, partition_len: NonZeroU64) -> Self {
        self.partition_len = Some(partition_len);
        self
    }
}

/// Wraps a data-child strategy with one or more index builders.
///
/// Sits in the same slot as `ZonedStrategy`, meaning above the repartition step, so it knows the
/// data child's row block size and can hand it to block-granular index kinds, making their blocks
/// line up with the data child's chunks by default.
pub struct IndexedStrategy {
    data: Arc<dyn LayoutStrategy>,
    index: Arc<dyn LayoutStrategy>,
    configs: Arc<[IndexConfig]>,
    data_block_len: Option<u64>,
}

impl IndexedStrategy {
    /// Create a strategy writing data through `data` and each index's content through `index`.
    pub fn new<D: LayoutStrategy, I: LayoutStrategy>(
        data: D,
        index: I,
        configs: Vec<IndexConfig>,
    ) -> Self {
        Self {
            data: Arc::new(data),
            index: Arc::new(index),
            configs: configs.into(),
            data_block_len: None,
        }
    }

    /// Tell block-granular index kinds the data child's row block size, so pruned blocks align
    /// with chunk and segment boundaries.
    pub fn with_data_block_len(mut self, data_block_len: u64) -> Self {
        self.data_block_len = Some(data_block_len);
        self
    }
}

/// An index kind with its configured options parsed, once per write.
pub(crate) trait IndexWriter: Send + Sync {
    fn index_dtype(&self, dtype: &DType) -> Option<DType>;

    /// A builder for one partition, whose output must be of `index_dtype`.
    fn builder(
        &self,
        dtype: &DType,
        index_dtype: &DType,
        data_block_len: Option<u64>,
        session: &VortexSession,
    ) -> VortexResult<Box<dyn PartitionBuilder>>;
}

pub(crate) struct TypedIndexWriter<V: IndexVTable> {
    vtable: Arc<V>,
    options: V::Options,
}

/// One partition's builder, with the kind's types erased. This is where a kind's content and
/// options become bytes, through [`IndexVTable::encode`] and [`IndexVTable::serialize_options`].
pub(crate) trait PartitionBuilder: Send {
    fn push(
        &mut self,
        chunk: &ArrayRef,
        row_offset: u64,
        ctx: &mut ExecutionCtx,
    ) -> VortexResult<()>;

    fn finish(self: Box<Self>) -> VortexResult<PartitionOutput>;

    fn buffered_bytes(&self) -> u64;
}

struct TypedPartitionBuilder<V: IndexVTable> {
    vtable: Arc<V>,
    builder: V::Builder,
    index_dtype: DType,
    session: VortexSession,
}

impl<V: IndexVTable> PartitionBuilder for TypedPartitionBuilder<V> {
    fn push(
        &mut self,
        chunk: &ArrayRef,
        row_offset: u64,
        ctx: &mut ExecutionCtx,
    ) -> VortexResult<()> {
        self.builder.push(chunk, row_offset, ctx)
    }

    fn finish(self: Box<Self>) -> VortexResult<PartitionOutput> {
        let Self {
            vtable,
            builder,
            index_dtype,
            session,
        } = *self;
        let Some((chunks, options)) = builder.finish()? else {
            return Ok(None);
        };

        let mut ctx = session.create_execution_ctx();
        let arrays = chunks
            .iter()
            .map(|chunk| {
                let array = vtable.encode(chunk, &options, &mut ctx)?;
                vortex_ensure!(
                    array.dtype() == &index_dtype,
                    "Index {} encoded a chunk of dtype {}, but the index kind declared {index_dtype}",
                    vtable.id(),
                    array.dtype()
                );
                Ok(array)
            })
            .collect::<VortexResult<Vec<_>>>()?;

        let content =
            ArrayStreamAdapter::new(index_dtype, stream::iter(arrays.into_iter().map(Ok)));
        Ok(Some((
            ArrayStreamExt::boxed(content),
            vtable.serialize_options(&options),
        )))
    }

    fn buffered_bytes(&self) -> u64 {
        self.builder.buffered_bytes()
    }
}

impl<V: IndexVTable> TypedIndexWriter<V> {
    pub(crate) fn try_new(vtable: Arc<V>, options: &[u8]) -> VortexResult<Self> {
        let options = vtable.deserialize_options(options)?;
        Ok(Self { vtable, options })
    }
}

impl<V: IndexVTable> IndexWriter for TypedIndexWriter<V> {
    fn index_dtype(&self, dtype: &DType) -> Option<DType> {
        self.vtable.index_dtype(dtype, &self.options)
    }

    fn builder(
        &self,
        dtype: &DType,
        index_dtype: &DType,
        data_block_len: Option<u64>,
        session: &VortexSession,
    ) -> VortexResult<Box<dyn PartitionBuilder>> {
        Ok(Box::new(TypedPartitionBuilder {
            vtable: Arc::clone(&self.vtable),
            builder: self
                .vtable
                .builder(dtype, &self.options, data_block_len, session)?,
            index_dtype: index_dtype.clone(),
            session: session.clone(),
        }))
    }
}

/// Everything needed to start another partition's builder mid-stream.
struct BuilderFactory {
    dtype: DType,
    data_block_len: Option<u64>,
    session: VortexSession,
}

impl BuilderFactory {
    fn builder(
        &self,
        writer: &dyn IndexWriter,
        index_dtype: &DType,
    ) -> VortexResult<Box<dyn PartitionBuilder>> {
        writer.builder(&self.dtype, index_dtype, self.data_block_len, &self.session)
    }
}

/// A finished partition's output: its content stream and final options, or `None` if it declined.
type PartitionOutput = Option<(SendableArrayStream, Vec<u8>)>;

/// One configured index, built partition by partition.
struct IndexState {
    vtable: IndexVTableRef,
    /// The kind with its configured options parsed, starting every partition's builder.
    writer: Arc<dyn IndexWriter>,
    /// What the kind declared it builds, which every partition's output must match.
    index_dtype: DType,
    partition_len: Option<u64>,
    builder: Box<dyn PartitionBuilder>,
    /// Rows the current builder has seen, which is also the next row's partition-local offset.
    partition_rows: u64,
    finished: Vec<PartitionOutput>,
    /// What each finished builder last reported buffering; its content stream still holds it.
    finished_bytes: u64,
}

impl IndexState {
    fn push(
        &mut self,
        chunk: &ArrayRef,
        factory: &BuilderFactory,
        ctx: &mut ExecutionCtx,
    ) -> VortexResult<()> {
        let Some(partition_len) = self.partition_len else {
            return self
                .builder
                .push(chunk, self.partition_rows, ctx)
                .map(|()| {
                    self.partition_rows += chunk.len() as u64;
                });
        };

        // Chunks come from upstream of any repartitioning, so one can straddle several
        // partitions; each builder must see only its own partition's rows.
        let mut start = 0;
        while start < chunk.len() {
            let room = usize::try_from(partition_len - self.partition_rows)?;
            let end = chunk.len().min(start + room);
            let piece = if start == 0 && end == chunk.len() {
                chunk.clone()
            } else {
                chunk.slice(start..end)?
            };
            self.builder.push(&piece, self.partition_rows, ctx)?;
            self.partition_rows += (end - start) as u64;
            start = end;

            if self.partition_rows == partition_len {
                let next = factory.builder(self.writer.as_ref(), &self.index_dtype)?;
                self.finish_partition(next)?;
            }
        }
        Ok(())
    }

    fn finish_partition(&mut self, next: Box<dyn PartitionBuilder>) -> VortexResult<()> {
        let done = std::mem::replace(&mut self.builder, next);
        self.finished_bytes += done.buffered_bytes();
        self.finished.push(done.finish()?);
        self.partition_rows = 0;
        Ok(())
    }

    fn buffered_bytes(&self) -> u64 {
        self.builder.buffered_bytes() + self.finished_bytes
    }

    /// Finish the trailing partition and return every partition's output in row order.
    ///
    /// An unpartitioned index always yields exactly one partition, even over zero rows, leaving
    /// the empty case to the builder. A partitioned index yields one per started partition, so
    /// the builder opened after an exactly-full final partition is dropped unused.
    fn finish(mut self) -> VortexResult<Vec<PartitionOutput>> {
        if self.partition_len.is_none() || self.partition_rows > 0 {
            self.finished.push(self.builder.finish()?);
        }
        Ok(self.finished)
    }
}

/// Index state shared between the stream-mapping closure and the finishing code. Index building
/// is globally stateful, so pushes stay sequential in stream order.
struct BuilderState {
    factory: BuilderFactory,
    indexes: Vec<IndexState>,
    /// Reservation reflecting the indexes' current combined buffered bytes.
    ///
    /// Builders report a running total rather than a per-chunk delta, so each push replaces this
    /// reservation (drop the old, reserve the new total) instead of accumulating one reservation
    /// per chunk the way `BufferedStrategy` does for known-size chunks.
    buffered: Option<BufferedBytesReservation>,
}

impl BuilderState {
    fn push(&mut self, chunk: &ArrayRef, tracker: &BufferedBytesTracker) -> VortexResult<()> {
        let mut ctx = self.factory.session.create_execution_ctx();
        for index in &mut self.indexes {
            index.push(chunk, &self.factory, &mut ctx)?;
        }

        let total: u64 = self.indexes.iter().map(IndexState::buffered_bytes).sum();
        self.buffered = Some(tracker.reserve(total));
        Ok(())
    }
}

/// Every built partition of one index, concatenated into a single stream for the index child.
struct IndexContent {
    stream: SendableArrayStream,
    options: Vec<u8>,
    dtype: DType,
    /// Index rows written per partition, filled in as the index child consumes `stream`.
    partition_rows: Arc<Mutex<Vec<u64>>>,
    declined: RoaringBitmap,
}

impl IndexContent {
    /// Concatenate the built partitions, each already of the declared `dtype`, or `None` if every
    /// partition declined.
    fn assemble(
        id: impl std::fmt::Display,
        dtype: DType,
        partitions: Vec<PartitionOutput>,
    ) -> VortexResult<Option<Self>> {
        let mut declined = RoaringBitmap::new();
        let mut built = Vec::with_capacity(partitions.len());
        let mut options: Option<Vec<u8>> = None;
        let partition_count = partitions.len();

        for (partition, output) in partitions.into_iter().enumerate() {
            let Some((content, partition_options)) = output else {
                declined.insert(u32::try_from(partition)?);
                continue;
            };
            match &options {
                None => options = Some(partition_options),
                // One spec records one options blob, so partitions may not disagree about it.
                Some(first) => vortex_ensure!(
                    *first == partition_options,
                    "Index {id} partition {partition} finished with different options than earlier partitions"
                ),
            }
            built.push((partition, content));
        }

        let Some(options) = options else {
            return Ok(None);
        };

        let partition_rows = Arc::new(Mutex::new(vec![0u64; partition_count]));
        let counts = Arc::clone(&partition_rows);
        let chunks = stream::iter(built).flat_map(move |(partition, content)| {
            let counts = Arc::clone(&counts);
            content.map(move |chunk| {
                if let Ok(chunk) = &chunk {
                    counts.lock()[partition] += chunk.len() as u64;
                }
                chunk
            })
        });

        Ok(Some(Self {
            stream: ArrayStreamExt::boxed(ArrayStreamAdapter::new(dtype.clone(), chunks)),
            options,
            dtype,
            partition_rows,
            declined,
        }))
    }
}

/// The partitioning to record for an index, from the index rows each partition wrote.
fn partitioning(
    partition_len: u64,
    partition_rows: &[u64],
    declined: RoaringBitmap,
) -> IndexPartitioning {
    let ends = partition_rows
        .iter()
        .scan(0u64, |end, rows| {
            *end += rows;
            Some(*end)
        })
        .collect();
    IndexPartitioning::new(partition_len, ends, declined)
}

#[async_trait]
impl LayoutStrategy for IndexedStrategy {
    async fn write_stream(
        &self,
        ctx: LayoutWriterContext,
        segment_sink: SegmentSinkRef,
        stream: SendableSequentialStream,
        mut eof: SequencePointer,
        session: &VortexSession,
    ) -> VortexResult<LayoutRef> {
        let dtype = stream.dtype().clone();

        // Checked before the dtype filter below, so a misconfigured partitioning fails every
        // write rather than only the ones whose column happens to be indexable.
        for config in self.configs.iter() {
            let Some(partition_len) = config.partition_len else {
                continue;
            };
            let Some(block_len) = self.data_block_len.filter(|len| *len > 0) else {
                vortex_bail!(
                    "Index {} is partitioned every {partition_len} rows, which requires a non-zero \
                     data block length (IndexedStrategy::with_data_block_len)",
                    config.vtable.id()
                );
            };
            vortex_ensure!(
                partition_len.get() % block_len == 0,
                "Index {} partition length {partition_len} must be a multiple of the data block \
                 length {block_len}",
                config.vtable.id()
            );
        }

        let factory = BuilderFactory {
            dtype: dtype.clone(),
            data_block_len: self.data_block_len,
            session: session.clone(),
        };
        let mut indexes = Vec::with_capacity(self.configs.len());
        for config in self.configs.iter() {
            let writer = config.vtable.open_writer(&config.options)?;
            let Some(index_dtype) = writer.index_dtype(&dtype) else {
                continue;
            };
            indexes.push(IndexState {
                builder: factory.builder(writer.as_ref(), &index_dtype)?,
                writer,
                index_dtype,
                vtable: config.vtable.clone(),
                partition_len: config.partition_len.map(NonZeroU64::get),
                partition_rows: 0,
                finished: Vec::new(),
                finished_bytes: 0,
            });
        }

        // Nothing to index for this dtype: don't emit a wrapper at all, so readers see the plain
        // data layout.
        if indexes.is_empty() {
            return self
                .data
                .write_stream(ctx, segment_sink, stream, eof, session)
                .await;
        }

        let state = Arc::new(Mutex::new(BuilderState {
            factory,
            indexes,
            buffered: None,
        }));

        let feed_state = Arc::clone(&state);
        let feed_tracker = ctx.buffered_bytes_tracker().clone();
        let stream = SequentialStreamAdapter::new(
            dtype,
            stream.map(move |item| {
                let (sequence_id, chunk) = item?;
                feed_state.lock().push(&chunk, &feed_tracker)?;
                Ok((sequence_id, chunk))
            }),
        )
        .sendable();

        // Data segments come first, so a reader that ignores indexes keeps its locality and a
        // streaming writer never has to seek back.
        let data_eof = eof.split_off();
        let data_layout = self
            .data
            .write_stream(
                ctx.clone(),
                Arc::clone(&segment_sink),
                stream,
                data_eof,
                session,
            )
            .await?;

        // The stream is drained, so every builder has seen every chunk. Bytes now move from being
        // buffered in memory to being written out below, so the reservation is released here
        // rather than left to drop at the end of the function.
        let indexes = {
            let mut state = state.lock();
            state.buffered.take();
            std::mem::take(&mut state.indexes)
        };

        let mut index_layouts = Vec::with_capacity(indexes.len());
        let mut specs = Vec::with_capacity(indexes.len());
        for index in indexes {
            let vtable = index.vtable.clone();
            let partition_len = index.partition_len;
            let declared = index.index_dtype.clone();
            // An index whose every partition declined leaves no trace: no child, no spec, and no
            // sequence pointer, since the splits below are what allocate one.
            let Some(IndexContent {
                stream: content,
                options,
                dtype: index_dtype,
                partition_rows,
                declined,
            }) = IndexContent::assemble(vtable.id(), declared, index.finish()?)?
            else {
                trace!(index = %vtable.id(), "index builder declined, writing no child");
                continue;
            };

            // Each index child gets its own (stream pointer, eof) pair, all ordered after the data
            // segments. A chunking index strategy keeps each partition in its own segments, which
            // is what lets a probe of one partition skip the others' bytes.
            let content_ptr = eof.split_off();
            let child_eof = eof.split_off();
            let layout = self
                .index
                .write_stream(
                    ctx.clone(),
                    Arc::clone(&segment_sink),
                    content.sequenced(content_ptr),
                    child_eof,
                    session,
                )
                .await?;

            // The child has consumed the stream, so every partition's row count is final.
            let partitioning =
                partition_len.map(|len| partitioning(len, &partition_rows.lock(), declined));
            specs.push(IndexSpec::new(vtable, options, index_dtype, partitioning));
            index_layouts.push(layout);
        }

        // Every builder declined, so there is nothing to wrap. The data layout is already written
        // and stands on its own, so hand it back as if no index had been configured.
        if index_layouts.is_empty() {
            return Ok(data_layout);
        }

        Ok(IndexedLayout::try_new(data_layout, index_layouts, specs)?.into_layout())
    }
}