reddb-io-rql 1.15.0

RedDB Query Language (RQL) front-end + conformance authority (ADR 0053). Owns the sqllogictest-format conformance suite whose truth comes from the public SQLite corpus; depends only on reddb-io-types.
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
//! Filter Ranking
//!
//! Optimizes filter execution order based on selectivity and cost.
//!
//! The key insight is that we want to execute filters that:
//! 1. Eliminate the most rows (low selectivity)
//! 2. Are cheap to evaluate (low cost)
//!
//! We rank by `selectivity / cost` - lower is better.

use super::stats::TableStats;
use std::cmp::Ordering;
use std::fmt::Debug;

/// Filter expression (simplified for ranking)
#[derive(Debug, Clone)]
pub enum FilterExpr {
    /// Equality: column = value
    Eq { column: String, value: FilterValue },
    /// Not equal: column != value
    Ne { column: String, value: FilterValue },
    /// Less than: column < value
    Lt { column: String, value: FilterValue },
    /// Less than or equal: column <= value
    Le { column: String, value: FilterValue },
    /// Greater than: column > value
    Gt { column: String, value: FilterValue },
    /// Greater than or equal: column >= value
    Ge { column: String, value: FilterValue },
    /// Range: lower <= column <= upper
    Between {
        column: String,
        lower: FilterValue,
        upper: FilterValue,
    },
    /// IN list: column IN (v1, v2, ...)
    In {
        column: String,
        values: Vec<FilterValue>,
    },
    /// LIKE pattern: column LIKE 'pattern%'
    Like { column: String, pattern: String },
    /// IS NULL: column IS NULL
    IsNull { column: String },
    /// IS NOT NULL: column IS NOT NULL
    IsNotNull { column: String },
    /// AND: expr1 AND expr2
    And(Box<FilterExpr>, Box<FilterExpr>),
    /// OR: expr1 OR expr2
    Or(Box<FilterExpr>, Box<FilterExpr>),
    /// NOT: NOT expr
    Not(Box<FilterExpr>),
    /// Function call (expensive)
    Function { name: String, args: Vec<FilterExpr> },
    /// Subquery (very expensive)
    Subquery { id: u64 },
    /// True literal (passes all)
    True,
    /// False literal (passes none)
    False,
}

/// Filter value for comparison
#[derive(Debug, Clone, PartialEq)]
pub enum FilterValue {
    Null,
    Bool(bool),
    Int(i64),
    Float(f64),
    String(String),
    Bytes(Vec<u8>),
}

impl FilterValue {
    /// Get numeric value for range estimation
    pub fn as_f64(&self) -> Option<f64> {
        match self {
            FilterValue::Int(i) => Some(*i as f64),
            FilterValue::Float(f) => Some(*f),
            _ => None,
        }
    }
}

/// Cost estimates for different operations
#[derive(Debug, Clone)]
pub struct CostModel {
    /// Base cost of any comparison
    pub base_compare: f64,
    /// Cost per byte for string comparison
    pub string_per_byte: f64,
    /// Cost of function call
    pub function_call: f64,
    /// Cost of subquery execution
    pub subquery: f64,
    /// Cost of regex/LIKE pattern matching
    pub pattern_match: f64,
    /// Cost of NULL check
    pub null_check: f64,
    /// Cost of IN list (per element)
    pub in_per_element: f64,
}

impl Default for CostModel {
    fn default() -> Self {
        Self {
            base_compare: 1.0,
            string_per_byte: 0.01,
            function_call: 10.0,
            subquery: 1000.0,
            pattern_match: 5.0,
            null_check: 0.5,
            in_per_element: 1.0,
        }
    }
}

/// Selectivity estimates
#[derive(Debug, Clone)]
pub struct SelectivityModel {
    /// Default selectivity for equality
    pub default_equality: f64,
    /// Default selectivity for inequality
    pub default_inequality: f64,
    /// Default selectivity for range
    pub default_range: f64,
    /// Default selectivity for LIKE
    pub default_like: f64,
    /// Default selectivity for IS NULL
    pub default_is_null: f64,
}

impl Default for SelectivityModel {
    fn default() -> Self {
        Self {
            default_equality: 0.01,   // 1% of rows match
            default_inequality: 0.99, // 99% of rows match
            default_range: 0.25,      // 25% of rows match
            default_like: 0.10,       // 10% of rows match
            default_is_null: 0.05,    // 5% of rows are NULL
        }
    }
}

/// Ranking configuration
#[derive(Debug, Clone)]
pub struct RankingConfig {
    /// Cost model
    pub cost_model: CostModel,
    /// Selectivity model
    pub selectivity_model: SelectivityModel,
    /// Use column statistics if available
    pub use_statistics: bool,
    /// Minimum selectivity (prevent division issues)
    pub min_selectivity: f64,
    /// Maximum cost (cap outliers)
    pub max_cost: f64,
}

impl Default for RankingConfig {
    fn default() -> Self {
        Self {
            cost_model: CostModel::default(),
            selectivity_model: SelectivityModel::default(),
            use_statistics: true,
            min_selectivity: 0.0001,
            max_cost: 10000.0,
        }
    }
}

/// A filter with its ranking score
#[derive(Debug, Clone)]
pub struct RankedFilter {
    /// The filter expression
    pub filter: FilterExpr,
    /// Estimated selectivity (0.0 - 1.0)
    pub selectivity: f64,
    /// Estimated evaluation cost
    pub cost: f64,
    /// Ranking score (lower is better)
    pub score: f64,
    /// Original position in input
    pub original_index: usize,
}

impl RankedFilter {
    /// Create new ranked filter
    pub fn new(filter: FilterExpr, selectivity: f64, cost: f64, original_index: usize) -> Self {
        let score = if cost > 0.0 {
            selectivity / cost
        } else {
            selectivity
        };

        Self {
            filter,
            selectivity,
            cost,
            score,
            original_index,
        }
    }
}

impl PartialEq for RankedFilter {
    fn eq(&self, other: &Self) -> bool {
        self.score == other.score
    }
}

impl Eq for RankedFilter {}

impl PartialOrd for RankedFilter {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for RankedFilter {
    fn cmp(&self, other: &Self) -> Ordering {
        self.score
            .partial_cmp(&other.score)
            .unwrap_or(Ordering::Equal)
    }
}

/// Filter ranker
pub struct FilterRanker {
    /// Configuration
    config: RankingConfig,
    /// Table statistics (optional)
    table_stats: Option<TableStats>,
}

impl FilterRanker {
    /// Create new filter ranker
    pub fn new(config: RankingConfig) -> Self {
        Self {
            config,
            table_stats: None,
        }
    }

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

    /// Set table statistics
    pub fn with_stats(mut self, stats: TableStats) -> Self {
        self.table_stats = Some(stats);
        self
    }

    /// Estimate selectivity of a filter
    pub fn estimate_selectivity(&self, filter: &FilterExpr) -> f64 {
        let sel = match filter {
            FilterExpr::Eq { column, value } => self.estimate_equality_selectivity(column, value),
            FilterExpr::Ne { column, value } => {
                1.0 - self.estimate_equality_selectivity(column, value)
            }
            FilterExpr::Lt { column, value } | FilterExpr::Le { column, value } => {
                self.estimate_range_selectivity(column, None, Some(value))
            }
            FilterExpr::Gt { column, value } | FilterExpr::Ge { column, value } => {
                self.estimate_range_selectivity(column, Some(value), None)
            }
            FilterExpr::Between {
                column,
                lower,
                upper,
            } => self.estimate_range_selectivity(column, Some(lower), Some(upper)),
            FilterExpr::In { column, values } => self.estimate_in_selectivity(column, values),
            FilterExpr::Like { column, pattern } => self.estimate_like_selectivity(column, pattern),
            FilterExpr::IsNull { column } => self.estimate_null_selectivity(column, true),
            FilterExpr::IsNotNull { column } => self.estimate_null_selectivity(column, false),
            FilterExpr::And(left, right) => {
                // Assume independence
                self.estimate_selectivity(left) * self.estimate_selectivity(right)
            }
            FilterExpr::Or(left, right) => {
                // P(A or B) = P(A) + P(B) - P(A and B)
                let sel_a = self.estimate_selectivity(left);
                let sel_b = self.estimate_selectivity(right);
                sel_a + sel_b - (sel_a * sel_b)
            }
            FilterExpr::Not(inner) => 1.0 - self.estimate_selectivity(inner),
            FilterExpr::Function { .. } => self.config.selectivity_model.default_equality,
            FilterExpr::Subquery { .. } => self.config.selectivity_model.default_equality,
            FilterExpr::True => 1.0,
            FilterExpr::False => 0.0,
        };

        sel.max(self.config.min_selectivity)
    }

    /// Estimate cost of evaluating a filter
    pub fn estimate_cost(&self, filter: &FilterExpr) -> f64 {
        let cost = match filter {
            FilterExpr::Eq { value, .. } | FilterExpr::Ne { value, .. } => {
                self.value_compare_cost(value)
            }
            FilterExpr::Lt { value, .. }
            | FilterExpr::Le { value, .. }
            | FilterExpr::Gt { value, .. }
            | FilterExpr::Ge { value, .. } => self.value_compare_cost(value),
            FilterExpr::Between { lower, upper, .. } => {
                self.value_compare_cost(lower) + self.value_compare_cost(upper)
            }
            FilterExpr::In { values, .. } => {
                self.config.cost_model.in_per_element * values.len() as f64
            }
            FilterExpr::Like { pattern, .. } => {
                self.config.cost_model.pattern_match
                    + self.config.cost_model.string_per_byte * pattern.len() as f64
            }
            FilterExpr::IsNull { .. } | FilterExpr::IsNotNull { .. } => {
                self.config.cost_model.null_check
            }
            FilterExpr::And(left, right) | FilterExpr::Or(left, right) => {
                self.estimate_cost(left) + self.estimate_cost(right)
            }
            FilterExpr::Not(inner) => self.estimate_cost(inner),
            FilterExpr::Function { args, .. } => {
                let arg_cost: f64 = args.iter().map(|a| self.estimate_cost(a)).sum();
                self.config.cost_model.function_call + arg_cost
            }
            FilterExpr::Subquery { .. } => self.config.cost_model.subquery,
            FilterExpr::True | FilterExpr::False => 0.0,
        };

        cost.min(self.config.max_cost)
    }

    /// Rank a list of filters
    pub fn rank(&self, filters: Vec<FilterExpr>) -> Vec<RankedFilter> {
        let mut ranked: Vec<RankedFilter> = filters
            .into_iter()
            .enumerate()
            .map(|(i, f)| {
                let selectivity = self.estimate_selectivity(&f);
                let cost = self.estimate_cost(&f);
                RankedFilter::new(f, selectivity, cost, i)
            })
            .collect();

        // Sort by score (lower is better)
        ranked.sort();

        ranked
    }

    /// Reorder filters for optimal execution
    pub fn reorder(&self, filters: Vec<FilterExpr>) -> Vec<FilterExpr> {
        self.rank(filters).into_iter().map(|r| r.filter).collect()
    }

    /// Estimate equality selectivity
    fn estimate_equality_selectivity(&self, column: &str, _value: &FilterValue) -> f64 {
        if self.config.use_statistics {
            if let Some(ref stats) = self.table_stats {
                if let Some(col_stats) = stats.get_column(column) {
                    // Use NDV (number of distinct values) for selectivity
                    if col_stats.ndv > 0 {
                        return 1.0 / col_stats.ndv as f64;
                    }
                }
            }
        }

        self.config.selectivity_model.default_equality
    }

    /// Estimate range selectivity
    fn estimate_range_selectivity(
        &self,
        column: &str,
        lower: Option<&FilterValue>,
        upper: Option<&FilterValue>,
    ) -> f64 {
        if self.config.use_statistics {
            if let Some(ref stats) = self.table_stats {
                if let Some(col_stats) = stats.get_column(column) {
                    // Use min/max for range estimation
                    if let (Some(min), Some(max)) = (col_stats.min_value, col_stats.max_value) {
                        if max > min {
                            let range = max - min;
                            let lower_bound = lower.and_then(|v| v.as_f64()).unwrap_or(min);
                            let upper_bound = upper.and_then(|v| v.as_f64()).unwrap_or(max);

                            let fraction = (upper_bound - lower_bound) / range;
                            return fraction.clamp(0.0, 1.0);
                        }
                    }
                }
            }
        }

        self.config.selectivity_model.default_range
    }

    /// Estimate IN selectivity
    fn estimate_in_selectivity(&self, column: &str, values: &[FilterValue]) -> f64 {
        let single_sel = self.estimate_equality_selectivity(column, &values[0]);
        // Assume values are distinct, so selectivity is additive
        (single_sel * values.len() as f64).min(1.0)
    }

    /// Estimate LIKE selectivity
    fn estimate_like_selectivity(&self, _column: &str, pattern: &str) -> f64 {
        // Prefix matching is more selective
        if !pattern.starts_with('%') && pattern.ends_with('%') {
            // Prefix match: fairly selective
            self.config.selectivity_model.default_like * 0.5
        } else if pattern.starts_with('%') && pattern.ends_with('%') {
            // Contains: less selective
            self.config.selectivity_model.default_like * 2.0
        } else {
            self.config.selectivity_model.default_like
        }
    }

    /// Estimate NULL selectivity
    fn estimate_null_selectivity(&self, column: &str, is_null: bool) -> f64 {
        if self.config.use_statistics {
            if let Some(ref stats) = self.table_stats {
                if let Some(col_stats) = stats.get_column(column) {
                    let null_fraction = col_stats.null_fraction;
                    return if is_null {
                        null_fraction
                    } else {
                        1.0 - null_fraction
                    };
                }
            }
        }

        if is_null {
            self.config.selectivity_model.default_is_null
        } else {
            1.0 - self.config.selectivity_model.default_is_null
        }
    }

    /// Calculate value comparison cost
    fn value_compare_cost(&self, value: &FilterValue) -> f64 {
        match value {
            FilterValue::String(s) => {
                self.config.cost_model.base_compare
                    + self.config.cost_model.string_per_byte * s.len() as f64
            }
            FilterValue::Bytes(b) => {
                self.config.cost_model.base_compare
                    + self.config.cost_model.string_per_byte * b.len() as f64
            }
            _ => self.config.cost_model.base_compare,
        }
    }

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

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

#[cfg(test)]
mod tests {
    use super::*;
    use crate::optimizer::stats::ColumnStats;

    #[test]
    fn test_simple_ranking() {
        let ranker = FilterRanker::with_defaults();

        let filters = vec![
            FilterExpr::Eq {
                column: "id".to_string(),
                value: FilterValue::Int(42),
            },
            FilterExpr::Like {
                column: "name".to_string(),
                pattern: "%test%".to_string(),
            },
            FilterExpr::IsNull {
                column: "deleted_at".to_string(),
            },
        ];

        let ranked = ranker.rank(filters);

        // Ranking is by score = selectivity/cost (lower is better)
        // Eq: 0.01/1.0 = 0.01 (first - lowest selectivity per cost)
        // Like: ~0.2/5.06 ≈ 0.04 (second)
        // IsNull: 0.05/0.5 = 0.1 (third - highest score)
        assert!(ranked[0].score < ranked[1].score);
        assert!(ranked[1].score < ranked[2].score);
        assert!(matches!(ranked[0].filter, FilterExpr::Eq { .. }));
    }

    #[test]
    fn test_selectivity_estimation() {
        let ranker = FilterRanker::with_defaults();

        let eq_sel = ranker.estimate_selectivity(&FilterExpr::Eq {
            column: "x".to_string(),
            value: FilterValue::Int(1),
        });

        let ne_sel = ranker.estimate_selectivity(&FilterExpr::Ne {
            column: "x".to_string(),
            value: FilterValue::Int(1),
        });

        // ne should be 1 - eq
        assert!((eq_sel + ne_sel - 1.0).abs() < 0.0001);

        // AND should multiply
        let and_sel = ranker.estimate_selectivity(&FilterExpr::And(
            Box::new(FilterExpr::Eq {
                column: "x".to_string(),
                value: FilterValue::Int(1),
            }),
            Box::new(FilterExpr::Eq {
                column: "y".to_string(),
                value: FilterValue::Int(2),
            }),
        ));

        assert!((and_sel - eq_sel * eq_sel).abs() < 0.0001);
    }

    #[test]
    fn test_cost_estimation() {
        let ranker = FilterRanker::with_defaults();

        // NULL check should be cheap
        let null_cost = ranker.estimate_cost(&FilterExpr::IsNull {
            column: "x".to_string(),
        });

        // String comparison should be more expensive
        let string_cost = ranker.estimate_cost(&FilterExpr::Eq {
            column: "x".to_string(),
            value: FilterValue::String("a long string value".to_string()),
        });

        // Subquery should be very expensive
        let subquery_cost = ranker.estimate_cost(&FilterExpr::Subquery { id: 1 });

        assert!(null_cost < string_cost);
        assert!(string_cost < subquery_cost);
    }

    #[test]
    fn test_with_statistics() {
        let mut stats = TableStats::new("test".to_string(), 10000);
        stats.add_column(ColumnStats {
            name: "status".to_string(),
            ndv: 5, // 5 distinct values
            null_fraction: 0.0,
            min_value: None,
            max_value: None,
        });

        let ranker = FilterRanker::with_defaults().with_stats(stats);

        let sel = ranker.estimate_selectivity(&FilterExpr::Eq {
            column: "status".to_string(),
            value: FilterValue::String("active".to_string()),
        });

        // With NDV=5, selectivity should be ~0.2
        assert!((sel - 0.2).abs() < 0.01);
    }

    #[test]
    fn test_reorder() {
        let ranker = FilterRanker::with_defaults();

        let filters = vec![
            FilterExpr::Subquery { id: 1 }, // Very expensive but very selective
            FilterExpr::IsNull {
                column: "x".to_string(),
            }, // Cheap but less selective
            FilterExpr::Function {
                name: "expensive_fn".to_string(),
                args: vec![],
            }, // Expensive
        ];

        let reordered = ranker.reorder(filters);

        // Ranking by score = selectivity/cost (lower is better):
        // Subquery: 0.01/1000 = 0.00001 (first - best score due to high cost denominator)
        // Function: 0.01/10 = 0.001 (second)
        // IsNull: 0.05/0.5 = 0.1 (last - worst score)
        assert!(matches!(reordered[0], FilterExpr::Subquery { .. }));
        assert!(matches!(reordered[1], FilterExpr::Function { .. }));
        assert!(matches!(reordered[2], FilterExpr::IsNull { .. }));
    }

    #[test]
    fn test_in_selectivity() {
        let ranker = FilterRanker::with_defaults();

        let in_1 = ranker.estimate_selectivity(&FilterExpr::In {
            column: "x".to_string(),
            values: vec![FilterValue::Int(1)],
        });

        let in_5 = ranker.estimate_selectivity(&FilterExpr::In {
            column: "x".to_string(),
            values: vec![
                FilterValue::Int(1),
                FilterValue::Int(2),
                FilterValue::Int(3),
                FilterValue::Int(4),
                FilterValue::Int(5),
            ],
        });

        // IN with more values should have higher selectivity
        assert!(in_5 > in_1);
    }

    #[test]
    fn test_like_patterns() {
        let ranker = FilterRanker::with_defaults();

        let prefix = ranker.estimate_selectivity(&FilterExpr::Like {
            column: "x".to_string(),
            pattern: "test%".to_string(),
        });

        let contains = ranker.estimate_selectivity(&FilterExpr::Like {
            column: "x".to_string(),
            pattern: "%test%".to_string(),
        });

        // Prefix match should be more selective (lower)
        assert!(prefix < contains);
    }
}