From 0c44ef1d61ce330e8fb53708cf1b86fe255e2a5f Mon Sep 17 00:00:00 2001 From: blaginin Date: Tue, 1 Dec 2026 14:30:45 -0800 Subject: [PATCH] Generic: Keep clause keywords usable as unquoted column and table names --- src/dialect/generic.rs | 7 ++++++ src/dialect/mod.rs | 16 ++++++++++++ src/dialect/postgresql.rs | 6 +++++ src/parser/mod.rs | 3 +++ tests/sqlparser_common.rs | 52 ++++++++++++++++++++++++++++++++++++++- 5 files changed, 83 insertions(+), 1 deletion(-) diff --git a/src/dialect/generic.rs b/src/dialect/generic.rs index fdf3529054..7aeaf71286 100644 --- a/src/dialect/generic.rs +++ b/src/dialect/generic.rs @@ -16,6 +16,8 @@ // under the License. use crate::dialect::Dialect; +use crate::keywords::Keyword; +use crate::parser::Parser; /// A permissive, general purpose [`Dialect`], which parses a wide variety of SQL /// statements, from many different dialects. @@ -233,6 +235,11 @@ impl Dialect for GenericDialect { true } + /// The generic dialect reserves no keyword as a table name. + fn is_table_factor(&self, _kw: &Keyword, _parser: &mut Parser) -> bool { + true + } + fn supports_nested_comments(&self) -> bool { true } diff --git a/src/dialect/mod.rs b/src/dialect/mod.rs index b975805b5f..5ab761e837 100644 --- a/src/dialect/mod.rs +++ b/src/dialect/mod.rs @@ -825,6 +825,22 @@ pub trait Dialect: Debug + Any { false } + /// Return true if the dialect supports a `SELECT` with neither a target list nor a + /// `FROM` clause, so that the keyword of a later clause may directly follow `SELECT`. + /// Only consulted when [`Self::supports_empty_projections`] is true. + /// + /// Example + /// ```sql + /// SELECT WHERE true + /// SELECT UNION SELECT + /// ``` + /// + /// A dialect that accepts those keywords as unquoted column names must return false, + /// otherwise `SELECT offset FROM t` loses its target list. + fn supports_empty_projections_without_from(&self) -> bool { + false + } + /// Return true if the dialect supports wildcard expansion on /// arbitrary expressions in projections. /// diff --git a/src/dialect/postgresql.rs b/src/dialect/postgresql.rs index 4e7265cb02..585be51c27 100644 --- a/src/dialect/postgresql.rs +++ b/src/dialect/postgresql.rs @@ -499,6 +499,12 @@ impl Dialect for PostgreSqlDialect { true } + /// Every keyword that can follow an empty target list is reserved, see + /// + fn supports_empty_projections_without_from(&self) -> bool { + true + } + fn supports_nested_comments(&self) -> bool { true } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index 2e653c7983..00a7691c86 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -16858,6 +16858,9 @@ impl<'a> Parser<'a> { /// Returns true if the next token ends a `SELECT` target list before it begins. fn peek_empty_projection(&self) -> bool { + if !self.dialect.supports_empty_projections_without_from() { + return self.peek_keyword(Keyword::FROM); + } match &self.peek_token_ref().token { Token::EOF | Token::SemiColon | Token::RParen => true, Token::Word(w) => matches!( diff --git a/tests/sqlparser_common.rs b/tests/sqlparser_common.rs index 5c396d9da3..4c1f36d650 100644 --- a/tests/sqlparser_common.rs +++ b/tests/sqlparser_common.rs @@ -17778,6 +17778,10 @@ fn parse_deeply_nested_boolean_expr_does_not_stackoverflow() { fn parse_select_without_projection() { let dialects = all_dialects_where(|d| d.supports_empty_projections()); dialects.verified_stmt("SELECT FROM users"); + + let dialects = all_dialects_where(|d| { + d.supports_empty_projections() && d.supports_empty_projections_without_from() + }); for sql in [ "SELECT", "SELECT ALL", @@ -17810,13 +17814,59 @@ fn parse_select_without_projection() { ParserError::ParserError("Expected: an expression, found: EOF".to_string()) ); - let dialects = all_dialects_where(|d| !d.supports_empty_projections()); + let dialects = all_dialects_where(|d| { + !d.supports_empty_projections() || !d.supports_empty_projections_without_from() + }); assert_eq!( dialects.parse_sql_statements("SELECT").unwrap_err(), ParserError::ParserError("Expected: an expression, found: EOF".to_string()) ); } +#[test] +fn parse_generic_clause_keyword_as_first_select_item() { + let generic = TestedDialects::new(vec![Box::new(GenericDialect {})]); + for keyword in [ + "into", + "where", + "group", + "having", + "window", + "order", + "limit", + "offset", + "fetch", + "for", + "union", + "intersect", + "except", + ] { + for sql in [ + format!("SELECT {keyword} FROM t"), + format!("SELECT {keyword}, a FROM t"), + format!("SELECT {keyword} + 1 FROM t"), + format!("SELECT DISTINCT {keyword} FROM t"), + ] { + let select = generic.verified_only_select(&sql); + assert!(!select.projection.is_empty(), "{sql}"); + assert_eq!(select.from.len(), 1, "{sql}"); + } + } +} + +#[test] +fn parse_generic_reserved_keyword_as_table_name() { + let generic = TestedDialects::new(vec![Box::new(GenericDialect {})]); + for keyword in ["into", "limit", "having", "where", "on"] { + generic.verified_stmt(&format!("SELECT * FROM {keyword}")); + generic.verified_stmt(&format!("SELECT * FROM {keyword} AS x")); + generic.verified_stmt(&format!("SELECT * FROM t JOIN {keyword} ON true")); + generic.verified_stmt(&format!( + "WITH {keyword} AS (SELECT 1) SELECT * FROM {keyword}" + )); + } +} + #[test] fn parse_update_from_before_select() { verified_stmt("UPDATE t1 FROM (SELECT name, id FROM t1 GROUP BY id) AS t2 SET name = t2.name WHERE t1.id = t2.id");