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_projection_trailing_commas(&self) -> bool {
76 true
77 }
78
79 /// See <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_table_statement>
80 fn supports_column_definition_trailing_commas(&self) -> bool {
81 true
82 }
83
84 fn is_identifier_start(&self, ch: char) -> bool {
85 ch.is_ascii_lowercase() || ch.is_ascii_uppercase() || ch == '_'
86 // BigQuery supports `@@foo.bar` variable syntax in its procedural language.
87 // https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#beginexceptionend
88 || ch == '@'
89 }
90
91 fn is_identifier_part(&self, ch: char) -> bool {
92 ch.is_ascii_lowercase() || ch.is_ascii_uppercase() || ch.is_ascii_digit() || ch == '_'
93 }
94
95 /// See [doc](https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#quoted_literals)
96 fn supports_triple_quoted_string(&self) -> bool {
97 true
98 }
99
100 /// See [doc](https://cloud.google.com/bigquery/docs/reference/standard-sql/navigation_functions#first_value)
101 fn supports_window_function_null_treatment_arg(&self) -> bool {
102 true
103 }
104
105 // See https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#escape_sequences
106 fn supports_string_literal_backslash_escape(&self) -> bool {
107 true
108 }
109
110 /// See [doc](https://cloud.google.com/bigquery/docs/reference/standard-sql/window-function-calls#ref_named_window)
111 fn supports_window_clause_named_window_reference(&self) -> bool {
112 true
113 }
114
115 /// See [doc](https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#set)
116 fn supports_parenthesized_set_variables(&self) -> bool {
117 true
118 }
119
120 // See https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#select_except
121 fn supports_select_wildcard_except(&self) -> bool {
122 true
123 }
124
125 fn require_interval_qualifier(&self) -> bool {
126 true
127 }
128
129 // See https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#constructing_a_struct
130 fn supports_struct_literal(&self) -> bool {
131 true
132 }
133
134 /// See <https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#select_expression_star>
135 fn supports_select_expr_star(&self) -> bool {
136 true
137 }
138
139 /// BigQuery allows a query to start with `FROM` (e.g. `FROM t`, and the
140 /// entry form for pipe syntax, `FROM t |> ...`).
141 ///
142 /// See <https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#from_queries>
143 fn supports_from_first_select(&self) -> bool {
144 true
145 }
146
147 /// See <https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#execute_immediate>
148 fn supports_execute_immediate(&self) -> bool {
149 true
150 }
151
152 // See <https://cloud.google.com/bigquery/docs/access-historical-data>
153 fn supports_table_versioning(&self) -> bool {
154 true
155 }
156
157 // See <https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#group_by_clause>
158 fn supports_group_by_expr(&self) -> bool {
159 true
160 }
161
162 fn is_column_alias(&self, kw: &Keyword, _parser: &mut Parser) -> bool {
163 !RESERVED_FOR_COLUMN_ALIAS.contains(kw)
164 }
165
166 fn supports_pipe_operator(&self) -> bool {
167 true
168 }
169
170 fn supports_create_table_multi_schema_info_sources(&self) -> bool {
171 true
172 }
173
174 /// See <https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#select_replace>
175 fn supports_select_wildcard_replace(&self) -> bool {
176 true
177 }
178
179 fn supports_comma_separated_trim(&self) -> bool {
180 true
181 }
182}