lance-encoding 4.0.0

Encoders and decoders for the Lance file format
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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The Lance Authors

//! Routines for decoding blob data
//!
//! The blob structural encoding is a structural encoding where the values (blobs) are stored
//! out-of-line in the file.  The page contains the descriptions, encoded using some other layout.

use std::{collections::VecDeque, ops::Range, sync::Arc};

use arrow_array::{Array, UInt64Array, cast::AsArray, make_array};
use bytes::Bytes;
use futures::{FutureExt, future::BoxFuture};

use lance_core::{
    Error, Result, cache::DeepSizeOf, datatypes::BLOB_DESC_TYPE, error::LanceOptionExt,
};

use crate::{
    EncodingsIo,
    buffer::LanceBuffer,
    data::{BlockInfo, DataBlock, VariableWidthBlock},
    decoder::{DecodePageTask, DecodedPage, StructuralPageDecoder},
    encodings::logical::primitive::{CachedPageData, PageLoadTask, StructuralPageScheduler},
    repdef::{DefinitionInterpretation, RepDefUnraveler},
};

/// How many bytes to target in each unloaded / loaded shard.  A larger value means
/// we buffer more data in memory / make bigger requests to the I/O scheduler while
/// a smaller value means more requests to the I/O scheduler.
///
/// This is probably a reasonable default for most cases.
pub const TARGET_SHARD_SIZE: u64 = 32 * 1024 * 1024;

#[derive(Debug)]
pub(super) struct BlobDescriptionPageScheduler {
    inner_scheduler: Box<dyn StructuralPageScheduler>,
    def_meaning: Arc<[DefinitionInterpretation]>,
}

impl BlobDescriptionPageScheduler {
    pub fn new(
        inner_scheduler: Box<dyn StructuralPageScheduler>,
        def_meaning: Arc<[DefinitionInterpretation]>,
    ) -> Self {
        Self {
            inner_scheduler,
            def_meaning,
        }
    }

    fn wrap_decoder_fut(
        decoder_fut: BoxFuture<'static, Result<Box<dyn StructuralPageDecoder>>>,
        def_meaning: Arc<[DefinitionInterpretation]>,
    ) -> BoxFuture<'static, Result<Box<dyn StructuralPageDecoder>>> {
        async move {
            let decoder = decoder_fut.await?;
            Ok(
                Box::new(BlobDescriptionPageDecoder::new(decoder, def_meaning))
                    as Box<dyn StructuralPageDecoder>,
            )
        }
        .boxed()
    }
}

impl StructuralPageScheduler for BlobDescriptionPageScheduler {
    fn initialize<'a>(
        &'a mut self,
        io: &Arc<dyn EncodingsIo>,
    ) -> BoxFuture<'a, Result<Arc<dyn CachedPageData>>> {
        self.inner_scheduler.initialize(io)
    }

    fn load(&mut self, data: &Arc<dyn CachedPageData>) {
        self.inner_scheduler.load(data);
    }

    fn schedule_ranges(
        &self,
        ranges: &[Range<u64>],
        io: &Arc<dyn EncodingsIo>,
    ) -> Result<Vec<PageLoadTask>> {
        let tasks = self.inner_scheduler.schedule_ranges(ranges, io)?;
        Ok(tasks
            .into_iter()
            .map(|task| PageLoadTask {
                decoder_fut: Self::wrap_decoder_fut(task.decoder_fut, self.def_meaning.clone()),
                num_rows: task.num_rows,
            })
            .collect())
    }
}

#[derive(Debug)]
struct BlobDescriptionPageDecoder {
    inner: Box<dyn StructuralPageDecoder>,
    def_meaning: Arc<[DefinitionInterpretation]>,
}

impl BlobDescriptionPageDecoder {
    fn new(
        inner: Box<dyn StructuralPageDecoder>,
        def_meaning: Arc<[DefinitionInterpretation]>,
    ) -> Self {
        Self { inner, def_meaning }
    }
}

impl StructuralPageDecoder for BlobDescriptionPageDecoder {
    fn drain(&mut self, num_rows: u64) -> Result<Box<dyn DecodePageTask>> {
        Ok(Box::new(BlobDescriptionDecodePageTask::new(
            self.inner.drain(num_rows)?,
            self.def_meaning.clone(),
        )))
    }

    fn num_rows(&self) -> u64 {
        self.inner.num_rows()
    }
}

#[derive(Debug)]
struct BlobDescriptionDecodePageTask {
    inner: Box<dyn DecodePageTask>,
    def_meaning: Arc<[DefinitionInterpretation]>,
}

impl BlobDescriptionDecodePageTask {
    fn new(inner: Box<dyn DecodePageTask>, def_meaning: Arc<[DefinitionInterpretation]>) -> Self {
        Self { inner, def_meaning }
    }
}

impl DecodePageTask for BlobDescriptionDecodePageTask {
    fn decode(self: Box<Self>) -> Result<DecodedPage> {
        let decoded = self.inner.decode()?;
        let num_values = decoded.data.num_values();

        // Need to extract out the repdef information
        let DataBlock::Struct(descriptions) = &decoded.data else {
            return Err(Error::internal(
                "Expected struct data block for descriptions",
            ));
        };
        let mut description_children = descriptions.children.iter();
        let DataBlock::FixedWidth(positions) = description_children.next().expect_ok()? else {
            return Err(Error::internal(
                "Expected fixed width data block for positions",
            ));
        };
        let DataBlock::FixedWidth(sizes) = description_children.next().expect_ok()? else {
            return Err(Error::internal("Expected fixed width data block for sizes"));
        };
        let positions = positions.data.borrow_to_typed_slice::<u64>();
        let sizes = sizes.data.borrow_to_typed_slice::<u64>();

        let mut rep = Vec::with_capacity(num_values as usize);
        let mut def = Vec::with_capacity(num_values as usize);

        for (position, size) in positions.iter().copied().zip(sizes.iter().copied()) {
            if size == 0 {
                if position == 0 {
                    rep.push(0);
                    def.push(0);
                } else {
                    let repval = (position & 0xFFFF) as u16;
                    let defval = ((position >> 16) & 0xFFFF) as u16;
                    rep.push(repval);
                    def.push(defval);
                }
            } else {
                rep.push(0);
                def.push(0);
            }
        }

        let rep = if rep.iter().any(|r| *r != 0) {
            Some(rep)
        } else {
            None
        };
        let def = if self.def_meaning.len() > 1
            || self.def_meaning[0] != DefinitionInterpretation::AllValidItem
        {
            Some(def)
        } else {
            None
        };

        let repdef =
            RepDefUnraveler::new(rep, def, self.def_meaning.clone(), positions.len() as u64);

        Ok(DecodedPage {
            data: decoded.data,
            repdef,
        })
    }
}

struct BlobCacheableState {
    positions: Arc<UInt64Array>,
    sizes: Arc<UInt64Array>,
    inner_state: Arc<dyn CachedPageData>,
}

impl DeepSizeOf for BlobCacheableState {
    fn deep_size_of_children(&self, context: &mut lance_core::cache::Context) -> usize {
        self.positions.get_array_memory_size()
            + self.sizes.get_array_memory_size()
            + self.inner_state.deep_size_of_children(context)
    }
}

impl CachedPageData for BlobCacheableState {
    fn as_arc_any(self: Arc<Self>) -> Arc<dyn std::any::Any + Send + Sync + 'static> {
        self
    }
}

#[derive(Debug)]
pub(super) struct BlobPageScheduler {
    inner_scheduler: Box<dyn StructuralPageScheduler>,
    row_number: u64,
    num_rows: u64,
    def_meaning: Arc<[DefinitionInterpretation]>,
    positions: Option<Arc<UInt64Array>>,
    sizes: Option<Arc<UInt64Array>>,
}

impl BlobPageScheduler {
    pub fn new(
        inner_scheduler: Box<dyn StructuralPageScheduler>,
        row_number: u64,
        num_rows: u64,
        def_meaning: Arc<[DefinitionInterpretation]>,
    ) -> Self {
        Self {
            inner_scheduler,
            row_number,
            num_rows,
            def_meaning,
            positions: None,
            sizes: None,
        }
    }

    fn create_page_load_task(
        ranges_to_read: Vec<Range<u64>>,
        mut loaded_blobs: Vec<LoadedBlob>,
        first_row_number: u64,
        io: &dyn EncodingsIo,
        def_meaning: Arc<[DefinitionInterpretation]>,
    ) -> Result<PageLoadTask> {
        let num_rows = loaded_blobs.len() as u64;
        let read_fut = io.submit_request(ranges_to_read, first_row_number);
        let decoder_fut = async move {
            let bytes = read_fut.await?;
            let mut bytes_iter = bytes.into_iter();
            for blob in loaded_blobs.iter_mut() {
                if blob.def == 0 {
                    blob.set_bytes(bytes_iter.next().expect_ok()?);
                }
            }
            debug_assert!(bytes_iter.next().is_none());
            Ok(Box::new(BlobPageDecoder::new(loaded_blobs, def_meaning))
                as Box<dyn StructuralPageDecoder>)
        }
        .boxed();
        Ok(PageLoadTask {
            decoder_fut,
            num_rows,
        })
    }
}

impl StructuralPageScheduler for BlobPageScheduler {
    fn initialize<'a>(
        &'a mut self,
        io: &Arc<dyn EncodingsIo>,
    ) -> BoxFuture<'a, Result<Arc<dyn CachedPageData>>> {
        let io = io.clone();
        let num_rows = self.num_rows;
        async move {
            let cached = self.inner_scheduler.initialize(&io).await?;
            let mut desc_decoders = self.inner_scheduler.schedule_ranges(&[0..num_rows], &io)?;
            if desc_decoders.len() != 1 {
                // This can't happen yet today so being a little lazy but if it did happen we just
                // need to concatenate the descriptions.  I'm guessing by then we might be doing something
                // different than "load all descriptors in initialize" anyways.
                return Err(Error::not_supported_source(
                    "Expected exactly one descriptor decoder".into(),
                ));
            }
            let desc_decoder_task = desc_decoders.pop().unwrap();
            let mut desc_decoder = desc_decoder_task.decoder_fut.await?;

            let descs = desc_decoder.drain(desc_decoder_task.num_rows)?;
            let descs = descs.decode()?;
            let descs = make_array(descs.data.into_arrow(BLOB_DESC_TYPE.clone(), true)?);
            let descs = descs.as_struct();
            let positions = Arc::new(
                descs
                    .column(0)
                    .as_any()
                    .downcast_ref::<UInt64Array>()
                    .unwrap()
                    .clone(),
            );
            let sizes = Arc::new(
                descs
                    .column(1)
                    .as_any()
                    .downcast_ref::<UInt64Array>()
                    .unwrap()
                    .clone(),
            );
            self.positions = Some(positions.clone());
            self.sizes = Some(sizes.clone());
            let state = Arc::new(BlobCacheableState {
                inner_state: cached,
                positions,
                sizes,
            });
            Ok(state as Arc<dyn CachedPageData>)
        }
        .boxed()
    }

    fn load(&mut self, data: &Arc<dyn CachedPageData>) {
        let blob_state = data
            .clone()
            .as_arc_any()
            .downcast::<BlobCacheableState>()
            .unwrap();
        self.positions = Some(blob_state.positions.clone());
        self.sizes = Some(blob_state.sizes.clone());
        self.inner_scheduler.load(&blob_state.inner_state);
    }

    fn schedule_ranges(
        &self,
        ranges: &[Range<u64>],
        io: &Arc<dyn EncodingsIo>,
    ) -> Result<Vec<PageLoadTask>> {
        let num_rows: u64 = ranges.iter().map(|r| r.end - r.start).sum();

        let positions = self.positions.as_ref().expect_ok()?;
        let sizes = self.sizes.as_ref().expect_ok()?;

        let mut page_load_tasks = Vec::new();
        let mut bytes_so_far = 0;
        let mut ranges_to_read = Vec::with_capacity(num_rows as usize);
        let mut loaded_blobs = Vec::with_capacity(num_rows as usize);
        let mut first_row_number = None;
        for range in ranges {
            for row in range.start..range.end {
                if first_row_number.is_none() {
                    first_row_number = Some(row + self.row_number);
                }
                let position = positions.value(row as usize);
                let size = sizes.value(row as usize);

                if size == 0 {
                    let rep = (position & 0xFFFF) as u16;
                    let def = ((position >> 16) & 0xFFFF) as u16;
                    loaded_blobs.push(LoadedBlob::new(rep, def));
                } else {
                    loaded_blobs.push(LoadedBlob::new(0, 0));
                    ranges_to_read.push(position..(position + size));
                    bytes_so_far += size;
                }

                if bytes_so_far >= TARGET_SHARD_SIZE {
                    let page_load_task = Self::create_page_load_task(
                        std::mem::take(&mut ranges_to_read),
                        std::mem::take(&mut loaded_blobs),
                        first_row_number.unwrap(),
                        io.as_ref(),
                        self.def_meaning.clone(),
                    )?;
                    page_load_tasks.push(page_load_task);
                    bytes_so_far = 0;
                    first_row_number = None;
                }
            }
        }
        if !loaded_blobs.is_empty() {
            let page_load_task = Self::create_page_load_task(
                std::mem::take(&mut ranges_to_read),
                std::mem::take(&mut loaded_blobs),
                first_row_number.unwrap(),
                io.as_ref(),
                self.def_meaning.clone(),
            )?;
            page_load_tasks.push(page_load_task);
        }

        Ok(page_load_tasks)
    }
}

#[derive(Debug)]
struct LoadedBlob {
    bytes: Option<Bytes>,
    rep: u16,
    def: u16,
}

impl LoadedBlob {
    fn new(rep: u16, def: u16) -> Self {
        Self {
            bytes: None,
            rep,
            def,
        }
    }

    fn set_bytes(&mut self, bytes: Bytes) {
        self.bytes = Some(bytes);
    }
}

#[derive(Debug)]
struct BlobPageDecoder {
    blobs: VecDeque<LoadedBlob>,
    def_meaning: Arc<[DefinitionInterpretation]>,
    num_rows: u64,
}

impl BlobPageDecoder {
    fn new(blobs: Vec<LoadedBlob>, def_meaning: Arc<[DefinitionInterpretation]>) -> Self {
        Self {
            num_rows: blobs.len() as u64,
            blobs: blobs.into_iter().collect(),
            def_meaning,
        }
    }
}

impl StructuralPageDecoder for BlobPageDecoder {
    fn drain(&mut self, num_rows: u64) -> Result<Box<dyn DecodePageTask>> {
        let blobs = self.blobs.drain(0..num_rows as usize).collect::<Vec<_>>();
        Ok(Box::new(BlobDecodePageTask::new(
            blobs,
            self.def_meaning.clone(),
        )))
    }

    fn num_rows(&self) -> u64 {
        self.num_rows
    }
}

#[derive(Debug)]
struct BlobDecodePageTask {
    blobs: Vec<LoadedBlob>,
    def_meaning: Arc<[DefinitionInterpretation]>,
}

impl BlobDecodePageTask {
    fn new(blobs: Vec<LoadedBlob>, def_meaning: Arc<[DefinitionInterpretation]>) -> Self {
        Self { blobs, def_meaning }
    }
}

impl DecodePageTask for BlobDecodePageTask {
    fn decode(self: Box<Self>) -> Result<DecodedPage> {
        let num_values = self.blobs.len() as u64;
        let num_bytes = self
            .blobs
            .iter()
            .filter_map(|b| b.bytes.as_ref())
            .map(|b| b.len())
            .sum::<usize>();
        let mut buffer = Vec::with_capacity(num_bytes);
        let mut offsets = Vec::with_capacity(num_values as usize + 1);
        let mut rep = Vec::with_capacity(num_values as usize);
        let mut def = Vec::with_capacity(num_values as usize);
        offsets.push(0_u64);
        for blob in self.blobs {
            rep.push(blob.rep);
            def.push(blob.def);
            if let Some(bytes) = blob.bytes {
                offsets.push(offsets.last().unwrap() + bytes.len() as u64);
                buffer.extend_from_slice(&bytes);
            } else {
                // Null / emptyvalue
                offsets.push(*offsets.last().unwrap());
            }
        }
        let offsets = LanceBuffer::reinterpret_vec(offsets);
        let data = LanceBuffer::from(buffer);
        let data_block = DataBlock::VariableWidth(VariableWidthBlock {
            data,
            offsets,
            bits_per_offset: 64,
            num_values,
            block_info: BlockInfo::new(),
        });

        let rep = if rep.iter().any(|r| *r != 0) {
            Some(rep)
        } else {
            None
        };
        let def = if self.def_meaning.len() > 1
            || self.def_meaning[0] != DefinitionInterpretation::AllValidItem
        {
            Some(def)
        } else {
            None
        };

        Ok(DecodedPage {
            data: data_block,
            repdef: RepDefUnraveler::new(rep, def, self.def_meaning, num_values),
        })
    }
}