use crate::lex::{Tok, Token, lex};
use crate::{SqlError, fold_parse};
use kevy_scalar::Scalar;
#[derive(Debug, Clone, PartialEq)]
#[non_exhaustive]
pub struct Folded {
pub columns: Vec<kevy_scalar::Scalar>,
}
pub fn fold_select(sql: &str, now_micros: i64) -> Result<Folded, SqlError> {
let toks = lex(sql)?;
let mut p = Parser { t: &toks, i: 0, now: now_micros };
p.expect_kw("select")?;
let mut columns = Vec::new();
loop {
columns.push(p.expr()?);
if !p.eat_sym(',') {
break;
}
}
p.eat_sym(';');
let t = p.peek();
if !matches!(t.tok, Tok::Eof) {
return Err(SqlError::at(
t.line,
t.col,
match &t.tok {
Tok::Ident(w) if w == "from" => {
"FROM is not foldable — a table query compiles through the plan face; \
only table-free SELECT folds here"
.to_string()
}
other => format!("unexpected {other:?} after the select list"),
},
));
}
Ok(Folded { columns })
}
pub(crate) struct Parser<'a> {
pub(crate) t: &'a [Token],
pub(crate) i: usize,
pub(crate) now: i64,
}
impl<'a> Parser<'a> {
pub(crate) fn peek(&self) -> &'a Token {
&self.t[self.i.min(self.t.len() - 1)]
}
pub(crate) fn bump(&mut self) -> &'a Token {
let t = &self.t[self.i.min(self.t.len() - 1)];
self.i += 1;
t
}
pub(crate) fn eat_sym(&mut self, c: char) -> bool {
if matches!(self.peek().tok, Tok::Sym(s) if s == c) {
self.i += 1;
return true;
}
false
}
pub(crate) fn eat_kw(&mut self, kw: &str) -> bool {
if matches!(&self.peek().tok, Tok::Ident(w) if w == kw) {
self.i += 1;
return true;
}
false
}
pub(crate) fn expect_kw(&mut self, kw: &str) -> Result<(), SqlError> {
let t = self.peek();
if self.eat_kw(kw) {
return Ok(());
}
Err(SqlError::at(t.line, t.col, format!("expected {}", kw.to_uppercase())))
}
pub(crate) fn expect_sym(&mut self, c: char) -> Result<(), SqlError> {
let t = self.peek();
if self.eat_sym(c) {
return Ok(());
}
Err(SqlError::at(t.line, t.col, format!("expected '{c}'")))
}
pub(crate) fn expr(&mut self) -> Result<Scalar, SqlError> {
let mut acc = self.and_expr()?;
while self.eat_kw("or") {
let rhs = self.and_expr()?;
acc =
kevy_scalar::logic_or(&acc, &rhs).map_err(|e| SqlError::at(0, 0, e.to_string()))?;
}
Ok(acc)
}
fn and_expr(&mut self) -> Result<Scalar, SqlError> {
let mut acc = self.not_expr()?;
while self.eat_kw("and") {
let rhs = self.not_expr()?;
acc = kevy_scalar::logic_and(&acc, &rhs)
.map_err(|e| SqlError::at(0, 0, e.to_string()))?;
}
Ok(acc)
}
fn not_expr(&mut self) -> Result<Scalar, SqlError> {
if self.eat_kw("not") {
let v = self.not_expr()?;
return kevy_scalar::logic_not(&v).map_err(|e| SqlError::at(0, 0, e.to_string()));
}
self.cmp_expr()
}
fn cmp_expr(&mut self) -> Result<Scalar, SqlError> {
let lhs = self.add_expr()?;
let t = self.peek();
let (line, col) = (t.line, t.col);
let op: &str = match t.tok {
Tok::Op(o @ ("=" | "<" | ">" | "<=" | ">=" | "<>" | "!=")) => o,
_ => return Ok(lhs),
};
self.i += 1;
let rhs = self.add_expr()?;
kevy_scalar::cmp_op(op, &lhs, &rhs).map_err(|e| SqlError::at(line, col, e.to_string()))
}
fn add_expr(&mut self) -> Result<Scalar, SqlError> {
let mut acc = self.concat_expr()?;
loop {
let t = self.peek();
let op = match t.tok {
Tok::Sym(c @ ('+' | '-')) => c,
_ => return Ok(acc),
};
let (line, col) = (t.line, t.col);
self.i += 1;
let rhs = self.concat_expr()?;
acc = kevy_scalar::binop(op, &acc, &rhs)
.map_err(|e| SqlError::at(line, col, e.to_string()))?;
}
}
fn concat_expr(&mut self) -> Result<Scalar, SqlError> {
let mut acc = self.term()?;
while matches!(self.peek().tok, Tok::Op("||")) {
self.i += 1;
let rhs = self.term()?;
acc = match (&acc, &rhs) {
(Scalar::Null, _) | (_, Scalar::Null) => Scalar::Null,
(a, b) => Scalar::Text(format!("{}{}", a.render(), b.render())),
};
}
Ok(acc)
}
fn term(&mut self) -> Result<Scalar, SqlError> {
let mut acc = fold_parse::primary(self)?;
loop {
let t = self.peek();
let op = match t.tok {
Tok::Sym(c @ ('*' | '/')) => c,
_ => return Ok(acc),
};
let (line, col) = (t.line, t.col);
self.i += 1;
let rhs = fold_parse::primary(self)?;
acc = kevy_scalar::binop(op, &acc, &rhs)
.map_err(|e| SqlError::at(line, col, e.to_string()))?;
}
}
}