Skip to content

Databricks: support SELECT * EXCEPT #1261

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

Merged
merged 1 commit into from
May 13, 2024
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 @@ -54,4 +54,9 @@ impl Dialect for BigQueryDialect {
fn supports_parenthesized_set_variables(&self) -> bool {
true
}

// See https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#select_except
fn supports_select_wildcard_except(&self) -> bool {
true
}
}
4 changes: 4 additions & 0 deletions src/dialect/clickhouse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ impl Dialect for ClickHouseDialect {
fn supports_string_literal_backslash_escape(&self) -> bool {
true
}

fn supports_select_wildcard_except(&self) -> bool {
true
}
}
5 changes: 5 additions & 0 deletions src/dialect/databricks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,9 @@ impl Dialect for DatabricksDialect {
fn supports_lambda_functions(&self) -> bool {
true
}

// https://docs.databricks.com/en/sql/language-manual/sql-ref-syntax-qry-select.html#syntax
fn supports_select_wildcard_except(&self) -> bool {
true
}
}
4 changes: 4 additions & 0 deletions src/dialect/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,8 @@ impl Dialect for GenericDialect {
fn supports_parenthesized_set_variables(&self) -> bool {
true
}

fn supports_select_wildcard_except(&self) -> bool {
true
}
}
10 changes: 10 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,16 @@ pub trait Dialect: Debug + Any {
fn supports_parenthesized_set_variables(&self) -> bool {
false
}
/// Returns true if the dialect supports an `EXCEPT` clause following a
/// wildcard in a select list.
///
/// For example
/// ```sql
/// SELECT * EXCEPT order_id FROM orders;
/// ```
fn supports_select_wildcard_except(&self) -> bool {
false
}
/// Returns true if the dialect has a CONVERT function which accepts a type first
/// and an expression second, e.g. `CONVERT(varchar, 1)`
fn convert_type_before_value(&self) -> bool {
Expand Down
3 changes: 1 addition & 2 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9704,8 +9704,7 @@ impl<'a> Parser<'a> {
} else {
None
};
let opt_except = if dialect_of!(self is GenericDialect | BigQueryDialect | ClickHouseDialect)
{
let opt_except = if self.dialect.supports_select_wildcard_except() {
self.parse_optional_select_item_except()?
} else {
None
Expand Down
32 changes: 0 additions & 32 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1838,38 +1838,6 @@ fn parse_array_agg_func() {
}
}

#[test]
fn test_select_wildcard_with_except() {
let select = bigquery_and_generic().verified_only_select("SELECT * EXCEPT (col_a) FROM data");
let expected = SelectItem::Wildcard(WildcardAdditionalOptions {
opt_except: Some(ExceptSelectItem {
first_element: Ident::new("col_a"),
additional_elements: vec![],
}),
..Default::default()
});
assert_eq!(expected, select.projection[0]);

let select = bigquery_and_generic()
.verified_only_select("SELECT * EXCEPT (department_id, employee_id) FROM employee_table");
let expected = SelectItem::Wildcard(WildcardAdditionalOptions {
opt_except: Some(ExceptSelectItem {
first_element: Ident::new("department_id"),
additional_elements: vec![Ident::new("employee_id")],
}),
..Default::default()
});
assert_eq!(expected, select.projection[0]);

assert_eq!(
bigquery_and_generic()
.parse_sql_statements("SELECT * EXCEPT () FROM employee_table")
.unwrap_err()
.to_string(),
"sql parser error: Expected identifier, found: )"
);
}

#[test]
fn parse_big_query_declare() {
for (sql, expected_names, expected_data_type, expected_assigned_expr) in [
Expand Down
34 changes: 34 additions & 0 deletions tests/sqlparser_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9854,3 +9854,37 @@ fn tests_select_values_without_parens_and_set_op() {
_ => panic!("Expected a SET OPERATION"),
}
}

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

let select = dialects.verified_only_select("SELECT * EXCEPT (col_a) FROM data");
let expected = SelectItem::Wildcard(WildcardAdditionalOptions {
opt_except: Some(ExceptSelectItem {
first_element: Ident::new("col_a"),
additional_elements: vec![],
}),
..Default::default()
});
assert_eq!(expected, select.projection[0]);

let select = dialects
.verified_only_select("SELECT * EXCEPT (department_id, employee_id) FROM employee_table");
let expected = SelectItem::Wildcard(WildcardAdditionalOptions {
opt_except: Some(ExceptSelectItem {
first_element: Ident::new("department_id"),
additional_elements: vec![Ident::new("employee_id")],
}),
..Default::default()
});
assert_eq!(expected, select.projection[0]);

assert_eq!(
dialects
.parse_sql_statements("SELECT * EXCEPT () FROM employee_table")
.unwrap_err()
.to_string(),
"sql parser error: Expected identifier, found: )"
);
}
Loading