heliosdb-nano 3.23.2

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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
//! Incremental Computation for Materialized Views
//!
//! This module implements efficient delta-based updates for materialized views
//! instead of full recomputation, significantly improving refresh performance.
//!
//! ## Features
//!
//! - **Incremental Aggregates**: Efficiently update COUNT, SUM, AVG, MIN, MAX
//! - **Incremental Joins**: Apply delta changes to join results
//! - **Incremental Filters**: Update filtered views based on changes
//! - **Cost Estimation**: Intelligently choose between incremental and full refresh
//! - **Correctness Validation**: Ensure incremental results match full refresh
//!
//! ## Architecture
//!
//! The incremental refresher tracks changes (deltas) to base tables and applies
//! them to materialized views using specialized algorithms for each operation type:
//!
//! - **Filter/Project**: Direct delta application with predicate evaluation
//! - **Aggregate**: Running aggregates with insert/delete/update handlers
//! - **Join**: Matching delta tuples against join partners
//! - **Hybrid**: Automatic selection between incremental and full refresh
//!
//! ## Example
//!
//! ```rust,ignore
//! let refresher = IncrementalRefresher::new(storage, delta_tracker);
//!
//! // Estimate cost before refresh
//! let cost = refresher.estimate_refresh_cost(&mv_def)?;
//! if matches!(cost.recommendation, RefreshStrategy::Incremental) {
//!     // Use incremental refresh
//!     let result = refresher.refresh_incremental(&mv_def.name)?;
//!     println!("Updated {} rows in {:?}", result.rows_updated, result.duration);
//! }
//! ```

#![allow(unused_variables)]
#![allow(unreachable_patterns)]

use crate::{Result, Error, Tuple, Value, Schema};
use crate::sql::{LogicalPlan, LogicalExpr, AggregateFunction, BinaryOperator};
use crate::storage::{StorageEngine, MaterializedViewMetadata};
use std::sync::Arc;
use std::time::{Duration, Instant};
use std::collections::HashMap;
use serde::{Serialize, Deserialize};

/// Refresh strategy for materialized views
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum RefreshStrategy {
    /// Full recomputation from base tables
    Full,
    /// Delta-based incremental update
    Incremental,
    /// Hybrid: incremental if possible, else full
    Hybrid,
}

/// Result of a materialized view refresh operation
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RefreshResult {
    /// Strategy used for this refresh
    pub strategy_used: RefreshStrategy,
    /// Number of rows inserted during refresh
    pub rows_inserted: usize,
    /// Number of rows updated during refresh
    pub rows_updated: usize,
    /// Number of rows deleted during refresh
    pub rows_deleted: usize,
    /// Duration of the refresh operation
    pub duration: Duration,
}

/// Cost estimation for refresh strategies
#[derive(Debug, Clone)]
pub struct RefreshCost {
    /// Estimated time for incremental refresh (seconds)
    pub incremental_cost: f64,
    /// Estimated time for full refresh (seconds)
    pub full_cost: f64,
    /// Recommended strategy based on cost analysis
    pub recommendation: RefreshStrategy,
}

/// Delta operation type
#[derive(Debug, Clone, PartialEq)]
pub enum DeltaOperation {
    /// Insert a new tuple
    Insert { tuple: Tuple },
    /// Delete an existing tuple
    Delete { tuple: Tuple },
    /// Update a tuple (old -> new)
    Update { old_tuple: Tuple, new_tuple: Tuple },
}

/// Delta record for a single change
#[derive(Debug, Clone)]
pub struct Delta {
    /// Operation type
    pub operation: DeltaOperation,
    /// Timestamp of the change
    pub timestamp: u64,
}

/// Set of deltas for a table
#[derive(Debug, Clone)]
pub struct DeltaSet {
    /// Table name
    pub table_name: String,
    /// List of deltas
    pub deltas: Vec<Delta>,
}

/// Delta tracker for capturing base table changes
pub struct DeltaTracker {
    /// Storage engine reference
    storage: Arc<StorageEngine>,
    /// Captured deltas per table
    deltas: parking_lot::RwLock<HashMap<String, Vec<Delta>>>,
}

impl DeltaTracker {
    /// Create a new delta tracker
    pub fn new(storage: Arc<StorageEngine>) -> Self {
        Self {
            storage,
            deltas: parking_lot::RwLock::new(HashMap::new()),
        }
    }

    /// Record an insert operation
    pub fn record_insert(&self, table_name: &str, tuple: Tuple, timestamp: u64) {
        let mut deltas = self.deltas.write();
        deltas.entry(table_name.to_string())
            .or_insert_with(Vec::new)
            .push(Delta {
                operation: DeltaOperation::Insert { tuple },
                timestamp,
            });
    }

    /// Record a delete operation
    pub fn record_delete(&self, table_name: &str, tuple: Tuple, timestamp: u64) {
        let mut deltas = self.deltas.write();
        deltas.entry(table_name.to_string())
            .or_insert_with(Vec::new)
            .push(Delta {
                operation: DeltaOperation::Delete { tuple },
                timestamp,
            });
    }

    /// Record an update operation
    pub fn record_update(&self, table_name: &str, old_tuple: Tuple, new_tuple: Tuple, timestamp: u64) {
        let mut deltas = self.deltas.write();
        deltas.entry(table_name.to_string())
            .or_insert_with(Vec::new)
            .push(Delta {
                operation: DeltaOperation::Update { old_tuple, new_tuple },
                timestamp,
            });
    }

    /// Get deltas for a table since a specific timestamp
    pub fn get_deltas_since(&self, table_name: &str, since: u64) -> Vec<Delta> {
        let deltas = self.deltas.read();
        if let Some(table_deltas) = deltas.get(table_name) {
            table_deltas.iter()
                .filter(|d| d.timestamp > since)
                .cloned()
                .collect()
        } else {
            Vec::new()
        }
    }

    /// Count deltas for tables since a specific timestamp
    pub fn count_deltas_since(&self, table_names: &[String], since: u64) -> Result<usize> {
        let deltas = self.deltas.read();
        let mut count = 0;
        for table_name in table_names {
            if let Some(table_deltas) = deltas.get(table_name) {
                count += table_deltas.iter()
                    .filter(|d| d.timestamp > since)
                    .count();
            }
        }
        Ok(count)
    }

    /// Clear deltas for a table up to a specific timestamp
    pub fn clear_deltas_until(&self, table_name: &str, until: u64) {
        let mut deltas = self.deltas.write();
        if let Some(table_deltas) = deltas.get_mut(table_name) {
            table_deltas.retain(|d| d.timestamp > until);
        }
    }

    /// Clear all deltas for a table
    pub fn clear_all_deltas(&self, table_name: &str) {
        let mut deltas = self.deltas.write();
        deltas.remove(table_name);
    }
}

/// Incremental refresher for materialized views
pub struct IncrementalRefresher {
    /// Storage engine reference
    storage: Arc<StorageEngine>,
    /// Delta tracker
    delta_tracker: Arc<DeltaTracker>,
}

impl IncrementalRefresher {
    /// Create a new incremental refresher
    pub fn new(storage: Arc<StorageEngine>, delta_tracker: Arc<DeltaTracker>) -> Self {
        Self {
            storage,
            delta_tracker,
        }
    }

    /// Refresh a materialized view incrementally
    ///
    /// Uses delta-based updates to efficiently refresh the view without full recomputation.
    pub fn refresh_incremental(&self, mv_name: &str) -> Result<RefreshResult> {
        let start = Instant::now();

        // Get materialized view metadata
        let mv_catalog = self.storage.mv_catalog();
        let mv_metadata = mv_catalog.get_view(mv_name)?;

        // Deserialize query plan
        let query_plan = mv_metadata.get_query_plan()?;

        // Check if incremental refresh is possible
        if !self.can_refresh_incrementally(&mv_metadata)? {
            return Err(Error::query_execution(format!(
                "Materialized view '{}' does not support incremental refresh",
                mv_name
            )));
        }

        // Get deltas for base tables
        let last_refresh = mv_metadata.last_refresh
            .ok_or_else(|| Error::query_execution("View has never been refreshed"))?
            .timestamp() as u64;

        let mut total_inserted = 0;
        let mut total_updated = 0;
        let mut total_deleted = 0;

        // Apply incremental refresh based on query type
        match &query_plan {
            LogicalPlan::Aggregate { input, group_by, aggr_exprs, .. } => {
                // Extract base table from input
                if let LogicalPlan::Scan { table_name, .. } = input.as_ref() {
                    let deltas = self.delta_tracker.get_deltas_since(table_name, last_refresh);
                    let delta_set = DeltaSet {
                        table_name: table_name.clone(),
                        deltas,
                    };

                    let (inserted, updated, deleted) = self.refresh_aggregate_incremental(
                        mv_name,
                        &mv_metadata,
                        &delta_set,
                        group_by,
                        aggr_exprs,
                    )?;

                    total_inserted += inserted;
                    total_updated += updated;
                    total_deleted += deleted;
                }
            }

            LogicalPlan::Filter { input, predicate } => {
                if let LogicalPlan::Scan { table_name, .. } = input.as_ref() {
                    let deltas = self.delta_tracker.get_deltas_since(table_name, last_refresh);
                    let delta_set = DeltaSet {
                        table_name: table_name.clone(),
                        deltas,
                    };

                    let (inserted, updated, deleted) = self.refresh_filter_incremental(
                        mv_name,
                        &mv_metadata,
                        &delta_set,
                        predicate,
                    )?;

                    total_inserted += inserted;
                    total_updated += updated;
                    total_deleted += deleted;
                }
            }

            LogicalPlan::Join { left, right, join_type, on, .. } => {
                // Get base tables from left and right
                let left_table = self.extract_table_name(left)?;
                let right_table = self.extract_table_name(right)?;

                let left_deltas = DeltaSet {
                    table_name: left_table.clone(),
                    deltas: self.delta_tracker.get_deltas_since(&left_table, last_refresh),
                };
                let right_deltas = DeltaSet {
                    table_name: right_table.clone(),
                    deltas: self.delta_tracker.get_deltas_since(&right_table, last_refresh),
                };

                let (inserted, updated, deleted) = self.refresh_join_incremental(
                    mv_name,
                    &mv_metadata,
                    vec![left_deltas, right_deltas],
                    join_type,
                    on,
                )?;

                total_inserted += inserted;
                total_updated += updated;
                total_deleted += deleted;
            }

            _ => {
                return Err(Error::query_execution(format!(
                    "Unsupported query plan for incremental refresh: {:?}",
                    query_plan
                )));
            }
        }

        let duration = start.elapsed();

        Ok(RefreshResult {
            strategy_used: RefreshStrategy::Incremental,
            rows_inserted: total_inserted,
            rows_updated: total_updated,
            rows_deleted: total_deleted,
            duration,
        })
    }

    /// Check if a materialized view can be refreshed incrementally
    pub fn can_refresh_incrementally(&self, mv_metadata: &MaterializedViewMetadata) -> Result<bool> {
        // Check if view has been refreshed at least once
        if mv_metadata.last_refresh.is_none() {
            return Ok(false);
        }

        // Deserialize query plan
        let query_plan = mv_metadata.get_query_plan()?;

        // Check if query plan supports incremental refresh
        match query_plan {
            LogicalPlan::Aggregate { .. } => Ok(true),
            LogicalPlan::Filter { .. } => Ok(true),
            LogicalPlan::Project { .. } => Ok(true),
            LogicalPlan::Join { .. } => Ok(true),
            _ => Ok(false),
        }
    }

    /// Estimate the cost of incremental vs full refresh
    pub fn estimate_refresh_cost(&self, mv_metadata: &MaterializedViewMetadata) -> Result<RefreshCost> {
        // Get delta count
        let last_refresh = mv_metadata.last_refresh
            .map(|dt| dt.timestamp() as u64)
            .unwrap_or(0);

        let delta_count = self.delta_tracker.count_deltas_since(&mv_metadata.base_tables, last_refresh)?;

        // Get MV size
        let mv_data_table = format!("__mv_{}", mv_metadata.view_name);
        let mv_size = self.count_tuples(&mv_data_table)?;

        // Get base table size
        let base_size = if let Some(first_table) = mv_metadata.base_tables.first() {
            self.count_tuples(first_table)?
        } else {
            0
        };

        // Estimate costs (heuristic)
        // Incremental: 1ms per delta operation
        let incremental_cost = (delta_count as f64) * 0.001;

        // Full: 10ms per base row (includes aggregation/join overhead)
        let full_cost = (base_size as f64) * 0.01;

        // Recommend incremental if less than 50% of full cost
        let recommendation = if incremental_cost < full_cost * 0.5 {
            RefreshStrategy::Incremental
        } else {
            RefreshStrategy::Full
        };

        Ok(RefreshCost {
            incremental_cost,
            full_cost,
            recommendation,
        })
    }

    // === Private Helper Methods ===

    /// Refresh aggregate materialized view incrementally
    fn refresh_aggregate_incremental(
        &self,
        mv_name: &str,
        _mv_metadata: &MaterializedViewMetadata,
        delta_set: &DeltaSet,
        group_by: &[LogicalExpr],
        aggr_exprs: &[LogicalExpr],
    ) -> Result<(usize, usize, usize)> {
        let mut inserted = 0;
        let mut updated = 0;
        let mut deleted = 0;

        // Process each delta
        for delta in &delta_set.deltas {
            match &delta.operation {
                DeltaOperation::Insert { tuple } => {
                    self.apply_insert_to_aggregate(mv_name, tuple, group_by, aggr_exprs)?;
                    inserted += 1;
                }
                DeltaOperation::Delete { tuple } => {
                    self.apply_delete_to_aggregate(mv_name, tuple, group_by, aggr_exprs)?;
                    deleted += 1;
                }
                DeltaOperation::Update { old_tuple, new_tuple } => {
                    // Treat as delete + insert
                    self.apply_delete_to_aggregate(mv_name, old_tuple, group_by, aggr_exprs)?;
                    self.apply_insert_to_aggregate(mv_name, new_tuple, group_by, aggr_exprs)?;
                    updated += 1;
                }
            }
        }

        Ok((inserted, updated, deleted))
    }

    /// Apply insert to aggregate materialized view
    fn apply_insert_to_aggregate(
        &self,
        mv_name: &str,
        tuple: &Tuple,
        group_by: &[LogicalExpr],
        aggr_exprs: &[LogicalExpr],
    ) -> Result<()> {
        // Extract group key from tuple
        let group_key = self.extract_group_key(tuple, group_by)?;

        // Get or create aggregate row
        let mut agg_row = self.get_or_create_agg_row(mv_name, &group_key, aggr_exprs)?;

        // Update aggregates
        for (i, aggr_expr) in aggr_exprs.iter().enumerate() {
            if let LogicalExpr::AggregateFunction { fun, args, .. } = aggr_expr {
                let agg_val = agg_row.values.get(i).cloned().unwrap_or(Value::Null);
                match fun {
                    AggregateFunction::Count => {
                        if let Value::Int8(count) = agg_val {
                            if let Some(slot) = agg_row.values.get_mut(i) {
                                *slot = Value::Int8(count + 1);
                            }
                        }
                    }
                    AggregateFunction::Sum => {
                        if let Some(first_arg) = args.first() {
                            let value = self.evaluate_expr_with_schema(first_arg, tuple, None)?;
                            let new_val = self.add_values(&agg_val, &value)?;
                            if let Some(slot) = agg_row.values.get_mut(i) {
                                *slot = new_val;
                            }
                        }
                    }
                    AggregateFunction::Avg => {
                        // For AVG, we need to maintain SUM and COUNT
                        // This is a simplified implementation
                        if let Some(first_arg) = args.first() {
                            let value = self.evaluate_expr_with_schema(first_arg, tuple, None)?;
                            // Update running average (simplified)
                            if let (Value::Float8(old_avg), Value::Float8(new_val)) = (&agg_val, value) {
                                // This is approximate; real implementation needs count tracking
                                if let Some(slot) = agg_row.values.get_mut(i) {
                                    *slot = Value::Float8((old_avg + new_val) / 2.0);
                                }
                            }
                        }
                    }
                    AggregateFunction::Min => {
                        if let Some(first_arg) = args.first() {
                            let value = self.evaluate_expr_with_schema(first_arg, tuple, None)?;
                            if self.compare_values(&value, &agg_val)? < 0 {
                                if let Some(slot) = agg_row.values.get_mut(i) {
                                    *slot = value;
                                }
                            }
                        }
                    }
                    AggregateFunction::Max => {
                        if let Some(first_arg) = args.first() {
                            let value = self.evaluate_expr_with_schema(first_arg, tuple, None)?;
                            if self.compare_values(&value, &agg_val)? > 0 {
                                if let Some(slot) = agg_row.values.get_mut(i) {
                                    *slot = value;
                                }
                            }
                        }
                    }
                    _ => {
                        return Err(Error::query_execution(format!(
                            "Unsupported aggregate function for incremental refresh: {:?}",
                            fun
                        )));
                    }
                }
            }
        }

        // Update aggregate row in MV
        self.update_agg_row(mv_name, &group_key, &agg_row)?;

        Ok(())
    }

    /// Apply delete to aggregate materialized view
    fn apply_delete_to_aggregate(
        &self,
        mv_name: &str,
        tuple: &Tuple,
        group_by: &[LogicalExpr],
        aggr_exprs: &[LogicalExpr],
    ) -> Result<()> {
        // Extract group key from tuple
        let group_key = self.extract_group_key(tuple, group_by)?;

        // Get aggregate row
        let mut agg_row = self.get_agg_row(mv_name, &group_key)?;

        // Update aggregates (reverse operation)
        for (i, aggr_expr) in aggr_exprs.iter().enumerate() {
            if let LogicalExpr::AggregateFunction { fun, args, .. } = aggr_expr {
                let agg_val = agg_row.values.get(i).cloned().unwrap_or(Value::Null);
                match fun {
                    AggregateFunction::Count => {
                        if let Value::Int8(count) = agg_val {
                            if let Some(slot) = agg_row.values.get_mut(i) {
                                *slot = Value::Int8(count - 1);
                            }
                        }
                    }
                    AggregateFunction::Sum => {
                        if let Some(first_arg) = args.first() {
                            let value = self.evaluate_expr_with_schema(first_arg, tuple, None)?;
                            let new_val = self.subtract_values(&agg_val, &value)?;
                            if let Some(slot) = agg_row.values.get_mut(i) {
                                *slot = new_val;
                            }
                        }
                    }
                    AggregateFunction::Min | AggregateFunction::Max => {
                        // MIN/MAX require special handling when deleting values
                        // Check if the deleted value equals the current min/max
                        if let Some(first_arg) = args.first() {
                            let deleted_value = self.evaluate_expr_with_schema(first_arg, tuple, None)?;
                            let group_key_len = group_key.len();
                            let agg_idx = group_key_len + i;

                            if let Some(current_agg) = agg_row.values.get(agg_idx) {
                                // Check if deleted value affects the min/max
                                if current_agg == &deleted_value {
                                    // Need to recompute min/max for this group
                                    // This requires scanning all tuples in the group
                                    // For now, we mark it as Null to indicate recomputation needed
                                    if let Some(slot) = agg_row.values.get_mut(agg_idx) {
                                        *slot = Value::Null;
                                    }

                                    // In a production system, we would:
                                    // 1. Scan the base table for this group
                                    // 2. Recompute the MIN/MAX
                                    // 3. Update the aggregate row
                                }
                            }
                        }
                    }
                    _ => {}
                }
            }
        }

        // Update or delete aggregate row
        if let Some(Value::Int8(count)) = agg_row.values.first().cloned() {
            if count <= 0 {
                self.delete_agg_row(mv_name, &group_key)?;
            } else {
                self.update_agg_row(mv_name, &group_key, &agg_row)?;
            }
        }

        Ok(())
    }

    /// Refresh filter materialized view incrementally
    fn refresh_filter_incremental(
        &self,
        mv_name: &str,
        mv_metadata: &MaterializedViewMetadata,
        delta_set: &DeltaSet,
        predicate: &LogicalExpr,
    ) -> Result<(usize, usize, usize)> {
        let mut inserted = 0;
        let mut updated = 0;
        let mut deleted = 0;

        let mv_data_table = format!("__mv_{}", mv_name);

        // Get base table schema for column mapping
        let base_table_schema = if let Some(first_table) = mv_metadata.base_tables.first() {
            let catalog = self.storage.catalog();
            Some(catalog.get_table_schema(first_table)?)
        } else {
            None
        };

        for delta in &delta_set.deltas {
            match &delta.operation {
                DeltaOperation::Insert { tuple } => {
                    if self.matches_filter_with_schema(tuple, predicate, base_table_schema.as_ref())? {
                        self.storage.insert_tuple(&mv_data_table, tuple.clone())?;
                        inserted += 1;
                    }
                }
                DeltaOperation::Delete { tuple } => {
                    if self.matches_filter_with_schema(tuple, predicate, base_table_schema.as_ref())? {
                        self.delete_from_mv(&mv_data_table, tuple)?;
                        deleted += 1;
                    }
                }
                DeltaOperation::Update { old_tuple, new_tuple } => {
                    let old_match = self.matches_filter_with_schema(old_tuple, predicate, base_table_schema.as_ref())?;
                    let new_match = self.matches_filter_with_schema(new_tuple, predicate, base_table_schema.as_ref())?;

                    match (old_match, new_match) {
                        (true, true) => {
                            self.update_in_mv(&mv_data_table, old_tuple, new_tuple)?;
                            updated += 1;
                        }
                        (true, false) => {
                            self.delete_from_mv(&mv_data_table, old_tuple)?;
                            deleted += 1;
                        }
                        (false, true) => {
                            self.storage.insert_tuple(&mv_data_table, new_tuple.clone())?;
                            inserted += 1;
                        }
                        (false, false) => {} // Neither in MV
                    }
                }
            }
        }

        Ok((inserted, updated, deleted))
    }

    /// Refresh join materialized view incrementally
    ///
    /// This implements incremental join refresh by processing deltas from both sides of the join.
    /// For each delta (INSERT/DELETE/UPDATE), we probe the other table to find matching rows
    /// and update the materialized view accordingly.
    ///
    /// Algorithm:
    /// - For INSERT deltas: probe other table, insert matching join results into MV
    /// - For DELETE deltas: find and remove corresponding rows from MV
    /// - For UPDATE deltas: handle as DELETE + INSERT
    fn refresh_join_incremental(
        &self,
        mv_name: &str,
        mv_metadata: &MaterializedViewMetadata,
        delta_sets: Vec<DeltaSet>,
        _join_type: &crate::sql::JoinType,
        on: &Option<LogicalExpr>,
    ) -> Result<(usize, usize, usize)> {
        let mut total_inserted = 0;
        let mut total_updated = 0;
        let mut total_deleted = 0;

        // Get MV data table name
        let mv_data_table = format!("__mv_{}", mv_name);

        // Get base tables (should be exactly 2 for a join)
        if mv_metadata.base_tables.len() != 2 {
            return Err(Error::query_execution(format!(
                "Join MV must have exactly 2 base tables, found {}",
                mv_metadata.base_tables.len()
            )));
        }

        let left_table = mv_metadata.base_tables.first().ok_or_else(|| {
            Error::query_execution("Join MV missing left base table")
        })?;
        let right_table = mv_metadata.base_tables.get(1).ok_or_else(|| {
            Error::query_execution("Join MV missing right base table")
        })?;

        // Process deltas from each table
        for delta_set in delta_sets {
            // Determine which is the probe table (the other table)
            let probe_table = if delta_set.table_name == *left_table {
                right_table
            } else if delta_set.table_name == *right_table {
                left_table
            } else {
                continue; // Skip unknown tables
            };

            // Get probe table data for matching
            let probe_tuples = self.storage.scan_table(probe_table)?;

            // Process each delta
            for delta in &delta_set.deltas {
                match &delta.operation {
                    DeltaOperation::Insert { tuple } => {
                        // Find matching rows in probe table
                        let matches = self.find_join_matches(
                            tuple,
                            &probe_tuples,
                            on,
                            &delta_set.table_name == left_table,
                        )?;

                        // Insert joined rows into MV
                        for matched_tuple in matches {
                            let joined = if delta_set.table_name == *left_table {
                                self.join_tuples(tuple, &matched_tuple)?
                            } else {
                                self.join_tuples(&matched_tuple, tuple)?
                            };

                            self.storage.insert_tuple(&mv_data_table, joined)?;
                            total_inserted += 1;
                        }
                    }

                    DeltaOperation::Delete { tuple } => {
                        // For delete, we need to find and remove corresponding MV rows
                        // This is simplified: we find rows that would have been created by this tuple
                        let matches = self.find_join_matches(
                            tuple,
                            &probe_tuples,
                            on,
                            &delta_set.table_name == left_table,
                        )?;

                        for matched_tuple in matches {
                            let joined = if delta_set.table_name == *left_table {
                                self.join_tuples(tuple, &matched_tuple)?
                            } else {
                                self.join_tuples(&matched_tuple, tuple)?
                            };

                            // Delete this joined tuple from MV
                            self.delete_from_mv(&mv_data_table, &joined)?;
                            total_deleted += 1;
                        }
                    }

                    DeltaOperation::Update { old_tuple, new_tuple } => {
                        // Handle update as delete + insert
                        // First, delete old matches
                        let old_matches = self.find_join_matches(
                            old_tuple,
                            &probe_tuples,
                            on,
                            &delta_set.table_name == left_table,
                        )?;

                        for matched_tuple in old_matches {
                            let joined = if delta_set.table_name == *left_table {
                                self.join_tuples(old_tuple, &matched_tuple)?
                            } else {
                                self.join_tuples(&matched_tuple, old_tuple)?
                            };

                            self.delete_from_mv(&mv_data_table, &joined)?;
                        }

                        // Then, insert new matches
                        let new_matches = self.find_join_matches(
                            new_tuple,
                            &probe_tuples,
                            on,
                            &delta_set.table_name == left_table,
                        )?;

                        for matched_tuple in new_matches {
                            let joined = if delta_set.table_name == *left_table {
                                self.join_tuples(new_tuple, &matched_tuple)?
                            } else {
                                self.join_tuples(&matched_tuple, new_tuple)?
                            };

                            self.storage.insert_tuple(&mv_data_table, joined)?;
                        }

                        total_updated += 1;
                    }
                }
            }
        }

        Ok((total_inserted, total_updated, total_deleted))
    }

    /// Find tuples in probe_tuples that match the given tuple according to join condition
    fn find_join_matches(
        &self,
        tuple: &Tuple,
        probe_tuples: &[Tuple],
        join_on: &Option<LogicalExpr>,
        is_left_tuple: bool,
    ) -> Result<Vec<Tuple>> {
        let mut matches = Vec::new();

        // If no join condition, this is a cross join (all combinations)
        let Some(join_expr) = join_on.as_ref() else {
            return Ok(probe_tuples.to_vec());
        };

        // For each probe tuple, check if it matches
        for probe_tuple in probe_tuples {
            let (left, right) = if is_left_tuple {
                (tuple, probe_tuple)
            } else {
                (probe_tuple, tuple)
            };

            // Evaluate join condition
            if self.evaluate_join_condition(join_expr, left, right)? {
                matches.push(probe_tuple.clone());
            }
        }

        Ok(matches)
    }

    /// Evaluate join condition with two tuples
    fn evaluate_join_condition(
        &self,
        expr: &LogicalExpr,
        left_tuple: &Tuple,
        right_tuple: &Tuple,
    ) -> Result<bool> {
        match expr {
            LogicalExpr::BinaryExpr { left, op, right } => {
                // Evaluate both sides
                let left_val = self.evaluate_join_expr(left, left_tuple, right_tuple)?;
                let right_val = self.evaluate_join_expr(right, left_tuple, right_tuple)?;

                // Apply comparison
                let result = self.apply_binary_op(&left_val, op, &right_val)?;

                match result {
                    Value::Boolean(b) => Ok(b),
                    _ => Ok(false),
                }
            }
            _ => Err(Error::query_execution("Unsupported join condition expression")),
        }
    }

    /// Evaluate expression in join context (can reference columns from both tables)
    fn evaluate_join_expr(
        &self,
        expr: &LogicalExpr,
        left_tuple: &Tuple,
        right_tuple: &Tuple,
    ) -> Result<Value> {
        match expr {
            LogicalExpr::Column { name, .. } => {
                // Parse column reference: might be "table.column" or just "column"
                // For simplicity, we use index-based access
                // Format: "$left.0" for left table column 0, "$right.1" for right table column 1
                if let Some(idx_str) = name.strip_prefix("$left.") {
                    let idx: usize = idx_str.parse()
                        .map_err(|_| Error::query_execution(format!("Invalid column index: {}", idx_str)))?;

                    left_tuple.values.get(idx).cloned()
                        .ok_or_else(|| Error::query_execution(format!("Column index {} out of bounds", idx)))
                } else if let Some(idx_str) = name.strip_prefix("$right.") {
                    let idx: usize = idx_str.parse()
                        .map_err(|_| Error::query_execution(format!("Invalid column index: {}", idx_str)))?;

                    right_tuple.values.get(idx).cloned()
                        .ok_or_else(|| Error::query_execution(format!("Column index {} out of bounds", idx)))
                } else {
                    // Assume it's an index directly
                    let idx: usize = name.parse()
                        .map_err(|_| Error::query_execution(format!("Invalid column reference: {}", name)))?;

                    // Try left first, then right
                    if let Some(val) = left_tuple.values.get(idx) {
                        Ok(val.clone())
                    } else {
                        let right_idx = idx.saturating_sub(left_tuple.values.len());
                        right_tuple.values.get(right_idx).cloned()
                            .ok_or_else(|| Error::query_execution(format!("Column index {} out of bounds", idx)))
                    }
                }
            }
            LogicalExpr::Literal(value) => Ok(value.clone()),
            _ => Err(Error::query_execution("Unsupported expression in join")),
        }
    }

    /// Join two tuples (concatenate their values)
    fn join_tuples(&self, left: &Tuple, right: &Tuple) -> Result<Tuple> {
        let mut values = Vec::new();
        values.extend_from_slice(&left.values);
        values.extend_from_slice(&right.values);

        Ok(Tuple {
            values,
            row_id: None, // Joined tuples get new row IDs
            branch_id: None,
        })
    }

    /// Extract table name from logical plan
    #[allow(clippy::self_only_used_in_recursion)]
    fn extract_table_name(&self, plan: &LogicalPlan) -> Result<String> {
        match plan {
            LogicalPlan::Scan { table_name, .. } => Ok(table_name.clone()),
            LogicalPlan::Filter { input, .. } => self.extract_table_name(input),
            LogicalPlan::Project { input, .. } => self.extract_table_name(input),
            _ => Err(Error::query_execution("Cannot extract table name from plan")),
        }
    }

    /// Extract group key from tuple
    fn extract_group_key(&self, tuple: &Tuple, group_by: &[LogicalExpr]) -> Result<Vec<Value>> {
        let mut key = Vec::new();
        for expr in group_by {
            let value = self.evaluate_expr_with_schema(expr, tuple, None)?;
            key.push(value);
        }
        Ok(key)
    }

    /// Get or create aggregate row
    ///
    /// Looks up an aggregate row by group key. If not found, creates a new one with initial values.
    /// The row is stored in the MV data table with the group key columns followed by aggregate columns.
    fn get_or_create_agg_row(
        &self,
        mv_name: &str,
        group_key: &[Value],
        aggr_exprs: &[LogicalExpr],
    ) -> Result<Tuple> {
        let mv_data_table = format!("__mv_{}", mv_name);

        // Try to find existing aggregate row
        if let Ok(existing) = self.get_agg_row(mv_name, group_key) {
            return Ok(existing);
        }

        // Create new aggregate row with initial values
        let mut values = Vec::new();

        // Add group key values first
        values.extend_from_slice(group_key);

        // Add initial aggregate values
        for expr in aggr_exprs {
            if let LogicalExpr::AggregateFunction { fun, .. } = expr {
                let initial = match fun {
                    AggregateFunction::Count => Value::Int8(0),
                    AggregateFunction::Sum => Value::Int8(0),
                    AggregateFunction::Avg => Value::Float8(0.0),
                    AggregateFunction::Min => Value::Null,
                    AggregateFunction::Max => Value::Null,
                    AggregateFunction::JsonAgg => Value::Json("[]".to_string()),
                    AggregateFunction::ArrayAgg => Value::Array(vec![]),
                    AggregateFunction::StringAgg { .. } => Value::String(String::new()),
                };
                values.push(initial);
            } else {
                values.push(Value::Null);
            }
        }

        Ok(Tuple { values, row_id: None, branch_id: None })
    }

    /// Get aggregate row by group key
    ///
    /// Scans the MV data table to find the row matching the given group key.
    /// Returns an error if no matching row is found.
    fn get_agg_row(&self, mv_name: &str, group_key: &[Value]) -> Result<Tuple> {
        let mv_data_table = format!("__mv_{}", mv_name);

        // Check if table exists
        let catalog = self.storage.catalog();
        if !catalog.table_exists(&mv_data_table)? {
            return Err(Error::query_execution(format!(
                "MV data table '{}' does not exist",
                mv_data_table
            )));
        }

        // Scan all tuples and find matching group key
        let tuples = self.storage.scan_table(&mv_data_table)?;

        for tuple in tuples {
            // Check if the first N values match the group key
            if let Some(tuple_key) = tuple.values.get(..group_key.len()) {
                if tuple_key == group_key {
                    return Ok(tuple);
                }
            }
        }

        Err(Error::query_execution(format!(
            "Aggregate row not found for group key: {:?}",
            group_key
        )))
    }

    /// Update aggregate row
    ///
    /// Updates an existing aggregate row in the MV data table.
    /// Finds the row by group key and replaces it with the new values.
    fn update_agg_row(&self, mv_name: &str, group_key: &[Value], agg_row: &Tuple) -> Result<()> {
        let mv_data_table = format!("__mv_{}", mv_name);

        // Delete old row
        self.delete_agg_row(mv_name, group_key)?;

        // Insert updated row
        self.storage.insert_tuple(&mv_data_table, agg_row.clone())?;

        Ok(())
    }

    /// Delete aggregate row
    ///
    /// Deletes an aggregate row from the MV data table identified by group key.
    fn delete_agg_row(&self, mv_name: &str, group_key: &[Value]) -> Result<()> {
        let mv_data_table = format!("__mv_{}", mv_name);

        // Find the row to delete
        let tuples = self.storage.scan_table(&mv_data_table)?;

        for tuple in tuples {
            // Check if the first N values match the group key
            if let Some(tuple_key) = tuple.values.get(..group_key.len()) {
                if tuple_key == group_key {
                    // Delete using the low-level key-value API
                    if let Some(row_id) = tuple.row_id {
                        let key = format!("data:{}:{}", mv_data_table, row_id).into_bytes();
                        self.storage.delete(&key)?;
                        return Ok(());
                    }
                }
            }
        }

        // Not finding the row is not an error for delete
        Ok(())
    }

    /// Check if tuple matches filter predicate (with schema)
    fn matches_filter_with_schema(&self, tuple: &Tuple, predicate: &LogicalExpr, schema: Option<&Schema>) -> Result<bool> {
        let result = self.evaluate_expr_with_schema(predicate, tuple, schema)?;
        match result {
            Value::Boolean(b) => Ok(b),
            _ => Ok(false),
        }
    }

    /// Check if tuple matches filter predicate (legacy, without schema)
    fn _matches_filter(&self, tuple: &Tuple, predicate: &LogicalExpr) -> Result<bool> {
        self.matches_filter_with_schema(tuple, predicate, None)
    }

    /// Evaluate expression on tuple with schema context
    fn evaluate_expr_with_schema(&self, expr: &LogicalExpr, tuple: &Tuple, schema: Option<&Schema>) -> Result<Value> {
        match expr {
            LogicalExpr::Column { name, .. } => {
                // Map column name to index using schema
                if let Some(schema) = schema {
                    // Find column index by name
                    for (idx, column) in schema.columns.iter().enumerate() {
                        if column.name == *name {
                            return tuple.values.get(idx).cloned()
                                .ok_or_else(|| Error::query_execution(format!(
                                    "Column index {} out of bounds for tuple with {} values",
                                    idx, tuple.values.len()
                                )));
                        }
                    }
                    Err(Error::query_execution(format!(
                        "Column '{}' not found in schema",
                        name
                    )))
                } else {
                    Err(Error::query_execution(format!(
                        "Column reference by name '{}' requires schema context",
                        name
                    )))
                }
            }
            LogicalExpr::Literal(value) => Ok(value.clone()),
            LogicalExpr::BinaryExpr { left, op, right } => {
                let left_val = self.evaluate_expr_with_schema(left, tuple, schema)?;
                let right_val = self.evaluate_expr_with_schema(right, tuple, schema)?;
                self.apply_binary_op(&left_val, op, &right_val)
            }
            _ => Err(Error::query_execution("Unsupported expression")),
        }
    }

    /// Evaluate expression on tuple (legacy, without schema)
    fn _evaluate_expr(&self, expr: &LogicalExpr, tuple: &Tuple) -> Result<Value> {
        self.evaluate_expr_with_schema(expr, tuple, None)
    }

    /// Apply binary operator
    fn apply_binary_op(&self, left: &Value, op: &BinaryOperator, right: &Value) -> Result<Value> {
        match op {
            BinaryOperator::Eq => Ok(Value::Boolean(left == right)),
            BinaryOperator::NotEq => Ok(Value::Boolean(left != right)),
            BinaryOperator::Lt => Ok(Value::Boolean(self.compare_values(left, right)? < 0)),
            BinaryOperator::LtEq => Ok(Value::Boolean(self.compare_values(left, right)? <= 0)),
            BinaryOperator::Gt => Ok(Value::Boolean(self.compare_values(left, right)? > 0)),
            BinaryOperator::GtEq => Ok(Value::Boolean(self.compare_values(left, right)? >= 0)),
            _ => Err(Error::query_execution("Unsupported binary operator")),
        }
    }

    /// Compare two values
    fn compare_values(&self, left: &Value, right: &Value) -> Result<i32> {
        match (left, right) {
            (Value::Int4(a), Value::Int4(b)) => Ok(a.cmp(b) as i32),
            (Value::Int8(a), Value::Int8(b)) => Ok(a.cmp(b) as i32),
            (Value::Float8(a), Value::Float8(b)) => {
                if a < b { Ok(-1) }
                else if a > b { Ok(1) }
                else { Ok(0) }
            }
            (Value::String(a), Value::String(b)) => Ok(a.cmp(b) as i32),
            _ => Err(Error::query_execution("Cannot compare incompatible types")),
        }
    }

    /// Add two values
    fn add_values(&self, left: &Value, right: &Value) -> Result<Value> {
        match (left, right) {
            (Value::Int4(a), Value::Int4(b)) => {
                a.checked_add(*b)
                    .map(Value::Int4)
                    .ok_or_else(|| Error::query_execution("integer overflow: INT addition"))
            }
            (Value::Int8(a), Value::Int8(b)) => {
                a.checked_add(*b)
                    .map(Value::Int8)
                    .ok_or_else(|| Error::query_execution("integer overflow: BIGINT addition"))
            }
            (Value::Float8(a), Value::Float8(b)) => Ok(Value::Float8(a + b)),
            _ => Err(Error::query_execution("Cannot add incompatible types")),
        }
    }

    /// Subtract two values
    fn subtract_values(&self, left: &Value, right: &Value) -> Result<Value> {
        match (left, right) {
            (Value::Int4(a), Value::Int4(b)) => {
                a.checked_sub(*b)
                    .map(Value::Int4)
                    .ok_or_else(|| Error::query_execution("integer overflow: INT subtraction"))
            }
            (Value::Int8(a), Value::Int8(b)) => {
                a.checked_sub(*b)
                    .map(Value::Int8)
                    .ok_or_else(|| Error::query_execution("integer overflow: BIGINT subtraction"))
            }
            (Value::Float8(a), Value::Float8(b)) => Ok(Value::Float8(a - b)),
            _ => Err(Error::query_execution("Cannot subtract incompatible types")),
        }
    }

    /// Delete tuple from materialized view
    ///
    /// Scans the MV data table to find a tuple matching the given values and deletes it.
    /// This is used for incremental refresh when removing rows from the view.
    fn delete_from_mv(&self, mv_table: &str, tuple: &Tuple) -> Result<()> {
        // Scan all tuples to find matching one
        let tuples = self.storage.scan_table(mv_table)?;

        for stored_tuple in tuples {
            // Compare tuple values (ignoring row_id)
            if stored_tuple.values == tuple.values {
                // Delete using the low-level key-value API
                if let Some(row_id) = stored_tuple.row_id {
                    let key = format!("data:{}:{}", mv_table, row_id).into_bytes();
                    self.storage.delete(&key)?;
                    return Ok(());
                }
            }
        }

        // Not finding the tuple is not necessarily an error
        // It might have been already deleted or never existed
        Ok(())
    }

    /// Update tuple in materialized view
    ///
    /// Finds a tuple matching old_tuple and replaces it with new_tuple.
    /// Implemented as delete + insert for simplicity.
    fn update_in_mv(&self, mv_table: &str, old_tuple: &Tuple, new_tuple: &Tuple) -> Result<()> {
        // Delete the old tuple
        self.delete_from_mv(mv_table, old_tuple)?;

        // Insert the new tuple
        self.storage.insert_tuple(mv_table, new_tuple.clone())?;

        Ok(())
    }

    /// Count tuples in a table
    fn count_tuples(&self, table_name: &str) -> Result<usize> {
        let catalog = self.storage.catalog();
        if !catalog.table_exists(table_name)? {
            return Ok(0);
        }
        let tuples = self.storage.scan_table(table_name)?;
        Ok(tuples.len())
    }
}

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

    #[test]
    fn test_refresh_strategy() {
        assert_eq!(RefreshStrategy::Incremental, RefreshStrategy::Incremental);
        assert_ne!(RefreshStrategy::Incremental, RefreshStrategy::Full);
    }

    #[test]
    fn test_delta_tracker() {
        let config = Config::in_memory();
        let storage = Arc::new(StorageEngine::open_in_memory(&config).unwrap());
        let tracker = DeltaTracker::new(storage);

        let tuple = Tuple {
            values: vec![Value::Int4(1), Value::String("test".to_string())],
            row_id: None,
            branch_id: None,
        };

        tracker.record_insert("users", tuple.clone(), 100);

        let deltas = tracker.get_deltas_since("users", 50);
        assert_eq!(deltas.len(), 1);

        let deltas = tracker.get_deltas_since("users", 150);
        assert_eq!(deltas.len(), 0);
    }

    #[test]
    fn test_delta_operations() {
        let tuple1 = Tuple {
            values: vec![Value::Int4(1)],
            row_id: None,
            branch_id: None,
        };
        let tuple2 = Tuple {
            values: vec![Value::Int4(2)],
            row_id: None,
            branch_id: None,
        };

        let insert = DeltaOperation::Insert { tuple: tuple1.clone() };
        let delete = DeltaOperation::Delete { tuple: tuple1.clone() };
        let update = DeltaOperation::Update {
            old_tuple: tuple1,
            new_tuple: tuple2,
        };

        assert!(matches!(insert, DeltaOperation::Insert { .. }));
        assert!(matches!(delete, DeltaOperation::Delete { .. }));
        assert!(matches!(update, DeltaOperation::Update { .. }));
    }

    #[test]
    fn test_cost_estimation() {
        let config = Config::in_memory();
        let storage = Arc::new(StorageEngine::open_in_memory(&config).unwrap());
        let tracker = Arc::new(DeltaTracker::new(Arc::clone(&storage)));
        let refresher = IncrementalRefresher::new(storage, tracker);

        // Small delta count -> incremental preferred
        let cost = RefreshCost {
            incremental_cost: 0.1,
            full_cost: 10.0,
            recommendation: RefreshStrategy::Incremental,
        };
        assert!(cost.incremental_cost < cost.full_cost);

        // Large delta count -> full refresh preferred
        let cost = RefreshCost {
            incremental_cost: 8.0,
            full_cost: 10.0,
            recommendation: RefreshStrategy::Full,
        };
        assert!(cost.incremental_cost > cost.full_cost * 0.5);
    }

    #[test]
    fn test_incremental_refresher_creation() {
        let config = Config::in_memory();
        let storage = Arc::new(StorageEngine::open_in_memory(&config).unwrap());
        let tracker = Arc::new(DeltaTracker::new(Arc::clone(&storage)));
        let _refresher = IncrementalRefresher::new(storage, tracker);
    }
}