lake-pulse 0.3.0

A Rust library for analyzing data lake table health across multiple formats and storage providers.
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
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
// Copyright 2025 Adobe. All rights reserved.
// This file is licensed to you under the Apache License,
// Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0)
// or the MIT license (http://opensource.org/licenses/MIT),
// at your option.
//
// Unless required by applicable law or agreed to in writing,
// this software is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR REPRESENTATIONS OF ANY KIND, either express or
// implied. See the LICENSE-MIT and LICENSE-APACHE files for the
// specific language governing permissions and limitations under
// each license.

use crate::analyze::common::{
    calculate_recommended_retention, calculate_retention_efficiency,
    calculate_schema_stability_score,
};
use crate::analyze::metrics::{
    ClusteringInfo, DeletionVectorMetrics, FileCompactionMetrics, HealthMetrics,
    SchemaEvolutionMetrics, TableConstraintsMetrics, TimeTravelMetrics,
};
use crate::analyze::table_analyzer::TableAnalyzer;
use crate::storage::{FileMetadata, StorageProvider};
use apache_avro::{from_value, Reader as AvroReader};
use async_trait::async_trait;
use chrono::Utc;
use serde::Deserialize;
use std::collections::HashMap;
use std::error::Error;
use std::sync::Arc;
use tracing::{info, warn};

/// Paimon snapshot JSON structure (minimal for parsing)
#[derive(Debug, Clone, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
struct PaimonSnapshot {
    pub id: i64,
    pub schema_id: i64,
    pub time_millis: Option<i64>,
    pub total_record_count: Option<i64>,
    pub commit_kind: Option<String>,
    pub base_manifest_list: Option<String>,
    pub delta_manifest_list: Option<String>,
}

/// Paimon schema JSON structure (minimal for parsing)
#[derive(Debug, Clone, Deserialize, Default)]
#[serde(rename_all = "camelCase")]
#[allow(dead_code)]
struct PaimonSchema {
    pub id: i64,
    pub fields: Vec<serde_json::Value>,
    pub partition_keys: Vec<String>,
    pub primary_keys: Vec<String>,
    pub options: Option<HashMap<String, String>>,
}

/// Manifest list entry from Avro manifest-list file (for analyzer)
/// Uses deny_unknown_fields = false to allow extra fields in Avro schema
#[derive(Debug, Clone, Deserialize, Default)]
#[allow(dead_code)]
struct ManifestListEntry {
    #[serde(rename = "_FILE_NAME", default)]
    pub file_name: String,
    #[serde(rename = "_FILE_SIZE", default)]
    pub file_size: i64,
    #[serde(rename = "_NUM_ADDED_FILES", default)]
    pub num_added_files: i64,
    #[serde(rename = "_NUM_DELETED_FILES", default)]
    pub num_deleted_files: i64,
    // Additional fields that may be present in newer Paimon versions
    #[serde(rename = "_VERSION", default)]
    pub version: i32,
    #[serde(rename = "_SCHEMA_ID", default)]
    pub schema_id: i64,
}

/// Data file meta from manifest entry (for analyzer)
#[derive(Debug, Clone, Deserialize, Default)]
#[allow(dead_code)]
struct DataFileMeta {
    #[serde(rename = "_FILE_NAME", default)]
    pub file_name: String,
    #[serde(rename = "_FILE_SIZE", default)]
    pub file_size: i64,
}

/// Manifest entry from Avro manifest file (for analyzer)
#[derive(Debug, Clone, Deserialize, Default)]
#[allow(dead_code)]
struct ManifestEntry {
    #[serde(rename = "_KIND", default)]
    pub kind: i8, // 0 = ADD, 1 = DELETE
    #[serde(rename = "_FILE", default)]
    pub file: DataFileMeta,
}

/// Apache Paimon-specific analyzer for processing Paimon tables.
///
/// This analyzer implements the `TableAnalyzer` trait and provides functionality
/// to parse Paimon snapshot and schema files, extract metrics, and analyze table health.
pub struct PaimonAnalyzer {
    storage_provider: Arc<dyn StorageProvider>,
    #[allow(dead_code)]
    parallelism: usize,
}

impl PaimonAnalyzer {
    /// Create a new PaimonAnalyzer.
    pub fn new(storage_provider: Arc<dyn StorageProvider>, parallelism: usize) -> Self {
        Self {
            storage_provider,
            parallelism,
        }
    }

    /// Categorize files into data files and Paimon metadata files.
    pub fn categorize_paimon_files(
        &self,
        objects: Vec<FileMetadata>,
    ) -> (Vec<FileMetadata>, Vec<FileMetadata>) {
        let mut data_files = Vec::new();
        let mut metadata_files = Vec::new();

        for obj in objects {
            let path = &obj.path;
            // Paimon data files are in bucket directories
            if (path.ends_with(".orc") || path.ends_with(".parquet"))
                && !path.contains("/snapshot/")
                && !path.contains("/schema/")
                && !path.contains("/manifest/")
            {
                data_files.push(obj);
            }
            // Paimon metadata files are in snapshot/, schema/, manifest/ directories
            else if path.contains("/snapshot/")
                || path.contains("/schema/")
                || path.contains("/manifest/")
            {
                metadata_files.push(obj);
            }
        }

        (data_files, metadata_files)
    }

    /// Find referenced files from Paimon metadata.
    ///
    /// This method parses the Paimon manifest files to find all data files
    /// that are referenced by the current snapshot.
    pub async fn find_paimon_referenced_files(
        &self,
        metadata_files: &[FileMetadata],
    ) -> Result<Vec<String>, Box<dyn Error + Send + Sync>> {
        // Find the latest snapshot file to get manifest list references
        let mut latest_snapshot: Option<PaimonSnapshot> = None;
        let mut latest_snapshot_id: i64 = -1;

        for file in metadata_files {
            let filename = file.path.rsplit('/').next().unwrap_or(&file.path);
            if filename.starts_with("snapshot-") {
                if let Ok(content) = self.storage_provider.read_file(&file.path).await {
                    if let Ok(snapshot) = serde_json::from_slice::<PaimonSnapshot>(&content) {
                        if snapshot.id > latest_snapshot_id {
                            latest_snapshot_id = snapshot.id;
                            latest_snapshot = Some(snapshot);
                        }
                    }
                }
            }
        }

        let snapshot = match latest_snapshot {
            Some(s) => s,
            None => return Ok(Vec::new()),
        };

        let mut referenced_files = Vec::new();

        // Parse base manifest list
        if let Some(ref base_manifest_list) = snapshot.base_manifest_list {
            if let Ok(files) = self
                .parse_manifest_list_for_files(metadata_files, base_manifest_list)
                .await
            {
                referenced_files.extend(files);
            }
        }

        // Parse delta manifest list
        if let Some(ref delta_manifest_list) = snapshot.delta_manifest_list {
            if let Ok(files) = self
                .parse_manifest_list_for_files(metadata_files, delta_manifest_list)
                .await
            {
                referenced_files.extend(files);
            }
        }

        info!(
            "Found {} referenced files from Paimon manifests",
            referenced_files.len()
        );
        Ok(referenced_files)
    }

    /// Parse a manifest-list file and extract all referenced data file names.
    async fn parse_manifest_list_for_files(
        &self,
        metadata_files: &[FileMetadata],
        manifest_list_name: &str,
    ) -> Result<Vec<String>, Box<dyn Error + Send + Sync>> {
        // Find the manifest-list file
        let manifest_list_path = metadata_files
            .iter()
            .find(|f| f.path.ends_with(manifest_list_name))
            .map(|f| &f.path);

        let manifest_list_path = match manifest_list_path {
            Some(p) => p,
            None => {
                warn!("Manifest-list file not found: {}", manifest_list_name);
                return Ok(Vec::new());
            }
        };

        let content = self.storage_provider.read_file(manifest_list_path).await?;

        let reader = match AvroReader::new(content.as_slice()) {
            Ok(r) => r,
            Err(e) => {
                warn!("Failed to create Avro reader for manifest-list: {}", e);
                return Ok(Vec::new());
            }
        };

        let mut manifest_names = Vec::new();
        for value in reader {
            match value {
                Ok(v) => {
                    if let Ok(entry) = from_value::<ManifestListEntry>(&v) {
                        manifest_names.push(entry.file_name);
                    }
                }
                Err(e) => {
                    warn!("Failed to read manifest-list entry: {}", e);
                }
            }
        }

        // Now parse each manifest to get data file names
        let mut data_files = Vec::new();
        for manifest_name in manifest_names {
            if let Ok(files) = self
                .parse_manifest_for_files(metadata_files, &manifest_name)
                .await
            {
                data_files.extend(files);
            }
        }

        Ok(data_files)
    }

    /// Parse a manifest file and extract data file names.
    async fn parse_manifest_for_files(
        &self,
        metadata_files: &[FileMetadata],
        manifest_name: &str,
    ) -> Result<Vec<String>, Box<dyn Error + Send + Sync>> {
        // Find the manifest file
        let manifest_path = metadata_files
            .iter()
            .find(|f| f.path.ends_with(manifest_name))
            .map(|f| &f.path);

        let manifest_path = match manifest_path {
            Some(p) => p,
            None => return Ok(Vec::new()),
        };

        let content = self.storage_provider.read_file(manifest_path).await?;
        let reader = AvroReader::new(content.as_slice())?;

        let mut data_files = Vec::new();
        for value in reader {
            match value {
                Ok(v) => {
                    if let Ok(entry) = from_value::<ManifestEntry>(&v) {
                        // Only include ADD entries (kind == 0)
                        if entry.kind == 0 {
                            data_files.push(entry.file.file_name);
                        }
                    }
                }
                Err(e) => {
                    warn!("Failed to read manifest entry: {}", e);
                }
            }
        }

        Ok(data_files)
    }

    /// Update health metrics from Paimon metadata files.
    pub async fn update_metrics_from_paimon_metadata(
        &self,
        metadata_files: &[FileMetadata],
        data_files_total_size: u64,
        data_files_total_files: usize,
        metrics: &mut HealthMetrics,
    ) -> Result<(), Box<dyn Error + Send + Sync>> {
        info!("Updating metrics from Paimon metadata");

        let result = self.process_paimon_metadata(metadata_files).await?;

        // Calculate time-based metrics
        let now = Utc::now().timestamp_millis() as u64;
        let oldest_age_days = if result.oldest_timestamp > 0 {
            (now - result.oldest_timestamp) as f64 / (1000.0 * 60.0 * 60.0 * 24.0)
        } else {
            0.0
        };
        let newest_age_days = if result.newest_timestamp > 0 {
            (now - result.newest_timestamp) as f64 / (1000.0 * 60.0 * 60.0 * 24.0)
        } else {
            0.0
        };

        // Schema evolution metrics
        let schema_stability_score = calculate_schema_stability_score(
            result.total_schema_versions,
            0, // No breaking change detection yet
            if oldest_age_days > 0.0 {
                result.total_schema_versions as f64 / oldest_age_days
            } else {
                0.0
            },
            oldest_age_days,
        );

        metrics.schema_evolution = Some(SchemaEvolutionMetrics {
            total_schema_changes: result.total_schema_versions,
            breaking_changes: 0,
            non_breaking_changes: result.total_schema_versions,
            schema_stability_score,
            days_since_last_change: oldest_age_days,
            schema_change_frequency: 0.0,
            current_schema_version: result.current_schema_id as u64,
        });

        // Time travel metrics
        let retention_efficiency = calculate_retention_efficiency(
            result.total_snapshots,
            oldest_age_days,
            newest_age_days,
        );
        let recommended_retention =
            calculate_recommended_retention(result.total_snapshots, oldest_age_days);

        metrics.time_travel_metrics = Some(TimeTravelMetrics {
            total_snapshots: result.total_snapshots,
            oldest_snapshot_age_days: oldest_age_days,
            newest_snapshot_age_days: newest_age_days,
            total_historical_size_bytes: 0,
            avg_snapshot_size_bytes: 0.0,
            storage_cost_impact_score: 0.0,
            retention_efficiency_score: retention_efficiency,
            recommended_retention_days: recommended_retention,
        });

        // File compaction metrics
        let compaction_opportunity = if data_files_total_files > 100 {
            0.5
        } else {
            0.0
        };
        let compaction_priority = if compaction_opportunity > 0.7 {
            "high"
        } else if compaction_opportunity > 0.3 {
            "medium"
        } else {
            "low"
        };

        metrics.file_compaction = Some(FileCompactionMetrics {
            compaction_opportunity_score: compaction_opportunity,
            small_files_count: 0,
            small_files_size_bytes: 0,
            potential_compaction_files: 0,
            estimated_compaction_savings_bytes: 0,
            recommended_target_file_size_bytes: 128 * 1024 * 1024,
            compaction_priority: compaction_priority.to_string(),
            z_order_opportunity: false,
            z_order_columns: Vec::new(),
        });

        // Deletion vector metrics (Paimon doesn't use deletion vectors)
        metrics.deletion_vector_metrics = Some(DeletionVectorMetrics {
            deletion_vector_count: 0,
            total_deletion_vector_size_bytes: 0,
            avg_deletion_vector_size_bytes: 0.0,
            deletion_vector_age_days: 0.0,
            deleted_rows_count: 0,
            deletion_vector_impact_score: 0.0,
        });

        // Table constraints metrics
        metrics.table_constraints = Some(TableConstraintsMetrics {
            total_constraints: 0,
            check_constraints: 0,
            not_null_constraints: 0,
            unique_constraints: 0,
            foreign_key_constraints: 0,
            constraint_violation_risk: 0.0,
            data_quality_score: 1.0,
            constraint_coverage_score: 0.0,
        });

        // Clustering info
        let partition_count = metrics.partitions.len().max(1);
        let avg_files_per_cluster = if partition_count > 0 {
            data_files_total_files as f64 / partition_count as f64
        } else {
            0.0
        };
        let avg_cluster_size_bytes = if partition_count > 0 {
            data_files_total_size as f64 / partition_count as f64
        } else {
            0.0
        };

        metrics.clustering = Some(ClusteringInfo {
            clustering_columns: result.partition_keys.clone(),
            cluster_count: partition_count,
            avg_files_per_cluster,
            avg_cluster_size_bytes,
        });

        info!(
            "Paimon metrics: snapshots={}, schemas={}, partitions={}",
            result.total_snapshots, result.total_schema_versions, partition_count
        );

        Ok(())
    }

    /// Process Paimon metadata files and aggregate results.
    async fn process_paimon_metadata(
        &self,
        metadata_files: &[FileMetadata],
    ) -> Result<PaimonMetadataResult, Box<dyn Error + Send + Sync>> {
        let mut result = PaimonMetadataResult {
            oldest_timestamp: u64::MAX,
            ..Default::default()
        };

        // Count snapshots and find timestamps
        for file in metadata_files {
            let filename = file.path.rsplit('/').next().unwrap_or(&file.path);

            if filename.starts_with("snapshot-") {
                result.total_snapshots += 1;

                // Try to read and parse snapshot
                if let Ok(content) = self.storage_provider.read_file(&file.path).await {
                    if let Ok(snapshot) = serde_json::from_slice::<PaimonSnapshot>(&content) {
                        if let Some(ts) = snapshot.time_millis {
                            let ts = ts as u64;
                            result.oldest_timestamp = result.oldest_timestamp.min(ts);
                            result.newest_timestamp = result.newest_timestamp.max(ts);
                        }
                        result.current_snapshot_id = result.current_snapshot_id.max(snapshot.id);
                        result.current_schema_id = snapshot.schema_id;
                    }
                }
            } else if filename.starts_with("schema-") {
                result.total_schema_versions += 1;

                // Try to read and parse schema for partition keys
                if result.partition_keys.is_empty() {
                    if let Ok(content) = self.storage_provider.read_file(&file.path).await {
                        if let Ok(schema) = serde_json::from_slice::<PaimonSchema>(&content) {
                            result.partition_keys = schema.partition_keys;
                            result.primary_keys = schema.primary_keys;
                        }
                    }
                }
            }
        }

        if result.oldest_timestamp == u64::MAX {
            result.oldest_timestamp = 0;
        }

        Ok(result)
    }
}

/// Intermediate structure to hold aggregated results from Paimon metadata processing.
#[derive(Debug, Default)]
struct PaimonMetadataResult {
    pub total_snapshots: usize,
    pub total_schema_versions: usize,
    pub current_snapshot_id: i64,
    pub current_schema_id: i64,
    pub oldest_timestamp: u64,
    pub newest_timestamp: u64,
    pub partition_keys: Vec<String>,
    pub primary_keys: Vec<String>,
}

#[async_trait]
impl TableAnalyzer for PaimonAnalyzer {
    fn categorize_files(
        &self,
        objects: Vec<FileMetadata>,
    ) -> (Vec<FileMetadata>, Vec<FileMetadata>) {
        self.categorize_paimon_files(objects)
    }

    async fn find_referenced_files(
        &self,
        metadata_files: &[FileMetadata],
    ) -> Result<Vec<String>, Box<dyn Error + Send + Sync>> {
        self.find_paimon_referenced_files(metadata_files).await
    }

    async fn update_metrics_from_metadata(
        &self,
        metadata_files: &[FileMetadata],
        data_files_total_size: u64,
        data_files_total_files: usize,
        metrics: &mut HealthMetrics,
    ) -> Result<(), Box<dyn Error + Send + Sync>> {
        self.update_metrics_from_paimon_metadata(
            metadata_files,
            data_files_total_size,
            data_files_total_files,
            metrics,
        )
        .await
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::storage::error::StorageResult;
    use std::collections::HashMap;
    use std::sync::OnceLock;

    // Mock storage provider for testing
    struct MockStorageProvider {
        options: OnceLock<HashMap<String, String>>,
    }

    impl MockStorageProvider {
        fn new() -> Self {
            Self {
                options: OnceLock::new(),
            }
        }
    }

    #[async_trait]
    impl StorageProvider for MockStorageProvider {
        fn base_path(&self) -> &str {
            "/mock/paimon/table"
        }

        async fn validate_connection(&self, _path: &str) -> StorageResult<()> {
            Ok(())
        }

        async fn list_files(
            &self,
            _path: &str,
            _recursive: bool,
        ) -> StorageResult<Vec<FileMetadata>> {
            Ok(vec![])
        }

        async fn discover_partitions(
            &self,
            _path: &str,
            _exclude_prefixes: Vec<&str>,
        ) -> StorageResult<Vec<String>> {
            Ok(vec![])
        }

        async fn list_files_parallel(
            &self,
            _path: &str,
            _partitions: Vec<String>,
            _parallelism: usize,
        ) -> StorageResult<Vec<FileMetadata>> {
            Ok(vec![])
        }

        async fn read_file(&self, _path: &str) -> StorageResult<Vec<u8>> {
            Ok(vec![])
        }

        async fn exists(&self, _path: &str) -> StorageResult<bool> {
            Ok(true)
        }

        async fn get_metadata(&self, _path: &str) -> StorageResult<FileMetadata> {
            Ok(FileMetadata {
                path: "test".to_string(),
                size: 0,
                last_modified: None,
            })
        }

        fn options(&self) -> &HashMap<String, String> {
            self.options.get_or_init(HashMap::new)
        }

        fn clean_options(&self) -> HashMap<String, String> {
            HashMap::new()
        }

        fn uri_from_path(&self, path: &str) -> String {
            path.to_string()
        }
    }

    // Configurable mock storage provider for more complex tests
    struct ConfigurableMockStorageProvider {
        base_path: String,
        options: OnceLock<HashMap<String, String>>,
        file_contents: HashMap<String, Vec<u8>>,
    }

    impl ConfigurableMockStorageProvider {
        fn new(base_path: &str) -> Self {
            Self {
                base_path: base_path.to_string(),
                options: OnceLock::new(),
                file_contents: HashMap::new(),
            }
        }

        fn with_file_content(mut self, path: &str, content: Vec<u8>) -> Self {
            self.file_contents.insert(path.to_string(), content);
            self
        }
    }

    #[async_trait]
    impl StorageProvider for ConfigurableMockStorageProvider {
        fn base_path(&self) -> &str {
            &self.base_path
        }

        async fn validate_connection(&self, _path: &str) -> StorageResult<()> {
            Ok(())
        }

        async fn list_files(
            &self,
            _path: &str,
            _recursive: bool,
        ) -> StorageResult<Vec<FileMetadata>> {
            Ok(vec![])
        }

        async fn discover_partitions(
            &self,
            _path: &str,
            _exclude_prefixes: Vec<&str>,
        ) -> StorageResult<Vec<String>> {
            Ok(vec![])
        }

        async fn list_files_parallel(
            &self,
            _path: &str,
            _partitions: Vec<String>,
            _parallelism: usize,
        ) -> StorageResult<Vec<FileMetadata>> {
            Ok(vec![])
        }

        async fn read_file(&self, path: &str) -> StorageResult<Vec<u8>> {
            // Extract filename from path
            let filename = path.rsplit('/').next().unwrap_or(path);
            if let Some(content) = self.file_contents.get(filename) {
                Ok(content.clone())
            } else if let Some(content) = self.file_contents.get(path) {
                Ok(content.clone())
            } else {
                Ok(vec![])
            }
        }

        async fn exists(&self, _path: &str) -> StorageResult<bool> {
            Ok(true)
        }

        async fn get_metadata(&self, _path: &str) -> StorageResult<FileMetadata> {
            Ok(FileMetadata {
                path: "test".to_string(),
                size: 0,
                last_modified: None,
            })
        }

        fn options(&self) -> &HashMap<String, String> {
            self.options.get_or_init(HashMap::new)
        }

        fn clean_options(&self) -> HashMap<String, String> {
            HashMap::new()
        }

        fn uri_from_path(&self, path: &str) -> String {
            path.to_string()
        }
    }

    // Helper function to create a test PaimonAnalyzer
    fn create_test_analyzer() -> PaimonAnalyzer {
        let mock_storage = Arc::new(MockStorageProvider::new());
        PaimonAnalyzer::new(mock_storage, 4)
    }

    fn create_file_metadata(path: &str, size: u64) -> FileMetadata {
        FileMetadata {
            path: path.to_string(),
            size,
            last_modified: None,
        }
    }

    // ========== Tests for PaimonAnalyzer::new ==========

    #[test]
    fn test_paimon_analyzer_new() {
        let mock_storage = Arc::new(MockStorageProvider::new());
        let analyzer = PaimonAnalyzer::new(mock_storage, 8);
        assert_eq!(analyzer.parallelism, 8);
    }

    #[test]
    fn test_paimon_analyzer_new_default_parallelism() {
        let mock_storage = Arc::new(MockStorageProvider::new());
        let analyzer = PaimonAnalyzer::new(mock_storage, 1);
        assert_eq!(analyzer.parallelism, 1);
    }

    // ========== Tests for categorize_paimon_files ==========

    #[test]
    fn test_categorize_paimon_files_empty() {
        let analyzer = create_test_analyzer();
        let (data_files, metadata_files) = analyzer.categorize_paimon_files(vec![]);
        assert!(data_files.is_empty());
        assert!(metadata_files.is_empty());
    }

    #[test]
    fn test_categorize_paimon_files_data_only() {
        let analyzer = create_test_analyzer();
        let files = vec![
            create_file_metadata("bucket-0/data-file-1.orc", 1024),
            create_file_metadata("bucket-0/data-file-2.parquet", 2048),
            create_file_metadata("dt=2024-01-01/bucket-0/data.orc", 512),
        ];

        let (data_files, metadata_files) = analyzer.categorize_paimon_files(files);
        assert_eq!(data_files.len(), 3);
        assert!(metadata_files.is_empty());
    }

    #[test]
    fn test_categorize_paimon_files_metadata_only() {
        let analyzer = create_test_analyzer();
        let files = vec![
            create_file_metadata("/table/snapshot/snapshot-1", 100),
            create_file_metadata("/table/snapshot/snapshot-2", 100),
            create_file_metadata("/table/schema/schema-0", 200),
            create_file_metadata("/table/manifest/manifest-list-abc.avro", 300),
            create_file_metadata("/table/manifest/manifest-xyz.avro", 400),
        ];

        let (data_files, metadata_files) = analyzer.categorize_paimon_files(files);
        assert!(data_files.is_empty());
        assert_eq!(metadata_files.len(), 5);
    }

    #[test]
    fn test_categorize_paimon_files_mixed() {
        let analyzer = create_test_analyzer();
        let files = vec![
            // Data files
            create_file_metadata("bucket-0/data-file-1.orc", 1024),
            create_file_metadata("dt=2024-01-01/bucket-0/data.parquet", 2048),
            // Metadata files
            create_file_metadata("/table/snapshot/snapshot-1", 100),
            create_file_metadata("/table/schema/schema-0", 200),
            create_file_metadata("/table/manifest/manifest-list.avro", 300),
            // Files that should be ignored (not data or metadata)
            create_file_metadata("some/other/file.txt", 50),
        ];

        let (data_files, metadata_files) = analyzer.categorize_paimon_files(files);
        assert_eq!(data_files.len(), 2);
        assert_eq!(metadata_files.len(), 3);
    }

    #[test]
    fn test_categorize_paimon_files_excludes_metadata_orc() {
        let analyzer = create_test_analyzer();
        // ORC files in snapshot/schema/manifest directories should NOT be data files
        let files = vec![
            create_file_metadata("/table/snapshot/some.orc", 100),
            create_file_metadata("/table/schema/some.parquet", 200),
            create_file_metadata("/table/manifest/some.orc", 300),
        ];

        let (data_files, metadata_files) = analyzer.categorize_paimon_files(files);
        assert!(data_files.is_empty());
        assert_eq!(metadata_files.len(), 3);
    }

    // ========== Tests for find_paimon_referenced_files ==========

    #[tokio::test]
    async fn test_find_paimon_referenced_files_empty() {
        let analyzer = create_test_analyzer();
        let result = analyzer.find_paimon_referenced_files(&[]).await.unwrap();
        assert!(result.is_empty());
    }

    #[tokio::test]
    async fn test_find_paimon_referenced_files_returns_empty() {
        let analyzer = create_test_analyzer();
        let metadata_files = vec![
            create_file_metadata("/table/snapshot/snapshot-1", 100),
            create_file_metadata("/table/manifest/manifest-list.avro", 200),
        ];
        // Currently returns empty - would need manifest parsing
        let result = analyzer
            .find_paimon_referenced_files(&metadata_files)
            .await
            .unwrap();
        assert!(result.is_empty());
    }

    // ========== Tests for process_paimon_metadata ==========

    #[tokio::test]
    async fn test_process_paimon_metadata_empty() {
        let analyzer = create_test_analyzer();
        let result = analyzer.process_paimon_metadata(&[]).await.unwrap();
        assert_eq!(result.total_snapshots, 0);
        assert_eq!(result.total_schema_versions, 0);
        assert_eq!(result.oldest_timestamp, 0);
        assert_eq!(result.newest_timestamp, 0);
    }

    #[tokio::test]
    async fn test_process_paimon_metadata_with_snapshots() {
        let snapshot_json = r#"{
            "id": 5,
            "schemaId": 2,
            "timeMillis": 1705000000000,
            "totalRecordCount": 1000,
            "commitKind": "APPEND"
        }"#;

        let mock_storage = Arc::new(
            ConfigurableMockStorageProvider::new("/test")
                .with_file_content("snapshot-5", snapshot_json.as_bytes().to_vec()),
        );
        let analyzer = PaimonAnalyzer::new(mock_storage, 4);

        let metadata_files = vec![create_file_metadata("/table/snapshot/snapshot-5", 100)];

        let result = analyzer
            .process_paimon_metadata(&metadata_files)
            .await
            .unwrap();
        assert_eq!(result.total_snapshots, 1);
        assert_eq!(result.current_snapshot_id, 5);
        assert_eq!(result.current_schema_id, 2);
        assert_eq!(result.oldest_timestamp, 1705000000000);
        assert_eq!(result.newest_timestamp, 1705000000000);
    }

    #[tokio::test]
    async fn test_process_paimon_metadata_with_schema() {
        let schema_json = r#"{
            "id": 0,
            "fields": [{"id": 0, "name": "id", "type": "INT"}],
            "partitionKeys": ["dt", "region"],
            "primaryKeys": ["id"]
        }"#;

        let mock_storage = Arc::new(
            ConfigurableMockStorageProvider::new("/test")
                .with_file_content("schema-0", schema_json.as_bytes().to_vec()),
        );
        let analyzer = PaimonAnalyzer::new(mock_storage, 4);

        let metadata_files = vec![create_file_metadata("/table/schema/schema-0", 200)];

        let result = analyzer
            .process_paimon_metadata(&metadata_files)
            .await
            .unwrap();
        assert_eq!(result.total_schema_versions, 1);
        assert_eq!(result.partition_keys, vec!["dt", "region"]);
        assert_eq!(result.primary_keys, vec!["id"]);
    }

    #[tokio::test]
    async fn test_process_paimon_metadata_multiple_snapshots() {
        let snapshot1_json = r#"{"id": 1, "schemaId": 0, "timeMillis": 1700000000000}"#;
        let snapshot2_json = r#"{"id": 2, "schemaId": 0, "timeMillis": 1705000000000}"#;
        let snapshot3_json = r#"{"id": 3, "schemaId": 1, "timeMillis": 1710000000000}"#;

        let mock_storage = Arc::new(
            ConfigurableMockStorageProvider::new("/test")
                .with_file_content("snapshot-1", snapshot1_json.as_bytes().to_vec())
                .with_file_content("snapshot-2", snapshot2_json.as_bytes().to_vec())
                .with_file_content("snapshot-3", snapshot3_json.as_bytes().to_vec()),
        );
        let analyzer = PaimonAnalyzer::new(mock_storage, 4);

        let metadata_files = vec![
            create_file_metadata("/table/snapshot/snapshot-1", 100),
            create_file_metadata("/table/snapshot/snapshot-2", 100),
            create_file_metadata("/table/snapshot/snapshot-3", 100),
        ];

        let result = analyzer
            .process_paimon_metadata(&metadata_files)
            .await
            .unwrap();
        assert_eq!(result.total_snapshots, 3);
        assert_eq!(result.current_snapshot_id, 3);
        assert_eq!(result.current_schema_id, 1);
        assert_eq!(result.oldest_timestamp, 1700000000000);
        assert_eq!(result.newest_timestamp, 1710000000000);
    }

    // ========== Tests for update_metrics_from_paimon_metadata ==========

    #[tokio::test]
    async fn test_update_metrics_from_paimon_metadata_empty() {
        let analyzer = create_test_analyzer();
        let mut metrics = HealthMetrics::default();

        analyzer
            .update_metrics_from_paimon_metadata(&[], 0, 0, &mut metrics)
            .await
            .unwrap();

        // Should have schema evolution metrics
        assert!(metrics.schema_evolution.is_some());
        let schema_evo = metrics.schema_evolution.unwrap();
        assert_eq!(schema_evo.total_schema_changes, 0);

        // Should have time travel metrics
        assert!(metrics.time_travel_metrics.is_some());
        let time_travel = metrics.time_travel_metrics.unwrap();
        assert_eq!(time_travel.total_snapshots, 0);

        // Should have file compaction metrics
        assert!(metrics.file_compaction.is_some());

        // Should have deletion vector metrics (empty for Paimon)
        assert!(metrics.deletion_vector_metrics.is_some());
        let dv = metrics.deletion_vector_metrics.unwrap();
        assert_eq!(dv.deletion_vector_count, 0);

        // Should have table constraints metrics
        assert!(metrics.table_constraints.is_some());

        // Should have clustering info
        assert!(metrics.clustering.is_some());
    }

    #[tokio::test]
    async fn test_update_metrics_from_paimon_metadata_with_data() {
        let snapshot_json = r#"{
            "id": 5,
            "schemaId": 2,
            "timeMillis": 1705000000000,
            "totalRecordCount": 1000
        }"#;
        let schema_json = r#"{
            "id": 2,
            "fields": [],
            "partitionKeys": ["dt"],
            "primaryKeys": ["id"]
        }"#;

        let mock_storage = Arc::new(
            ConfigurableMockStorageProvider::new("/test")
                .with_file_content("snapshot-5", snapshot_json.as_bytes().to_vec())
                .with_file_content("schema-2", schema_json.as_bytes().to_vec()),
        );
        let analyzer = PaimonAnalyzer::new(mock_storage, 4);

        let metadata_files = vec![
            create_file_metadata("/table/snapshot/snapshot-5", 100),
            create_file_metadata("/table/schema/schema-2", 200),
        ];

        let mut metrics = HealthMetrics::default();
        analyzer
            .update_metrics_from_paimon_metadata(&metadata_files, 1024 * 1024, 10, &mut metrics)
            .await
            .unwrap();

        // Check schema evolution
        let schema_evo = metrics.schema_evolution.unwrap();
        assert_eq!(schema_evo.total_schema_changes, 1);
        assert_eq!(schema_evo.current_schema_version, 2);

        // Check time travel
        let time_travel = metrics.time_travel_metrics.unwrap();
        assert_eq!(time_travel.total_snapshots, 1);

        // Check clustering
        let clustering = metrics.clustering.unwrap();
        assert_eq!(clustering.clustering_columns, vec!["dt"]);
    }

    #[tokio::test]
    async fn test_update_metrics_compaction_priority_high() {
        let analyzer = create_test_analyzer();
        let mut metrics = HealthMetrics::default();

        // More than 100 files should trigger compaction opportunity
        analyzer
            .update_metrics_from_paimon_metadata(&[], 1024 * 1024 * 1024, 150, &mut metrics)
            .await
            .unwrap();

        let compaction = metrics.file_compaction.unwrap();
        assert_eq!(compaction.compaction_opportunity_score, 0.5);
        assert_eq!(compaction.compaction_priority, "medium");
    }

    #[tokio::test]
    async fn test_update_metrics_compaction_priority_low() {
        let analyzer = create_test_analyzer();
        let mut metrics = HealthMetrics::default();

        // Less than 100 files should have low compaction opportunity
        analyzer
            .update_metrics_from_paimon_metadata(&[], 1024 * 1024, 50, &mut metrics)
            .await
            .unwrap();

        let compaction = metrics.file_compaction.unwrap();
        assert_eq!(compaction.compaction_opportunity_score, 0.0);
        assert_eq!(compaction.compaction_priority, "low");
    }

    // ========== Tests for TableAnalyzer trait implementation ==========

    #[test]
    fn test_categorize_files_trait() {
        let analyzer = create_test_analyzer();
        let files = vec![
            create_file_metadata("bucket-0/data.orc", 1024),
            create_file_metadata("/table/snapshot/snapshot-1", 100),
        ];

        let (data_files, metadata_files) = analyzer.categorize_files(files);
        assert_eq!(data_files.len(), 1);
        assert_eq!(metadata_files.len(), 1);
    }

    #[tokio::test]
    async fn test_find_referenced_files_trait() {
        let analyzer = create_test_analyzer();
        let result = analyzer.find_referenced_files(&[]).await.unwrap();
        assert!(result.is_empty());
    }

    #[tokio::test]
    async fn test_update_metrics_from_metadata_trait() {
        let analyzer = create_test_analyzer();
        let mut metrics = HealthMetrics::default();

        analyzer
            .update_metrics_from_metadata(&[], 0, 0, &mut metrics)
            .await
            .unwrap();

        assert!(metrics.schema_evolution.is_some());
        assert!(metrics.time_travel_metrics.is_some());
    }

    // ========== Tests for PaimonSnapshot parsing ==========

    #[test]
    fn test_paimon_snapshot_parse() {
        let json = r#"{
            "id": 10,
            "schemaId": 3,
            "timeMillis": 1705000000000,
            "totalRecordCount": 5000,
            "commitKind": "COMPACT"
        }"#;

        let snapshot: PaimonSnapshot = serde_json::from_str(json).unwrap();
        assert_eq!(snapshot.id, 10);
        assert_eq!(snapshot.schema_id, 3);
        assert_eq!(snapshot.time_millis, Some(1705000000000));
        assert_eq!(snapshot.total_record_count, Some(5000));
        assert_eq!(snapshot.commit_kind, Some("COMPACT".to_string()));
    }

    #[test]
    fn test_paimon_snapshot_minimal() {
        let json = r#"{"id": 1, "schemaId": 0}"#;
        let snapshot: PaimonSnapshot = serde_json::from_str(json).unwrap();
        assert_eq!(snapshot.id, 1);
        assert_eq!(snapshot.schema_id, 0);
        assert!(snapshot.time_millis.is_none());
        assert!(snapshot.total_record_count.is_none());
    }

    #[test]
    fn test_paimon_snapshot_default() {
        let snapshot = PaimonSnapshot::default();
        assert_eq!(snapshot.id, 0);
        assert_eq!(snapshot.schema_id, 0);
        assert!(snapshot.time_millis.is_none());
    }

    // ========== Tests for PaimonSchema parsing ==========

    #[test]
    fn test_paimon_schema_parse() {
        let json = r#"{
            "id": 2,
            "fields": [{"id": 0, "name": "id", "type": "INT"}],
            "partitionKeys": ["dt", "hour"],
            "primaryKeys": ["id", "dt"],
            "options": {"bucket": "8"}
        }"#;

        let schema: PaimonSchema = serde_json::from_str(json).unwrap();
        assert_eq!(schema.id, 2);
        assert_eq!(schema.fields.len(), 1);
        assert_eq!(schema.partition_keys, vec!["dt", "hour"]);
        assert_eq!(schema.primary_keys, vec!["id", "dt"]);
        assert!(schema.options.is_some());
    }

    #[test]
    fn test_paimon_schema_minimal() {
        let json = r#"{
            "id": 0,
            "fields": [],
            "partitionKeys": [],
            "primaryKeys": []
        }"#;

        let schema: PaimonSchema = serde_json::from_str(json).unwrap();
        assert_eq!(schema.id, 0);
        assert!(schema.fields.is_empty());
        assert!(schema.partition_keys.is_empty());
        assert!(schema.primary_keys.is_empty());
        assert!(schema.options.is_none());
    }

    #[test]
    fn test_paimon_schema_default() {
        let schema = PaimonSchema::default();
        assert_eq!(schema.id, 0);
        assert!(schema.fields.is_empty());
        assert!(schema.partition_keys.is_empty());
        assert!(schema.primary_keys.is_empty());
    }

    // ========== Tests for PaimonMetadataResult ==========

    #[test]
    fn test_paimon_metadata_result_default() {
        let result = PaimonMetadataResult::default();
        assert_eq!(result.total_snapshots, 0);
        assert_eq!(result.total_schema_versions, 0);
        assert_eq!(result.current_snapshot_id, 0);
        assert_eq!(result.current_schema_id, 0);
        assert_eq!(result.oldest_timestamp, 0);
        assert_eq!(result.newest_timestamp, 0);
        assert!(result.partition_keys.is_empty());
        assert!(result.primary_keys.is_empty());
    }
}