Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support trailing commas in FROM clause #1645

Merged
merged 8 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
10 changes: 10 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,11 @@ pub trait Dialect: Debug + Any {
self.supports_trailing_commas()
}

/// Does the dialect support trailing commas in the FROM clause list?
barsela1 marked this conversation as resolved.
Show resolved Hide resolved
fn supports_from_trailing_commas(&self) -> bool {
barsela1 marked this conversation as resolved.
Show resolved Hide resolved
false
}

/// Returns true if the dialect supports double dot notation for object names
///
/// Example
Expand Down Expand Up @@ -758,6 +763,11 @@ pub trait Dialect: Debug + Any {
keywords::RESERVED_FOR_IDENTIFIER.contains(&kw)
}

// Returns list of keyword allowed after from
barsela1 marked this conversation as resolved.
Show resolved Hide resolved
fn get_reserved_keyword_after_from(&self) -> &[Keyword] {
keywords::ALLOWED_KEYWORD_AFTER_FROM
}

/// Returns true if this dialect supports the `TABLESAMPLE` option
/// before the table alias option. For example:
///
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ impl Dialect for SnowflakeDialect {
true
}

fn supports_from_trailing_commas(&self) -> bool {
true
}

// Snowflake supports double-dot notation when the schema name is not specified
// In this case the default PUBLIC schema is used
//
Expand Down
10 changes: 10 additions & 0 deletions src/keywords.rs
Original file line number Diff line number Diff line change
Expand Up @@ -975,6 +975,16 @@ pub const RESERVED_FOR_COLUMN_ALIAS: &[Keyword] = &[
Keyword::END,
];

// Global list of reserved keywords alloweed after FROM.
// Parser should call Dialect::get_reserved_keyword_after_from
// to allow for each dialect to customize the list.
pub const ALLOWED_KEYWORD_AFTER_FROM: &[Keyword] = &[
barsela1 marked this conversation as resolved.
Show resolved Hide resolved
Keyword::INTO,
Keyword::LIMIT,
Keyword::HAVING,
Keyword::WHERE,
];

/// Global list of reserved keywords that cannot be parsed as identifiers
/// without special handling like quoting. Parser should call `Dialect::is_reserved_for_identifier`
/// to allow for each dialect to customize the list.
Expand Down
52 changes: 41 additions & 11 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3921,7 +3921,11 @@ impl<'a> Parser<'a> {
let trailing_commas =
self.options.trailing_commas | self.dialect.supports_projection_trailing_commas();

self.parse_comma_separated_with_trailing_commas(|p| p.parse_select_item(), trailing_commas)
self.parse_comma_separated_with_trailing_commas(
|p| p.parse_select_item(),
trailing_commas,
self.default_reserved_keywords(),
)
}

pub fn parse_actions_list(&mut self) -> Result<Vec<ParsedAction>, ParserError> {
Expand All @@ -3947,20 +3951,31 @@ impl<'a> Parser<'a> {
Ok(values)
}

//Parse a 'FROM' statment. support trailing comma
pub fn parse_from(&mut self) -> Result<Vec<TableWithJoins>, ParserError> {
barsela1 marked this conversation as resolved.
Show resolved Hide resolved
let trailing_commas = self.dialect.supports_from_trailing_commas();

self.parse_comma_separated_with_trailing_commas(
Parser::parse_table_and_joins,
trailing_commas,
self.dialect.get_reserved_keyword_after_from(),
)
}

/// Parse the comma of a comma-separated syntax element.
/// Allows for control over trailing commas
/// Returns true if there is a next element
fn is_parse_comma_separated_end_with_trailing_commas(&mut self, trailing_commas: bool) -> bool {
fn is_parse_comma_separated_end_with_trailing_commas(
&mut self,
trailing_commas: bool,
reserved_keywords: &[Keyword],
) -> bool {
if !self.consume_token(&Token::Comma) {
true
} else if trailing_commas {
let token = self.peek_token().token;
match token {
Token::Word(ref kw)
if keywords::RESERVED_FOR_COLUMN_ALIAS.contains(&kw.keyword) =>
{
true
}
Token::Word(ref kw) if reserved_keywords.contains(&kw.keyword) => true,
Token::RParen | Token::SemiColon | Token::EOF | Token::RBracket | Token::RBrace => {
true
}
Expand All @@ -3974,15 +3989,26 @@ impl<'a> Parser<'a> {
/// Parse the comma of a comma-separated syntax element.
/// Returns true if there is a next element
fn is_parse_comma_separated_end(&mut self) -> bool {
self.is_parse_comma_separated_end_with_trailing_commas(self.options.trailing_commas)
self.is_parse_comma_separated_end_with_trailing_commas(
self.options.trailing_commas,
self.default_reserved_keywords(),
)
}

fn default_reserved_keywords(&self) -> &'static [Keyword] {
keywords::RESERVED_FOR_COLUMN_ALIAS
}
barsela1 marked this conversation as resolved.
Show resolved Hide resolved

/// Parse a comma-separated list of 1+ items accepted by `F`
pub fn parse_comma_separated<T, F>(&mut self, f: F) -> Result<Vec<T>, ParserError>
where
F: FnMut(&mut Parser<'a>) -> Result<T, ParserError>,
{
self.parse_comma_separated_with_trailing_commas(f, self.options.trailing_commas)
self.parse_comma_separated_with_trailing_commas(
f,
self.options.trailing_commas,
self.default_reserved_keywords(),
)
}

/// Parse a comma-separated list of 1+ items accepted by `F`
Expand All @@ -3991,14 +4017,18 @@ impl<'a> Parser<'a> {
&mut self,
mut f: F,
trailing_commas: bool,
reserved_keywords: &[Keyword],
) -> Result<Vec<T>, ParserError>
where
F: FnMut(&mut Parser<'a>) -> Result<T, ParserError>,
{
let mut values = vec![];
loop {
values.push(f(self)?);
if self.is_parse_comma_separated_end_with_trailing_commas(trailing_commas) {
if self.is_parse_comma_separated_end_with_trailing_commas(
trailing_commas,
reserved_keywords,
) {
break;
}
}
Expand Down Expand Up @@ -9951,7 +9981,7 @@ impl<'a> Parser<'a> {
// or `from`.

let from = if self.parse_keyword(Keyword::FROM) {
self.parse_comma_separated(Parser::parse_table_and_joins)?
self.parse_from()?
} else {
vec![]
};
Expand Down
31 changes: 31 additions & 0 deletions tests/sqlparser_snowflake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2483,6 +2483,37 @@ fn test_sf_trailing_commas() {
snowflake().verified_only_select_with_canonical("SELECT 1, 2, FROM t", "SELECT 1, 2 FROM t");
}

#[test]
barsela1 marked this conversation as resolved.
Show resolved Hide resolved
fn test_sf_trailing_commas_in_from() {
snowflake().verified_only_select_with_canonical("SELECT 1, 2 FROM t,", "SELECT 1, 2 FROM t");
}

#[test]
fn test_sf_trailing_commas_multiple_sources() {
snowflake()
.verified_only_select_with_canonical("SELECT 1, 2 FROM t1, t2,", "SELECT 1, 2 FROM t1, t2");
}

#[test]
fn test_from_trailing_commas_with_limit() {
let sql = "SELECT a, FROM b, LIMIT 1";
barsela1 marked this conversation as resolved.
Show resolved Hide resolved
let _ = snowflake().parse_sql_statements(sql).unwrap();
}

#[test]
fn test_sf_trailing_commas_multiple_subqueries() {
snowflake().verified_only_select_with_canonical(
"SELECT 1, 2 FROM (SELECT * FROM t1), (SELECT * FROM t2),",
"SELECT 1, 2 FROM (SELECT * FROM t1), (SELECT * FROM t2)",
);
}

#[test]
fn test_from_with_nested_trailing_commas() {
let sql = "SELECT 1, 2 FROM (SELECT * FROM t,),";
barsela1 marked this conversation as resolved.
Show resolved Hide resolved
let _ = snowflake().parse_sql_statements(sql).unwrap();
}

#[test]
fn test_select_wildcard_with_ilike() {
let select = snowflake_and_generic().verified_only_select(r#"SELECT * ILIKE '%id%' FROM tbl"#);
Expand Down
Loading