reddb-io-server 1.1.2

RedDB server-side engine: storage, runtime, replication, MCP, AI, and the gRPC/HTTP/RedWire/PG-wire dispatchers. Re-exported by the umbrella `reddb` crate.
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
//! Query Rewriter
//!
//! Multi-pass AST transformation system inspired by Neo4j's query rewriting.
//!
//! # Rewrite Passes
//!
//! 1. **Normalize**: Standardize AST structure
//! 2. **InjectCachedProperties**: Cache property lookups at compile time
//! 3. **SimplifyFilters**: Combine and simplify filter expressions
//! 4. **PushdownPredicates**: Move filters closer to data source
//! 5. **ValidateFunctions**: Check function calls against schema

use crate::storage::query::ast::{
    CompareOp, FieldRef, Filter as AstFilter, JoinQuery, Projection, QueryExpr,
};
use crate::storage::query::sql_lowering::{
    effective_graph_filter, effective_join_filter, effective_table_filter, effective_vector_filter,
};
use crate::storage::schema::Value;

/// Context for rewrite operations
#[derive(Debug, Clone, Default)]
pub struct RewriteContext {
    /// Property cache for compile-time lookups
    pub property_cache: Vec<CachedProperty>,
    /// Validation errors encountered
    pub errors: Vec<String>,
    /// Warnings generated
    pub warnings: Vec<String>,
    /// Statistics about rewrites
    pub stats: RewriteStats,
}

/// A cached property lookup
#[derive(Debug, Clone)]
pub struct CachedProperty {
    /// Source alias (table or node)
    pub source: String,
    /// Property name
    pub property: String,
    /// Cached value if known at compile time
    pub cached_value: Option<String>,
}

/// Statistics about rewrite passes
#[derive(Debug, Clone, Default)]
pub struct RewriteStats {
    /// Number of filters simplified
    pub filters_simplified: u32,
    /// Number of predicates pushed down
    pub predicates_pushed: u32,
    /// Number of properties cached
    pub properties_cached: u32,
    /// Number of expressions normalized
    pub expressions_normalized: u32,
}

/// A rewrite rule that transforms query expressions
pub trait RewriteRule: Send + Sync {
    /// Rule name for debugging
    fn name(&self) -> &str;

    /// Apply the rule to a query expression
    fn apply(&self, query: QueryExpr, ctx: &mut RewriteContext) -> QueryExpr;

    /// Check if this rule is applicable to the query
    fn is_applicable(&self, query: &QueryExpr) -> bool;
}

/// Query rewriter with pluggable rules
pub struct QueryRewriter {
    /// Ordered list of rewrite rules
    rules: Vec<Box<dyn RewriteRule>>,
    /// Maximum number of rewrite iterations
    max_iterations: usize,
}

impl QueryRewriter {
    /// Create a new rewriter with default rules
    pub fn new() -> Self {
        let rules: Vec<Box<dyn RewriteRule>> = vec![
            Box::new(NormalizeRule),
            Box::new(SimplifyFiltersRule),
            Box::new(PushdownPredicatesRule),
            Box::new(EliminateDeadCodeRule),
            Box::new(FoldConstantsRule),
        ];

        Self {
            rules,
            max_iterations: 10,
        }
    }

    /// Add a custom rewrite rule
    pub fn add_rule(&mut self, rule: Box<dyn RewriteRule>) {
        self.rules.push(rule);
    }

    /// Rewrite a query expression
    pub fn rewrite(&self, query: QueryExpr) -> QueryExpr {
        let mut ctx = RewriteContext::default();
        self.rewrite_with_context(query, &mut ctx)
    }

    /// Rewrite with access to context
    pub fn rewrite_with_context(
        &self,
        mut query: QueryExpr,
        ctx: &mut RewriteContext,
    ) -> QueryExpr {
        // Apply rules iteratively until fixed point
        for _iteration in 0..self.max_iterations {
            let original = format!("{:?}", query);

            for rule in &self.rules {
                if rule.is_applicable(&query) {
                    query = rule.apply(query, ctx);
                }
            }

            // Check for fixed point
            if format!("{:?}", query) == original {
                break;
            }
        }

        query
    }
}

impl Default for QueryRewriter {
    fn default() -> Self {
        Self::new()
    }
}

// =============================================================================
// Built-in Rewrite Rules
// =============================================================================

/// Normalize AST structure
struct NormalizeRule;

impl RewriteRule for NormalizeRule {
    fn name(&self) -> &str {
        "Normalize"
    }

    fn apply(&self, query: QueryExpr, ctx: &mut RewriteContext) -> QueryExpr {
        match query {
            QueryExpr::Table(mut tq) => {
                // Normalize column order
                tq.columns.sort_by(|a, b| {
                    let a_name = projection_name(a);
                    let b_name = projection_name(b);
                    a_name.cmp(&b_name)
                });
                ctx.stats.expressions_normalized += 1;
                QueryExpr::Table(tq)
            }
            QueryExpr::Graph(gq) => {
                // Graph queries don't need normalization currently
                QueryExpr::Graph(gq)
            }
            QueryExpr::Join(jq) => {
                // Recursively normalize children
                let left = self.apply(*jq.left, ctx);
                let right = self.apply(*jq.right, ctx);
                QueryExpr::Join(JoinQuery {
                    left: Box::new(left),
                    right: Box::new(right),
                    ..jq
                })
            }
            QueryExpr::Path(pq) => QueryExpr::Path(pq),
            QueryExpr::Vector(vq) => {
                // Vector queries don't need normalization currently
                QueryExpr::Vector(vq)
            }
            QueryExpr::Hybrid(mut hq) => {
                // Normalize the structured part
                hq.structured = Box::new(self.apply(*hq.structured, ctx));
                QueryExpr::Hybrid(hq)
            }
            // DML/DDL/Command statements pass through without normalization
            other @ (QueryExpr::Insert(_)
            | QueryExpr::Update(_)
            | QueryExpr::Delete(_)
            | QueryExpr::CreateTable(_)
            | QueryExpr::CreateCollection(_)
            | QueryExpr::CreateVector(_)
            | QueryExpr::DropTable(_)
            | QueryExpr::DropGraph(_)
            | QueryExpr::DropVector(_)
            | QueryExpr::DropDocument(_)
            | QueryExpr::DropKv(_)
            | QueryExpr::DropCollection(_)
            | QueryExpr::Truncate(_)
            | QueryExpr::AlterTable(_)
            | QueryExpr::GraphCommand(_)
            | QueryExpr::SearchCommand(_)
            | QueryExpr::CreateIndex(_)
            | QueryExpr::DropIndex(_)
            | QueryExpr::ProbabilisticCommand(_)
            | QueryExpr::Ask(_)
            | QueryExpr::SetConfig { .. }
            | QueryExpr::ShowConfig { .. }
            | QueryExpr::SetSecret { .. }
            | QueryExpr::DeleteSecret { .. }
            | QueryExpr::ShowSecrets { .. }
            | QueryExpr::SetTenant(_)
            | QueryExpr::ShowTenant
            | QueryExpr::CreateTimeSeries(_)
            | QueryExpr::DropTimeSeries(_)
            | QueryExpr::CreateQueue(_)
            | QueryExpr::AlterQueue(_)
            | QueryExpr::DropQueue(_)
            | QueryExpr::QueueSelect(_)
            | QueryExpr::QueueCommand(_)
            | QueryExpr::KvCommand(_)
            | QueryExpr::ConfigCommand(_)
            | QueryExpr::CreateTree(_)
            | QueryExpr::DropTree(_)
            | QueryExpr::TreeCommand(_)
            | QueryExpr::ExplainAlter(_)
            | QueryExpr::TransactionControl(_)
            | QueryExpr::MaintenanceCommand(_)
            | QueryExpr::CreateSchema(_)
            | QueryExpr::DropSchema(_)
            | QueryExpr::CreateSequence(_)
            | QueryExpr::DropSequence(_)
            | QueryExpr::CopyFrom(_)
            | QueryExpr::CreateView(_)
            | QueryExpr::DropView(_)
            | QueryExpr::RefreshMaterializedView(_)
            | QueryExpr::CreatePolicy(_)
            | QueryExpr::DropPolicy(_)
            | QueryExpr::CreateServer(_)
            | QueryExpr::DropServer(_)
            | QueryExpr::CreateForeignTable(_)
            | QueryExpr::DropForeignTable(_)
            | QueryExpr::Grant(_)
            | QueryExpr::Revoke(_)
            | QueryExpr::AlterUser(_)
            | QueryExpr::CreateIamPolicy { .. }
            | QueryExpr::DropIamPolicy { .. }
            | QueryExpr::AttachPolicy { .. }
            | QueryExpr::DetachPolicy { .. }
            | QueryExpr::ShowPolicies { .. }
            | QueryExpr::ShowEffectivePermissions { .. }
            | QueryExpr::SimulatePolicy { .. }
            | QueryExpr::CreateMigration(_)
            | QueryExpr::ApplyMigration(_)
            | QueryExpr::RollbackMigration(_)
            | QueryExpr::ExplainMigration(_)
            | QueryExpr::EventsBackfill(_)
            | QueryExpr::EventsBackfillStatus { .. }) => other,
        }
    }

    fn is_applicable(&self, _query: &QueryExpr) -> bool {
        true
    }
}

/// Simplify filter expressions
struct SimplifyFiltersRule;

impl RewriteRule for SimplifyFiltersRule {
    fn name(&self) -> &str {
        "SimplifyFilters"
    }

    fn apply(&self, query: QueryExpr, ctx: &mut RewriteContext) -> QueryExpr {
        match query {
            QueryExpr::Table(mut tq) => {
                if let Some(filter) = effective_table_filter(&tq) {
                    tq.filter = Some(simplify_filter(filter, ctx));
                }
                QueryExpr::Table(tq)
            }
            QueryExpr::Graph(mut gq) => {
                if let Some(filter) = effective_graph_filter(&gq) {
                    gq.filter = Some(simplify_filter(filter, ctx));
                }
                QueryExpr::Graph(gq)
            }
            QueryExpr::Join(mut jq) => {
                let join_filter = effective_join_filter(&jq);
                let left = self.apply(*jq.left, ctx);
                let right = self.apply(*jq.right, ctx);
                if let Some(filter) = join_filter {
                    jq.filter = Some(simplify_filter(filter, ctx));
                }
                jq.left = Box::new(left);
                jq.right = Box::new(right);
                QueryExpr::Join(jq)
            }
            QueryExpr::Path(pq) => QueryExpr::Path(pq),
            QueryExpr::Vector(vq) => {
                // Vector queries have MetadataFilter, not AstFilter
                // Pass through for now
                QueryExpr::Vector(vq)
            }
            QueryExpr::Hybrid(mut hq) => {
                // Simplify filters in the structured part
                hq.structured = Box::new(self.apply(*hq.structured, ctx));
                QueryExpr::Hybrid(hq)
            }
            // DML/DDL/Command statements pass through without filter simplification
            other @ (QueryExpr::Insert(_)
            | QueryExpr::Update(_)
            | QueryExpr::Delete(_)
            | QueryExpr::CreateTable(_)
            | QueryExpr::CreateCollection(_)
            | QueryExpr::CreateVector(_)
            | QueryExpr::DropTable(_)
            | QueryExpr::DropGraph(_)
            | QueryExpr::DropVector(_)
            | QueryExpr::DropDocument(_)
            | QueryExpr::DropKv(_)
            | QueryExpr::DropCollection(_)
            | QueryExpr::Truncate(_)
            | QueryExpr::AlterTable(_)
            | QueryExpr::GraphCommand(_)
            | QueryExpr::SearchCommand(_)
            | QueryExpr::CreateIndex(_)
            | QueryExpr::DropIndex(_)
            | QueryExpr::ProbabilisticCommand(_)
            | QueryExpr::Ask(_)
            | QueryExpr::SetConfig { .. }
            | QueryExpr::ShowConfig { .. }
            | QueryExpr::SetSecret { .. }
            | QueryExpr::DeleteSecret { .. }
            | QueryExpr::ShowSecrets { .. }
            | QueryExpr::SetTenant(_)
            | QueryExpr::ShowTenant
            | QueryExpr::CreateTimeSeries(_)
            | QueryExpr::DropTimeSeries(_)
            | QueryExpr::CreateQueue(_)
            | QueryExpr::AlterQueue(_)
            | QueryExpr::DropQueue(_)
            | QueryExpr::QueueSelect(_)
            | QueryExpr::QueueCommand(_)
            | QueryExpr::KvCommand(_)
            | QueryExpr::ConfigCommand(_)
            | QueryExpr::CreateTree(_)
            | QueryExpr::DropTree(_)
            | QueryExpr::TreeCommand(_)
            | QueryExpr::ExplainAlter(_)
            | QueryExpr::TransactionControl(_)
            | QueryExpr::MaintenanceCommand(_)
            | QueryExpr::CreateSchema(_)
            | QueryExpr::DropSchema(_)
            | QueryExpr::CreateSequence(_)
            | QueryExpr::DropSequence(_)
            | QueryExpr::CopyFrom(_)
            | QueryExpr::CreateView(_)
            | QueryExpr::DropView(_)
            | QueryExpr::RefreshMaterializedView(_)
            | QueryExpr::CreatePolicy(_)
            | QueryExpr::DropPolicy(_)
            | QueryExpr::CreateServer(_)
            | QueryExpr::DropServer(_)
            | QueryExpr::CreateForeignTable(_)
            | QueryExpr::DropForeignTable(_)
            | QueryExpr::Grant(_)
            | QueryExpr::Revoke(_)
            | QueryExpr::AlterUser(_)
            | QueryExpr::CreateIamPolicy { .. }
            | QueryExpr::DropIamPolicy { .. }
            | QueryExpr::AttachPolicy { .. }
            | QueryExpr::DetachPolicy { .. }
            | QueryExpr::ShowPolicies { .. }
            | QueryExpr::ShowEffectivePermissions { .. }
            | QueryExpr::SimulatePolicy { .. }
            | QueryExpr::CreateMigration(_)
            | QueryExpr::ApplyMigration(_)
            | QueryExpr::RollbackMigration(_)
            | QueryExpr::ExplainMigration(_)
            | QueryExpr::EventsBackfill(_)
            | QueryExpr::EventsBackfillStatus { .. }) => other,
        }
    }

    fn is_applicable(&self, query: &QueryExpr) -> bool {
        match query {
            QueryExpr::Table(tq) => effective_table_filter(tq).is_some(),
            QueryExpr::Graph(gq) => effective_graph_filter(gq).is_some(),
            QueryExpr::Join(_) => true,
            QueryExpr::Path(_) => false,
            QueryExpr::Vector(vq) => effective_vector_filter(vq).is_some(),
            QueryExpr::Hybrid(_) => true, // May have filters in structured part
            // DML/DDL/Command statements are not applicable for filter simplification
            QueryExpr::Insert(_)
            | QueryExpr::Update(_)
            | QueryExpr::Delete(_)
            | QueryExpr::CreateTable(_)
            | QueryExpr::CreateCollection(_)
            | QueryExpr::CreateVector(_)
            | QueryExpr::DropTable(_)
            | QueryExpr::DropGraph(_)
            | QueryExpr::DropVector(_)
            | QueryExpr::DropDocument(_)
            | QueryExpr::DropKv(_)
            | QueryExpr::DropCollection(_)
            | QueryExpr::Truncate(_)
            | QueryExpr::AlterTable(_)
            | QueryExpr::GraphCommand(_)
            | QueryExpr::SearchCommand(_)
            | QueryExpr::CreateIndex(_)
            | QueryExpr::DropIndex(_)
            | QueryExpr::ProbabilisticCommand(_)
            | QueryExpr::Ask(_)
            | QueryExpr::SetConfig { .. }
            | QueryExpr::ShowConfig { .. }
            | QueryExpr::SetSecret { .. }
            | QueryExpr::DeleteSecret { .. }
            | QueryExpr::ShowSecrets { .. }
            | QueryExpr::SetTenant(_)
            | QueryExpr::ShowTenant
            | QueryExpr::CreateTimeSeries(_)
            | QueryExpr::DropTimeSeries(_)
            | QueryExpr::CreateQueue(_)
            | QueryExpr::AlterQueue(_)
            | QueryExpr::DropQueue(_)
            | QueryExpr::QueueSelect(_)
            | QueryExpr::QueueCommand(_)
            | QueryExpr::KvCommand(_)
            | QueryExpr::ConfigCommand(_)
            | QueryExpr::CreateTree(_)
            | QueryExpr::DropTree(_)
            | QueryExpr::TreeCommand(_)
            | QueryExpr::ExplainAlter(_)
            | QueryExpr::TransactionControl(_)
            | QueryExpr::MaintenanceCommand(_)
            | QueryExpr::CreateSchema(_)
            | QueryExpr::DropSchema(_)
            | QueryExpr::CreateSequence(_)
            | QueryExpr::DropSequence(_)
            | QueryExpr::CopyFrom(_)
            | QueryExpr::CreateView(_)
            | QueryExpr::DropView(_)
            | QueryExpr::RefreshMaterializedView(_)
            | QueryExpr::CreatePolicy(_)
            | QueryExpr::DropPolicy(_)
            | QueryExpr::CreateServer(_)
            | QueryExpr::DropServer(_)
            | QueryExpr::CreateForeignTable(_)
            | QueryExpr::DropForeignTable(_)
            | QueryExpr::Grant(_)
            | QueryExpr::Revoke(_)
            | QueryExpr::AlterUser(_)
            | QueryExpr::CreateIamPolicy { .. }
            | QueryExpr::DropIamPolicy { .. }
            | QueryExpr::AttachPolicy { .. }
            | QueryExpr::DetachPolicy { .. }
            | QueryExpr::ShowPolicies { .. }
            | QueryExpr::ShowEffectivePermissions { .. }
            | QueryExpr::SimulatePolicy { .. }
            | QueryExpr::CreateMigration(_)
            | QueryExpr::ApplyMigration(_)
            | QueryExpr::RollbackMigration(_)
            | QueryExpr::ExplainMigration(_)
            | QueryExpr::EventsBackfill(_)
            | QueryExpr::EventsBackfillStatus { .. } => false,
        }
    }
}

/// Push predicates down to data sources
struct PushdownPredicatesRule;

impl RewriteRule for PushdownPredicatesRule {
    fn name(&self) -> &str {
        "PushdownPredicates"
    }

    fn apply(&self, query: QueryExpr, ctx: &mut RewriteContext) -> QueryExpr {
        match query {
            QueryExpr::Join(mut jq) => {
                // Try to push join predicates down to children
                // This is a simplified version - real implementation would analyze
                // which predicates can be pushed to which child

                // For now, just recursively apply to children
                jq.left = Box::new(self.apply(*jq.left, ctx));
                jq.right = Box::new(self.apply(*jq.right, ctx));
                ctx.stats.predicates_pushed += 1;
                QueryExpr::Join(jq)
            }
            other => other,
        }
    }

    fn is_applicable(&self, query: &QueryExpr) -> bool {
        matches!(query, QueryExpr::Join(_))
    }
}

/// Eliminate dead code branches
struct EliminateDeadCodeRule;

impl RewriteRule for EliminateDeadCodeRule {
    fn name(&self) -> &str {
        "EliminateDeadCode"
    }

    fn apply(&self, query: QueryExpr, _ctx: &mut RewriteContext) -> QueryExpr {
        match query {
            QueryExpr::Table(mut tq) => {
                // Remove always-true filters
                if let Some(filter) = effective_table_filter(&tq).as_ref() {
                    if is_always_true(filter) {
                        tq.filter = None;
                    }
                }
                QueryExpr::Table(tq)
            }
            other => other,
        }
    }

    fn is_applicable(&self, query: &QueryExpr) -> bool {
        matches!(query, QueryExpr::Table(_))
    }
}

/// Fold constant expressions
struct FoldConstantsRule;

impl RewriteRule for FoldConstantsRule {
    fn name(&self) -> &str {
        "FoldConstants"
    }

    fn apply(&self, query: QueryExpr, _ctx: &mut RewriteContext) -> QueryExpr {
        // Constant folding is complex - for now just pass through
        // A real implementation would evaluate constant expressions at compile time
        query
    }

    fn is_applicable(&self, _query: &QueryExpr) -> bool {
        true
    }
}

// =============================================================================
// Helper Functions
// =============================================================================

fn projection_name(proj: &Projection) -> String {
    match proj {
        Projection::All => "*".to_string(),
        Projection::Column(name) => name.clone(),
        Projection::Alias(_, alias) => alias.clone(),
        Projection::Function(name, _) => name.clone(),
        Projection::Expression(expr, _) => format!("{:?}", expr),
        Projection::Field(field, alias) => alias.clone().unwrap_or_else(|| format!("{:?}", field)),
    }
}

fn simplify_filter(filter: AstFilter, ctx: &mut RewriteContext) -> AstFilter {
    match filter {
        AstFilter::And(left, right) => {
            let left = simplify_filter(*left, ctx);
            let right = simplify_filter(*right, ctx);

            // AND with TRUE -> other side
            if is_always_true(&left) {
                ctx.stats.filters_simplified += 1;
                return right;
            }
            if is_always_true(&right) {
                ctx.stats.filters_simplified += 1;
                return left;
            }

            // AND with FALSE -> FALSE
            if is_always_false(&left) || is_always_false(&right) {
                ctx.stats.filters_simplified += 1;
                return AstFilter::Compare {
                    field: FieldRef::TableColumn {
                        table: String::new(),
                        column: "1".to_string(),
                    },
                    op: CompareOp::Eq,
                    value: Value::Integer(0),
                };
            }

            AstFilter::And(Box::new(left), Box::new(right))
        }
        AstFilter::Or(left, right) => {
            let left = simplify_filter(*left, ctx);
            let right = simplify_filter(*right, ctx);

            // OR with FALSE -> other side
            if is_always_false(&left) {
                ctx.stats.filters_simplified += 1;
                return right;
            }
            if is_always_false(&right) {
                ctx.stats.filters_simplified += 1;
                return left;
            }

            // OR with TRUE -> TRUE
            if is_always_true(&left) || is_always_true(&right) {
                ctx.stats.filters_simplified += 1;
                return AstFilter::Compare {
                    field: FieldRef::TableColumn {
                        table: String::new(),
                        column: "1".to_string(),
                    },
                    op: CompareOp::Eq,
                    value: Value::Integer(1),
                };
            }

            AstFilter::Or(Box::new(left), Box::new(right))
        }
        AstFilter::Not(inner) => {
            let inner = simplify_filter(*inner, ctx);

            // NOT NOT x -> x
            if let AstFilter::Not(double_inner) = inner {
                ctx.stats.filters_simplified += 1;
                return *double_inner;
            }

            AstFilter::Not(Box::new(inner))
        }
        other => other,
    }
}

fn is_always_true(filter: &AstFilter) -> bool {
    match filter {
        AstFilter::Compare { field, op, value } => {
            // 1 = 1 is always true
            matches!(field, FieldRef::TableColumn { column, .. } if column == "1")
                && matches!(op, CompareOp::Eq)
                && matches!(value, Value::Integer(1))
        }
        _ => false,
    }
}

fn is_always_false(filter: &AstFilter) -> bool {
    match filter {
        AstFilter::Compare { field, op, value } => {
            // 1 = 0 is always false
            matches!(field, FieldRef::TableColumn { column, .. } if column == "1")
                && matches!(op, CompareOp::Eq)
                && matches!(value, Value::Integer(0))
        }
        _ => false,
    }
}

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

    fn make_field(name: &str) -> FieldRef {
        FieldRef::TableColumn {
            table: String::new(),
            column: name.to_string(),
        }
    }

    #[test]
    fn test_simplify_and_with_true() {
        let mut ctx = RewriteContext::default();

        let filter = AstFilter::And(
            Box::new(AstFilter::Compare {
                field: make_field("1"),
                op: CompareOp::Eq,
                value: Value::Integer(1),
            }),
            Box::new(AstFilter::Compare {
                field: make_field("x"),
                op: CompareOp::Eq,
                value: Value::Integer(5),
            }),
        );

        let simplified = simplify_filter(filter, &mut ctx);

        match simplified {
            AstFilter::Compare { field, .. } => {
                assert!(matches!(field, FieldRef::TableColumn { column, .. } if column == "x"));
            }
            _ => panic!("Expected Compare filter"),
        }
    }

    #[test]
    fn test_simplify_double_not() {
        let mut ctx = RewriteContext::default();

        let filter = AstFilter::Not(Box::new(AstFilter::Not(Box::new(AstFilter::Compare {
            field: make_field("x"),
            op: CompareOp::Eq,
            value: Value::Integer(5),
        }))));

        let simplified = simplify_filter(filter, &mut ctx);

        match simplified {
            AstFilter::Compare { field, .. } => {
                assert!(matches!(field, FieldRef::TableColumn { column, .. } if column == "x"));
            }
            _ => panic!("Expected Compare filter"),
        }
    }
}