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
15 changes: 15 additions & 0 deletions src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,15 @@ pub enum SelectItem {
/// The alias for the expression.
alias: Ident,
},
/// An expression, followed by `[ AS ] (alias1, alias2, ...)`
///
/// [Spark SQL](https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select.html)
ExprWithAliases {
/// The expression being projected.
expr: Expr,
/// The list of aliases for the expression.
aliases: Vec<Ident>,
},
/// An expression, followed by a wildcard expansion.
/// e.g. `alias.*`, `STRUCT<STRING>('foo').*`
QualifiedWildcard(SelectItemQualifiedWildcardKind, WildcardAdditionalOptions),
Expand Down Expand Up @@ -1175,6 +1184,12 @@ impl fmt::Display for SelectItem {
f.write_str(" AS ")?;
alias.fmt(f)
}
SelectItem::ExprWithAliases { expr, aliases } => {
expr.fmt(f)?;
f.write_str(" AS (")?;
display_comma_separated(aliases).fmt(f)?;
f.write_str(")")
}
SelectItem::QualifiedWildcard(kind, additional_options) => {
kind.fmt(f)?;
additional_options.fmt(f)
Expand Down
3 changes: 3 additions & 0 deletions src/ast/spans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1821,6 +1821,9 @@ impl Spanned for SelectItem {
match self {
SelectItem::UnnamedExpr(expr) => expr.span(),
SelectItem::ExprWithAlias { expr, alias } => expr.span().union(&alias.span),
SelectItem::ExprWithAliases { expr, aliases } => {
union_spans(iter::once(expr.span()).chain(aliases.iter().map(|i| i.span)))
}
SelectItem::QualifiedWildcard(kind, wildcard_additional_options) => union_spans(
[kind.span()]
.into_iter()
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/databricks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,8 @@ impl Dialect for DatabricksDialect {
fn supports_optimize_table(&self) -> bool {
true
}

fn supports_select_item_multi_column_alias(&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 @@ -288,4 +288,8 @@ impl Dialect for GenericDialect {
fn supports_comma_separated_trim(&self) -> bool {
true
}

fn supports_select_item_multi_column_alias(&self) -> bool {
true
}
}
11 changes: 11 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1664,6 +1664,17 @@ pub trait Dialect: Debug + Any {
fn supports_comma_separated_trim(&self) -> bool {
false
}

/// Returns true if the dialect supports parenthesized multi-column
/// aliases in SELECT items. For example:
/// ```sql
/// SELECT stack(2, 'a', 'b') AS (col1, col2)
/// ```
///
/// [Spark SQL](https://spark.apache.org/docs/latest/sql-ref-syntax-qry-select.html)
fn supports_select_item_multi_column_alias(&self) -> bool {
false
}
}

/// Operators for which precedence must be defined.
Expand Down
13 changes: 13 additions & 0 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18074,6 +18074,19 @@ impl<'a> Parser<'a> {
self.parse_wildcard_additional_options(wildcard_token)?,
))
}
expr if self.dialect.supports_select_item_multi_column_alias()
&& self.peek_keyword(Keyword::AS)
&& self.peek_nth_token(1).token == Token::LParen =>
{
self.expect_keyword(Keyword::AS)?;
self.expect_token(&Token::LParen)?;
let aliases = self.parse_comma_separated(|p| p.parse_identifier())?;
self.expect_token(&Token::RParen)?;
Ok(SelectItem::ExprWithAliases {
expr: maybe_prefixed_expr(expr, prefix),
aliases,
})
}
expr => self
.maybe_parse_select_item_alias()
.map(|alias| match alias {
Expand Down
14 changes: 14 additions & 0 deletions tests/sqlparser_databricks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,3 +644,17 @@ fn parse_databricks_json_accessor() {
"SELECT raw:store.bicycle.price::DOUBLE FROM store_data",
);
}

#[test]
fn parse_select_item_multi_column_alias() {
databricks_and_generic().verified_stmt("SELECT stack(2, 'a', 'b', 'c', 'd') AS (col1, col2)");

databricks_and_generic()
.verified_stmt("SELECT stack(2, 'a', 'b', 'c', 'd') AS (col1, col2) FROM t");

assert!(
all_dialects_where(|d| !d.supports_select_item_multi_column_alias())
.parse_sql_statements("SELECT stack(2, 'a', 'b') AS (col1, col2)")
.is_err()
);
}
Loading