Skip to main content

cufflink_sql_parse/
lib.rs

1//! PostgreSQL's own parser for Cufflink guests that accept SQL they did not
2//! write.
3//!
4//! - [`bounds`]: what is refused before the parser runs.
5//! - [`wire`]: how the platform talks to a parser process.
6//! - [`golden`]: statements and the answers this release gives them.
7//! - With the `parser` feature, the parse itself (`pg_query`), which the
8//!   parser process and the native test host run.
9
10pub mod bounds;
11#[cfg(feature = "parser")]
12mod canonical;
13pub mod golden;
14#[cfg(feature = "parser")]
15mod parse;
16pub mod wire;
17
18#[cfg(feature = "parser")]
19pub use parse::{fingerprint, fingerprint_with, parse, parse_on_this_thread, parsed, STACK_BYTES};
20
21/// The `pg_query` release that parses, which fixes PostgreSQL's grammar.
22pub const PARSER: &str = "6.2.1";
23
24/// The probe corpus: one statement a line.
25pub const PROBE: &str = include_str!("../probe.sql");
26
27/// The SHA-256 of [`PROBE`]'s trees as [`PARSER`] reads them: for each
28/// statement in order, its tree's canonical text and a `\n`. A parser
29/// process whose fingerprint differs is not used.
30pub const PROBE_FINGERPRINT: &str =
31    "98e4c9b72f500c15ea6a9d2c34124cc5a5aedf1b1d5a33e94754473b1a1c2030";
32
33/// The deepest a tree's text may nest, in objects and arrays. A guest
34/// reads the answer with `serde_json`, which stops at 128 levels, and the
35/// answer wraps the tree in two more; a deeper tree is `TooLarge`.
36pub const MAX_TREE_NESTING: usize = 120;
37
38/// What surrounds a tree's canonical text in the `sql_parse` host
39/// function's answer, a `ParseResponse::Ok`, so that the tree is written
40/// into the answer as it is read and never parsed again on the way.
41pub fn answer_around() -> (String, &'static str) {
42    (
43        format!(r#"{{"ok":{{"fingerprint":"{PROBE_FINGERPRINT}","parser":"{PARSER}","tree":"#),
44        "}}",
45    )
46}
47
48#[cfg(test)]
49mod tests {
50    use super::*;
51    use cufflink_types::sql_parse::{ParseResponse, Parsed};
52
53    #[test]
54    fn an_answer_around_a_tree_is_the_ok_response() {
55        let (before, after) = answer_around();
56        let answer = format!(r#"{before}{{"stmts":[],"version":170007}}{after}"#);
57        assert_eq!(
58            serde_json::from_str::<ParseResponse>(&answer).expect("decodes"),
59            ParseResponse::Ok(Parsed {
60                tree: serde_json::json!({"stmts": [], "version": 170007}),
61                parser: PARSER.to_string(),
62                fingerprint: PROBE_FINGERPRINT.to_string(),
63            })
64        );
65    }
66
67    fn nested(levels: usize) -> String {
68        format!("{}{}", "[".repeat(levels), "]".repeat(levels))
69    }
70
71    #[test]
72    fn a_tree_at_the_nesting_limit_is_read_back_and_a_deeper_one_is_not() {
73        let (before, after) = answer_around();
74        let decodes =
75            |tree: &str| serde_json::from_str::<ParseResponse>(&format!("{before}{tree}{after}"));
76        assert!(decodes(&nested(MAX_TREE_NESTING)).is_ok());
77        assert!(decodes(&nested(MAX_TREE_NESTING + 8)).is_err());
78    }
79}