heliosdb-nano 3.30.0

PostgreSQL-compatible embedded database with TDE + ZKE encryption, HNSW vector search, Product Quantization, git-like branching, time-travel queries, materialized views, row-level security, and 50+ enterprise features
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
//! Storage-Level Predicate Pushdown
//!
//! This module integrates bloom filters, zone maps, and SIMD filtering to enable
//! efficient predicate evaluation at the storage layer, minimizing data transfer
//! and tuple materialization.
//!
//! Architecture:
//! 1. Zone Maps - Skip entire blocks that can't match range predicates
//! 2. Bloom Filters - Skip blocks that definitely don't contain equality matches
//! 3. SIMD Filtering - Vectorized predicate evaluation for remaining data
//!
//! The pushdown optimizer analyzes predicates and determines the most efficient
//! filtering strategy based on predicate types and available indexes.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use parking_lot::RwLock;

use crate::sql::logical_plan::{LogicalExpr, BinaryOperator};
use crate::storage::bloom_filter::TableBloomFilters;
use crate::storage::zone_map::{TableZoneMap, RangeOp, ZoneMapStats};
use crate::storage::simd_filter::{
    SimdPredicateFilteringEngine, FilterPredicate, FilterOp, CombinedPredicate,
};
use crate::{Schema, Tuple, Value};

/// Predicate pushdown capabilities
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct PushdownCapabilities {
    /// Support equality pushdown via bloom filters
    pub bloom_filter: bool,
    /// Support range pushdown via zone maps
    pub zone_map: bool,
    /// Support SIMD-accelerated filtering
    pub simd_filter: bool,
    /// Support early termination with LIMIT
    pub early_termination: bool,
    /// Support projection pushdown (column pruning)
    pub projection_pushdown: bool,
}

impl Default for PushdownCapabilities {
    fn default() -> Self {
        Self {
            bloom_filter: true,
            zone_map: true,
            simd_filter: true,
            early_termination: true,
            projection_pushdown: true,
        }
    }
}

/// Configuration for predicate pushdown
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PushdownConfig {
    /// Enable/disable capabilities
    pub capabilities: PushdownCapabilities,
    /// Selectivity threshold for using bloom filters (0.0 - 1.0)
    /// Use bloom filter when estimated selectivity is below this threshold
    pub bloom_selectivity_threshold: f64,
    /// Minimum row count to use zone maps
    pub zone_map_min_rows: usize,
    /// Block size for zone maps
    pub zone_map_block_size: usize,
    /// Expected distinct values per column for bloom filters
    pub bloom_expected_distinct: usize,
    /// False positive rate for bloom filters
    pub bloom_fpr: f64,
}

impl Default for PushdownConfig {
    fn default() -> Self {
        Self {
            capabilities: PushdownCapabilities::default(),
            bloom_selectivity_threshold: 0.1, // Use bloom filter when < 10% selectivity expected
            zone_map_min_rows: 100,
            zone_map_block_size: 1000,
            bloom_expected_distinct: 1000,
            bloom_fpr: 0.01,
        }
    }
}

/// Statistics for pushdown operations
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct PushdownStats {
    /// Total scans performed
    pub total_scans: u64,
    /// Scans that used pushdown
    pub pushdown_scans: u64,
    /// Rows skipped by bloom filters
    pub bloom_rows_skipped: u64,
    /// Blocks skipped by zone maps
    pub zone_blocks_skipped: u64,
    /// Rows filtered by SIMD engine
    pub simd_rows_filtered: u64,
    /// Total rows scanned without pushdown
    pub baseline_rows: u64,
    /// Rows returned after all filtering
    pub returned_rows: u64,
    /// Early terminations (LIMIT reached)
    pub early_terminations: u64,
    /// Time saved estimate (microseconds)
    pub time_saved_micros: u64,
}

impl PushdownStats {
    /// Calculate overall efficiency (rows skipped / total rows)
    pub fn efficiency(&self) -> f64 {
        if self.baseline_rows == 0 {
            0.0
        } else {
            let skipped = self.bloom_rows_skipped + (self.zone_blocks_skipped * 1000); // Estimate
            skipped as f64 / self.baseline_rows as f64
        }
    }

    /// Calculate pushdown usage rate
    pub fn pushdown_rate(&self) -> f64 {
        if self.total_scans == 0 {
            0.0
        } else {
            self.pushdown_scans as f64 / self.total_scans as f64
        }
    }
}

/// Analyzed predicate ready for pushdown
#[derive(Debug, Clone)]
pub struct AnalyzedPredicate {
    /// Column name
    pub column_name: String,
    /// Column index in schema
    pub column_index: usize,
    /// Operation type
    pub op: PredicateOp,
    /// Primary value
    pub value: Value,
    /// Secondary value (for BETWEEN)
    pub value2: Option<Value>,
    /// List of values (for IN)
    pub value_list: Vec<Value>,
    /// Estimated selectivity
    pub selectivity: f64,
    /// Can use bloom filter
    pub can_use_bloom: bool,
    /// Can use zone map
    pub can_use_zone_map: bool,
}

/// Predicate operation type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PredicateOp {
    Eq,
    NotEq,
    Lt,
    LtEq,
    Gt,
    GtEq,
    IsNull,
    IsNotNull,
    Between,
    In,
    Like,
}

impl PredicateOp {
    /// Convert to zone map range operation
    pub fn to_range_op(self) -> Option<RangeOp> {
        match self {
            PredicateOp::Eq => Some(RangeOp::Eq),
            PredicateOp::NotEq => Some(RangeOp::NotEq),
            PredicateOp::Lt => Some(RangeOp::Lt),
            PredicateOp::LtEq => Some(RangeOp::LtEq),
            PredicateOp::Gt => Some(RangeOp::Gt),
            PredicateOp::GtEq => Some(RangeOp::GtEq),
            _ => None,
        }
    }

    /// Convert to SIMD filter operation
    pub fn to_filter_op(self) -> Option<FilterOp> {
        match self {
            PredicateOp::Eq => Some(FilterOp::Eq),
            PredicateOp::NotEq => Some(FilterOp::NotEq),
            PredicateOp::Lt => Some(FilterOp::Lt),
            PredicateOp::LtEq => Some(FilterOp::LtEq),
            PredicateOp::Gt => Some(FilterOp::Gt),
            PredicateOp::GtEq => Some(FilterOp::GtEq),
            PredicateOp::IsNull => Some(FilterOp::IsNull),
            PredicateOp::IsNotNull => Some(FilterOp::IsNotNull),
            PredicateOp::Between => Some(FilterOp::Between),
            PredicateOp::In => Some(FilterOp::In),
            PredicateOp::Like => Some(FilterOp::Like),
        }
    }
}

/// Storage-level predicate pushdown manager
pub struct PredicatePushdownManager {
    /// Configuration
    config: PushdownConfig,
    /// Per-table bloom filters
    bloom_filters: Arc<RwLock<HashMap<String, TableBloomFilters>>>,
    /// Per-table zone maps
    zone_maps: Arc<RwLock<HashMap<String, TableZoneMap>>>,
    /// SIMD filtering engine
    simd_engine: SimdPredicateFilteringEngine,
    /// Statistics
    stats: Arc<RwLock<PushdownStats>>,
}

impl PredicatePushdownManager {
    /// Create a new pushdown manager
    pub fn new(config: PushdownConfig) -> Self {
        Self {
            config,
            bloom_filters: Arc::new(RwLock::new(HashMap::new())),
            zone_maps: Arc::new(RwLock::new(HashMap::new())),
            simd_engine: SimdPredicateFilteringEngine::new(),
            stats: Arc::new(RwLock::new(PushdownStats::default())),
        }
    }

    /// Create with default configuration
    pub fn with_defaults() -> Self {
        Self::new(PushdownConfig::default())
    }

    /// Initialize indexes for a table
    pub fn initialize_table(
        &self,
        table_name: &str,
        columns: &[String],
        expected_rows: usize,
    ) {
        // Create bloom filters for columns
        let mut tbf = TableBloomFilters::new(table_name.to_string(), expected_rows);
        for col in columns {
            tbf.add_column(col.clone(), self.config.bloom_expected_distinct);
        }
        self.bloom_filters.write().insert(table_name.to_string(), tbf);

        // Create zone map
        let tzm = TableZoneMap::new(table_name.to_string(), self.config.zone_map_block_size);
        self.zone_maps.write().insert(table_name.to_string(), tzm);
    }

    /// Index a row (call when inserting/updating)
    pub fn index_row(&self, table_name: &str, row_id: u64, values: &[(String, Value)]) {
        // Update bloom filters
        if let Some(tbf) = self.bloom_filters.write().get_mut(table_name) {
            tbf.index_row(row_id, values);
        }

        // Update zone map
        if let Some(tzm) = self.zone_maps.write().get_mut(table_name) {
            tzm.add_row(row_id, values);
        }
    }

    /// Analyze a logical expression to extract pushable predicates
    pub fn analyze_predicate(
        &self,
        expr: &LogicalExpr,
        schema: &Schema,
    ) -> Vec<AnalyzedPredicate> {
        let mut predicates = Vec::new();
        self.extract_predicates(expr, schema, &mut predicates);
        predicates
    }

    fn extract_predicates(
        &self,
        expr: &LogicalExpr,
        schema: &Schema,
        predicates: &mut Vec<AnalyzedPredicate>,
    ) {
        match expr {
            LogicalExpr::BinaryExpr { left, op, right } => {
                match op {
                    BinaryOperator::And => {
                        // Recurse into AND branches
                        self.extract_predicates(left, schema, predicates);
                        self.extract_predicates(right, schema, predicates);
                    }
                    BinaryOperator::Or => {
                        // OR predicates are harder to push down
                        // For now, we don't extract them
                    }
                    BinaryOperator::Eq | BinaryOperator::NotEq |
                    BinaryOperator::Lt | BinaryOperator::LtEq |
                    BinaryOperator::Gt | BinaryOperator::GtEq => {
                        // Try to extract column = value predicates
                        if let Some(pred) = self.extract_comparison(left, right, op, schema) {
                            predicates.push(pred);
                        } else if let Some(pred) = self.extract_comparison(right, left, op, schema) {
                            predicates.push(pred);
                        }
                    }
                    BinaryOperator::Like => {
                        if let Some(pred) = self.extract_like(left, right, schema) {
                            predicates.push(pred);
                        }
                    }
                    _ => {}
                }
            }
            LogicalExpr::IsNull { expr, is_null } => {
                if let LogicalExpr::Column { name, .. } = expr.as_ref() {
                    if let Some(col_idx) = schema.get_column_index(name) {
                        predicates.push(AnalyzedPredicate {
                            column_name: name.clone(),
                            column_index: col_idx,
                            op: if *is_null { PredicateOp::IsNull } else { PredicateOp::IsNotNull },
                            value: Value::Null,
                            value2: None,
                            value_list: Vec::new(),
                            selectivity: 0.1, // Estimate
                            can_use_bloom: false,
                            can_use_zone_map: true,
                        });
                    }
                }
            }
            LogicalExpr::Between { expr, low, high, negated } => {
                if let LogicalExpr::Column { name, .. } = expr.as_ref() {
                    if let (LogicalExpr::Literal(low_val), LogicalExpr::Literal(high_val)) =
                        (low.as_ref(), high.as_ref())
                    {
                        if let Some(col_idx) = schema.get_column_index(name) {
                            predicates.push(AnalyzedPredicate {
                                column_name: name.clone(),
                                column_index: col_idx,
                                op: if *negated { PredicateOp::NotEq } else { PredicateOp::Between },
                                value: low_val.clone(),
                                value2: Some(high_val.clone()),
                                value_list: Vec::new(),
                                selectivity: 0.2, // Estimate
                                can_use_bloom: false,
                                can_use_zone_map: true,
                            });
                        }
                    }
                }
            }
            LogicalExpr::InList { expr, list, negated } => {
                if let LogicalExpr::Column { name, .. } = expr.as_ref() {
                    let values: Vec<Value> = list
                        .iter()
                        .filter_map(|e| {
                            if let LogicalExpr::Literal(v) = e {
                                Some(v.clone())
                            } else {
                                None
                            }
                        })
                        .collect();

                    if !values.is_empty() {
                        if let Some(col_idx) = schema.get_column_index(name) {
                            predicates.push(AnalyzedPredicate {
                                column_name: name.clone(),
                                column_index: col_idx,
                                op: if *negated { PredicateOp::NotEq } else { PredicateOp::In },
                                value: values.first().cloned().unwrap_or(Value::Null),
                                value2: None,
                                value_list: values,
                                selectivity: 0.1, // Estimate
                                can_use_bloom: !*negated,
                                can_use_zone_map: false,
                            });
                        }
                    }
                }
            }
            _ => {}
        }
    }

    fn extract_comparison(
        &self,
        left: &LogicalExpr,
        right: &LogicalExpr,
        op: &BinaryOperator,
        schema: &Schema,
    ) -> Option<AnalyzedPredicate> {
        let (column_name, column_index) = match left {
            LogicalExpr::Column { name, .. } => {
                let idx = schema.get_column_index(name)?;
                (name.clone(), idx)
            }
            _ => return None,
        };

        let value = match right {
            LogicalExpr::Literal(v) => v.clone(),
            _ => return None,
        };

        let pred_op = match op {
            BinaryOperator::Eq => PredicateOp::Eq,
            BinaryOperator::NotEq => PredicateOp::NotEq,
            BinaryOperator::Lt => PredicateOp::Lt,
            BinaryOperator::LtEq => PredicateOp::LtEq,
            BinaryOperator::Gt => PredicateOp::Gt,
            BinaryOperator::GtEq => PredicateOp::GtEq,
            _ => return None,
        };

        Some(AnalyzedPredicate {
            column_name,
            column_index,
            op: pred_op,
            value,
            value2: None,
            value_list: Vec::new(),
            selectivity: match pred_op {
                PredicateOp::Eq => 0.01,  // Very selective
                PredicateOp::NotEq => 0.99,
                _ => 0.33, // Range predicates
            },
            can_use_bloom: pred_op == PredicateOp::Eq,
            can_use_zone_map: matches!(pred_op, PredicateOp::Eq | PredicateOp::Lt |
                PredicateOp::LtEq | PredicateOp::Gt | PredicateOp::GtEq),
        })
    }

    fn extract_like(
        &self,
        left: &LogicalExpr,
        right: &LogicalExpr,
        schema: &Schema,
    ) -> Option<AnalyzedPredicate> {
        let (column_name, column_index) = match left {
            LogicalExpr::Column { name, .. } => {
                let idx = schema.get_column_index(name)?;
                (name.clone(), idx)
            }
            _ => return None,
        };

        let pattern = match right {
            LogicalExpr::Literal(Value::String(s)) => s.clone(),
            _ => return None,
        };

        Some(AnalyzedPredicate {
            column_name,
            column_index,
            op: PredicateOp::Like,
            value: Value::String(pattern),
            value2: None,
            value_list: Vec::new(),
            selectivity: 0.1, // Estimate
            can_use_bloom: false,
            can_use_zone_map: false,
        })
    }

    /// Execute a filtered scan with pushdown
    pub fn scan_with_pushdown(
        &self,
        table_name: &str,
        tuples: Vec<Tuple>,
        predicates: &[AnalyzedPredicate],
        schema: &Schema,
        limit: Option<usize>,
    ) -> Vec<Tuple> {
        let mut stats = self.stats.write();
        stats.total_scans += 1;
        stats.baseline_rows += tuples.len() as u64;

        if predicates.is_empty() || !self.config.capabilities.simd_filter {
            // No pushdown - return all tuples (or apply limit)
            return match limit {
                Some(l) => tuples.into_iter().take(l).collect(),
                None => tuples,
            };
        }

        stats.pushdown_scans += 1;

        // Phase 1: Bloom filter pre-filtering (for equality predicates)
        let mut candidate_indices: Vec<usize> = (0..tuples.len()).collect();

        if self.config.capabilities.bloom_filter {
            if let Some(tbf) = self.bloom_filters.write().get_mut(table_name) {
                for pred in predicates.iter().filter(|p| p.can_use_bloom) {
                    if pred.op == PredicateOp::Eq {
                        // Check bloom filter - if definitely not present, skip
                        if !tbf.might_contain_value(&pred.column_name, &pred.value) {
                            stats.bloom_rows_skipped += tuples.len() as u64;
                            stats.returned_rows += 0;
                            return Vec::new();
                        }
                    } else if pred.op == PredicateOp::In {
                        // Check if any value in the IN list might exist
                        let might_exist = pred.value_list.iter()
                            .any(|v| tbf.might_contain_value(&pred.column_name, v));
                        if !might_exist {
                            stats.bloom_rows_skipped += tuples.len() as u64;
                            stats.returned_rows += 0;
                            return Vec::new();
                        }
                    }
                }
            }
        }

        // Phase 2: Zone map block skipping (for range predicates)
        if self.config.capabilities.zone_map && tuples.len() >= self.config.zone_map_min_rows {
            if let Some(tzm) = self.zone_maps.write().get_mut(table_name) {
                for pred in predicates.iter().filter(|p| p.can_use_zone_map) {
                    if let Some(range_op) = pred.op.to_range_op() {
                        let matching_blocks = if pred.op == PredicateOp::Between {
                            if let Some(high) = &pred.value2 {
                                tzm.get_matching_blocks_between(&pred.column_name, &pred.value, high)
                            } else {
                                continue;
                            }
                        } else {
                            tzm.get_matching_blocks_range(&pred.column_name, range_op, &pred.value)
                        };

                        // Filter candidate indices to only matching blocks
                        let block_size = self.config.zone_map_block_size;
                        let matching_set: std::collections::HashSet<u64> =
                            matching_blocks.into_iter().collect();

                        let before_count = candidate_indices.len();
                        candidate_indices.retain(|&idx| {
                            let block_id = (idx / block_size) as u64;
                            matching_set.contains(&block_id)
                        });
                        let skipped = before_count - candidate_indices.len();
                        stats.zone_blocks_skipped += (skipped / block_size) as u64;
                    }
                }
            }
        }

        // Phase 3: SIMD predicate filtering
        let filter_predicates: Vec<FilterPredicate> = predicates
            .iter()
            .filter_map(|p| {
                let filter_op = p.op.to_filter_op()?;
                Some(FilterPredicate {
                    column_index: p.column_index,
                    column_name: p.column_name.clone(),
                    op: filter_op,
                    value: p.value.clone(),
                    value2: p.value2.clone(),
                    value_list: p.value_list.clone(),
                    pattern: match &p.value {
                        Value::String(s) if p.op == PredicateOp::Like => Some(s.clone()),
                        _ => None,
                    },
                })
            })
            .collect();

        // Build candidate rows
        let candidate_rows: Vec<Vec<Value>> = candidate_indices
            .iter()
            .filter_map(|&idx| tuples.get(idx).map(|t| t.values.to_vec()))
            .collect();

        // Apply SIMD filtering
        let result = if let Some(lim) = limit {
            let combined = if filter_predicates.len() == 1 {
                // Length is 1, so next() will succeed; use map_or for safety
                filter_predicates.into_iter().next().map_or_else(
                    || CombinedPredicate::And(vec![]),
                    CombinedPredicate::Single
                )
            } else {
                CombinedPredicate::And(
                    filter_predicates.into_iter()
                        .map(CombinedPredicate::Single)
                        .collect()
                )
            };
            self.simd_engine.filter_rows_with_limit(&candidate_rows, &combined, lim)
        } else {
            self.simd_engine.filter_and_predicates(&candidate_rows, &filter_predicates)
        };

        stats.simd_rows_filtered += result.total_count as u64 - result.matched_count as u64;

        // Collect matching tuples
        let matching_tuples: Vec<Tuple> = result.matched_indices
            .iter()
            .filter_map(|&idx| {
                candidate_indices.get(idx).and_then(|&ci| tuples.get(ci)).cloned()
            })
            .collect();

        if result.matched_count < result.total_count && limit.is_some() {
            stats.early_terminations += 1;
        }

        stats.returned_rows += matching_tuples.len() as u64;
        matching_tuples
    }

    /// Get pushdown statistics
    pub fn get_stats(&self) -> PushdownStats {
        self.stats.read().clone()
    }

    /// Reset statistics
    pub fn reset_stats(&self) {
        *self.stats.write() = PushdownStats::default();
    }

    /// Get configuration
    pub fn config(&self) -> &PushdownConfig {
        &self.config
    }

    /// Update configuration
    pub fn set_config(&mut self, config: PushdownConfig) {
        self.config = config;
    }

    /// Get zone map statistics for a table
    pub fn get_zone_map_stats(&self, table_name: &str) -> Option<ZoneMapStats> {
        self.zone_maps.read().get(table_name).map(|zm| zm.stats().clone())
    }

    /// Get bloom filter memory usage
    pub fn bloom_filter_memory_usage(&self) -> usize {
        self.bloom_filters.read().values().map(|bf| bf.memory_usage()).sum()
    }

    /// Get zone map memory usage
    pub fn zone_map_memory_usage(&self) -> usize {
        self.zone_maps.read().values().map(|zm| zm.memory_usage()).sum()
    }

    /// Total memory usage
    pub fn total_memory_usage(&self) -> usize {
        self.bloom_filter_memory_usage() + self.zone_map_memory_usage()
    }

    /// Register bloom filters for a table
    pub fn register_bloom_filters(&self, table_name: String, filters: TableBloomFilters) {
        self.bloom_filters.write().insert(table_name, filters);
    }

    /// Register zone maps for a table
    pub fn register_zone_maps(&self, table_name: String, zone_map: TableZoneMap) {
        self.zone_maps.write().insert(table_name, zone_map);
    }

    /// Remove bloom filters and zone maps for a dropped table
    pub fn remove_table(&self, table_name: &str) {
        self.bloom_filters.write().remove(table_name);
        self.zone_maps.write().remove(table_name);
    }
}

impl Default for PredicatePushdownManager {
    fn default() -> Self {
        Self::with_defaults()
    }
}

/// Result of pushdown analysis
#[derive(Debug, Clone)]
pub struct PushdownAnalysis {
    /// Predicates that can be pushed down
    pub pushable_predicates: Vec<AnalyzedPredicate>,
    /// Predicates that must remain at executor level
    pub remaining_predicates: Vec<LogicalExpr>,
    /// Estimated selectivity after pushdown
    pub estimated_selectivity: f64,
    /// Recommended limit pushdown
    pub limit_pushdown: Option<usize>,
}

/// Analyze a filter expression for pushdown opportunities
pub fn analyze_for_pushdown(
    expr: &LogicalExpr,
    schema: &Schema,
) -> PushdownAnalysis {
    let manager = PredicatePushdownManager::with_defaults();
    let predicates = manager.analyze_predicate(expr, schema);

    let estimated_selectivity = predicates
        .iter()
        .map(|p| p.selectivity)
        .product::<f64>()
        .max(0.001); // Minimum selectivity

    PushdownAnalysis {
        pushable_predicates: predicates,
        remaining_predicates: Vec::new(), // For complex expressions not yet supported
        estimated_selectivity,
        limit_pushdown: None,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{Column, DataType};

    fn create_test_schema() -> Schema {
        Schema::new(vec![
            Column::new("id", DataType::Int8),
            Column::new("name", DataType::Text),
            Column::new("age", DataType::Int4),
            Column::new("status", DataType::Text),
        ])
    }

    #[test]
    fn test_predicate_analysis() {
        let schema = create_test_schema();
        let manager = PredicatePushdownManager::with_defaults();

        // Test simple equality
        let expr = LogicalExpr::BinaryExpr {
            left: Box::new(LogicalExpr::Column { table: None, name: "id".to_string()  }),
            op: BinaryOperator::Eq,
            right: Box::new(LogicalExpr::Literal(Value::Int8(42))),
        };

        let predicates = manager.analyze_predicate(&expr, &schema);
        assert_eq!(predicates.len(), 1);
        assert_eq!(predicates[0].column_name, "id");
        assert_eq!(predicates[0].op, PredicateOp::Eq);
        assert!(predicates[0].can_use_bloom);
    }

    #[test]
    fn test_and_predicates() {
        let schema = create_test_schema();
        let manager = PredicatePushdownManager::with_defaults();

        let expr = LogicalExpr::BinaryExpr {
            left: Box::new(LogicalExpr::BinaryExpr {
                left: Box::new(LogicalExpr::Column { table: None, name: "age".to_string()  }),
                op: BinaryOperator::GtEq,
                right: Box::new(LogicalExpr::Literal(Value::Int4(18))),
            }),
            op: BinaryOperator::And,
            right: Box::new(LogicalExpr::BinaryExpr {
                left: Box::new(LogicalExpr::Column { table: None, name: "status".to_string()  }),
                op: BinaryOperator::Eq,
                right: Box::new(LogicalExpr::Literal(Value::String("active".to_string()))),
            }),
        };

        let predicates = manager.analyze_predicate(&expr, &schema);
        assert_eq!(predicates.len(), 2);
    }

    #[test]
    fn test_pushdown_manager_initialization() {
        let manager = PredicatePushdownManager::with_defaults();
        manager.initialize_table("users", &["id".to_string(), "name".to_string()], 1000);

        // Index some rows
        manager.index_row("users", 1, &[
            ("id".to_string(), Value::Int8(1)),
            ("name".to_string(), Value::String("Alice".to_string())),
        ]);
        manager.index_row("users", 2, &[
            ("id".to_string(), Value::Int8(2)),
            ("name".to_string(), Value::String("Bob".to_string())),
        ]);

        assert!(manager.bloom_filter_memory_usage() > 0);
    }

    #[test]
    fn test_scan_with_pushdown() {
        let schema = Arc::new(create_test_schema());
        let manager = PredicatePushdownManager::with_defaults();
        manager.initialize_table("test", &["id".to_string(), "status".to_string()], 100);

        // Create test tuples
        let tuples: Vec<Tuple> = (0..10)
            .map(|i| {
                Tuple::new(vec![
                    Value::Int8(i),
                    Value::String(format!("name_{}", i)),
                    Value::Int4((i * 5 + 20) as i32),
                    Value::String(if i % 2 == 0 { "active" } else { "inactive" }.to_string()),
                ])
            })
            .collect();

        // Index tuples
        for (idx, t) in tuples.iter().enumerate() {
            manager.index_row("test", idx as u64, &[
                ("id".to_string(), t.values[0].clone()),
                ("status".to_string(), t.values[3].clone()),
            ]);
        }

        // Create filter predicate
        let pred = AnalyzedPredicate {
            column_name: "status".to_string(),
            column_index: 3,
            op: PredicateOp::Eq,
            value: Value::String("active".to_string()),
            value2: None,
            value_list: Vec::new(),
            selectivity: 0.5,
            can_use_bloom: true,
            can_use_zone_map: true,
        };

        let result = manager.scan_with_pushdown("test", tuples, &[pred], &schema, None);
        assert_eq!(result.len(), 5); // 0, 2, 4, 6, 8 are active
    }
}