innodb-utils 5.1.0

InnoDB file analysis toolkit
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
//! B+Tree index health metrics for InnoDB tablespaces.
//!
//! Computes per-index health metrics including fill factor, fragmentation,
//! garbage ratio, and tree depth by analyzing INDEX page headers. The analysis
//! requires only a single pass over the tablespace pages.
//!
//! # Usage
//!
//! ```rust,ignore
//! use idb::innodb::tablespace::Tablespace;
//! use idb::innodb::health::{extract_index_page_snapshot, analyze_health};
//!
//! let mut ts = Tablespace::open("table.ibd").unwrap();
//! let page_size = ts.page_size();
//! let mut snapshots = Vec::new();
//! let mut empty_pages = 0u64;
//! let mut total_pages = 0u64;
//!
//! ts.for_each_page(|page_num, data| {
//!     total_pages += 1;
//!     if let Some(snap) = extract_index_page_snapshot(data, page_num) {
//!         snapshots.push(snap);
//!     } else if data.iter().all(|&b| b == 0) {
//!         empty_pages += 1;
//!     }
//!     Ok(())
//! }).unwrap();
//!
//! let report = analyze_health(snapshots, page_size, total_pages, empty_pages, "table.ibd");
//! println!("Indexes: {}", report.indexes.len());
//! ```

use serde::Serialize;
use std::collections::BTreeMap;

use crate::innodb::constants::*;
use crate::innodb::index::IndexHeader;
use crate::innodb::page::FilHeader;
use crate::innodb::page_types::PageType;

/// Intermediate per-page snapshot of an INDEX page's key metrics.
#[derive(Debug, Clone)]
pub struct IndexPageSnapshot {
    /// Page number within the tablespace.
    pub page_number: u64,
    /// Index ID this page belongs to.
    pub index_id: u64,
    /// B+Tree level (0 = leaf).
    pub level: u16,
    /// Byte offset of the heap top within the page.
    pub heap_top: u16,
    /// Bytes consumed by deleted/garbage records.
    pub garbage: u16,
    /// Number of user records on this page.
    pub n_recs: u16,
    /// Previous page pointer (FIL_NULL if none).
    pub prev: u32,
    /// Next page pointer (FIL_NULL if none).
    pub next: u32,
}

/// Aggregated health metrics for a single index.
#[derive(Debug, Clone, Serialize)]
pub struct IndexHealth {
    /// Index ID.
    pub index_id: u64,
    /// Optional index name (resolved from SDI, if available).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub index_name: Option<String>,
    /// Maximum B+Tree depth observed (leaf level is 0).
    pub tree_depth: u16,
    /// Total number of pages belonging to this index.
    pub total_pages: u64,
    /// Number of leaf-level pages (level 0).
    pub leaf_pages: u64,
    /// Number of non-leaf pages (level > 0).
    pub non_leaf_pages: u64,
    /// Total user records across all pages.
    pub total_records: u64,
    /// Average fill factor across all pages (0.0..1.0).
    pub avg_fill_factor: f64,
    /// Minimum fill factor across all pages.
    pub min_fill_factor: f64,
    /// Maximum fill factor across all pages.
    pub max_fill_factor: f64,
    /// Average garbage ratio across all pages (0.0..1.0).
    pub avg_garbage_ratio: f64,
    /// Total garbage bytes across all pages.
    pub total_garbage_bytes: u64,
    /// Fragmentation ratio for leaf pages (0.0..1.0).
    /// Measured as the fraction of non-sequential page transitions.
    pub fragmentation: f64,
    /// Number of leaf pages with zero user records.
    pub empty_leaf_pages: u64,
    /// Delete-marked records across leaf pages (populated when bloat analysis enabled).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub delete_marked_records: Option<u64>,
    /// Total records walked across leaf pages (populated when bloat analysis enabled).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub total_walked_records: Option<u64>,
    /// Bloat assessment (populated when bloat analysis enabled).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub bloat: Option<BloatScore>,
    /// Cardinality estimate for leading key column (populated when cardinality enabled).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub cardinality: Option<CardinalityEstimate>,
}

/// Tablespace-level health summary.
#[derive(Debug, Clone, Serialize)]
pub struct TablespaceHealth {
    /// Total pages in the tablespace.
    pub total_pages: u64,
    /// Number of INDEX pages.
    pub index_pages: u64,
    /// Number of non-INDEX pages (FSP, INODE, SDI, etc.).
    pub non_index_pages: u64,
    /// Number of all-zero (empty/allocated) pages.
    pub empty_pages: u64,
    /// Page size in bytes.
    pub page_size: u32,
    /// Average fill factor across all INDEX pages.
    pub avg_fill_factor: f64,
    /// Average garbage ratio across all INDEX pages.
    pub avg_garbage_ratio: f64,
    /// Average fragmentation across all indexes.
    pub avg_fragmentation: f64,
    /// Number of distinct indexes found.
    pub index_count: u64,
    /// Number of RTREE (spatial index) pages.
    #[serde(skip_serializing_if = "is_zero_u64")]
    pub rtree_pages: u64,
    /// Number of BLOB/LOB pages.
    #[serde(skip_serializing_if = "is_zero_u64")]
    pub lob_pages: u64,
    /// Number of UNDO log pages.
    #[serde(skip_serializing_if = "is_zero_u64")]
    pub undo_pages: u64,
}

fn is_zero_u64(v: &u64) -> bool {
    *v == 0
}

/// Top-level health report for a tablespace.
#[derive(Debug, Clone, Serialize)]
pub struct HealthReport {
    /// Path to the analyzed file.
    pub file: String,
    /// Tablespace-level summary.
    pub summary: TablespaceHealth,
    /// Per-index health metrics.
    pub indexes: Vec<IndexHealth>,
}

/// Extract an [`IndexPageSnapshot`] from raw page bytes.
///
/// Returns `None` if the page is not an INDEX page (type 17855) or if
/// the page data is too short to contain valid FIL + INDEX headers.
pub fn extract_index_page_snapshot(
    page_data: &[u8],
    page_number: u64,
) -> Option<IndexPageSnapshot> {
    let fil = FilHeader::parse(page_data)?;
    if fil.page_type != PageType::Index {
        return None;
    }

    let idx = IndexHeader::parse(page_data)?;

    Some(IndexPageSnapshot {
        page_number,
        index_id: idx.index_id,
        level: idx.level,
        heap_top: idx.heap_top,
        garbage: idx.garbage,
        n_recs: idx.n_recs,
        prev: fil.prev_page,
        next: fil.next_page,
    })
}

/// Compute the fill factor for a single INDEX page.
///
/// The usable data area is `page_size - PAGE_DATA_OFFSET - SIZE_FIL_TRAILER`.
/// The used portion is `heap_top - PAGE_DATA_OFFSET - garbage`.
/// Result is clamped to \[0.0, 1.0\].
pub fn compute_fill_factor(heap_top: u16, garbage: u16, page_size: u32) -> f64 {
    let usable = page_size as f64 - PAGE_DATA_OFFSET as f64 - SIZE_FIL_TRAILER as f64;
    if usable <= 0.0 {
        return 0.0;
    }
    let used = heap_top as f64 - PAGE_DATA_OFFSET as f64 - garbage as f64;
    (used / usable).clamp(0.0, 1.0)
}

/// Compute the garbage ratio for a single INDEX page.
///
/// Ratio of garbage bytes to the usable data area.
pub fn compute_garbage_ratio(garbage: u16, page_size: u32) -> f64 {
    let usable = page_size as f64 - PAGE_DATA_OFFSET as f64 - SIZE_FIL_TRAILER as f64;
    if usable <= 0.0 {
        return 0.0;
    }
    (garbage as f64 / usable).clamp(0.0, 1.0)
}

/// Compute leaf-level fragmentation for a set of pages sorted by page number.
///
/// Fragmentation is the ratio of non-sequential page transitions to the
/// total number of transitions. A perfectly defragmented index has all
/// leaf pages in sequential page-number order (fragmentation = 0.0).
///
/// Returns 0.0 for a single page (no transitions to measure).
pub fn compute_fragmentation(leaf_page_numbers: &mut [u64]) -> f64 {
    if leaf_page_numbers.len() <= 1 {
        return 0.0;
    }
    leaf_page_numbers.sort_unstable();
    let transitions = leaf_page_numbers.len() - 1;
    let non_sequential = leaf_page_numbers
        .windows(2)
        .filter(|w| w[1] != w[0] + 1)
        .count();
    non_sequential as f64 / transitions as f64
}

/// Analyze collected INDEX page snapshots and produce a [`HealthReport`].
///
/// Groups snapshots by `index_id`, computes per-index metrics, and builds
/// a tablespace summary. The `total_pages` and `empty_pages` counts should
/// include all pages in the tablespace, not just INDEX pages.
pub fn analyze_health(
    snapshots: Vec<IndexPageSnapshot>,
    page_size: u32,
    total_pages: u64,
    empty_pages: u64,
    file: &str,
) -> HealthReport {
    // Group by index_id
    let mut groups: BTreeMap<u64, Vec<IndexPageSnapshot>> = BTreeMap::new();
    for snap in snapshots {
        groups.entry(snap.index_id).or_default().push(snap);
    }

    let index_page_count: u64 = groups.values().map(|v| v.len() as u64).sum();
    let non_index_pages = total_pages.saturating_sub(index_page_count + empty_pages);

    let mut indexes = Vec::with_capacity(groups.len());
    let mut all_fill_factors = Vec::new();
    let mut all_garbage_ratios = Vec::new();

    for (index_id, pages) in &groups {
        let mut tree_depth: u16 = 0;
        let mut leaf_pages: u64 = 0;
        let mut non_leaf_pages: u64 = 0;
        let mut total_records: u64 = 0;
        let mut total_garbage_bytes: u64 = 0;
        let mut empty_leaf_pages: u64 = 0;
        let mut fill_factors = Vec::with_capacity(pages.len());
        let mut garbage_ratios = Vec::with_capacity(pages.len());
        let mut leaf_page_numbers = Vec::new();

        for snap in pages {
            let ff = compute_fill_factor(snap.heap_top, snap.garbage, page_size);
            let gr = compute_garbage_ratio(snap.garbage, page_size);
            fill_factors.push(ff);
            garbage_ratios.push(gr);
            all_fill_factors.push(ff);
            all_garbage_ratios.push(gr);

            if snap.level > tree_depth {
                tree_depth = snap.level;
            }
            if snap.level == 0 {
                leaf_pages += 1;
                leaf_page_numbers.push(snap.page_number);
                if snap.n_recs == 0 {
                    empty_leaf_pages += 1;
                }
            } else {
                non_leaf_pages += 1;
            }
            total_records += snap.n_recs as u64;
            total_garbage_bytes += snap.garbage as u64;
        }

        let avg_fill = if fill_factors.is_empty() {
            0.0
        } else {
            fill_factors.iter().sum::<f64>() / fill_factors.len() as f64
        };
        let min_fill = fill_factors.iter().cloned().fold(f64::INFINITY, f64::min);
        let max_fill = fill_factors
            .iter()
            .cloned()
            .fold(f64::NEG_INFINITY, f64::max);
        let avg_garbage = if garbage_ratios.is_empty() {
            0.0
        } else {
            garbage_ratios.iter().sum::<f64>() / garbage_ratios.len() as f64
        };

        let fragmentation = compute_fragmentation(&mut leaf_page_numbers);

        // tree_depth reported as max_level + 1 (number of levels including leaf)
        indexes.push(IndexHealth {
            index_id: *index_id,
            index_name: None,
            tree_depth: tree_depth + 1,
            total_pages: pages.len() as u64,
            leaf_pages,
            non_leaf_pages,
            total_records,
            avg_fill_factor: round2(avg_fill),
            min_fill_factor: round2(if min_fill.is_infinite() {
                0.0
            } else {
                min_fill
            }),
            max_fill_factor: round2(if max_fill.is_infinite() {
                0.0
            } else {
                max_fill
            }),
            avg_garbage_ratio: round2(avg_garbage),
            total_garbage_bytes,
            fragmentation: round2(fragmentation),
            empty_leaf_pages,
            delete_marked_records: None,
            total_walked_records: None,
            bloat: None,
            cardinality: None,
        });
    }

    let avg_fill = if all_fill_factors.is_empty() {
        0.0
    } else {
        round2(all_fill_factors.iter().sum::<f64>() / all_fill_factors.len() as f64)
    };
    let avg_garbage = if all_garbage_ratios.is_empty() {
        0.0
    } else {
        round2(all_garbage_ratios.iter().sum::<f64>() / all_garbage_ratios.len() as f64)
    };
    let avg_frag = if indexes.is_empty() {
        0.0
    } else {
        round2(indexes.iter().map(|i| i.fragmentation).sum::<f64>() / indexes.len() as f64)
    };

    HealthReport {
        file: file.to_string(),
        summary: TablespaceHealth {
            total_pages,
            index_pages: index_page_count,
            non_index_pages,
            empty_pages,
            page_size,
            avg_fill_factor: avg_fill,
            avg_garbage_ratio: avg_garbage,
            avg_fragmentation: avg_frag,
            index_count: indexes.len() as u64,
            rtree_pages: 0,
            lob_pages: 0,
            undo_pages: 0,
        },
        indexes,
    }
}

/// Round to 2 decimal places.
fn round2(v: f64) -> f64 {
    (v * 100.0).round() / 100.0
}

// ---------------------------------------------------------------------------
// Bloat scoring (Issue #157)
// ---------------------------------------------------------------------------

/// Bloat severity weights — must sum to 1.0.
const W_FILL: f64 = 0.30;
const W_GARBAGE: f64 = 0.25;
const W_FRAG: f64 = 0.25;
const W_DELETE: f64 = 0.20;

/// Bloat grade from A (healthy) to F (severely bloated).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize)]
pub enum BloatGrade {
    A,
    B,
    C,
    D,
    F,
}

impl BloatGrade {
    /// Human-readable label.
    pub fn label(self) -> &'static str {
        match self {
            BloatGrade::A => "A",
            BloatGrade::B => "B",
            BloatGrade::C => "C",
            BloatGrade::D => "D",
            BloatGrade::F => "F",
        }
    }
}

impl std::fmt::Display for BloatGrade {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.label())
    }
}

/// Individual component values used in bloat scoring.
#[derive(Debug, Clone, Serialize)]
pub struct BloatComponents {
    /// 1 - avg_fill_factor (lower fill = more wasted space).
    pub fill_factor_deficit: f64,
    /// Average garbage bytes / usable space.
    pub garbage_ratio: f64,
    /// Leaf page fragmentation (non-sequential ratio).
    pub fragmentation: f64,
    /// Delete-marked records / total walked records.
    pub delete_mark_ratio: f64,
}

/// Per-index bloat assessment.
#[derive(Debug, Clone, Serialize)]
pub struct BloatScore {
    /// Raw bloat score (0.0 = no bloat, 1.0 = maximum bloat).
    pub score: f64,
    /// Letter grade.
    pub grade: BloatGrade,
    /// Component breakdown.
    pub components: BloatComponents,
    /// Human-readable recommendation (None for grade A/B).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub recommendation: Option<String>,
}

/// Compute a bloat score for an index given its health metrics and delete-mark ratio.
///
/// The score is a weighted average of four components:
/// - Fill factor deficit (30%): `1 - avg_fill_factor`
/// - Garbage ratio (25%): average garbage bytes / usable space
/// - Fragmentation (25%): non-sequential leaf page transitions
/// - Delete-mark ratio (20%): delete-marked records / total records
///
/// Grade thresholds: A < 0.10, B < 0.20, C < 0.35, D < 0.50, F >= 0.50
pub fn score_bloat(health: &IndexHealth, delete_mark_ratio: f64) -> BloatScore {
    let components = BloatComponents {
        fill_factor_deficit: round2(1.0 - health.avg_fill_factor),
        garbage_ratio: health.avg_garbage_ratio,
        fragmentation: health.fragmentation,
        delete_mark_ratio: round2(delete_mark_ratio),
    };

    let score = (W_FILL * components.fill_factor_deficit
        + W_GARBAGE * components.garbage_ratio
        + W_FRAG * components.fragmentation
        + W_DELETE * components.delete_mark_ratio)
        .clamp(0.0, 1.0);

    let score = round2(score);

    let grade = if score < 0.10 {
        BloatGrade::A
    } else if score < 0.20 {
        BloatGrade::B
    } else if score < 0.35 {
        BloatGrade::C
    } else if score < 0.50 {
        BloatGrade::D
    } else {
        BloatGrade::F
    };

    let recommendation = match grade {
        BloatGrade::A | BloatGrade::B => None,
        BloatGrade::C => Some("Consider OPTIMIZE TABLE during low-traffic period".to_string()),
        BloatGrade::D => Some("OPTIMIZE TABLE recommended; significant bloat detected".to_string()),
        BloatGrade::F => {
            Some("OPTIMIZE TABLE strongly recommended; severe bloat detected".to_string())
        }
    };

    BloatScore {
        score,
        grade,
        components,
        recommendation,
    }
}

// ---------------------------------------------------------------------------
// Cardinality estimation (Issue #156)
// ---------------------------------------------------------------------------

/// Per-index cardinality estimate for the leading key column.
#[derive(Debug, Clone, Serialize)]
pub struct CardinalityEstimate {
    /// Column name of the leading key column.
    pub column_name: String,
    /// Estimated number of distinct values.
    pub estimated_distinct: u64,
    /// Total records sampled.
    pub sampled_records: u64,
    /// Number of leaf pages sampled.
    pub sampled_pages: u64,
    /// Total leaf pages in the index.
    pub total_leaf_pages: u64,
    /// Confidence level (0.0-1.0, based on sample ratio).
    pub confidence: f64,
}

/// Estimate cardinality of the leading key column by sampling leaf pages.
///
/// Uses deterministic sampling (every k-th page) to avoid a `rand` dependency.
/// For each sampled page, decodes records and collects the leading column value
/// into a `HashSet` for distinct counting. Extrapolates to the full index.
///
/// Returns `None` if `leaf_pages` is empty or record decoding fails.
pub fn estimate_cardinality(
    ts: &mut crate::innodb::tablespace::Tablespace,
    leaf_pages: &[u64],
    columns: &[crate::innodb::field_decode::ColumnStorageInfo],
    column_name: &str,
    page_size: u32,
    sample_size: usize,
) -> Option<CardinalityEstimate> {
    if leaf_pages.is_empty() || columns.is_empty() || sample_size == 0 {
        return None;
    }

    let total_leaf = leaf_pages.len();
    let actual_sample = sample_size.min(total_leaf);

    // Deterministic sampling: every k-th page
    let step = if actual_sample >= total_leaf {
        1
    } else {
        total_leaf / actual_sample
    };

    let mut distinct_values = std::collections::HashSet::new();
    let mut total_records_sampled = 0u64;
    let mut pages_sampled = 0u64;

    for i in (0..total_leaf).step_by(step) {
        if pages_sampled >= actual_sample as u64 {
            break;
        }
        let page_num = leaf_pages[i];
        let page_data = match ts.read_page(page_num) {
            Ok(d) => d,
            Err(_) => continue,
        };

        let rows = crate::innodb::export::decode_page_records(
            &page_data, columns, false, false, page_size,
        );
        for row in &rows {
            total_records_sampled += 1;
            // Extract leading column value (first column in the row)
            if let Some((_name, value)) = row.first() {
                distinct_values.insert(format!("{:?}", value));
            }
        }
        pages_sampled += 1;
    }

    if pages_sampled == 0 || total_records_sampled == 0 {
        return None;
    }

    let distinct_in_sample = distinct_values.len() as u64;
    // Extrapolate: scale by (total_leaf / sampled), capped at the extrapolated
    // total record count to avoid estimates exceeding the actual record count.
    let scale = total_leaf as f64 / pages_sampled as f64;
    let estimated_total_records = (total_records_sampled as f64 * scale) as u64;
    let raw_estimate = (distinct_in_sample as f64 * scale) as u64;
    let estimated = raw_estimate.min(estimated_total_records);
    let confidence = round2((pages_sampled as f64 / total_leaf as f64).min(1.0));

    Some(CardinalityEstimate {
        column_name: column_name.to_string(),
        estimated_distinct: estimated,
        sampled_records: total_records_sampled,
        sampled_pages: pages_sampled,
        total_leaf_pages: total_leaf as u64,
        confidence,
    })
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_fill_factor_full_page() {
        // heap_top at end of usable area, no garbage
        let usable_end = (16384 - SIZE_FIL_TRAILER) as u16;
        let ff = compute_fill_factor(usable_end, 0, 16384);
        assert!((ff - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_fill_factor_half_page() {
        let usable = 16384.0 - PAGE_DATA_OFFSET as f64 - SIZE_FIL_TRAILER as f64;
        let half_heap_top = PAGE_DATA_OFFSET as u16 + (usable / 2.0) as u16;
        let ff = compute_fill_factor(half_heap_top, 0, 16384);
        assert!((ff - 0.5).abs() < 0.01);
    }

    #[test]
    fn test_fill_factor_empty_page() {
        let ff = compute_fill_factor(PAGE_DATA_OFFSET as u16, 0, 16384);
        assert!(ff.abs() < 0.001);
    }

    #[test]
    fn test_fill_factor_with_garbage() {
        // heap_top at 3/4, garbage = 1/4 of usable => fill = 0.5
        let usable = 16384.0 - PAGE_DATA_OFFSET as f64 - SIZE_FIL_TRAILER as f64;
        let heap_top = PAGE_DATA_OFFSET as u16 + (usable * 0.75) as u16;
        let garbage = (usable * 0.25) as u16;
        let ff = compute_fill_factor(heap_top, garbage, 16384);
        assert!((ff - 0.5).abs() < 0.01);
    }

    #[test]
    fn test_garbage_ratio() {
        let usable = 16384.0 - PAGE_DATA_OFFSET as f64 - SIZE_FIL_TRAILER as f64;
        let garbage = (usable * 0.25) as u16;
        let gr = compute_garbage_ratio(garbage, 16384);
        assert!((gr - 0.25).abs() < 0.01);
    }

    #[test]
    fn test_fragmentation_sequential() {
        let mut pages = vec![1, 2, 3, 4, 5];
        assert!(compute_fragmentation(&mut pages).abs() < 0.001);
    }

    #[test]
    fn test_fragmentation_scattered() {
        let mut pages = vec![1, 10, 20, 30, 40];
        let frag = compute_fragmentation(&mut pages);
        assert!((frag - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_fragmentation_single_page() {
        let mut pages = vec![5];
        assert!(compute_fragmentation(&mut pages).abs() < 0.001);
    }

    #[test]
    fn test_fragmentation_empty() {
        let mut pages: Vec<u64> = vec![];
        assert!(compute_fragmentation(&mut pages).abs() < 0.001);
    }

    #[test]
    fn test_fragmentation_partial() {
        // 1->2 sequential, 2->5 non-sequential, 5->6 sequential = 1/3
        let mut pages = vec![1, 2, 5, 6];
        let frag = compute_fragmentation(&mut pages);
        assert!((frag - 1.0 / 3.0).abs() < 0.01);
    }

    #[test]
    fn test_analyze_health_groups_by_index() {
        let snapshots = vec![
            IndexPageSnapshot {
                page_number: 3,
                index_id: 100,
                level: 0,
                heap_top: 8000,
                garbage: 0,
                n_recs: 50,
                prev: FIL_NULL,
                next: 4,
            },
            IndexPageSnapshot {
                page_number: 4,
                index_id: 100,
                level: 0,
                heap_top: 8000,
                garbage: 0,
                n_recs: 50,
                prev: 3,
                next: FIL_NULL,
            },
            IndexPageSnapshot {
                page_number: 5,
                index_id: 200,
                level: 0,
                heap_top: 4000,
                garbage: 100,
                n_recs: 20,
                prev: FIL_NULL,
                next: FIL_NULL,
            },
        ];

        let report = analyze_health(snapshots, 16384, 10, 2, "test.ibd");
        assert_eq!(report.indexes.len(), 2);
        assert_eq!(report.summary.index_count, 2);
        assert_eq!(report.summary.index_pages, 3);
        assert_eq!(report.summary.empty_pages, 2);
        assert_eq!(report.summary.total_pages, 10);

        let idx100 = report.indexes.iter().find(|i| i.index_id == 100).unwrap();
        assert_eq!(idx100.total_pages, 2);
        assert_eq!(idx100.leaf_pages, 2);
        assert_eq!(idx100.total_records, 100);
        assert_eq!(idx100.tree_depth, 1);
        // Sequential pages 3->4, fragmentation should be 0
        assert!(idx100.fragmentation.abs() < 0.001);

        let idx200 = report.indexes.iter().find(|i| i.index_id == 200).unwrap();
        assert_eq!(idx200.total_pages, 1);
        assert_eq!(idx200.total_records, 20);
        assert!(idx200.total_garbage_bytes > 0);
    }

    #[test]
    fn test_analyze_health_empty() {
        let report = analyze_health(vec![], 16384, 5, 5, "empty.ibd");
        assert!(report.indexes.is_empty());
        assert_eq!(report.summary.index_count, 0);
        assert_eq!(report.summary.total_pages, 5);
        assert_eq!(report.summary.empty_pages, 5);
    }

    #[test]
    fn test_analyze_health_multi_level() {
        let snapshots = vec![
            IndexPageSnapshot {
                page_number: 3,
                index_id: 100,
                level: 2, // root
                heap_top: 300,
                garbage: 0,
                n_recs: 2,
                prev: FIL_NULL,
                next: FIL_NULL,
            },
            IndexPageSnapshot {
                page_number: 4,
                index_id: 100,
                level: 1,
                heap_top: 500,
                garbage: 0,
                n_recs: 5,
                prev: FIL_NULL,
                next: FIL_NULL,
            },
            IndexPageSnapshot {
                page_number: 5,
                index_id: 100,
                level: 0,
                heap_top: 8000,
                garbage: 0,
                n_recs: 50,
                prev: FIL_NULL,
                next: 6,
            },
            IndexPageSnapshot {
                page_number: 6,
                index_id: 100,
                level: 0,
                heap_top: 8000,
                garbage: 0,
                n_recs: 50,
                prev: 5,
                next: FIL_NULL,
            },
        ];

        let report = analyze_health(snapshots, 16384, 10, 0, "multi.ibd");
        let idx = &report.indexes[0];
        assert_eq!(idx.tree_depth, 3); // levels 0, 1, 2 => depth = 3
        assert_eq!(idx.leaf_pages, 2);
        assert_eq!(idx.non_leaf_pages, 2);
        assert_eq!(idx.total_records, 107);
    }

    // -- Bloat scoring tests --

    fn make_health(fill: f64, garbage: f64, frag: f64) -> IndexHealth {
        IndexHealth {
            index_id: 1,
            index_name: None,
            tree_depth: 1,
            total_pages: 100,
            leaf_pages: 90,
            non_leaf_pages: 10,
            total_records: 5000,
            avg_fill_factor: fill,
            min_fill_factor: fill,
            max_fill_factor: fill,
            avg_garbage_ratio: garbage,
            total_garbage_bytes: 0,
            fragmentation: frag,
            empty_leaf_pages: 0,
            delete_marked_records: None,
            total_walked_records: None,
            bloat: None,
            cardinality: None,
        }
    }

    #[test]
    fn test_bloat_weights_sum_to_one() {
        let total = W_FILL + W_GARBAGE + W_FRAG + W_DELETE;
        assert!((total - 1.0).abs() < 0.001);
    }

    #[test]
    fn test_score_bloat_healthy() {
        let h = make_health(0.95, 0.01, 0.02);
        let s = score_bloat(&h, 0.0);
        assert_eq!(s.grade, BloatGrade::A);
        assert!(s.score < 0.10);
        assert!(s.recommendation.is_none());
    }

    #[test]
    fn test_score_bloat_moderate() {
        let h = make_health(0.60, 0.20, 0.30);
        let s = score_bloat(&h, 0.15);
        assert!(
            s.grade == BloatGrade::C || s.grade == BloatGrade::D,
            "Expected C or D, got {:?} (score={})",
            s.grade,
            s.score
        );
        assert!(s.recommendation.is_some());
    }

    #[test]
    fn test_score_bloat_severe() {
        let h = make_health(0.20, 0.60, 0.80);
        let s = score_bloat(&h, 0.70);
        assert_eq!(s.grade, BloatGrade::F);
        assert!(s.score >= 0.50);
        assert!(s.recommendation.is_some());
    }

    #[test]
    fn test_bloat_grade_boundaries() {
        // Test each grade boundary
        let h_a = make_health(0.98, 0.0, 0.0);
        assert_eq!(score_bloat(&h_a, 0.0).grade, BloatGrade::A);

        let h_b = make_health(0.80, 0.10, 0.10);
        let s_b = score_bloat(&h_b, 0.05);
        assert!(
            s_b.grade == BloatGrade::B || s_b.grade == BloatGrade::A,
            "Expected A or B, got {:?} (score={})",
            s_b.grade,
            s_b.score
        );
    }

    #[test]
    fn test_score_bloat_no_deletes() {
        let h = make_health(0.80, 0.10, 0.20);
        let s = score_bloat(&h, 0.0);
        assert_eq!(s.components.delete_mark_ratio, 0.0);
        // Without delete marks, score is driven by fill/garbage/frag only
        assert!(s.score > 0.0);
    }

    #[test]
    fn test_cardinality_empty_pages() {
        // estimate_cardinality returns None when leaf_pages is empty
        // (can't test with real Tablespace easily, so test the guard)
        assert!(true); // Guard tested in function: leaf_pages.is_empty() => None
    }
}