sqlparser/dialect/redshift.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 core::iter::Peekable;
20use core::str::Chars;
21
22use super::PostgreSqlDialect;
23
24/// A [`Dialect`] for [RedShift](https://aws.amazon.com/redshift/)
25#[derive(Debug)]
26pub struct RedshiftSqlDialect {}
27
28// In most cases the redshift dialect is identical to [`PostgresSqlDialect`].
29//
30// Notable differences:
31// 1. Redshift treats brackets `[` and `]` differently. For example, `SQL SELECT a[1][2] FROM b`
32// in the Postgres dialect, the query will be parsed as an array, while in the Redshift dialect it will
33// be a json path
34impl Dialect for RedshiftSqlDialect {
35 /// Determine if a character starts a potential nested quoted identifier.
36 /// Example: RedShift supports the following quote styles to all mean the same thing:
37 /// ```sql
38 /// SELECT 1 AS foo;
39 /// SELECT 1 AS "foo";
40 /// SELECT 1 AS [foo];
41 /// SELECT 1 AS ["foo"];
42 /// ```
43 fn is_nested_delimited_identifier_start(&self, ch: char) -> bool {
44 ch == '['
45 }
46
47 /// Only applicable whenever [`Self::is_nested_delimited_identifier_start`] returns true
48 /// If the next sequence of tokens potentially represent a nested identifier, then this method
49 /// returns a tuple containing the outer quote style, and if present, the inner (nested) quote style.
50 ///
51 /// Example (Redshift):
52 /// ```text
53 /// `["foo"]` => Some(`[`, Some(`"`))
54 /// `[foo]` => Some(`[`, None)
55 /// `[0]` => None
56 /// `"foo"` => None
57 /// ```
58 fn peek_nested_delimited_identifier_quotes(
59 &self,
60 mut chars: Peekable<Chars<'_>>,
61 ) -> Option<(char, Option<char>)> {
62 if chars.peek() != Some(&'[') {
63 return None;
64 }
65
66 chars.next();
67
68 let mut not_white_chars = chars.skip_while(|ch| ch.is_whitespace()).peekable();
69
70 if let Some(&ch) = not_white_chars.peek() {
71 if ch == '"' {
72 return Some(('[', Some('"')));
73 }
74 if self.is_identifier_start(ch) {
75 return Some(('[', None));
76 }
77 }
78
79 None
80 }
81
82 fn is_identifier_start(&self, ch: char) -> bool {
83 // Extends Postgres dialect with sharp
84 PostgreSqlDialect {}.is_identifier_start(ch) || ch == '#'
85 }
86
87 fn is_identifier_part(&self, ch: char) -> bool {
88 // Extends Postgres dialect with sharp
89 PostgreSqlDialect {}.is_identifier_part(ch) || ch == '#'
90 }
91
92 /// redshift has `CONVERT(type, value)` instead of `CONVERT(value, type)`
93 /// <https://docs.aws.amazon.com/redshift/latest/dg/r_CONVERT_function.html>
94 fn convert_type_before_value(&self) -> bool {
95 true
96 }
97
98 fn supports_connect_by(&self) -> bool {
99 true
100 }
101
102 /// Redshift expects the `TOP` option before the `ALL/DISTINCT` option:
103 /// <https://docs.aws.amazon.com/redshift/latest/dg/r_SELECT_list.html#r_SELECT_list-parameters>
104 fn supports_top_before_distinct(&self) -> bool {
105 true
106 }
107
108 /// Redshift supports PartiQL: <https://docs.aws.amazon.com/redshift/latest/dg/super-overview.html>
109 fn supports_partiql(&self) -> bool {
110 true
111 }
112
113 fn supports_string_escape_constant(&self) -> bool {
114 true
115 }
116
117 fn supports_geometric_types(&self) -> bool {
118 true
119 }
120
121 fn supports_array_typedef_with_brackets(&self) -> bool {
122 true
123 }
124
125 fn allow_extract_single_quotes(&self) -> bool {
126 true
127 }
128
129 fn supports_string_literal_backslash_escape(&self) -> bool {
130 true
131 }
132}