Skip to main content

vecdb_core/planner/sql/
ast.rs

1use crate::types::Vector;
2
3// ─────────────────────────────────────────────────────────────────
4// Top-level statement
5// ─────────────────────────────────────────────────────────────────
6
7/// A parsed `SELECT` statement.
8#[derive(Debug, Clone, PartialEq)]
9pub struct SelectStatement {
10    /// The table / collection name (FROM clause).
11    pub table: String,
12    /// Projected columns.  Empty means `SELECT *`.
13    pub columns: Vec<String>,
14    /// Optional WHERE clause.
15    pub where_clause: Option<Condition>,
16    /// Optional ORDER BY clause.
17    pub order_by: Option<OrderBy>,
18    /// Optional LIMIT clause.
19    pub limit: Option<usize>,
20}
21
22// ─────────────────────────────────────────────────────────────────
23// Condition tree
24// ─────────────────────────────────────────────────────────────────
25
26#[derive(Debug, Clone, PartialEq)]
27pub enum Condition {
28    /// `VECTOR_SIM(col, [...]) op threshold`
29    Vector(VectorCondition),
30    /// `field op literal`
31    Scalar(ScalarCondition),
32    /// `left AND right`
33    And(Box<Condition>, Box<Condition>),
34    /// `left OR right`
35    Or(Box<Condition>, Box<Condition>),
36    /// `NOT condition`
37    Not(Box<Condition>),
38}
39
40// ─────────────────────────────────────────────────────────────────
41// VectorCondition
42// ─────────────────────────────────────────────────────────────────
43
44/// `VECTOR_SIM(column, [f32, ...]) op threshold`
45#[derive(Debug, Clone, PartialEq)]
46pub struct VectorCondition {
47    /// The vector column name (first argument to VECTOR_SIM).
48    pub column: String,
49    /// The query vector (second argument).
50    pub vector: Vector,
51    /// Comparison operator — typically `Gt` or `GtEq`.
52    pub op: Operator,
53    /// Similarity threshold.
54    pub threshold: f32,
55}
56
57// ─────────────────────────────────────────────────────────────────
58// ScalarCondition
59// ─────────────────────────────────────────────────────────────────
60
61/// `field op literal`
62#[derive(Debug, Clone, PartialEq)]
63pub struct ScalarCondition {
64    /// Field name (may include dot / arrow path like `meta->>'key'`).
65    pub field: String,
66    pub op: Operator,
67    pub value: Literal,
68}
69
70// ─────────────────────────────────────────────────────────────────
71// Operator
72// ─────────────────────────────────────────────────────────────────
73
74#[derive(Debug, Clone, PartialEq)]
75pub enum Operator {
76    Eq,
77    NotEq,
78    Lt,
79    LtEq,
80    Gt,
81    GtEq,
82    Like,
83}
84
85// ─────────────────────────────────────────────────────────────────
86// Literal
87// ─────────────────────────────────────────────────────────────────
88
89#[derive(Debug, Clone, PartialEq)]
90pub enum Literal {
91    String(String),
92    Float(f64),
93    Integer(i64),
94    Bool(bool),
95}
96
97// ─────────────────────────────────────────────────────────────────
98// OrderBy
99// ─────────────────────────────────────────────────────────────────
100
101#[derive(Debug, Clone, PartialEq)]
102pub struct OrderBy {
103    pub field: String,
104    pub descending: bool,
105}