delta_kernel 0.24.0

Core crate providing a Delta/Deltalake implementation focused on interoperability with a wide range of query engines.
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
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
//! Tests for P&M replay and ICT reads with CRC files.
//!
//! Each test sets up an in-memory Delta log with V2 checkpoint JSONs, commit files, and CRC files,
//! then verifies that Protocol & Metadata loading and ICT reads resolve correctly.

use std::collections::HashMap;
use std::sync::Arc;

use rstest::rstest;
use serde_json::{json, Value};
use test_utils::{assert_result_error_with_message, delta_path_for_version};
use url::Url;

use super::LogSegment;
use crate::actions::{DomainMetadata, Format, Metadata, Protocol, SetTransaction};
use crate::crc::{
    try_read_crc_file, Crc, DomainMetadataState, FileSizeHistogram, SetTransactionState,
};
use crate::engine::sync::SyncEngine;
use crate::object_store::memory::InMemory;
use crate::object_store::ObjectStoreExt as _;
use crate::path::ParsedLogPath;
use crate::{DeltaResult, Engine, Snapshot};

// ============================================================================
// Expected values
// ============================================================================

const SCHEMA_STRING: &str = r#"{"type":"struct","fields":[{"name":"id","type":"integer","nullable":true,"metadata":{}},{"name":"val","type":"string","nullable":true,"metadata":{}}]}"#;

const COMMIT_INFO_TIMESTAMP: i64 = 1587968586154;
const DEFAULT_OPERATION: &str = "TEST_OP";

fn protocol_v2() -> Protocol {
    Protocol::try_new_modern(["v2Checkpoint"], ["v2Checkpoint"]).unwrap()
}

fn protocol_v2_dv() -> Protocol {
    Protocol::try_new_modern(
        ["v2Checkpoint", "deletionVectors"],
        ["v2Checkpoint", "deletionVectors"],
    )
    .unwrap()
}

fn protocol_v2_dv_ntz() -> Protocol {
    Protocol::try_new_modern(
        ["v2Checkpoint", "deletionVectors", "timestampNtz"],
        ["v2Checkpoint", "deletionVectors", "timestampNtz"],
    )
    .unwrap()
}

fn protocol_v2_ict() -> Protocol {
    Protocol::try_new_modern(["v2Checkpoint"], ["v2Checkpoint", "inCommitTimestamp"]).unwrap()
}

fn metadata_a() -> Metadata {
    Metadata::new_unchecked(
        "aaa",
        None,
        None,
        Format::default(),
        SCHEMA_STRING,
        vec![],
        Some(1587968585495),
        HashMap::new(),
    )
}

fn metadata_b() -> Metadata {
    Metadata::new_unchecked(
        "bbb",
        None,
        None,
        Format::default(),
        SCHEMA_STRING,
        vec![],
        Some(1587968585495),
        HashMap::new(),
    )
}

fn metadata_ict() -> Metadata {
    Metadata::new_unchecked(
        "5fba94ed-9794-4965-ba6e-6ee3c0d22af9",
        None,
        None,
        Format::default(),
        SCHEMA_STRING,
        vec![],
        Some(1587968585495),
        HashMap::from([(
            "delta.enableInCommitTimestamps".to_string(),
            "true".to_string(),
        )]),
    )
}

// ============================================================================
// Action JSON helpers
// ============================================================================

fn create_table_actions() -> Vec<serde_json::Value> {
    vec![
        commit_info("CREATE", None),
        protocol(protocol_v2()),
        metadata(metadata_a()),
    ]
}

fn create_table_actions_with_ict() -> Vec<serde_json::Value> {
    vec![
        commit_info("CREATE", None),
        protocol(protocol_v2_ict()),
        metadata(metadata_a()),
    ]
}

/// A commitInfo action with the given operation and optional in-commit timestamp.
fn commit_info(op: &str, ict: Option<i64>) -> serde_json::Value {
    let mut obj = json!({"commitInfo": {"timestamp": COMMIT_INFO_TIMESTAMP, "operation": op}});
    if let Some(ict) = ict {
        obj["commitInfo"]["inCommitTimestamp"] = json!(ict);
    }
    obj
}

fn protocol(p: Protocol) -> serde_json::Value {
    json!({"protocol": serde_json::to_value(&p).unwrap()})
}

fn metadata(m: Metadata) -> serde_json::Value {
    json!({"metaData": serde_json::to_value(&m).unwrap()})
}

fn add(path: &str, size: i64) -> serde_json::Value {
    json!({"add": {
        "path": path,
        "size": size,
        "partitionValues": {},
        "modificationTime": 0,
        "dataChange": true,
    }})
}

fn remove(path: &str, size: Option<i64>) -> serde_json::Value {
    let mut obj = json!({"remove": {
        "path": path,
        "dataChange": true,
        "deletionTimestamp": 0,
    }});
    if let Some(s) = size {
        obj["remove"]["size"] = json!(s);
    }
    obj
}

fn domain_metadata(domain: &str, configuration: &str) -> serde_json::Value {
    json!({"domainMetadata": {
        "domain": domain,
        "configuration": configuration,
        "removed": false,
    }})
}

fn domain_metadata_tombstone(domain: &str) -> serde_json::Value {
    json!({"domainMetadata": {
        "domain": domain,
        "configuration": "",
        "removed": true,
    }})
}

fn set_txn(app_id: &str, version: i64, last_updated: Option<i64>) -> serde_json::Value {
    let mut obj = json!({"txn": {"appId": app_id, "version": version}});
    if let Some(lu) = last_updated {
        obj["txn"]["lastUpdated"] = json!(lu);
    }
    obj
}

fn crc_json(
    protocol: &Protocol,
    metadata: &Metadata,
    ict: Option<i64>,
    file_sizes: &[i64],
) -> serde_json::Value {
    let total_bytes: i64 = file_sizes.iter().sum();
    let num_files = file_sizes.len() as i64;
    let mut obj = json!({
        "tableSizeBytes": total_bytes,
        "numFiles": num_files,
        "numMetadata": 1,
        "numProtocol": 1,
        "metadata": serde_json::to_value(metadata).unwrap(),
        "protocol": serde_json::to_value(protocol).unwrap(),
    });
    if !file_sizes.is_empty() {
        let mut hist = FileSizeHistogram::create_default();
        for &s in file_sizes {
            hist.insert(s).unwrap();
        }
        obj["fileSizeHistogram"] = serde_json::to_value(&hist).unwrap();
    }
    if let Some(ict) = ict {
        obj["inCommitTimestampOpt"] = json!(ict);
    }
    obj
}

// ============================================================================
// CrcReadTest builder
// ============================================================================

/// Operations that can be applied to an in-memory Delta log.
enum Op {
    V2Checkpoint {
        version: u64,
        protocol: Protocol,
        metadata: Metadata,
    },
    Commit {
        version: u64,
        actions: Vec<serde_json::Value>,
    },
    Crc {
        version: u64,
        protocol: Protocol,
        metadata: Metadata,
        ict: Option<i64>,
        file_sizes: Vec<i64>,
    },
    CorruptCrc {
        version: u64,
    },
}

/// Declarative test builder: accumulate log operations, then build and assert.
struct CrcReadTest {
    ops: Vec<Op>,
}

impl CrcReadTest {
    fn new() -> Self {
        Self { ops: vec![] }
    }

    fn v2_checkpoint(mut self, version: u64, protocol: Protocol, metadata: Metadata) -> Self {
        self.ops.push(Op::V2Checkpoint {
            version,
            protocol,
            metadata,
        });
        self
    }

    /// Write a commit file containing exactly the given actions, in order.
    fn commit(
        mut self,
        version: u64,
        actions: impl IntoIterator<Item = serde_json::Value>,
    ) -> Self {
        let actions: Vec<serde_json::Value> = actions.into_iter().collect();
        self.ops.push(Op::Commit { version, actions });
        self
    }

    fn crc(
        self,
        version: u64,
        protocol: Protocol,
        metadata: Metadata,
        ict: impl Into<Option<i64>>,
    ) -> Self {
        self.crc_with_files(version, protocol, metadata, ict, &[])
    }

    /// Write a CRC file whose on-disk `fileSizeHistogram` is built from `file_sizes`.
    fn crc_with_files(
        mut self,
        version: u64,
        protocol: Protocol,
        metadata: Metadata,
        ict: impl Into<Option<i64>>,
        file_sizes: &[i64],
    ) -> Self {
        self.ops.push(Op::Crc {
            version,
            protocol,
            metadata,
            ict: ict.into(),
            file_sizes: file_sizes.to_vec(),
        });
        self
    }

    fn corrupt_crc(mut self, version: u64) -> Self {
        self.ops.push(Op::CorruptCrc { version });
        self
    }

    /// Execute all operations, returning a built test that can be asserted against.
    async fn build(self) -> BuiltCrcTest {
        Self::validate_ops(&self.ops);

        let store = Arc::new(InMemory::new());
        let url = Url::parse("memory:///").unwrap();
        let engine = SyncEngine::new_with_store(store.clone());

        for op in self.ops {
            match op {
                Op::Commit { version, actions } => {
                    let content = actions
                        .iter()
                        .map(|a| a.to_string())
                        .collect::<Vec<_>>()
                        .join("\n");
                    put(&store, version, "json", &content).await;
                }
                Op::Crc {
                    version,
                    ref protocol,
                    ref metadata,
                    ict,
                    ref file_sizes,
                } => {
                    put(
                        &store,
                        version,
                        "crc",
                        &crc_json(protocol, metadata, ict, file_sizes).to_string(),
                    )
                    .await;
                }
                Op::CorruptCrc { version } => {
                    put(&store, version, "crc", "CORRUPT_CRC_DATA").await;
                }
                Op::V2Checkpoint {
                    version,
                    protocol: p,
                    metadata: m,
                } => {
                    const V2_CKPT_SUFFIX: &str =
                        "checkpoint.00000000-0000-0000-0000-000000000000.json";
                    let content = format!(
                        "{}\n{}\n{}",
                        protocol(p),
                        metadata(m),
                        json!({"checkpointMetadata": {"version": 2}})
                    );
                    put(&store, version, V2_CKPT_SUFFIX, &content).await;
                }
            }
        }

        BuiltCrcTest { engine, url }
    }

    /// Asserts the accumulated ops are valid before `build()` writes anything.
    fn validate_ops(ops: &[Op]) {
        let mut prev: Option<u64> = None;
        for op in ops {
            if let Op::Commit { version, actions } = op {
                // Each commit must contain at least one action.
                assert!(
                    !actions.is_empty(),
                    "commit(v{version}, []): a commit must contain at least one action",
                );
                // Commit versions must be strictly increasing (and therefore unique).
                if let Some(p) = prev {
                    assert!(
                        *version > p,
                        "commit(v{version}): commits must be added in strictly increasing \
                         version order (previous commit was at v{p})",
                    );
                }
                prev = Some(*version);
            }
        }
    }
}

struct BuiltCrcTest {
    engine: SyncEngine,
    url: Url,
}

impl BuiltCrcTest {
    /// Construct a `LogSegment` directly from the store state (no `Snapshot`) and run
    /// `build_incremental_crc_from_base` against `base`.
    fn incrementally_build_crc(&self, base: &Crc) -> DeltaResult<Crc> {
        let storage = self.engine.storage_handler();
        let log_root = self.url.join("_delta_log/").unwrap();
        let log_segment =
            LogSegment::for_snapshot_impl(storage.as_ref(), log_root, vec![], None, None)?;
        log_segment.build_incremental_crc_from_base(&self.engine, base)
    }

    /// Read the on-disk CRC at `version` from this test's log.
    fn read_crc_at(&self, version: u64) -> DeltaResult<Crc> {
        try_read_crc_file(
            &self.engine,
            &ParsedLogPath::create_parsed_crc(&self.url, version),
        )
    }

    /// Build a snapshot at `version` (or latest if `None`) and return it
    /// alongside a human-readable label like `"v2"` or `"latest"`.
    fn snapshot_at(&self, version: Option<u64>) -> (crate::snapshot::SnapshotRef, String) {
        let mut builder = Snapshot::builder_for(self.url.clone());
        if let Some(v) = version {
            builder = builder.at_version(v);
        }
        let snapshot = builder.build(&self.engine).unwrap();
        let label = version.map_or("latest".to_string(), |v| format!("v{v}"));
        (snapshot, label)
    }

    fn assert_p_m(
        &self,
        version: impl Into<Option<u64>>,
        expected_protocol: &Protocol,
        expected_metadata: &Metadata,
    ) {
        let (snapshot, label) = self.snapshot_at(version.into());
        let table_config = snapshot.table_configuration();
        assert_eq!(
            table_config.protocol(),
            expected_protocol,
            "Protocol mismatch at {label}"
        );
        assert_eq!(
            table_config.metadata(),
            expected_metadata,
            "Metadata mismatch at {label}"
        );
    }

    fn assert_ict(&self, version: impl Into<Option<u64>>, expected_ict: Option<i64>) {
        let (snapshot, label) = self.snapshot_at(version.into());
        let ict = snapshot.get_in_commit_timestamp(&self.engine).unwrap();
        assert_eq!(ict, expected_ict, "ICT mismatch at {label}");
    }
}

async fn put(store: &InMemory, version: u64, suffix: &str, content: &str) {
    store
        .put(
            &delta_path_for_version(version, suffix),
            content.as_bytes().to_vec().into(),
        )
        .await
        .unwrap();
}

// TODO: Time travel tests
// TODO: Log compaction tests
// TODO: build_from tests
// TODO: _last_checkpoint tests

// ============================================================================
// Tests: Baseline (no CRC)
// ============================================================================

#[tokio::test]
async fn test_get_p_m_from_delta_no_checkpoint() {
    CrcReadTest::new()
        .commit(
            0, // <-- P & M from here
            [
                commit_info(DEFAULT_OPERATION, None),
                protocol(protocol_v2()),
                metadata(metadata_a()),
            ],
        )
        .commit(1, [commit_info(DEFAULT_OPERATION, None)])
        .commit(2, [commit_info(DEFAULT_OPERATION, None)])
        .build()
        .await
        .assert_p_m(None, &protocol_v2(), &metadata_a());
}

#[tokio::test]
async fn test_get_p_and_m_from_different_deltas() {
    CrcReadTest::new()
        .v2_checkpoint(0, protocol_v2(), metadata_a())
        .commit(
            1, // <-- P from here
            [
                commit_info(DEFAULT_OPERATION, None),
                protocol(protocol_v2_dv()),
            ],
        )
        .commit(
            2, // <-- M from here
            [commit_info(DEFAULT_OPERATION, None), metadata(metadata_b())],
        )
        .build()
        .await
        .assert_p_m(None, &protocol_v2_dv(), &metadata_b());
}

#[tokio::test]
async fn test_get_p_m_from_checkpoint() {
    CrcReadTest::new()
        .v2_checkpoint(0, protocol_v2(), metadata_a()) // <-- P & M from here
        .commit(1, [commit_info(DEFAULT_OPERATION, None)])
        .commit(2, [commit_info(DEFAULT_OPERATION, None)])
        .build()
        .await
        .assert_p_m(None, &protocol_v2(), &metadata_a());
}

#[tokio::test]
async fn test_get_p_m_from_delta_after_checkpoint() {
    CrcReadTest::new()
        .v2_checkpoint(0, protocol_v2(), metadata_a())
        .commit(
            1, // <-- P & M from here
            [
                commit_info(DEFAULT_OPERATION, None),
                protocol(protocol_v2_dv()),
                metadata(metadata_b()),
            ],
        )
        .commit(2, [commit_info(DEFAULT_OPERATION, None)])
        .build()
        .await
        .assert_p_m(None, &protocol_v2_dv(), &metadata_b());
}

// ============================================================================
// Tests: CRC at target version
// ============================================================================

#[tokio::test]
async fn test_get_p_m_from_crc_at_target() {
    CrcReadTest::new()
        .v2_checkpoint(0, protocol_v2(), metadata_a())
        .commit(1, [commit_info(DEFAULT_OPERATION, None)])
        .commit(2, [commit_info(DEFAULT_OPERATION, None)])
        .crc(2, protocol_v2_dv(), metadata_b(), None) // <-- P & M from here
        .build()
        .await
        .assert_p_m(None, &protocol_v2_dv(), &metadata_b());
}

#[tokio::test]
async fn test_crc_preferred_over_delta_at_target() {
    // The P & M for the 002.crc and 002.json should NOT be different in practice.
    // We only do this for this test so we can differentiate which P & M is used.
    CrcReadTest::new()
        .v2_checkpoint(0, protocol_v2(), metadata_a())
        .commit(1, [commit_info(DEFAULT_OPERATION, None)])
        .commit(
            2, // <-- P from here
            [
                commit_info(DEFAULT_OPERATION, None),
                protocol(protocol_v2_dv()),
                metadata(metadata_a()),
            ],
        )
        .crc(2, protocol_v2_dv_ntz(), metadata_b(), None) // <-- P & M from here
        .build()
        .await
        .assert_p_m(None, &protocol_v2_dv_ntz(), &metadata_b());
}

#[tokio::test]
async fn test_corrupt_crc_at_target_falls_back() {
    CrcReadTest::new()
        .v2_checkpoint(0, protocol_v2(), metadata_a()) // <-- P & M from here
        .commit(1, [commit_info(DEFAULT_OPERATION, None)])
        .commit(2, [commit_info(DEFAULT_OPERATION, None)])
        .corrupt_crc(2) // <-- Corrupt! Fall back to replay.
        .build()
        .await
        .assert_p_m(None, &protocol_v2(), &metadata_a());
}

#[tokio::test]
async fn test_crc_wins_over_checkpoint() {
    // The P & M for the 002.crc and the v2 checkpoint should NOT be different in practice.
    // We only do this for this test so we can differentiate which P & M is used.
    CrcReadTest::new()
        .v2_checkpoint(0, protocol_v2(), metadata_a())
        .commit(1, [commit_info(DEFAULT_OPERATION, None)])
        .commit(2, [commit_info(DEFAULT_OPERATION, None)])
        .v2_checkpoint(2, protocol_v2(), metadata_a())
        .crc(2, protocol_v2_dv(), metadata_b(), None) // <-- P & M from here
        .build()
        .await
        .assert_p_m(None, &protocol_v2_dv(), &metadata_b());
}

#[tokio::test]
async fn test_checkpoint_on_corrupt_crc() {
    CrcReadTest::new()
        .v2_checkpoint(0, protocol_v2(), metadata_a())
        .commit(1, [commit_info(DEFAULT_OPERATION, None)])
        .commit(2, [commit_info(DEFAULT_OPERATION, None)])
        .v2_checkpoint(2, protocol_v2(), metadata_a()) // <-- P & M from here
        .corrupt_crc(2) // <-- Corrupt! Fall back to replay.
        .build()
        .await
        .assert_p_m(None, &protocol_v2(), &metadata_a());
}

// ============================================================================
// Tests: CRC at version < target
// ============================================================================

#[tokio::test]
async fn test_crc_at_earlier_version() {
    CrcReadTest::new()
        .v2_checkpoint(0, protocol_v2(), metadata_a())
        .commit(1, [commit_info(DEFAULT_OPERATION, None)])
        .crc(1, protocol_v2_dv(), metadata_b(), None) // <-- P & M from here
        .commit(2, [commit_info(DEFAULT_OPERATION, None)])
        .build()
        .await
        .assert_p_m(None, &protocol_v2_dv(), &metadata_b());
}

#[tokio::test]
async fn test_get_p_from_newer_delta_over_older_crc() {
    CrcReadTest::new()
        .v2_checkpoint(0, protocol_v2(), metadata_a())
        .commit(1, [commit_info(DEFAULT_OPERATION, None)])
        .crc(1, protocol_v2_dv(), metadata_b(), None) // <-- M from here
        .commit(
            2,
            [
                commit_info(DEFAULT_OPERATION, None),
                protocol(protocol_v2_dv_ntz()),
            ],
        )
        .build()
        .await
        .assert_p_m(None, &protocol_v2_dv_ntz(), &metadata_b());
}

#[tokio::test]
async fn test_get_m_from_newer_delta_over_older_crc() {
    CrcReadTest::new()
        .v2_checkpoint(0, protocol_v2(), metadata_a())
        .commit(1, [commit_info(DEFAULT_OPERATION, None)])
        .crc(1, protocol_v2_dv(), metadata_b(), None) // <-- P from here
        .commit(
            2, // <-- M from here
            [commit_info(DEFAULT_OPERATION, None), metadata(metadata_a())],
        )
        .build()
        .await
        .assert_p_m(None, &protocol_v2_dv(), &metadata_a());
}

#[tokio::test]
async fn test_corrupt_crc_at_non_target_version_falls_back() {
    CrcReadTest::new()
        .v2_checkpoint(0, protocol_v2(), metadata_a()) // <-- P & M from here
        .commit(1, [commit_info(DEFAULT_OPERATION, None)])
        .corrupt_crc(1) // <-- Corrupt! Fall back to replay.
        .commit(2, [commit_info(DEFAULT_OPERATION, None)])
        .build()
        .await
        .assert_p_m(None, &protocol_v2(), &metadata_a());
}

#[tokio::test]
async fn test_crc_before_checkpoint_is_ignored() {
    CrcReadTest::new()
        .commit(
            0,
            [
                commit_info(DEFAULT_OPERATION, None),
                protocol(protocol_v2()),
                metadata(metadata_a()),
            ],
        )
        .commit(1, [commit_info(DEFAULT_OPERATION, None)])
        .crc(1, protocol_v2_dv_ntz(), metadata_b(), None)
        .v2_checkpoint(2, protocol_v2_dv(), metadata_a()) // <-- P & M from here
        .commit(3, [commit_info(DEFAULT_OPERATION, None)])
        .build()
        .await
        .assert_p_m(None, &protocol_v2_dv(), &metadata_a());
}

// ============================================================================
// Tests: ICT from CRC
// ============================================================================

#[tokio::test]
async fn test_ict_from_crc_at_snapshot_version() {
    CrcReadTest::new()
        .v2_checkpoint(0, protocol_v2_ict(), metadata_ict())
        .commit(1, [commit_info(DEFAULT_OPERATION, Some(2000))])
        .crc(1, protocol_v2_ict(), metadata_ict(), 1000) // <-- ICT from here
        .build()
        .await
        .assert_ict(None, Some(1000));
}

#[tokio::test]
async fn test_ict_errors_when_crc_has_no_ict() {
    let setup = CrcReadTest::new()
        .v2_checkpoint(0, protocol_v2_ict(), metadata_ict())
        .commit(1, [commit_info(DEFAULT_OPERATION, Some(2000))])
        .crc(1, protocol_v2_ict(), metadata_ict(), None)
        .build()
        .await;

    let (snapshot, _) = setup.snapshot_at(None);
    let result = snapshot.get_in_commit_timestamp(&setup.engine);

    assert_result_error_with_message(
        result,
        "In-Commit Timestamp not found in CRC file at version 1",
    );
}

// ============================================================================
// Tests: CrcReadTest framework itself
// ============================================================================

/// Build a `CrcReadTest` containing one commit per version, in the given order.
async fn build_with_commit_versions(versions: &[u64]) -> BuiltCrcTest {
    let mut t = CrcReadTest::new();
    for &v in versions {
        t = t.commit(v, [commit_info(DEFAULT_OPERATION, None)]);
    }
    t.build().await
}

#[rstest::rstest]
#[case::single(&[0])]
#[case::consecutive(&[0, 1, 2])]
#[case::with_gaps(&[0, 5, 88, 1000])]
#[tokio::test]
async fn test_commit_versions_strictly_increasing_succeeds(#[case] versions: &[u64]) {
    build_with_commit_versions(versions).await;
}

#[rstest::rstest]
#[case::immediate_duplicate(&[0, 0])]
#[case::later_duplicate(&[0, 1, 1])]
#[case::immediate_decrease(&[5, 3])]
#[case::later_decrease(&[0, 5, 4])]
#[case::decrease_to_zero(&[2, 0])]
#[tokio::test]
#[should_panic(expected = "strictly increasing")]
async fn test_commit_versions_not_increasing_panics(#[case] versions: &[u64]) {
    build_with_commit_versions(versions).await;
}

#[tokio::test]
#[should_panic(expected = "at least one action")]
async fn test_commit_empty_actions_panics() {
    CrcReadTest::new().commit(0, []).build().await;
}

// ============================================================================
// Integration tests for incremental CRC replay
// ============================================================================

fn crc_with_dm_and_txn_states(dm: DomainMetadataState, txn: SetTransactionState) -> Crc {
    Crc {
        domain_metadata_state: dm,
        set_transaction_state: txn,
        ..Default::default()
    }
}

fn crc_complete_empty_dm_set_txn() -> Crc {
    crc_with_dm_and_txn_states(
        DomainMetadataState::Complete(HashMap::new()),
        SetTransactionState::Complete(HashMap::new()),
    )
}

fn dm_entry(domain: &str, configuration: &str) -> (String, DomainMetadata) {
    (
        domain.to_string(),
        DomainMetadata::new(domain.to_string(), configuration.to_string()),
    )
}

fn txn_entry(app_id: &str, version: i64, last_updated: Option<i64>) -> (String, SetTransaction) {
    (
        app_id.to_string(),
        SetTransaction::new(app_id.to_string(), version, last_updated),
    )
}

// === Protocol / metadata ===

#[rstest]
#[case::newest_p_wins(vec![protocol(protocol_v2_dv()), commit_info("WRITE", None)], protocol_v2_dv(), metadata_a())]
#[case::newest_m_wins(vec![metadata(metadata_b()), commit_info("WRITE", None)], protocol_v2(), metadata_b())]
#[case::base_preserved_when_segment_has_none(vec![commit_info("WRITE", None)], protocol_v2(), metadata_a())]
#[tokio::test]
async fn test_p_m_propagation(
    #[case] v1_actions: Vec<Value>,
    #[case] expected_p: Protocol,
    #[case] expected_m: Metadata,
) {
    let base = Crc {
        protocol: protocol_v2(),
        metadata: metadata_a(),
        ..crc_complete_empty_dm_set_txn()
    };
    let crc = CrcReadTest::new()
        .commit(0, create_table_actions())
        .commit(1, v1_actions)
        .build()
        .await
        .incrementally_build_crc(&base)
        .unwrap();
    assert_eq!(crc.protocol, expected_p);
    assert_eq!(crc.metadata, expected_m);
}

// === ICT ===

#[rstest]
#[case::captures_newest(vec![commit_info("WRITE", Some(2000))], Some(2000))]
#[case::none_when_newest_has_no_commit_info(vec![set_txn("app", 1, None)], None)]
#[tokio::test]
async fn test_ict_from_newest_commit_only_replaces_base(
    #[case] v2_actions: Vec<Value>,
    #[case] expected_ict: Option<i64>,
) {
    // Base carries an ICT so each case proves the delta's ICT replaces the base's
    let base = Crc {
        in_commit_timestamp_opt: Some(500),
        ..crc_complete_empty_dm_set_txn()
    };
    let crc = CrcReadTest::new()
        .commit(0, create_table_actions_with_ict())
        .commit(1, [commit_info("WRITE", Some(1000))])
        .commit(2, v2_actions)
        .build()
        .await
        .incrementally_build_crc(&base)
        .unwrap();
    assert_eq!(crc.in_commit_timestamp_opt, expected_ict);
}

// === Domain metadata ===

#[rstest]
#[case::complete_base(DomainMetadataState::Complete(
    HashMap::from([dm_entry("a", "base_a"), dm_entry("b", "base_b")])
))]
#[case::partial_base(DomainMetadataState::Partial(
    HashMap::from([dm_entry("a", "base_a"), dm_entry("b", "base_b")])
))]
#[tokio::test]
async fn test_dm_overrides_base_and_preserves_completeness(#[case] base_dm: DomainMetadataState) {
    let base = crc_with_dm_and_txn_states(base_dm, SetTransactionState::Complete(HashMap::new()));
    let crc = CrcReadTest::new()
        .commit(0, create_table_actions())
        .commit(
            1, // <-- updates "a"; "b" stays at base value
            [commit_info("WRITE", None), domain_metadata("a", "new_a")],
        )
        .build()
        .await
        .incrementally_build_crc(&base)
        .unwrap();
    // Variant preservation: `expect_*` panics if the base's variant did not survive.
    let map = match &base.domain_metadata_state {
        DomainMetadataState::Complete(_) => crc.domain_metadata_state.expect_complete(),
        DomainMetadataState::Partial(_) => crc.domain_metadata_state.expect_partial(),
    };
    assert_eq!(map["a"].configuration(), "new_a");
    assert_eq!(map["b"].configuration(), "base_b");
}

#[rstest]
#[case::complete_base(DomainMetadataState::Complete(
    HashMap::from([dm_entry("a", "base_a"), dm_entry("b", "base_b")])
))]
#[case::partial_base(DomainMetadataState::Partial(
    HashMap::from([dm_entry("a", "base_a"), dm_entry("b", "base_b")])
))]
#[tokio::test]
async fn test_dm_tombstone_removes_entry_from_base(#[case] base_dm: DomainMetadataState) {
    let base = crc_with_dm_and_txn_states(base_dm, SetTransactionState::Complete(HashMap::new()));
    let crc = CrcReadTest::new()
        .commit(0, create_table_actions())
        .commit(
            1, // <-- tombstone removes "a" from the base map
            [commit_info("WRITE", None), domain_metadata_tombstone("a")],
        )
        .build()
        .await
        .incrementally_build_crc(&base)
        .unwrap();
    let map = match &base.domain_metadata_state {
        DomainMetadataState::Complete(_) => crc.domain_metadata_state.expect_complete(),
        DomainMetadataState::Partial(_) => crc.domain_metadata_state.expect_partial(),
    };
    assert!(!map.contains_key("a"));
    assert_eq!(map["b"].configuration(), "base_b"); // <-- untouched entry preserved
}

#[tokio::test]
async fn test_dm_newer_commit_wins_over_older_commit_for_same_domain() {
    let crc = CrcReadTest::new()
        .commit(0, create_table_actions())
        .commit(1, [commit_info("WRITE", None), domain_metadata("d", "old")])
        .commit(
            2, // <-- same domain in two commits; newest wins
            [commit_info("WRITE", None), domain_metadata("d", "new")],
        )
        .build()
        .await
        .incrementally_build_crc(&crc_complete_empty_dm_set_txn())
        .unwrap();
    let map = crc.domain_metadata_state.expect_complete();
    assert_eq!(map["d"].configuration(), "new");
}

// === SetTransaction ===

#[rstest]
#[case::complete_base(SetTransactionState::Complete(HashMap::from([
    txn_entry("app_a", 1, Some(100)),
    txn_entry("app_b", 2, Some(200)),
])))]
#[case::partial_base(SetTransactionState::Partial(HashMap::from([
    txn_entry("app_a", 1, Some(100)),
    txn_entry("app_b", 2, Some(200)),
])))]
#[tokio::test]
async fn test_txn_overrides_base_and_preserves_completeness(#[case] base_txn: SetTransactionState) {
    let base = crc_with_dm_and_txn_states(DomainMetadataState::Complete(HashMap::new()), base_txn);
    let crc = CrcReadTest::new()
        .commit(0, create_table_actions())
        .commit(
            1,
            [set_txn("app_a", 42, Some(999)), commit_info("WRITE", None)],
        )
        .build()
        .await
        .incrementally_build_crc(&base)
        .unwrap();
    let map = match &base.set_transaction_state {
        SetTransactionState::Complete(_) => crc.set_transaction_state.expect_complete(),
        SetTransactionState::Partial(_) => crc.set_transaction_state.expect_partial(),
    };
    assert_eq!(map["app_a"].version, 42);
    assert_eq!(map["app_a"].last_updated, Some(999));
    assert_eq!(map["app_b"].version, 2);
    assert_eq!(map["app_b"].last_updated, Some(200));
}

#[tokio::test]
async fn test_txn_newer_commit_wins_over_older_commit_for_same_app() {
    let crc = CrcReadTest::new()
        .commit(0, create_table_actions())
        .commit(
            1,
            [set_txn("app", 1, Some(100)), commit_info("WRITE", None)],
        )
        .commit(
            2, // <-- same app in two commits; newest wins
            [set_txn("app", 42, Some(999)), commit_info("WRITE", None)],
        )
        .build()
        .await
        .incrementally_build_crc(&crc_complete_empty_dm_set_txn())
        .unwrap();
    let map = crc.set_transaction_state.expect_complete();
    assert_eq!(map["app"].version, 42);
    assert_eq!(map["app"].last_updated, Some(999));
}

// === File stats (in-memory base) ===

#[tokio::test]
async fn test_adds_and_removes_accumulate() {
    let crc = CrcReadTest::new()
        .commit(0, create_table_actions())
        .commit(1, [commit_info("WRITE", None), add("a", 100)])
        .commit(2, [commit_info("WRITE", None), add("b", 200)])
        .commit(3, [commit_info("WRITE", None), add("c", 300)])
        .commit(4, [remove("a", Some(100)), commit_info("WRITE", None)])
        .commit(5, [commit_info("WRITE", None), add("d", 400)])
        .commit(6, [remove("b", Some(200)), commit_info("WRITE", None)])
        .build()
        .await
        .incrementally_build_crc(&crc_complete_empty_dm_set_txn())
        .unwrap();
    assert!(crc.file_stats_state().is_complete());
    let stats = crc.file_stats().unwrap();
    assert_eq!(stats.num_files(), 2); // a and b removed; c and d remain
    assert_eq!(stats.table_size_bytes(), 700); // c (300) + d (400)
}

// === Indeterminate trips ===

// Three distinct ways a single commit can lose incremental safety.
#[rstest]
#[case::remove_no_size(vec![commit_info("WRITE", None), remove("orphan", None)])]
#[case::add_with_unsafe_op(vec![commit_info("ANALYZE STATS", None), add("a", 100)])]
#[case::add_with_no_commit_info(vec![add("a", 100)])]
#[tokio::test]
async fn test_trips_indeterminate(#[case] v1_actions: Vec<Value>) {
    // Base carries DM + txn entries so we can verify the indeterminate trip is scoped
    // to file stats and leaves other state alone.
    let base = crc_with_dm_and_txn_states(
        DomainMetadataState::Complete(HashMap::from([dm_entry("d", "base_d")])),
        SetTransactionState::Complete(HashMap::from([txn_entry("app", 5, Some(100))])),
    );
    let crc = CrcReadTest::new()
        .commit(0, create_table_actions())
        .commit(1, v1_actions)
        .build()
        .await
        .incrementally_build_crc(&base)
        .unwrap();
    assert!(crc.file_stats_state().is_indeterminate());
    assert_eq!(
        crc.domain_metadata_state.expect_complete()["d"].configuration(),
        "base_d",
    );
    assert_eq!(
        crc.set_transaction_state.expect_complete()["app"].version,
        5
    );
}

// === On-disk base CRC (exercises serde round-trip) ===

#[tokio::test]
async fn test_from_disk_advances_file_stats() {
    let built = CrcReadTest::new()
        .commit(0, create_table_actions())
        // Base CRC: a (100 B, bin 0) + b (10_000 B, bin 1).
        .crc_with_files(0, protocol_v2(), metadata_a(), None, &[100, 10_000])
        .commit(1, [commit_info("WRITE", None), add("c", 20_000)]) // c lands in bin 2
        .commit(2, [remove("a", Some(100)), commit_info("WRITE", None)]) // a removed from bin 0
        .build()
        .await;
    let base = built.read_crc_at(0).unwrap();
    let crc = built.incrementally_build_crc(&base).unwrap();
    assert!(crc.file_stats_state().is_complete());
    let stats = crc.file_stats().unwrap();
    assert_eq!(stats.num_files(), 2);
    assert_eq!(stats.table_size_bytes(), 30_000); // b (10_000) + c (20_000)

    let hist = stats.file_size_histogram().unwrap();
    assert_eq!(hist.file_counts()[0], 0); // a was removed
    assert_eq!(hist.file_counts()[1], 1); // b
    assert_eq!(hist.total_bytes()[1], 10_000);
    assert_eq!(hist.file_counts()[2], 1); // c
    assert_eq!(hist.total_bytes()[2], 20_000);
}

#[tokio::test]
async fn test_from_disk_no_histogram_means_no_result_histogram() {
    // Empty file_sizes => no histogram persisted on disk.
    let built = CrcReadTest::new()
        .commit(0, create_table_actions())
        .crc(0, protocol_v2(), metadata_a(), None)
        .commit(1, [commit_info("WRITE", None), add("a", 100)])
        .build()
        .await;
    let base = built.read_crc_at(0).unwrap();
    let crc = built.incrementally_build_crc(&base).unwrap();
    assert!(crc.file_stats().unwrap().file_size_histogram().is_none());
}

#[tokio::test]
async fn test_from_disk_with_non_zero_crc_version() {
    // Stale CRC is at v2, not v0. Replay covers (2, 3].
    let built = CrcReadTest::new()
        .commit(0, create_table_actions())
        .commit(1, [commit_info("WRITE", None), add("a", 100)])
        .commit(2, [commit_info("WRITE", None), add("b", 200)])
        .crc_with_files(2, protocol_v2(), metadata_a(), None, &[100, 200])
        .commit(3, [commit_info("WRITE", None), add("c", 300)])
        .build()
        .await;
    let base = built.read_crc_at(2).unwrap();
    let crc = built.incrementally_build_crc(&base).unwrap();
    assert!(crc.file_stats_state().is_complete());
    let stats = crc.file_stats().unwrap();
    // Base CRC at v=2 has a (100), b (200); replay over (2, 3] adds c (300).
    assert_eq!(stats.num_files(), 3); // a, b, c
    assert_eq!(stats.table_size_bytes(), 600); // a (100) + b (200) + c (300)
}

#[tokio::test]
async fn test_full_replay_across_two_commits_propagates_all_state() {
    // Realistic shape: a table evolves over two commits. v=1 initial activity; v=2 adds
    // more, removes an older file, updates metadata, and stamps an ICT. The create_table_actions
    // (and the base) use an ICT-enabled protocol so the v=2 ICT is well-formed.
    let base = Crc {
        protocol: protocol_v2_ict(),
        metadata: metadata_a(),
        ..crc_complete_empty_dm_set_txn()
    };
    let crc = CrcReadTest::new()
        .commit(0, create_table_actions_with_ict())
        .commit(
            1,
            [
                commit_info("WRITE", None),
                add("a", 100),
                domain_metadata("kept", "v1_value"),
                set_txn("app_a", 1, Some(100)),
            ],
        )
        .commit(
            2,
            [
                commit_info("WRITE", Some(9999)),
                metadata(metadata_b()),
                add("b", 200),
                remove("a", Some(100)),
                domain_metadata("d", "x"),
                set_txn("app_b", 5, Some(200)),
            ],
        )
        .build()
        .await
        .incrementally_build_crc(&base)
        .unwrap();

    // Newest commit's M and ICT win; protocol stays at create_table_actions's ICT-enabled choice.
    assert_eq!(crc.protocol, protocol_v2_ict());
    assert_eq!(crc.metadata, metadata_b());
    assert_eq!(crc.in_commit_timestamp_opt, Some(9999));

    // File stats accumulated across both commits: a added then removed; b remains.
    assert!(crc.file_stats_state().is_complete());
    let stats = crc.file_stats().unwrap();
    assert_eq!(stats.num_files(), 1); // only b remains
    assert_eq!(stats.table_size_bytes(), 200); // b (200)

    // DM and txn entries from BOTH commits land in the result map.
    let dm = crc.domain_metadata_state.expect_complete();
    assert_eq!(dm["kept"].configuration(), "v1_value");
    assert_eq!(dm["d"].configuration(), "x");
    let txn = crc.set_transaction_state.expect_complete();
    assert_eq!(txn["app_a"].version, 1);
    assert_eq!(txn["app_b"].version, 5);
}