velesdb-core 1.14.1

High-performance vector database engine written in Rust
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
//! Tests for collection statistics (EPIC-046 US-001).

use super::*;
use histogram::{Histogram, HistogramBucket};

#[test]
fn test_collection_stats_new() {
    let stats = CollectionStats::new();
    assert_eq!(stats.row_count, 0);
    assert_eq!(stats.deleted_count, 0);
    assert!(stats.column_stats.is_empty());
}

#[test]
fn test_collection_stats_with_counts() {
    let stats = CollectionStats::with_counts(10_000, 500);
    assert_eq!(stats.row_count, 10_000);
    assert_eq!(stats.deleted_count, 500);
    assert_eq!(stats.live_row_count(), 9_500);
}

#[test]
fn test_deletion_ratio() {
    let stats = CollectionStats::with_counts(1000, 100);
    assert!((stats.deletion_ratio() - 0.1).abs() < 0.001);

    let empty = CollectionStats::new();
    assert!((empty.deletion_ratio() - 0.0).abs() < f64::EPSILON);
}

#[test]
fn test_estimate_selectivity_with_column() {
    let mut stats = CollectionStats::with_counts(10_000, 0);
    stats.column_stats.insert(
        "category".to_string(),
        ColumnStats::new("category").with_distinct_count(50),
    );

    let selectivity = stats.estimate_selectivity("category");
    assert!((selectivity - 0.02).abs() < 0.001); // 1/50 = 0.02
}

#[test]
fn test_estimate_selectivity_unknown_column() {
    let stats = CollectionStats::with_counts(10_000, 0);
    let selectivity = stats.estimate_selectivity("unknown");
    assert!((selectivity - 0.1).abs() < 0.001); // Default 10%
}

#[test]
fn test_column_stats_builder() {
    let col = ColumnStats::new("age")
        .with_distinct_count(100)
        .with_null_count(5);

    assert_eq!(col.name, "age");
    assert_eq!(col.distinct_count, 100);
    assert_eq!(col.null_count, 5);
}

#[test]
fn test_index_stats_builder() {
    let idx = IndexStats::new("hnsw_embedding", "HNSW")
        .with_entry_count(10_000)
        .with_depth(4);

    assert_eq!(idx.name, "hnsw_embedding");
    assert_eq!(idx.index_type, "HNSW");
    assert_eq!(idx.entry_count, 10_000);
    assert_eq!(idx.depth, 4);
}

#[test]
fn test_stats_collector_basic() {
    let mut collector = StatsCollector::new();
    collector.set_row_count(10_000);
    collector.set_deleted_count(100);
    collector.set_total_size(2_560_000); // 256 bytes avg

    let stats = collector.build();

    assert_eq!(stats.row_count, 10_000);
    assert_eq!(stats.deleted_count, 100);
    assert_eq!(stats.avg_row_size_bytes, 256);
    assert!(stats.last_analyzed_epoch_ms.is_some());
}

#[test]
fn test_stats_collector_with_columns_and_indexes() {
    let mut collector = StatsCollector::new();
    collector.set_row_count(5_000);

    collector.add_column_stats(ColumnStats::new("category").with_distinct_count(20));
    collector.add_column_stats(ColumnStats::new("status").with_distinct_count(5));

    collector
        .add_index_stats(IndexStats::new("idx_category", "PropertyIndex").with_entry_count(5_000));

    let stats = collector.build();

    assert_eq!(stats.column_stats.len(), 2);
    assert_eq!(stats.index_stats.len(), 1);
    assert_eq!(
        stats.column_stats.get("category").unwrap().distinct_count,
        20
    );
}

#[test]
fn test_stats_serialization() {
    let mut stats = CollectionStats::with_counts(1000, 50);
    stats.column_stats.insert(
        "name".to_string(),
        ColumnStats::new("name").with_distinct_count(800),
    );
    stats.mark_analyzed();

    // Test JSON serialization
    let json = serde_json::to_string(&stats).expect("serialize");
    let deserialized: CollectionStats = serde_json::from_str(&json).expect("deserialize");

    assert_eq!(deserialized.row_count, 1000);
    assert_eq!(deserialized.deleted_count, 50);
    assert_eq!(
        deserialized
            .column_stats
            .get("name")
            .unwrap()
            .distinct_count,
        800
    );
}

// --- Histogram core method tests ---

/// Helper: builds a 3-bucket histogram for testing.
fn make_test_histogram() -> Histogram {
    Histogram {
        buckets: vec![
            HistogramBucket {
                lower_bound: 0.0,
                upper_bound: 10.0,
                count: 100,
                distinct_count: 10,
            },
            HistogramBucket {
                lower_bound: 10.0,
                upper_bound: 20.0,
                count: 200,
                distinct_count: 20,
            },
            HistogramBucket {
                lower_bound: 20.0,
                upper_bound: 30.0,
                count: 300,
                distinct_count: 15,
            },
        ],
        total_count: 600,
        incremental_updates: 0,
        stale: false,
    }
}

#[test]
fn test_find_bucket_in_range() {
    let h = make_test_histogram();
    assert_eq!(h.find_bucket(5.0), Some(0));
    assert_eq!(h.find_bucket(0.0), Some(0)); // lower_bound inclusive
    assert_eq!(h.find_bucket(10.0), Some(1)); // next bucket starts at 10.0
    assert_eq!(h.find_bucket(25.0), Some(2));
}

#[test]
fn test_find_bucket_outside_range() {
    let h = make_test_histogram();
    assert_eq!(h.find_bucket(-1.0), None);
    assert_eq!(h.find_bucket(30.0), None); // upper_bound exclusive
    assert_eq!(h.find_bucket(100.0), None);
}

#[test]
fn test_find_bucket_empty_histogram() {
    let h = Histogram::default();
    assert_eq!(h.find_bucket(5.0), None);
}

#[test]
fn test_eq_selectivity_in_bucket() {
    let h = make_test_histogram();
    // Bucket 0: count=100, distinct=10, total=600
    // Expected: 100 / (10 * 600) = 100/6000 ≈ 0.01667
    let sel = h.estimate_eq_selectivity(5.0);
    assert!((sel - 100.0 / 6000.0).abs() < 1e-10);
}

#[test]
fn test_eq_selectivity_outside_range() {
    let h = make_test_histogram();
    // Outside: 1/600 ≈ 0.001667
    let sel = h.estimate_eq_selectivity(50.0);
    assert!((sel - 1.0 / 600.0).abs() < 1e-10);
}

#[test]
fn test_eq_selectivity_zero_total() {
    let h = Histogram {
        total_count: 0,
        ..Default::default()
    };
    assert!((h.estimate_eq_selectivity(5.0) - 0.0).abs() < f64::EPSILON);
}

#[test]
fn test_eq_selectivity_zero_distinct() {
    let h = Histogram {
        buckets: vec![HistogramBucket {
            lower_bound: 0.0,
            upper_bound: 10.0,
            count: 50,
            distinct_count: 0,
        }],
        total_count: 50,
        ..Default::default()
    };
    let sel = h.estimate_eq_selectivity(5.0);
    assert!((sel - 1.0 / 50.0).abs() < 1e-10);
}

#[test]
fn test_lt_selectivity_below_first() {
    let h = make_test_histogram();
    assert!((h.estimate_lt_selectivity(-1.0) - 0.0).abs() < f64::EPSILON);
    assert!((h.estimate_lt_selectivity(0.0) - 0.0).abs() < f64::EPSILON);
}

#[test]
fn test_lt_selectivity_above_last() {
    let h = make_test_histogram();
    assert!((h.estimate_lt_selectivity(30.0) - 1.0).abs() < f64::EPSILON);
    assert!((h.estimate_lt_selectivity(100.0) - 1.0).abs() < f64::EPSILON);
}

#[test]
fn test_lt_selectivity_midpoint() {
    let h = make_test_histogram();
    // value=15.0 → bucket 0 fully below (100), bucket 1 partial: (15-10)/(20-10)=0.5 → 200*0.5=100
    // total below = 200, selectivity = 200/600 ≈ 0.3333
    let sel = h.estimate_lt_selectivity(15.0);
    assert!((sel - 200.0 / 600.0).abs() < 1e-10);
}

#[test]
fn test_lt_selectivity_empty() {
    let h = Histogram::default();
    assert!((h.estimate_lt_selectivity(5.0) - 0.0).abs() < f64::EPSILON);
}

#[test]
fn test_range_selectivity_full_range() {
    let h = make_test_histogram();
    // Range encompasses entire histogram
    assert!((h.estimate_range_selectivity(-10.0, 50.0) - 1.0).abs() < f64::EPSILON);
}

#[test]
fn test_range_selectivity_outside() {
    let h = make_test_histogram();
    assert!((h.estimate_range_selectivity(50.0, 60.0) - 0.0).abs() < f64::EPSILON);
    assert!((h.estimate_range_selectivity(-20.0, -10.0) - 0.0).abs() < f64::EPSILON);
}

#[test]
fn test_range_selectivity_inverted() {
    let h = make_test_histogram();
    assert!((h.estimate_range_selectivity(20.0, 10.0) - 0.0).abs() < f64::EPSILON);
}

#[test]
fn test_range_selectivity_partial() {
    let h = make_test_histogram();
    // [10.0, 20.0] = exactly bucket 1 → 200/600 ≈ 0.3333
    let sel = h.estimate_range_selectivity(10.0, 20.0);
    assert!((sel - 200.0 / 600.0).abs() < 1e-10);
}

#[test]
fn test_increment_bucket_updates_count() {
    let mut h = make_test_histogram();
    h.increment_bucket(5.0);
    assert_eq!(h.buckets[0].count, 101);
    assert_eq!(h.incremental_updates, 1);
    assert!(!h.stale);
}

#[test]
fn test_increment_bucket_outside_noop() {
    let mut h = make_test_histogram();
    h.increment_bucket(50.0);
    assert_eq!(h.buckets[0].count, 100);
    assert_eq!(h.incremental_updates, 0);
}

#[test]
fn test_decrement_bucket_updates_count() {
    let mut h = make_test_histogram();
    h.decrement_bucket(5.0);
    assert_eq!(h.buckets[0].count, 99);
    assert_eq!(h.incremental_updates, 1);
}

#[test]
fn test_decrement_bucket_floors_at_zero() {
    let mut h = Histogram {
        buckets: vec![HistogramBucket {
            lower_bound: 0.0,
            upper_bound: 10.0,
            count: 0,
            distinct_count: 1,
        }],
        total_count: 100,
        ..Default::default()
    };
    h.decrement_bucket(5.0);
    assert_eq!(h.buckets[0].count, 0);
    assert_eq!(h.incremental_updates, 1);
}

#[test]
fn test_staleness_threshold() {
    let mut h = Histogram {
        buckets: vec![HistogramBucket {
            lower_bound: 0.0,
            upper_bound: 100.0,
            count: 100,
            distinct_count: 50,
        }],
        total_count: 100,
        incremental_updates: 0,
        stale: false,
    };
    // 20% of 100 = 20. Need > 20 updates to trigger staleness.
    for _ in 0..20 {
        h.increment_bucket(50.0);
    }
    assert!(!h.stale);
    h.increment_bucket(50.0); // 21st update → > 100/5 = 20
    assert!(h.stale);
}

// --- HistogramBuilder tests ---

use histogram::HistogramBuilder;

#[test]
fn test_builder_empty_input() {
    let builder = HistogramBuilder::new(10);
    let h = builder.build(&mut []);
    assert!(h.buckets.is_empty());
    assert_eq!(h.total_count, 0);
    assert_eq!(h.incremental_updates, 0);
    assert!(!h.stale);
}

#[test]
fn test_builder_single_value() {
    let builder = HistogramBuilder::new(10);
    let mut values = [42.0, 42.0, 42.0, 42.0, 42.0];
    let h = builder.build(&mut values);
    assert_eq!(h.buckets.len(), 1);
    assert_eq!(h.buckets[0].count, 5);
    assert_eq!(h.buckets[0].distinct_count, 1);
    assert!((h.buckets[0].lower_bound - 42.0).abs() < f64::EPSILON);
    assert_eq!(h.total_count, 5);
}

#[test]
fn test_builder_all_nan() {
    let builder = HistogramBuilder::new(10);
    let mut values = [f64::NAN, f64::NAN, f64::NAN];
    let h = builder.build(&mut values);
    assert!(h.buckets.is_empty());
    assert_eq!(h.total_count, 0);
}

#[test]
fn test_builder_fewer_distinct_than_buckets() {
    let builder = HistogramBuilder::new(10);
    // 3 distinct values, 10 buckets → 3 buckets (one per distinct)
    let mut values = [1.0, 1.0, 2.0, 2.0, 2.0, 3.0];
    let h = builder.build(&mut values);
    assert_eq!(h.buckets.len(), 3);
    assert_eq!(h.buckets[0].count, 2); // two 1.0s
    assert_eq!(h.buckets[0].distinct_count, 1);
    assert_eq!(h.buckets[1].count, 3); // three 2.0s
    assert_eq!(h.buckets[1].distinct_count, 1);
    assert_eq!(h.buckets[2].count, 1); // one 3.0
    assert_eq!(h.buckets[2].distinct_count, 1);
    assert_eq!(h.total_count, 6);
}

#[test]
fn test_builder_normal_distribution() {
    let builder = HistogramBuilder::new(10);
    let mut values: Vec<f64> = (0..100).map(|i| i as f64).collect();
    let h = builder.build(&mut values);
    assert_eq!(h.buckets.len(), 10);
    // Each bucket should have ~10 values
    for bucket in &h.buckets {
        assert!(bucket.count == 10);
        assert!(bucket.distinct_count == 10);
    }
    assert_eq!(h.total_count, 100);
    assert!(!h.stale);
    assert_eq!(h.incremental_updates, 0);
}

#[test]
fn test_builder_total_count_matches() {
    let builder = HistogramBuilder::new(5);
    let mut values = [1.0, f64::NAN, 2.0, 3.0, f64::NAN, 4.0, 5.0, 6.0, 7.0];
    let h = builder.build(&mut values);
    // 7 non-NaN values
    assert_eq!(h.total_count, 7);
    let bucket_sum: u64 = h.buckets.iter().map(|b| b.count).sum();
    assert_eq!(bucket_sum, h.total_count);
}

#[test]
fn test_builder_default_buckets_on_zero() {
    let builder = HistogramBuilder::new(0);
    // Should default to 64 buckets
    let mut values: Vec<f64> = (0..1000).map(|i| i as f64).collect();
    let h = builder.build(&mut values);
    // With 1000 values and 64 buckets, we get ceil(1000/64)=16 per chunk → ~63 buckets
    assert!(h.buckets.len() <= 64);
    assert!(!h.buckets.is_empty());
    assert_eq!(h.total_count, 1000);
}

#[test]
fn test_builder_mixed_nan_and_values() {
    let builder = HistogramBuilder::new(4);
    let mut values = [f64::NAN, 3.0, 1.0, f64::NAN, 2.0, 4.0];
    let h = builder.build(&mut values);
    assert_eq!(h.total_count, 4);
    // 4 distinct values, 4 buckets → one per distinct
    assert_eq!(h.buckets.len(), 4);
    // Buckets should be sorted by lower_bound
    for i in 1..h.buckets.len() {
        assert!(h.buckets[i].lower_bound >= h.buckets[i - 1].lower_bound);
    }
}

// --- Task 8.1: Histogram persistence verification tests ---

#[test]
fn test_histogram_included_in_stats_json_serialization() {
    let mut stats = CollectionStats::with_counts(1000, 0);
    let histogram = Histogram {
        buckets: vec![
            HistogramBucket {
                lower_bound: 0.0,
                upper_bound: 50.0,
                count: 400,
                distinct_count: 40,
            },
            HistogramBucket {
                lower_bound: 50.0,
                upper_bound: 100.0,
                count: 600,
                distinct_count: 55,
            },
        ],
        total_count: 1000,
        incremental_updates: 42,
        stale: true,
    };
    let mut col = ColumnStats::new("price").with_distinct_count(95);
    col.histogram = Some(histogram.clone());
    stats.column_stats.insert("price".to_string(), col.clone());
    stats.field_stats.insert("price".to_string(), col);

    // Serialize to JSON (same path as Database::analyze_collection)
    let json = serde_json::to_vec_pretty(&stats).expect("serialize stats");
    let json_str = String::from_utf8_lossy(&json);

    // Verify histogram fields are present in the JSON output
    assert!(
        json_str.contains("\"histogram\""),
        "histogram field missing"
    );
    assert!(json_str.contains("\"total_count\""), "total_count missing");
    assert!(
        json_str.contains("\"incremental_updates\""),
        "incremental_updates missing"
    );
    assert!(json_str.contains("\"stale\""), "stale missing");
    assert!(
        json_str.contains("\"distinct_count\""),
        "distinct_count missing"
    );

    // Deserialize (same path as Database::get_collection_stats)
    let restored: CollectionStats = serde_json::from_slice(&json).expect("deserialize stats");
    let restored_hist = restored
        .column_stats
        .get("price")
        .and_then(|cs| cs.histogram.as_ref())
        .expect("histogram should be present after round-trip");

    assert_eq!(restored_hist, &histogram);
    assert_eq!(restored_hist.total_count, 1000);
    assert_eq!(restored_hist.incremental_updates, 42);
    assert!(restored_hist.stale);
    assert_eq!(restored_hist.buckets.len(), 2);
    assert_eq!(restored_hist.buckets[0].distinct_count, 40);
}

#[test]
fn test_pre_histogram_stats_json_backward_compat() {
    // Simulate a stats JSON from before histogram support was added.
    // No histogram field, no distinct_count on buckets, no total_count/stale.
    let old_json = r#"{
        "total_points": 500,
        "payload_size_bytes": 1024,
        "field_stats": {
            "age": {
                "name": "age",
                "null_count": 5,
                "distinct_count": 50,
                "distinct_values": 50,
                "min_value": null,
                "max_value": null,
                "avg_size_bytes": 8
            }
        },
        "row_count": 500,
        "deleted_count": 0,
        "avg_row_size_bytes": 64,
        "total_size_bytes": 32000,
        "column_stats": {},
        "index_stats": {}
    }"#;

    let stats: CollectionStats = serde_json::from_str(old_json)
        .expect("pre-histogram JSON should deserialize with defaults");

    assert_eq!(stats.row_count, 500);
    // histogram field should default to None
    let age_stats = stats
        .field_stats
        .get("age")
        .expect("age field should exist");
    assert!(
        age_stats.histogram.is_none(),
        "histogram should default to None"
    );
}

#[test]
fn test_histogram_bucket_serde_default_distinct_count() {
    // A bucket JSON without distinct_count should deserialize with default 0.
    let bucket_json = r#"{"lower_bound": 0.0, "upper_bound": 10.0, "count": 50}"#;
    let bucket: HistogramBucket = serde_json::from_str(bucket_json)
        .expect("bucket without distinct_count should deserialize");
    assert_eq!(bucket.distinct_count, 0);
    assert_eq!(bucket.count, 50);
}

#[test]
fn test_histogram_serde_default_metadata_fields() {
    // A histogram JSON without total_count/incremental_updates/stale should use defaults.
    let hist_json = r#"{
        "buckets": [
            {"lower_bound": 0.0, "upper_bound": 10.0, "count": 100}
        ]
    }"#;
    let hist: Histogram =
        serde_json::from_str(hist_json).expect("histogram without metadata should deserialize");
    assert_eq!(hist.total_count, 0);
    assert_eq!(hist.incremental_updates, 0);
    assert!(!hist.stale);
    assert_eq!(hist.buckets.len(), 1);
    assert_eq!(hist.buckets[0].distinct_count, 0);
}

// --- WP-2A: Zero-width bucket inflation regression tests ---

#[test]
fn test_no_zero_width_buckets_with_heavy_duplicates() {
    // Regression test: duplicate-heavy data used to produce zero-width buckets
    // whose counts inflated bucket_sum(), deflating selectivity estimates.
    let builder = HistogramBuilder::new(4);
    // 20 copies of 5.0 mixed with a few distinct values → forces chunk boundaries
    // inside the 5.0 run, previously creating zero-width buckets.
    let mut values = vec![5.0; 20];
    values.extend_from_slice(&[1.0, 2.0, 3.0, 10.0, 20.0]);
    let h = builder.build(&mut values);

    // Every bucket must have non-zero width.
    for (i, bucket) in h.buckets.iter().enumerate() {
        assert!(
            bucket.upper_bound > bucket.lower_bound,
            "bucket {i} is zero-width: lower={}, upper={}",
            bucket.lower_bound,
            bucket.upper_bound,
        );
    }

    // Total count across buckets must equal number of values.
    let bucket_sum: u64 = h.buckets.iter().map(|b| b.count).sum();
    assert_eq!(bucket_sum, 25, "bucket_sum must equal total input count");
    assert_eq!(h.total_count, 25);
}

#[test]
fn test_selectivity_not_inflated_by_duplicates() {
    // Build a histogram from data with heavy duplicates and verify that
    // bucket_sum() equals total_count (no inflation from zero-width ghosts).
    let builder = HistogramBuilder::new(4);
    let mut values: Vec<f64> = vec![1.0; 50];
    values.extend_from_slice(&[2.0, 3.0, 4.0, 5.0]);
    let h = builder.build(&mut values);

    // Core invariant: bucket_sum must equal total_count — no inflation.
    let bucket_sum: u64 = h.buckets.iter().map(|b| b.count).sum();
    assert_eq!(
        bucket_sum, h.total_count,
        "bucket_sum ({bucket_sum}) != total_count ({}): zero-width inflation detected",
        h.total_count
    );

    // No zero-width buckets should remain.
    for (i, bucket) in h.buckets.iter().enumerate() {
        assert!(
            bucket.upper_bound > bucket.lower_bound,
            "bucket {i} is zero-width after merge"
        );
    }

    // Selectivity for 1.0 must be findable (not silently lost in a zero-width bucket).
    let sel = h.estimate_eq_selectivity(1.0);
    assert!(
        sel > 0.0,
        "selectivity for dominant value should be > 0, got {sel}"
    );
}

#[test]
fn test_zero_width_merge_preserves_count() {
    // Directly test the merge helper: fabricate buckets including zero-width ones
    // and verify counts are preserved after merging.
    let buckets = vec![
        HistogramBucket {
            lower_bound: 0.0,
            upper_bound: 5.0,
            count: 10,
            distinct_count: 5,
        },
        // Zero-width: lower == upper == 5.0
        HistogramBucket {
            lower_bound: 5.0,
            upper_bound: 5.0,
            count: 20,
            distinct_count: 1,
        },
        HistogramBucket {
            lower_bound: 5.0,
            upper_bound: 10.0,
            count: 15,
            distinct_count: 5,
        },
    ];
    let merged = histogram::merge_zero_width_buckets(buckets);

    // The zero-width bucket should have been absorbed.
    assert_eq!(merged.len(), 2, "zero-width bucket should be merged");

    // First bucket: original non-zero-width (count=10), unchanged.
    assert_eq!(merged[0].count, 10);
    assert!((merged[0].lower_bound - 0.0).abs() < f64::EPSILON);
    assert!((merged[0].upper_bound - 5.0).abs() < f64::EPSILON);

    // Second bucket: original (count=15) + absorbed zero-width (count=20) = 35.
    // The zero-width bucket is merged forward into the next non-zero-width bucket.
    assert_eq!(merged[1].count, 35);
    assert!((merged[1].lower_bound - 5.0).abs() < f64::EPSILON);
    assert!((merged[1].upper_bound - 10.0).abs() < f64::EPSILON);

    // Total count preserved.
    let total: u64 = merged.iter().map(|b| b.count).sum();
    assert_eq!(total, 45);
}

#[test]
fn test_zero_width_merge_does_not_double_count_distinct() {
    // Regression test for BUG-4: when a zero-width bucket shares its single
    // distinct value with the absorbing bucket, distinct_count must not be
    // inflated by summing. Using max() prevents double-counting.
    let buckets = vec![
        // Zero-width bucket: all values are 5.0 (distinct_count=1).
        HistogramBucket {
            lower_bound: 5.0,
            upper_bound: 5.0,
            count: 30,
            distinct_count: 1,
        },
        // Absorbing bucket: range [5.0, 10.0) already contains 5.0 among
        // its 5 distinct values.
        HistogramBucket {
            lower_bound: 5.0,
            upper_bound: 10.0,
            count: 20,
            distinct_count: 5,
        },
    ];
    let merged = histogram::merge_zero_width_buckets(buckets);

    assert_eq!(merged.len(), 1, "both buckets should merge into one");
    assert_eq!(merged[0].count, 50, "row counts should be summed");
    // distinct_count must be max(1, 5) = 5, NOT 1 + 5 = 6.
    assert_eq!(
        merged[0].distinct_count, 5,
        "distinct_count should use max (not sum) to avoid double-counting"
    );
}

#[test]
fn test_zero_width_merge_trailing_does_not_double_count_distinct() {
    // Trailing zero-width buckets merged backward must also use max, not sum.
    let buckets = vec![
        HistogramBucket {
            lower_bound: 0.0,
            upper_bound: 10.0,
            count: 40,
            distinct_count: 8,
        },
        // Trailing zero-width bucket with distinct value in absorber's range.
        HistogramBucket {
            lower_bound: 10.0,
            upper_bound: 10.0,
            count: 10,
            distinct_count: 1,
        },
    ];
    let merged = histogram::merge_zero_width_buckets(buckets);

    assert_eq!(merged.len(), 1);
    assert_eq!(merged[0].count, 50);
    // max(8, 1) = 8, not 8 + 1 = 9.
    assert_eq!(
        merged[0].distinct_count, 8,
        "trailing zero-width merge should use max for distinct_count"
    );
}