sqlparser/dialect/bigquery.rs
1// Licensed to the Apache Software Foundation (ASF) under one
2// or more contributor license agreements. See the NOTICE file
3// distributed with this work for additional information
4// regarding copyright ownership. The ASF licenses this file
5// to you under the Apache License, Version 2.0 (the
6// "License"); you may not use this file except in compliance
7// with the License. You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing,
12// software distributed under the License is distributed on an
13// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14// KIND, either express or implied. See the License for the
15// specific language governing permissions and limitations
16// under the License.
17
18use crate::ast::Statement;
19use crate::dialect::Dialect;
20use crate::keywords::Keyword;
21use crate::parser::{Parser, ParserError};
22
23/// These keywords are disallowed as column identifiers. Such that
24/// `SELECT 5 AS <col> FROM T` is rejected by BigQuery.
25const RESERVED_FOR_COLUMN_ALIAS: &[Keyword] = &[
26 Keyword::WITH,
27 Keyword::SELECT,
28 Keyword::WHERE,
29 Keyword::GROUP,
30 Keyword::HAVING,
31 Keyword::ORDER,
32 Keyword::LATERAL,
33 Keyword::LIMIT,
34 Keyword::FETCH,
35 Keyword::UNION,
36 Keyword::EXCEPT,
37 Keyword::INTERSECT,
38 Keyword::FROM,
39 Keyword::INTO,
40 Keyword::END,
41];
42
43/// A [`Dialect`] for [Google Bigquery](https://cloud.google.com/bigquery/)
44#[derive(Debug, Default)]
45pub struct BigQueryDialect;
46
47impl Dialect for BigQueryDialect {
48 fn parse_statement(&self, parser: &mut Parser) -> Option<Result<Statement, ParserError>> {
49 self.maybe_parse_statement(parser)
50 }
51
52 /// See <https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#identifiers>
53 fn is_delimited_identifier_start(&self, ch: char) -> bool {
54 ch == '`'
55 }
56
57 fn supports_projection_trailing_commas(&self) -> bool {
58 true
59 }
60
61 /// See <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_table_statement>
62 fn supports_column_definition_trailing_commas(&self) -> bool {
63 true
64 }
65
66 fn is_identifier_start(&self, ch: char) -> bool {
67 ch.is_ascii_lowercase() || ch.is_ascii_uppercase() || ch == '_'
68 // BigQuery supports `@@foo.bar` variable syntax in its procedural language.
69 // https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#beginexceptionend
70 || ch == '@'
71 }
72
73 fn is_identifier_part(&self, ch: char) -> bool {
74 ch.is_ascii_lowercase() || ch.is_ascii_uppercase() || ch.is_ascii_digit() || ch == '_'
75 }
76
77 /// See [doc](https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#quoted_literals)
78 fn supports_triple_quoted_string(&self) -> bool {
79 true
80 }
81
82 /// See [doc](https://cloud.google.com/bigquery/docs/reference/standard-sql/navigation_functions#first_value)
83 fn supports_window_function_null_treatment_arg(&self) -> bool {
84 true
85 }
86
87 // See https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#escape_sequences
88 fn supports_string_literal_backslash_escape(&self) -> bool {
89 true
90 }
91
92 /// See [doc](https://cloud.google.com/bigquery/docs/reference/standard-sql/window-function-calls#ref_named_window)
93 fn supports_window_clause_named_window_reference(&self) -> bool {
94 true
95 }
96
97 /// See [doc](https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#set)
98 fn supports_parenthesized_set_variables(&self) -> bool {
99 true
100 }
101
102 // See https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#select_except
103 fn supports_select_wildcard_except(&self) -> bool {
104 true
105 }
106
107 fn require_interval_qualifier(&self) -> bool {
108 true
109 }
110
111 // See https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#constructing_a_struct
112 fn supports_struct_literal(&self) -> bool {
113 true
114 }
115
116 /// See <https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#select_expression_star>
117 fn supports_select_expr_star(&self) -> bool {
118 true
119 }
120
121 /// See <https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#execute_immediate>
122 fn supports_execute_immediate(&self) -> bool {
123 true
124 }
125
126 // See <https://cloud.google.com/bigquery/docs/access-historical-data>
127 fn supports_timestamp_versioning(&self) -> bool {
128 true
129 }
130
131 // See <https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#group_by_clause>
132 fn supports_group_by_expr(&self) -> bool {
133 true
134 }
135
136 fn is_column_alias(&self, kw: &Keyword, _parser: &mut Parser) -> bool {
137 !RESERVED_FOR_COLUMN_ALIAS.contains(kw)
138 }
139}
140
141impl BigQueryDialect {
142 fn maybe_parse_statement(&self, parser: &mut Parser) -> Option<Result<Statement, ParserError>> {
143 if parser.peek_keyword(Keyword::BEGIN) {
144 return Some(self.parse_begin(parser));
145 }
146 None
147 }
148
149 /// Parse a `BEGIN` statement.
150 /// <https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#beginexceptionend>
151 fn parse_begin(&self, parser: &mut Parser) -> Result<Statement, ParserError> {
152 parser.expect_keyword(Keyword::BEGIN)?;
153
154 let statements = parser.parse_statement_list(&[Keyword::EXCEPTION, Keyword::END])?;
155
156 let has_exception_when_clause = parser.parse_keywords(&[
157 Keyword::EXCEPTION,
158 Keyword::WHEN,
159 Keyword::ERROR,
160 Keyword::THEN,
161 ]);
162 let exception_statements = if has_exception_when_clause {
163 if !parser.peek_keyword(Keyword::END) {
164 Some(parser.parse_statement_list(&[Keyword::END])?)
165 } else {
166 Some(Default::default())
167 }
168 } else {
169 None
170 };
171
172 parser.expect_keyword(Keyword::END)?;
173
174 Ok(Statement::StartTransaction {
175 begin: true,
176 statements,
177 exception_statements,
178 has_end_keyword: true,
179 transaction: None,
180 modifier: None,
181 modes: Default::default(),
182 })
183 }
184}