Skip to content
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
9 changes: 9 additions & 0 deletions src/ast/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3340,20 +3340,29 @@ impl fmt::Display for OpenJsonTableColumn {
/// BigQuery supports ValueTables which have 2 modes:
/// `SELECT AS STRUCT`
/// `SELECT AS VALUE`
///
/// They can be combined with `[ { ALL | DISTINCT } ]`, e.g.
/// `SELECT DISTINCT AS STRUCT`
///
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#value_tables>
/// <https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#select_list>
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "visitor", derive(Visit, VisitMut))]
pub enum ValueTableMode {
AsStruct,
AsValue,
DistinctAsStruct,
DistinctAsValue,
}

impl fmt::Display for ValueTableMode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
ValueTableMode::AsStruct => write!(f, "AS STRUCT"),
ValueTableMode::AsValue => write!(f, "AS VALUE"),
ValueTableMode::DistinctAsStruct => write!(f, "DISTINCT AS STRUCT"),
ValueTableMode::DistinctAsValue => write!(f, "DISTINCT AS VALUE"),
}
}
}
Expand Down
17 changes: 14 additions & 3 deletions src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11505,8 +11505,16 @@ impl<'a> Parser<'a> {
}

let select_token = self.expect_keyword(Keyword::SELECT)?;
let value_table_mode =
if dialect_of!(self is BigQueryDialect) && self.parse_keyword(Keyword::AS) {
let value_table_mode = if dialect_of!(self is BigQueryDialect) {
if self.parse_keywords(&[Keyword::DISTINCT, Keyword::AS]) {
if self.parse_keyword(Keyword::VALUE) {
Some(ValueTableMode::DistinctAsValue)
} else if self.parse_keyword(Keyword::STRUCT) {
Some(ValueTableMode::DistinctAsStruct)
} else {
self.expected("VALUE or STRUCT", self.peek_token())?
}
} else if self.parse_keyword(Keyword::AS) {
if self.parse_keyword(Keyword::VALUE) {
Some(ValueTableMode::AsValue)
} else if self.parse_keyword(Keyword::STRUCT) {
Expand All @@ -11516,7 +11524,10 @@ impl<'a> Parser<'a> {
}
} else {
None
};
}
} else {
None
};

let mut top_before_distinct = false;
let mut top = None;
Expand Down
11 changes: 11 additions & 0 deletions tests/sqlparser_bigquery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2377,3 +2377,14 @@ fn test_any_type() {
fn test_any_type_dont_break_custom_type() {
bigquery_and_generic().verified_stmt("CREATE TABLE foo (x ANY)");
}

#[test]
fn test_select_distinct_as_struct_or_value() {
for sql in [
"SELECT DISTINCT AS STRUCT a, ABS(b) FROM UNNEST(c) AS T",
"SELECT DISTINCT AS VALUE a, ABS(b) FROM UNNEST(c) AS T",
"SELECT ARRAY(SELECT DISTINCT AS STRUCT a, b, ABS(c) AS c, ABS(d) AS d FROM UNNEST(e) AS T)",
] {
bigquery().verified_stmt(sql);
}
}