Skip to main content

h5i_app_pgsql/
read.rs

1//! SQL for the declarative reads of `h5i_app_sql` (`Read`, `Pred`).
2//!
3//! Not extracted to Lean: `compile_read` is checked by differential tests
4//! (`crates/h5i-app-pg/src/reads_test.rs`) against `h5i_app_sql::eval`, which
5//! mirrors `H5iAppLib.Query`. The checks below are what makes the SQL mean
6//! what the Lean says: membership only between `NOT NULL` columns, windows
7//! only on tables whose key columns are all integers or booleans (so the key
8//! order is not a collation's), and the result always sorted by key ascending.
9//! In opt-in ScalarWhenUnique mode, a lookup fixed by its entire PK uses `=`;
10//! its empty-result UNKNOWN selects the same rows as FALSE in this positive
11//! AND/OR language. Adding negation would require revisiting that rewrite.
12
13use super::{has_kind, push_lit, push_name, tenant_col, valid, Kind, Table};
14use h5i_app_sql::{Pred, Read, Val, Window};
15
16/// A read's text and its parameters, `$1` (the tenant) first. Every value,
17/// limit and offset is a parameter, so the text depends only on the read's
18/// shape and on whether its window has a limit and a non-zero offset: at
19/// most four texts per shape, whatever the numbers. `OFFSET 0` is left out
20/// on purpose: with an `OFFSET` parameter PostgreSQL estimates a generic plan
21/// as dearer than the custom ones and never reuses it, re-planning on every
22/// execution of the prepared statement.
23#[derive(Clone, Debug, PartialEq, Eq)]
24pub struct ReadQuery {
25    pub text: Vec<u8>,
26    pub params: Vec<Val>,
27}
28
29/// SQL spelling for membership predicates. Both modes preserve the same read
30/// and exact-count semantics; their PostgreSQL execution plans can differ.
31#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
32pub enum ReadSqlMode {
33    /// Always use `IN (SELECT ...)`, retaining the original renderer behavior.
34    #[default]
35    Membership,
36    /// Use scalar `= (SELECT ...)` when all primary-key columns are fixed.
37    /// Other lookups retain `IN`. This is workload-dependent, not always faster.
38    ScalarWhenUnique,
39}
40
41struct Out<'a> {
42    tables: &'a Vec<Table>,
43    mode: ReadSqlMode,
44    text: Vec<u8>,
45    params: Vec<Val>,
46}
47
48fn windowed(w: &Window) -> bool {
49    w.offset > 0 || w.limit.is_some()
50}
51
52/// A column that never holds `NULL`: a key column or a non-nullable one.
53fn not_null(t: &Table, i: usize) -> bool {
54    i < t.key_len as usize || !t.columns[i].nullable
55}
56
57/// A conjunction fixes this key column to one value. Disjunctions and
58/// membership predicates are deliberately not used to infer uniqueness.
59fn fixes_column(p: &Pred, key: u32) -> bool {
60    match p {
61        Pred::Eq { col, .. } => *col == key,
62        Pred::And(a, b) => fixes_column(a, key) || fixes_column(b, key),
63        _ => false,
64    }
65}
66
67/// Every SELECT is already restricted to one tenant. Fixing the remaining
68/// primary-key columns therefore leaves at most one row; a window cannot
69/// increase that cardinality. With no key columns the tenant itself is the PK.
70fn at_most_one(t: &Table, p: &Pred) -> bool {
71    (0..t.key_len).all(|key| fixes_column(p, key))
72}
73
74fn ordered_key(t: &Table) -> bool {
75    t.key_len > 0
76        && t.columns[..t.key_len as usize].iter().all(|c| matches!(c.kind, Kind::Int | Kind::Bool))
77}
78
79fn int_param(n: u64) -> Val {
80    Val::Int(n.min(i64::MAX as u64) as i64)
81}
82
83impl Out<'_> {
84    fn lit(&mut self, s: &str) {
85        push_lit(&mut self.text, s.as_bytes());
86    }
87
88    fn param(&mut self, v: Val) {
89        self.params.push(v);
90        let n = self.params.len();
91        self.lit(&format!("${n}"));
92    }
93
94    fn col(&mut self, alias: usize, name: &Vec<u8>) {
95        self.lit(&format!("q{alias}."));
96        push_name(&mut self.text, name);
97    }
98
99    /// `SELECT <cols> FROM "t" AS q<alias> WHERE tenant AND <pred> [ORDER BY key [LIMIT/OFFSET]]`.
100    fn select(&mut self, table: u32, pred: &Pred, w: &Window, cols: &[usize], alias: usize, order: bool) -> Option<()> {
101        let t = self.tables.get(table as usize)?;
102        if windowed(w) && !ordered_key(t) {
103            return None;
104        }
105        self.lit("SELECT ");
106        for (n, &i) in cols.iter().enumerate() {
107            if n > 0 {
108                self.lit(", ");
109            }
110            self.col(alias, &t.columns.get(i)?.name.clone());
111        }
112        self.lit(" FROM ");
113        push_name(&mut self.text, &t.name);
114        self.lit(&format!(" AS q{alias} WHERE "));
115        self.col(alias, &tenant_col());
116        self.lit(" = $1 AND ");
117        self.pred(t, pred, alias)?;
118        if order || windowed(w) {
119            self.order_by(t, alias, w.desc && windowed(w));
120        }
121        if windowed(w) {
122            if let Some(n) = w.limit {
123                self.lit(" LIMIT ");
124                self.param(int_param(n));
125            }
126            if w.offset > 0 {
127                self.lit(" OFFSET ");
128                self.param(int_param(w.offset));
129            }
130        }
131        Some(())
132    }
133
134    fn order_by(&mut self, t: &Table, alias: usize, desc: bool) {
135        self.lit(" ORDER BY ");
136        for i in 0..t.key_len as usize {
137            if i > 0 {
138                self.lit(", ");
139            }
140            self.col(alias, &t.columns[i].name.clone());
141            if desc {
142                self.lit(" DESC");
143            }
144        }
145    }
146
147    fn pred(&mut self, t: &Table, p: &Pred, alias: usize) -> Option<()> {
148        match p {
149            Pred::All => self.lit("TRUE"),
150            Pred::Eq { col, val } => {
151                let c = t.columns.get(*col as usize)?;
152                if !has_kind(val, c.kind) {
153                    return None;
154                }
155                self.col(alias, &c.name.clone());
156                // The same on a column that is never NULL, and `=` can use an index.
157                self.lit(if not_null(t, *col as usize) { " = " } else { " IS NOT DISTINCT FROM " });
158                self.param(val.clone());
159            }
160            Pred::In { col, table, pred, of, window } => {
161                let i = *col as usize;
162                let c = t.columns.get(i)?;
163                let sub = self.tables.get(*table as usize)?;
164                let j = *of as usize;
165                let d = sub.columns.get(j)?;
166                if c.kind != d.kind || !not_null(t, i) || !not_null(sub, j) {
167                    return None;
168                }
169                self.col(alias, &c.name.clone());
170                // An uncorrelated scalar lookup is evaluated once (an InitPlan)
171                // instead of joining every outer row to the lookup table. Only
172                // use it when the schema's PK proves there cannot be two rows.
173                // Both columns are NOT NULL. On an empty subquery `=` yields
174                // UNKNOWN rather than IN's FALSE; these are interchangeable in
175                // this positive AND/OR predicate language's WHERE selection.
176                self.lit(if self.mode == ReadSqlMode::ScalarWhenUnique && at_most_one(sub, pred) { " = (" } else { " IN (" });
177                self.select(*table, pred, window, &[j], alias + 1, false)?;
178                self.lit(")");
179            }
180            Pred::And(a, b) | Pred::Or(a, b) => {
181                let op = if matches!(p, Pred::And(..)) { " AND " } else { " OR " };
182                self.lit("(");
183                self.pred(t, a, alias)?;
184                self.lit(op);
185                self.pred(t, b, alias)?;
186                self.lit(")");
187            }
188        }
189        Some(())
190    }
191}
192
193/// The `SELECT` returning `q`'s rows of `tenant` (no `tenant_id`) in
194/// ascending key order, or with `count` the number of them. `None` on an
195/// invalid schema, a missing table or column, a value of the wrong type, `IN`
196/// on a nullable column, or a window on a table without an integer key.
197pub fn compile_read(tables: &Vec<Table>, tenant: i64, q: &Read, count: bool) -> Option<ReadQuery> {
198    compile_read_with_mode(tables, tenant, q, count, ReadSqlMode::default())
199}
200
201/// Compile a read or exact count with an explicit SQL strategy. The mode
202/// applies recursively to nested membership predicates; validation is identical.
203pub fn compile_read_with_mode(tables: &Vec<Table>, tenant: i64, q: &Read, count: bool, mode: ReadSqlMode) -> Option<ReadQuery> {
204    if !valid(tables) {
205        return None;
206    }
207    let t = tables.get(q.table as usize)?;
208    let all: Vec<usize> = (0..t.columns.len()).collect();
209    let mut out = Out { tables, mode, text: Vec::new(), params: vec![Val::Int(tenant)] };
210    let desc = q.window.desc && windowed(&q.window);
211    if count {
212        out.lit("SELECT count(*) FROM (");
213        out.select(q.table, &q.pred, &q.window, &all, 0, false)?;
214        out.lit(") AS c");
215    } else if desc {
216        // The window counts from the newest key; the rows come back oldest first.
217        out.lit("SELECT * FROM (");
218        out.select(q.table, &q.pred, &q.window, &all, 0, false)?;
219        out.lit(") AS w ORDER BY ");
220        for i in 0..t.key_len as usize {
221            if i > 0 {
222                out.lit(", ");
223            }
224            push_name(&mut out.text, &t.columns[i].name);
225        }
226    } else {
227        out.select(q.table, &q.pred, &q.window, &all, 0, t.key_len > 0)?;
228    }
229    Some(ReadQuery { text: out.text, params: out.params })
230}
231
232/// `CREATE INDEX IF NOT EXISTS` on `tenant_id` and columns `cols` of table
233/// `table`, so reads that filter on them need not scan the tenant. Indexes
234/// change no result, only speed. `None` on a bad schema, table or column.
235pub fn create_index(tables: &Vec<Table>, table: u32, cols: &[u32]) -> Option<Vec<u8>> {
236    if !valid(tables) || cols.is_empty() {
237        return None;
238    }
239    let t = tables.get(table as usize)?;
240    let mut names = vec![tenant_col()];
241    for &c in cols {
242        names.push(t.columns.get(c as usize)?.name.clone());
243    }
244    // A name PostgreSQL keeps whole: the table's number and the columns'.
245    let mut idx = format!("h5i_idx_{table}");
246    for &c in cols {
247        idx.push_str(&format!("_{c}"));
248    }
249    let mut out = Vec::new();
250    push_lit(&mut out, b"CREATE INDEX IF NOT EXISTS ");
251    push_name(&mut out, &[&t.name[..], b"_", idx.as_bytes()].concat().iter().take(63).copied().collect());
252    push_lit(&mut out, b" ON ");
253    push_name(&mut out, &t.name);
254    push_lit(&mut out, b" (");
255    for (i, n) in names.iter().enumerate() {
256        if i > 0 {
257            push_lit(&mut out, b", ");
258        }
259        push_name(&mut out, n);
260    }
261    push_lit(&mut out, b")");
262    Some(out)
263}
264
265#[cfg(test)]
266mod tests {
267    use super::*;
268    use crate::Column;
269    use h5i_app_sql::whole;
270
271    fn col(name: &str, kind: Kind, nullable: bool) -> Column {
272        Column { name: name.as_bytes().to_vec(), kind, nullable }
273    }
274
275    fn schema() -> Vec<Table> {
276        vec![
277            Table {
278                name: b"articles".to_vec(),
279                columns: vec![col("id", Kind::Int, false), col("author", Kind::Int, false), col("slug", Kind::Bytes, false)],
280                key_len: 1,
281            },
282            Table { name: b"tags".to_vec(), columns: vec![col("article", Kind::Int, false), col("tag", Kind::Bytes, false)], key_len: 2 },
283            Table { name: b"notes".to_vec(), columns: vec![col("tag", Kind::Bytes, false), col("n", Kind::Int, true)], key_len: 1 },
284        ]
285    }
286
287    fn text(q: &ReadQuery) -> String {
288        String::from_utf8(q.text.clone()).unwrap()
289    }
290
291    #[test]
292    fn renders_a_page_of_a_tag() {
293        let q = Read {
294            table: 0,
295            pred: Pred::In {
296                col: 0,
297                table: 1,
298                pred: Box::new(Pred::Eq { col: 1, val: Val::Bytes(b"rust".to_vec()) }),
299                of: 0,
300                window: whole(),
301            },
302            window: Window { desc: true, offset: 0, limit: Some(20) },
303        };
304        let c = compile_read(&schema(), 7, &q, false).unwrap();
305        assert_eq!(
306            text(&c),
307            "SELECT * FROM (SELECT q0.\"id\", q0.\"author\", q0.\"slug\" FROM \"articles\" AS q0 WHERE q0.\"tenant_id\" = $1 \
308             AND q0.\"id\" IN (SELECT q1.\"article\" FROM \"tags\" AS q1 WHERE q1.\"tenant_id\" = $1 AND \
309             q1.\"tag\" = $2) ORDER BY q0.\"id\" DESC LIMIT $3) AS w ORDER BY \"id\""
310        );
311        assert_eq!(c.params, vec![Val::Int(7), Val::Bytes(b"rust".to_vec()), Val::Int(20)]);
312        // Another page size, same text: one prepared statement serves both.
313        let mut other = q.clone();
314        other.window.limit = Some(50);
315        assert_eq!(compile_read(&schema(), 9, &other, false).unwrap().text, c.text);
316        let n = compile_read(&schema(), 7, &q, true).unwrap();
317        assert!(text(&n).starts_with("SELECT count(*) FROM (SELECT q0."));
318    }
319
320    #[test]
321    fn scalar_membership_requires_the_entire_primary_key() {
322        let eq = |col| Pred::Eq { col, val: Val::Int(1) };
323        let s = schema();
324        assert!(at_most_one(&s[0], &eq(0)));
325        assert!(!at_most_one(&s[0], &eq(1)));
326        assert!(!at_most_one(&s[1], &eq(0)));
327        assert!(at_most_one(&s[1], &Pred::And(Box::new(eq(0)), Box::new(eq(1)))));
328        assert!(!at_most_one(&s[0], &Pred::Or(Box::new(eq(0)), Box::new(eq(0)))));
329        let singleton = Table { name: b"counter".to_vec(), columns: vec![col("value", Kind::Int, false)], key_len: 0 };
330        assert!(at_most_one(&singleton, &Pred::All));
331    }
332
333    #[test]
334    fn modes_preserve_parameters_and_default_to_membership() {
335        let q = Read { table: 0, pred: Pred::In {
336            col: 1, table: 0, pred: Box::new(Pred::Eq { col: 0, val: Val::Int(1) }),
337            of: 1, window: whole(),
338        }, window: whole() };
339        assert_eq!(ReadSqlMode::default(), ReadSqlMode::Membership);
340        for count in [false, true] {
341            let old = compile_read(&schema(), 7, &q, count).unwrap();
342            let member = compile_read_with_mode(&schema(), 7, &q, count, ReadSqlMode::Membership).unwrap();
343            let scalar = compile_read_with_mode(&schema(), 7, &q, count, ReadSqlMode::ScalarWhenUnique).unwrap();
344            assert_eq!(old, member);
345            assert!(text(&member).contains(" IN (SELECT"));
346            assert_eq!(text(&member).replace(" IN (SELECT", " = (SELECT"), text(&scalar));
347            assert_eq!(member.params, scalar.params);
348        }
349        // A nonunique lookup must keep IN even in scalar mode.
350        let mut multi = q.clone();
351        if let Pred::In { pred, .. } = &mut multi.pred { **pred = Pred::All; }
352        assert_eq!(compile_read(&schema(), 7, &multi, false),
353            compile_read_with_mode(&schema(), 7, &multi, false, ReadSqlMode::ScalarWhenUnique));
354    }
355
356    #[test]
357    fn rejects_what_the_lean_does_not_mean() {
358        let s = schema();
359        // A window over a text key.
360        let w = Read { table: 2, pred: Pred::All, window: Window { desc: false, offset: 1, limit: None } };
361        assert!(compile_read(&s, 1, &w, false).is_none());
362        // IN on a nullable column.
363        let n = Read {
364            table: 0,
365            pred: Pred::In { col: 1, table: 2, pred: Box::new(Pred::All), of: 1, window: whole() },
366            window: whole(),
367        };
368        assert!(compile_read(&s, 1, &n, false).is_none());
369        // A value of the wrong type, a missing column.
370        assert!(compile_read(&s, 1, &h5i_app_sql::read_eq(0, 2, Val::Int(1)), false).is_none());
371        assert!(compile_read(&s, 1, &h5i_app_sql::read_eq(0, 9, Val::Int(1)), false).is_none());
372        assert!(compile_read(&s, 1, &h5i_app_sql::read_all(0), false).is_some());
373    }
374
375    #[test]
376    fn renders_an_index() {
377        let i = create_index(&schema(), 0, &[2]).unwrap();
378        assert_eq!(
379            String::from_utf8(i).unwrap(),
380            "CREATE INDEX IF NOT EXISTS \"articles_h5i_idx_0_2\" ON \"articles\" (\"tenant_id\", \"slug\")"
381        );
382        assert!(create_index(&schema(), 0, &[9]).is_none());
383    }
384}