wasm-dbms 0.8.2

Runtime-agnostic DBMS engine for WASM environments
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
// Rust guideline compliant 2026-02-28

mod index;

use wasm_dbms_api::prelude::{ColumnDef, IndexDef, Value};

pub use self::index::IndexOverlay;

/// The table overlay tracks uncommitted changes for a specific table.
#[derive(Debug, Clone)]
pub struct TableOverlay {
    /// The stack of operations applied to the table.
    pub(super) operations: Vec<Operation>,
    /// The index overlay for tracking uncommitted changes to indexes.
    pub(super) index_overlay: IndexOverlay,
    /// Table index definitions, used for knowing when to update indexes on insert/update/delete operations.
    indexes: &'static [IndexDef],
}

/// An operation within a [`TableOverlay`].
///
/// All operations are indexed by a primary key value.
#[derive(Debug, Clone)]
pub(super) enum Operation {
    Insert(Value, Vec<(ColumnDef, Value)>),
    Update(Value, Vec<(&'static str, Value)>),
    Delete(Value),
}

impl Operation {
    /// Get the primary key value associated with the operation.
    fn primary_key_value(&self) -> &Value {
        match self {
            Operation::Insert(pk, _) => pk,
            Operation::Update(pk, _) => pk,
            Operation::Delete(pk) => pk,
        }
    }
}

impl TableOverlay {
    /// Creates a new [`TableOverlay`] with the given index definitions.
    pub fn new(indexes: &'static [IndexDef]) -> Self {
        Self {
            operations: Vec::default(),
            index_overlay: IndexOverlay::default(),
            indexes,
        }
    }

    /// Inserts a new record into the overlay.
    ///
    /// Indexed column values are extracted from `record` and added to the index overlay.
    pub fn insert(&mut self, pk: Value, record: Vec<(ColumnDef, Value)>) {
        for index_def in self.indexes {
            let indexed_values = Self::extract_indexed_values(index_def.columns(), &record);
            self.index_overlay
                .insert(index_def.columns(), indexed_values, pk.clone());
        }
        self.operations.push(Operation::Insert(pk, record));
    }

    /// Updates a record in the overlay.
    ///
    /// `current_row` is the full row before the update, used to compute old indexed values
    /// for any indexes whose columns are affected by the update.
    pub fn update(
        &mut self,
        pk: Value,
        updates: Vec<(&'static str, Value)>,
        current_row: &[(ColumnDef, Value)],
    ) {
        for index_def in self.indexes {
            let columns = index_def.columns();
            let affects_index = columns
                .iter()
                .any(|col| updates.iter().any(|(name, _)| name == col));
            if affects_index {
                let old_values = Self::extract_indexed_values(columns, current_row);
                let new_values =
                    Self::compute_updated_indexed_values(columns, current_row, &updates);
                self.index_overlay
                    .update(columns, old_values, new_values, pk.clone());
            }
        }
        self.operations.push(Operation::Update(pk, updates));
    }

    /// Marks a record as deleted in the overlay.
    ///
    /// `current_row` is the full row being deleted, used to remove its indexed values
    /// from the index overlay.
    pub fn delete(&mut self, pk: Value, current_row: &[(ColumnDef, Value)]) {
        for index_def in self.indexes {
            let indexed_values = Self::extract_indexed_values(index_def.columns(), current_row);
            self.index_overlay
                .delete(index_def.columns(), indexed_values, pk.clone());
        }
        self.operations.push(Operation::Delete(pk));
    }

    /// Extracts the values for the given indexed columns from a row.
    fn extract_indexed_values(columns: &[&'static str], row: &[(ColumnDef, Value)]) -> Vec<Value> {
        columns
            .iter()
            .map(|col_name| {
                row.iter()
                    .find(|(col_def, _)| col_def.name == *col_name)
                    .map(|(_, value)| value.clone())
                    .unwrap_or(Value::Null)
            })
            .collect()
    }

    /// Computes the new indexed values after applying updates to a row.
    ///
    /// For each indexed column, uses the updated value if present in `updates`,
    /// otherwise falls back to the current row value.
    fn compute_updated_indexed_values(
        columns: &[&'static str],
        current_row: &[(ColumnDef, Value)],
        updates: &[(&'static str, Value)],
    ) -> Vec<Value> {
        columns
            .iter()
            .map(|col_name| {
                updates
                    .iter()
                    .find(|(name, _)| name == col_name)
                    .map(|(_, value)| value.clone())
                    .or_else(|| {
                        current_row
                            .iter()
                            .find(|(col_def, _)| col_def.name == *col_name)
                            .map(|(_, value)| value.clone())
                    })
                    .unwrap_or(Value::Null)
            })
            .collect()
    }

    /// Returns an iterator over the inserted records which are still valid after the operation stack.
    pub fn iter_inserted(&self) -> impl Iterator<Item = Vec<(ColumnDef, Value)>> {
        self.operations.iter().filter_map(|op| {
            if let Operation::Insert(_, record) = op {
                self.patch_row(record.clone())
            } else {
                None
            }
        })
    }

    /// Patches a row with the overlay changes.
    ///
    /// The return may be [`None`] if the row has been deleted in the overlay.
    ///
    /// The current PK is tracked across operations so that a PK update
    /// (e.g. `id: 1 → 2`) correctly chains subsequent operations keyed by
    /// the new PK value. See <https://github.com/veeso/wasm-dbms/issues/65>.
    ///
    /// NOTE: `clippy::manual_try_fold`
    /// this lint is TOTALLY WRONG HERE. We may have a row which first becomes None (deleted), then an insert again returns Some.
    #[allow(clippy::manual_try_fold)]
    pub fn patch_row(&self, row: Vec<(ColumnDef, Value)>) -> Option<Vec<(ColumnDef, Value)>> {
        // get primary key value
        let mut current_pk = row
            .iter()
            .find(|(col_def, _)| col_def.primary_key)
            .map(|(_, value)| value)
            .cloned()?;

        // apply all operations for this primary key to the row, tracking PK changes
        let mut current_row = Some(row);
        for op in &self.operations {
            if op.primary_key_value() != &current_pk {
                continue;
            }
            current_row = self.apply_operation(current_row, op);
            // If an update changed the PK column, track the new value
            if let (Some(patched), Operation::Update(_, updates)) = (&current_row, op)
                && let Some((_, new_pk)) = updates.iter().find(|(name, _)| {
                    patched
                        .iter()
                        .any(|(col_def, _)| col_def.primary_key && col_def.name == *name)
                })
            {
                current_pk = new_pk.clone();
            }
        }

        current_row
    }

    /// Applies a single [`Operation`] to a row.
    fn apply_operation(
        &self,
        row: Option<Vec<(ColumnDef, Value)>>,
        op: &Operation,
    ) -> Option<Vec<(ColumnDef, Value)>> {
        match (row, op) {
            (_, Operation::Insert(_, record)) => Some(record.clone()), // it's definetely weird if we have `Some` row here, but just return the inserted record
            (_, Operation::Delete(_)) => None, // row is deleted; it would be weird to have `None` row here; just return None
            (None, Operation::Update(_, _)) => None, // trying to update a non-existing row; just return None
            (Some(mut existing_row), Operation::Update(_, updates)) => {
                for (col_name, new_value) in updates {
                    if let Some((_, value)) = existing_row
                        .iter_mut()
                        .find(|(col_def, _)| col_def.name == *col_name)
                    {
                        *value = new_value.clone();
                    }
                }
                Some(existing_row)
            }
        }
    }
}

#[cfg(test)]
mod tests {

    use wasm_dbms_api::prelude::DataTypeKind;

    use super::*;

    fn index_defs() -> &'static [IndexDef] {
        &[IndexDef(&["id"])]
    }

    fn name_index_defs() -> &'static [IndexDef] {
        &[IndexDef(&["name"])]
    }

    fn multi_index_defs() -> &'static [IndexDef] {
        &[IndexDef(&["id"]), IndexDef(&["name"])]
    }

    fn composite_index_defs() -> &'static [IndexDef] {
        &[IndexDef(&["name", "age"])]
    }

    fn col_def(name: &'static str, data_type: DataTypeKind, primary_key: bool) -> ColumnDef {
        ColumnDef {
            name,
            data_type,
            auto_increment: false,
            nullable: false,
            unique: false,
            primary_key,
            foreign_key: None,
        }
    }

    fn make_row(id: u32, name: &str, age: u32) -> Vec<(ColumnDef, Value)> {
        vec![
            (
                col_def("id", DataTypeKind::Uint32, true),
                Value::Uint32(id.into()),
            ),
            (
                col_def("name", DataTypeKind::Text, false),
                Value::Text(name.to_string().into()),
            ),
            (
                col_def("age", DataTypeKind::Uint32, false),
                Value::Uint32(age.into()),
            ),
        ]
    }

    #[test]
    fn test_should_get_op_pk() {
        let op = Operation::Insert(Value::Int32(1.into()), vec![]);
        assert_eq!(op.primary_key_value(), &Value::Int32(1.into()));
        let op = Operation::Update(Value::Text("key".to_string().into()), vec![]);
        assert_eq!(
            op.primary_key_value(),
            &Value::Text("key".to_string().into())
        );
        let op = Operation::Delete(Value::Null);
        assert_eq!(op.primary_key_value(), &Value::Null);
    }

    #[test]
    fn test_should_patch_row() {
        // let's make some ops
        let mut overlay = TableOverlay::new(index_defs());
        let pk = Value::Uint32(1.into());
        let row = vec![
            (
                ColumnDef {
                    name: "id",
                    data_type: DataTypeKind::Uint32,
                    auto_increment: false,
                    nullable: false,
                    primary_key: true,
                    unique: false,
                    foreign_key: None,
                },
                pk.clone(),
            ),
            (
                ColumnDef {
                    name: "name",
                    data_type: DataTypeKind::Text,
                    auto_increment: false,
                    nullable: false,
                    primary_key: false,
                    unique: false,
                    foreign_key: None,
                },
                Value::Text("Alice".to_string().into()),
            ),
            (
                ColumnDef {
                    name: "age",
                    data_type: DataTypeKind::Uint32,
                    auto_increment: false,
                    nullable: false,
                    primary_key: false,
                    unique: false,
                    foreign_key: None,
                },
                Value::Uint32(24.into()),
            ),
        ];
        // update name
        overlay.update(
            pk.clone(),
            vec![("name", Value::Text("Bob".to_string().into()))],
            &row,
        );
        // update age
        let row_after_name_update = vec![
            row[0].clone(),
            (row[1].0, Value::Text("Bob".to_string().into())),
            row[2].clone(),
        ];
        overlay.update(
            pk.clone(),
            vec![("age", Value::Uint32(30.into()))],
            &row_after_name_update,
        );

        // get patched row
        let row = overlay.patch_row(row).expect("should be Some");
        assert_eq!(
            row,
            vec![
                (
                    ColumnDef {
                        name: "id",
                        data_type: DataTypeKind::Uint32,
                        auto_increment: false,
                        nullable: false,
                        primary_key: true,
                        unique: false,
                        foreign_key: None,
                    },
                    pk.clone(),
                ),
                (
                    ColumnDef {
                        name: "name",
                        data_type: DataTypeKind::Text,
                        auto_increment: false,
                        nullable: false,
                        primary_key: false,
                        unique: false,
                        foreign_key: None,
                    },
                    Value::Text("Bob".to_string().into()),
                ),
                (
                    ColumnDef {
                        name: "age",
                        data_type: DataTypeKind::Uint32,
                        auto_increment: false,
                        nullable: false,
                        primary_key: false,
                        unique: false,
                        foreign_key: None,
                    },
                    Value::Uint32(30.into()),
                ),
            ]
        );
    }

    #[test]
    fn test_should_iter_inserted_row_with_patch() {
        let mut overlay = TableOverlay::new(index_defs());
        let first_pk = Value::Uint32(1.into());
        overlay.insert(
            first_pk.clone(),
            vec![
                (
                    ColumnDef {
                        name: "id",
                        data_type: DataTypeKind::Uint32,
                        auto_increment: false,
                        nullable: false,
                        primary_key: true,
                        unique: false,
                        foreign_key: None,
                    },
                    first_pk.clone(),
                ),
                (
                    ColumnDef {
                        name: "name",
                        data_type: DataTypeKind::Text,
                        auto_increment: false,
                        nullable: false,
                        primary_key: false,
                        unique: false,
                        foreign_key: None,
                    },
                    Value::Text("Alice".to_string().into()),
                ),
                (
                    ColumnDef {
                        name: "age",
                        data_type: DataTypeKind::Uint32,
                        auto_increment: false,
                        nullable: false,
                        primary_key: false,
                        unique: false,
                        foreign_key: None,
                    },
                    Value::Uint32(24.into()),
                ),
            ],
        );
        let second_pk = Value::Uint32(2.into());
        overlay.insert(
            second_pk.clone(),
            vec![
                (
                    ColumnDef {
                        name: "id",
                        data_type: DataTypeKind::Uint32,
                        auto_increment: false,
                        nullable: false,
                        primary_key: true,
                        unique: false,
                        foreign_key: None,
                    },
                    second_pk.clone(),
                ),
                (
                    ColumnDef {
                        name: "name",
                        data_type: DataTypeKind::Text,
                        auto_increment: false,
                        nullable: false,
                        primary_key: false,
                        unique: false,
                        foreign_key: None,
                    },
                    Value::Text("Bob".to_string().into()),
                ),
                (
                    ColumnDef {
                        name: "age",
                        data_type: DataTypeKind::Uint32,
                        auto_increment: false,
                        nullable: false,
                        primary_key: false,
                        unique: false,
                        foreign_key: None,
                    },
                    Value::Uint32(32.into()),
                ),
            ],
        );

        // update second row
        let second_row = vec![
            (
                ColumnDef {
                    name: "id",
                    data_type: DataTypeKind::Uint32,
                    auto_increment: false,
                    nullable: false,
                    primary_key: true,
                    unique: false,
                    foreign_key: None,
                },
                second_pk.clone(),
            ),
            (
                ColumnDef {
                    name: "name",
                    data_type: DataTypeKind::Text,
                    auto_increment: false,
                    nullable: false,
                    primary_key: false,
                    unique: false,
                    foreign_key: None,
                },
                Value::Text("Bob".to_string().into()),
            ),
            (
                ColumnDef {
                    name: "age",
                    data_type: DataTypeKind::Uint32,
                    auto_increment: false,
                    nullable: false,
                    primary_key: false,
                    unique: false,
                    foreign_key: None,
                },
                Value::Uint32(32.into()),
            ),
        ];
        overlay.update(
            second_pk.clone(),
            vec![("age", Value::Uint32(33.into()))],
            &second_row,
        );

        // insert a third
        let third_pk = Value::Uint32(3.into());
        overlay.insert(
            third_pk.clone(),
            vec![
                (
                    ColumnDef {
                        name: "id",
                        data_type: DataTypeKind::Uint32,
                        auto_increment: false,
                        nullable: false,
                        primary_key: true,
                        unique: false,
                        foreign_key: None,
                    },
                    third_pk.clone(),
                ),
                (
                    ColumnDef {
                        name: "name",
                        data_type: DataTypeKind::Text,
                        auto_increment: false,
                        nullable: false,
                        primary_key: false,
                        unique: false,
                        foreign_key: None,
                    },
                    Value::Text("Charlie".to_string().into()),
                ),
                (
                    ColumnDef {
                        name: "age",
                        data_type: DataTypeKind::Uint32,
                        auto_increment: false,
                        nullable: false,
                        primary_key: false,
                        unique: false,
                        foreign_key: None,
                    },
                    Value::Uint32(28.into()),
                ),
            ],
        );

        // delete third
        let third_row = vec![
            (
                ColumnDef {
                    name: "id",
                    data_type: DataTypeKind::Uint32,
                    auto_increment: false,
                    nullable: false,
                    primary_key: true,
                    unique: false,
                    foreign_key: None,
                },
                third_pk.clone(),
            ),
            (
                ColumnDef {
                    name: "name",
                    data_type: DataTypeKind::Text,
                    auto_increment: false,
                    nullable: false,
                    primary_key: false,
                    unique: false,
                    foreign_key: None,
                },
                Value::Text("Charlie".to_string().into()),
            ),
            (
                ColumnDef {
                    name: "age",
                    data_type: DataTypeKind::Uint32,
                    auto_increment: false,
                    nullable: false,
                    primary_key: false,
                    unique: false,
                    foreign_key: None,
                },
                Value::Uint32(28.into()),
            ),
        ];
        overlay.delete(third_pk.clone(), &third_row);

        // update second row (again) — age was updated to 33 above
        let second_row_after_age_update = vec![
            second_row[0].clone(),
            second_row[1].clone(),
            (second_row[2].0, Value::Uint32(33.into())),
        ];
        overlay.update(
            second_pk.clone(),
            vec![("name", Value::Text("Robert".to_string().into()))],
            &second_row_after_age_update,
        );

        let inserted_rows: Vec<_> = overlay.iter_inserted().collect();
        assert_eq!(inserted_rows.len(), 2); // third should be deleted
        assert_eq!(
            inserted_rows[0],
            vec![
                (
                    ColumnDef {
                        name: "id",
                        data_type: DataTypeKind::Uint32,
                        auto_increment: false,
                        nullable: false,
                        primary_key: true,
                        unique: false,
                        foreign_key: None,
                    },
                    first_pk.clone(),
                ),
                (
                    ColumnDef {
                        name: "name",
                        data_type: DataTypeKind::Text,
                        auto_increment: false,
                        nullable: false,
                        primary_key: false,
                        unique: false,
                        foreign_key: None,
                    },
                    Value::Text("Alice".to_string().into()),
                ),
                (
                    ColumnDef {
                        name: "age",
                        data_type: DataTypeKind::Uint32,
                        auto_increment: false,
                        nullable: false,
                        primary_key: false,
                        unique: false,
                        foreign_key: None,
                    },
                    Value::Uint32(24.into()),
                ),
            ]
        );
        assert_eq!(
            inserted_rows[1],
            vec![
                (
                    ColumnDef {
                        name: "id",
                        data_type: DataTypeKind::Uint32,
                        auto_increment: false,
                        nullable: false,
                        primary_key: true,
                        unique: false,
                        foreign_key: None,
                    },
                    second_pk.clone(),
                ),
                (
                    ColumnDef {
                        name: "name",
                        data_type: DataTypeKind::Text,
                        auto_increment: false,
                        nullable: false,
                        primary_key: false,
                        unique: false,
                        foreign_key: None,
                    },
                    Value::Text("Robert".to_string().into()), // patched name
                ),
                (
                    ColumnDef {
                        name: "age",
                        data_type: DataTypeKind::Uint32,
                        auto_increment: false,
                        nullable: false,
                        primary_key: false,
                        unique: false,
                        foreign_key: None,
                    },
                    Value::Uint32(33.into()), // patched age
                ),
            ]
        );
    }

    // -- extract_indexed_values tests --

    #[test]
    fn test_extract_indexed_values_single_column() {
        let row = make_row(1, "Alice", 24);
        let values = TableOverlay::extract_indexed_values(&["name"], &row);
        assert_eq!(values, vec![Value::Text("Alice".to_string().into())]);
    }

    #[test]
    fn test_extract_indexed_values_composite() {
        let row = make_row(1, "Alice", 24);
        let values = TableOverlay::extract_indexed_values(&["name", "age"], &row);
        assert_eq!(
            values,
            vec![
                Value::Text("Alice".to_string().into()),
                Value::Uint32(24.into()),
            ]
        );
    }

    #[test]
    fn test_extract_indexed_values_missing_column_returns_null() {
        let row = make_row(1, "Alice", 24);
        let values = TableOverlay::extract_indexed_values(&["nonexistent"], &row);
        assert_eq!(values, vec![Value::Null]);
    }

    // -- compute_updated_indexed_values tests --

    #[test]
    fn test_compute_updated_indexed_values_with_update() {
        let row = make_row(1, "Alice", 24);
        let updates = vec![("name", Value::Text("Bob".to_string().into()))];
        let values = TableOverlay::compute_updated_indexed_values(&["name"], &row, &updates);
        assert_eq!(values, vec![Value::Text("Bob".to_string().into())]);
    }

    #[test]
    fn test_compute_updated_indexed_values_falls_back_to_current_row() {
        let row = make_row(1, "Alice", 24);
        let updates = vec![("age", Value::Uint32(30.into()))];
        // "name" not in updates, should fall back to row value
        let values = TableOverlay::compute_updated_indexed_values(&["name"], &row, &updates);
        assert_eq!(values, vec![Value::Text("Alice".to_string().into())]);
    }

    #[test]
    fn test_compute_updated_indexed_values_composite_partial_update() {
        let row = make_row(1, "Alice", 24);
        let updates = vec![("age", Value::Uint32(30.into()))];
        let values = TableOverlay::compute_updated_indexed_values(&["name", "age"], &row, &updates);
        assert_eq!(
            values,
            vec![
                Value::Text("Alice".to_string().into()), // unchanged
                Value::Uint32(30.into()),                // updated
            ]
        );
    }

    // -- insert index overlay integration tests --

    #[test]
    fn test_insert_populates_index_overlay() {
        let mut overlay = TableOverlay::new(name_index_defs());
        let row = make_row(1, "Alice", 24);
        overlay.insert(Value::Uint32(1.into()), row);

        let added = overlay
            .index_overlay
            .added_pks(&["name"], &[Value::Text("Alice".to_string().into())]);
        assert!(added.contains(&Value::Uint32(1.into())));
    }

    #[test]
    fn test_insert_populates_multiple_indexes() {
        let mut overlay = TableOverlay::new(multi_index_defs());
        let row = make_row(1, "Alice", 24);
        overlay.insert(Value::Uint32(1.into()), row);

        let id_added = overlay
            .index_overlay
            .added_pks(&["id"], &[Value::Uint32(1.into())]);
        assert!(id_added.contains(&Value::Uint32(1.into())));

        let name_added = overlay
            .index_overlay
            .added_pks(&["name"], &[Value::Text("Alice".to_string().into())]);
        assert!(name_added.contains(&Value::Uint32(1.into())));
    }

    #[test]
    fn test_insert_populates_composite_index() {
        let mut overlay = TableOverlay::new(composite_index_defs());
        let row = make_row(1, "Alice", 24);
        overlay.insert(Value::Uint32(1.into()), row);

        let added = overlay.index_overlay.added_pks(
            &["name", "age"],
            &[
                Value::Text("Alice".to_string().into()),
                Value::Uint32(24.into()),
            ],
        );
        assert!(added.contains(&Value::Uint32(1.into())));
    }

    // -- delete index overlay integration tests --

    #[test]
    fn test_delete_populates_index_overlay_removed() {
        let mut overlay = TableOverlay::new(name_index_defs());
        let row = make_row(1, "Alice", 24);
        overlay.delete(Value::Uint32(1.into()), &row);

        let removed = overlay
            .index_overlay
            .removed_pks(&["name"], &[Value::Text("Alice".to_string().into())]);
        assert!(removed.contains(&Value::Uint32(1.into())));
    }

    #[test]
    fn test_delete_populates_multiple_indexes_removed() {
        let mut overlay = TableOverlay::new(multi_index_defs());
        let row = make_row(1, "Alice", 24);
        overlay.delete(Value::Uint32(1.into()), &row);

        let id_removed = overlay
            .index_overlay
            .removed_pks(&["id"], &[Value::Uint32(1.into())]);
        assert!(id_removed.contains(&Value::Uint32(1.into())));

        let name_removed = overlay
            .index_overlay
            .removed_pks(&["name"], &[Value::Text("Alice".to_string().into())]);
        assert!(name_removed.contains(&Value::Uint32(1.into())));
    }

    // -- update index overlay integration tests --

    #[test]
    fn test_update_indexed_column_updates_overlay() {
        let mut overlay = TableOverlay::new(name_index_defs());
        let row = make_row(1, "Alice", 24);
        overlay.update(
            Value::Uint32(1.into()),
            vec![("name", Value::Text("Bob".to_string().into()))],
            &row,
        );

        // old value should be in removed
        let removed = overlay
            .index_overlay
            .removed_pks(&["name"], &[Value::Text("Alice".to_string().into())]);
        assert!(removed.contains(&Value::Uint32(1.into())));

        // new value should be in added
        let added = overlay
            .index_overlay
            .added_pks(&["name"], &[Value::Text("Bob".to_string().into())]);
        assert!(added.contains(&Value::Uint32(1.into())));
    }

    #[test]
    fn test_update_non_indexed_column_does_not_affect_index_overlay() {
        let mut overlay = TableOverlay::new(name_index_defs());
        let row = make_row(1, "Alice", 24);
        // update "age" which is not in the name index
        overlay.update(
            Value::Uint32(1.into()),
            vec![("age", Value::Uint32(30.into()))],
            &row,
        );

        // index overlay should be empty — "age" is not indexed
        let added = overlay
            .index_overlay
            .added_pks(&["name"], &[Value::Text("Alice".to_string().into())]);
        assert!(added.is_empty());

        let removed = overlay
            .index_overlay
            .removed_pks(&["name"], &[Value::Text("Alice".to_string().into())]);
        assert!(removed.is_empty());
    }

    #[test]
    fn test_update_composite_index_partial_column_change() {
        let mut overlay = TableOverlay::new(composite_index_defs());
        let row = make_row(1, "Alice", 24);
        // update only "age" — composite index ["name", "age"] is affected
        overlay.update(
            Value::Uint32(1.into()),
            vec![("age", Value::Uint32(30.into()))],
            &row,
        );

        // old composite key removed
        let removed = overlay.index_overlay.removed_pks(
            &["name", "age"],
            &[
                Value::Text("Alice".to_string().into()),
                Value::Uint32(24.into()),
            ],
        );
        assert!(removed.contains(&Value::Uint32(1.into())));

        // new composite key added (name unchanged, age updated)
        let added = overlay.index_overlay.added_pks(
            &["name", "age"],
            &[
                Value::Text("Alice".to_string().into()),
                Value::Uint32(30.into()),
            ],
        );
        assert!(added.contains(&Value::Uint32(1.into())));
    }

    // -- insert then delete consistency --

    #[test]
    fn test_patch_row_after_pk_update_applies_subsequent_operations() {
        // Reproduce #65: update PK (id: 1 → 2), then update another column keyed by new PK.
        // patch_row must apply both operations to the base row.
        let mut overlay = TableOverlay::new(index_defs());
        let original_pk = Value::Uint32(1.into());
        let row = make_row(1, "Alice", 24);

        // First op: update PK from 1 to 2 (keyed by original PK = 1)
        overlay.update(
            original_pk.clone(),
            vec![("id", Value::Uint32(2.into()))],
            &row,
        );

        // After the PK update, existing_rows_for_filter would return the row with PK = 2.
        // So the second op is keyed by the new PK = 2.
        let new_pk = Value::Uint32(2.into());
        let row_after_pk_update = make_row(2, "Alice", 24);
        overlay.update(
            new_pk.clone(),
            vec![("name", Value::Text("Bob".to_string().into()))],
            &row_after_pk_update,
        );

        // patch_row receives the original base row (PK = 1)
        let patched = overlay.patch_row(row).expect("row should not be deleted");
        assert_eq!(
            patched[0].1,
            Value::Uint32(2.into()),
            "PK should be updated to 2"
        );
        assert_eq!(
            patched[1].1,
            Value::Text("Bob".to_string().into()),
            "name should be updated to Bob"
        );
        assert_eq!(
            patched[2].1,
            Value::Uint32(24.into()),
            "age should remain 24"
        );
    }

    #[test]
    fn test_patch_row_after_pk_update_then_delete() {
        // Reproduce #65 variant: update PK then delete by new PK.
        let mut overlay = TableOverlay::new(index_defs());
        let original_pk = Value::Uint32(1.into());
        let row = make_row(1, "Alice", 24);

        // Update PK: 1 → 2
        overlay.update(
            original_pk.clone(),
            vec![("id", Value::Uint32(2.into()))],
            &row,
        );

        // Delete by new PK = 2
        let row_after_pk_update = make_row(2, "Alice", 24);
        overlay.delete(Value::Uint32(2.into()), &row_after_pk_update);

        // patch_row with original row (PK = 1) should return None (deleted)
        let patched = overlay.patch_row(row);
        assert!(
            patched.is_none(),
            "row should be deleted after PK update + delete"
        );
    }

    #[test]
    fn test_insert_then_delete_leaves_clean_index_overlay() {
        let mut overlay = TableOverlay::new(name_index_defs());
        let row = make_row(1, "Alice", 24);
        overlay.insert(Value::Uint32(1.into()), row.clone());
        overlay.delete(Value::Uint32(1.into()), &row);

        // Insert added the pk, delete should remove from added.
        // Since the entry was overlay-only (never in base index), it should NOT be in removed.
        let added = overlay
            .index_overlay
            .added_pks(&["name"], &[Value::Text("Alice".to_string().into())]);
        assert!(!added.contains(&Value::Uint32(1.into())));

        let removed = overlay
            .index_overlay
            .removed_pks(&["name"], &[Value::Text("Alice".to_string().into())]);
        assert!(!removed.contains(&Value::Uint32(1.into())));
    }
}