uqa-sql 0.1.6

PostgreSQL-compatible SQL compiler built on libpg_query
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
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

//! Full-text search query-string parser.
//!
//! Grammar
//! -------
//! ```text
//!     query      = or_expr
//!     or_expr    = and_expr ( 'OR' and_expr )*
//!     and_expr   = unary ( ('AND' | <implicit>) unary )*
//!     unary      = 'NOT' unary | primary
//!     primary    = '(' or_expr ')'
//!                | TERM ':' PHRASE          -- field:"phrase"
//!                | TERM ':' VECTOR          -- field:[0.1, 0.2]
//!                | TERM ':' TERM            -- field:term
//!                | PHRASE                   -- "phrase"
//!                | TERM                     -- bare term
//! ```
//!
//! Operators AND / OR / NOT are case-insensitive keywords. Adjacent
//! terms without an explicit operator are treated as implicit AND.
//! Precedence: NOT > AND > OR.
//!
//! This module stops at the syntax AST. Physical retrieval lowering belongs
//! to the engine/planner boundary, so the SQL crate remains independent of
//! storage, scoring, fusion, and operator implementations.

use crate::error::{Result, SQLError};

// ---------------------------------------------------------------------
// Token
// ---------------------------------------------------------------------

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FTSTokenType {
    Term,
    Phrase,
    Vector,
    And,
    Or,
    Not,
    LParen,
    RParen,
    Colon,
    Eof,
}

#[derive(Debug, Clone)]
pub struct FTSToken {
    pub kind: FTSTokenType,
    pub value: String,
    pub pos: usize,
}

pub fn tokenize(source: &str) -> Result<Vec<FTSToken>> {
    let mut tokens: Vec<FTSToken> = Vec::new();
    let bytes: Vec<char> = source.chars().collect();
    let n = bytes.len();
    let mut i = 0_usize;

    while i < n {
        let ch = bytes[i];
        if ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r' {
            i += 1;
            continue;
        }
        match ch {
            '(' => {
                tokens.push(FTSToken {
                    kind: FTSTokenType::LParen,
                    value: "(".into(),
                    pos: i,
                });
                i += 1;
                continue;
            }
            ')' => {
                tokens.push(FTSToken {
                    kind: FTSTokenType::RParen,
                    value: ")".into(),
                    pos: i,
                });
                i += 1;
                continue;
            }
            ':' => {
                tokens.push(FTSToken {
                    kind: FTSTokenType::Colon,
                    value: ":".into(),
                    pos: i,
                });
                i += 1;
                continue;
            }
            '"' => {
                let start = i;
                i += 1;
                let body_start = i;
                while i < n && bytes[i] != '"' {
                    i += 1;
                }
                if i >= n {
                    return Err(SQLError::TypeMismatch(format!(
                        "Unterminated quoted phrase starting at position {start}"
                    )));
                }
                let phrase: String = bytes[body_start..i].iter().collect();
                tokens.push(FTSToken {
                    kind: FTSTokenType::Phrase,
                    value: phrase,
                    pos: start,
                });
                i += 1;
                continue;
            }
            '[' => {
                let start = i;
                i += 1;
                let body_start = i;
                while i < n && bytes[i] != ']' {
                    i += 1;
                }
                if i >= n {
                    return Err(SQLError::TypeMismatch(format!(
                        "Unterminated vector literal starting at position {start}"
                    )));
                }
                let content: String = bytes[body_start..i].iter().collect();
                tokens.push(FTSToken {
                    kind: FTSTokenType::Vector,
                    value: content,
                    pos: start,
                });
                i += 1;
                continue;
            }
            _ => {}
        }
        if is_word_char(ch) {
            let start = i;
            while i < n && is_word_char(bytes[i]) {
                i += 1;
            }
            let word: String = bytes[start..i].iter().collect();
            let lower = word.to_ascii_lowercase();
            let kind = match lower.as_str() {
                "and" => FTSTokenType::And,
                "or" => FTSTokenType::Or,
                "not" => FTSTokenType::Not,
                _ => FTSTokenType::Term,
            };
            tokens.push(FTSToken {
                kind,
                value: word,
                pos: start,
            });
            continue;
        }
        return Err(SQLError::TypeMismatch(format!(
            "Unexpected character {ch:?} at position {i}"
        )));
    }

    tokens.push(FTSToken {
        kind: FTSTokenType::Eof,
        value: String::new(),
        pos: n,
    });
    Ok(tokens)
}

fn is_word_char(ch: char) -> bool {
    !matches!(
        ch,
        ' ' | '\t' | '\n' | '\r' | '(' | ')' | ':' | '"' | '[' | ']'
    )
}

// ---------------------------------------------------------------------
// AST
// ---------------------------------------------------------------------

#[derive(Debug, Clone)]
pub enum FTSNode {
    Term {
        field: Option<String>,
        term: String,
    },
    Phrase {
        field: Option<String>,
        phrase: String,
    },
    Vector {
        field: Option<String>,
        values: Vec<f32>,
    },
    And(Box<FTSNode>, Box<FTSNode>),
    Or(Box<FTSNode>, Box<FTSNode>),
    Not(Box<FTSNode>),
}

// ---------------------------------------------------------------------
// Parser
// ---------------------------------------------------------------------

pub struct FTSParser {
    tokens: Vec<FTSToken>,
    pos: usize,
}

impl FTSParser {
    pub fn new(tokens: Vec<FTSToken>) -> Self {
        Self { tokens, pos: 0 }
    }

    pub fn parse(mut self) -> Result<FTSNode> {
        if self.peek().kind == FTSTokenType::Eof {
            return Err(SQLError::TypeMismatch("Empty query".into()));
        }
        let node = self.or_expr()?;
        if self.peek().kind != FTSTokenType::Eof {
            let tok = self.peek();
            return Err(SQLError::TypeMismatch(format!(
                "Unexpected token {:?} at position {}",
                tok.value, tok.pos
            )));
        }
        Ok(node)
    }

    fn peek(&self) -> &FTSToken {
        &self.tokens[self.pos]
    }

    fn advance(&mut self) -> FTSToken {
        let tok = self.tokens[self.pos].clone();
        self.pos += 1;
        tok
    }

    fn expect(&mut self, kind: FTSTokenType) -> Result<FTSToken> {
        let tok = self.advance();
        if tok.kind != kind {
            return Err(SQLError::TypeMismatch(format!(
                "Expected {:?}, got {:?} ({:?}) at position {}",
                kind, tok.kind, tok.value, tok.pos
            )));
        }
        Ok(tok)
    }

    fn or_expr(&mut self) -> Result<FTSNode> {
        let mut left = self.and_expr()?;
        while self.peek().kind == FTSTokenType::Or {
            self.advance();
            let right = self.and_expr()?;
            left = FTSNode::Or(Box::new(left), Box::new(right));
        }
        Ok(left)
    }

    fn and_expr(&mut self) -> Result<FTSNode> {
        let mut left = self.unary()?;
        loop {
            let kind = self.peek().kind;
            if kind == FTSTokenType::And {
                self.advance();
                let right = self.unary()?;
                left = FTSNode::And(Box::new(left), Box::new(right));
            } else if matches!(
                kind,
                FTSTokenType::Term
                    | FTSTokenType::Phrase
                    | FTSTokenType::Vector
                    | FTSTokenType::LParen
                    | FTSTokenType::Not
            ) {
                let right = self.unary()?;
                left = FTSNode::And(Box::new(left), Box::new(right));
            } else {
                break;
            }
        }
        Ok(left)
    }

    fn unary(&mut self) -> Result<FTSNode> {
        if self.peek().kind == FTSTokenType::Not {
            self.advance();
            let operand = self.unary()?;
            return Ok(FTSNode::Not(Box::new(operand)));
        }
        self.primary()
    }

    fn primary(&mut self) -> Result<FTSNode> {
        let kind = self.peek().kind;
        if kind == FTSTokenType::LParen {
            self.advance();
            let node = self.or_expr()?;
            self.expect(FTSTokenType::RParen)?;
            return Ok(node);
        }
        if kind == FTSTokenType::Phrase {
            let tok = self.advance();
            return Ok(FTSNode::Phrase {
                field: None,
                phrase: tok.value,
            });
        }
        if kind == FTSTokenType::Vector {
            let tok = self.advance();
            return Ok(FTSNode::Vector {
                field: None,
                values: parse_vector_literal(&tok.value)?,
            });
        }
        if kind == FTSTokenType::Term {
            let tok = self.advance();
            if self.peek().kind == FTSTokenType::Colon {
                self.advance();
                let next = self.peek().clone();
                match next.kind {
                    FTSTokenType::Phrase => {
                        self.advance();
                        return Ok(FTSNode::Phrase {
                            field: Some(tok.value),
                            phrase: next.value,
                        });
                    }
                    FTSTokenType::Vector => {
                        self.advance();
                        return Ok(FTSNode::Vector {
                            field: Some(tok.value),
                            values: parse_vector_literal(&next.value)?,
                        });
                    }
                    FTSTokenType::Term => {
                        self.advance();
                        return Ok(FTSNode::Term {
                            field: Some(tok.value),
                            term: next.value,
                        });
                    }
                    other => {
                        return Err(SQLError::TypeMismatch(format!(
                            "Expected term, phrase, or vector after ':', got {other:?} at position {}",
                            next.pos
                        )));
                    }
                }
            }
            return Ok(FTSNode::Term {
                field: None,
                term: tok.value,
            });
        }
        let tok = self.peek().clone();
        Err(SQLError::TypeMismatch(format!(
            "Unexpected token {:?} ({:?}) at position {}",
            tok.kind, tok.value, tok.pos
        )))
    }
}

fn parse_vector_literal(content: &str) -> Result<Vec<f32>> {
    let trimmed = content.trim();
    if trimmed.is_empty() {
        return Err(SQLError::TypeMismatch("Empty vector literal".into()));
    }
    trimmed
        .split(',')
        .map(|s| {
            s.trim()
                .parse::<f32>()
                .map_err(|e| SQLError::TypeMismatch(format!("Malformed vector literal: {e}")))
        })
        .collect()
}

/// Tokenize and parse a full-text query string without choosing a physical
/// retrieval representation.
pub fn parse_query_string(query_string: &str) -> Result<FTSNode> {
    let tokens = tokenize(query_string)?;
    FTSParser::new(tokens).parse()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn tokenize_terms_and_phrase() {
        let tokens = tokenize(r#"hello "world today" foo"#).unwrap();
        let kinds: Vec<_> = tokens.iter().map(|t| t.kind).collect();
        assert_eq!(
            kinds,
            vec![
                FTSTokenType::Term,
                FTSTokenType::Phrase,
                FTSTokenType::Term,
                FTSTokenType::Eof,
            ]
        );
        assert_eq!(tokens[1].value, "world today");
    }

    #[test]
    fn tokenize_vector() {
        let tokens = tokenize("[0.1, 0.2, 0.3]").unwrap();
        assert_eq!(tokens[0].kind, FTSTokenType::Vector);
        assert_eq!(tokens[0].value, "0.1, 0.2, 0.3");
    }

    #[test]
    fn parse_implicit_and() {
        let tokens = tokenize("rust ferris").unwrap();
        let ast = FTSParser::new(tokens).parse().unwrap();
        assert!(matches!(ast, FTSNode::And(_, _)));
    }

    #[test]
    fn parse_field_qualified_term() {
        let tokens = tokenize("title:rust").unwrap();
        let ast = FTSParser::new(tokens).parse().unwrap();
        match ast {
            FTSNode::Term { field, term } => {
                assert_eq!(field.as_deref(), Some("title"));
                assert_eq!(term, "rust");
            }
            _ => panic!("expected Term"),
        }
    }

    #[test]
    fn parse_not_precedence() {
        let tokens = tokenize("NOT foo OR bar").unwrap();
        let ast = FTSParser::new(tokens).parse().unwrap();
        // Should parse as (NOT foo) OR bar.
        match ast {
            FTSNode::Or(left, _) => match *left {
                FTSNode::Not(_) => {}
                _ => panic!("expected NOT on left"),
            },
            _ => panic!("expected OR"),
        }
    }

    #[test]
    fn parse_query_string_stops_at_syntax_ast() {
        let ast = parse_query_string("body:rust OR embedding:[1, 0]").unwrap();
        assert!(matches!(ast, FTSNode::Or(_, _)));
    }
}