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};
22use crate::tokenizer::Token;
23
24/// These keywords are disallowed as column identifiers. Such that
25/// `SELECT 5 AS <col> FROM T` is rejected by BigQuery.
26const RESERVED_FOR_COLUMN_ALIAS: &[Keyword] = &[
27 Keyword::WITH,
28 Keyword::SELECT,
29 Keyword::WHERE,
30 Keyword::GROUP,
31 Keyword::HAVING,
32 Keyword::ORDER,
33 Keyword::LATERAL,
34 Keyword::LIMIT,
35 Keyword::FETCH,
36 Keyword::UNION,
37 Keyword::EXCEPT,
38 Keyword::INTERSECT,
39 Keyword::FROM,
40 Keyword::INTO,
41 Keyword::END,
42];
43
44/// A [`Dialect`] for [Google Bigquery](https://cloud.google.com/bigquery/)
45#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
46#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
47pub struct BigQueryDialect;
48
49impl Dialect for BigQueryDialect {
50 fn parse_statement(&self, parser: &mut Parser) -> Option<Result<Statement, ParserError>> {
51 if parser.parse_keyword(Keyword::BEGIN) {
52 if parser.peek_keyword(Keyword::TRANSACTION)
53 || parser.peek_token_ref().token == Token::SemiColon
54 || parser.peek_token_ref().token == Token::EOF
55 {
56 parser.prev_token();
57 return None;
58 }
59 return Some(parser.parse_begin_exception_end());
60 }
61
62 None
63 }
64
65 /// See <https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#identifiers>
66 fn is_delimited_identifier_start(&self, ch: char) -> bool {
67 ch == '`'
68 }
69
70 /// See <https://docs.cloud.google.com/bigquery/docs/reference/standard-sql/lexical#quoted_identifiers>
71 fn identifier_quote_style(&self, _identifier: &str) -> Option<char> {
72 Some('`')
73 }
74
75 fn supports_hash_comment(&self) -> bool {
76 true
77 }
78
79 fn supports_projection_trailing_commas(&self) -> bool {
80 true
81 }
82
83 /// See <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_table_statement>
84 fn supports_column_definition_trailing_commas(&self) -> bool {
85 true
86 }
87
88 fn is_identifier_start(&self, ch: char) -> bool {
89 ch.is_ascii_lowercase() || ch.is_ascii_uppercase() || ch == '_'
90 // BigQuery supports `@@foo.bar` variable syntax in its procedural language.
91 // https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#beginexceptionend
92 || ch == '@'
93 }
94
95 fn is_identifier_part(&self, ch: char) -> bool {
96 ch.is_ascii_lowercase() || ch.is_ascii_uppercase() || ch.is_ascii_digit() || ch == '_'
97 }
98
99 /// See [doc](https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#quoted_literals)
100 fn supports_triple_quoted_string(&self) -> bool {
101 true
102 }
103
104 /// See [doc](https://cloud.google.com/bigquery/docs/reference/standard-sql/navigation_functions#first_value)
105 fn supports_window_function_null_treatment_arg(&self) -> bool {
106 true
107 }
108
109 // See https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#escape_sequences
110 fn supports_string_literal_backslash_escape(&self) -> bool {
111 true
112 }
113
114 /// See [doc](https://cloud.google.com/bigquery/docs/reference/standard-sql/window-function-calls#ref_named_window)
115 fn supports_window_clause_named_window_reference(&self) -> bool {
116 true
117 }
118
119 /// See [doc](https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#set)
120 fn supports_parenthesized_set_variables(&self) -> bool {
121 true
122 }
123
124 // See https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#select_except
125 fn supports_select_wildcard_except(&self) -> bool {
126 true
127 }
128
129 fn require_interval_qualifier(&self) -> bool {
130 true
131 }
132
133 // See https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#constructing_a_struct
134 fn supports_struct_literal(&self) -> bool {
135 true
136 }
137
138 /// See <https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#select_expression_star>
139 fn supports_select_expr_star(&self) -> bool {
140 true
141 }
142
143 /// BigQuery allows a query to start with `FROM` (e.g. `FROM t`, and the
144 /// entry form for pipe syntax, `FROM t |> ...`).
145 ///
146 /// See <https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#from_queries>
147 fn supports_from_first_select(&self) -> bool {
148 true
149 }
150
151 /// See <https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#execute_immediate>
152 fn supports_execute_immediate(&self) -> bool {
153 true
154 }
155
156 // See <https://cloud.google.com/bigquery/docs/access-historical-data>
157 fn supports_table_versioning(&self) -> bool {
158 true
159 }
160
161 // See <https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#group_by_clause>
162 fn supports_group_by_expr(&self) -> bool {
163 true
164 }
165
166 fn is_column_alias(&self, kw: &Keyword, _parser: &mut Parser) -> bool {
167 !RESERVED_FOR_COLUMN_ALIAS.contains(kw)
168 }
169
170 fn supports_pipe_operator(&self) -> bool {
171 true
172 }
173
174 fn supports_create_table_multi_schema_info_sources(&self) -> bool {
175 true
176 }
177
178 /// See <https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#select_replace>
179 fn supports_select_wildcard_replace(&self) -> bool {
180 true
181 }
182
183 fn supports_comma_separated_trim(&self) -> bool {
184 true
185 }
186
187 /// See <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#alter_table_add_column_statement>
188 fn supports_add_column_if_not_exists(&self) -> bool {
189 true
190 }
191}