Skip to content
Open
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
7 changes: 7 additions & 0 deletions src/dialect/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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
}
Expand Down
16 changes: 16 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
6 changes: 6 additions & 0 deletions src/dialect/postgresql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,12 @@ impl Dialect for PostgreSqlDialect {
true
}

/// Every keyword that can follow an empty target list is reserved, see
/// <https://www.postgresql.org/docs/current/sql-keywords-appendix.html>
fn supports_empty_projections_without_from(&self) -> bool {
true
}

fn supports_nested_comments(&self) -> bool {
true
}
Expand Down
3 changes: 3 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
52 changes: 51 additions & 1 deletion tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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");
Expand Down
Loading