aletheiadb 0.1.0

A high-performance bi-temporal graph database for LLM integration
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
//! SQL to QueryOp Converter.
//!
//! This module converts parsed SQL AST from sqlparser-rs into AletheiaDB's
//! internal Query representation (QueryOp operations).

use std::collections::HashMap;
use std::sync::Arc;

use sqlparser::ast::{
    BinaryOperator, Expr, OrderByExpr, Query as SqlQuery, SelectItem, SetExpr, Statement,
    TableFactor, TableWithJoins, Value,
};

use crate::index::vector::DistanceMetric;
use crate::query::builder::Query;
use crate::query::ir::{Predicate, PredicateValue, QueryOp, SortKey};
use crate::query::plan::QueryHints;

use super::error::SqlError;
use super::match_parser;
use super::parser::SqlParser;
use super::temporal_parser;
use super::vector_parser::{self, EmbeddingRef, VectorOp};

/// Default k for SIMILAR_TO threshold-based search when no explicit limit is given.
/// This is an approximation: we over-fetch candidates then filter by threshold.
const SIMILAR_TO_DEFAULT_K: usize = 100;

/// Parameter values that can be bound to SQL queries.
#[derive(Debug, Clone)]
pub enum SqlParameterValue {
    /// Scalar value (string, int, float, bool)
    Scalar(PredicateValue),
    /// Vector embedding for k-NN search
    Embedding(Arc<[f32]>),
}

/// Converter from SQL AST to AletheiaDB Query.
///
/// # Example
///
/// ```rust
/// use aletheiadb::sql::SqlConverter;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let converter = SqlConverter::new();
/// let query = converter.convert_sql("SELECT * FROM nodes WHERE label = 'Person'")?;
/// # Ok(())
/// # }
/// ```
pub struct SqlConverter {
    /// Parameter bindings
    parameters: HashMap<String, SqlParameterValue>,
}

impl SqlConverter {
    /// Create a new SQL converter.
    pub fn new() -> Self {
        SqlConverter {
            parameters: HashMap::new(),
        }
    }

    /// Create a converter with pre-bound parameters.
    pub fn with_parameters(parameters: HashMap<String, SqlParameterValue>) -> Self {
        SqlConverter { parameters }
    }

    /// Bind a parameter value.
    pub fn bind(&mut self, name: impl Into<String>, value: SqlParameterValue) -> &mut Self {
        self.parameters.insert(name.into(), value);
        self
    }

    /// Convert a SQL string to a Query.
    pub fn convert_sql(&self, sql: &str) -> Result<Query, SqlError> {
        // Pipeline: SQL → temporal → match → vector → sqlparser → convert → wire up ops
        let extracted_temporal = temporal_parser::extract_temporal_clauses(sql)?;
        let extracted_match = match_parser::extract_match_clauses(&extracted_temporal.cleaned_sql)?;
        let extracted_vector = vector_parser::extract_vector_clauses(&extracted_match.cleaned_sql)?;

        // Parse the cleaned SQL (without temporal, MATCH, or vector clauses)
        let stmt = SqlParser::parse(&extracted_vector.cleaned_sql)?;

        // Convert to Query and add temporal context
        let mut query = self.convert(&stmt)?;
        query.temporal_context = extracted_temporal.to_temporal_context()?;

        // Insert graph traversal ops after source ops (ScanNodes/ScanEdges)
        // but before filter/projection/sort/limit ops.
        for pattern in extracted_match.patterns.iter().rev() {
            let insert_pos = query
                .ops
                .iter()
                .position(|op| !matches!(op, QueryOp::ScanNodes { .. } | QueryOp::ScanEdges { .. }))
                .unwrap_or(query.ops.len());
            query.ops.insert(insert_pos, pattern.to_query_op());
        }

        // Resolve and insert vector ops
        for vector_op in &extracted_vector.vector_ops {
            self.apply_vector_op(&mut query, vector_op)?;
        }

        Ok(query)
    }

    /// Convert a SQL statement to a Query.
    pub fn convert(&self, stmt: &Statement) -> Result<Query, SqlError> {
        match stmt {
            Statement::Query(query) => self.convert_query(query),
            _ => Err(SqlError::UnsupportedFeature(format!(
                "Only SELECT queries are supported, got: {:?}",
                stmt
            ))),
        }
    }

    /// Convert a SQL SELECT query to a Query.
    fn convert_query(&self, query: &SqlQuery) -> Result<Query, SqlError> {
        // Handle the body of the query
        let select = match query.body.as_ref() {
            SetExpr::Select(select) => select,
            _ => {
                return Err(SqlError::UnsupportedFeature(
                    "Only simple SELECT queries are supported".to_string(),
                ));
            }
        };

        let mut ops = Vec::new();

        // Convert FROM clause
        self.convert_from(&select.from, &mut ops)?;

        // Convert WHERE clause
        if let Some(ref selection) = select.selection {
            let predicate = self.convert_expr_to_predicate(selection)?;
            ops.push(QueryOp::Filter(predicate));
        }

        // Convert SELECT projection
        self.convert_projection(&select.projection, &mut ops)?;

        // Convert ORDER BY
        for order_by in &query.order_by {
            self.convert_order_by(order_by, &mut ops)?;
        }

        // Convert OFFSET (must come before LIMIT for correct SQL semantics:
        // skip N rows first, then take M rows from the result)
        if let Some(ref offset) = query.offset {
            let n = self.expr_to_usize(&offset.value)?;
            ops.push(QueryOp::Skip(n));
        }

        // Convert LIMIT
        if let Some(ref limit) = query.limit {
            let n = self.expr_to_usize(limit)?;
            ops.push(QueryOp::Limit(n));
        }

        Ok(Query {
            ops,
            // Temporal context is set by convert_sql() after extraction
            temporal_context: None,
            hints: QueryHints::default(),
        })
    }

    /// Convert FROM clause to source operations.
    fn convert_from(
        &self,
        from: &[TableWithJoins],
        ops: &mut Vec<QueryOp>,
    ) -> Result<(), SqlError> {
        if from.is_empty() {
            return Err(SqlError::MissingClause(
                "FROM clause is required".to_string(),
            ));
        }

        if from.len() > 1 {
            return Err(SqlError::UnsupportedFeature(
                "Multiple tables (joins) not yet supported".to_string(),
            ));
        }

        let table = &from[0];
        if !table.joins.is_empty() {
            return Err(SqlError::UnsupportedFeature(
                "JOIN clauses are not yet supported. Use MATCH for graph traversal: \
                 MATCH (source)-[:EDGE_TYPE]->(target)"
                    .to_string(),
            ));
        }

        match &table.relation {
            TableFactor::Table { name, alias: _, .. } => {
                let table_name = name.to_string().to_lowercase();
                match table_name.as_str() {
                    "nodes" => {
                        ops.push(QueryOp::ScanNodes { label: None });
                    }
                    "edges" => {
                        ops.push(QueryOp::ScanEdges { edge_type: None });
                    }
                    _ => {
                        // Treat other table names as label filters
                        // e.g., "SELECT * FROM Person" scans nodes with label 'Person'
                        ops.push(QueryOp::ScanNodes {
                            label: Some(table_name),
                        });
                    }
                }
            }
            _ => {
                return Err(SqlError::UnsupportedFeature(
                    "Complex table expressions not supported".to_string(),
                ));
            }
        }

        Ok(())
    }

    /// Convert SELECT projection.
    fn convert_projection(
        &self,
        projection: &[SelectItem],
        ops: &mut Vec<QueryOp>,
    ) -> Result<(), SqlError> {
        let mut columns = Vec::new();
        let mut is_star = false;

        for item in projection {
            match item {
                SelectItem::Wildcard(_) => {
                    is_star = true;
                }
                SelectItem::UnnamedExpr(expr) => {
                    if let Some(col) = self.expr_to_column_name(expr) {
                        columns.push(col);
                    }
                }
                SelectItem::ExprWithAlias { expr, alias } => {
                    if self.expr_to_column_name(expr).is_some() {
                        columns.push(alias.value.clone());
                    }
                }
                SelectItem::QualifiedWildcard(_, _) => {
                    is_star = true;
                }
            }
        }

        // If not SELECT *, add projection
        if !is_star && !columns.is_empty() {
            ops.push(QueryOp::Project(columns));
        }

        Ok(())
    }

    /// Convert ORDER BY clause.
    fn convert_order_by(
        &self,
        order_by: &OrderByExpr,
        ops: &mut Vec<QueryOp>,
    ) -> Result<(), SqlError> {
        let key = match &order_by.expr {
            Expr::Identifier(ident) => {
                let name = ident.value.to_lowercase();
                match name.as_str() {
                    "score" => SortKey::Score,
                    "timestamp" => SortKey::Timestamp,
                    _ => SortKey::Property(ident.value.clone()),
                }
            }
            Expr::CompoundIdentifier(parts) => {
                // Handle table.column syntax
                let col = parts.last().map(|p| p.value.clone()).ok_or_else(|| {
                    SqlError::InvalidColumn("Empty compound identifier in ORDER BY".to_string())
                })?;
                SortKey::Property(col)
            }
            _ => {
                return Err(SqlError::UnsupportedFeature(
                    "Complex ORDER BY expressions not yet supported. Use simple column names (e.g., ORDER BY name DESC)".to_string(),
                ));
            }
        };

        let descending = order_by.asc.map(|asc| !asc).unwrap_or(false);

        ops.push(QueryOp::Sort { key, descending });

        Ok(())
    }

    /// Convert a SQL expression to a predicate.
    fn convert_expr_to_predicate(&self, expr: &Expr) -> Result<Predicate, SqlError> {
        match expr {
            Expr::BinaryOp { left, op, right } => self.convert_binary_op(left, op, right),
            Expr::Nested(inner) => self.convert_expr_to_predicate(inner),
            Expr::IsNull(inner) => {
                let key = self.expr_to_property_key(inner)?;
                Ok(Predicate::Eq {
                    key,
                    value: PredicateValue::Null,
                })
            }
            Expr::IsNotNull(inner) => {
                let key = self.expr_to_property_key(inner)?;
                Ok(Predicate::Ne {
                    key,
                    value: PredicateValue::Null,
                })
            }
            Expr::InList {
                expr,
                list,
                negated,
            } => {
                let key = self.expr_to_property_key(expr)?;
                let values: Result<Vec<PredicateValue>, SqlError> =
                    list.iter().map(|e| self.expr_to_value(e)).collect();
                let pred = Predicate::In {
                    key,
                    values: values?,
                };
                if *negated { Ok(!pred) } else { Ok(pred) }
            }
            Expr::Like {
                expr,
                pattern,
                negated,
                ..
            } => {
                let key = self.expr_to_property_key(expr)?;
                let pattern_str = self.expr_to_string(pattern)?;

                // Convert LIKE pattern to appropriate predicate using slicing
                // to correctly handle patterns like 'a%b'
                let pred = if pattern_str.starts_with('%')
                    && pattern_str.ends_with('%')
                    && pattern_str.len() > 1
                {
                    // %substring% -> Contains
                    let substring = pattern_str[1..pattern_str.len() - 1].to_string();
                    Predicate::Contains { key, substring }
                } else if pattern_str.ends_with('%') && !pattern_str.starts_with('%') {
                    // prefix% -> StartsWith
                    let prefix = pattern_str[..pattern_str.len() - 1].to_string();
                    Predicate::StartsWith { key, prefix }
                } else if pattern_str.starts_with('%') && !pattern_str.ends_with('%') {
                    // %suffix -> EndsWith
                    let suffix = pattern_str[1..].to_string();
                    Predicate::EndsWith { key, suffix }
                } else {
                    // Exact match (no wildcards or complex pattern)
                    Predicate::Eq {
                        key,
                        value: PredicateValue::String(pattern_str),
                    }
                };

                if *negated { Ok(!pred) } else { Ok(pred) }
            }
            _ => Err(SqlError::UnsupportedFeature(format!(
                "Expression type not supported in WHERE: {:?}",
                expr
            ))),
        }
    }

    /// Convert a binary operation to a predicate.
    fn convert_binary_op(
        &self,
        left: &Expr,
        op: &BinaryOperator,
        right: &Expr,
    ) -> Result<Predicate, SqlError> {
        // Handle logical operators
        match op {
            BinaryOperator::And => {
                let l = self.convert_expr_to_predicate(left)?;
                let r = self.convert_expr_to_predicate(right)?;
                return Ok(l.and(r));
            }
            BinaryOperator::Or => {
                let l = self.convert_expr_to_predicate(left)?;
                let r = self.convert_expr_to_predicate(right)?;
                return Ok(l.or(r));
            }
            _ => {}
        }

        // Handle comparison operators
        let key = self.expr_to_property_key(left)?;
        let value = self.expr_to_value(right)?;

        match op {
            BinaryOperator::Eq => Ok(Predicate::Eq { key, value }),
            BinaryOperator::NotEq => Ok(Predicate::Ne { key, value }),
            BinaryOperator::Lt => Ok(Predicate::Lt { key, value }),
            BinaryOperator::LtEq => Ok(Predicate::Lte { key, value }),
            BinaryOperator::Gt => Ok(Predicate::Gt { key, value }),
            BinaryOperator::GtEq => Ok(Predicate::Gte { key, value }),
            _ => Err(SqlError::UnsupportedFeature(format!(
                "Operator not supported: {:?}",
                op
            ))),
        }
    }

    /// Extract property key from expression.
    fn expr_to_property_key(&self, expr: &Expr) -> Result<String, SqlError> {
        match expr {
            Expr::Identifier(ident) => Ok(ident.value.clone()),
            Expr::CompoundIdentifier(parts) => {
                // Handle table.column syntax - return just the column name
                parts
                    .last()
                    .map(|p| p.value.clone())
                    .ok_or_else(|| SqlError::InvalidColumn("Empty compound identifier".to_string()))
            }
            _ => Err(SqlError::InvalidColumn(format!(
                "Cannot extract property key from: {:?}",
                expr
            ))),
        }
    }

    /// Convert expression to predicate value.
    fn expr_to_value(&self, expr: &Expr) -> Result<PredicateValue, SqlError> {
        match expr {
            Expr::Value(value) => self.value_to_predicate_value(value),
            Expr::Identifier(ident) => {
                // Check if it's a parameter reference
                if let Some(param) = self.parameters.get(&ident.value) {
                    match param {
                        SqlParameterValue::Scalar(v) => Ok(v.clone()),
                        _ => Err(SqlError::TypeError(
                            "Expected scalar parameter value".to_string(),
                        )),
                    }
                } else {
                    Err(SqlError::ParameterError(format!(
                        "Unknown parameter: {}",
                        ident.value
                    )))
                }
            }
            Expr::UnaryOp { op, expr } => {
                // Handle negative numbers
                match op {
                    sqlparser::ast::UnaryOperator::Minus => {
                        let inner = self.expr_to_value(expr)?;
                        match inner {
                            PredicateValue::Int(n) => Ok(PredicateValue::Int(-n)),
                            PredicateValue::Float(n) => Ok(PredicateValue::Float(-n)),
                            _ => Err(SqlError::TypeError(
                                "Cannot negate non-numeric value".to_string(),
                            )),
                        }
                    }
                    _ => Err(SqlError::UnsupportedFeature(format!(
                        "Unary operator not supported: {:?}",
                        op
                    ))),
                }
            }
            _ => Err(SqlError::UnsupportedFeature(format!(
                "Expression type not supported as value: {:?}",
                expr
            ))),
        }
    }

    /// Convert SQL value to predicate value.
    fn value_to_predicate_value(&self, value: &Value) -> Result<PredicateValue, SqlError> {
        match value {
            Value::Null => Ok(PredicateValue::Null),
            Value::Boolean(b) => Ok(PredicateValue::Bool(*b)),
            Value::Number(n, _) => {
                // Try parsing as i64 first, then f64
                if let Ok(i) = n.parse::<i64>() {
                    Ok(PredicateValue::Int(i))
                } else if let Ok(f) = n.parse::<f64>() {
                    Ok(PredicateValue::Float(f))
                } else {
                    Err(SqlError::TypeError(format!("Invalid number: {}", n)))
                }
            }
            Value::SingleQuotedString(s) | Value::DoubleQuotedString(s) => {
                Ok(PredicateValue::String(s.clone()))
            }
            _ => Err(SqlError::UnsupportedFeature(format!(
                "Value type not supported: {:?}",
                value
            ))),
        }
    }

    /// Extract column name from expression.
    fn expr_to_column_name(&self, expr: &Expr) -> Option<String> {
        match expr {
            Expr::Identifier(ident) => Some(ident.value.clone()),
            Expr::CompoundIdentifier(parts) => parts.last().map(|p| p.value.clone()),
            _ => None,
        }
    }

    /// Convert expression to usize.
    fn expr_to_usize(&self, expr: &Expr) -> Result<usize, SqlError> {
        match expr {
            Expr::Value(Value::Number(n, _)) => n
                .parse::<usize>()
                .map_err(|_| SqlError::TypeError(format!("Expected positive integer, got: {}", n))),
            _ => Err(SqlError::TypeError(format!(
                "Expected integer literal, got: {:?}",
                expr
            ))),
        }
    }

    /// Convert expression to string.
    fn expr_to_string(&self, expr: &Expr) -> Result<String, SqlError> {
        match expr {
            Expr::Value(Value::SingleQuotedString(s) | Value::DoubleQuotedString(s)) => {
                Ok(s.clone())
            }
            _ => Err(SqlError::TypeError(format!(
                "Expected string literal, got: {:?}",
                expr
            ))),
        }
    }

    // =========================================================================
    // Vector operation wiring
    // =========================================================================

    /// Resolve an `EmbeddingRef` to a concrete `Arc<[f32]>` using bound parameters.
    fn resolve_embedding(&self, r: &EmbeddingRef) -> Result<Arc<[f32]>, SqlError> {
        match r {
            EmbeddingRef::Literal(v) => Ok(v.clone().into()),
            EmbeddingRef::Parameter(name) => {
                // Strip leading $ if present
                let clean_name = name.strip_prefix('$').unwrap_or(name);
                match self.parameters.get(clean_name) {
                    Some(SqlParameterValue::Embedding(e)) => Ok(e.clone()),
                    Some(_) => Err(SqlError::TypeError(format!(
                        "Parameter '{}' is not an embedding",
                        clean_name
                    ))),
                    None => Err(SqlError::ParameterError(format!(
                        "Unbound embedding parameter: {}",
                        clean_name
                    ))),
                }
            }
        }
    }

    /// Apply a parsed vector operation to the query ops pipeline.
    fn apply_vector_op(&self, query: &mut Query, op: &VectorOp) -> Result<(), SqlError> {
        match op {
            VectorOp::KnnOrderBy {
                property_key,
                embedding_ref,
                metric,
            } => {
                let embedding = self.resolve_embedding(embedding_ref)?;

                // Detect whether we have graph traversal ops in the pipeline.
                // If so, this is a hybrid query: rank traversal results by similarity.
                let has_traversal = query.ops.iter().any(|op| {
                    matches!(
                        op,
                        QueryOp::TraverseOut { .. }
                            | QueryOp::TraverseIn { .. }
                            | QueryOp::TraverseBoth { .. }
                    )
                });

                let k = query.ops.iter().find_map(|op| {
                    if let QueryOp::Limit(n) = op {
                        Some(*n)
                    } else {
                        None
                    }
                });

                if has_traversal {
                    // Hybrid mode: rank traversal results by similarity.
                    // Insert RankBySimilarity before the Limit op.
                    let pos = query
                        .ops
                        .iter()
                        .position(|op| matches!(op, QueryOp::Limit(_)))
                        .unwrap_or(query.ops.len());
                    query.ops.insert(
                        pos,
                        QueryOp::RankBySimilarity {
                            embedding,
                            top_k: k,
                            property_key: Some(property_key.clone()),
                        },
                    );
                } else {
                    // Standalone k-NN search.
                    let limit = k.unwrap_or(10);

                    // If OFFSET is present, VectorSearch must fetch enough rows so
                    // that after Skip(offset) we still have `limit` results.
                    let offset = query.ops.iter().find_map(|op| {
                        if let QueryOp::Skip(n) = op {
                            Some(*n)
                        } else {
                            None
                        }
                    });
                    let effective_k = limit + offset.unwrap_or(0);

                    // Remove the Limit op since VectorSearch has its own k.
                    // Keep Skip so offset semantics are preserved.
                    query.ops.retain(|op| !matches!(op, QueryOp::Limit(_)));

                    // Insert VectorSearch after source ops.
                    let pos = query
                        .ops
                        .iter()
                        .position(|op| {
                            !matches!(op, QueryOp::ScanNodes { .. } | QueryOp::ScanEdges { .. })
                        })
                        .unwrap_or(query.ops.len());
                    query.ops.insert(
                        pos,
                        QueryOp::VectorSearch {
                            embedding,
                            k: effective_k,
                            metric: *metric,
                            property_key: Some(property_key.clone()),
                        },
                    );

                    // Re-add Limit after Skip so the final row count equals `limit`.
                    if offset.is_some() {
                        query.ops.push(QueryOp::Limit(limit));
                    }
                }
            }
            VectorOp::KnnFunction {
                property_key,
                embedding_ref,
                k,
                ..
            } => {
                let embedding = self.resolve_embedding(embedding_ref)?;

                // Preserve label filter from the ScanNodes we are about to remove.
                // e.g. `FROM KNN('Documents', ...)` produces ScanNodes { label: Some("documents") }.
                let label = query.ops.iter().find_map(|op| {
                    if let QueryOp::ScanNodes { label: Some(l) } = op {
                        Some(l.clone())
                    } else {
                        None
                    }
                });

                // Replace the scan with VectorSearch.
                query
                    .ops
                    .retain(|op| !matches!(op, QueryOp::ScanNodes { .. }));
                query.ops.insert(
                    0,
                    QueryOp::VectorSearch {
                        embedding,
                        k: *k,
                        metric: DistanceMetric::Cosine,
                        property_key: Some(property_key.clone()),
                    },
                );

                // Re-apply label filter so results are scoped to the original label.
                if let Some(l) = label {
                    query.ops.insert(1, QueryOp::FilterLabel(l));
                }
            }
            VectorOp::SimilarToFilter {
                property_key,
                embedding_ref,
                threshold,
            } => {
                // A full implementation would add a dedicated VectorFilter QueryOp.
                // For now we over-fetch with VectorSearch and post-filter by threshold.
                let embedding = self.resolve_embedding(embedding_ref)?;

                let k = SIMILAR_TO_DEFAULT_K;
                let pos = query
                    .ops
                    .iter()
                    .position(|op| {
                        !matches!(op, QueryOp::ScanNodes { .. } | QueryOp::ScanEdges { .. })
                    })
                    .unwrap_or(query.ops.len());
                query.ops.insert(
                    pos,
                    QueryOp::VectorSearch {
                        embedding,
                        k,
                        metric: DistanceMetric::Cosine,
                        property_key: Some(property_key.clone()),
                    },
                );

                // Enforce the similarity threshold as a post-search filter.
                // VectorSearch returns a "score" property for each result;
                // we keep only results whose score >= the user-specified threshold.
                query.ops.insert(
                    pos + 1,
                    QueryOp::Filter(Predicate::Gte {
                        key: "score".to_string(),
                        value: PredicateValue::Float(*threshold),
                    }),
                );
            }
        }

        Ok(())
    }
}

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

/// Parse a SQL query and convert it to a AletheiaDB Query.
///
/// This is the simplest way to execute a SQL query.
///
/// # Example
///
/// ```rust
/// use aletheiadb::sql::parse_sql;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let query = parse_sql("SELECT * FROM nodes WHERE label = 'Person' LIMIT 10")?;
/// # Ok(())
/// # }
/// ```
pub fn parse_sql(sql: &str) -> Result<Query, SqlError> {
    let converter = SqlConverter::new();
    converter.convert_sql(sql)
}

/// Parse a SQL query with parameters.
///
/// # Example
///
/// ```rust
/// use aletheiadb::sql::{parse_sql_with_params, SqlParameterValue};
/// use aletheiadb::query::ir::PredicateValue;
/// use std::collections::HashMap;
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// let mut params = HashMap::new();
/// params.insert("min_age".to_string(), SqlParameterValue::Scalar(PredicateValue::Int(21)));
///
/// let query = parse_sql_with_params(
///     "SELECT * FROM nodes WHERE age > min_age",
///     params
/// )?;
/// # Ok(())
/// # }
/// ```
pub fn parse_sql_with_params(
    sql: &str,
    params: HashMap<String, SqlParameterValue>,
) -> Result<Query, SqlError> {
    let converter = SqlConverter::with_parameters(params);
    converter.convert_sql(sql)
}