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)]
46pub struct BigQueryDialect;
47
48impl Dialect for BigQueryDialect {
49    fn parse_statement(&self, parser: &mut Parser) -> Option<Result<Statement, ParserError>> {
50        if parser.parse_keyword(Keyword::BEGIN) {
51            if parser.peek_keyword(Keyword::TRANSACTION)
52                || parser.peek_token_ref().token == Token::SemiColon
53                || parser.peek_token_ref().token == Token::EOF
54            {
55                parser.prev_token();
56                return None;
57            }
58            return Some(parser.parse_begin_exception_end());
59        }
60
61        None
62    }
63
64    /// See <https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#identifiers>
65    fn is_delimited_identifier_start(&self, ch: char) -> bool {
66        ch == '`'
67    }
68
69    fn supports_projection_trailing_commas(&self) -> bool {
70        true
71    }
72
73    /// See <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_table_statement>
74    fn supports_column_definition_trailing_commas(&self) -> bool {
75        true
76    }
77
78    fn is_identifier_start(&self, ch: char) -> bool {
79        ch.is_ascii_lowercase() || ch.is_ascii_uppercase() || ch == '_'
80            // BigQuery supports `@@foo.bar` variable syntax in its procedural language.
81            // https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#beginexceptionend
82            || ch == '@'
83    }
84
85    fn is_identifier_part(&self, ch: char) -> bool {
86        ch.is_ascii_lowercase() || ch.is_ascii_uppercase() || ch.is_ascii_digit() || ch == '_'
87    }
88
89    /// See [doc](https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#quoted_literals)
90    fn supports_triple_quoted_string(&self) -> bool {
91        true
92    }
93
94    /// See [doc](https://cloud.google.com/bigquery/docs/reference/standard-sql/navigation_functions#first_value)
95    fn supports_window_function_null_treatment_arg(&self) -> bool {
96        true
97    }
98
99    // See https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#escape_sequences
100    fn supports_string_literal_backslash_escape(&self) -> bool {
101        true
102    }
103
104    /// See [doc](https://cloud.google.com/bigquery/docs/reference/standard-sql/window-function-calls#ref_named_window)
105    fn supports_window_clause_named_window_reference(&self) -> bool {
106        true
107    }
108
109    /// See [doc](https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#set)
110    fn supports_parenthesized_set_variables(&self) -> bool {
111        true
112    }
113
114    // See https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#select_except
115    fn supports_select_wildcard_except(&self) -> bool {
116        true
117    }
118
119    fn require_interval_qualifier(&self) -> bool {
120        true
121    }
122
123    // See https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#constructing_a_struct
124    fn supports_struct_literal(&self) -> bool {
125        true
126    }
127
128    /// See <https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#select_expression_star>
129    fn supports_select_expr_star(&self) -> bool {
130        true
131    }
132
133    /// See <https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#execute_immediate>
134    fn supports_execute_immediate(&self) -> bool {
135        true
136    }
137
138    // See <https://cloud.google.com/bigquery/docs/access-historical-data>
139    fn supports_timestamp_versioning(&self) -> bool {
140        true
141    }
142
143    // See <https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#group_by_clause>
144    fn supports_group_by_expr(&self) -> bool {
145        true
146    }
147
148    fn is_column_alias(&self, kw: &Keyword, _parser: &mut Parser) -> bool {
149        !RESERVED_FOR_COLUMN_ALIAS.contains(kw)
150    }
151
152    fn supports_pipe_operator(&self) -> bool {
153        true
154    }
155
156    fn supports_create_table_multi_schema_info_sources(&self) -> bool {
157        true
158    }
159
160    fn supports_while_do_end_while(&self) -> bool {
161        true
162    }
163
164    fn supports_loop_end_loop(&self) -> bool {
165        true
166    }
167
168    fn supports_for_do_end_for(&self) -> bool {
169        true
170    }
171
172    fn supports_repeat_until_end_repeat(&self) -> bool {
173        true
174    }
175
176    fn supports_lambda_functions(&self) -> bool {
177        true
178    }
179}