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