use super::{has_kind, push_lit, push_name, tenant_col, valid, Kind, Table};
use h5i_app_sql::{Pred, Read, Val, Window};
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ReadQuery {
pub text: Vec<u8>,
pub params: Vec<Val>,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ReadSqlMode {
#[default]
Membership,
ScalarWhenUnique,
}
struct Out<'a> {
tables: &'a Vec<Table>,
mode: ReadSqlMode,
text: Vec<u8>,
params: Vec<Val>,
}
fn windowed(w: &Window) -> bool {
w.offset > 0 || w.limit.is_some()
}
fn not_null(t: &Table, i: usize) -> bool {
i < t.key_len as usize || !t.columns[i].nullable
}
fn fixes_column(p: &Pred, key: u32) -> bool {
match p {
Pred::Eq { col, .. } => *col == key,
Pred::And(a, b) => fixes_column(a, key) || fixes_column(b, key),
_ => false,
}
}
fn at_most_one(t: &Table, p: &Pred) -> bool {
(0..t.key_len).all(|key| fixes_column(p, key))
}
fn ordered_key(t: &Table) -> bool {
t.key_len > 0
&& t.columns[..t.key_len as usize].iter().all(|c| matches!(c.kind, Kind::Int | Kind::Bool))
}
fn int_param(n: u64) -> Val {
Val::Int(n.min(i64::MAX as u64) as i64)
}
impl Out<'_> {
fn lit(&mut self, s: &str) {
push_lit(&mut self.text, s.as_bytes());
}
fn param(&mut self, v: Val) {
self.params.push(v);
let n = self.params.len();
self.lit(&format!("${n}"));
}
fn col(&mut self, alias: usize, name: &Vec<u8>) {
self.lit(&format!("q{alias}."));
push_name(&mut self.text, name);
}
fn select(&mut self, table: u32, pred: &Pred, w: &Window, cols: &[usize], alias: usize, order: bool) -> Option<()> {
let t = self.tables.get(table as usize)?;
if windowed(w) && !ordered_key(t) {
return None;
}
self.lit("SELECT ");
for (n, &i) in cols.iter().enumerate() {
if n > 0 {
self.lit(", ");
}
self.col(alias, &t.columns.get(i)?.name.clone());
}
self.lit(" FROM ");
push_name(&mut self.text, &t.name);
self.lit(&format!(" AS q{alias} WHERE "));
self.col(alias, &tenant_col());
self.lit(" = $1 AND ");
self.pred(t, pred, alias)?;
if order || windowed(w) {
self.order_by(t, alias, w.desc && windowed(w));
}
if windowed(w) {
if let Some(n) = w.limit {
self.lit(" LIMIT ");
self.param(int_param(n));
}
if w.offset > 0 {
self.lit(" OFFSET ");
self.param(int_param(w.offset));
}
}
Some(())
}
fn order_by(&mut self, t: &Table, alias: usize, desc: bool) {
self.lit(" ORDER BY ");
for i in 0..t.key_len as usize {
if i > 0 {
self.lit(", ");
}
self.col(alias, &t.columns[i].name.clone());
if desc {
self.lit(" DESC");
}
}
}
fn pred(&mut self, t: &Table, p: &Pred, alias: usize) -> Option<()> {
match p {
Pred::All => self.lit("TRUE"),
Pred::Eq { col, val } => {
let c = t.columns.get(*col as usize)?;
if !has_kind(val, c.kind) {
return None;
}
self.col(alias, &c.name.clone());
self.lit(if not_null(t, *col as usize) { " = " } else { " IS NOT DISTINCT FROM " });
self.param(val.clone());
}
Pred::Ge { col, val } | Pred::Lt { col, val } => {
let c = t.columns.get(*col as usize)?;
if c.kind != Kind::Int || !matches!(val, Val::Int(_)) {
return None;
}
self.col(alias, &c.name.clone());
self.lit(if matches!(p, Pred::Ge { .. }) { " >= " } else { " < " });
self.param(val.clone());
}
Pred::In { col, table, pred, of, window } => {
let i = *col as usize;
let c = t.columns.get(i)?;
let sub = self.tables.get(*table as usize)?;
let j = *of as usize;
let d = sub.columns.get(j)?;
if c.kind != d.kind || !not_null(t, i) || !not_null(sub, j) {
return None;
}
self.col(alias, &c.name.clone());
self.lit(if self.mode == ReadSqlMode::ScalarWhenUnique && at_most_one(sub, pred) { " = (" } else { " IN (" });
self.select(*table, pred, window, &[j], alias + 1, false)?;
self.lit(")");
}
Pred::And(a, b) | Pred::Or(a, b) => {
let op = if matches!(p, Pred::And(..)) { " AND " } else { " OR " };
self.lit("(");
self.pred(t, a, alias)?;
self.lit(op);
self.pred(t, b, alias)?;
self.lit(")");
}
}
Some(())
}
}
pub fn compile_read(tables: &Vec<Table>, tenant: i64, q: &Read, count: bool) -> Option<ReadQuery> {
compile_read_with_mode(tables, tenant, q, count, ReadSqlMode::default())
}
pub fn compile_read_with_mode(tables: &Vec<Table>, tenant: i64, q: &Read, count: bool, mode: ReadSqlMode) -> Option<ReadQuery> {
if !valid(tables) {
return None;
}
let t = tables.get(q.table as usize)?;
let all: Vec<usize> = (0..t.columns.len()).collect();
let mut out = Out { tables, mode, text: Vec::new(), params: vec![Val::Int(tenant)] };
let desc = q.window.desc && windowed(&q.window);
if count {
out.lit("SELECT count(*) FROM (");
out.select(q.table, &q.pred, &q.window, &all, 0, false)?;
out.lit(") AS c");
} else if desc {
out.lit("SELECT * FROM (");
out.select(q.table, &q.pred, &q.window, &all, 0, false)?;
out.lit(") AS w ORDER BY ");
for i in 0..t.key_len as usize {
if i > 0 {
out.lit(", ");
}
push_name(&mut out.text, &t.columns[i].name);
}
} else {
out.select(q.table, &q.pred, &q.window, &all, 0, t.key_len > 0)?;
}
Some(ReadQuery { text: out.text, params: out.params })
}
pub fn create_index(tables: &Vec<Table>, table: u32, cols: &[u32]) -> Option<Vec<u8>> {
if !valid(tables) || cols.is_empty() {
return None;
}
let t = tables.get(table as usize)?;
let mut names = vec![tenant_col()];
for &c in cols {
names.push(t.columns.get(c as usize)?.name.clone());
}
let mut idx = format!("h5i_idx_{table}");
for &c in cols {
idx.push_str(&format!("_{c}"));
}
let mut out = Vec::new();
push_lit(&mut out, b"CREATE INDEX IF NOT EXISTS ");
push_name(&mut out, &[&t.name[..], b"_", idx.as_bytes()].concat().iter().take(63).copied().collect());
push_lit(&mut out, b" ON ");
push_name(&mut out, &t.name);
push_lit(&mut out, b" (");
for (i, n) in names.iter().enumerate() {
if i > 0 {
push_lit(&mut out, b", ");
}
push_name(&mut out, n);
}
push_lit(&mut out, b")");
Some(out)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::Column;
use h5i_app_sql::whole;
fn col(name: &str, kind: Kind, nullable: bool) -> Column {
Column { name: name.as_bytes().to_vec(), kind, nullable }
}
fn schema() -> Vec<Table> {
vec![
Table {
name: b"articles".to_vec(),
columns: vec![col("id", Kind::Int, false), col("author", Kind::Int, false), col("slug", Kind::Bytes, false)],
key_len: 1,
},
Table { name: b"tags".to_vec(), columns: vec![col("article", Kind::Int, false), col("tag", Kind::Bytes, false)], key_len: 2 },
Table { name: b"notes".to_vec(), columns: vec![col("tag", Kind::Bytes, false), col("n", Kind::Int, true)], key_len: 1 },
]
}
fn text(q: &ReadQuery) -> String {
String::from_utf8(q.text.clone()).unwrap()
}
#[test]
fn renders_a_page_of_a_tag() {
let q = Read {
table: 0,
pred: Pred::In {
col: 0,
table: 1,
pred: Box::new(Pred::Eq { col: 1, val: Val::Bytes(b"rust".to_vec()) }),
of: 0,
window: whole(),
},
window: Window { desc: true, offset: 0, limit: Some(20) },
};
let c = compile_read(&schema(), 7, &q, false).unwrap();
assert_eq!(
text(&c),
"SELECT * FROM (SELECT q0.\"id\", q0.\"author\", q0.\"slug\" FROM \"articles\" AS q0 WHERE q0.\"tenant_id\" = $1 \
AND q0.\"id\" IN (SELECT q1.\"article\" FROM \"tags\" AS q1 WHERE q1.\"tenant_id\" = $1 AND \
q1.\"tag\" = $2) ORDER BY q0.\"id\" DESC LIMIT $3) AS w ORDER BY \"id\""
);
assert_eq!(c.params, vec![Val::Int(7), Val::Bytes(b"rust".to_vec()), Val::Int(20)]);
let mut other = q.clone();
other.window.limit = Some(50);
assert_eq!(compile_read(&schema(), 9, &other, false).unwrap().text, c.text);
let n = compile_read(&schema(), 7, &q, true).unwrap();
assert!(text(&n).starts_with("SELECT count(*) FROM (SELECT q0."));
}
#[test]
fn scalar_membership_requires_the_entire_primary_key() {
let eq = |col| Pred::Eq { col, val: Val::Int(1) };
let s = schema();
assert!(at_most_one(&s[0], &eq(0)));
assert!(!at_most_one(&s[0], &eq(1)));
assert!(!at_most_one(&s[1], &eq(0)));
assert!(at_most_one(&s[1], &Pred::And(Box::new(eq(0)), Box::new(eq(1)))));
assert!(!at_most_one(&s[0], &Pred::Or(Box::new(eq(0)), Box::new(eq(0)))));
let singleton = Table { name: b"counter".to_vec(), columns: vec![col("value", Kind::Int, false)], key_len: 0 };
assert!(at_most_one(&singleton, &Pred::All));
}
#[test]
fn modes_preserve_parameters_and_default_to_membership() {
let q = Read { table: 0, pred: Pred::In {
col: 1, table: 0, pred: Box::new(Pred::Eq { col: 0, val: Val::Int(1) }),
of: 1, window: whole(),
}, window: whole() };
assert_eq!(ReadSqlMode::default(), ReadSqlMode::Membership);
for count in [false, true] {
let old = compile_read(&schema(), 7, &q, count).unwrap();
let member = compile_read_with_mode(&schema(), 7, &q, count, ReadSqlMode::Membership).unwrap();
let scalar = compile_read_with_mode(&schema(), 7, &q, count, ReadSqlMode::ScalarWhenUnique).unwrap();
assert_eq!(old, member);
assert!(text(&member).contains(" IN (SELECT"));
assert_eq!(text(&member).replace(" IN (SELECT", " = (SELECT"), text(&scalar));
assert_eq!(member.params, scalar.params);
}
let mut multi = q.clone();
if let Pred::In { pred, .. } = &mut multi.pred { **pred = Pred::All; }
assert_eq!(compile_read(&schema(), 7, &multi, false),
compile_read_with_mode(&schema(), 7, &multi, false, ReadSqlMode::ScalarWhenUnique));
}
#[test]
fn rejects_what_the_lean_does_not_mean() {
let s = schema();
let w = Read { table: 2, pred: Pred::All, window: Window { desc: false, offset: 1, limit: None } };
assert!(compile_read(&s, 1, &w, false).is_none());
let n = Read {
table: 0,
pred: Pred::In { col: 1, table: 2, pred: Box::new(Pred::All), of: 1, window: whole() },
window: whole(),
};
assert!(compile_read(&s, 1, &n, false).is_none());
assert!(compile_read(&s, 1, &h5i_app_sql::read_eq(0, 2, Val::Int(1)), false).is_none());
assert!(compile_read(&s, 1, &h5i_app_sql::read_eq(0, 9, Val::Int(1)), false).is_none());
assert!(compile_read(&s, 1, &h5i_app_sql::read_all(0), false).is_some());
let range = |table, col, val| Read { table, pred: Pred::Ge { col, val }, window: whole() };
assert!(compile_read(&s, 1, &range(0, 2, Val::Bytes(b"a".to_vec())), false).is_none());
assert!(compile_read(&s, 1, &range(0, 0, Val::Null), false).is_none());
assert!(compile_read(&s, 1, &range(0, 0, Val::Bytes(b"a".to_vec())), false).is_none());
assert!(compile_read(&s, 1, &range(0, 9, Val::Int(1)), false).is_none());
let lt = Read { table: 2, pred: Pred::Lt { col: 0, val: Val::Int(1) }, window: whole() };
assert!(compile_read(&s, 1, <, false).is_none());
}
#[test]
fn renders_an_id_range() {
let q = Read {
table: 0,
pred: Pred::And(Box::new(Pred::Ge { col: 0, val: Val::Int(64) }), Box::new(Pred::Lt { col: 0, val: Val::Int(128) })),
window: whole(),
};
let c = compile_read(&schema(), 7, &q, false).unwrap();
assert_eq!(
text(&c),
"SELECT q0.\"id\", q0.\"author\", q0.\"slug\" FROM \"articles\" AS q0 WHERE q0.\"tenant_id\" = $1 \
AND (q0.\"id\" >= $2 AND q0.\"id\" < $3) ORDER BY q0.\"id\""
);
assert_eq!(c.params, vec![Val::Int(7), Val::Int(64), Val::Int(128)]);
let n = Read { table: 2, pred: Pred::Lt { col: 1, val: Val::Int(-3) }, window: whole() };
assert!(text(&compile_read(&schema(), 7, &n, false).unwrap()).contains("q0.\"n\" < $2"));
}
#[test]
fn renders_an_index() {
let i = create_index(&schema(), 0, &[2]).unwrap();
assert_eq!(
String::from_utf8(i).unwrap(),
"CREATE INDEX IF NOT EXISTS \"articles_h5i_idx_0_2\" ON \"articles\" (\"tenant_id\", \"slug\")"
);
assert!(create_index(&schema(), 0, &[9]).is_none());
}
}