sqlparser/dialect/clickhouse.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::dialect::Dialect;
19use crate::keywords::Keyword;
20use crate::parser::Parser;
21
22/// A [`Dialect`] for [ClickHouse](https://clickhouse.com/).
23#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
24#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
25pub struct ClickHouseDialect {}
26
27impl Dialect for ClickHouseDialect {
28 fn is_identifier_start(&self, ch: char) -> bool {
29 // See https://clickhouse.com/docs/en/sql-reference/syntax/#syntax-identifiers
30 ch.is_ascii_lowercase() || ch.is_ascii_uppercase() || ch == '_'
31 }
32
33 /// Aliases are identifiers, quoted with `"` or a backtick, see
34 /// <https://clickhouse.com/docs/reference/syntax#identifiers>
35 fn supports_string_literal_column_alias(&self, _explicit: bool) -> bool {
36 false
37 }
38
39 /// See <https://clickhouse.com/docs/reference/syntax#identifiers>
40 fn supports_string_literal_table_alias(&self, _explicit: bool) -> bool {
41 false
42 }
43
44 fn is_identifier_part(&self, ch: char) -> bool {
45 // Undocumented, but the server accepts `$` in identifiers for MySQL compatibility
46 // since https://github.com/ClickHouse/ClickHouse/pull/14232
47 self.is_identifier_start(ch) || ch.is_ascii_digit() || ch == '$'
48 }
49
50 /// ClickHouse keywords are not reserved, so any of them can be a table name.
51 /// See <https://clickhouse.com/docs/en/sql-reference/syntax>
52 fn is_table_factor(&self, _kw: &Keyword, _parser: &mut Parser) -> bool {
53 true
54 }
55
56 /// ClickHouse accepts both backticks and double quotes, but its own
57 /// formatter emits backticks by default.
58 /// See <https://clickhouse.com/docs/en/sql-reference/syntax#identifiers>
59 fn identifier_quote_style(&self, _identifier: &str) -> Option<char> {
60 Some('`')
61 }
62
63 fn supports_string_literal_backslash_escape(&self) -> bool {
64 true
65 }
66
67 fn supports_select_wildcard_except(&self) -> bool {
68 true
69 }
70
71 fn describe_requires_table_keyword(&self) -> bool {
72 true
73 }
74
75 fn require_interval_qualifier(&self) -> bool {
76 true
77 }
78
79 fn supports_limit_comma(&self) -> bool {
80 true
81 }
82
83 fn supports_insert_table_function(&self) -> bool {
84 true
85 }
86
87 fn supports_insert_format(&self) -> bool {
88 true
89 }
90
91 fn supports_numeric_literal_underscores(&self) -> bool {
92 true
93 }
94
95 fn supports_partition_by_after_order_by(&self) -> bool {
96 true
97 }
98
99 fn supports_array_join_syntax(&self) -> bool {
100 true
101 }
102
103 fn supports_array_typedef_with_parentheses(&self) -> bool {
104 true
105 }
106
107 fn supports_map_typedef_with_parentheses(&self) -> bool {
108 true
109 }
110
111 // ClickHouse uses this for some FORMAT expressions in `INSERT` context, e.g. when inserting
112 // with FORMAT JSONEachRow a raw JSON key-value expression is valid and expected.
113 //
114 // [ClickHouse formats](https://clickhouse.com/docs/en/interfaces/formats)
115 fn supports_dictionary_syntax(&self) -> bool {
116 true
117 }
118
119 // See <https://clickhouse.com/docs/sql-reference/operators/in>
120 fn supports_in_unparenthesized_expr(&self) -> bool {
121 true
122 }
123
124 // See <https://clickhouse.com/docs/sql-reference/operators/in#distributed-subqueries>
125 fn supports_global_in(&self) -> bool {
126 true
127 }
128
129 /// See <https://clickhouse.com/docs/en/sql-reference/functions#higher-order-functions---operator-and-lambdaparams-expr-function>
130 fn supports_lambda_functions(&self) -> bool {
131 true
132 }
133
134 fn supports_from_first_select(&self) -> bool {
135 true
136 }
137
138 /// See <https://clickhouse.com/docs/en/sql-reference/statements/select/order-by>
139 fn supports_order_by_all(&self) -> bool {
140 true
141 }
142
143 // See <https://clickhouse.com/docs/en/sql-reference/aggregate-functions/grouping_function#grouping-sets>
144 fn supports_group_by_expr(&self) -> bool {
145 true
146 }
147
148 /// See <https://clickhouse.com/docs/en/sql-reference/statements/select/group-by#rollup-modifier>
149 fn supports_group_by_with_modifier(&self) -> bool {
150 true
151 }
152
153 /// Supported since 2020.
154 /// See <https://clickhouse.com/docs/whats-new/changelog/2020#backward-incompatible-change-2>
155 fn supports_nested_comments(&self) -> bool {
156 true
157 }
158
159 /// See <https://clickhouse.com/docs/sql-reference/syntax#comments>
160 fn supports_hash_comment(&self) -> bool {
161 true
162 }
163
164 /// See <https://github.com/ClickHouse/ClickHouse/blob/master/src/Parsers/Lexer.cpp>
165 fn requires_hash_comment_space(&self) -> bool {
166 true
167 }
168
169 /// See <https://clickhouse.com/docs/en/sql-reference/statements/optimize>
170 fn supports_optimize_table(&self) -> bool {
171 true
172 }
173
174 /// See <https://clickhouse.com/docs/en/sql-reference/statements/select/prewhere>
175 fn supports_prewhere(&self) -> bool {
176 true
177 }
178
179 /// See <https://clickhouse.com/docs/en/sql-reference/statements/select/order-by#order-by-expr-with-fill-modifier>
180 fn supports_with_fill(&self) -> bool {
181 true
182 }
183
184 /// See <https://clickhouse.com/docs/en/sql-reference/statements/select/limit-by>
185 fn supports_limit_by(&self) -> bool {
186 true
187 }
188
189 /// See <https://clickhouse.com/docs/en/sql-reference/statements/select/order-by#order-by-expr-with-fill-modifier>
190 fn supports_interpolate(&self) -> bool {
191 true
192 }
193
194 /// See <https://clickhouse.com/docs/en/sql-reference/statements/select#settings-in-select-query>
195 fn supports_settings(&self) -> bool {
196 true
197 }
198
199 /// See <https://clickhouse.com/docs/en/sql-reference/statements/select/format>
200 fn supports_select_format(&self) -> bool {
201 true
202 }
203
204 /// See <https://clickhouse.com/docs/sql-reference/statements/select#replace>
205 fn supports_select_wildcard_replace(&self) -> bool {
206 true
207 }
208
209 fn supports_comma_separated_trim(&self) -> bool {
210 true
211 }
212
213 /// See <https://clickhouse.com/docs/sql-reference/statements/alter/column>
214 fn supports_alter_column_position(&self) -> bool {
215 true
216 }
217
218 /// See <https://clickhouse.com/docs/sql-reference/data-types/tuple#addressing-tuple-elements>
219 fn supports_tuple_element_access(&self) -> bool {
220 true
221 }
222}