datafusion-ducklake 0.3.1

DuckLake query engine for rust, built with datafusion.
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
//! Integration tests for single-catalog maintenance on the SQLite writer:
//! `drop_table`, `expire_snapshots`, and `cleanup_old_files_sqlite`.
//!
//! Mirrors the upstream DuckLake suite
//! (`test/sql/catalog/drop_table.test`, `test/sql/compaction/expire_snapshots*.test`).

#![cfg(all(feature = "write-sqlite", feature = "metadata-sqlite"))]

use std::collections::HashSet;
use std::path::{Path, PathBuf};
use std::sync::Arc;

use object_store::ObjectStore;
use object_store::local::LocalFileSystem;
use sqlx::Row;
use sqlx::sqlite::SqlitePool;
use tempfile::TempDir;

use datafusion_ducklake::SqliteMetadataWriter;
use datafusion_ducklake::maintenance::{
    CleanupCriteria, ExpireCriteria, cleanup_old_files_sqlite, delete_orphaned_files_sqlite,
};
use datafusion_ducklake::metadata_writer::{ColumnDef, DataFileInfo, MetadataWriter, WriteMode};

fn cols() -> Vec<ColumnDef> {
    vec![
        ColumnDef::new("id", "int64", false).unwrap(),
        ColumnDef::new("name", "varchar", true).unwrap(),
    ]
}

/// A writable single-catalog SQLite environment plus the bits needed to assert on it.
struct Harness {
    writer: SqliteMetadataWriter,
    conn_str: String,
    data_path: PathBuf,
    _temp: TempDir,
}

async fn setup() -> Harness {
    let temp = TempDir::new().unwrap();
    let db_path = temp.path().join("test.db");
    let data_path = temp.path().join("data");
    std::fs::create_dir_all(&data_path).unwrap();
    let conn_str = format!("sqlite:{}?mode=rwc", db_path.display());

    let writer = SqliteMetadataWriter::new_with_init(&conn_str)
        .await
        .unwrap();
    writer.set_data_path(data_path.to_str().unwrap()).unwrap();

    Harness {
        writer,
        conn_str,
        data_path,
        _temp: temp,
    }
}

/// Open a fresh read connection to the metadata DB for raw assertions.
async fn pool(h: &Harness) -> SqlitePool {
    SqlitePool::connect(&h.conn_str).await.unwrap()
}

async fn scalar_i64(pool: &SqlitePool, sql: &str) -> i64 {
    sqlx::query(sql)
        .fetch_one(pool)
        .await
        .unwrap()
        .try_get::<i64, _>(0)
        .unwrap()
}

/// Register a data file in the catalog AND create the matching physical file on disk
/// at `<data_path>/<schema>/<table>/<name>`, mirroring the real write layout.
fn write_physical_file(data_path: &Path, schema: &str, table: &str, name: &str) {
    let dir = data_path.join(schema).join(table);
    std::fs::create_dir_all(&dir).unwrap();
    std::fs::write(dir.join(name), b"parquet-bytes").unwrap();
}

#[tokio::test(flavor = "multi_thread")]
async fn drop_table_tombstones_children_and_is_idempotent() {
    let h = setup().await;
    let s = h
        .writer
        .begin_write_transaction("main", "users", &cols(), WriteMode::Replace)
        .unwrap();
    h.writer
        .register_data_file(
            s.table_id,
            s.snapshot_id,
            &DataFileInfo::new("f1.parquet", 100, 5),
            WriteMode::Replace,
            &cols(),
            &s.column_ids,
        )
        .unwrap();

    let dropped = h.writer.drop_table("main", "users").unwrap();
    assert!(dropped, "live table should drop");

    let p = pool(&h).await;
    // No live rows remain for the table; children carry the drop snapshot.
    for tbl in ["ducklake_table", "ducklake_column", "ducklake_data_file"] {
        let live = scalar_i64(
            &p,
            &format!(
                "SELECT COUNT(*) FROM {tbl} WHERE table_id = {} AND end_snapshot IS NULL",
                s.table_id
            ),
        )
        .await;
        assert_eq!(live, 0, "no live rows in {tbl} after drop");
    }

    // The stats row is intentionally preserved (monotonic next_row_id).
    let stats = scalar_i64(
        &p,
        &format!(
            "SELECT COUNT(*) FROM ducklake_table_stats WHERE table_id = {}",
            s.table_id
        ),
    )
    .await;
    assert_eq!(stats, 1, "table_stats row preserved across drop");
    let next_row_id = scalar_i64(
        &p,
        &format!(
            "SELECT next_row_id FROM ducklake_table_stats WHERE table_id = {}",
            s.table_id
        ),
    )
    .await;
    assert_eq!(next_row_id, 5, "next_row_id preserved");

    // Second drop is a no-op.
    let dropped_again = h.writer.drop_table("main", "users").unwrap();
    assert!(!dropped_again, "second drop returns false");
}

#[tokio::test(flavor = "multi_thread")]
async fn drop_table_unknown_and_empty_names() {
    let h = setup().await;
    assert!(
        !h.writer.drop_table("main", "ghost").unwrap(),
        "unknown table -> false"
    );
    assert!(
        h.writer.drop_table("", "users").is_err(),
        "empty schema rejected"
    );
    assert!(
        h.writer.drop_table("main", "").is_err(),
        "empty table rejected"
    );
}

/// Write three Replace generations of one table so the first two data files become
/// superseded (end-snapshotted), then return the writer + snapshot ids.
fn three_generations(writer: &SqliteMetadataWriter) -> (i64, i64, i64, i64) {
    let s1 = writer
        .begin_write_transaction("main", "t", &cols(), WriteMode::Replace)
        .unwrap();
    writer
        .register_data_file(
            s1.table_id,
            s1.snapshot_id,
            &DataFileInfo::new("f1.parquet", 100, 5),
            WriteMode::Replace,
            &cols(),
            &s1.column_ids,
        )
        .unwrap();
    let s2 = writer
        .begin_write_transaction("main", "t", &cols(), WriteMode::Replace)
        .unwrap();
    writer
        .register_data_file(
            s2.table_id,
            s2.snapshot_id,
            &DataFileInfo::new("f2.parquet", 100, 5),
            WriteMode::Replace,
            &cols(),
            &s2.column_ids,
        )
        .unwrap();
    let s3 = writer
        .begin_write_transaction("main", "t", &cols(), WriteMode::Replace)
        .unwrap();
    writer
        .register_data_file(
            s3.table_id,
            s3.snapshot_id,
            &DataFileInfo::new("f3.parquet", 100, 5),
            WriteMode::Replace,
            &cols(),
            &s3.column_ids,
        )
        .unwrap();
    (s1.table_id, s1.snapshot_id, s2.snapshot_id, s3.snapshot_id)
}

#[tokio::test(flavor = "multi_thread")]
async fn expire_by_version_schedules_orphaned_file() {
    let h = setup().await;
    let (_tid, s1, _s2, _s3) = three_generations(&h.writer);

    // f1 lives in [s1, s2); expiring s1 leaves no surviving snapshot in that range.
    let expired = h
        .writer
        .expire_snapshots(ExpireCriteria::Versions(vec![s1]))
        .unwrap();
    assert_eq!(expired.len(), 1);
    assert_eq!(expired[0].snapshot_id, s1);

    let p = pool(&h).await;
    assert_eq!(
        scalar_i64(
            &p,
            &format!("SELECT COUNT(*) FROM ducklake_snapshot WHERE snapshot_id = {s1}")
        )
        .await,
        0,
        "expired snapshot row removed"
    );
    // Exactly f1 is scheduled, with the data_path-relative resolved path.
    let scheduled: Vec<(String, i64)> =
        sqlx::query("SELECT path, path_is_relative FROM ducklake_files_scheduled_for_deletion")
            .fetch_all(&p)
            .await
            .unwrap()
            .into_iter()
            .map(|r| {
                (
                    r.try_get::<String, _>(0).unwrap(),
                    r.try_get::<i64, _>(1).unwrap(),
                )
            })
            .collect();
    assert_eq!(scheduled.len(), 1);
    assert_eq!(scheduled[0].0, "main/t/f1.parquet");
    assert_eq!(scheduled[0].1, 1, "path is relative to data_path");
    // f1's catalog row is gone; f2/f3 remain.
    assert_eq!(
        scalar_i64(&p, "SELECT COUNT(*) FROM ducklake_data_file").await,
        2
    );
}

#[tokio::test(flavor = "multi_thread")]
async fn expire_full_after_drop_reclaims_all_table_metadata() {
    let h = setup().await;
    let s = h
        .writer
        .begin_write_transaction("main", "t", &cols(), WriteMode::Replace)
        .unwrap();
    h.writer
        .register_data_file(
            s.table_id,
            s.snapshot_id,
            &DataFileInfo::new("f1.parquet", 100, 5),
            WriteMode::Replace,
            &cols(),
            &s.column_ids,
        )
        .unwrap();
    // Drop allocates snapshot 2; the table is now fully tombstoned in [1, 2).
    assert!(h.writer.drop_table("main", "t").unwrap());

    // Expire snapshot 1 (the drop snapshot 2 is the most recent and is kept).
    let expired = h
        .writer
        .expire_snapshots(ExpireCriteria::Versions(vec![s.snapshot_id]))
        .unwrap();
    assert_eq!(expired.len(), 1);

    let p = pool(&h).await;
    let tid = s.table_id;
    for tbl in ["ducklake_table", "ducklake_column", "ducklake_data_file", "ducklake_table_stats"] {
        let cnt = scalar_i64(
            &p,
            &format!("SELECT COUNT(*) FROM {tbl} WHERE table_id = {tid}"),
        )
        .await;
        assert_eq!(cnt, 0, "{tbl} fully reclaimed after expire");
    }
    // The orphaned file was scheduled for physical deletion.
    assert_eq!(
        scalar_i64(
            &p,
            "SELECT COUNT(*) FROM ducklake_files_scheduled_for_deletion"
        )
        .await,
        1
    );
}

#[tokio::test(flavor = "multi_thread")]
async fn cleanup_old_files_deletes_physical_file() {
    let h = setup().await;
    let (_tid, s1, _s2, _s3) = three_generations(&h.writer);
    // Materialize the physical files referenced by the catalog.
    for name in ["f1.parquet", "f2.parquet", "f3.parquet"] {
        write_physical_file(&h.data_path, "main", "t", name);
    }

    h.writer
        .expire_snapshots(ExpireCriteria::Versions(vec![s1]))
        .unwrap();

    let store: Arc<dyn ObjectStore> = Arc::new(LocalFileSystem::new());
    let f1 = h.data_path.join("main").join("t").join("f1.parquet");

    // Dry run reports the path without deleting.
    let dry = cleanup_old_files_sqlite(&h.writer, store.clone(), CleanupCriteria::All, true)
        .await
        .unwrap();
    assert_eq!(dry.len(), 1);
    assert!(f1.exists(), "dry run must not delete");

    // Real run deletes the object and clears the bookkeeping row.
    let done = cleanup_old_files_sqlite(&h.writer, store.clone(), CleanupCriteria::All, false)
        .await
        .unwrap();
    assert_eq!(done.len(), 1);
    assert!(!f1.exists(), "f1 physically removed");
    assert!(
        h.data_path
            .join("main")
            .join("t")
            .join("f2.parquet")
            .exists(),
        "live file f2 untouched"
    );

    let p = pool(&h).await;
    assert_eq!(
        scalar_i64(
            &p,
            "SELECT COUNT(*) FROM ducklake_files_scheduled_for_deletion"
        )
        .await,
        0,
        "scheduled rows cleared"
    );

    // Idempotent: nothing left to clean.
    let again = cleanup_old_files_sqlite(&h.writer, store, CleanupCriteria::All, false)
        .await
        .unwrap();
    assert!(again.is_empty());
}

#[tokio::test(flavor = "multi_thread")]
async fn expire_older_than_uses_datetime_utc() {
    // Cover the OlderThan public API (chrono::DateTime<Utc>) end-to-end: a
    // far-future cutoff means every non-most-recent snapshot is expirable.
    let h = setup().await;
    let (_tid, _s1, _s2, _s3) = three_generations(&h.writer);

    let cutoff = chrono::Utc::now() + chrono::Duration::days(1);
    let expired = h
        .writer
        .expire_snapshots(ExpireCriteria::OlderThan(cutoff))
        .unwrap();
    // three_generations creates s1, s2, s3. s3 is most-recent and kept;
    // the other two are expired.
    assert_eq!(expired.len(), 2);
}

#[tokio::test(flavor = "multi_thread")]
async fn expire_and_cleanup_no_op_paths() {
    let h = setup().await;
    // Single snapshot: nothing is expirable (the most recent is always kept).
    let s = h
        .writer
        .begin_write_transaction("main", "t", &cols(), WriteMode::Replace)
        .unwrap();
    let expired = h
        .writer
        .expire_snapshots(ExpireCriteria::Versions(vec![s.snapshot_id]))
        .unwrap();
    assert!(
        expired.is_empty(),
        "cannot expire the only/most-recent snapshot"
    );

    // Nothing scheduled -> cleanup returns empty.
    let store: Arc<dyn ObjectStore> = Arc::new(LocalFileSystem::new());
    let cleaned = cleanup_old_files_sqlite(&h.writer, store, CleanupCriteria::All, false)
        .await
        .unwrap();
    assert!(cleaned.is_empty());
}

// ---------------------------------------------------------------------------
// delete_orphaned_files
// ---------------------------------------------------------------------------

#[tokio::test(flavor = "multi_thread")]
async fn delete_orphaned_files_removes_unreferenced_keeps_referenced() {
    let h = setup().await;
    // One registered, referenced file.
    let s = h
        .writer
        .begin_write_transaction("main", "t", &cols(), WriteMode::Replace)
        .unwrap();
    h.writer
        .register_data_file(
            s.table_id,
            s.snapshot_id,
            &DataFileInfo::new("referenced.parquet", 100, 5),
            WriteMode::Replace,
            &cols(),
            &s.column_ids,
        )
        .unwrap();
    write_physical_file(&h.data_path, "main", "t", "referenced.parquet");

    // Drop a stray file alongside it that's NOT in the catalog.
    write_physical_file(&h.data_path, "main", "t", "orphan.parquet");
    // And a non-parquet file that must be ignored (only .parquet is swept).
    std::fs::write(
        h.data_path.join("main").join("t").join("README.txt"),
        b"keep me",
    )
    .unwrap();

    let store: Arc<dyn ObjectStore> = Arc::new(LocalFileSystem::new());
    let orphan_path = h.data_path.join("main").join("t").join("orphan.parquet");
    let ref_path = h
        .data_path
        .join("main")
        .join("t")
        .join("referenced.parquet");
    let readme = h.data_path.join("main").join("t").join("README.txt");

    // Dry run reports the orphan without touching disk.
    let dry = delete_orphaned_files_sqlite(&h.writer, store.clone(), CleanupCriteria::All, true)
        .await
        .unwrap();
    assert_eq!(dry.len(), 1);
    assert!(
        dry[0].ends_with("main/t/orphan.parquet"),
        "got {:?}",
        dry[0]
    );
    assert!(orphan_path.exists());

    // Real run deletes only the orphan.
    let done = delete_orphaned_files_sqlite(&h.writer, store, CleanupCriteria::All, false)
        .await
        .unwrap();
    assert_eq!(done.len(), 1);
    assert!(!orphan_path.exists(), "orphan should be gone");
    assert!(ref_path.exists(), "referenced file must survive");
    assert!(readme.exists(), "non-parquet file must be ignored");
}

#[tokio::test(flavor = "multi_thread")]
async fn delete_orphaned_files_older_than_skips_recent_files() {
    // Critical safety check: a file just written (in-flight) is newer than the
    // cutoff and must be kept even though it's unreferenced. Matches the
    // upstream `last_modified < older_than` guard.
    let h = setup().await;
    write_physical_file(&h.data_path, "main", "t", "fresh_orphan.parquet");
    let fresh = h
        .data_path
        .join("main")
        .join("t")
        .join("fresh_orphan.parquet");
    assert!(fresh.exists());

    let store: Arc<dyn ObjectStore> = Arc::new(LocalFileSystem::new());
    let cutoff = chrono::Utc::now() - chrono::Duration::hours(1);
    let deleted =
        delete_orphaned_files_sqlite(&h.writer, store, CleanupCriteria::OlderThan(cutoff), false)
            .await
            .unwrap();
    assert!(
        deleted.is_empty(),
        "files newer than cutoff must not be deleted (got {deleted:?})"
    );
    assert!(fresh.exists(), "fresh orphan survives older_than filter");
}

#[tokio::test(flavor = "multi_thread")]
async fn delete_orphaned_files_spares_files_pending_in_scheduled_table() {
    // Files in `ducklake_files_scheduled_for_deletion` are "queued for
    // cleanup_old_files" — they may still exist on disk. delete_orphaned_files
    // must treat them as referenced so it doesn't race ahead and delete them
    // (cleanup_old_files would then double-delete and remove the bookkeeping).
    let h = setup().await;
    write_physical_file(&h.data_path, "main", "t", "scheduled.parquet");

    // Seed the scheduled-for-deletion table directly. This matches what
    // `expire_snapshots` would have done — but we shortcut it for the test.
    let p = pool(&h).await;
    sqlx::query(
        "INSERT INTO ducklake_files_scheduled_for_deletion
             (data_file_id, path, path_is_relative, schedule_start)
         VALUES (?, ?, 1, CURRENT_TIMESTAMP)",
    )
    .bind(42_i64)
    .bind("main/t/scheduled.parquet")
    .execute(&p)
    .await
    .unwrap();

    let store: Arc<dyn ObjectStore> = Arc::new(LocalFileSystem::new());
    let deleted = delete_orphaned_files_sqlite(&h.writer, store, CleanupCriteria::All, false)
        .await
        .unwrap();
    assert!(
        deleted.is_empty(),
        "files in scheduled-for-deletion must be considered referenced (got {deleted:?})"
    );
    assert!(
        h.data_path
            .join("main")
            .join("t")
            .join("scheduled.parquet")
            .exists()
    );
}

// ---------------------------------------------------------------------------
// delete_orphaned_files — edge-case contract coverage
//
// The orphan sweep relies on three implicit invariants that the happy-path
// tests above don't exercise:
//
//   * `object_store::Path` normalises consecutive slashes, so a relative path
//     starting with `/` (which the `RESOLVED_PATH` SQL CASE can emit when
//     `s.path = ''`) still matches the listing for the same physical file.
//   * `resolve_path` short-circuits when `path_is_relative = false`, so a
//     data-file row whose `path` is absolute is keyed by that absolute path
//     verbatim — and must still match `object_store.list`'s key for the same
//     file when the file happens to live under `data_path`.
//   * `object_store.list(Some(&prefix))` recurses into subdirectories — a
//     hive-partitioned layout (`year=…/month=…/file.parquet`) must be fully
//     visible.
//
// Plus two operational corners worth pinning down:
//
//   * `dry_run` and the real run return identical path sets — they take
//     separate code paths inside `run_orphan_cleanup`.
//   * `CleanupCriteria::All` against an empty catalog deletes every
//     `.parquet` in `data_path`. Documented behaviour (mirrors the official
//     `cleanup_all => true`), made explicit here so anyone changing the
//     safety story sees the footgun directly.
// ---------------------------------------------------------------------------

/// Schema rows with `path = ''` (the DDL default) make the `RESOLVED_PATH`
/// SQL emit a leading-slash relative path (`'' || '/' || t || '/' || f`).
/// After `join_paths(base_key, "/t/f")` we get a path with a double slash.
/// `ObjectPath::from(...)` normalises that to the single-slash form, which
/// matches what `object_store.list` returns for the same file. This test
/// locks the contract in case ObjectPath's normalisation ever weakens.
#[tokio::test(flavor = "multi_thread")]
async fn delete_orphaned_files_handles_empty_schema_path() {
    let h = setup().await;
    let p = pool(&h).await;

    // Bypass `begin_write_transaction` — it always passes `schema_name` as
    // `path`, so we'd never naturally produce an empty `s.path`. Insert
    // the catalog skeleton directly to exercise the DDL default.
    sqlx::query("INSERT INTO ducklake_snapshot (snapshot_time) VALUES (CURRENT_TIMESTAMP)")
        .execute(&p)
        .await
        .unwrap();
    sqlx::query(
        "INSERT INTO ducklake_schema (schema_name, path, path_is_relative, begin_snapshot)
         VALUES ('s', '', 1, 1)",
    )
    .execute(&p)
    .await
    .unwrap();
    sqlx::query(
        "INSERT INTO ducklake_table (schema_id, table_name, path, path_is_relative, begin_snapshot)
         VALUES (1, 't', 't', 1, 1)",
    )
    .execute(&p)
    .await
    .unwrap();
    sqlx::query(
        "INSERT INTO ducklake_data_file (table_id, path, path_is_relative, file_size_bytes, record_count, begin_snapshot)
         VALUES (1, 'f.parquet', 1, 100, 5, 1)",
    )
    .execute(&p)
    .await
    .unwrap();

    // With s.path='' the resolved path collapses to `<data_path>/t/f.parquet`
    // (the empty schema contributes nothing). The physical file lives there.
    let dir = h.data_path.join("t");
    std::fs::create_dir_all(&dir).unwrap();
    let referenced = dir.join("f.parquet");
    std::fs::write(&referenced, b"data").unwrap();

    let store: Arc<dyn ObjectStore> = Arc::new(LocalFileSystem::new());
    let deleted = delete_orphaned_files_sqlite(&h.writer, store, CleanupCriteria::All, false)
        .await
        .unwrap();
    assert!(
        deleted.is_empty(),
        "referenced file under empty-schema-path resolution must survive (got {deleted:?})"
    );
    assert!(referenced.exists(), "referenced file must still exist");
}

/// `path_is_relative = false` makes `RESOLVED_PATH` return `df.path` verbatim
/// (no prepending). The orphan-cleanup resolver short-circuits in
/// `resolve_path` and uses the absolute key as-is. A file at that absolute
/// key — even if it happens to live under `data_path` — must be excluded
/// from deletion. Our writer doesn't emit absolute paths, so the SQL CASE
/// branch is otherwise untested by integration tests.
#[tokio::test(flavor = "multi_thread")]
async fn delete_orphaned_files_handles_absolute_file_paths() {
    let h = setup().await;
    let p = pool(&h).await;
    let s = h
        .writer
        .begin_write_transaction("main", "t", &cols(), WriteMode::Replace)
        .unwrap();

    // Materialise the file at an absolute path that happens to fall under
    // data_path — that's the configuration where the SQL CASE's absolute
    // branch interacts with the listing.
    write_physical_file(&h.data_path, "main", "t", "abs.parquet");
    let abs_file = h.data_path.join("main").join("t").join("abs.parquet");
    let abs_str = abs_file.to_str().unwrap().to_string();

    // Register it with path_is_relative = false (= 0 in SQLite).
    sqlx::query(
        "INSERT INTO ducklake_data_file (table_id, path, path_is_relative, file_size_bytes, record_count, begin_snapshot)
         VALUES (?, ?, 0, 100, 5, ?)",
    )
    .bind(s.table_id)
    .bind(&abs_str)
    .bind(s.snapshot_id)
    .execute(&p)
    .await
    .unwrap();

    // Drop a stray IN data_path so we know the sweep IS running.
    write_physical_file(&h.data_path, "main", "t", "stray.parquet");
    let stray = h.data_path.join("main").join("t").join("stray.parquet");

    let store: Arc<dyn ObjectStore> = Arc::new(LocalFileSystem::new());
    let deleted = delete_orphaned_files_sqlite(&h.writer, store, CleanupCriteria::All, false)
        .await
        .unwrap();
    assert_eq!(
        deleted.len(),
        1,
        "stray is the only orphan (got {deleted:?})"
    );
    assert!(
        deleted[0].ends_with("main/t/stray.parquet"),
        "got {:?}",
        deleted[0]
    );
    assert!(!stray.exists(), "stray deleted");
    assert!(abs_file.exists(), "absolute-path registered file survives");
}

/// `object_store.list(Some(&prefix))` must recurse into subdirectories so
/// partition-style layouts (`year=…/month=…/file.parquet`) are reachable.
/// Our writer doesn't produce nested layouts today, but other tools might —
/// and a future change to the listing call (e.g. switching to a delimiter
/// to optimise paginating) could silently stop recursing.
#[tokio::test(flavor = "multi_thread")]
async fn delete_orphaned_files_recurses_into_nested_directories() {
    let h = setup().await;
    let s = h
        .writer
        .begin_write_transaction("main", "t", &cols(), WriteMode::Replace)
        .unwrap();
    h.writer
        .register_data_file(
            s.table_id,
            s.snapshot_id,
            &DataFileInfo::new("ref.parquet", 100, 5),
            WriteMode::Replace,
            &cols(),
            &s.column_ids,
        )
        .unwrap();
    write_physical_file(&h.data_path, "main", "t", "ref.parquet");

    // Two orphan files at progressively deeper nesting.
    let deep_dir = h
        .data_path
        .join("main")
        .join("t")
        .join("year=2024")
        .join("month=01");
    std::fs::create_dir_all(&deep_dir).unwrap();
    let level2 = deep_dir.join("partition.parquet");
    std::fs::write(&level2, b"orphan-2").unwrap();
    let deeper_dir = deep_dir.join("day=15");
    std::fs::create_dir_all(&deeper_dir).unwrap();
    let level3 = deeper_dir.join("nested.parquet");
    std::fs::write(&level3, b"orphan-3").unwrap();

    let store: Arc<dyn ObjectStore> = Arc::new(LocalFileSystem::new());
    let deleted: HashSet<String> =
        delete_orphaned_files_sqlite(&h.writer, store, CleanupCriteria::All, false)
            .await
            .unwrap()
            .into_iter()
            .collect();
    assert_eq!(
        deleted.len(),
        2,
        "both nested orphans must be discovered (got {deleted:?})"
    );
    assert!(!level2.exists());
    assert!(!level3.exists());
    assert!(
        h.data_path
            .join("main")
            .join("t")
            .join("ref.parquet")
            .exists(),
        "referenced survives"
    );
}

/// dry_run and real-run must return identical path strings (callers might
/// compare them or feed dry_run into a confirmation prompt then expect the
/// same paths to be deleted on confirm). They take separate code paths
/// (dry_run formats from the pre-delete `orphans` Vec; real-run formats
/// per-orphan after `object_store.delete` succeeds), so a future refactor
/// could diverge them.
#[tokio::test(flavor = "multi_thread")]
async fn delete_orphaned_files_dry_run_matches_real_run() {
    let h = setup().await;
    let s = h
        .writer
        .begin_write_transaction("main", "t", &cols(), WriteMode::Replace)
        .unwrap();
    h.writer
        .register_data_file(
            s.table_id,
            s.snapshot_id,
            &DataFileInfo::new("ref.parquet", 100, 5),
            WriteMode::Replace,
            &cols(),
            &s.column_ids,
        )
        .unwrap();
    write_physical_file(&h.data_path, "main", "t", "ref.parquet");
    for name in ["o1.parquet", "o2.parquet", "o3.parquet"] {
        write_physical_file(&h.data_path, "main", "t", name);
    }

    let store: Arc<dyn ObjectStore> = Arc::new(LocalFileSystem::new());
    let dry: HashSet<String> =
        delete_orphaned_files_sqlite(&h.writer, store.clone(), CleanupCriteria::All, true)
            .await
            .unwrap()
            .into_iter()
            .collect();
    assert_eq!(dry.len(), 3);
    // Dry-run must not touch disk.
    for name in ["o1.parquet", "o2.parquet", "o3.parquet"] {
        assert!(h.data_path.join("main").join("t").join(name).exists());
    }

    let real: HashSet<String> =
        delete_orphaned_files_sqlite(&h.writer, store, CleanupCriteria::All, false)
            .await
            .unwrap()
            .into_iter()
            .collect();
    assert_eq!(
        dry, real,
        "dry_run and real_run must return identical path sets"
    );
}

/// `CleanupCriteria::All` against an empty catalog deletes every `.parquet`
/// under `data_path`. This mirrors the official `cleanup_all => true`
/// semantics but is the classic operator-misuse footgun — pointing the API
/// at a data_path that has files but no catalog rows wipes everything.
/// `OlderThan` is the only safeguard at the crate level; callers should
/// enforce it as a policy invariant (e.g. an automated worker should never
/// pass `All`). This test makes the contract explicit so anyone changing
/// the safety story has to update it deliberately.
#[tokio::test(flavor = "multi_thread")]
async fn delete_orphaned_files_all_on_empty_catalog_wipes_data_path() {
    let h = setup().await;
    // No begin_write_transaction — the catalog is completely empty.
    write_physical_file(&h.data_path, "main", "t", "innocent.parquet");
    write_physical_file(&h.data_path, "main", "t", "innocent2.parquet");
    let innocent1 = h.data_path.join("main").join("t").join("innocent.parquet");
    let innocent2 = h.data_path.join("main").join("t").join("innocent2.parquet");

    let store: Arc<dyn ObjectStore> = Arc::new(LocalFileSystem::new());

    // `All` on an empty catalog → everything in data_path is "unreferenced"
    // → everything is deleted. Documented contract.
    let deleted =
        delete_orphaned_files_sqlite(&h.writer, store.clone(), CleanupCriteria::All, false)
            .await
            .unwrap();
    assert_eq!(
        deleted.len(),
        2,
        "All on empty catalog deletes every .parquet"
    );
    assert!(!innocent1.exists());
    assert!(!innocent2.exists());

    // Restore + verify `OlderThan` is the safeguard against the same scenario.
    write_physical_file(&h.data_path, "main", "t", "fresh.parquet");
    let fresh = h.data_path.join("main").join("t").join("fresh.parquet");
    let cutoff = chrono::Utc::now() - chrono::Duration::hours(1);
    let kept =
        delete_orphaned_files_sqlite(&h.writer, store, CleanupCriteria::OlderThan(cutoff), false)
            .await
            .unwrap();
    assert!(
        kept.is_empty(),
        "OlderThan filter saves a fresh unreferenced file"
    );
    assert!(
        fresh.exists(),
        "fresh file survives OlderThan sweep on empty catalog"
    );
}

// --- spec-aligned atomic Replace: the transient-empty-read regression --------------

/// Rows visible to a reader resolving at `snapshot` (the file-visibility predicate).
async fn visible_rows(pool: &SqlitePool, table_id: i64, snapshot: i64) -> i64 {
    scalar_i64(
        pool,
        &format!(
            "SELECT COALESCE(SUM(record_count), 0) FROM ducklake_data_file
             WHERE table_id = {table_id}
               AND {snapshot} >= begin_snapshot
               AND ({snapshot} < end_snapshot OR end_snapshot IS NULL)"
        ),
    )
    .await
}

async fn head(pool: &SqlitePool) -> i64 {
    scalar_i64(
        pool,
        "SELECT COALESCE(MAX(snapshot_id), 0) FROM ducklake_snapshot",
    )
    .await
}

#[tokio::test(flavor = "multi_thread")]
async fn replace_defers_head_and_retirement_until_register() {
    // A read interleaved between begin_write_transaction and register_data_file
    // on a Replace must still see the OLD generation (never count = 0), and the
    // head must not advance to a fileless snapshot. begin only RESERVES the
    // snapshot id; the snapshot-row insert + prior-generation retirement happen
    // atomically in register_data_file. Pre-fix this asserts FALSE (begin
    // committed the snapshot row and retired the old files before the upload).
    let h = setup().await;
    let p = pool(&h).await;

    // Generation 1: committed, non-empty.
    let s1 = h
        .writer
        .begin_write_transaction("main", "t", &cols(), WriteMode::Replace)
        .unwrap();
    h.writer
        .register_data_file(
            s1.table_id,
            s1.snapshot_id,
            &DataFileInfo::new("gen1.parquet", 100, 5),
            WriteMode::Replace,
            &cols(),
            &s1.column_ids,
        )
        .unwrap();
    assert_eq!(head(&p).await, s1.snapshot_id, "gen 1 is the head");
    assert_eq!(visible_rows(&p, s1.table_id, s1.snapshot_id).await, 5);

    // Begin generation 2 — the upload window. Reserves only.
    let s2 = h
        .writer
        .begin_write_transaction("main", "t", &cols(), WriteMode::Replace)
        .unwrap();
    assert_eq!(s2.snapshot_id, s1.snapshot_id + 1, "reserved the next id");

    // DURING THE WINDOW: head unchanged, gen 1 fully visible (no empty read).
    assert_eq!(
        head(&p).await,
        s1.snapshot_id,
        "head must stay at gen 1 until register commits the snapshot",
    );
    assert_eq!(
        scalar_i64(
            &p,
            &format!(
                "SELECT COUNT(*) FROM ducklake_data_file
                 WHERE table_id = {} AND end_snapshot IS NULL",
                s1.table_id
            ),
        )
        .await,
        1,
        "gen 1 file must not be retired during the upload window",
    );
    assert_eq!(
        visible_rows(&p, s1.table_id, s1.snapshot_id).await,
        5,
        "old generation still serves its complete data",
    );

    // Commit generation 2 — atomic flip.
    h.writer
        .register_data_file(
            s2.table_id,
            s2.snapshot_id,
            &DataFileInfo::new("gen2.parquet", 100, 7),
            WriteMode::Replace,
            &cols(),
            &s2.column_ids,
        )
        .unwrap();

    assert_eq!(head(&p).await, s2.snapshot_id, "head advanced to gen 2");
    assert_eq!(
        visible_rows(&p, s2.table_id, s2.snapshot_id).await,
        7,
        "gen 2 fully visible at the new head",
    );
    assert_eq!(
        scalar_i64(
            &p,
            &format!(
                "SELECT COUNT(*) FROM ducklake_data_file
                 WHERE table_id = {} AND end_snapshot = {}",
                s1.table_id, s2.snapshot_id
            ),
        )
        .await,
        1,
        "gen 1 file retired exactly at the new snapshot",
    );
}

#[tokio::test(flavor = "multi_thread")]
async fn replace_does_not_leak_new_column_generation_until_register() {
    // The SQLite read path resolves a table's columns by `end_snapshot IS NULL`
    // only (not snapshot-scoped), so the new column generation of a
    // schema-evolving Replace must NOT appear until register_data_file commits.
    let h = setup().await;
    let p = pool(&h).await;

    let live_cols = |table_id: i64| {
        format!(
            "SELECT COUNT(*) FROM ducklake_column \
             WHERE table_id = {table_id} AND end_snapshot IS NULL"
        )
    };

    // Generation 1: two columns.
    let s1 = h
        .writer
        .begin_write_transaction("main", "t", &cols(), WriteMode::Replace)
        .unwrap();
    h.writer
        .register_data_file(
            s1.table_id,
            s1.snapshot_id,
            &DataFileInfo::new("g1.parquet", 100, 5),
            WriteMode::Replace,
            &cols(),
            &s1.column_ids,
        )
        .unwrap();
    assert_eq!(scalar_i64(&p, &live_cols(s1.table_id)).await, 2);

    // Begin a schema-evolving Replace (three columns), then pause before commit.
    let evolved = vec![
        ColumnDef::new("id", "int64", false).unwrap(),
        ColumnDef::new("name", "varchar", true).unwrap(),
        ColumnDef::new("extra", "int64", true).unwrap(),
    ];
    let s2 = h
        .writer
        .begin_write_transaction("main", "t", &evolved, WriteMode::Replace)
        .unwrap();

    // DURING THE WINDOW: still the OLD 2-column generation.
    assert_eq!(
        scalar_i64(&p, &live_cols(s1.table_id)).await,
        2,
        "new column generation must not be visible until register commits",
    );

    h.writer
        .register_data_file(
            s2.table_id,
            s2.snapshot_id,
            &DataFileInfo::new("g2.parquet", 100, 7),
            WriteMode::Replace,
            &evolved,
            &s2.column_ids,
        )
        .unwrap();

    // After commit: the new 3-column generation, with the reserved ids the
    // staged parquet's field_id metadata references.
    assert_eq!(
        scalar_i64(&p, &live_cols(s2.table_id)).await,
        3,
        "new column generation visible after register",
    );
    let max_live_col = scalar_i64(
        &p,
        &format!(
            "SELECT COALESCE(MAX(column_id), 0) FROM ducklake_column \
             WHERE table_id = {} AND end_snapshot IS NULL",
            s2.table_id
        ),
    )
    .await;
    assert_eq!(
        max_live_col,
        *s2.column_ids.iter().max().unwrap(),
        "committed column ids match the ids reserved at begin",
    );
}

/// Two concurrent same-table Replace writers that commit out of reservation
/// order must leave exactly one file and one column generation live at the
/// head, with no inverted (end_snapshot < begin_snapshot) column lifetimes.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn replace_out_of_order_commit_does_not_corrupt() {
    let h = setup().await;
    let p = pool(&h).await;

    // Generation 1: committed, non-empty.
    let s0 = h
        .writer
        .begin_write_transaction("main", "t", &cols(), WriteMode::Replace)
        .unwrap();
    h.writer
        .register_data_file(
            s0.table_id,
            s0.snapshot_id,
            &DataFileInfo::new("gen0.parquet", 100, 5),
            WriteMode::Replace,
            &cols(),
            &s0.column_ids,
        )
        .unwrap();
    let tid = s0.table_id;

    // Two Replace writers open their windows...
    let w1 = h
        .writer
        .begin_write_transaction("main", "t", &cols(), WriteMode::Replace)
        .unwrap();
    let w2 = h
        .writer
        .begin_write_transaction("main", "t", &cols(), WriteMode::Replace)
        .unwrap();

    // ...and commit in the OPPOSITE order (w2 before w1).
    h.writer
        .register_data_file(
            w2.table_id,
            w2.snapshot_id,
            &DataFileInfo::new("gen_w2.parquet", 100, 7),
            WriteMode::Replace,
            &cols(),
            &w2.column_ids,
        )
        .unwrap();
    h.writer
        .register_data_file(
            w1.table_id,
            w1.snapshot_id,
            &DataFileInfo::new("gen_w1.parquet", 100, 3),
            WriteMode::Replace,
            &cols(),
            &w1.column_ids,
        )
        .unwrap();

    let live_files = scalar_i64(&p, &format!("SELECT COUNT(*) FROM ducklake_data_file WHERE table_id = {tid} AND end_snapshot IS NULL")).await;
    let live_cols = scalar_i64(
        &p,
        &format!(
            "SELECT COUNT(*) FROM ducklake_column WHERE table_id = {tid} AND end_snapshot IS NULL"
        ),
    )
    .await;
    let inverted = scalar_i64(&p, &format!("SELECT COUNT(*) FROM ducklake_column WHERE table_id = {tid} AND end_snapshot IS NOT NULL AND end_snapshot < begin_snapshot")).await;

    assert_eq!(
        inverted, 0,
        "no column may end before it begins (published snapshot mutated)"
    );
    assert_eq!(
        live_files, 1,
        "exactly one file generation may be live at the head after Replace"
    );
    assert_eq!(
        live_cols,
        cols().len() as i64,
        "exactly one column generation may be live at the head"
    );
}