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 if parser.parse_keyword(Keyword::BEGIN) {
50 return Some(parser.parse_begin_exception_end());
51 }
52
53 None
54 }
55
56 /// See <https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#identifiers>
57 fn is_delimited_identifier_start(&self, ch: char) -> bool {
58 ch == '`'
59 }
60
61 fn supports_projection_trailing_commas(&self) -> bool {
62 true
63 }
64
65 /// See <https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#create_table_statement>
66 fn supports_column_definition_trailing_commas(&self) -> bool {
67 true
68 }
69
70 fn is_identifier_start(&self, ch: char) -> bool {
71 ch.is_ascii_lowercase() || ch.is_ascii_uppercase() || ch == '_'
72 // BigQuery supports `@@foo.bar` variable syntax in its procedural language.
73 // https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#beginexceptionend
74 || ch == '@'
75 }
76
77 fn is_identifier_part(&self, ch: char) -> bool {
78 ch.is_ascii_lowercase() || ch.is_ascii_uppercase() || ch.is_ascii_digit() || ch == '_'
79 }
80
81 /// See [doc](https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#quoted_literals)
82 fn supports_triple_quoted_string(&self) -> bool {
83 true
84 }
85
86 /// See [doc](https://cloud.google.com/bigquery/docs/reference/standard-sql/navigation_functions#first_value)
87 fn supports_window_function_null_treatment_arg(&self) -> bool {
88 true
89 }
90
91 // See https://cloud.google.com/bigquery/docs/reference/standard-sql/lexical#escape_sequences
92 fn supports_string_literal_backslash_escape(&self) -> bool {
93 true
94 }
95
96 /// See [doc](https://cloud.google.com/bigquery/docs/reference/standard-sql/window-function-calls#ref_named_window)
97 fn supports_window_clause_named_window_reference(&self) -> bool {
98 true
99 }
100
101 /// See [doc](https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#set)
102 fn supports_parenthesized_set_variables(&self) -> bool {
103 true
104 }
105
106 // See https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#select_except
107 fn supports_select_wildcard_except(&self) -> bool {
108 true
109 }
110
111 fn require_interval_qualifier(&self) -> bool {
112 true
113 }
114
115 // See https://cloud.google.com/bigquery/docs/reference/standard-sql/data-types#constructing_a_struct
116 fn supports_struct_literal(&self) -> bool {
117 true
118 }
119
120 /// See <https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#select_expression_star>
121 fn supports_select_expr_star(&self) -> bool {
122 true
123 }
124
125 /// See <https://cloud.google.com/bigquery/docs/reference/standard-sql/procedural-language#execute_immediate>
126 fn supports_execute_immediate(&self) -> bool {
127 true
128 }
129
130 // See <https://cloud.google.com/bigquery/docs/access-historical-data>
131 fn supports_timestamp_versioning(&self) -> bool {
132 true
133 }
134
135 // See <https://cloud.google.com/bigquery/docs/reference/standard-sql/query-syntax#group_by_clause>
136 fn supports_group_by_expr(&self) -> bool {
137 true
138 }
139
140 fn is_column_alias(&self, kw: &Keyword, _parser: &mut Parser) -> bool {
141 !RESERVED_FOR_COLUMN_ALIAS.contains(kw)
142 }
143
144 fn supports_pipe_operator(&self) -> bool {
145 true
146 }
147
148 fn supports_create_table_multi_schema_info_sources(&self) -> bool {
149 true
150 }
151}