Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions changelog.d/rp-617-declare-not-one-type-name.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
### A PL/pgSQL declaration whose type is not one type name is refused

A DECLARE such as `x int(3)` or `x varchar(nosuch)` used to be created, and
fail later or never. The Rust PostgreSQL server now refuses it at CREATE as
PostgreSQL does: `typeStringToTypeName` reads the text with the grammar, and
text that is not one type name is a 42601 syntax error at its token, with
the context `invalid type name "..."`.

#### Fixed

- A PL/pgSQL DECLARE whose type text is not one type name is 42601 at the
token the grammar stops at, placed in the CREATE's text.
- Fixes RP-617 (tests/backlog/test_rp_found_rp573.py::test_declare_of_text_that_is_not_one_type_name)
11 changes: 11 additions & 0 deletions changelog.d/rp-865-quoted-declared-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
### A PL/pgSQL variable of a quoted type name keeps its case

`declare x "Over845"` used to look up the type `over845`, and fail with
42704. The Rust PostgreSQL server now reads a declaration's type as the
grammar does, keeping a quoted name's case, so a variable of a mixed-case
domain is created and its constraints are checked when it is assigned.

#### Fixed

- A PL/pgSQL declaration of a quoted, mixed-case type name keeps its case.
- Fixes RP-865 (tests/backlog/test_rp_found_rp839.py::test_a_plpgsql_declaration_keeps_a_quoted_types_case)
15 changes: 15 additions & 0 deletions changelog.d/rp-867-escaped-literal-position.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
### An escape-string literal's input error is placed

`select E'a\tb'::int4` now reports its 22P02 at the literal, position 8, as
PostgreSQL 15.19 does. The error position was found by matching the value the
message quotes against each literal's spelling, and an escape-string literal
whose escapes decode to something else (a real tab for `\t`) never matched.
The literal's escapes are now decoded as the scanner decodes them before the
comparison, for plain casts and for `int2vector` / `oidvector` input alike.

#### Fixed

- Rust PG server: an input error from an `E'...'` literal holding an escape
carries the literal's position.

Fixes RP-867 (tests/backlog/test_rp_found_rp605.py::test_escaped_literal_input_error_has_a_position)
7 changes: 6 additions & 1 deletion crates/secantus-pg/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14614,13 +14614,18 @@ impl PgHandler {
}
None => match secantus_pgplan::plpgsql_declared_type(&d.typname) {
Ok(()) => continue,
Err(refused @ secantus_pgplan::DeclaredTypeError::SyntaxAt(_, at)) => {
at_percent = Some(at);
refused
}
Err(refused) => refused,
},
};
let offset =
plpgsql_fn::declared_type_offset(body, d).map(|at| at + at_percent.unwrap_or(0));
let (e, at_type) = match refused {
secantus_pgplan::DeclaredTypeError::AtType(e) => (e, true),
secantus_pgplan::DeclaredTypeError::AtType(e)
| secantus_pgplan::DeclaredTypeError::SyntaxAt(e, _) => (e, true),
secantus_pgplan::DeclaredTypeError::Compile(e) => (e, false),
secantus_pgplan::DeclaredTypeError::PseudoType(t) => (
secantus_pgplan::Error::Sqlstate(
Expand Down
67 changes: 60 additions & 7 deletions crates/secantus-pg/src/plpgsql_fn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1013,13 +1013,26 @@ pub fn declared_type_offset(body: &str, d: &Declared) -> Option<usize> {
}

/// A type name as PL/pgSQL records it (`pg_catalog.int4`, `int `,
/// `pg_catalog."integer"`) in the server's canonical spelling.
/// `pg_catalog."integer"`) in the server's canonical spelling. A quoted
/// name keeps its case, as the grammar reads it (RP-865); the rest folds.
fn canonical_type(raw: &str) -> String {
let t = raw
.trim()
.trim_start_matches("pg_catalog.")
.replace('"', "");
let t = t.trim().to_ascii_lowercase();
let t = raw.trim();
let t = t.strip_prefix("pg_catalog.").unwrap_or(t);
let mut folded = String::with_capacity(t.len());
let mut chars = t.chars().peekable();
let mut quoted = false;
while let Some(c) = chars.next() {
match c {
'"' if quoted && chars.peek() == Some(&'"') => {
chars.next();
folded.push('"');
}
'"' => quoted = !quoted,
c if quoted => folded.push(c),
c => folded.push(c.to_ascii_lowercase()),
}
}
let t = folded.trim().to_string();
let (base, array) = match t.strip_suffix("[]") {
Some(b) => (b.trim().to_string(), true),
None => (t, false),
Expand Down Expand Up @@ -3683,8 +3696,34 @@ fn length_read_type(base: &str) -> String {
}
}

/// A canonical type (see [`canonical_type`]) as SQL text: a name the
/// grammar would fold (`Over845`) is quoted, so the cast names the same
/// type (RP-865). A modifier or `[]` after the name is kept as written.
fn sql_type(ty: &str) -> String {
ty.to_string()
let plain = |w: &str| {
w.chars()
.next()
.is_some_and(|c| c.is_ascii_lowercase() || c == '_')
&& w.chars()
.all(|c| c.is_ascii_lowercase() || c.is_ascii_digit() || c == '_' || c == '$')
};
let end = ty.find(['(', '[']).unwrap_or(ty.len());
let (name, rest) = ty.split_at(end);
let name = name.trim_end();
if name.contains(' ') || name.split('.').all(plain) {
return ty.to_string();
}
let quoted: Vec<String> = name
.split('.')
.map(|w| {
if plain(w) {
w.to_string()
} else {
format!("\"{}\"", w.replace('"', "\"\""))
}
})
.collect();
format!("{}{rest}", quoted.join("."))
}

/// A `CALL`'s arguments as written: the variable each names (a named
Expand Down Expand Up @@ -3730,6 +3769,20 @@ mod rp573_declarations {
/// The variables a body declares, with their type text as written and
/// their line; a routine's parameters and the compiler's own variables
/// are not among them (RP-573).
/// A quoted type name keeps its case, and is quoted again where it is
/// spliced into a cast (RP-865).
#[test]
fn a_quoted_type_keeps_its_case() {
assert_eq!(canonical_type("\"Over845\""), "Over845");
assert_eq!(canonical_type("public.\"My\"\"T\""), "public.My\"T");
assert_eq!(canonical_type("pg_catalog.\"integer\""), "int4");
assert_eq!(canonical_type("VARCHAR(3)"), "varchar(3)");
assert_eq!(sql_type("Over845"), "\"Over845\"");
assert_eq!(sql_type("public.My\"T[]"), "public.\"My\"\"T\"[]");
assert_eq!(sql_type("varchar(3)"), "varchar(3)");
assert_eq!(sql_type("double precision"), "double precision");
}

#[test]
fn declared_types_are_the_bodys() {
let sql = "create function f573(a int) returns int language plpgsql as $$\n\
Expand Down
106 changes: 103 additions & 3 deletions crates/secantus-pgplan/src/errpos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,68 @@ use super::*;
struct Tok<'a> {
start: usize,
text: &'a str,
/// An escape-string literal with an escape: the plain literal its
/// value spells (`E'a\\tb'` as `'a<tab>b'`).
decoded: Option<String>,
}

/// The plain literal an escape-string literal's body (`a\\tb`, between
/// `E'` and `'`) reads as, its escapes decoded as the scanner decodes them
/// (`scan.l`'s `xe` rules): `\b \f \n \r \t`, octal `\ooo`, hex `\xhh`,
/// `\uXXXX`, `\UXXXXXXXX`, any other `\c` as `c`, and `''` as `'`.
/// `None` when an escape names no character.
fn decode_escape_literal(body: &str) -> Option<String> {
let mut out = String::with_capacity(body.len());
let mut it = body.chars().peekable();
let take = |it: &mut std::iter::Peekable<std::str::Chars>, n: usize, radix: u32| {
let mut v = 0u32;
let mut k = 0;
while k < n {
match it.peek().and_then(|c| c.to_digit(radix)) {
Some(d) => {
v = v * radix + d;
it.next();
k += 1;
}
None => break,
}
}
(k > 0).then_some(v)
};
while let Some(c) = it.next() {
match c {
'\'' => {
it.next_if_eq(&'\'');
out.push('\'');
}
'\\' => match it.next()? {
'b' => out.push('\x08'),
'f' => out.push('\x0c'),
'n' => out.push('\n'),
'r' => out.push('\r'),
't' => out.push('\t'),
d @ '0'..='7' => {
let mut v = d as u32 - '0' as u32;
for _ in 0..2 {
match it.peek().and_then(|c| c.to_digit(8)) {
Some(x) => {
v = v * 8 + x;
it.next();
}
None => break,
}
}
out.push(char::from_u32(v)?);
}
'x' => out.push(char::from_u32(take(&mut it, 2, 16)?)?),
'u' => out.push(char::from_u32(take(&mut it, 4, 16)?)?),
'U' => out.push(char::from_u32(take(&mut it, 8, 16)?)?),
other => out.push(other),
},
c => out.push(c),
}
}
Some(out)
}

fn unquote_ident(text: &str) -> String {
Expand Down Expand Up @@ -93,7 +155,9 @@ fn element_literal(toks: &[Tok], value: &str) -> Option<usize> {
w[1].text == "::"
&& crate::vectors::is_vector(&w[2].text.to_ascii_lowercase())
&& w[0]
.text
.decoded
.as_deref()
.unwrap_or(w[0].text)
.strip_prefix('\'')
.and_then(|r| r.strip_suffix('\''))
.is_some_and(|body| vector_tail(&body.replace("''", "'")))
Expand All @@ -111,7 +175,7 @@ fn input_literal(toks: &[Tok], value: &str) -> Option<usize> {
let lit = format!("'{}'", value.replace('\'', "''"));
let i = toks
.iter()
.position(|t| t.text == lit)
.position(|t| t.text == lit || t.decoded.as_deref() == Some(lit.as_str()))
.or_else(|| element_literal(toks, value))?;
// A literal cast to a string type first is a text VALUE when the
// target's input function reads it, at execution, where there is
Expand Down Expand Up @@ -324,11 +388,20 @@ pub fn error_position(sql: &str, sqlstate: &str, message: &str) -> Option<usize>
// its `E`: under `standard_conforming_strings = off` every
// plain literal reaches here rewritten as one
// (`escape_strings`), and the `E` maps back to its quote.
let decoded = text
.get(..2)
.filter(|p| p.eq_ignore_ascii_case("E'") && text.contains('\\'))
.and_then(|_| decode_escape_literal(text.get(2..text.len().checked_sub(1)?)?))
.map(|v| format!("'{}'", v.replace('\'', "''")));
let text = match text.get(..2) {
Some("E'" | "e'") => &text[1..],
_ => text,
};
Some(Tok { start: s, text })
Some(Tok {
start: s,
text,
decoded,
})
})
.collect();
let pos = |start: usize| Some(sql.get(..start)?.chars().count() + 1);
Expand Down Expand Up @@ -809,3 +882,30 @@ fn regtype_literal_names(tok: &str, full: &str) -> bool {
text.to_ascii_lowercase() == full
}
}

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

#[test]
fn an_escaped_literal_input_error_is_placed_at_its_e() {
let msg = "invalid input syntax for type integer: \"a\tb\"";
assert_eq!(
error_position("select E'a\\tb'::int4", "22P02", msg),
Some(8)
);
let msg = "invalid input syntax for type smallint: \"1\t2\"";
assert_eq!(
error_position("select E'1\\t2'::int2vector", "22P02", msg),
Some(8)
);
}

#[test]
fn escapes_decode_as_the_scanner_reads_them() {
assert_eq!(
decode_escape_literal(r"a\101\x42C\q''").as_deref(),
Some("aABCq'")
);
}
}
Loading
Loading