Skip to main content

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