From 1a07c5d67c13c0b635cfe0b3e6b13b5be6cae275 Mon Sep 17 00:00:00 2001 From: Ophir LOJKINE Date: Thu, 15 Feb 2024 12:34:38 +0100 Subject: [PATCH] accept JSON_TABLE both as an unquoted table name and a table-valued function (#1134) --- src/parser/mod.rs | 6 +++++- tests/sqlparser_postgres.rs | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 210d157d..c2397c74 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -7515,7 +7515,11 @@ impl<'a> Parser<'a> { with_offset, with_offset_alias, }) - } else if self.parse_keyword(Keyword::JSON_TABLE) { + } else if matches!( + self.peek_token().token, Token::Word(w) + if w.keyword == Keyword::JSON_TABLE && self.peek_nth_token(1).token == Token::LParen + ) { + self.expect_keyword(Keyword::JSON_TABLE)?; self.expect_token(&Token::LParen)?; let json_expr = self.parse_expr()?; self.expect_token(&Token::Comma)?; diff --git a/tests/sqlparser_postgres.rs b/tests/sqlparser_postgres.rs index 45a3dbed..c987822b 100644 --- a/tests/sqlparser_postgres.rs +++ b/tests/sqlparser_postgres.rs @@ -2328,6 +2328,21 @@ fn test_json() { ); } +#[test] +fn parse_json_table_is_not_reserved() { + // JSON_TABLE is not a reserved keyword in PostgreSQL, even though it is in SQL:2023 + // see: https://en.wikipedia.org/wiki/List_of_SQL_reserved_words + let Select { from, .. } = pg_and_generic().verified_only_select("SELECT * FROM JSON_TABLE"); + assert_eq!(1, from.len()); + match &from[0].relation { + TableFactor::Table { + name: ObjectName(name), + .. + } => assert_eq!("JSON_TABLE", name[0].value), + other => panic!("Expected JSON_TABLE to be parsed as a table name, but got {other:?}"), + } +} + #[test] fn test_composite_value() { let sql = "SELECT (on_hand.item).name FROM on_hand WHERE (on_hand.item).price > 9";