Skip to main content

citadel_sql/
planner.rs

1//! Query planner: chooses between seq scan, PK lookup, or index scan.
2
3use crate::encoding::encode_composite_key;
4use crate::parser::{BinOp, Expr};
5use crate::types::{IndexDef, IndexKey, IndexKind, InvertedKind, TableSchema, Value};
6
7/// Canonical form of an expression for symbolic-equivalence matching against expression indexes.
8/// Strips table qualifiers, lowercases identifiers and function names, sorts commutative operands.
9#[derive(Debug, Clone, PartialEq, Eq)]
10enum CanonicalExpr {
11    Literal(String),
12    Column(String),
13    Function {
14        name: String,
15        args: Vec<CanonicalExpr>,
16    },
17    BinaryOp {
18        op: BinOp,
19        operands: Vec<CanonicalExpr>,
20    },
21    UnaryOp {
22        op: crate::parser::UnaryOp,
23        operand: Box<CanonicalExpr>,
24    },
25    Cast {
26        expr: Box<CanonicalExpr>,
27        data_type: crate::types::DataType,
28    },
29    Other(String),
30}
31
32fn canonicalize(expr: &Expr) -> CanonicalExpr {
33    match expr {
34        Expr::Literal(v) => CanonicalExpr::Literal(format!("{v:?}")),
35        Expr::Column(name) => CanonicalExpr::Column(name.to_ascii_lowercase()),
36        Expr::QualifiedColumn { column, .. } => CanonicalExpr::Column(column.to_ascii_lowercase()),
37        Expr::Function { name, args, .. } => {
38            let canon_args: Vec<CanonicalExpr> = args.iter().map(canonicalize).collect();
39            CanonicalExpr::Function {
40                name: name.to_ascii_lowercase(),
41                args: canon_args,
42            }
43        }
44        Expr::BinaryOp { left, op, right } => {
45            let mut operands = vec![canonicalize(left), canonicalize(right)];
46            if is_commutative(*op) {
47                operands.sort_by_key(|e| format!("{e:?}"));
48            }
49            CanonicalExpr::BinaryOp { op: *op, operands }
50        }
51        Expr::UnaryOp { op, expr: inner } => CanonicalExpr::UnaryOp {
52            op: *op,
53            operand: Box::new(canonicalize(inner)),
54        },
55        Expr::Cast {
56            expr: inner,
57            data_type,
58        } => CanonicalExpr::Cast {
59            expr: Box::new(canonicalize(inner)),
60            data_type: *data_type,
61        },
62        Expr::Collate { expr: inner, .. } => canonicalize(inner),
63        other => CanonicalExpr::Other(format!("{other:?}")),
64    }
65}
66
67fn is_commutative(op: BinOp) -> bool {
68    matches!(op, BinOp::Add | BinOp::Mul | BinOp::And | BinOp::Or)
69}
70
71#[derive(Debug, Clone)]
72pub enum ScanPlan {
73    SeqScan,
74    PkLookup {
75        pk_values: Vec<Value>,
76    },
77    PkRangeScan {
78        start_key: Vec<u8>,
79        range_conds: Vec<(BinOp, Value)>,
80        num_pk_cols: usize,
81    },
82    IndexScan {
83        index_name: String,
84        idx_table: Vec<u8>,
85        prefix: Vec<u8>,
86        num_prefix_cols: usize,
87        range_conds: Vec<(BinOp, Value)>,
88        is_unique: bool,
89        index_columns: Vec<u16>,
90    },
91    InvertedScan {
92        kind: InvertedKind,
93        idx_table: Vec<u8>,
94        column_idx: u16,
95        probe_entries: Vec<Vec<u8>>,
96        recheck_expr: Expr,
97        recheck_needed: bool,
98    },
99}
100
101struct SimplePredicate {
102    col_idx: usize,
103    op: BinOp,
104    value: Value,
105}
106
107fn flatten_and(expr: &Expr) -> Vec<&Expr> {
108    match expr {
109        Expr::BinaryOp {
110            left,
111            op: BinOp::And,
112            right,
113        } => {
114            let mut v = flatten_and(left);
115            v.extend(flatten_and(right));
116            v
117        }
118        _ => vec![expr],
119    }
120}
121
122fn is_comparison(op: BinOp) -> bool {
123    matches!(
124        op,
125        BinOp::Eq | BinOp::Lt | BinOp::LtEq | BinOp::Gt | BinOp::GtEq
126    )
127}
128
129fn is_range_op(op: BinOp) -> bool {
130    matches!(op, BinOp::Lt | BinOp::LtEq | BinOp::Gt | BinOp::GtEq)
131}
132
133fn flip_op(op: BinOp) -> BinOp {
134    match op {
135        BinOp::Lt => BinOp::Gt,
136        BinOp::LtEq => BinOp::GtEq,
137        BinOp::Gt => BinOp::Lt,
138        BinOp::GtEq => BinOp::LtEq,
139        other => other,
140    }
141}
142
143fn resolve_column_name(expr: &Expr) -> Option<&str> {
144    match expr {
145        Expr::Column(name) => Some(name.as_str()),
146        Expr::QualifiedColumn { column, .. } => Some(column.as_str()),
147        _ => None,
148    }
149}
150
151fn resolve_literal(expr: &Expr) -> Option<Value> {
152    match expr {
153        Expr::Literal(v) => Some(v.clone()),
154        Expr::Parameter(n) => crate::eval::resolve_scoped_param(*n).ok(),
155        Expr::Function { .. } | Expr::Cast { .. } => {
156            let col_map = crate::eval::ColumnMap::new(&[]);
157            let ctx = crate::eval::EvalCtx::new(&col_map, &[]);
158            crate::eval::eval_expr(expr, &ctx).ok()
159        }
160        _ => None,
161    }
162}
163
164fn extract_simple_predicate(expr: &Expr, schema: &TableSchema) -> Option<SimplePredicate> {
165    match expr {
166        Expr::BinaryOp { left, op, right } if is_comparison(*op) => {
167            if let (Some(name), Some(val)) = (resolve_column_name(left), resolve_literal(right)) {
168                let col_idx = schema.column_index(name)?;
169                return Some(SimplePredicate {
170                    col_idx,
171                    op: *op,
172                    value: val,
173                });
174            }
175            if let (Some(val), Some(name)) = (resolve_literal(left), resolve_column_name(right)) {
176                let col_idx = schema.column_index(name)?;
177                return Some(SimplePredicate {
178                    col_idx,
179                    op: flip_op(*op),
180                    value: val,
181                });
182            }
183            None
184        }
185        _ => None,
186    }
187}
188
189/// Decompose BETWEEN into two range predicates for planner use.
190fn flatten_between(expr: &Expr, schema: &TableSchema, out: &mut Vec<SimplePredicate>) {
191    match expr {
192        Expr::Between {
193            expr: col_expr,
194            low,
195            high,
196            negated: false,
197        } => {
198            if let (Some(name), Some(lo), Some(hi)) = (
199                resolve_column_name(col_expr),
200                resolve_literal(low),
201                resolve_literal(high),
202            ) {
203                if let Some(col_idx) = schema.column_index(name) {
204                    out.push(SimplePredicate {
205                        col_idx,
206                        op: BinOp::GtEq,
207                        value: lo,
208                    });
209                    out.push(SimplePredicate {
210                        col_idx,
211                        op: BinOp::LtEq,
212                        value: hi,
213                    });
214                }
215            }
216        }
217        Expr::BinaryOp {
218            left,
219            op: BinOp::And,
220            right,
221        } => {
222            flatten_between(left, schema, out);
223            flatten_between(right, schema, out);
224        }
225        _ => {}
226    }
227}
228
229pub fn plan_select(schema: &TableSchema, where_clause: &Option<Expr>) -> ScanPlan {
230    plan_select_inner(schema, where_clause, false)
231}
232
233pub fn plan_select_inverted(schema: &TableSchema, where_clause: &Option<Expr>) -> ScanPlan {
234    plan_select_inner(schema, where_clause, true)
235}
236
237fn plan_select_inner(
238    schema: &TableSchema,
239    where_clause: &Option<Expr>,
240    allow_inverted: bool,
241) -> ScanPlan {
242    let where_expr = match where_clause {
243        Some(e) => e,
244        None => return ScanPlan::SeqScan,
245    };
246
247    let predicates = flatten_and(where_expr);
248    let simple: Vec<Option<SimplePredicate>> = predicates
249        .iter()
250        .map(|p| extract_simple_predicate(p, schema))
251        .collect();
252
253    if let Some(plan) = try_pk_lookup(schema, &simple) {
254        return plan;
255    }
256
257    let mut range_preds: Vec<SimplePredicate> = simple
258        .iter()
259        .filter_map(|p| {
260            let p = p.as_ref()?;
261            if is_range_op(p.op) {
262                Some(SimplePredicate {
263                    col_idx: p.col_idx,
264                    op: p.op,
265                    value: p.value.clone(),
266                })
267            } else {
268                None
269            }
270        })
271        .collect();
272    flatten_between(where_expr, schema, &mut range_preds);
273
274    if let Some(plan) = try_pk_range_scan(schema, &range_preds) {
275        return plan;
276    }
277
278    if allow_inverted {
279        if let Some(plan) = try_inverted_scan(schema, where_expr) {
280            return plan;
281        }
282    }
283
284    if let Some(plan) = try_best_index(schema, where_expr, &simple) {
285        return plan;
286    }
287
288    ScanPlan::SeqScan
289}
290
291fn try_inverted_scan(schema: &TableSchema, where_expr: &Expr) -> Option<ScanPlan> {
292    use crate::parser::BinOp as B;
293    let (col_idx, rhs_val, op) = match where_expr {
294        Expr::BinaryOp {
295            left,
296            op: B::JsonContains,
297            right,
298        } => {
299            let name = resolve_column_name(left)?;
300            let col_idx = schema.column_index(name)? as u16;
301            let rhs = resolve_literal(right)?;
302            (col_idx, rhs, B::JsonContains)
303        }
304        Expr::BinaryOp {
305            left,
306            op: B::JsonPathMatch,
307            right,
308        } => {
309            let name = resolve_column_name(left)?;
310            let col_idx = schema.column_index(name)? as u16;
311            let rhs = resolve_literal(right)?;
312            (col_idx, rhs, B::JsonPathMatch)
313        }
314        _ => return None,
315    };
316    let idx = schema.indices.iter().find(|i| {
317        matches!(i.kind, IndexKind::Inverted(_))
318            && i.column_positions_iter()
319                .next()
320                .is_some_and(|c| c == col_idx)
321            && i.predicate_expr.is_none()
322    })?;
323    let kind = match idx.kind {
324        IndexKind::Inverted(k) => k,
325        _ => return None,
326    };
327    match (kind, op) {
328        (InvertedKind::Gin(_), B::JsonContains) => {}
329        (InvertedKind::Fts { .. }, B::JsonPathMatch) => {}
330        _ => return None,
331    }
332    let probe_entries = extract_inverted_probe(&rhs_val, kind)?;
333    if probe_entries.is_empty() {
334        return None;
335    }
336    let recheck_needed = inverted_recheck_needed(kind, &rhs_val);
337    let idx_table = TableSchema::index_table_name(&schema.name, &idx.name);
338    Some(ScanPlan::InvertedScan {
339        kind,
340        idx_table,
341        column_idx: col_idx,
342        probe_entries,
343        recheck_expr: where_expr.clone(),
344        recheck_needed,
345    })
346}
347
348fn inverted_recheck_needed(kind: InvertedKind, rhs: &Value) -> bool {
349    match kind {
350        InvertedKind::Gin(_) => true,
351        InvertedKind::Fts { .. } => match rhs {
352            Value::TsQuery(bytes) => match crate::fts::TsQueryAst::decode(bytes) {
353                Ok(ast) => !fts_ast_exact_for_index(&ast),
354                Err(_) => true,
355            },
356            _ => true,
357        },
358    }
359}
360
361fn fts_ast_exact_for_index(ast: &crate::fts::TsQueryAst) -> bool {
362    use crate::fts::TsQueryAst;
363    match ast {
364        TsQueryAst::Lexeme {
365            prefix: false,
366            weight_mask: 0,
367            ..
368        } => true,
369        TsQueryAst::Lexeme { .. } => false,
370        TsQueryAst::And(l, r) => fts_ast_exact_for_index(l) && fts_ast_exact_for_index(r),
371        _ => false,
372    }
373}
374
375pub(crate) fn fts_ast_is_pure_phrase(ast: &crate::fts::TsQueryAst) -> bool {
376    use crate::fts::TsQueryAst;
377    match ast {
378        TsQueryAst::Lexeme {
379            prefix: false,
380            weight_mask: 0,
381            ..
382        } => true,
383        TsQueryAst::Phrase { left, right, .. } => {
384            fts_ast_is_pure_phrase(left) && fts_ast_is_pure_phrase(right)
385        }
386        _ => false,
387    }
388}
389
390fn extract_inverted_probe(rhs: &Value, kind: InvertedKind) -> Option<Vec<Vec<u8>>> {
391    use crate::types::GinOpsClass;
392    match kind {
393        InvertedKind::Gin(ops) => {
394            let entries = crate::json::extract_gin_entries(rhs, ops).ok()?;
395            let filtered: Vec<Vec<u8>> = match ops {
396                GinOpsClass::JsonbOps => entries
397                    .into_iter()
398                    .filter(|e| !matches!(e.first(), Some(&0x01)))
399                    .collect(),
400                GinOpsClass::JsonbPathOps => entries,
401            };
402            Some(filtered)
403        }
404        InvertedKind::Fts { .. } => match rhs {
405            Value::TsQuery(bytes) => {
406                let ast = crate::fts::TsQueryAst::decode(bytes).ok()?;
407                let required = fts_required_lexemes(&ast)?;
408                if required.is_empty() {
409                    None
410                } else {
411                    Some(required)
412                }
413            }
414            _ => None,
415        },
416    }
417}
418
419fn fts_required_lexemes(ast: &crate::fts::TsQueryAst) -> Option<Vec<Vec<u8>>> {
420    let mut out: std::collections::BTreeSet<Vec<u8>> = std::collections::BTreeSet::new();
421    let ok = collect_required(ast, &mut out);
422    if !ok || out.is_empty() {
423        None
424    } else {
425        Some(out.into_iter().collect())
426    }
427}
428
429fn collect_required(
430    ast: &crate::fts::TsQueryAst,
431    out: &mut std::collections::BTreeSet<Vec<u8>>,
432) -> bool {
433    use crate::fts::TsQueryAst;
434    match ast {
435        TsQueryAst::Lexeme { prefix, .. } if *prefix => false,
436        TsQueryAst::Lexeme { lexeme, .. } => {
437            out.insert(lexeme.clone());
438            true
439        }
440        TsQueryAst::And(l, r) => {
441            let lo = collect_required(l, out);
442            let ro = collect_required(r, out);
443            lo || ro
444        }
445        TsQueryAst::Or(..) => false,
446        TsQueryAst::Not(_) => false,
447        TsQueryAst::Phrase { left, right, .. } => {
448            let lo = collect_required(left, out);
449            let ro = collect_required(right, out);
450            lo && ro
451        }
452    }
453}
454
455fn try_pk_range_scan(schema: &TableSchema, range_preds: &[SimplePredicate]) -> Option<ScanPlan> {
456    if schema.primary_key_columns.len() != 1 {
457        return None; // Only single-column PK for now
458    }
459    let pk_col = schema.primary_key_columns[0] as usize;
460    let conds: Vec<(BinOp, Value)> = range_preds
461        .iter()
462        .filter(|p| p.col_idx == pk_col)
463        .map(|p| (p.op, p.value.clone()))
464        .collect();
465    if conds.is_empty() {
466        return None;
467    }
468    let start_key = conds
469        .iter()
470        .filter(|(op, _)| matches!(op, BinOp::GtEq | BinOp::Gt))
471        .map(|(_, v)| encode_composite_key(std::slice::from_ref(v)))
472        .min_by(|a, b| a.cmp(b))
473        .unwrap_or_default();
474    Some(ScanPlan::PkRangeScan {
475        start_key,
476        range_conds: conds,
477        num_pk_cols: 1,
478    })
479}
480
481fn try_pk_lookup(schema: &TableSchema, predicates: &[Option<SimplePredicate>]) -> Option<ScanPlan> {
482    let pk_cols = &schema.primary_key_columns;
483    // No PK → fall through to SeqScan. An empty-key PkLookup would silently match 0 rows.
484    if pk_cols.is_empty() {
485        return None;
486    }
487    let mut pk_values: Vec<Option<Value>> = vec![None; pk_cols.len()];
488
489    for pred in predicates.iter().flatten() {
490        if pred.op == BinOp::Eq {
491            if let Some(pk_pos) = pk_cols.iter().position(|&c| c == pred.col_idx as u16) {
492                pk_values[pk_pos] = Some(pred.value.clone());
493            }
494        }
495    }
496
497    if pk_values.iter().all(|v| v.is_some()) {
498        let values: Vec<Value> = pk_values.into_iter().map(|v| v.unwrap()).collect();
499        Some(ScanPlan::PkLookup { pk_values: values })
500    } else {
501        None
502    }
503}
504
505#[derive(PartialEq, Eq, PartialOrd, Ord)]
506struct IndexScore {
507    num_equality: usize,
508    has_range: bool,
509    is_unique: bool,
510}
511
512fn try_best_index(
513    schema: &TableSchema,
514    where_expr: &Expr,
515    predicates: &[Option<SimplePredicate>],
516) -> Option<ScanPlan> {
517    let mut best_score: Option<IndexScore> = None;
518    let mut best_plan: Option<ScanPlan> = None;
519
520    let conjuncts = flatten_and(where_expr);
521    for idx in &schema.indices {
522        if !partial_predicate_implied(idx, where_expr, &conjuncts) {
523            continue;
524        }
525        if let Some((score, plan)) = try_index_scan(schema, idx, predicates) {
526            if best_score.is_none() || score > *best_score.as_ref().unwrap() {
527                best_score = Some(score);
528                best_plan = Some(plan);
529            }
530        }
531        if !idx.is_pure_column_index() {
532            if let Some((score, plan)) = try_expr_index_scan(schema, idx, &conjuncts) {
533                if best_score.is_none() || score > *best_score.as_ref().unwrap() {
534                    best_score = Some(score);
535                    best_plan = Some(plan);
536                }
537            }
538        }
539    }
540
541    best_plan
542}
543
544fn try_expr_index_scan(
545    schema: &TableSchema,
546    idx: &IndexDef,
547    conjuncts: &[&Expr],
548) -> Option<(IndexScore, ScanPlan)> {
549    // v0.16 supports only equality on the first expression key (`WHERE LOWER(email) = ?`).
550    let first_key = idx.keys.first()?;
551    let key_expr = match first_key {
552        IndexKey::Expr { expr, .. } => expr,
553        IndexKey::Column { .. } => return None,
554    };
555    let canonical_key = canonicalize(key_expr);
556
557    let mut matched: Option<Value> = None;
558    for conj in conjuncts {
559        if let Expr::BinaryOp {
560            left,
561            op: BinOp::Eq,
562            right,
563        } = conj
564        {
565            let (expr_side, value_side) = match (left.as_ref(), right.as_ref()) {
566                (Expr::Literal(v), other) | (other, Expr::Literal(v)) => (other, v.clone()),
567                _ => continue,
568            };
569            if canonicalize(expr_side) == canonical_key {
570                matched = Some(value_side);
571                break;
572            }
573        }
574    }
575
576    let value = matched?;
577    let score = IndexScore {
578        num_equality: 1,
579        has_range: false,
580        is_unique: idx.unique,
581    };
582    let prefix = encode_composite_key(&[value]);
583    let idx_table = TableSchema::index_table_name(&schema.name, &idx.name);
584    Some((
585        score,
586        ScanPlan::IndexScan {
587            index_name: idx.name.clone(),
588            idx_table,
589            prefix,
590            num_prefix_cols: 1,
591            range_conds: vec![],
592            is_unique: idx.unique,
593            index_columns: vec![],
594        },
595    ))
596}
597
598fn partial_predicate_implied(idx: &IndexDef, where_expr: &Expr, conjuncts: &[&Expr]) -> bool {
599    let Some(pred) = idx.predicate_expr.as_ref() else {
600        return true;
601    };
602    if expr_structurally_eq(pred, where_expr) {
603        return true;
604    }
605    if conjuncts.iter().any(|c| expr_structurally_eq(pred, c)) {
606        return true;
607    }
608    if let Expr::IsNotNull(target) = pred {
609        if let Expr::Column(col) = target.as_ref() {
610            return conjuncts.iter().any(|c| conjunct_proves_not_null(c, col));
611        }
612    }
613    false
614}
615
616fn expr_structurally_eq(a: &Expr, b: &Expr) -> bool {
617    format!("{a:?}") == format!("{b:?}")
618}
619
620fn conjunct_proves_not_null(expr: &Expr, col: &str) -> bool {
621    let mentions = |e: &Expr| matches!(e, Expr::Column(n) if n.eq_ignore_ascii_case(col));
622    match expr {
623        Expr::BinaryOp {
624            left,
625            op: BinOp::Eq | BinOp::NotEq | BinOp::Lt | BinOp::LtEq | BinOp::Gt | BinOp::GtEq,
626            right,
627        } => mentions(left) || mentions(right),
628        Expr::IsNotNull(inner) => mentions(inner),
629        _ => false,
630    }
631}
632
633fn try_index_scan(
634    schema: &TableSchema,
635    idx: &IndexDef,
636    predicates: &[Option<SimplePredicate>],
637) -> Option<(IndexScore, ScanPlan)> {
638    let mut used = Vec::new();
639    let mut equality_values: Vec<Value> = Vec::new();
640    let mut range_conds: Vec<(BinOp, Value)> = Vec::new();
641
642    // Expression-key indexes go through `try_expr_index_scan`, not the column path.
643    if !idx.is_pure_column_index() {
644        return None;
645    }
646    let idx_columns = idx.columns_vec();
647    for &col_idx in &idx_columns {
648        let mut found_eq = false;
649        for (i, pred) in predicates.iter().enumerate() {
650            if used.contains(&i) {
651                continue;
652            }
653            if let Some(sp) = pred {
654                if sp.col_idx == col_idx as usize && sp.op == BinOp::Eq {
655                    equality_values.push(sp.value.clone());
656                    used.push(i);
657                    found_eq = true;
658                    break;
659                }
660            }
661        }
662        if !found_eq {
663            for (i, pred) in predicates.iter().enumerate() {
664                if used.contains(&i) {
665                    continue;
666                }
667                if let Some(sp) = pred {
668                    if sp.col_idx == col_idx as usize && is_range_op(sp.op) {
669                        range_conds.push((sp.op, sp.value.clone()));
670                        used.push(i);
671                    }
672                }
673            }
674            break;
675        }
676    }
677
678    if equality_values.is_empty() && range_conds.is_empty() {
679        return None;
680    }
681
682    let score = IndexScore {
683        num_equality: equality_values.len(),
684        has_range: !range_conds.is_empty(),
685        is_unique: idx.unique,
686    };
687
688    let prefix = encode_composite_key(&equality_values);
689    let idx_table = TableSchema::index_table_name(&schema.name, &idx.name);
690
691    Some((
692        score,
693        ScanPlan::IndexScan {
694            index_name: idx.name.clone(),
695            idx_table,
696            prefix,
697            num_prefix_cols: equality_values.len(),
698            range_conds,
699            is_unique: idx.unique,
700            index_columns: idx_columns.clone(),
701        },
702    ))
703}
704
705pub fn describe_plan(plan: &ScanPlan, table_schema: &TableSchema) -> String {
706    match plan {
707        ScanPlan::SeqScan => String::new(),
708
709        ScanPlan::PkLookup { pk_values } => {
710            let pk_cols: Vec<&str> = table_schema
711                .primary_key_columns
712                .iter()
713                .map(|&idx| table_schema.columns[idx as usize].name.as_str())
714                .collect();
715            let conditions: Vec<String> = pk_cols
716                .iter()
717                .zip(pk_values.iter())
718                .map(|(col, val)| format!("{col} = {}", format_value(val)))
719                .collect();
720            format!("USING PRIMARY KEY ({})", conditions.join(", "))
721        }
722
723        ScanPlan::PkRangeScan { range_conds, .. } => {
724            let pk_col = &table_schema.columns[table_schema.primary_key_columns[0] as usize].name;
725            let conditions: Vec<String> = range_conds
726                .iter()
727                .map(|(op, val)| format!("{pk_col} {} {}", op_symbol(*op), format_value(val)))
728                .collect();
729            format!("USING PRIMARY KEY RANGE ({})", conditions.join(", "))
730        }
731
732        ScanPlan::IndexScan {
733            index_name,
734            num_prefix_cols,
735            range_conds,
736            index_columns,
737            ..
738        } => {
739            let mut conditions = Vec::new();
740            for &col in index_columns.iter().take(*num_prefix_cols) {
741                let col_idx = col as usize;
742                let col_name = &table_schema.columns[col_idx].name;
743                conditions.push(format!("{col_name} = ?"));
744            }
745            if !range_conds.is_empty() && *num_prefix_cols < index_columns.len() {
746                let col_idx = index_columns[*num_prefix_cols] as usize;
747                let col_name = &table_schema.columns[col_idx].name;
748                for (op, _) in range_conds {
749                    conditions.push(format!("{col_name} {} ?", op_symbol(*op)));
750                }
751            }
752            if conditions.is_empty() {
753                format!("USING INDEX {index_name}")
754            } else {
755                format!("USING INDEX {index_name} ({})", conditions.join(", "))
756            }
757        }
758
759        ScanPlan::InvertedScan { .. } => "USING INVERTED INDEX".to_string(),
760    }
761}
762
763fn format_value(val: &Value) -> String {
764    match val {
765        Value::Null => "NULL".into(),
766        Value::Integer(i) => i.to_string(),
767        Value::Real(f) => format!("{f}"),
768        Value::Text(s) => format!("'{s}'"),
769        Value::Blob(_) => "BLOB".into(),
770        Value::Boolean(b) => b.to_string(),
771        Value::Date(d) => format!("DATE '{}'", crate::datetime::format_date(*d)),
772        Value::Time(t) => format!("TIME '{}'", crate::datetime::format_time(*t)),
773        Value::Timestamp(t) => format!("TIMESTAMP '{}'", crate::datetime::format_timestamp(*t)),
774        Value::Interval {
775            months,
776            days,
777            micros,
778        } => format!(
779            "INTERVAL '{}'",
780            crate::datetime::format_interval(*months, *days, *micros)
781        ),
782        Value::Json(s) => format!("JSON '{s}'"),
783        Value::Jsonb(_) => "JSONB '<binary>'".into(),
784        Value::TsVector(_) => "TSVECTOR '<binary>'".into(),
785        Value::TsQuery(_) => "TSQUERY '<binary>'".into(),
786        Value::Array(_) => val.to_string(),
787    }
788}
789
790fn op_symbol(op: BinOp) -> &'static str {
791    match op {
792        BinOp::Lt => "<",
793        BinOp::LtEq => "<=",
794        BinOp::Gt => ">",
795        BinOp::GtEq => ">=",
796        BinOp::Eq => "=",
797        BinOp::NotEq => "!=",
798        _ => "?",
799    }
800}
801
802#[cfg(test)]
803#[path = "planner_tests.rs"]
804mod tests;