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 // ClickHouse uses this for some FORMAT expressions in `INSERT` context, e.g. when inserting
83 // with FORMAT JSONEachRow a raw JSON key-value expression is valid and expected.
84 //
85 // [ClickHouse formats](https://clickhouse.com/docs/en/interfaces/formats)
86 fn supports_dictionary_syntax(&self) -> bool {
87 true
88 }
89
90 // See <https://clickhouse.com/docs/sql-reference/operators/in>
91 fn supports_in_unparenthesized_expr(&self) -> bool {
92 true
93 }
94
95 /// See <https://clickhouse.com/docs/en/sql-reference/functions#higher-order-functions---operator-and-lambdaparams-expr-function>
96 fn supports_lambda_functions(&self) -> bool {
97 true
98 }
99
100 fn supports_from_first_select(&self) -> bool {
101 true
102 }
103
104 /// See <https://clickhouse.com/docs/en/sql-reference/statements/select/order-by>
105 fn supports_order_by_all(&self) -> bool {
106 true
107 }
108
109 // See <https://clickhouse.com/docs/en/sql-reference/aggregate-functions/grouping_function#grouping-sets>
110 fn supports_group_by_expr(&self) -> bool {
111 true
112 }
113
114 /// See <https://clickhouse.com/docs/en/sql-reference/statements/select/group-by#rollup-modifier>
115 fn supports_group_by_with_modifier(&self) -> bool {
116 true
117 }
118
119 /// Supported since 2020.
120 /// See <https://clickhouse.com/docs/whats-new/changelog/2020#backward-incompatible-change-2>
121 fn supports_nested_comments(&self) -> bool {
122 true
123 }
124
125 /// See <https://clickhouse.com/docs/en/sql-reference/statements/optimize>
126 fn supports_optimize_table(&self) -> bool {
127 true
128 }
129
130 /// See <https://clickhouse.com/docs/en/sql-reference/statements/select/prewhere>
131 fn supports_prewhere(&self) -> bool {
132 true
133 }
134
135 /// See <https://clickhouse.com/docs/en/sql-reference/statements/select/order-by#order-by-expr-with-fill-modifier>
136 fn supports_with_fill(&self) -> bool {
137 true
138 }
139
140 /// See <https://clickhouse.com/docs/en/sql-reference/statements/select/limit-by>
141 fn supports_limit_by(&self) -> bool {
142 true
143 }
144
145 /// See <https://clickhouse.com/docs/en/sql-reference/statements/select/order-by#order-by-expr-with-fill-modifier>
146 fn supports_interpolate(&self) -> bool {
147 true
148 }
149
150 /// See <https://clickhouse.com/docs/en/sql-reference/statements/select#settings-in-select-query>
151 fn supports_settings(&self) -> bool {
152 true
153 }
154
155 /// See <https://clickhouse.com/docs/en/sql-reference/statements/select/format>
156 fn supports_select_format(&self) -> bool {
157 true
158 }
159
160 /// See <https://clickhouse.com/docs/sql-reference/statements/select#replace>
161 fn supports_select_wildcard_replace(&self) -> bool {
162 true
163 }
164
165 fn supports_comma_separated_trim(&self) -> bool {
166 true
167 }
168}