buoyant_kernel 0.21.100

Buoyant Data distribution of delta-kernel
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
//! Utilities to make working with directory and file paths easier

use std::slice;
use std::str::FromStr;

use crate::actions::visitors::InCommitTimestampVisitor;
use crate::engine_data::RowVisitor;
use crate::utils::require;
use crate::{DeltaResult, Engine, Error, FileMeta, Version};
use delta_kernel_derive::internal_api;

use url::Url;
use uuid::Uuid;

/// How many characters a version tag has
const VERSION_LEN: usize = 20;

/// How many characters a part specifier on a multipart checkpoint has
const MULTIPART_PART_LEN: usize = 10;

/// The number of characters in the uuid part of a uuid checkpoint
const UUID_PART_LEN: usize = 36;

/// The subdirectory name within the table root where the delta log resides
const DELTA_LOG_DIR: &str = "_delta_log";
const DELTA_LOG_DIR_WITH_SLASH: &str = "_delta_log/";
/// The subdirectory name within the delta log where staged commits reside
const STAGED_COMMITS_DIR: &str = "_staged_commits/";

#[derive(Debug, Clone, PartialEq, Eq)]
#[internal_api]
pub(crate) enum LogPathFileType {
    Commit,
    /// Staged commits are commits with UUID filenames, stored in _delta_log/_staged_commits dir.
    StagedCommit,
    SinglePartCheckpoint,
    #[allow(unused)]
    UuidCheckpoint,
    // NOTE: Delta spec doesn't actually say, but checkpoint part numbers are effectively 31-bit
    // unsigned integers: Negative values are never allowed, but Java integer types are always
    // signed. Approximate that as u32 here.
    #[allow(unused)]
    MultiPartCheckpoint {
        part_num: u32,
        num_parts: u32,
    },
    #[allow(unused)]
    CompactedCommit {
        hi: Version,
    },
    Crc,
    Unknown,
}

/// A ParsedLogPath is a well-understood path to a file in the _delta_log directory.
///
/// Note this includes things like checkpoints and commits (containing current table state), but
/// also files used for various optimizations like CRC, compaction, etc.
///
/// Every parsed log path has a version. And additionally, we implement a 'should_list' method
/// which controls whether or not we include this file in our listing. For example, when we list
/// the _delta_log we may see _staged_commits/00000000000000000000.{uuid}.json, but we MUST NOT
/// include those in listing, as only the catalog can tell us which are valid commits.
#[derive(Debug, Clone, PartialEq, Eq)]
#[internal_api]
pub(crate) struct ParsedLogPath<Location: AsUrl = FileMeta> {
    pub location: Location,
    #[allow(unused)]
    pub filename: String,
    #[allow(unused)]
    pub extension: String,
    pub version: Version,
    pub file_type: LogPathFileType,
}

// Internal helper used by TryFrom<FileMeta> below. It parses a fixed-length string into the numeric
// type expected by the caller. A parsing failure returns None. A wrong length produces None, even
// if the parse succeeded.
fn parse_path_part<T: FromStr>(value: &str, expect_len: usize) -> Option<T> {
    match value.parse() {
        Ok(result) if value.len() == expect_len => Some(result),
        _ => None,
    }
}

// We normally construct ParsedLogPath from FileMeta, but in testing it's convenient to use
// a Url directly instead. This trait decouples the two.
#[internal_api]
pub(crate) trait AsUrl {
    fn as_url(&self) -> &Url;
}

impl AsUrl for FileMeta {
    fn as_url(&self) -> &Url {
        &self.location
    }
}

impl AsUrl for Url {
    fn as_url(&self) -> &Url {
        self
    }
}

fn path_contains_delta_log_dir(mut path_segments: std::str::Split<'_, char>) -> bool {
    path_segments.any(|p| p == DELTA_LOG_DIR)
}

impl<Location: AsUrl> ParsedLogPath<Location> {
    // NOTE: We can't actually impl TryFrom because Option<T> is a foreign struct even if T is local.
    #[internal_api]
    pub(crate) fn try_from(location: Location) -> DeltaResult<Option<ParsedLogPath<Location>>> {
        let url = location.as_url();
        let Some(mut path_segments) = url.path_segments() else {
            return Ok(None);
        };
        #[allow(clippy::unwrap_used)]
        let filename = path_segments
            .next_back()
            .unwrap() // "the iterator always contains at least one string (which may be empty)"
            .to_string();
        let subdir = path_segments.next_back();
        if filename.is_empty() {
            return Ok(None); // Not a valid log path
        }

        let mut split = filename.split('.');

        // NOTE: str::split always returns at least one item, even for the empty string.
        #[allow(clippy::unwrap_used)]
        let version = split.next().unwrap();

        // Every valid log path starts with a numeric version part. If version parsing fails, it
        // must not be a log path and we simply return None. However, it is an error if version
        // parsing succeeds for a wrong-length numeric string.
        let version = match version.parse().ok() {
            Some(v) if version.len() == VERSION_LEN => v,
            Some(_) => return Ok(None), // has a version but it's not 20 chars
            None => return Ok(None),
        };

        // Every valid log path has a file extension as its last part. Return None if it's missing.
        let split: Vec<_> = split.collect();
        let extension = match split.last() {
            Some(extension) => extension.to_string(),
            None => return Ok(None),
        };

        // this check determines if we're in the delta log dir, or in the staged commits dir. The check is:
        // 1. If the dir is named _staged_commits, check if the parent dir is _delta_log, and ensure
        //    no higher level directories are _also_ named _delta_log. If those checks pass we're in
        //    the staged_commits dir
        // 2. if the dir is named _delta_log, ensure no higher level directories are _also_ named
        //    _delta_log. If those checks pass, we're in the delta log dir
        let (in_delta_log_dir, in_staged_commits_dir) = if subdir == Some("_staged_commits") {
            if path_segments.next_back() == Some(DELTA_LOG_DIR)
                && !path_contains_delta_log_dir(path_segments)
            {
                (false, true)
            } else {
                (false, false)
            }
        } else {
            (
                subdir == Some(DELTA_LOG_DIR) && !path_contains_delta_log_dir(path_segments),
                false,
            )
        };

        // Parse the file type, based on the number of remaining parts
        let file_type = match split.as_slice() {
            ["json"] if in_delta_log_dir => LogPathFileType::Commit,
            [uuid, "json"] if in_staged_commits_dir => {
                // staged commits like _delta_log/_staged_commits/00000000000000000000.{uuid}.json
                match parse_path_part::<String>(uuid, UUID_PART_LEN) {
                    Some(_uuid) => LogPathFileType::StagedCommit,
                    None => LogPathFileType::Unknown,
                }
            }
            ["crc"] if in_delta_log_dir => LogPathFileType::Crc,
            ["checkpoint", "parquet"] if in_delta_log_dir => LogPathFileType::SinglePartCheckpoint,
            ["checkpoint", uuid, "json" | "parquet"] if in_delta_log_dir => {
                let Some(_) = parse_path_part::<String>(uuid, UUID_PART_LEN) else {
                    return Ok(None);
                };
                LogPathFileType::UuidCheckpoint
            }
            [hi, "compacted", "json"] if in_delta_log_dir => {
                let Some(hi) = parse_path_part(hi, VERSION_LEN) else {
                    return Ok(None);
                };
                LogPathFileType::CompactedCommit { hi }
            }
            ["checkpoint", part_num, num_parts, "parquet"] if in_delta_log_dir => {
                let Some(part_num) = parse_path_part(part_num, MULTIPART_PART_LEN) else {
                    return Ok(None);
                };
                let Some(num_parts) = parse_path_part(num_parts, MULTIPART_PART_LEN) else {
                    return Ok(None);
                };

                // A valid part_num must be in the range [1, num_parts]
                if !(0 < part_num && part_num <= num_parts) {
                    return Ok(None);
                }
                LogPathFileType::MultiPartCheckpoint {
                    part_num,
                    num_parts,
                }
            }

            // Unrecognized log paths are allowed, so long as they have a valid version.
            _ => LogPathFileType::Unknown,
        };
        Ok(Some(ParsedLogPath {
            location,
            filename,
            extension,
            version,
            file_type,
        }))
    }

    /// Parse a location into a commit path (published or staged), returning an error if invalid or
    /// not a commit.
    pub(crate) fn parse_commit(location: Location) -> DeltaResult<Self> {
        let url = location.as_url().to_string();
        let parsed = Self::try_from(location)?.ok_or_else(|| Error::invalid_log_path(&url))?;
        require!(
            parsed.is_commit(),
            Error::generic(format!(
                "Expected a commit path, got {} of type {:?}",
                url, parsed.file_type
            ))
        );
        Ok(parsed)
    }

    pub(crate) fn should_list(&self) -> bool {
        match self.file_type {
            LogPathFileType::Commit
            | LogPathFileType::SinglePartCheckpoint
            | LogPathFileType::UuidCheckpoint
            | LogPathFileType::MultiPartCheckpoint { .. }
            | LogPathFileType::CompactedCommit { .. }
            | LogPathFileType::Crc
            | LogPathFileType::Unknown => true,
            LogPathFileType::StagedCommit => false,
        }
    }

    #[internal_api]
    pub(crate) fn is_commit(&self) -> bool {
        matches!(
            self.file_type,
            LogPathFileType::Commit | LogPathFileType::StagedCommit
        )
    }

    #[internal_api]
    pub(crate) fn is_checkpoint(&self) -> bool {
        matches!(
            self.file_type,
            LogPathFileType::SinglePartCheckpoint
                | LogPathFileType::MultiPartCheckpoint { .. }
                | LogPathFileType::UuidCheckpoint
        )
    }

    #[internal_api]
    #[allow(dead_code)] // currently only used in tests, which don't "count"
    pub(crate) fn is_unknown(&self) -> bool {
        matches!(self.file_type, LogPathFileType::Unknown)
    }
}

impl ParsedLogPath<FileMeta> {
    /// Extract the In-Commit Timestamp from the CommitInfo action in this commit log file.
    /// This is a utility function that can be used by multiple parts of the codebase
    /// (snapshot, CDF, time travel, etc.).
    ///
    /// This method performs IO by reading the commit log file from storage.
    ///
    /// Returns the inCommitTimestamp value, or an error if ICT is not found or cannot be read.
    /// Callers should handle enablement version checks before calling this method.
    pub(crate) fn read_in_commit_timestamp(&self, engine: &dyn Engine) -> DeltaResult<i64> {
        // Only works on commit files
        if !self.is_commit() {
            return Err(Error::generic(format!(
                "read_in_commit_timestamp can only be called on commit files, got: {:?}",
                self.file_type
            )));
        }

        let mut action_iter = engine.json_handler().read_json_files(
            slice::from_ref(&self.location),
            InCommitTimestampVisitor::schema(),
            None,
        )?;

        // Process the actions to find inCommitTimestamp
        // According to protocol, CommitInfo MUST be the first action when ICT is enabled,
        // so we can optimize by only reading the first batch
        match action_iter.next() {
            Some(Ok(actions)) => {
                let mut visitor = InCommitTimestampVisitor::default();
                visitor.visit_rows_of(actions.as_ref())?;
                visitor
                    .in_commit_timestamp
                    .ok_or_else(|| Error::generic("In-Commit Timestamp not found in commit file"))
            }
            Some(Err(err)) => Err(err),
            None => Err(Error::generic("Commit file contains no actions")),
        }
    }
}

impl ParsedLogPath<Url> {
    /// Helper method to create a path with the given filename generator
    fn create_path(table_root: &Url, filename: String) -> DeltaResult<Self> {
        let location = table_root.join(DELTA_LOG_DIR_WITH_SLASH)?.join(&filename)?;
        Self::try_from(location)?.ok_or_else(|| {
            Error::internal_error(format!("Attempted to create an invalid path: {filename}"))
        })
    }

    // TODO: normalize all these log path constructors. we have overlap with this + LogPath +
    // LogRoot types.
    #[allow(unused)]
    /// Create a new ParsedCommitPath<Url> for a new json commit file
    pub(crate) fn new_commit(table_root: &Url, version: Version) -> DeltaResult<Self> {
        let filename = format!("{version:020}.json");
        let path = Self::create_path(table_root, filename)?;
        if !path.is_commit() {
            return Err(Error::internal_error(
                "ParsedLogPath::new_commit created a non-commit path",
            ));
        }
        Ok(path)
    }

    /// Create a new ParsedCheckpointPath<Url> for a classic parquet checkpoint file
    pub(crate) fn new_classic_parquet_checkpoint(
        table_root: &Url,
        version: Version,
    ) -> DeltaResult<Self> {
        let filename = format!("{version:020}.checkpoint.parquet");
        let path = Self::create_path(table_root, filename)?;
        if !path.is_checkpoint() {
            return Err(Error::internal_error(
                "ParsedLogPath::new_classic_parquet_checkpoint created a non-checkpoint path",
            ));
        }
        Ok(path)
    }

    /// Create a new ParsedCheckpointPath<Url> for a UUID-based parquet checkpoint file
    #[allow(dead_code)] // TODO: Remove this once we have a use case for it
    pub(crate) fn new_uuid_parquet_checkpoint(
        table_root: &Url,
        version: Version,
    ) -> DeltaResult<Self> {
        let filename = format!("{:020}.checkpoint.{}.parquet", version, Uuid::new_v4());
        let path = Self::create_path(table_root, filename)?;
        if !path.is_checkpoint() {
            return Err(Error::internal_error(
                "ParsedLogPath::new_uuid_parquet_checkpoint created a non-checkpoint path",
            ));
        }
        Ok(path)
    }

    /// Create a new `ParsedLogPath<Url>` for a version checksum (CRC) file.
    pub(crate) fn new_crc(table_root: &Url, version: Version) -> DeltaResult<Self> {
        let filename = format!("{version:020}.crc");
        let path = Self::create_path(table_root, filename)?;
        if !matches!(path.file_type, LogPathFileType::Crc) {
            return Err(Error::internal_error(
                "ParsedLogPath::new_crc created a non-CRC path",
            ));
        }
        Ok(path)
    }

    /// Create a new ParsedLogPath<Url> for a log compaction file
    pub(crate) fn new_log_compaction(
        table_root: &Url,
        start_version: Version,
        end_version: Version,
    ) -> DeltaResult<Self> {
        let filename = format!("{start_version:020}.{end_version:020}.compacted.json");
        let path = Self::create_path(table_root, filename)?;
        if !matches!(path.file_type, LogPathFileType::CompactedCommit { .. }) {
            return Err(Error::internal_error(
                "ParsedLogPath::new_log_compaction created a non-compaction path",
            ));
        }
        Ok(path)
    }
}

/// A wrapper around parsed log path to provide more structure/safety when handling
/// table/log/commit paths.
#[derive(Debug, Clone)]
pub(crate) struct LogRoot {
    table_root: Url,
    log_root: Url,
}

impl LogRoot {
    /// Create a new LogRoot from the table root URL (e.g. s3://bucket/table ->
    /// s3://bucket/table/_delta_log/)
    ///
    /// TODO: could take a `table_root: TableRoot`
    pub(crate) fn new(mut table_root: Url) -> DeltaResult<Self> {
        if !table_root.path().ends_with('/') {
            let new_path = format!("{}/", table_root.path());
            table_root.set_path(&new_path);
        }
        let log_root = table_root.join(DELTA_LOG_DIR_WITH_SLASH)?;
        Ok(Self {
            table_root,
            log_root,
        })
    }

    pub(crate) fn table_root(&self) -> &Url {
        &self.table_root
    }

    pub(crate) fn log_root(&self) -> &Url {
        &self.log_root
    }

    /// Create a new commit path (absolute path) for the given version.
    pub(crate) fn new_commit_path(&self, version: Version) -> DeltaResult<ParsedLogPath<Url>> {
        let filename = format!("{version:020}.json");
        let path = self.log_root().join(&filename)?;
        ParsedLogPath::try_from(path)?.ok_or_else(|| {
            Error::internal_error(format!("Attempted to create an invalid path: {filename}"))
        })
    }

    /// Create a new staged commit path (absolute path) for the given version.
    pub(crate) fn new_staged_commit_path(
        &self,
        version: Version,
    ) -> DeltaResult<ParsedLogPath<Url>> {
        let uuid = uuid::Uuid::new_v4();
        let filename = format!("{version:020}.{uuid}.json");
        let path = self.log_root().join(STAGED_COMMITS_DIR)?.join(&filename)?;
        ParsedLogPath::try_from(path)?.ok_or_else(|| {
            Error::internal_error(format!("Attempted to create an invalid path: {filename}"))
        })
    }
}

#[cfg(test)]
pub(crate) mod tests {
    use std::path::PathBuf;
    use std::sync::Arc;

    use super::*;
    use crate::engine::default::DefaultEngineBuilder;
    use crate::engine::sync::SyncEngine;
    use crate::object_store::memory::InMemory;
    use crate::utils::test_utils::assert_result_error_with_message;
    use test_utils::add_commit;

    impl ParsedLogPath<FileMeta> {
        pub(crate) fn create_parsed_published_commit(table_root: &Url, version: Version) -> Self {
            let filename = format!("{version:020}.json");
            let location = table_root
                .join(DELTA_LOG_DIR_WITH_SLASH)
                .unwrap()
                .join(&filename)
                .unwrap();
            let parsed = ParsedLogPath::try_from(FileMeta::new(location, 0, 0))
                .unwrap()
                .unwrap();
            assert!(parsed.file_type == LogPathFileType::Commit);
            parsed
        }

        pub(crate) fn create_parsed_staged_commit(table_root: &Url, version: Version) -> Self {
            let uuid = Uuid::new_v4();
            let filename = format!("{version:020}.{uuid}.json");
            let location = table_root
                .join(DELTA_LOG_DIR_WITH_SLASH)
                .unwrap()
                .join(STAGED_COMMITS_DIR)
                .unwrap()
                .join(&filename)
                .unwrap();
            let parsed = ParsedLogPath::try_from(FileMeta::new(location, 0, 0))
                .unwrap()
                .unwrap();
            assert!(parsed.file_type == LogPathFileType::StagedCommit);
            parsed
        }

        pub(crate) fn create_parsed_crc(table_root: &Url, version: Version) -> Self {
            let filename = format!("{version:020}.crc");
            let location = table_root
                .join(DELTA_LOG_DIR_WITH_SLASH)
                .unwrap()
                .join(&filename)
                .unwrap();
            let parsed = ParsedLogPath::try_from(FileMeta::new(location, 0, 0))
                .unwrap()
                .unwrap();
            assert!(parsed.file_type == LogPathFileType::Crc);
            parsed
        }
    }

    fn table_root_dir_url() -> Url {
        let path = PathBuf::from("./tests/data/table-with-dv-small/");
        let path = std::fs::canonicalize(path).unwrap();
        assert!(path.is_dir());
        let url = url::Url::from_directory_path(path).unwrap();
        assert!(url.path().ends_with('/'));
        url
    }

    fn table_log_dir_url() -> Url {
        let path = PathBuf::from("./tests/data/table-with-dv-small/_delta_log/");
        let path = std::fs::canonicalize(path).unwrap();
        assert!(path.is_dir());
        let url = url::Url::from_directory_path(path).unwrap();
        assert!(url.path().ends_with('/'));
        url
    }

    #[test]
    fn test_unknown_invalid_patterns() {
        let table_log_dir = table_log_dir_url();

        // invalid -- not a file
        let log_path = table_log_dir.join("subdir/").unwrap();
        assert!(log_path
            .path()
            .ends_with("/tests/data/table-with-dv-small/_delta_log/subdir/"));
        let log_path = ParsedLogPath::try_from(log_path).unwrap();
        assert!(log_path.is_none());

        // ignored - not versioned
        let log_path = table_log_dir.join("_last_checkpoint").unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap();
        assert!(log_path.is_none());

        // ignored - no extension
        let log_path = table_log_dir.join("00000000000000000010").unwrap();
        let result = ParsedLogPath::try_from(log_path);
        assert!(
            matches!(result, Ok(None)),
            "Expected Ok(None) for missing file extension"
        );

        // empty extension - should be treated as unknown file type
        let log_path = table_log_dir.join("00000000000000000011.").unwrap();
        let result = ParsedLogPath::try_from(log_path);
        assert!(
            matches!(
                result,
                Ok(Some(ParsedLogPath {
                    file_type: LogPathFileType::Unknown,
                    ..
                }))
            ),
            "Expected Unknown file type, got {result:?}"
        );

        // ignored - version fails to parse
        let log_path = table_log_dir.join("abc.json").unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap();
        assert!(log_path.is_none());

        // invalid - version has too many digits
        let log_path = table_log_dir.join("000000000000000000010.json").unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap();
        assert!(log_path.is_none());

        // invalid - version has too few digits
        let log_path = table_log_dir.join("0000000000000000010.json").unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap();
        assert!(log_path.is_none());

        // unknown - two parts
        let log_path = table_log_dir.join("00000000000000000010.foo").unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap().unwrap();
        assert_eq!(log_path.filename, "00000000000000000010.foo");
        assert_eq!(log_path.extension, "foo");
        assert_eq!(log_path.version, 10);
        assert!(matches!(log_path.file_type, LogPathFileType::Unknown));
        assert!(log_path.is_unknown());

        // unknown - many parts
        let log_path = table_log_dir
            .join("00000000000000000010.a.b.c.foo")
            .unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap().unwrap();
        assert_eq!(log_path.filename, "00000000000000000010.a.b.c.foo");
        assert_eq!(log_path.extension, "foo");
        assert_eq!(log_path.version, 10);
        assert!(log_path.is_unknown());
    }

    #[test]
    fn test_commit_patterns() {
        let table_log_dir = table_log_dir_url();

        let log_path = table_log_dir.join("00000000000000000000.json").unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap().unwrap();
        assert_eq!(log_path.filename, "00000000000000000000.json");
        assert_eq!(log_path.extension, "json");
        assert_eq!(log_path.version, 0);
        assert!(matches!(log_path.file_type, LogPathFileType::Commit));
        assert!(log_path.is_commit());
        assert!(!log_path.is_checkpoint());

        let log_path = table_log_dir.join("00000000000000000005.json").unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap().unwrap();
        assert_eq!(log_path.version, 5);
        assert!(log_path.is_commit());
    }

    #[test]
    fn test_crc_patterns() {
        let table_log_dir = table_log_dir_url();

        let log_path = table_log_dir.join("00000000000000000000.crc").unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap().unwrap();
        assert_eq!(log_path.filename, "00000000000000000000.crc");
        assert_eq!(log_path.extension, "crc");
        assert_eq!(log_path.version, 0);
        assert!(matches!(log_path.file_type, LogPathFileType::Crc));
        assert!(!log_path.is_commit());
        assert!(!log_path.is_checkpoint());

        let log_path = table_log_dir.join("00000000000000000005.crc").unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap().unwrap();
        assert_eq!(log_path.version, 5);
        assert!(log_path.file_type == LogPathFileType::Crc);
    }

    #[test]
    fn test_single_part_checkpoint_patterns() {
        let table_log_dir = table_log_dir_url();

        let log_path = table_log_dir
            .join("00000000000000000002.checkpoint.parquet")
            .unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap().unwrap();
        assert_eq!(log_path.filename, "00000000000000000002.checkpoint.parquet");
        assert_eq!(log_path.extension, "parquet");
        assert_eq!(log_path.version, 2);
        assert!(matches!(
            log_path.file_type,
            LogPathFileType::SinglePartCheckpoint
        ));
        assert!(!log_path.is_commit());
        assert!(log_path.is_checkpoint());

        // invalid file extension
        let log_path = table_log_dir
            .join("00000000000000000002.checkpoint.json")
            .unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap().unwrap();
        assert_eq!(log_path.filename, "00000000000000000002.checkpoint.json");
        assert_eq!(log_path.extension, "json");
        assert_eq!(log_path.version, 2);
        assert!(!log_path.is_commit());
        assert!(!log_path.is_checkpoint());
        assert!(log_path.is_unknown());
    }

    #[test]
    fn test_uuid_checkpoint_patterns() {
        let table_log_dir = table_log_dir_url();

        let log_path = table_log_dir
            .join("00000000000000000002.checkpoint.3a0d65cd-4056-49b8-937b-95f9e3ee90e5.parquet")
            .unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap().unwrap();
        assert_eq!(
            log_path.filename,
            "00000000000000000002.checkpoint.3a0d65cd-4056-49b8-937b-95f9e3ee90e5.parquet"
        );
        assert_eq!(log_path.extension, "parquet");
        assert_eq!(log_path.version, 2);
        assert!(matches!(
            log_path.file_type,
            LogPathFileType::UuidCheckpoint
        ));
        assert!(!log_path.is_commit());
        assert!(log_path.is_checkpoint());

        let log_path = table_log_dir
            .join("00000000000000000002.checkpoint.3a0d65cd-4056-49b8-937b-95f9e3ee90e5.json")
            .unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap().unwrap();
        assert_eq!(
            log_path.filename,
            "00000000000000000002.checkpoint.3a0d65cd-4056-49b8-937b-95f9e3ee90e5.json"
        );
        assert_eq!(log_path.extension, "json");
        assert_eq!(log_path.version, 2);
        assert!(matches!(
            log_path.file_type,
            LogPathFileType::UuidCheckpoint
        ));
        assert!(!log_path.is_commit());
        assert!(log_path.is_checkpoint());

        let log_path = table_log_dir
            .join("00000000000000000002.checkpoint.3a0d65cd-4056-49b8-937b-95f9e3ee90e5.foo")
            .unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap().unwrap();
        assert_eq!(
            log_path.filename,
            "00000000000000000002.checkpoint.3a0d65cd-4056-49b8-937b-95f9e3ee90e5.foo"
        );
        assert_eq!(log_path.extension, "foo");
        assert_eq!(log_path.version, 2);
        assert!(!log_path.is_commit());
        assert!(!log_path.is_checkpoint());
        assert!(log_path.is_unknown());

        let log_path = table_log_dir
            .join("00000000000000000002.checkpoint.foo.parquet")
            .unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap();
        assert!(log_path.is_none());

        // invalid file extension
        let log_path = table_log_dir
            .join("00000000000000000002.checkpoint.foo")
            .unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap().unwrap();
        assert_eq!(log_path.filename, "00000000000000000002.checkpoint.foo");
        assert_eq!(log_path.extension, "foo");
        assert_eq!(log_path.version, 2);
        assert!(!log_path.is_commit());
        assert!(!log_path.is_checkpoint());
        assert!(log_path.is_unknown());

        // Boundary test - UUID with exactly 35 characters (one too short)
        let log_path = table_log_dir
            .join("00000000000000000010.checkpoint.3a0d65cd-4056-49b8-937b-95f9e3ee90e.parquet")
            .unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap();
        assert!(log_path.is_none());
    }

    #[test]
    fn test_multi_part_checkpoint_patterns() {
        let table_log_dir = table_log_dir_url();

        let log_path = table_log_dir
            .join("00000000000000000008.checkpoint.0000000000.0000000002.json")
            .unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap().unwrap();
        assert_eq!(
            log_path.filename,
            "00000000000000000008.checkpoint.0000000000.0000000002.json"
        );
        assert_eq!(log_path.extension, "json");
        assert_eq!(log_path.version, 8);
        assert!(!log_path.is_commit());
        assert!(!log_path.is_checkpoint());
        assert!(log_path.is_unknown());

        let log_path = table_log_dir
            .join("00000000000000000008.checkpoint.0000000000.0000000002.parquet")
            .unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap();
        assert!(log_path.is_none());

        let log_path = table_log_dir
            .join("00000000000000000008.checkpoint.0000000001.0000000002.parquet")
            .unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap().unwrap();
        assert_eq!(
            log_path.filename,
            "00000000000000000008.checkpoint.0000000001.0000000002.parquet"
        );
        assert_eq!(log_path.extension, "parquet");
        assert_eq!(log_path.version, 8);
        assert!(matches!(
            log_path.file_type,
            LogPathFileType::MultiPartCheckpoint {
                part_num: 1,
                num_parts: 2
            }
        ));
        assert!(!log_path.is_commit());
        assert!(log_path.is_checkpoint());

        let log_path = table_log_dir
            .join("00000000000000000008.checkpoint.0000000002.0000000002.parquet")
            .unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap().unwrap();
        assert_eq!(
            log_path.filename,
            "00000000000000000008.checkpoint.0000000002.0000000002.parquet"
        );
        assert_eq!(log_path.extension, "parquet");
        assert_eq!(log_path.version, 8);
        assert!(matches!(
            log_path.file_type,
            LogPathFileType::MultiPartCheckpoint {
                part_num: 2,
                num_parts: 2
            }
        ));
        assert!(!log_path.is_commit());
        assert!(log_path.is_checkpoint());

        let log_path = table_log_dir
            .join("00000000000000000008.checkpoint.0000000003.0000000002.parquet")
            .unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap();
        assert!(log_path.is_none());

        let log_path = table_log_dir
            .join("00000000000000000008.checkpoint.000000001.0000000002.parquet")
            .unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap();
        assert!(log_path.is_none());

        let log_path = table_log_dir
            .join("00000000000000000008.checkpoint.0000000001.000000002.parquet")
            .unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap();
        assert!(log_path.is_none());

        let log_path = table_log_dir
            .join("00000000000000000008.checkpoint.00000000x1.0000000002.parquet")
            .unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap();
        assert!(log_path.is_none());

        let log_path = table_log_dir
            .join("00000000000000000008.checkpoint.0000000001.00000000x2.parquet")
            .unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap();
        assert!(log_path.is_none());
    }

    #[test]
    fn test_compacted_delta_patterns() {
        let table_log_dir = table_log_dir_url();

        let log_path = table_log_dir
            .join("00000000000000000008.00000000000000000015.compacted.json")
            .unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap().unwrap();
        assert_eq!(
            log_path.filename,
            "00000000000000000008.00000000000000000015.compacted.json"
        );
        assert_eq!(log_path.extension, "json");
        assert_eq!(log_path.version, 8);
        assert!(matches!(
            log_path.file_type,
            LogPathFileType::CompactedCommit { hi: 15 },
        ));
        assert!(!log_path.is_commit());
        assert!(!log_path.is_checkpoint());

        // invalid extension
        let log_path = table_log_dir
            .join("00000000000000000008.00000000000000000015.compacted.parquet")
            .unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap().unwrap();
        assert_eq!(
            log_path.filename,
            "00000000000000000008.00000000000000000015.compacted.parquet"
        );
        assert_eq!(log_path.extension, "parquet");
        assert_eq!(log_path.version, 8);
        assert!(!log_path.is_commit());
        assert!(!log_path.is_checkpoint());
        assert!(log_path.is_unknown());

        let log_path = table_log_dir
            .join("00000000000000000008.0000000000000000015.compacted.json")
            .unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap();
        assert!(log_path.is_none());

        let log_path = table_log_dir
            .join("00000000000000000008.000000000000000000015.compacted.json")
            .unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap();
        assert!(log_path.is_none());

        let log_path = table_log_dir
            .join("00000000000000000008.00000000000000000a15.compacted.json")
            .unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap();
        assert!(log_path.is_none());
    }

    #[test]
    fn test_new_commit() {
        let table_root_dir = table_root_dir_url();
        let log_path = ParsedLogPath::new_commit(&table_root_dir, 10).unwrap();
        assert_eq!(log_path.version, 10);
        assert!(log_path.is_commit());
        assert_eq!(log_path.extension, "json");
        assert!(matches!(log_path.file_type, LogPathFileType::Commit));
        assert_eq!(log_path.filename, "00000000000000000010.json");
    }

    #[test]
    fn test_new_uuid_parquet_checkpoint() {
        let table_root_dir = table_root_dir_url();
        let log_path = ParsedLogPath::new_uuid_parquet_checkpoint(&table_root_dir, 10).unwrap();

        assert_eq!(log_path.version, 10);
        assert!(log_path.is_checkpoint());
        assert_eq!(log_path.extension, "parquet");
        assert!(
            matches!(log_path.file_type, LogPathFileType::UuidCheckpoint),
            "Expected UuidCheckpoint file type"
        );

        let filename = log_path.filename.to_string();
        let filename_parts: Vec<&str> = filename.split('.').collect();
        assert_eq!(filename_parts.len(), 4);
        assert_eq!(filename_parts[0], "00000000000000000010");
        assert_eq!(filename_parts[1], "checkpoint");
        assert_eq!(filename_parts[2].len(), UUID_PART_LEN);
        assert_eq!(filename_parts[3], "parquet");
    }

    #[test]
    fn test_new_classic_parquet_checkpoint() {
        let table_root_dir = table_root_dir_url();
        let log_path = ParsedLogPath::new_classic_parquet_checkpoint(&table_root_dir, 10).unwrap();

        assert_eq!(log_path.version, 10);
        assert!(log_path.is_checkpoint());
        assert_eq!(log_path.extension, "parquet");
        assert!(matches!(
            log_path.file_type,
            LogPathFileType::SinglePartCheckpoint
        ));
        assert_eq!(log_path.filename, "00000000000000000010.checkpoint.parquet");
    }

    #[test]
    fn test_staged_commit_paths() {
        let table_log_dir = table_log_dir_url();

        // valid staged commit
        let log_path = table_log_dir
            .join("_staged_commits/00000000000000000010.3a0d65cd-4056-49b8-937b-95f9e3ee90e5.json")
            .unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap().unwrap();
        assert_eq!(
            log_path.filename,
            "00000000000000000010.3a0d65cd-4056-49b8-937b-95f9e3ee90e5.json"
        );
        assert_eq!(log_path.extension, "json");
        assert_eq!(log_path.version, 10);
        assert!(matches!(log_path.file_type, LogPathFileType::StagedCommit));
        assert!(log_path.is_commit());
        assert!(!log_path.is_checkpoint());
        assert!(!log_path.is_unknown());

        // invalid uuid
        let log_path = table_log_dir
            .join("_staged_commits/00000000000000000010.not-a-uuid.json")
            .unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap().unwrap();
        assert!(log_path.is_unknown());
        assert!(!log_path.is_commit());
        assert!(!log_path.is_checkpoint());

        // outside _staged_commits directory
        let log_path = table_log_dir
            .join("00000000000000000010.3a0d65cd-4056-49b8-937b-95f9e3ee90e5.json")
            .unwrap();
        let log_path = ParsedLogPath::try_from(log_path).unwrap().unwrap();
        assert_eq!(
            log_path.filename,
            "00000000000000000010.3a0d65cd-4056-49b8-937b-95f9e3ee90e5.json"
        );
        assert_eq!(log_path.extension, "json");
        assert_eq!(log_path.version, 10);
        assert!(matches!(log_path.file_type, LogPathFileType::Unknown));
        assert!(!log_path.is_commit());
        assert!(!log_path.is_checkpoint());
        assert!(log_path.is_unknown());
    }

    #[test]
    fn test_should_list() {
        let mut path = ParsedLogPath {
            location: table_log_dir_url(),
            filename: "".to_string(),
            extension: "".to_string(),
            version: 0,
            file_type: LogPathFileType::Commit,
        };

        for (file_type, should_list) in [
            (LogPathFileType::Commit, true),
            (LogPathFileType::StagedCommit, false),
            (LogPathFileType::SinglePartCheckpoint, true),
            (LogPathFileType::UuidCheckpoint, true),
            (
                LogPathFileType::MultiPartCheckpoint {
                    part_num: 1,
                    num_parts: 2,
                },
                true,
            ),
            (LogPathFileType::CompactedCommit { hi: 10 }, true),
            (LogPathFileType::Crc, true),
            (LogPathFileType::Unknown, true),
        ] {
            path.file_type = file_type;
            assert_eq!(
                path.should_list(),
                should_list,
                "file_type: {:?}",
                path.file_type
            );
        }
    }

    #[tokio::test]
    async fn test_read_in_commit_timestamp_success() {
        let store = Arc::new(InMemory::new());
        let engine = DefaultEngineBuilder::new(store.clone()).build();
        let table_root = "memory://test/";
        let table_url = url::Url::parse(table_root).unwrap();

        // Create a commit file with ICT using add_commit
        let commit_content = r#"{"commitInfo":{"timestamp":1000,"inCommitTimestamp":2000},"protocol":{"minReaderVersion":3,"minWriterVersion":7,"writerFeatures":["inCommitTimestamp"]},"metaData":{"id":"test","schemaString":"{\"type\":\"struct\",\"fields\":[{\"name\":\"id\",\"type\":\"integer\",\"nullable\":true}]}"}}"#;
        add_commit(table_root, store.as_ref(), 0, commit_content.to_string())
            .await
            .unwrap();

        // Create ParsedLogPath for the commit file
        let commit_path = table_url
            .join("_delta_log/00000000000000000000.json")
            .unwrap();
        let parsed_path = ParsedLogPath::try_from(FileMeta {
            location: commit_path,
            last_modified: 0,
            size: commit_content.len() as u64,
        })
        .unwrap()
        .unwrap();

        // Now actually test reading the timestamp
        let result = parsed_path.read_in_commit_timestamp(&engine).unwrap();
        assert_eq!(result, 2000);
    }

    #[tokio::test]
    async fn test_read_in_commit_timestamp_missing_ict() {
        let store = Arc::new(InMemory::new());
        let engine = DefaultEngineBuilder::new(store.clone()).build();
        let table_root = "memory://test/";
        let table_url = url::Url::parse(table_root).unwrap();

        // Create a commit file without ICT
        let commit_content = r#"{"commitInfo":{"timestamp":1000},"protocol":{"minReaderVersion":3,"minWriterVersion":7},"metaData":{"id":"test","schemaString":"{\"type\":\"struct\",\"fields\":[{\"name\":\"id\",\"type\":\"integer\",\"nullable\":true}]}"}}"#;
        add_commit(table_root, store.as_ref(), 0, commit_content.to_string())
            .await
            .unwrap();

        // Create ParsedLogPath for the commit file
        let commit_path = table_url
            .join("_delta_log/00000000000000000000.json")
            .unwrap();
        let parsed_path = ParsedLogPath::try_from(FileMeta {
            location: commit_path,
            last_modified: 0,
            size: commit_content.len() as u64,
        })
        .unwrap()
        .unwrap();

        // Should return error when ICT is missing
        let result = parsed_path.read_in_commit_timestamp(&engine);
        assert_result_error_with_message(result, "In-Commit Timestamp not found");
    }

    #[test]
    fn test_read_in_commit_timestamp_not_commit_file() {
        let engine = SyncEngine::new();
        let table_url = url::Url::try_from("file:///tmp/test_table").unwrap();

        // Create a checkpoint file (not a commit file)
        let checkpoint_path = table_url
            .join("_delta_log/00000000000000000000.checkpoint.parquet")
            .unwrap();
        let parsed_path = ParsedLogPath::try_from(FileMeta {
            location: checkpoint_path,
            last_modified: 0,
            size: 100,
        })
        .unwrap()
        .unwrap();

        // Should return error for non-commit files
        let result = parsed_path.read_in_commit_timestamp(&engine);
        assert_result_error_with_message(
            result,
            "read_in_commit_timestamp can only be called on commit files",
        );
    }
}