Skip to main content

reddb_server/storage/query/planner/
optimizer.rs

1//! Query Optimizer
2//!
3//! Multi-pass query optimization with pluggable strategies.
4//!
5//! # Optimization Passes
6//!
7//! 1. **PredicatePushdown**: Move filters to data sources
8//! 2. **JoinReordering**: Optimal join order via IDP algorithm
9//! 3. **IndexSelection**: Choose best indexes for scans
10//! 4. **ProjectionPushdown**: Eliminate unused columns early
11//! 5. **ExpressionSimplification**: Simplify complex expressions
12
13use crate::storage::query::ast::{JoinQuery, JoinType, QueryExpr};
14use crate::storage::query::sql_lowering::{effective_table_filter, effective_vector_filter};
15
16/// An optimization pass that transforms query expressions
17pub trait OptimizationPass: Send + Sync {
18    /// Pass name for debugging
19    fn name(&self) -> &str;
20
21    /// Apply the optimization pass
22    fn apply(&self, query: QueryExpr) -> QueryExpr;
23
24    /// Estimated benefit (higher = more important)
25    fn benefit(&self) -> u32;
26}
27
28/// Query optimizer with multiple passes
29pub struct QueryOptimizer {
30    /// Ordered optimization passes
31    passes: Vec<Box<dyn OptimizationPass>>,
32    /// Enable cost-based optimization
33    cost_based: bool,
34}
35
36impl QueryOptimizer {
37    /// Create a new optimizer with default passes
38    pub fn new() -> Self {
39        let passes: Vec<Box<dyn OptimizationPass>> = vec![
40            Box::new(PredicatePushdownPass),
41            Box::new(ProjectionPushdownPass),
42            Box::new(JoinReorderingPass),
43            Box::new(IndexSelectionPass),
44            Box::new(LimitPushdownPass),
45        ];
46
47        Self {
48            passes,
49            cost_based: true,
50        }
51    }
52
53    /// Add a custom optimization pass
54    pub fn add_pass(&mut self, pass: Box<dyn OptimizationPass>) {
55        self.passes.push(pass);
56        // Sort by benefit (highest first)
57        self.passes.sort_by_key(|b| std::cmp::Reverse(b.benefit()));
58    }
59
60    /// Optimize a query expression
61    pub fn optimize(&self, query: QueryExpr) -> (QueryExpr, Vec<String>) {
62        let mut optimized = query;
63        let mut applied_passes = Vec::new();
64
65        for pass in &self.passes {
66            let before = format!("{:?}", optimized);
67            optimized = pass.apply(optimized);
68            let after = format!("{:?}", optimized);
69
70            if before != after {
71                applied_passes.push(pass.name().to_string());
72            }
73        }
74
75        (optimized, applied_passes)
76    }
77
78    /// Optimize with hints
79    pub fn optimize_with_hints(&self, query: QueryExpr, hints: &OptimizationHints) -> QueryExpr {
80        let mut optimized = query;
81
82        for pass in &self.passes {
83            // Check if pass is disabled by hints
84            if hints.disabled_passes.contains(&pass.name().to_string()) {
85                continue;
86            }
87
88            optimized = pass.apply(optimized);
89        }
90
91        optimized
92    }
93}
94
95impl Default for QueryOptimizer {
96    fn default() -> Self {
97        Self::new()
98    }
99}
100
101/// Hints to control optimization
102#[derive(Debug, Clone, Default)]
103pub struct OptimizationHints {
104    /// Disabled optimization passes
105    pub disabled_passes: Vec<String>,
106    /// Force specific join order
107    pub join_order: Option<Vec<String>>,
108    /// Force specific index usage
109    pub force_index: Option<String>,
110    /// Disable parallel execution
111    pub no_parallel: bool,
112}
113
114// =============================================================================
115// Built-in Optimization Passes
116// =============================================================================
117
118/// Push predicates down to data sources
119struct PredicatePushdownPass;
120
121impl OptimizationPass for PredicatePushdownPass {
122    fn name(&self) -> &str {
123        "PredicatePushdown"
124    }
125
126    fn apply(&self, query: QueryExpr) -> QueryExpr {
127        match query {
128            QueryExpr::Join(jq) => self.optimize_join(jq),
129            other => other,
130        }
131    }
132
133    fn benefit(&self) -> u32 {
134        100 // High priority - reduces data early
135    }
136}
137
138impl PredicatePushdownPass {
139    fn optimize_join(&self, query: JoinQuery) -> QueryExpr {
140        // Analyze join condition to find pushable predicates
141        // This is a simplified version - real implementation would analyze
142        // predicate dependencies on join columns
143
144        let left = self.apply(*query.left);
145        let right = self.apply(*query.right);
146
147        QueryExpr::Join(JoinQuery {
148            left: Box::new(left),
149            right: Box::new(right),
150            ..query
151        })
152    }
153}
154
155/// Push projections down to eliminate columns early
156struct ProjectionPushdownPass;
157
158impl OptimizationPass for ProjectionPushdownPass {
159    fn name(&self) -> &str {
160        "ProjectionPushdown"
161    }
162
163    fn apply(&self, query: QueryExpr) -> QueryExpr {
164        match query {
165            QueryExpr::Join(jq) => {
166                // Analyze which columns are actually needed
167                let left = self.apply(*jq.left);
168                let right = self.apply(*jq.right);
169
170                QueryExpr::Join(JoinQuery {
171                    left: Box::new(left),
172                    right: Box::new(right),
173                    ..jq
174                })
175            }
176            QueryExpr::Table(tq) => {
177                // Table projections already use specific column projections
178                // No transformation needed - already efficient
179                QueryExpr::Table(tq)
180            }
181            other => other,
182        }
183    }
184
185    fn benefit(&self) -> u32 {
186        80 // High priority - reduces memory
187    }
188}
189
190/// Reorder joins for optimal execution
191struct JoinReorderingPass;
192
193impl OptimizationPass for JoinReorderingPass {
194    fn name(&self) -> &str {
195        "JoinReordering"
196    }
197
198    fn apply(&self, query: QueryExpr) -> QueryExpr {
199        match query {
200            QueryExpr::Join(jq) => {
201                // For now, just ensure smaller table is on build side
202                // Real IDP algorithm would enumerate join orderings
203                self.optimize_join_order(jq)
204            }
205            other => other,
206        }
207    }
208
209    fn benefit(&self) -> u32 {
210        90 // High priority - join order greatly affects cost
211    }
212}
213
214impl JoinReorderingPass {
215    fn optimize_join_order(&self, query: JoinQuery) -> QueryExpr {
216        // Estimate cardinalities
217        let left_size = Self::estimate_size(&query.left);
218        let right_size = Self::estimate_size(&query.right);
219
220        // For hash join, smaller table should be build side (left)
221        if left_size > right_size && query.join_type == JoinType::Inner {
222            // Swap left and right
223            let JoinQuery {
224                left,
225                right,
226                join_type,
227                on,
228                filter,
229                order_by,
230                limit,
231                offset,
232                return_items,
233                return_,
234            } = query;
235            QueryExpr::Join(JoinQuery {
236                left: right,
237                right: left,
238                join_type,
239                on: swap_condition(on),
240                filter,
241                order_by,
242                limit,
243                offset,
244                return_items,
245                return_,
246            })
247        } else {
248            QueryExpr::Join(query)
249        }
250    }
251
252    fn estimate_size(query: &QueryExpr) -> f64 {
253        match query {
254            QueryExpr::Table(tq) => {
255                let base = 1000.0;
256                if effective_table_filter(tq).is_some() {
257                    base * 0.1
258                } else if tq.limit.is_some() {
259                    tq.limit.unwrap() as f64
260                } else {
261                    base
262                }
263            }
264            QueryExpr::Graph(_) => 100.0,
265            QueryExpr::Join(jq) => {
266                Self::estimate_size(&jq.left) * Self::estimate_size(&jq.right) * 0.1
267            }
268            QueryExpr::Path(_) => 10.0,
269            QueryExpr::Vector(vq) => {
270                // Vector search returns k results
271                if effective_vector_filter(vq).is_some() {
272                    (vq.k as f64).min(100.0)
273                } else {
274                    vq.k as f64
275                }
276            }
277            QueryExpr::Hybrid(hq) => {
278                // Hybrid query combines structured and vector results
279                let structured_size = Self::estimate_size(&hq.structured);
280                let vector_size = hq.vector.k as f64;
281                // Fusion typically reduces to min of both, limited by limit
282                let base = structured_size.min(vector_size);
283                hq.limit.map(|l| base.min(l as f64)).unwrap_or(base)
284            }
285            // DML/DDL/Command statements return minimal result sets
286            QueryExpr::Insert(_)
287            | QueryExpr::Update(_)
288            | QueryExpr::Delete(_)
289            | QueryExpr::CreateTable(_)
290            | QueryExpr::DropTable(_)
291            | QueryExpr::DropGraph(_)
292            | QueryExpr::DropVector(_)
293            | QueryExpr::DropDocument(_)
294            | QueryExpr::DropKv(_)
295            | QueryExpr::DropCollection(_)
296            | QueryExpr::Truncate(_)
297            | QueryExpr::AlterTable(_)
298            | QueryExpr::GraphCommand(_)
299            | QueryExpr::SearchCommand(_)
300            | QueryExpr::CreateIndex(_)
301            | QueryExpr::DropIndex(_)
302            | QueryExpr::ProbabilisticCommand(_)
303            | QueryExpr::Ask(_)
304            | QueryExpr::SetConfig { .. }
305            | QueryExpr::ShowConfig { .. }
306            | QueryExpr::SetSecret { .. }
307            | QueryExpr::DeleteSecret { .. }
308            | QueryExpr::ShowSecrets { .. }
309            | QueryExpr::SetTenant(_)
310            | QueryExpr::ShowTenant
311            | QueryExpr::CreateTimeSeries(_)
312            | QueryExpr::DropTimeSeries(_)
313            | QueryExpr::CreateQueue(_)
314            | QueryExpr::AlterQueue(_)
315            | QueryExpr::DropQueue(_)
316            | QueryExpr::QueueSelect(_)
317            | QueryExpr::QueueCommand(_)
318            | QueryExpr::KvCommand(_)
319            | QueryExpr::ConfigCommand(_)
320            | QueryExpr::CreateTree(_)
321            | QueryExpr::DropTree(_)
322            | QueryExpr::TreeCommand(_)
323            | QueryExpr::ExplainAlter(_)
324            | QueryExpr::TransactionControl(_)
325            | QueryExpr::MaintenanceCommand(_)
326            | QueryExpr::CreateSchema(_)
327            | QueryExpr::DropSchema(_)
328            | QueryExpr::CreateSequence(_)
329            | QueryExpr::DropSequence(_)
330            | QueryExpr::CopyFrom(_)
331            | QueryExpr::CreateView(_)
332            | QueryExpr::DropView(_)
333            | QueryExpr::RefreshMaterializedView(_)
334            | QueryExpr::CreatePolicy(_)
335            | QueryExpr::DropPolicy(_)
336            | QueryExpr::CreateServer(_)
337            | QueryExpr::DropServer(_)
338            | QueryExpr::CreateForeignTable(_)
339            | QueryExpr::DropForeignTable(_)
340            | QueryExpr::Grant(_)
341            | QueryExpr::Revoke(_)
342            | QueryExpr::AlterUser(_)
343            | QueryExpr::CreateIamPolicy { .. }
344            | QueryExpr::DropIamPolicy { .. }
345            | QueryExpr::AttachPolicy { .. }
346            | QueryExpr::DetachPolicy { .. }
347            | QueryExpr::ShowPolicies { .. }
348            | QueryExpr::ShowEffectivePermissions { .. }
349            | QueryExpr::SimulatePolicy { .. }
350            | QueryExpr::CreateMigration(_)
351            | QueryExpr::ApplyMigration(_)
352            | QueryExpr::RollbackMigration(_)
353            | QueryExpr::ExplainMigration(_)
354            | QueryExpr::EventsBackfill(_)
355            | QueryExpr::EventsBackfillStatus { .. } => 1.0,
356        }
357    }
358}
359
360/// Select optimal indexes for table scans.
361///
362/// Analyzes filter predicates and annotates the query plan with index hints:
363/// - Equality predicates (`col = value`) → prefer Hash index if available
364/// - Low-cardinality equality → prefer Bitmap index
365/// - Range predicates (`col > value`, `BETWEEN`) → prefer B-tree
366/// - Spatial predicates → prefer R-tree
367///
368/// The hints are stored in the TableQuery's alias field as a prefix
369/// (e.g., `__idx:hash:col_name`) which the executor can read to skip
370/// full scans. This is a lightweight approach that avoids adding new
371/// fields to the AST while enabling index-aware execution.
372struct IndexSelectionPass;
373
374impl OptimizationPass for IndexSelectionPass {
375    fn name(&self) -> &str {
376        "IndexSelection"
377    }
378
379    fn apply(&self, query: QueryExpr) -> QueryExpr {
380        match query {
381            QueryExpr::Table(mut tq) => {
382                if let Some(filter) = effective_table_filter(&tq).as_ref() {
383                    if let Some(hint) = Self::analyze_filter(filter) {
384                        // Store index hint in expand metadata for executor
385                        let expand = tq.expand.get_or_insert_with(Default::default);
386                        expand.index_hint = Some(hint);
387                    }
388                }
389                QueryExpr::Table(tq)
390            }
391            other => other,
392        }
393    }
394
395    fn benefit(&self) -> u32 {
396        70
397    }
398}
399
400impl IndexSelectionPass {
401    /// Analyze a filter predicate and return the best index hint
402    fn analyze_filter(filter: &crate::storage::query::ast::Filter) -> Option<IndexHint> {
403        match filter {
404            // Equality on a single column → Hash index candidate
405            crate::storage::query::ast::Filter::Compare { field, op, .. }
406                if *op == crate::storage::query::ast::CompareOp::Eq =>
407            {
408                let col = Self::field_name(field);
409                Some(IndexHint {
410                    method: IndexHintMethod::Hash,
411                    column: col,
412                })
413            }
414            // Range predicates → B-tree candidate
415            crate::storage::query::ast::Filter::Compare {
416                field,
417                op:
418                    crate::storage::query::ast::CompareOp::Lt
419                    | crate::storage::query::ast::CompareOp::Le
420                    | crate::storage::query::ast::CompareOp::Gt
421                    | crate::storage::query::ast::CompareOp::Ge,
422                ..
423            } => {
424                let col = Self::field_name(field);
425                Some(IndexHint {
426                    method: IndexHintMethod::BTree,
427                    column: col,
428                })
429            }
430            // BETWEEN → B-tree candidate
431            crate::storage::query::ast::Filter::Between { field, .. } => {
432                let col = Self::field_name(field);
433                Some(IndexHint {
434                    method: IndexHintMethod::BTree,
435                    column: col,
436                })
437            }
438            // IN with few values → Bitmap candidate
439            crate::storage::query::ast::Filter::In { field, values } if values.len() <= 10 => {
440                let col = Self::field_name(field);
441                Some(IndexHint {
442                    method: IndexHintMethod::Bitmap,
443                    column: col,
444                })
445            }
446            // AND: pick the most selective hint from left or right
447            crate::storage::query::ast::Filter::And(left, right) => {
448                Self::analyze_filter(left).or_else(|| Self::analyze_filter(right))
449            }
450            _ => None,
451        }
452    }
453
454    fn field_name(field: &crate::storage::query::ast::FieldRef) -> String {
455        match field {
456            crate::storage::query::ast::FieldRef::TableColumn { column, .. } => column.clone(),
457            crate::storage::query::ast::FieldRef::NodeProperty { property, .. } => property.clone(),
458            crate::storage::query::ast::FieldRef::EdgeProperty { property, .. } => property.clone(),
459            crate::storage::query::ast::FieldRef::NodeId { alias } => {
460                format!("{}.id", alias)
461            }
462        }
463    }
464}
465
466/// Hint about which index method to prefer for a query
467#[derive(Debug, Clone)]
468pub struct IndexHint {
469    /// Preferred index method
470    pub method: IndexHintMethod,
471    /// Column the index applies to
472    pub column: String,
473}
474
475/// Which index method the optimizer recommends
476#[derive(Debug, Clone, Copy, PartialEq, Eq)]
477pub enum IndexHintMethod {
478    Hash,
479    BTree,
480    Bitmap,
481    Spatial,
482}
483
484/// Push LIMIT down through operations
485struct LimitPushdownPass;
486
487impl OptimizationPass for LimitPushdownPass {
488    fn name(&self) -> &str {
489        "LimitPushdown"
490    }
491
492    fn apply(&self, query: QueryExpr) -> QueryExpr {
493        match query {
494            QueryExpr::Join(jq) => {
495                // Can push limit through certain joins
496                let left = self.apply(*jq.left);
497                let right = self.apply(*jq.right);
498
499                QueryExpr::Join(JoinQuery {
500                    left: Box::new(left),
501                    right: Box::new(right),
502                    ..jq
503                })
504            }
505            other => other,
506        }
507    }
508
509    fn benefit(&self) -> u32 {
510        60
511    }
512}
513
514// =============================================================================
515// Helper Functions
516// =============================================================================
517
518fn swap_condition(
519    condition: crate::storage::query::ast::JoinCondition,
520) -> crate::storage::query::ast::JoinCondition {
521    crate::storage::query::ast::JoinCondition {
522        left_field: condition.right_field,
523        right_field: condition.left_field,
524    }
525}
526
527#[cfg(test)]
528mod tests {
529    use super::*;
530    use crate::storage::query::ast::{
531        DistanceMetric, FieldRef, FusionStrategy, JoinCondition, Projection, TableQuery,
532    };
533
534    fn make_table_query(name: &str) -> QueryExpr {
535        QueryExpr::Table(TableQuery {
536            table: name.to_string(),
537            source: None,
538            alias: Some(name.to_string()),
539            select_items: Vec::new(),
540            columns: vec![Projection::All],
541            where_expr: None,
542            filter: None,
543            group_by_exprs: Vec::new(),
544            group_by: Vec::new(),
545            having_expr: None,
546            having: None,
547            order_by: vec![],
548            limit: None,
549            offset: None,
550            expand: None,
551            as_of: None,
552        })
553    }
554
555    #[test]
556    fn test_optimizer_applies_passes() {
557        let optimizer = QueryOptimizer::new();
558        let query = make_table_query("hosts");
559
560        let (optimized, passes) = optimizer.optimize(query);
561        // Should at least attempt the passes
562        assert!(matches!(optimized, QueryExpr::Table(_)));
563    }
564
565    #[test]
566    fn test_join_reordering() {
567        let optimizer = QueryOptimizer::new();
568
569        let small = QueryExpr::Table(TableQuery {
570            table: "small".to_string(),
571            source: None,
572            alias: None,
573            select_items: Vec::new(),
574            columns: vec![Projection::All],
575            where_expr: None,
576            filter: None,
577            group_by_exprs: Vec::new(),
578            group_by: Vec::new(),
579            having_expr: None,
580            having: None,
581            order_by: vec![],
582            limit: Some(10), // Small table
583            offset: None,
584            expand: None,
585            as_of: None,
586        });
587
588        let large = QueryExpr::Table(TableQuery {
589            table: "large".to_string(),
590            source: None,
591            alias: None,
592            select_items: Vec::new(),
593            columns: vec![Projection::All],
594            where_expr: None,
595            filter: None,
596            group_by_exprs: Vec::new(),
597            group_by: Vec::new(),
598            having_expr: None,
599            having: None,
600            order_by: vec![],
601            limit: None, // Large table
602            offset: None,
603            expand: None,
604            as_of: None,
605        });
606
607        let join = QueryExpr::Join(JoinQuery {
608            left: Box::new(large.clone()),
609            right: Box::new(small.clone()),
610            join_type: JoinType::Inner,
611            on: JoinCondition {
612                left_field: FieldRef::TableColumn {
613                    table: "large".to_string(),
614                    column: "id".to_string(),
615                },
616                right_field: FieldRef::TableColumn {
617                    table: "small".to_string(),
618                    column: "id".to_string(),
619                },
620            },
621            filter: None,
622            order_by: Vec::new(),
623            limit: None,
624            offset: None,
625            return_items: Vec::new(),
626            return_: Vec::new(),
627        });
628
629        let (optimized, passes) = optimizer.optimize(join);
630
631        // Should have applied JoinReordering
632        if let QueryExpr::Join(jq) = optimized {
633            // Small table should now be on left (build side)
634            if let QueryExpr::Table(left) = *jq.left {
635                assert_eq!(left.table, "small");
636            }
637        }
638    }
639}