PostgreSQL: Parse the SHOW argument as one dotted name
diff --git a/src/ast/mod.rs b/src/ast/mod.rs index cefead4..e692d1a 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs
@@ -2534,6 +2534,35 @@ } } +#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] +#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] +#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))] +/// The argument of a `SHOW` statement, either a GUC name or one of the fixed +/// multi-word forms from the [PostgreSQL grammar](https://www.postgresql.org/docs/current/sql-show.html). +pub enum ShowVariableName { + /// A dot-separated, possibly quoted name such as `custom.my_guc`. + Name(ObjectName), + /// The fixed `SHOW TIME ZONE` form. + TimeZone, + /// The fixed `SHOW TRANSACTION ISOLATION LEVEL` form. + TransactionIsolationLevel, + /// The fixed `SHOW SESSION AUTHORIZATION` form. + SessionAuthorization, +} + +impl fmt::Display for ShowVariableName { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + ShowVariableName::Name(name) => write!(f, "{name}"), + ShowVariableName::TimeZone => f.write_str("TIME ZONE"), + ShowVariableName::TransactionIsolationLevel => { + f.write_str("TRANSACTION ISOLATION LEVEL") + } + ShowVariableName::SessionAuthorization => f.write_str("SESSION AUTHORIZATION"), + } + } +} + #[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)] #[cfg_attr(feature = "serde", derive(Serialize, Deserialize))] #[cfg_attr(feature = "visitor", derive(Visit, VisitMut))] @@ -4228,8 +4257,8 @@ /// /// Note: this is a PostgreSQL-specific statement. ShowVariable { - /// Variable name as one or more identifiers. - variable: Vec<Ident>, + /// The GUC name or fixed multi-word form to show. + variable: ShowVariableName, }, /// ```sql /// SHOW [GLOBAL | SESSION] STATUS [LIKE 'pattern' | WHERE expr] @@ -5898,11 +5927,7 @@ } Self::Set(set) => write!(f, "{set}"), Statement::ShowVariable { variable } => { - write!(f, "SHOW")?; - if !variable.is_empty() { - write!(f, " {}", display_separated(variable, " "))?; - } - Ok(()) + write!(f, "SHOW {variable}") } Statement::ShowStatus { filter,
diff --git a/src/parser/mod.rs b/src/parser/mod.rs index d523b48..92fad3b 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs
@@ -16816,6 +16816,23 @@ /// Parse a `SHOW` statement and dispatch to specific SHOW handlers. pub fn parse_show(&mut self) -> Result<Statement, ParserError> { + // The fixed PostgreSQL forms take no scope keywords, so they must be + // matched before SESSION is consumed as a scope. + if self.parse_keywords(&[Keyword::SESSION, Keyword::AUTHORIZATION]) { + return Ok(Statement::ShowVariable { + variable: ShowVariableName::SessionAuthorization, + }); + } + if self.parse_keywords(&[Keyword::TRANSACTION, Keyword::ISOLATION, Keyword::LEVEL]) { + return Ok(Statement::ShowVariable { + variable: ShowVariableName::TransactionIsolationLevel, + }); + } + if self.parse_keywords(&[Keyword::TIME, Keyword::ZONE]) { + return Ok(Statement::ShowVariable { + variable: ShowVariableName::TimeZone, + }); + } let terse = self.parse_keyword(Keyword::TERSE); let extended = self.parse_keyword(Keyword::EXTENDED); let full = self.parse_keyword(Keyword::FULL); @@ -16873,7 +16890,7 @@ self.parse_show_charset(true) } else { Ok(Statement::ShowVariable { - variable: self.parse_identifiers()?, + variable: ShowVariableName::Name(self.parse_object_name(false)?), }) } }
diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index 9f85cd0..1ea748f 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs
@@ -2360,21 +2360,19 @@ #[test] fn parse_show() { - let stmt = pg_and_generic().verified_stmt("SHOW a a"); + // A run of bare words is not a variable name. assert_eq!( - stmt, - Statement::ShowVariable { - variable: vec!["a".into(), "a".into()] - } + pg_and_generic() + .parse_sql_statements("SHOW a a") + .unwrap_err(), + ParserError::ParserError("Expected: end of statement, found: a".to_string()) ); - - let stmt = pg_and_generic().verified_stmt("SHOW ALL ALL"); assert_eq!( - stmt, - Statement::ShowVariable { - variable: vec!["ALL".into(), "ALL".into()] - } - ) + pg_and_generic() + .parse_sql_statements("SHOW ALL ALL") + .unwrap_err(), + ParserError::ParserError("Expected: end of statement, found: ALL".to_string()) + ); } #[test] @@ -12291,3 +12289,77 @@ ); } } + +#[test] +fn parse_show_dotted_variable() { + pg_and_generic().one_statement_parses_to("SHOW custom.my_guc", "SHOW custom.my_guc"); + pg_and_generic().one_statement_parses_to( + "SHOW custom.my.qualified.guc", + "SHOW custom.my.qualified.guc", + ); + pg_and_generic().one_statement_parses_to("SHOW search_path", "SHOW search_path"); + pg_and_generic().one_statement_parses_to(r#"SHOW "custom".my_guc"#, r#"SHOW "custom".my_guc"#); + pg_and_generic() + .one_statement_parses_to(r#"SHOW custom."bad-guc""#, r#"SHOW custom."bad-guc""#); + + let stmt = pg_and_generic().verified_stmt(r#"SHOW custom."bad-guc""#); + assert_eq!( + stmt, + Statement::ShowVariable { + variable: ShowVariableName::Name(ObjectName::from(vec![ + Ident::new("custom"), + Ident::with_quote('"', "bad-guc") + ])) + } + ); + + // The fixed multi-word forms from the PostgreSQL grammar. + pg_and_generic().one_statement_parses_to("SHOW ALL", "SHOW ALL"); + pg_and_generic().one_statement_parses_to("SHOW all", "SHOW all"); + pg_and_generic().one_statement_parses_to("SHOW time zone", "SHOW TIME ZONE"); + let stmt = pg_and_generic().verified_stmt("SHOW TIME ZONE"); + assert_eq!( + stmt, + Statement::ShowVariable { + variable: ShowVariableName::TimeZone + } + ); + pg_and_generic().one_statement_parses_to( + "SHOW transaction isolation level", + "SHOW TRANSACTION ISOLATION LEVEL", + ); + let stmt = pg_and_generic().verified_stmt("SHOW TRANSACTION ISOLATION LEVEL"); + assert_eq!( + stmt, + Statement::ShowVariable { + variable: ShowVariableName::TransactionIsolationLevel + } + ); + pg_and_generic() + .one_statement_parses_to("SHOW session authorization", "SHOW SESSION AUTHORIZATION"); + let stmt = pg_and_generic().verified_stmt("SHOW SESSION AUTHORIZATION"); + assert_eq!( + stmt, + Statement::ShowVariable { + variable: ShowVariableName::SessionAuthorization + } + ); + + // A GUC name is a single dot-separated run, not arbitrary words. + pg_and_generic() + .parse_sql_statements("SHOW 123") + .unwrap_err(); + pg_and_generic() + .parse_sql_statements("SHOW a.b c") + .unwrap_err(); + // Trailing strings are rejected instead of silently dropped. + pg_and_generic() + .parse_sql_statements("SHOW search_path 'x'") + .unwrap_err(); + pg_and_generic() + .parse_sql_statements("SHOW TIME ZONE extra") + .unwrap_err(); + pg_and_generic() + .parse_sql_statements("SHOW SESSION AUTHORIZATION extra") + .unwrap_err(); +}