moraine 0.4.2

A SlateDB-backed catalog for DuckLake
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
//! Inlined data through the verb surface: inlining a chunk, tombstoning a
//! row, reading rows back, and draining them to a file.
//!
//! The chunk bodies here are opaque bytes. moraine stores and returns them
//! verbatim — decoding Arrow is the caller's half of the contract — so the
//! catalog behaviour is exercised without an Arrow encoder in the way.

use moraine::{
    Catalog, CatalogOptions, DataFile, Error, FileIndexEntry, FileIndexRemoval, FlushedDataFile,
    IndexDef, IndexKeyValue, InlineChunk, IntWidth, RowHolder, SnapshotId, TableId,
};

use crate::fixtures::{col, datafile, seeded};

fn chunk(body: &str, rows: u64) -> InlineChunk {
    InlineChunk {
        schema_version: 0,
        arrow_schema: b"schema-v0".to_vec(),
        arrow_body: body.as_bytes().to_vec(),
        row_count: rows,
    }
}

/// The row ids of a table's live inlined rows, in order.
#[allow(clippy::unwrap_used)]
async fn row_ids(catalog: &Catalog, table: TableId) -> Vec<u64> {
    catalog
        .recent_rows(table)
        .await
        .unwrap()
        .into_iter()
        .map(|row| row.row_id)
        .collect()
}

#[tokio::test]
async fn an_inlined_chunk_reads_back_with_dense_ids_and_its_bytes() {
    let (catalog, _, table, _) = seeded().await;
    let start = catalog
        .commit(|tx| {
            tx.inline_insert(table, &chunk("rows-a", 3), &[])?;
            Ok(())
        })
        .await
        .unwrap();

    let rows = catalog.recent_rows(table).await.unwrap();
    assert_eq!(rows.len(), 3);
    assert_eq!(row_ids(&catalog, table).await, vec![0, 1, 2]);
    for (offset, row) in rows.iter().enumerate() {
        assert_eq!(row.offset_in_chunk, offset as u64);
        assert_eq!(row.schema_version, 0);
        assert_eq!(row.chunk_body.as_slice(), b"rows-a");
        assert_eq!(row.arrow_schema.as_slice(), b"schema-v0");
        assert_eq!(row.begin_snapshot, start);
    }
    // Rows of one chunk share one body and one schema, read and carried
    // once however many rows point at them.
    assert!(std::sync::Arc::ptr_eq(
        &rows[0].chunk_body,
        &rows[2].chunk_body
    ));
    assert!(std::sync::Arc::ptr_eq(
        &rows[0].arrow_schema,
        &rows[2].arrow_schema
    ));
}

/// `inline_insert` returns the chunk's first row id, and ids continue from
/// the table's counter across chunks and commits — the same allocation a
/// data-file registration performs.
#[tokio::test]
async fn row_ids_continue_across_chunks_commits_and_data_files() {
    let (catalog, _, table, _) = seeded().await;
    catalog
        .commit(|tx| {
            assert_eq!(tx.inline_insert(table, &chunk("a", 2), &[])?, 0);
            assert_eq!(tx.inline_insert(table, &chunk("b", 1), &[])?, 2);
            Ok(())
        })
        .await
        .unwrap();
    catalog
        .commit(|tx| {
            tx.register_data_file(table, datafile(5), &[])?;
            Ok(())
        })
        .await
        .unwrap();
    catalog
        .commit(|tx| {
            assert_eq!(tx.inline_insert(table, &chunk("c", 1), &[])?, 8);
            Ok(())
        })
        .await
        .unwrap();

    assert_eq!(row_ids(&catalog, table).await, vec![0, 1, 2, 8]);
    let stats = catalog
        .snapshot()
        .await
        .unwrap()
        .table_stats(table)
        .unwrap();
    assert_eq!(stats.next_row_id, 9);
    // Inlined rows count as rows: four inlined plus the file's five.
    assert_eq!(stats.record_count, 9);
}

#[tokio::test]
async fn a_tombstoned_row_disappears_from_the_live_rows() {
    let (catalog, _, table, _) = seeded().await;
    catalog
        .commit(|tx| {
            tx.inline_insert(table, &chunk("a", 3), &[])?;
            Ok(())
        })
        .await
        .unwrap();
    catalog
        .commit(|tx| tx.inline_delete(table, 1, &[]))
        .await
        .unwrap();

    assert_eq!(row_ids(&catalog, table).await, vec![0, 2]);
    assert!(catalog.recent_row(table, 1).await.unwrap().is_none());
    assert_eq!(
        catalog.recent_row(table, 2).await.unwrap().unwrap().row_id,
        2
    );
    // A tombstone is its own record, never a chunk rewrite: its neighbours
    // in the chunk keep the body and offsets they were written with.
    let survivor = catalog.recent_row(table, 2).await.unwrap().unwrap();
    assert_eq!(survivor.chunk_body.as_slice(), b"a");
    assert_eq!(survivor.offset_in_chunk, 2);

    // Deletes leave statistics alone, exactly as a delete file does.
    let stats = catalog
        .snapshot()
        .await
        .unwrap()
        .table_stats(table)
        .unwrap();
    assert_eq!(stats.record_count, 3);
}

#[tokio::test]
async fn recent_rows_are_scoped_to_their_table() {
    let (catalog, _, a, b) = seeded().await;
    catalog
        .commit(|tx| {
            tx.inline_insert(a, &chunk("a", 1), &[])?;
            Ok(())
        })
        .await
        .unwrap();

    assert_eq!(row_ids(&catalog, a).await, vec![0]);
    assert!(catalog.recent_rows(b).await.unwrap().is_empty());
    assert!(catalog.recent_row(b, 0).await.unwrap().is_none());
}

#[tokio::test]
async fn an_empty_chunk_is_refused() {
    let (catalog, _, table, _) = seeded().await;
    let err = catalog
        .commit(|tx| {
            tx.inline_insert(table, &chunk("", 0), &[])?;
            Ok(())
        })
        .await
        .unwrap_err();
    assert!(matches!(err, Error::Constraint(_)), "{err}");
}

#[tokio::test]
async fn inlining_into_an_absent_table_is_not_found() {
    let (catalog, _, _, _) = seeded().await;
    let err = catalog
        .commit(|tx| {
            tx.inline_insert(TableId::new(9999), &chunk("a", 1), &[])?;
            Ok(())
        })
        .await
        .unwrap_err();
    assert!(matches!(err, Error::NotFound(_)), "{err}");
}

/// A flush drains the chunks and registers the file the caller wrote,
/// preserving the rows' ids and backdating the record — so the rows are in
/// exactly one place before and after.
#[tokio::test]
async fn a_flush_drains_the_chunks_and_registers_the_backdated_file() {
    let (catalog, _, table, _) = seeded().await;
    let inlined = catalog
        .commit(|tx| {
            tx.inline_insert(table, &chunk("a", 3), &[])?;
            Ok(())
        })
        .await
        .unwrap();

    let file = DataFile {
        record_count: 3,
        ..datafile(3)
    };
    catalog
        .commit(move |tx| {
            let flushed = FlushedDataFile {
                file: file.clone(),
                row_id_start: 0,
                begin_snapshot: inlined,
            };
            let ids = tx.flush_inlined_data(table, 0, std::slice::from_ref(&flushed))?;
            assert_eq!(ids.len(), 1);
            Ok(())
        })
        .await
        .unwrap();

    assert!(
        catalog.recent_rows(table).await.unwrap().is_empty(),
        "the drained chunks are gone"
    );
    let head = catalog.snapshot().await.unwrap();
    let files = head.data_files_of(table);
    assert_eq!(files.len(), 1);
    assert_eq!(files[0].row_id_start, Some(0), "row ids are preserved");
    // The rows were counted when they were inlined; the flush adds only
    // the file's bytes.
    let stats = head.table_stats(table).unwrap();
    assert_eq!(stats.record_count, 3);
    assert_eq!(stats.next_row_id, 3);
    assert_eq!(stats.file_size_bytes, 30);

    // Backdated: the file is live at the snapshot the rows were inlined at.
    let past = catalog.snapshot_at(inlined).await.unwrap();
    assert_eq!(past.data_files_of(table).len(), 1);
}

#[tokio::test]
async fn a_flush_with_no_files_drains_wholly_tombstoned_chunks() {
    let (catalog, _, table, _) = seeded().await;
    catalog
        .commit(|tx| {
            tx.inline_insert(table, &chunk("a", 1), &[])?;
            Ok(())
        })
        .await
        .unwrap();
    catalog
        .commit(|tx| tx.inline_delete(table, 0, &[]))
        .await
        .unwrap();
    catalog
        .commit(|tx| {
            tx.flush_inlined_data(table, 0, &[])?;
            Ok(())
        })
        .await
        .unwrap();

    assert!(catalog.recent_rows(table).await.unwrap().is_empty());
    assert!(
        catalog
            .snapshot()
            .await
            .unwrap()
            .data_files_of(table)
            .is_empty()
    );
}

/// A drain reads the store as it stood before this commit, so a chunk
/// inlined in the same commit would survive it while its rows were also
/// written to the flushed file. Refused rather than double-counted.
#[tokio::test]
async fn inlining_and_flushing_one_table_in_one_commit_is_refused() {
    let (catalog, _, table, _) = seeded().await;
    let err = catalog
        .commit(|tx| {
            tx.inline_insert(table, &chunk("a", 1), &[])?;
            tx.flush_inlined_data(table, 0, &[])?;
            Ok(())
        })
        .await
        .unwrap_err();
    assert!(matches!(err, Error::Constraint(_)), "{err}");
}

#[tokio::test]
async fn a_flushed_file_must_be_backdated_and_stay_inside_the_allocated_ids() {
    let (catalog, _, table, _) = seeded().await;
    let inlined = catalog
        .commit(|tx| {
            tx.inline_insert(table, &chunk("a", 2), &[])?;
            Ok(())
        })
        .await
        .unwrap();

    // Not backdated: a record beginning at (or above) this commit's own
    // snapshot would hide the rows from the snapshots they were live at.
    let not_backdated = catalog
        .commit(move |tx| {
            tx.flush_inlined_data(
                table,
                0,
                &[FlushedDataFile {
                    file: datafile(2),
                    row_id_start: 0,
                    begin_snapshot: SnapshotId::new(u64::MAX),
                }],
            )?;
            Ok(())
        })
        .await
        .unwrap_err();
    assert!(
        matches!(not_backdated, Error::Constraint(_)),
        "{not_backdated}"
    );

    // Past the allocated ids: the file claims rows the table never minted.
    let past_end = catalog
        .commit(move |tx| {
            tx.flush_inlined_data(
                table,
                0,
                &[FlushedDataFile {
                    file: datafile(2),
                    row_id_start: 1,
                    begin_snapshot: inlined,
                }],
            )?;
            Ok(())
        })
        .await
        .unwrap_err();
    assert!(matches!(past_end, Error::Constraint(_)), "{past_end}");
}

/// Several chunks under one schema version land under distinct keys within
/// one commit — the sequence number that disambiguates them is allocated
/// per `(table, schema_version)`.
#[tokio::test]
async fn several_chunks_in_one_commit_all_survive() {
    let (catalog, _, table, _) = seeded().await;
    catalog
        .commit(|tx| {
            tx.inline_insert(table, &chunk("a", 1), &[])?;
            tx.inline_insert(table, &chunk("b", 1), &[])?;
            tx.inline_insert(table, &chunk("c", 1), &[])?;
            Ok(())
        })
        .await
        .unwrap();

    let rows = catalog.recent_rows(table).await.unwrap();
    let mut bodies: Vec<&[u8]> = rows.iter().map(|row| row.chunk_body.as_slice()).collect();
    bodies.sort_unstable();
    assert_eq!(
        bodies,
        vec![b"a".as_slice(), b"b".as_slice(), b"c".as_slice()]
    );
}

/// Chunks of different schema versions coexist, each decoding against its
/// own recorded schema, and a flush drains only the version it names.
#[tokio::test]
async fn schema_versions_are_independent_through_read_and_flush() {
    let (catalog, _, table, _) = seeded().await;
    catalog
        .commit(|tx| {
            tx.inline_insert(table, &chunk("v0", 1), &[])?;
            tx.inline_insert(
                table,
                &InlineChunk {
                    schema_version: 1,
                    arrow_schema: b"schema-v1".to_vec(),
                    ..chunk("v1", 1)
                },
                &[],
            )?;
            Ok(())
        })
        .await
        .unwrap();

    let rows = catalog.recent_rows(table).await.unwrap();
    assert_eq!(rows.len(), 2);
    let schema_of = |version: u64| {
        rows.iter()
            .find(|row| row.schema_version == version)
            .map(|row| row.arrow_schema.as_slice().to_vec())
            .unwrap()
    };
    assert_eq!(schema_of(0), b"schema-v0");
    assert_eq!(schema_of(1), b"schema-v1");

    catalog
        .commit(|tx| {
            tx.flush_inlined_data(table, 0, &[])?;
            Ok(())
        })
        .await
        .unwrap();

    let left = catalog.recent_rows(table).await.unwrap();
    assert_eq!(left.len(), 1, "only version 0 was drained");
    assert_eq!(left[0].schema_version, 1);
}

/// A catalog whose seeded table `a` carries a non-unique index over its
/// one column.
#[allow(clippy::unwrap_used)]
async fn indexed() -> (Catalog, TableId, moraine::IndexId) {
    let (catalog, _, table, _) = seeded().await;
    let column = catalog.snapshot().await.unwrap().columns_of(table)[0].id;
    catalog
        .commit(move |tx| {
            tx.create_index(
                table,
                &IndexDef {
                    name: "by_x".into(),
                    columns: vec![column],
                    unique: false,
                },
                &[],
            )?;
            Ok(())
        })
        .await
        .unwrap();
    let index = catalog
        .snapshot()
        .await
        .unwrap()
        .index_by_name(table, "by_x")
        .unwrap()
        .id;

    (catalog, table, index)
}

fn indexed_value(v: i128) -> IndexKeyValue {
    IndexKeyValue::Int {
        value: v,
        width: IntWidth::I64,
    }
}

/// An index that silently misses a table's inlined rows is a lie, so an
/// uncovered inline insert is refused — the rule a data-file registration
/// already enforces.
#[tokio::test]
async fn an_uncovered_inline_insert_into_an_indexed_table_is_refused() {
    let (catalog, table, _) = indexed().await;
    let err = catalog
        .commit(|tx| {
            tx.inline_insert(table, &chunk("a", 1), &[])?;
            Ok(())
        })
        .await
        .unwrap_err();
    assert!(matches!(err, Error::Constraint(_)), "{err}");

    let err = catalog
        .commit(|tx| tx.inline_delete(table, 0, &[]))
        .await
        .unwrap_err();
    assert!(matches!(err, Error::Constraint(_)), "{err}");
}

/// A covered inlined row is findable by the index and reports itself as
/// inlined; tombstoning it strips the entry, so the index never reports a
/// row the table no longer has.
#[tokio::test]
async fn a_covered_inlined_row_is_findable_until_it_is_tombstoned() {
    let (catalog, table, index) = indexed().await;
    catalog
        .commit(move |tx| {
            tx.inline_insert(
                table,
                &chunk("a", 2),
                &[
                    FileIndexEntry {
                        index,
                        ordinal: 0,
                        values: vec![Some(indexed_value(10))],
                    },
                    FileIndexEntry {
                        index,
                        ordinal: 1,
                        values: vec![Some(indexed_value(20))],
                    },
                ],
            )?;
            Ok(())
        })
        .await
        .unwrap();

    let twenty = [indexed_value(20)];
    let found = catalog.index_lookup(table, index, &twenty).await.unwrap();
    assert_eq!(found.len(), 1);
    assert_eq!(found[0].row_id, 1);
    assert_eq!(found[0].holder, RowHolder::Inline);

    catalog
        .commit(move |tx| {
            tx.inline_delete(
                table,
                1,
                &[FileIndexRemoval {
                    index,
                    row_id: 1,
                    values: vec![Some(indexed_value(20))],
                }],
            )
        })
        .await
        .unwrap();
    assert!(
        catalog
            .index_lookup(table, index, &twenty)
            .await
            .unwrap()
            .is_empty()
    );
}

/// A removal must name the row being tombstoned; naming another row would
/// strip a live row's entry.
#[tokio::test]
async fn an_inline_delete_refuses_a_removal_for_another_row() {
    let (catalog, table, index) = indexed().await;
    let err = catalog
        .commit(move |tx| {
            tx.inline_delete(
                table,
                0,
                &[FileIndexRemoval {
                    index,
                    row_id: 7,
                    values: vec![Some(indexed_value(1))],
                }],
            )
        })
        .await
        .unwrap_err();
    assert!(matches!(err, Error::Constraint(_)), "{err}");
}

/// Inlined rows are store state, not handle state: a second handle opened
/// over the same bucket reads them back, bytes and ids intact.
#[tokio::test]
async fn inlined_rows_survive_a_reopen() {
    let store: std::sync::Arc<dyn object_store::ObjectStore> =
        std::sync::Arc::new(object_store::memory::InMemory::new());
    let table = {
        let catalog = Catalog::open(std::sync::Arc::clone(&store), CatalogOptions::default())
            .await
            .unwrap();
        let created = std::cell::Cell::new(None);
        catalog
            .commit(|tx| {
                let schema = tx.schema_by_name("main").expect("bootstrap schema").id;
                let table = tx.create_table(schema, "orders", &[col("a")])?;
                tx.inline_insert(table, &chunk("durable", 2), &[])?;
                created.set(Some(table));
                Ok(())
            })
            .await
            .unwrap();
        catalog.close().await.unwrap();
        created.get().unwrap()
    };

    let reopened = Catalog::open(store, CatalogOptions::default())
        .await
        .unwrap();
    let rows = reopened.recent_rows(table).await.unwrap();
    assert_eq!(rows.len(), 2);
    assert_eq!(row_ids(&reopened, table).await, vec![0, 1]);
    assert_eq!(rows[0].chunk_body.as_slice(), b"durable");
    assert_eq!(rows[0].arrow_schema.as_slice(), b"schema-v0");
}