sqlparser/dialect/mod.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
18mod ansi;
19mod bigquery;
20mod clickhouse;
21mod databricks;
22mod duckdb;
23mod generic;
24mod hive;
25mod mssql;
26mod mysql;
27mod postgresql;
28mod redshift;
29mod snowflake;
30mod sqlite;
31
32use core::any::{Any, TypeId};
33use core::fmt::Debug;
34use core::iter::Peekable;
35use core::str::Chars;
36
37use log::debug;
38
39pub use self::ansi::AnsiDialect;
40pub use self::bigquery::BigQueryDialect;
41pub use self::clickhouse::ClickHouseDialect;
42pub use self::databricks::DatabricksDialect;
43pub use self::duckdb::DuckDbDialect;
44pub use self::generic::GenericDialect;
45pub use self::hive::HiveDialect;
46pub use self::mssql::MsSqlDialect;
47pub use self::mysql::MySqlDialect;
48pub use self::postgresql::PostgreSqlDialect;
49pub use self::redshift::RedshiftSqlDialect;
50pub use self::snowflake::SnowflakeDialect;
51pub use self::sqlite::SQLiteDialect;
52use crate::ast::{ColumnOption, Expr, Statement};
53pub use crate::keywords;
54use crate::keywords::Keyword;
55use crate::parser::{Parser, ParserError};
56use crate::tokenizer::Token;
57
58#[cfg(not(feature = "std"))]
59use alloc::boxed::Box;
60
61/// Convenience check if a [`Parser`] uses a certain dialect.
62///
63/// Note: when possible please the new style, adding a method to the [`Dialect`]
64/// trait rather than using this macro.
65///
66/// The benefits of adding a method on `Dialect` over this macro are:
67/// 1. user defined [`Dialect`]s can customize the parsing behavior
68/// 2. The differences between dialects can be clearly documented in the trait
69///
70/// `dialect_of!(parser is SQLiteDialect | GenericDialect)` evaluates
71/// to `true` if `parser.dialect` is one of the [`Dialect`]s specified.
72macro_rules! dialect_of {
73 ( $parsed_dialect: ident is $($dialect_type: ty)|+ ) => {
74 ($($parsed_dialect.dialect.is::<$dialect_type>())||+)
75 };
76}
77
78// Similar to above, but for applying directly against an instance of dialect
79// instead of a struct member named dialect. This avoids lifetime issues when
80// mixing match guards and token references.
81macro_rules! dialect_is {
82 ($dialect:ident is $($dialect_type:ty)|+) => {
83 ($($dialect.is::<$dialect_type>())||+)
84 }
85}
86
87/// Encapsulates the differences between SQL implementations.
88///
89/// # SQL Dialects
90///
91/// SQL implementations deviate from one another, either due to
92/// custom extensions or various historical reasons. This trait
93/// encapsulates the parsing differences between dialects.
94///
95/// [`GenericDialect`] is the most permissive dialect, and parses the union of
96/// all the other dialects, when there is no ambiguity. However, it does not
97/// currently allow `CREATE TABLE` statements without types specified for all
98/// columns; use [`SQLiteDialect`] if you require that.
99///
100/// # Examples
101/// Most users create a [`Dialect`] directly, as shown on the [module
102/// level documentation]:
103///
104/// ```
105/// # use sqlparser::dialect::AnsiDialect;
106/// let dialect = AnsiDialect {};
107/// ```
108///
109/// It is also possible to dynamically create a [`Dialect`] from its
110/// name. For example:
111///
112/// ```
113/// # use sqlparser::dialect::{AnsiDialect, dialect_from_str};
114/// let dialect = dialect_from_str("ansi").unwrap();
115///
116/// // Parsed dialect is an instance of `AnsiDialect`:
117/// assert!(dialect.is::<AnsiDialect>());
118/// ```
119///
120/// [module level documentation]: crate
121pub trait Dialect: Debug + Any {
122 /// Determine the [`TypeId`] of this dialect.
123 ///
124 /// By default, return the same [`TypeId`] as [`Any::type_id`]. Can be overridden
125 /// by dialects that behave like other dialects
126 /// (for example when wrapping a dialect).
127 fn dialect(&self) -> TypeId {
128 self.type_id()
129 }
130
131 /// Determine if a character starts a quoted identifier. The default
132 /// implementation, accepting "double quoted" ids is both ANSI-compliant
133 /// and appropriate for most dialects (with the notable exception of
134 /// MySQL, MS SQL, and sqlite). You can accept one of characters listed
135 /// in `Word::matching_end_quote` here
136 fn is_delimited_identifier_start(&self, ch: char) -> bool {
137 ch == '"' || ch == '`'
138 }
139
140 /// Determine if a character starts a potential nested quoted identifier.
141 /// Example: RedShift supports the following quote styles to all mean the same thing:
142 /// ```sql
143 /// SELECT 1 AS foo;
144 /// SELECT 1 AS "foo";
145 /// SELECT 1 AS [foo];
146 /// SELECT 1 AS ["foo"];
147 /// ```
148 fn is_nested_delimited_identifier_start(&self, _ch: char) -> bool {
149 false
150 }
151
152 /// Only applicable whenever [`Self::is_nested_delimited_identifier_start`] returns true
153 /// If the next sequence of tokens potentially represent a nested identifier, then this method
154 /// returns a tuple containing the outer quote style, and if present, the inner (nested) quote style.
155 ///
156 /// Example (Redshift):
157 /// ```text
158 /// `["foo"]` => Some(`[`, Some(`"`))
159 /// `[foo]` => Some(`[`, None)
160 /// `[0]` => None
161 /// `"foo"` => None
162 /// ```
163 fn peek_nested_delimited_identifier_quotes(
164 &self,
165 mut _chars: Peekable<Chars<'_>>,
166 ) -> Option<(char, Option<char>)> {
167 None
168 }
169
170 /// Return the character used to quote identifiers.
171 fn identifier_quote_style(&self, _identifier: &str) -> Option<char> {
172 None
173 }
174
175 /// Determine if a character is a valid start character for an unquoted identifier
176 fn is_identifier_start(&self, ch: char) -> bool;
177
178 /// Determine if a character is a valid unquoted identifier character
179 fn is_identifier_part(&self, ch: char) -> bool;
180
181 /// Most dialects do not have custom operators. Override this method to provide custom operators.
182 fn is_custom_operator_part(&self, _ch: char) -> bool {
183 false
184 }
185
186 /// Determine if the dialect supports escaping characters via '\' in string literals.
187 ///
188 /// Some dialects like BigQuery and Snowflake support this while others like
189 /// Postgres do not. Such that the following is accepted by the former but
190 /// rejected by the latter.
191 /// ```sql
192 /// SELECT 'ab\'cd';
193 /// ```
194 ///
195 /// Conversely, such dialects reject the following statement which
196 /// otherwise would be valid in the other dialects.
197 /// ```sql
198 /// SELECT '\';
199 /// ```
200 fn supports_string_literal_backslash_escape(&self) -> bool {
201 false
202 }
203
204 /// Determine whether the dialect strips the backslash when escaping LIKE wildcards (%, _).
205 ///
206 /// [MySQL] has a special case when escaping single quoted strings which leaves these unescaped
207 /// so they can be used in LIKE patterns without double-escaping (as is necessary in other
208 /// escaping dialects, such as [Snowflake]). Generally, special characters have escaping rules
209 /// causing them to be replaced with a different byte sequences (e.g. `'\0'` becoming the zero
210 /// byte), and the default if an escaped character does not have a specific escaping rule is to
211 /// strip the backslash (e.g. there is no rule for `h`, so `'\h' = 'h'`). MySQL's special case
212 /// for ignoring LIKE wildcard escapes is to *not* strip the backslash, so that `'\%' = '\\%'`.
213 /// This applies to all string literals though, not just those used in LIKE patterns.
214 ///
215 /// ```text
216 /// mysql> select '\_', hex('\\'), hex('_'), hex('\_');
217 /// +----+-----------+----------+-----------+
218 /// | \_ | hex('\\') | hex('_') | hex('\_') |
219 /// +----+-----------+----------+-----------+
220 /// | \_ | 5C | 5F | 5C5F |
221 /// +----+-----------+----------+-----------+
222 /// 1 row in set (0.00 sec)
223 /// ```
224 ///
225 /// [MySQL]: https://dev.mysql.com/doc/refman/8.4/en/string-literals.html
226 /// [Snowflake]: https://docs.snowflake.com/en/sql-reference/functions/like#usage-notes
227 fn ignores_wildcard_escapes(&self) -> bool {
228 false
229 }
230
231 /// Determine if the dialect supports string literals with `U&` prefix.
232 /// This is used to specify Unicode code points in string literals.
233 /// For example, in PostgreSQL, the following is a valid string literal:
234 /// ```sql
235 /// SELECT U&'\0061\0062\0063';
236 /// ```
237 /// This is equivalent to the string literal `'abc'`.
238 /// See
239 /// - [Postgres docs](https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS-UESCAPE)
240 /// - [H2 docs](http://www.h2database.com/html/grammar.html#string)
241 fn supports_unicode_string_literal(&self) -> bool {
242 false
243 }
244
245 /// Does the dialect support `FILTER (WHERE expr)` for aggregate queries?
246 fn supports_filter_during_aggregation(&self) -> bool {
247 false
248 }
249
250 /// Returns true if the dialect supports referencing another named window
251 /// within a window clause declaration.
252 ///
253 /// Example
254 /// ```sql
255 /// SELECT * FROM mytable
256 /// WINDOW mynamed_window AS another_named_window
257 /// ```
258 fn supports_window_clause_named_window_reference(&self) -> bool {
259 false
260 }
261
262 /// Returns true if the dialect supports `ARRAY_AGG() [WITHIN GROUP (ORDER BY)]` expressions.
263 /// Otherwise, the dialect should expect an `ORDER BY` without the `WITHIN GROUP` clause, e.g. [`ANSI`]
264 ///
265 /// [`ANSI`]: https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#array-aggregate-function
266 fn supports_within_after_array_aggregation(&self) -> bool {
267 false
268 }
269
270 /// Returns true if the dialects supports `group sets, roll up, or cube` expressions.
271 fn supports_group_by_expr(&self) -> bool {
272 false
273 }
274
275 /// Returns true if the dialects supports `GROUP BY` modifiers prefixed by a `WITH` keyword.
276 /// Example: `GROUP BY value WITH ROLLUP`.
277 fn supports_group_by_with_modifier(&self) -> bool {
278 false
279 }
280
281 /// Returns true if the dialect supports the `(+)` syntax for OUTER JOIN.
282 fn supports_outer_join_operator(&self) -> bool {
283 false
284 }
285
286 /// Returns true if the dialect supports CONNECT BY.
287 fn supports_connect_by(&self) -> bool {
288 false
289 }
290
291 /// Returns true if the dialect supports `EXECUTE IMMEDIATE` statements.
292 fn supports_execute_immediate(&self) -> bool {
293 false
294 }
295
296 /// Returns true if the dialect supports the MATCH_RECOGNIZE operation.
297 fn supports_match_recognize(&self) -> bool {
298 false
299 }
300
301 /// Returns true if the dialect supports `(NOT) IN ()` expressions
302 fn supports_in_empty_list(&self) -> bool {
303 false
304 }
305
306 /// Returns true if the dialect supports `BEGIN {DEFERRED | IMMEDIATE | EXCLUSIVE | TRY | CATCH} [TRANSACTION]` statements
307 fn supports_start_transaction_modifier(&self) -> bool {
308 false
309 }
310
311 /// Returns true if the dialect supports `END {TRY | CATCH}` statements
312 fn supports_end_transaction_modifier(&self) -> bool {
313 false
314 }
315
316 /// Returns true if the dialect supports named arguments of the form `FUN(a = '1', b = '2')`.
317 fn supports_named_fn_args_with_eq_operator(&self) -> bool {
318 false
319 }
320
321 /// Returns true if the dialect supports named arguments of the form `FUN(a : '1', b : '2')`.
322 fn supports_named_fn_args_with_colon_operator(&self) -> bool {
323 false
324 }
325
326 /// Returns true if the dialect supports named arguments of the form `FUN(a := '1', b := '2')`.
327 fn supports_named_fn_args_with_assignment_operator(&self) -> bool {
328 false
329 }
330
331 /// Returns true if the dialect supports named arguments of the form `FUN(a => '1', b => '2')`.
332 fn supports_named_fn_args_with_rarrow_operator(&self) -> bool {
333 true
334 }
335
336 /// Returns true if dialect supports argument name as arbitrary expression.
337 /// e.g. `FUN(LOWER('a'):'1', b:'2')`
338 /// Such function arguments are represented in the AST by the `FunctionArg::ExprNamed` variant,
339 /// otherwise use the `FunctionArg::Named` variant (compatible reason).
340 fn supports_named_fn_args_with_expr_name(&self) -> bool {
341 false
342 }
343
344 /// Returns true if the dialect supports identifiers starting with a numeric
345 /// prefix such as tables named `59901_user_login`
346 fn supports_numeric_prefix(&self) -> bool {
347 false
348 }
349
350 /// Returns true if the dialect supports numbers containing underscores, e.g. `10_000_000`
351 fn supports_numeric_literal_underscores(&self) -> bool {
352 false
353 }
354
355 /// Returns true if the dialects supports specifying null treatment
356 /// as part of a window function's parameter list as opposed
357 /// to after the parameter list.
358 ///
359 /// i.e The following syntax returns true
360 /// ```sql
361 /// FIRST_VALUE(a IGNORE NULLS) OVER ()
362 /// ```
363 /// while the following syntax returns false
364 /// ```sql
365 /// FIRST_VALUE(a) IGNORE NULLS OVER ()
366 /// ```
367 fn supports_window_function_null_treatment_arg(&self) -> bool {
368 false
369 }
370
371 /// Returns true if the dialect supports defining structs or objects using a
372 /// syntax like `{'x': 1, 'y': 2, 'z': 3}`.
373 fn supports_dictionary_syntax(&self) -> bool {
374 false
375 }
376
377 /// Returns true if the dialect supports defining object using the
378 /// syntax like `Map {1: 10, 2: 20}`.
379 fn support_map_literal_syntax(&self) -> bool {
380 false
381 }
382
383 /// Returns true if the dialect supports lambda functions, for example:
384 ///
385 /// ```sql
386 /// SELECT transform(array(1, 2, 3), x -> x + 1); -- returns [2,3,4]
387 /// ```
388 fn supports_lambda_functions(&self) -> bool {
389 false
390 }
391
392 /// Returns true if the dialect supports multiple variable assignment
393 /// using parentheses in a `SET` variable declaration.
394 ///
395 /// ```sql
396 /// SET (variable[, ...]) = (expression[, ...]);
397 /// ```
398 fn supports_parenthesized_set_variables(&self) -> bool {
399 false
400 }
401
402 /// Returns true if the dialect supports multiple `SET` statements
403 /// in a single statement.
404 ///
405 /// ```sql
406 /// SET variable = expression [, variable = expression];
407 /// ```
408 fn supports_comma_separated_set_assignments(&self) -> bool {
409 false
410 }
411
412 /// Returns true if the dialect supports an `EXCEPT` clause following a
413 /// wildcard in a select list.
414 ///
415 /// For example
416 /// ```sql
417 /// SELECT * EXCEPT order_id FROM orders;
418 /// ```
419 fn supports_select_wildcard_except(&self) -> bool {
420 false
421 }
422
423 /// Returns true if the dialect has a CONVERT function which accepts a type first
424 /// and an expression second, e.g. `CONVERT(varchar, 1)`
425 fn convert_type_before_value(&self) -> bool {
426 false
427 }
428
429 /// Returns true if the dialect supports triple quoted string
430 /// e.g. `"""abc"""`
431 fn supports_triple_quoted_string(&self) -> bool {
432 false
433 }
434
435 /// Dialect-specific prefix parser override
436 fn parse_prefix(&self, _parser: &mut Parser) -> Option<Result<Expr, ParserError>> {
437 // return None to fall back to the default behavior
438 None
439 }
440
441 /// Does the dialect support trailing commas around the query?
442 fn supports_trailing_commas(&self) -> bool {
443 false
444 }
445
446 /// Does the dialect support parsing `LIMIT 1, 2` as `LIMIT 2 OFFSET 1`?
447 fn supports_limit_comma(&self) -> bool {
448 false
449 }
450
451 /// Does the dialect support trailing commas in the projection list?
452 fn supports_projection_trailing_commas(&self) -> bool {
453 self.supports_trailing_commas()
454 }
455
456 /// Returns true if the dialect supports trailing commas in the `FROM` clause of a `SELECT` statement.
457 /// Example: `SELECT 1 FROM T, U, LIMIT 1`
458 fn supports_from_trailing_commas(&self) -> bool {
459 false
460 }
461
462 /// Returns true if the dialect supports trailing commas in the
463 /// column definitions list of a `CREATE` statement.
464 /// Example: `CREATE TABLE T (x INT, y TEXT,)`
465 fn supports_column_definition_trailing_commas(&self) -> bool {
466 false
467 }
468
469 /// Returns true if the dialect supports double dot notation for object names
470 ///
471 /// Example
472 /// ```sql
473 /// SELECT * FROM db_name..table_name
474 /// ```
475 fn supports_object_name_double_dot_notation(&self) -> bool {
476 false
477 }
478
479 /// Return true if the dialect supports the STRUCT literal
480 ///
481 /// Example
482 /// ```sql
483 /// SELECT STRUCT(1 as one, 'foo' as foo, false)
484 /// ```
485 fn supports_struct_literal(&self) -> bool {
486 false
487 }
488
489 /// Return true if the dialect supports empty projections in SELECT statements
490 ///
491 /// Example
492 /// ```sql
493 /// SELECT from table_name
494 /// ```
495 fn supports_empty_projections(&self) -> bool {
496 false
497 }
498
499 /// Return true if the dialect supports wildcard expansion on
500 /// arbitrary expressions in projections.
501 ///
502 /// Example:
503 /// ```sql
504 /// SELECT STRUCT<STRING>('foo').* FROM T
505 /// ```
506 fn supports_select_expr_star(&self) -> bool {
507 false
508 }
509
510 /// Return true if the dialect supports "FROM-first" selects.
511 ///
512 /// Example:
513 /// ```sql
514 /// FROM table
515 /// SELECT *
516 /// ```
517 fn supports_from_first_select(&self) -> bool {
518 false
519 }
520
521 /// Does the dialect support MySQL-style `'user'@'host'` grantee syntax?
522 fn supports_user_host_grantee(&self) -> bool {
523 false
524 }
525
526 /// Does the dialect support the `MATCH() AGAINST()` syntax?
527 fn supports_match_against(&self) -> bool {
528 false
529 }
530
531 /// Dialect-specific infix parser override
532 ///
533 /// This method is called to parse the next infix expression.
534 ///
535 /// If `None` is returned, falls back to the default behavior.
536 fn parse_infix(
537 &self,
538 _parser: &mut Parser,
539 _expr: &Expr,
540 _precedence: u8,
541 ) -> Option<Result<Expr, ParserError>> {
542 // return None to fall back to the default behavior
543 None
544 }
545
546 /// Dialect-specific precedence override
547 ///
548 /// This method is called to get the precedence of the next token.
549 ///
550 /// If `None` is returned, falls back to the default behavior.
551 fn get_next_precedence(&self, _parser: &Parser) -> Option<Result<u8, ParserError>> {
552 // return None to fall back to the default behavior
553 None
554 }
555
556 /// Get the precedence of the next token, looking at the full token stream.
557 ///
558 /// A higher number => higher precedence
559 ///
560 /// See [`Self::get_next_precedence`] to override the behavior for just the
561 /// next token.
562 ///
563 /// The default implementation is used for many dialects, but can be
564 /// overridden to provide dialect-specific behavior.
565 fn get_next_precedence_default(&self, parser: &Parser) -> Result<u8, ParserError> {
566 if let Some(precedence) = self.get_next_precedence(parser) {
567 return precedence;
568 }
569 macro_rules! p {
570 ($precedence:ident) => {
571 self.prec_value(Precedence::$precedence)
572 };
573 }
574
575 let token = parser.peek_token();
576 debug!("get_next_precedence_full() {:?}", token);
577 match token.token {
578 Token::Word(w) if w.keyword == Keyword::OR => Ok(p!(Or)),
579 Token::Word(w) if w.keyword == Keyword::AND => Ok(p!(And)),
580 Token::Word(w) if w.keyword == Keyword::XOR => Ok(p!(Xor)),
581
582 Token::Word(w) if w.keyword == Keyword::AT => {
583 match (
584 parser.peek_nth_token(1).token,
585 parser.peek_nth_token(2).token,
586 ) {
587 (Token::Word(w), Token::Word(w2))
588 if w.keyword == Keyword::TIME && w2.keyword == Keyword::ZONE =>
589 {
590 Ok(p!(AtTz))
591 }
592 _ => Ok(self.prec_unknown()),
593 }
594 }
595
596 Token::Word(w) if w.keyword == Keyword::NOT => match parser.peek_nth_token(1).token {
597 // The precedence of NOT varies depending on keyword that
598 // follows it. If it is followed by IN, BETWEEN, or LIKE,
599 // it takes on the precedence of those tokens. Otherwise, it
600 // is not an infix operator, and therefore has zero
601 // precedence.
602 Token::Word(w) if w.keyword == Keyword::IN => Ok(p!(Between)),
603 Token::Word(w) if w.keyword == Keyword::BETWEEN => Ok(p!(Between)),
604 Token::Word(w) if w.keyword == Keyword::LIKE => Ok(p!(Like)),
605 Token::Word(w) if w.keyword == Keyword::ILIKE => Ok(p!(Like)),
606 Token::Word(w) if w.keyword == Keyword::RLIKE => Ok(p!(Like)),
607 Token::Word(w) if w.keyword == Keyword::REGEXP => Ok(p!(Like)),
608 Token::Word(w) if w.keyword == Keyword::SIMILAR => Ok(p!(Like)),
609 _ => Ok(self.prec_unknown()),
610 },
611 Token::Word(w) if w.keyword == Keyword::IS => Ok(p!(Is)),
612 Token::Word(w) if w.keyword == Keyword::IN => Ok(p!(Between)),
613 Token::Word(w) if w.keyword == Keyword::BETWEEN => Ok(p!(Between)),
614 Token::Word(w) if w.keyword == Keyword::OVERLAPS => Ok(p!(Between)),
615 Token::Word(w) if w.keyword == Keyword::LIKE => Ok(p!(Like)),
616 Token::Word(w) if w.keyword == Keyword::ILIKE => Ok(p!(Like)),
617 Token::Word(w) if w.keyword == Keyword::RLIKE => Ok(p!(Like)),
618 Token::Word(w) if w.keyword == Keyword::REGEXP => Ok(p!(Like)),
619 Token::Word(w) if w.keyword == Keyword::SIMILAR => Ok(p!(Like)),
620 Token::Word(w) if w.keyword == Keyword::OPERATOR => Ok(p!(Between)),
621 Token::Word(w) if w.keyword == Keyword::DIV => Ok(p!(MulDivModOp)),
622 Token::Period => Ok(p!(Period)),
623 Token::Assignment
624 | Token::Eq
625 | Token::Lt
626 | Token::LtEq
627 | Token::Neq
628 | Token::Gt
629 | Token::GtEq
630 | Token::DoubleEq
631 | Token::Tilde
632 | Token::TildeAsterisk
633 | Token::ExclamationMarkTilde
634 | Token::ExclamationMarkTildeAsterisk
635 | Token::DoubleTilde
636 | Token::DoubleTildeAsterisk
637 | Token::ExclamationMarkDoubleTilde
638 | Token::ExclamationMarkDoubleTildeAsterisk
639 | Token::Spaceship => Ok(p!(Eq)),
640 Token::Pipe
641 | Token::QuestionMarkDash
642 | Token::DoubleSharp
643 | Token::Overlap
644 | Token::AmpersandLeftAngleBracket
645 | Token::AmpersandRightAngleBracket
646 | Token::QuestionMarkDashVerticalBar
647 | Token::AmpersandLeftAngleBracketVerticalBar
648 | Token::VerticalBarAmpersandRightAngleBracket
649 | Token::TwoWayArrow
650 | Token::LeftAngleBracketCaret
651 | Token::RightAngleBracketCaret
652 | Token::QuestionMarkSharp
653 | Token::QuestionMarkDoubleVerticalBar
654 | Token::QuestionPipe
655 | Token::TildeEqual
656 | Token::AtSign
657 | Token::ShiftLeftVerticalBar
658 | Token::VerticalBarShiftRight => Ok(p!(Pipe)),
659 Token::Caret | Token::Sharp | Token::ShiftRight | Token::ShiftLeft => Ok(p!(Caret)),
660 Token::Ampersand => Ok(p!(Ampersand)),
661 Token::Plus | Token::Minus => Ok(p!(PlusMinus)),
662 Token::Mul | Token::Div | Token::DuckIntDiv | Token::Mod | Token::StringConcat => {
663 Ok(p!(MulDivModOp))
664 }
665 Token::DoubleColon | Token::ExclamationMark | Token::LBracket | Token::CaretAt => {
666 Ok(p!(DoubleColon))
667 }
668 Token::Arrow
669 | Token::LongArrow
670 | Token::HashArrow
671 | Token::HashLongArrow
672 | Token::AtArrow
673 | Token::ArrowAt
674 | Token::HashMinus
675 | Token::AtQuestion
676 | Token::AtAt
677 | Token::Question
678 | Token::QuestionAnd
679 | Token::CustomBinaryOperator(_) => Ok(p!(PgOther)),
680 _ => Ok(self.prec_unknown()),
681 }
682 }
683
684 /// Dialect-specific statement parser override
685 ///
686 /// This method is called to parse the next statement.
687 ///
688 /// If `None` is returned, falls back to the default behavior.
689 fn parse_statement(&self, _parser: &mut Parser) -> Option<Result<Statement, ParserError>> {
690 // return None to fall back to the default behavior
691 None
692 }
693
694 /// Dialect-specific column option parser override
695 ///
696 /// This method is called to parse the next column option.
697 ///
698 /// If `None` is returned, falls back to the default behavior.
699 fn parse_column_option(
700 &self,
701 _parser: &mut Parser,
702 ) -> Result<Option<Result<Option<ColumnOption>, ParserError>>, ParserError> {
703 // return None to fall back to the default behavior
704 Ok(None)
705 }
706
707 /// Decide the lexical Precedence of operators.
708 ///
709 /// Uses (APPROXIMATELY) <https://www.postgresql.org/docs/7.0/operators.htm#AEN2026> as a reference
710 fn prec_value(&self, prec: Precedence) -> u8 {
711 match prec {
712 Precedence::Period => 100,
713 Precedence::DoubleColon => 50,
714 Precedence::AtTz => 41,
715 Precedence::MulDivModOp => 40,
716 Precedence::PlusMinus => 30,
717 Precedence::Xor => 24,
718 Precedence::Ampersand => 23,
719 Precedence::Caret => 22,
720 Precedence::Pipe => 21,
721 Precedence::Between => 20,
722 Precedence::Eq => 20,
723 Precedence::Like => 19,
724 Precedence::Is => 17,
725 Precedence::PgOther => 16,
726 Precedence::UnaryNot => 15,
727 Precedence::And => 10,
728 Precedence::Or => 5,
729 }
730 }
731
732 /// Returns the precedence when the precedence is otherwise unknown
733 fn prec_unknown(&self) -> u8 {
734 0
735 }
736
737 /// Returns true if this dialect requires the `TABLE` keyword after `DESCRIBE`
738 ///
739 /// Defaults to false.
740 ///
741 /// If true, the following statement is valid: `DESCRIBE TABLE my_table`
742 /// If false, the following statements are valid: `DESCRIBE my_table` and `DESCRIBE table`
743 fn describe_requires_table_keyword(&self) -> bool {
744 false
745 }
746
747 /// Returns true if this dialect allows the `EXTRACT` function to words other than [`Keyword`].
748 fn allow_extract_custom(&self) -> bool {
749 false
750 }
751
752 /// Returns true if this dialect allows the `EXTRACT` function to use single quotes in the part being extracted.
753 fn allow_extract_single_quotes(&self) -> bool {
754 false
755 }
756
757 /// Returns true if this dialect allows dollar placeholders
758 /// e.g. `SELECT $var` (SQLite)
759 fn supports_dollar_placeholder(&self) -> bool {
760 false
761 }
762
763 /// Does the dialect support with clause in create index statement?
764 /// e.g. `CREATE INDEX idx ON t WITH (key = value, key2)`
765 fn supports_create_index_with_clause(&self) -> bool {
766 false
767 }
768
769 /// Whether `INTERVAL` expressions require units (called "qualifiers" in the ANSI SQL spec) to be specified,
770 /// e.g. `INTERVAL 1 DAY` vs `INTERVAL 1`.
771 ///
772 /// Expressions within intervals (e.g. `INTERVAL '1' + '1' DAY`) are only allowed when units are required.
773 ///
774 /// See <https://github.com/sqlparser-rs/sqlparser-rs/pull/1398> for more information.
775 ///
776 /// When `true`:
777 /// * `INTERVAL '1' DAY` is VALID
778 /// * `INTERVAL 1 + 1 DAY` is VALID
779 /// * `INTERVAL '1' + '1' DAY` is VALID
780 /// * `INTERVAL '1'` is INVALID
781 ///
782 /// When `false`:
783 /// * `INTERVAL '1'` is VALID
784 /// * `INTERVAL '1' DAY` is VALID — unit is not required, but still allowed
785 /// * `INTERVAL 1 + 1 DAY` is INVALID
786 fn require_interval_qualifier(&self) -> bool {
787 false
788 }
789
790 fn supports_explain_with_utility_options(&self) -> bool {
791 false
792 }
793
794 fn supports_asc_desc_in_column_definition(&self) -> bool {
795 false
796 }
797
798 /// Returns true if the dialect supports `a!` expressions
799 fn supports_factorial_operator(&self) -> bool {
800 false
801 }
802
803 /// Returns true if the dialect supports nested comments
804 /// e.g. `/* /* nested */ */`
805 fn supports_nested_comments(&self) -> bool {
806 false
807 }
808
809 /// Returns true if this dialect supports treating the equals operator `=` within a `SelectItem`
810 /// as an alias assignment operator, rather than a boolean expression.
811 /// For example: the following statements are equivalent for such a dialect:
812 /// ```sql
813 /// SELECT col_alias = col FROM tbl;
814 /// SELECT col_alias AS col FROM tbl;
815 /// ```
816 fn supports_eq_alias_assignment(&self) -> bool {
817 false
818 }
819
820 /// Returns true if this dialect supports the `TRY_CONVERT` function
821 fn supports_try_convert(&self) -> bool {
822 false
823 }
824
825 /// Returns true if the dialect supports `!a` syntax for boolean `NOT` expressions.
826 fn supports_bang_not_operator(&self) -> bool {
827 false
828 }
829
830 /// Returns true if the dialect supports the `LISTEN`, `UNLISTEN` and `NOTIFY` statements
831 fn supports_listen_notify(&self) -> bool {
832 false
833 }
834
835 /// Returns true if the dialect supports the `LOAD DATA` statement
836 fn supports_load_data(&self) -> bool {
837 false
838 }
839
840 /// Returns true if the dialect supports the `LOAD extension` statement
841 fn supports_load_extension(&self) -> bool {
842 false
843 }
844
845 /// Returns true if this dialect expects the `TOP` option
846 /// before the `ALL`/`DISTINCT` options in a `SELECT` statement.
847 fn supports_top_before_distinct(&self) -> bool {
848 false
849 }
850
851 /// Returns true if the dialect supports boolean literals (`true` and `false`).
852 /// For example, in MSSQL these are treated as identifiers rather than boolean literals.
853 fn supports_boolean_literals(&self) -> bool {
854 true
855 }
856
857 /// Returns true if this dialect supports the `LIKE 'pattern'` option in
858 /// a `SHOW` statement before the `IN` option
859 fn supports_show_like_before_in(&self) -> bool {
860 false
861 }
862
863 /// Returns true if this dialect supports the `COMMENT` statement
864 fn supports_comment_on(&self) -> bool {
865 false
866 }
867
868 /// Returns true if the dialect supports the `CREATE TABLE SELECT` statement
869 fn supports_create_table_select(&self) -> bool {
870 false
871 }
872
873 /// Returns true if the dialect supports PartiQL for querying semi-structured data
874 /// <https://partiql.org/index.html>
875 fn supports_partiql(&self) -> bool {
876 false
877 }
878
879 /// Returns true if the specified keyword is reserved and cannot be
880 /// used as an identifier without special handling like quoting.
881 fn is_reserved_for_identifier(&self, kw: Keyword) -> bool {
882 keywords::RESERVED_FOR_IDENTIFIER.contains(&kw)
883 }
884
885 /// Returns reserved keywords when looking to parse a `TableFactor`.
886 /// See [Self::supports_from_trailing_commas]
887 fn get_reserved_keywords_for_table_factor(&self) -> &[Keyword] {
888 keywords::RESERVED_FOR_TABLE_FACTOR
889 }
890
891 /// Returns reserved keywords that may prefix a select item expression
892 /// e.g. `SELECT CONNECT_BY_ROOT name FROM Tbl2` (Snowflake)
893 fn get_reserved_keywords_for_select_item_operator(&self) -> &[Keyword] {
894 &[]
895 }
896
897 /// Returns true if this dialect supports the `TABLESAMPLE` option
898 /// before the table alias option. For example:
899 ///
900 /// Table sample before alias: `SELECT * FROM tbl AS t TABLESAMPLE (10)`
901 /// Table sample after alias: `SELECT * FROM tbl TABLESAMPLE (10) AS t`
902 ///
903 /// <https://jakewheat.github.io/sql-overview/sql-2016-foundation-grammar.html#_7_6_table_reference>
904 fn supports_table_sample_before_alias(&self) -> bool {
905 false
906 }
907
908 /// Returns true if this dialect supports the `INSERT INTO ... SET col1 = 1, ...` syntax.
909 ///
910 /// MySQL: <https://dev.mysql.com/doc/refman/8.4/en/insert.html>
911 fn supports_insert_set(&self) -> bool {
912 false
913 }
914
915 /// Does the dialect support table function in insertion?
916 fn supports_insert_table_function(&self) -> bool {
917 false
918 }
919
920 /// Does the dialect support insert formats, e.g. `INSERT INTO ... FORMAT <format>`
921 fn supports_insert_format(&self) -> bool {
922 false
923 }
924
925 /// Returns true if this dialect supports `SET` statements without an explicit
926 /// assignment operator such as `=`. For example: `SET SHOWPLAN_XML ON`.
927 fn supports_set_stmt_without_operator(&self) -> bool {
928 false
929 }
930
931 /// Returns true if the specified keyword should be parsed as a column identifier.
932 /// See [keywords::RESERVED_FOR_COLUMN_ALIAS]
933 fn is_column_alias(&self, kw: &Keyword, _parser: &mut Parser) -> bool {
934 !keywords::RESERVED_FOR_COLUMN_ALIAS.contains(kw)
935 }
936
937 /// Returns true if the specified keyword should be parsed as a select item alias.
938 /// When explicit is true, the keyword is preceded by an `AS` word. Parser is provided
939 /// to enable looking ahead if needed.
940 fn is_select_item_alias(&self, explicit: bool, kw: &Keyword, parser: &mut Parser) -> bool {
941 explicit || self.is_column_alias(kw, parser)
942 }
943
944 /// Returns true if the specified keyword should be parsed as a table factor alias.
945 /// When explicit is true, the keyword is preceded by an `AS` word. Parser is provided
946 /// to enable looking ahead if needed.
947 fn is_table_factor_alias(&self, explicit: bool, kw: &Keyword, _parser: &mut Parser) -> bool {
948 explicit || !keywords::RESERVED_FOR_TABLE_ALIAS.contains(kw)
949 }
950
951 /// Returns true if this dialect supports querying historical table data
952 /// by specifying which version of the data to query.
953 fn supports_timestamp_versioning(&self) -> bool {
954 false
955 }
956
957 /// Returns true if this dialect supports the E'...' syntax for string literals
958 ///
959 /// Postgres: <https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS-ESCAPE>
960 fn supports_string_escape_constant(&self) -> bool {
961 false
962 }
963
964 /// Returns true if the dialect supports the table hints in the `FROM` clause.
965 fn supports_table_hints(&self) -> bool {
966 false
967 }
968
969 /// Returns true if this dialect requires a whitespace character after `--` to start a single line comment.
970 ///
971 /// MySQL: <https://dev.mysql.com/doc/refman/8.4/en/ansi-diff-comments.html>
972 /// e.g. UPDATE account SET balance=balance--1
973 // WHERE account_id=5752 ^^^ will be interpreted as two minus signs instead of a comment
974 fn requires_single_line_comment_whitespace(&self) -> bool {
975 false
976 }
977
978 /// Returns true if the dialect supports array type definition with brackets with
979 /// an optional size. For example:
980 /// ```CREATE TABLE my_table (arr1 INT[], arr2 INT[3])```
981 /// ```SELECT x::INT[]```
982 fn supports_array_typedef_with_brackets(&self) -> bool {
983 false
984 }
985 /// Returns true if the dialect supports geometric types.
986 ///
987 /// Postgres: <https://www.postgresql.org/docs/9.5/functions-geometry.html>
988 /// e.g. @@ circle '((0,0),10)'
989 fn supports_geometric_types(&self) -> bool {
990 false
991 }
992
993 /// Returns true if the dialect supports `ORDER BY ALL`.
994 /// `ALL` which means all columns of the SELECT clause.
995 ///
996 /// For example: ```SELECT * FROM addresses ORDER BY ALL;```.
997 fn supports_order_by_all(&self) -> bool {
998 false
999 }
1000
1001 /// Returns true if the dialect supports `SET NAMES <charset_name> [COLLATE <collation_name>]`.
1002 ///
1003 /// - [MySQL](https://dev.mysql.com/doc/refman/8.4/en/set-names.html)
1004 /// - [PostgreSQL](https://www.postgresql.org/docs/17/sql-set.html)
1005 ///
1006 /// Note: Postgres doesn't support the `COLLATE` clause, but we permissively parse it anyway.
1007 fn supports_set_names(&self) -> bool {
1008 false
1009 }
1010}
1011
1012/// This represents the operators for which precedence must be defined
1013///
1014/// higher number -> higher precedence
1015#[derive(Debug, Clone, Copy)]
1016pub enum Precedence {
1017 Period,
1018 DoubleColon,
1019 AtTz,
1020 MulDivModOp,
1021 PlusMinus,
1022 Xor,
1023 Ampersand,
1024 Caret,
1025 Pipe,
1026 Between,
1027 Eq,
1028 Like,
1029 Is,
1030 PgOther,
1031 UnaryNot,
1032 And,
1033 Or,
1034}
1035
1036impl dyn Dialect {
1037 #[inline]
1038 pub fn is<T: Dialect>(&self) -> bool {
1039 // borrowed from `Any` implementation
1040 TypeId::of::<T>() == self.dialect()
1041 }
1042}
1043
1044/// Returns the built in [`Dialect`] corresponding to `dialect_name`.
1045///
1046/// See [`Dialect`] documentation for an example.
1047pub fn dialect_from_str(dialect_name: impl AsRef<str>) -> Option<Box<dyn Dialect>> {
1048 let dialect_name = dialect_name.as_ref();
1049 match dialect_name.to_lowercase().as_str() {
1050 "generic" => Some(Box::new(GenericDialect)),
1051 "mysql" => Some(Box::new(MySqlDialect {})),
1052 "postgresql" | "postgres" => Some(Box::new(PostgreSqlDialect {})),
1053 "hive" => Some(Box::new(HiveDialect {})),
1054 "sqlite" => Some(Box::new(SQLiteDialect {})),
1055 "snowflake" => Some(Box::new(SnowflakeDialect)),
1056 "redshift" => Some(Box::new(RedshiftSqlDialect {})),
1057 "mssql" => Some(Box::new(MsSqlDialect {})),
1058 "clickhouse" => Some(Box::new(ClickHouseDialect {})),
1059 "bigquery" => Some(Box::new(BigQueryDialect)),
1060 "ansi" => Some(Box::new(AnsiDialect {})),
1061 "duckdb" => Some(Box::new(DuckDbDialect {})),
1062 "databricks" => Some(Box::new(DatabricksDialect {})),
1063 _ => None,
1064 }
1065}
1066
1067#[cfg(test)]
1068mod tests {
1069 use super::*;
1070
1071 struct DialectHolder<'a> {
1072 dialect: &'a dyn Dialect,
1073 }
1074
1075 #[test]
1076 fn test_is_dialect() {
1077 let generic_dialect: &dyn Dialect = &GenericDialect {};
1078 let ansi_dialect: &dyn Dialect = &AnsiDialect {};
1079
1080 let generic_holder = DialectHolder {
1081 dialect: generic_dialect,
1082 };
1083 let ansi_holder = DialectHolder {
1084 dialect: ansi_dialect,
1085 };
1086
1087 assert!(dialect_of!(generic_holder is GenericDialect | AnsiDialect),);
1088 assert!(!dialect_of!(generic_holder is AnsiDialect));
1089 assert!(dialect_of!(ansi_holder is AnsiDialect));
1090 assert!(dialect_of!(ansi_holder is GenericDialect | AnsiDialect));
1091 assert!(!dialect_of!(ansi_holder is GenericDialect | MsSqlDialect));
1092 }
1093
1094 #[test]
1095 fn test_dialect_from_str() {
1096 assert!(parse_dialect("generic").is::<GenericDialect>());
1097 assert!(parse_dialect("mysql").is::<MySqlDialect>());
1098 assert!(parse_dialect("MySql").is::<MySqlDialect>());
1099 assert!(parse_dialect("postgresql").is::<PostgreSqlDialect>());
1100 assert!(parse_dialect("postgres").is::<PostgreSqlDialect>());
1101 assert!(parse_dialect("hive").is::<HiveDialect>());
1102 assert!(parse_dialect("sqlite").is::<SQLiteDialect>());
1103 assert!(parse_dialect("snowflake").is::<SnowflakeDialect>());
1104 assert!(parse_dialect("SnowFlake").is::<SnowflakeDialect>());
1105 assert!(parse_dialect("MsSql").is::<MsSqlDialect>());
1106 assert!(parse_dialect("clickhouse").is::<ClickHouseDialect>());
1107 assert!(parse_dialect("ClickHouse").is::<ClickHouseDialect>());
1108 assert!(parse_dialect("bigquery").is::<BigQueryDialect>());
1109 assert!(parse_dialect("BigQuery").is::<BigQueryDialect>());
1110 assert!(parse_dialect("ansi").is::<AnsiDialect>());
1111 assert!(parse_dialect("ANSI").is::<AnsiDialect>());
1112 assert!(parse_dialect("duckdb").is::<DuckDbDialect>());
1113 assert!(parse_dialect("DuckDb").is::<DuckDbDialect>());
1114 assert!(parse_dialect("DataBricks").is::<DatabricksDialect>());
1115 assert!(parse_dialect("databricks").is::<DatabricksDialect>());
1116
1117 // error cases
1118 assert!(dialect_from_str("Unknown").is_none());
1119 assert!(dialect_from_str("").is_none());
1120 }
1121
1122 fn parse_dialect(v: &str) -> Box<dyn Dialect> {
1123 dialect_from_str(v).unwrap()
1124 }
1125
1126 #[test]
1127 fn identifier_quote_style() {
1128 let tests: Vec<(&dyn Dialect, &str, Option<char>)> = vec![
1129 (&GenericDialect {}, "id", None),
1130 (&SQLiteDialect {}, "id", Some('`')),
1131 (&PostgreSqlDialect {}, "id", Some('"')),
1132 ];
1133
1134 for (dialect, ident, expected) in tests {
1135 let actual = dialect.identifier_quote_style(ident);
1136
1137 assert_eq!(actual, expected);
1138 }
1139 }
1140
1141 #[test]
1142 fn parse_with_wrapped_dialect() {
1143 /// Wrapper for a dialect. In a real-world example, this wrapper
1144 /// would tweak the behavior of the dialect. For the test case,
1145 /// it wraps all methods unaltered.
1146 #[derive(Debug)]
1147 struct WrappedDialect(MySqlDialect);
1148
1149 impl Dialect for WrappedDialect {
1150 fn dialect(&self) -> std::any::TypeId {
1151 self.0.dialect()
1152 }
1153
1154 fn is_identifier_start(&self, ch: char) -> bool {
1155 self.0.is_identifier_start(ch)
1156 }
1157
1158 fn is_delimited_identifier_start(&self, ch: char) -> bool {
1159 self.0.is_delimited_identifier_start(ch)
1160 }
1161
1162 fn is_nested_delimited_identifier_start(&self, ch: char) -> bool {
1163 self.0.is_nested_delimited_identifier_start(ch)
1164 }
1165
1166 fn peek_nested_delimited_identifier_quotes(
1167 &self,
1168 chars: std::iter::Peekable<std::str::Chars<'_>>,
1169 ) -> Option<(char, Option<char>)> {
1170 self.0.peek_nested_delimited_identifier_quotes(chars)
1171 }
1172
1173 fn identifier_quote_style(&self, identifier: &str) -> Option<char> {
1174 self.0.identifier_quote_style(identifier)
1175 }
1176
1177 fn supports_string_literal_backslash_escape(&self) -> bool {
1178 self.0.supports_string_literal_backslash_escape()
1179 }
1180
1181 fn supports_filter_during_aggregation(&self) -> bool {
1182 self.0.supports_filter_during_aggregation()
1183 }
1184
1185 fn supports_within_after_array_aggregation(&self) -> bool {
1186 self.0.supports_within_after_array_aggregation()
1187 }
1188
1189 fn supports_group_by_expr(&self) -> bool {
1190 self.0.supports_group_by_expr()
1191 }
1192
1193 fn supports_in_empty_list(&self) -> bool {
1194 self.0.supports_in_empty_list()
1195 }
1196
1197 fn convert_type_before_value(&self) -> bool {
1198 self.0.convert_type_before_value()
1199 }
1200
1201 fn parse_prefix(
1202 &self,
1203 parser: &mut sqlparser::parser::Parser,
1204 ) -> Option<Result<Expr, sqlparser::parser::ParserError>> {
1205 self.0.parse_prefix(parser)
1206 }
1207
1208 fn parse_infix(
1209 &self,
1210 parser: &mut sqlparser::parser::Parser,
1211 expr: &Expr,
1212 precedence: u8,
1213 ) -> Option<Result<Expr, sqlparser::parser::ParserError>> {
1214 self.0.parse_infix(parser, expr, precedence)
1215 }
1216
1217 fn get_next_precedence(
1218 &self,
1219 parser: &sqlparser::parser::Parser,
1220 ) -> Option<Result<u8, sqlparser::parser::ParserError>> {
1221 self.0.get_next_precedence(parser)
1222 }
1223
1224 fn parse_statement(
1225 &self,
1226 parser: &mut sqlparser::parser::Parser,
1227 ) -> Option<Result<Statement, sqlparser::parser::ParserError>> {
1228 self.0.parse_statement(parser)
1229 }
1230
1231 fn is_identifier_part(&self, ch: char) -> bool {
1232 self.0.is_identifier_part(ch)
1233 }
1234 }
1235
1236 #[allow(clippy::needless_raw_string_hashes)]
1237 let statement = r#"SELECT 'Wayne\'s World'"#;
1238 let res1 = Parser::parse_sql(&MySqlDialect {}, statement);
1239 let res2 = Parser::parse_sql(&WrappedDialect(MySqlDialect {}), statement);
1240 assert!(res1.is_ok());
1241 assert_eq!(res1, res2);
1242 }
1243}