hudi-core 0.6.0

The native Rust implementation for Apache Hudi
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
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
use crate::Result;
use crate::config::HudiConfigs;
use crate::file_group::base_file::reader::KeyPredicate;
use crate::file_group::log_file::BlockMetadataKey;
use crate::file_group::log_file::log_block::{BlockType, LogBlock, LogBlockContent};
use crate::file_group::log_file::reader::LogFileReader;
use crate::file_group::record_batches::RecordBatches;
use crate::hfile::HFileRecord;
use crate::storage::Storage;
use crate::timeline::selector::InstantRange;
use apache_avro::Schema as AvroSchema;
use arrow_array::Array;
use std::collections::{HashMap, HashSet};
use std::sync::Arc;

/// Metadata-table records of one log block.
#[derive(Debug)]
pub struct HFileRecordGroup {
    /// The block's writer schema, from its `SCHEMA` header; `None` for
    /// delete-block tombstones and blocks without one.
    pub schema: Option<Arc<AvroSchema>>,
    pub records: Vec<HFileRecord>,
}

/// Result of scanning log files.
///
/// The scanner auto-detects the block type and returns the appropriate variant:
/// - `RecordBatches`: Arrow record batches from Avro/Parquet data blocks (regular table reads)
/// - `HFileRecords`: HFile key-value records from HFile data blocks (metadata table reads)
/// - `Empty`: No data blocks found (only command blocks or empty files)
#[derive(Debug)]
pub enum ScanResult {
    /// Arrow RecordBatches from Avro/Parquet data blocks.
    /// Used for regular table reads.
    RecordBatches(RecordBatches),
    /// HFile key-value records from HFile data blocks.
    /// Used for metadata table reads.
    /// Note: Records are NOT merged by key - caller is responsible for merging.
    HFileRecords(Vec<HFileRecord>),
    /// No data blocks found.
    Empty,
}

impl ScanResult {
    /// Returns true if the result contains Arrow record batches.
    #[must_use]
    pub fn is_record_batches(&self) -> bool {
        matches!(self, ScanResult::RecordBatches(_))
    }

    /// Returns true if the result contains HFile records.
    #[must_use]
    pub fn is_hfile_records(&self) -> bool {
        matches!(self, ScanResult::HFileRecords(_))
    }

    /// Returns true if the result is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        matches!(self, ScanResult::Empty)
    }

    /// Unwrap as RecordBatches, panicking if not that variant.
    #[must_use]
    pub fn unwrap_record_batches(self) -> RecordBatches {
        match self {
            ScanResult::RecordBatches(batches) => batches,
            _ => panic!("called unwrap_record_batches on non-RecordBatches variant"),
        }
    }

    /// Unwrap as HFileRecords, panicking if not that variant.
    #[must_use]
    pub fn unwrap_hfile_records(self) -> Vec<HFileRecord> {
        match self {
            ScanResult::HFileRecords(records) => records,
            _ => panic!("called unwrap_hfile_records on non-HFileRecords variant"),
        }
    }
}

/// Internal content type for detection.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum ContentType {
    /// Avro/Parquet/Delete blocks -> RecordBatches
    Records,
    /// HFile blocks -> HFileRecords
    HFile,
    /// No data blocks
    Empty,
}

/// Result of collecting blocks from multiple log files.
struct CollectedBlocks {
    /// All blocks organized by log file.
    all_blocks: Vec<Vec<LogBlock>>,
    /// Instant times that have been rolled back.
    rollback_targets: HashSet<String>,
}

impl CollectedBlocks {
    /// Iterate over all blocks, filtering out rollback blocks and rolled-back data.
    fn iter_valid_blocks(self) -> impl Iterator<Item = LogBlock> {
        let rollback_targets = self.rollback_targets;
        self.all_blocks.into_iter().flatten().filter(move |block| {
            // Skip rollback command blocks (they have no content)
            if block.is_rollback_block() {
                return false;
            }
            // Skip blocks whose instant time was rolled back
            match block.instant_time() {
                Ok(instant) => !rollback_targets.contains(instant),
                // If we can't get the instant time, include the block
                // and let downstream handle the error
                Err(_) => true,
            }
        })
    }
}

#[derive(Debug)]
pub struct LogFileScanner {
    hudi_configs: Arc<HudiConfigs>,
    storage: Arc<Storage>,
    /// Keys to narrow HFile log blocks to; see [`LogFileReader::with_key_predicate`].
    key_predicate: Option<KeyPredicate>,
}

impl LogFileScanner {
    pub fn new(hudi_configs: Arc<HudiConfigs>, storage: Arc<Storage>) -> Self {
        Self {
            hudi_configs,
            storage,
            key_predicate: None,
        }
    }

    /// Narrow HFile log blocks to `key_predicate` on every file this scanner reads.
    pub(crate) fn with_key_predicate(mut self, key_predicate: Option<KeyPredicate>) -> Self {
        self.key_predicate = key_predicate;
        self
    }

    /// Read all blocks from multiple log files and collect rollback targets.
    async fn collect_blocks(
        &self,
        relative_paths: Vec<String>,
        instant_range: &InstantRange,
    ) -> Result<CollectedBlocks> {
        let mut all_blocks: Vec<Vec<LogBlock>> = Vec::with_capacity(relative_paths.len());
        let mut rollback_targets: HashSet<String> = HashSet::new();

        for path in relative_paths {
            let mut reader =
                LogFileReader::new(self.hudi_configs.clone(), self.storage.clone(), &path)
                    .await?
                    .with_key_predicate(self.key_predicate.clone());
            let blocks = reader.read_all_blocks(instant_range).await?;

            // Collect rollback targets from command blocks
            for block in &blocks {
                if block.is_rollback_block() {
                    rollback_targets.insert(block.target_instant_time()?.to_string());
                }
            }

            all_blocks.push(blocks);
        }

        Ok(CollectedBlocks {
            all_blocks,
            rollback_targets,
        })
    }

    /// Detect the content type from collected blocks.
    ///
    /// Returns an error if the log files contain mixed block types (both Arrow-based
    /// and HFile blocks), which is invalid.
    fn detect_content_type(&self, collected: &CollectedBlocks) -> Result<ContentType> {
        let mut has_record_blocks = false;
        let mut has_hfile_blocks = false;
        let mut has_delete_blocks = false;

        for blocks in &collected.all_blocks {
            for block in blocks {
                match block.block_type {
                    BlockType::AvroData | BlockType::ParquetData | BlockType::CdcData => {
                        has_record_blocks = true;
                    }
                    BlockType::HfileData => {
                        has_hfile_blocks = true;
                    }
                    BlockType::Delete => {
                        has_delete_blocks = true;
                    }
                    BlockType::Command | BlockType::Corrupted => {
                        // Command and corrupted blocks don't affect content type
                    }
                }

                // Only error on actual data format conflict (Arrow vs HFile data blocks).
                // Delete blocks are compatible with both formats.
                if has_record_blocks && has_hfile_blocks {
                    return Err(crate::error::CoreError::LogBlockError(
                        "Log files contain mixed block types (both Arrow-based and HFile blocks), which is invalid".into(),
                    ));
                }
            }
        }

        if has_hfile_blocks {
            Ok(ContentType::HFile)
        } else if has_record_blocks || has_delete_blocks {
            Ok(ContentType::Records)
        } else {
            Ok(ContentType::Empty)
        }
    }

    /// Collect Arrow record batches from blocks.
    fn collect_record_batches(&self, collected: CollectedBlocks) -> Result<ScanResult> {
        // Pre-count batches for capacity
        let mut num_data_batches = 0;
        let mut num_delete_batches = 0;
        for blocks in &collected.all_blocks {
            for block in blocks {
                match block.block_type {
                    BlockType::AvroData | BlockType::ParquetData | BlockType::CdcData => {
                        if let Some(records) = block.content.as_records() {
                            num_data_batches += records.num_data_batches();
                        }
                    }
                    BlockType::Delete => {
                        if let Some(records) = block.content.as_records() {
                            num_delete_batches += records.num_delete_batches();
                        }
                    }
                    _ => {}
                }
            }
        }

        // Collect valid record batches
        let mut batches = RecordBatches::new_with_capacity(num_data_batches, num_delete_batches);
        for block in collected.iter_valid_blocks() {
            if let LogBlockContent::Records(records) = block.content {
                batches.extend(records);
            }
        }

        Ok(ScanResult::RecordBatches(batches))
    }

    /// Collect HFile records from blocks.
    ///
    /// Delete blocks may appear alongside HFile data (MDT RLI tombstones). Apply them
    /// as empty-value records so callers treat the key as deleted.
    fn collect_hfile_records(&self, collected: CollectedBlocks) -> Result<Vec<HFileRecordGroup>> {
        let mut schemas: HashMap<String, Arc<AvroSchema>> = HashMap::new();
        // HFile records keep their block's schema; delete-block keys become
        // tombstones, which need none.
        let mut groups = Vec::new();
        for block in collected.iter_valid_blocks() {
            match block.content {
                LogBlockContent::HFileRecords(hfile_records) => {
                    let schema = match block.header.get(&BlockMetadataKey::Schema) {
                        Some(json) => Some(match schemas.get(json) {
                            Some(schema) => schema.clone(),
                            None => {
                                let schema =
                                    Arc::new(AvroSchema::parse_str(json).map_err(|e| {
                                        crate::error::CoreError::LogBlockError(format!(
                                            "invalid HFile log block schema: {e}"
                                        ))
                                    })?);
                                schemas.insert(json.clone(), schema.clone());
                                schema
                            }
                        }),
                        None => None,
                    };
                    groups.push(HFileRecordGroup {
                        schema,
                        records: hfile_records,
                    });
                }
                LogBlockContent::Records(batches) => {
                    if block.block_type == BlockType::Delete {
                        let mut records = Vec::new();
                        for (batch, _) in &batches.delete_batches {
                            if let Some(keys) = batch
                                .column_by_name("recordKey")
                                .and_then(|c| c.as_any().downcast_ref::<arrow_array::StringArray>())
                            {
                                for i in 0..keys.len() {
                                    if !keys.is_null(i) {
                                        records.push(crate::hfile::HFileRecord::from_str_key(
                                            keys.value(i),
                                            Vec::new(),
                                        ));
                                    }
                                }
                            }
                        }
                        groups.push(HFileRecordGroup {
                            schema: None,
                            records,
                        });
                    }
                }
                _ => {}
            }
        }

        Ok(groups)
    }

    /// Scan metadata-table log files, whose data blocks are HFile blocks.
    ///
    /// Records come grouped in block order with the writer schema of the block
    /// they were read from (its `SCHEMA` header), since blocks written by
    /// another writer may use a different schema than the base file.
    ///
    /// Delete blocks become tombstones even when no HFile block is in range: after an
    /// MDT compaction, a write that only deletes leaves delete-only logs, which the
    /// content detection of [`Self::scan`] would classify as Arrow records.
    pub async fn scan_hfile_records(
        &self,
        relative_paths: Vec<String>,
        instant_range: &InstantRange,
    ) -> Result<Vec<HFileRecordGroup>> {
        let collected = self.collect_blocks(relative_paths, instant_range).await?;
        if collected.all_blocks.iter().flatten().any(|block| {
            matches!(
                block.block_type,
                BlockType::AvroData | BlockType::ParquetData | BlockType::CdcData
            )
        }) {
            return Err(crate::error::CoreError::LogBlockError(
                "metadata table log files must not contain Avro, Parquet, or CDC data blocks"
                    .into(),
            ));
        }
        self.collect_hfile_records(collected)
    }

    /// Scan log files and return the appropriate content type.
    ///
    /// The scanner auto-detects the block type and returns:
    /// - `ScanResult::RecordBatches` for Avro/Parquet/Delete data blocks (regular table reads)
    /// - `ScanResult::HFileRecords` for HFile data blocks (metadata table reads)
    /// - `ScanResult::Empty` if no data blocks found
    ///
    /// # Errors
    ///
    /// Returns an error if the log files contain mixed block types (both Arrow-based
    /// and HFile blocks), which indicates data corruption or misconfiguration.
    pub async fn scan(
        &self,
        relative_paths: Vec<String>,
        instant_range: &InstantRange,
    ) -> Result<ScanResult> {
        let collected = self.collect_blocks(relative_paths, instant_range).await?;

        // Detect content type from blocks
        let content_type = self.detect_content_type(&collected)?;

        match content_type {
            ContentType::Records => self.collect_record_batches(collected),
            ContentType::HFile => self.collect_hfile_records(collected).map(|groups| {
                ScanResult::HFileRecords(groups.into_iter().flat_map(|g| g.records).collect())
            }),
            ContentType::Empty => Ok(ScanResult::Empty),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::HudiConfigs;
    use crate::file_group::record_batches::RecordBatches;
    use crate::hfile::HFileReader;
    use crate::metadata::table_record::{
        FilesPartitionRecord, decode_files_partition_record_with_schema,
    };
    use crate::storage::util::parse_uri;
    use apache_avro::Schema as AvroSchema;
    use hudi_test::QuickstartTripsTable;
    use std::path::PathBuf;

    // ============================================================================
    // ScanResult unit tests
    // ============================================================================

    #[test]
    fn test_scan_result_is_record_batches() {
        let batches = ScanResult::RecordBatches(RecordBatches::new());
        assert!(batches.is_record_batches());
        assert!(!batches.is_hfile_records());
        assert!(!batches.is_empty());
    }

    #[test]
    fn test_scan_result_is_hfile_records() {
        let records = ScanResult::HFileRecords(vec![]);
        assert!(!records.is_record_batches());
        assert!(records.is_hfile_records());
        assert!(!records.is_empty());
    }

    #[test]
    fn test_scan_result_is_empty() {
        let empty = ScanResult::Empty;
        assert!(!empty.is_record_batches());
        assert!(!empty.is_hfile_records());
        assert!(empty.is_empty());
    }

    #[test]
    fn test_scan_result_unwrap_record_batches_success() {
        let batches = ScanResult::RecordBatches(RecordBatches::new());
        let unwrapped = batches.unwrap_record_batches();
        assert_eq!(unwrapped.num_data_batches(), 0);
    }

    #[test]
    #[should_panic(expected = "called unwrap_record_batches on non-RecordBatches variant")]
    fn test_scan_result_unwrap_record_batches_panic_on_hfile() {
        let records = ScanResult::HFileRecords(vec![]);
        let _ = records.unwrap_record_batches();
    }

    #[test]
    #[should_panic(expected = "called unwrap_record_batches on non-RecordBatches variant")]
    fn test_scan_result_unwrap_record_batches_panic_on_empty() {
        let empty = ScanResult::Empty;
        let _ = empty.unwrap_record_batches();
    }

    #[test]
    fn test_scan_result_unwrap_hfile_records_success() {
        let records = ScanResult::HFileRecords(vec![]);
        let unwrapped = records.unwrap_hfile_records();
        assert!(unwrapped.is_empty());
    }

    #[test]
    #[should_panic(expected = "called unwrap_hfile_records on non-HFileRecords variant")]
    fn test_scan_result_unwrap_hfile_records_panic_on_record_batches() {
        let batches = ScanResult::RecordBatches(RecordBatches::new());
        let _ = batches.unwrap_hfile_records();
    }

    #[test]
    #[should_panic(expected = "called unwrap_hfile_records on non-HFileRecords variant")]
    fn test_scan_result_unwrap_hfile_records_panic_on_empty() {
        let empty = ScanResult::Empty;
        let _ = empty.unwrap_hfile_records();
    }

    // ============================================================================
    // Integration tests
    // ============================================================================

    /// Get the metadata table directory (base path for storage).
    fn metadata_table_dir() -> PathBuf {
        let table_path = QuickstartTripsTable::V8Trips8I3U1D.path_to_mor_avro();
        PathBuf::from(table_path).join(".hoodie").join("metadata")
    }

    /// Get the files partition directory.
    fn files_partition_dir() -> PathBuf {
        metadata_table_dir().join("files")
    }

    /// Find the latest HFile in the files partition to get the Avro schema.
    fn get_avro_schema_from_hfile() -> AvroSchema {
        let dir = files_partition_dir();
        let mut hfiles: Vec<_> = std::fs::read_dir(&dir)
            .unwrap()
            .filter_map(|e| e.ok())
            .filter(|e| {
                e.path()
                    .extension()
                    .map(|ext| ext == "hfile")
                    .unwrap_or(false)
            })
            .collect();
        hfiles.sort_by_key(|e| e.file_name());

        let hfile_path = hfiles.last().expect("No HFile found").path();
        let bytes = std::fs::read(&hfile_path).expect("Failed to read HFile");
        let reader = HFileReader::new(bytes).expect("Failed to create HFileReader");
        reader
            .get_avro_schema()
            .expect("Failed to get schema")
            .expect("No schema in HFile")
            .clone()
    }

    /// Log files for the V8Trips8I3U1D test table's files partition.
    /// These are relative paths from the metadata table directory.
    /// Note: The epoch log file (with timestamp 00000000000000000) is excluded
    /// as it contains no data and has an unparseable timestamp.
    const FILES_PARTITION_LOG_FILES: &[&str] = &[
        "files/.files-0000-0_20251220210108078.log.1_10-999-2838",
        "files/.files-0000-0_20251220210123755.log.1_3-1032-2950",
        "files/.files-0000-0_20251220210125441.log.1_5-1057-3024",
        "files/.files-0000-0_20251220210127080.log.1_3-1082-3100",
        "files/.files-0000-0_20251220210128625.log.1_5-1107-3174",
        "files/.files-0000-0_20251220210129235.log.1_3-1118-3220",
        "files/.files-0000-0_20251220210130911.log.1_3-1149-3338",
    ];

    // Expected record counts per log file (index matches FILES_PARTITION_LOG_FILES)
    const EXPECTED_RECORDS_PER_FILE: &[usize] = &[4, 2, 2, 2, 2, 4, 2];

    // UUIDs for each partition's files
    const CHENNAI_UUID: &str = "6e1d5cc4-c487-487d-abbe-fe9b30b1c0cc";
    const SAN_FRANCISCO_UUID: &str = "036ded81-9ed4-479f-bcea-7145dfa0079b";
    const SAO_PAULO_UUID: &str = "8aa68f7e-afd6-4c94-b86c-8a886552e08d";

    /// The key predicate narrows HFile data blocks but must leave delete
    /// blocks whole: a tombstone for a requested key may sit in a block that
    /// also names keys nobody asked for, and dropping it would bring the
    /// deleted key back.
    #[tokio::test]
    async fn test_scan_hfile_records_key_predicate_keeps_delete_blocks_whole() -> crate::Result<()>
    {
        use crate::file_group::base_file::reader::KeyPredicate;
        use crate::file_group::log_file::writer::write_log_block;
        use crate::file_group::log_file::{BlockMetadataKey, BlockType};
        use std::collections::{BTreeMap, HashMap};

        let dir = tempfile::tempdir()?;
        let instant = "20260101000000000";
        let entries: Vec<(String, Vec<u8>)> = ["a", "b", "c"]
            .iter()
            .map(|k| (k.to_string(), k.as_bytes().to_vec()))
            .collect();
        let hfile = crate::hfile::HFileWriter::write(&entries, BTreeMap::new())
            .map_err(|e| crate::error::CoreError::HFile(e.to_string()))?;
        let header = HashMap::from([(BlockMetadataKey::InstantTime, instant.to_string())]);
        let mut bytes = write_log_block(BlockType::HfileData, header, &hfile);
        bytes.extend(crate::write::build_delete_log_block(
            instant,
            &[
                ("b".to_string(), String::new()),
                ("d".to_string(), String::new()),
            ],
            0,
        )?);
        let log = ".f-0_20260101000000000.log.1_0-0-0";
        std::fs::write(dir.path().join(log), bytes)?;

        let storage = Storage::new_with_base_url(parse_uri(dir.path().to_str().unwrap())?)?;
        let scanner = LogFileScanner::new(Arc::new(HudiConfigs::empty()), storage)
            .with_key_predicate(Some(KeyPredicate::Keys(vec!["b".to_string()])));
        let records = scanner
            .scan_hfile_records(
                vec![log.to_string()],
                &InstantRange::up_to("99991231235959999", "utc"),
            )
            .await?;
        let mut keys: Vec<(String, bool)> = records
            .iter()
            .flat_map(|group| &group.records)
            .map(|r| (r.key_as_str().unwrap().to_string(), r.value().is_empty()))
            .collect();
        keys.sort();
        // `b` from the data block, then the tombstones for `b` and `d`; `a`
        // and `c` were narrowed away.
        assert_eq!(
            keys,
            vec![
                ("b".to_string(), false),
                ("b".to_string(), true),
                ("d".to_string(), true)
            ]
        );
        Ok(())
    }

    #[tokio::test]
    async fn test_scan_metadata_table_log_files() -> crate::Result<()> {
        use crate::metadata::table_record::MetadataRecordType;
        use std::collections::HashMap;

        let metadata_dir = metadata_table_dir();
        let schema = get_avro_schema_from_hfile();

        let dir_url = parse_uri(metadata_dir.to_str().unwrap())?;
        let storage = Storage::new_with_base_url(dir_url)?;
        let hudi_configs = Arc::new(HudiConfigs::empty());
        let scanner = LogFileScanner::new(hudi_configs, Arc::clone(&storage));
        let instant_range = InstantRange::up_to("99991231235959999", "utc");

        // Scan each log file individually and validate record counts
        let mut total_records = 0;
        for (file_idx, log_file) in FILES_PARTITION_LOG_FILES.iter().enumerate() {
            let result = scanner
                .scan(vec![log_file.to_string()], &instant_range)
                .await?;

            match result {
                ScanResult::HFileRecords(records) => {
                    assert_eq!(
                        records.len(),
                        EXPECTED_RECORDS_PER_FILE[file_idx],
                        "Log file {} should have {} records",
                        log_file,
                        EXPECTED_RECORDS_PER_FILE[file_idx]
                    );
                    total_records += records.len();
                }
                ScanResult::RecordBatches(_) => {
                    panic!("Expected HFileRecords, got RecordBatches");
                }
                ScanResult::Empty => {
                    panic!("Expected HFileRecords, got Empty for {log_file}");
                }
            }
        }

        assert_eq!(total_records, 18, "Total records should be 18");

        // Scan all log files together and decode records
        let all_paths: Vec<String> = FILES_PARTITION_LOG_FILES
            .iter()
            .map(|s| s.to_string())
            .collect();
        let result = scanner.scan(all_paths, &instant_range).await?;

        let records = match result {
            ScanResult::HFileRecords(r) => r,
            _ => panic!("Expected HFileRecords from combined scan"),
        };

        assert_eq!(records.len(), 18, "Combined scan should return 18 records");

        // Decode all records and group by key
        let mut records_by_key: HashMap<String, Vec<_>> = HashMap::new();
        for record in &records {
            let decoded = decode_files_partition_record_with_schema(record, &schema)?;
            records_by_key
                .entry(decoded.key.clone())
                .or_default()
                .push(decoded);
        }

        // Validate __all_partitions__ records
        let all_partitions = records_by_key
            .get(FilesPartitionRecord::ALL_PARTITIONS_KEY)
            .expect("Should have __all_partitions__ records");
        assert_eq!(
            all_partitions.len(),
            7,
            "Should have 7 __all_partitions__ records (one per log file)"
        );
        for record in all_partitions {
            assert_eq!(record.record_type, MetadataRecordType::AllPartitions);
        }

        // Validate chennai partition records
        let chennai = records_by_key
            .get("city=chennai")
            .expect("Should have city=chennai records");
        assert_eq!(chennai.len(), 4, "Chennai should have 4 records");
        for record in chennai {
            assert_eq!(record.record_type, MetadataRecordType::Files);
            for (name, info) in &record.files {
                assert!(
                    name.contains(CHENNAI_UUID),
                    "Chennai file should contain UUID: {name}"
                );
                assert!(!info.is_deleted, "Files should not be deleted");
                assert!(info.size > 0, "File size should be > 0");
            }
        }

        // Validate san_francisco partition records
        let sf = records_by_key
            .get("city=san_francisco")
            .expect("Should have city=san_francisco records");
        assert_eq!(sf.len(), 3, "San Francisco should have 3 records");
        for record in sf {
            assert_eq!(record.record_type, MetadataRecordType::Files);
            for (name, info) in &record.files {
                assert!(
                    name.contains(SAN_FRANCISCO_UUID),
                    "San Francisco file should contain UUID: {name}"
                );
                assert!(!info.is_deleted);
            }
        }

        // Validate sao_paulo partition records
        let sp = records_by_key
            .get("city=sao_paulo")
            .expect("Should have city=sao_paulo records");
        assert_eq!(sp.len(), 4, "Sao Paulo should have 4 records");
        for record in sp {
            assert_eq!(record.record_type, MetadataRecordType::Files);
            for (name, info) in &record.files {
                assert!(
                    name.contains(SAO_PAULO_UUID),
                    "Sao Paulo file should contain UUID: {name}"
                );
                assert!(!info.is_deleted);
            }
        }

        // Validate we have exactly 4 partition keys
        assert_eq!(
            records_by_key.len(),
            4,
            "Should have 4 unique keys: __all_partitions__, chennai, san_francisco, sao_paulo"
        );

        Ok(())
    }
}