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

BigQuery: Support trailing commas in column definitions list #1682

Merged
merged 1 commit into from
Jan 25, 2025
Merged
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
5 changes: 5 additions & 0 deletions src/dialect/bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ impl Dialect for BigQueryDialect {
true
}

/// See <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_table_statement>
fn supports_column_definition_trailing_commas(&self) -> bool {
true
}

fn is_identifier_start(&self, ch: char) -> bool {
ch.is_ascii_lowercase() || ch.is_ascii_uppercase() || ch == '_'
}
Expand Down
9 changes: 8 additions & 1 deletion src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,18 @@ pub trait Dialect: Debug + Any {
}

/// Returns true if the dialect supports trailing commas in the `FROM` clause of a `SELECT` statement.
/// /// Example: `SELECT 1 FROM T, U, LIMIT 1`
/// Example: `SELECT 1 FROM T, U, LIMIT 1`
fn supports_from_trailing_commas(&self) -> bool {
false
}

/// Returns true if the dialect supports trailing commas in the
/// column definitions list of a `CREATE` statement.
/// Example: `CREATE TABLE T (x INT, y TEXT,)`
fn supports_column_definition_trailing_commas(&self) -> bool {
false
}

/// Returns true if the dialect supports double dot notation for object names
///
/// Example
Expand Down
12 changes: 10 additions & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6718,7 +6718,11 @@ impl<'a> Parser<'a> {
return self.expected("',' or ')' after column definition", self.peek_token());
};

if rparen && (!comma || self.options.trailing_commas) {
if rparen
&& (!comma
|| self.dialect.supports_column_definition_trailing_commas()
|| self.options.trailing_commas)
{
let _ = self.consume_token(&Token::RParen);
break;
}
Expand Down Expand Up @@ -9298,7 +9302,11 @@ impl<'a> Parser<'a> {
self.next_token();
Ok(vec![])
} else {
let cols = self.parse_comma_separated(Parser::parse_view_column)?;
let cols = self.parse_comma_separated_with_trailing_commas(
Parser::parse_view_column,
self.dialect.supports_column_definition_trailing_commas(),
None,
)?;
self.expect_token(&Token::RParen)?;
Ok(cols)
}
Expand Down
59 changes: 42 additions & 17 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10189,15 +10189,19 @@ fn parse_trailing_comma() {
"Expected: column name or constraint definition, found: )".to_string()
)
);

let unsupported_dialects = all_dialects_where(|d| !d.supports_trailing_commas());
assert_eq!(
unsupported_dialects
.parse_sql_statements("SELECT * FROM track ORDER BY milliseconds,")
.unwrap_err(),
ParserError::ParserError("Expected: an expression, found: EOF".to_string())
);
}

#[test]
fn parse_projection_trailing_comma() {
// Some dialects allow trailing commas only in the projection
let trailing_commas = TestedDialects::new(vec![
Box::new(SnowflakeDialect {}),
Box::new(BigQueryDialect {}),
]);
let trailing_commas = all_dialects_where(|d| d.supports_projection_trailing_commas());

trailing_commas.one_statement_parses_to(
"SELECT album_id, name, FROM track",
Expand All @@ -10210,20 +10214,14 @@ fn parse_projection_trailing_comma() {

trailing_commas.verified_stmt("SELECT DISTINCT ON (album_id) name FROM track");

let unsupported_dialects = all_dialects_where(|d| {
!d.supports_projection_trailing_commas() && !d.supports_trailing_commas()
});
assert_eq!(
trailing_commas
.parse_sql_statements("SELECT * FROM track ORDER BY milliseconds,")
unsupported_dialects
.parse_sql_statements("SELECT album_id, name, FROM track")
.unwrap_err(),
ParserError::ParserError("Expected: an expression, found: EOF".to_string())
);

assert_eq!(
trailing_commas
.parse_sql_statements("CREATE TABLE employees (name text, age int,)")
.unwrap_err(),
ParserError::ParserError(
"Expected: column name or constraint definition, found: )".to_string()
),
ParserError::ParserError("Expected an expression, found: FROM".to_string())
);
}

Expand Down Expand Up @@ -13061,6 +13059,33 @@ fn parse_overlaps() {
verified_stmt("SELECT (DATE '2016-01-10', DATE '2016-02-01') OVERLAPS (DATE '2016-01-20', DATE '2016-02-10')");
}

#[test]
fn parse_column_definition_trailing_commas() {
let dialects = all_dialects_where(|d| d.supports_column_definition_trailing_commas());

dialects.one_statement_parses_to("CREATE TABLE T (x INT64,)", "CREATE TABLE T (x INT64)");
dialects.one_statement_parses_to(
"CREATE TABLE T (x INT64, y INT64, )",
"CREATE TABLE T (x INT64, y INT64)",
);
dialects.one_statement_parses_to(
"CREATE VIEW T (x, y, ) AS SELECT 1",
"CREATE VIEW T (x, y) AS SELECT 1",
);

let unsupported_dialects = all_dialects_where(|d| {
!d.supports_projection_trailing_commas() && !d.supports_trailing_commas()
});
assert_eq!(
unsupported_dialects
.parse_sql_statements("CREATE TABLE employees (name text, age int,)")
.unwrap_err(),
ParserError::ParserError(
"Expected: column name or constraint definition, found: )".to_string()
),
);
}

#[test]
fn test_trailing_commas_in_from() {
let dialects = all_dialects_where(|d| d.supports_from_trailing_commas());
Expand Down
Loading