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        // UTF-8 multibyte characters are supported in identifiers via the PostgreSqlDialect.
84        // https://docs.aws.amazon.com/redshift/latest/dg/r_names.html
85        PostgreSqlDialect {}.is_identifier_start(ch) || ch == '#'
86    }
87
88    fn is_identifier_part(&self, ch: char) -> bool {
89        // UTF-8 multibyte characters are supported in identifiers via the PostgreSqlDialect.
90        // https://docs.aws.amazon.com/redshift/latest/dg/r_names.html
91        PostgreSqlDialect {}.is_identifier_part(ch) || ch == '#'
92    }
93
94    /// redshift has `CONVERT(type, value)` instead of `CONVERT(value, type)`
95    /// <https://docs.aws.amazon.com/redshift/latest/dg/r_CONVERT_function.html>
96    fn convert_type_before_value(&self) -> bool {
97        true
98    }
99
100    fn supports_connect_by(&self) -> bool {
101        true
102    }
103
104    /// Redshift expects the `TOP` option before the `ALL/DISTINCT` option:
105    /// <https://docs.aws.amazon.com/redshift/latest/dg/r_SELECT_list.html#r_SELECT_list-parameters>
106    fn supports_top_before_distinct(&self) -> bool {
107        true
108    }
109
110    /// Redshift supports PartiQL: <https://docs.aws.amazon.com/redshift/latest/dg/super-overview.html>
111    fn supports_partiql(&self) -> bool {
112        true
113    }
114
115    fn supports_string_escape_constant(&self) -> bool {
116        true
117    }
118
119    fn supports_geometric_types(&self) -> bool {
120        true
121    }
122
123    fn supports_array_typedef_with_brackets(&self) -> bool {
124        true
125    }
126
127    fn allow_extract_single_quotes(&self) -> bool {
128        true
129    }
130
131    fn supports_string_literal_backslash_escape(&self) -> bool {
132        true
133    }
134
135    fn supports_select_wildcard_exclude(&self) -> bool {
136        true
137    }
138
139    fn supports_select_exclude(&self) -> bool {
140        true
141    }
142}