Skip to main content

sqlparser/dialect/
duckdb.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 [DuckDB](https://duckdb.org/)
21#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
22#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
23pub struct DuckDbDialect;
24
25// In most cases the redshift dialect is identical to [`PostgresSqlDialect`].
26impl Dialect for DuckDbDialect {
27    fn supports_cte_materialization(&self) -> bool {
28        true
29    }
30
31    fn supports_string_literal_column_alias(&self, explicit: bool) -> bool {
32        explicit
33    }
34
35    fn supports_string_literal_table_alias(&self, explicit: bool) -> bool {
36        explicit
37    }
38
39    fn supports_trailing_commas(&self) -> bool {
40        true
41    }
42
43    fn is_identifier_start(&self, ch: char) -> bool {
44        ch.is_alphabetic() || ch == '_'
45    }
46
47    fn is_identifier_part(&self, ch: char) -> bool {
48        ch.is_alphabetic() || ch.is_ascii_digit() || ch == '$' || ch == '_'
49    }
50
51    /// See <https://duckdb.org/docs/stable/sql/dialect/keywords_and_identifiers>
52    fn identifier_quote_style(&self, _identifier: &str) -> Option<char> {
53        Some('"')
54    }
55
56    fn supports_filter_during_aggregation(&self) -> bool {
57        true
58    }
59
60    fn supports_group_by_expr(&self) -> bool {
61        true
62    }
63
64    fn supports_bitwise_shift_operators(&self) -> bool {
65        true
66    }
67
68    fn supports_tilde_like_match_operator(&self) -> bool {
69        true
70    }
71
72    fn supports_aggregate_order_by_with_empty_args(&self) -> bool {
73        true
74    }
75
76    fn supports_comparison_in_between_low_bound(&self) -> bool {
77        true
78    }
79
80    fn supports_named_fn_args_with_eq_operator(&self) -> bool {
81        true
82    }
83
84    fn supports_named_fn_args_with_assignment_operator(&self) -> bool {
85        true
86    }
87
88    // DuckDB uses this syntax for `STRUCT`s.
89    //
90    // https://duckdb.org/docs/sql/data_types/struct.html#creating-structs
91    fn supports_dictionary_syntax(&self) -> bool {
92        true
93    }
94
95    // DuckDB uses this syntax for `MAP`s.
96    //
97    // https://duckdb.org/docs/sql/data_types/map.html#creating-maps
98    fn support_map_literal_syntax(&self) -> bool {
99        true
100    }
101
102    /// See <https://duckdb.org/docs/stable/sql/functions/lambda>
103    fn supports_lambda_functions(&self) -> bool {
104        true
105    }
106
107    /// Returns true if this dialect allows the `EXTRACT` function to use single quotes in the part being extracted.
108    fn allow_extract_single_quotes(&self) -> bool {
109        true
110    }
111
112    // DuckDB is compatible with PostgreSQL syntax for this statement,
113    // although not all features may be implemented.
114    fn supports_explain_with_utility_options(&self) -> bool {
115        true
116    }
117
118    /// See DuckDB <https://duckdb.org/docs/sql/statements/load_and_install.html#load>
119    fn supports_load_extension(&self) -> bool {
120        true
121    }
122
123    // See DuckDB <https://duckdb.org/docs/sql/data_types/array.html#defining-an-array-field>
124    fn supports_array_typedef_with_brackets(&self) -> bool {
125        true
126    }
127
128    fn supports_from_first_select(&self) -> bool {
129        true
130    }
131
132    /// See DuckDB <https://duckdb.org/docs/sql/query_syntax/orderby.html#order-by-all-examples>
133    fn supports_order_by_all(&self) -> bool {
134        true
135    }
136
137    fn supports_select_wildcard_exclude(&self) -> bool {
138        true
139    }
140
141    /// DuckDB supports `NOTNULL` as an alias for `IS NOT NULL`,
142    /// see DuckDB Comparisons <https://duckdb.org/docs/stable/sql/expressions/comparison_operators#between-and-is-not-null>
143    fn supports_notnull_operator(&self) -> bool {
144        true
145    }
146
147    /// See <https://duckdb.org/docs/extensions/overview>
148    fn supports_install(&self) -> bool {
149        true
150    }
151
152    /// See <https://duckdb.org/docs/sql/statements/attach#detach-syntax>
153    fn supports_detach(&self) -> bool {
154        true
155    }
156
157    /// See <https://duckdb.org/docs/sql/query_syntax/select#replace-clause>
158    fn supports_select_wildcard_replace(&self) -> bool {
159        true
160    }
161
162    fn supports_comma_separated_trim(&self) -> bool {
163        true
164    }
165
166    fn supports_numeric_literal_underscores(&self) -> bool {
167        true
168    }
169
170    fn supports_nested_comments(&self) -> bool {
171        true
172    }
173
174    /// See <https://duckdb.org/docs/stable/sql/statements/alter_table>
175    fn supports_add_column_if_not_exists(&self) -> bool {
176        true
177    }
178}