Skip to main content

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;
19
20/// A [`Dialect`] for [ClickHouse](https://clickhouse.com/).
21#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
22#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
23pub struct ClickHouseDialect {}
24
25impl Dialect for ClickHouseDialect {
26    fn is_identifier_start(&self, ch: char) -> bool {
27        // See https://clickhouse.com/docs/en/sql-reference/syntax/#syntax-identifiers
28        ch.is_ascii_lowercase() || ch.is_ascii_uppercase() || ch == '_'
29    }
30
31    fn is_identifier_part(&self, ch: char) -> bool {
32        self.is_identifier_start(ch) || ch.is_ascii_digit()
33    }
34
35    /// ClickHouse accepts both backticks and double quotes, but its own
36    /// formatter emits backticks by default.
37    /// See <https://clickhouse.com/docs/en/sql-reference/syntax#identifiers>
38    fn identifier_quote_style(&self, _identifier: &str) -> Option<char> {
39        Some('`')
40    }
41
42    fn supports_string_literal_backslash_escape(&self) -> bool {
43        true
44    }
45
46    fn supports_select_wildcard_except(&self) -> bool {
47        true
48    }
49
50    fn describe_requires_table_keyword(&self) -> bool {
51        true
52    }
53
54    fn require_interval_qualifier(&self) -> bool {
55        true
56    }
57
58    fn supports_limit_comma(&self) -> bool {
59        true
60    }
61
62    fn supports_insert_table_function(&self) -> bool {
63        true
64    }
65
66    fn supports_insert_format(&self) -> bool {
67        true
68    }
69
70    fn supports_numeric_literal_underscores(&self) -> bool {
71        true
72    }
73
74    fn supports_partition_by_after_order_by(&self) -> bool {
75        true
76    }
77
78    fn supports_array_join_syntax(&self) -> bool {
79        true
80    }
81
82    fn supports_array_typedef_with_parentheses(&self) -> bool {
83        true
84    }
85
86    fn supports_map_typedef_with_parentheses(&self) -> bool {
87        true
88    }
89
90    // ClickHouse uses this for some FORMAT expressions in `INSERT` context, e.g. when inserting
91    // with FORMAT JSONEachRow a raw JSON key-value expression is valid and expected.
92    //
93    // [ClickHouse formats](https://clickhouse.com/docs/en/interfaces/formats)
94    fn supports_dictionary_syntax(&self) -> bool {
95        true
96    }
97
98    // See <https://clickhouse.com/docs/sql-reference/operators/in>
99    fn supports_in_unparenthesized_expr(&self) -> bool {
100        true
101    }
102
103    /// See <https://clickhouse.com/docs/en/sql-reference/functions#higher-order-functions---operator-and-lambdaparams-expr-function>
104    fn supports_lambda_functions(&self) -> bool {
105        true
106    }
107
108    fn supports_from_first_select(&self) -> bool {
109        true
110    }
111
112    /// See <https://clickhouse.com/docs/en/sql-reference/statements/select/order-by>
113    fn supports_order_by_all(&self) -> bool {
114        true
115    }
116
117    // See <https://clickhouse.com/docs/en/sql-reference/aggregate-functions/grouping_function#grouping-sets>
118    fn supports_group_by_expr(&self) -> bool {
119        true
120    }
121
122    /// See <https://clickhouse.com/docs/en/sql-reference/statements/select/group-by#rollup-modifier>
123    fn supports_group_by_with_modifier(&self) -> bool {
124        true
125    }
126
127    /// Supported since 2020.
128    /// See <https://clickhouse.com/docs/whats-new/changelog/2020#backward-incompatible-change-2>
129    fn supports_nested_comments(&self) -> bool {
130        true
131    }
132
133    /// See <https://clickhouse.com/docs/en/sql-reference/statements/optimize>
134    fn supports_optimize_table(&self) -> bool {
135        true
136    }
137
138    /// See <https://clickhouse.com/docs/en/sql-reference/statements/select/prewhere>
139    fn supports_prewhere(&self) -> bool {
140        true
141    }
142
143    /// See <https://clickhouse.com/docs/en/sql-reference/statements/select/order-by#order-by-expr-with-fill-modifier>
144    fn supports_with_fill(&self) -> bool {
145        true
146    }
147
148    /// See <https://clickhouse.com/docs/en/sql-reference/statements/select/limit-by>
149    fn supports_limit_by(&self) -> bool {
150        true
151    }
152
153    /// See <https://clickhouse.com/docs/en/sql-reference/statements/select/order-by#order-by-expr-with-fill-modifier>
154    fn supports_interpolate(&self) -> bool {
155        true
156    }
157
158    /// See <https://clickhouse.com/docs/en/sql-reference/statements/select#settings-in-select-query>
159    fn supports_settings(&self) -> bool {
160        true
161    }
162
163    /// See <https://clickhouse.com/docs/en/sql-reference/statements/select/format>
164    fn supports_select_format(&self) -> bool {
165        true
166    }
167
168    /// See <https://clickhouse.com/docs/sql-reference/statements/select#replace>
169    fn supports_select_wildcard_replace(&self) -> bool {
170        true
171    }
172
173    fn supports_comma_separated_trim(&self) -> bool {
174        true
175    }
176
177    /// See <https://clickhouse.com/docs/sql-reference/statements/alter/column>
178    fn supports_alter_column_position(&self) -> bool {
179        true
180    }
181
182    /// See <https://clickhouse.com/docs/sql-reference/data-types/tuple#addressing-tuple-elements>
183    fn supports_tuple_element_access(&self) -> bool {
184        true
185    }
186}