sqltk_parser/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    fn is_delimited_identifier_start(&self, ch: char) -> bool {
36        ch == '"' || ch == '['
37    }
38
39    /// Determine if quoted characters are proper for identifier
40    /// It's needed to distinguish treating square brackets as quotes from
41    /// treating them as json path. If there is identifier then we assume
42    /// there is no json path.
43    fn is_proper_identifier_inside_quotes(&self, mut chars: Peekable<Chars<'_>>) -> bool {
44        chars.next();
45        let mut not_white_chars = chars.skip_while(|ch| ch.is_whitespace()).peekable();
46        if let Some(&ch) = not_white_chars.peek() {
47            return self.is_identifier_start(ch);
48        }
49        false
50    }
51
52    fn is_identifier_start(&self, ch: char) -> bool {
53        // Extends Postgres dialect with sharp
54        PostgreSqlDialect {}.is_identifier_start(ch) || ch == '#'
55    }
56
57    fn is_identifier_part(&self, ch: char) -> bool {
58        // Extends Postgres dialect with sharp
59        PostgreSqlDialect {}.is_identifier_part(ch) || ch == '#'
60    }
61
62    /// redshift has `CONVERT(type, value)` instead of `CONVERT(value, type)`
63    /// <https://docs.aws.amazon.com/redshift/latest/dg/r_CONVERT_function.html>
64    fn convert_type_before_value(&self) -> bool {
65        true
66    }
67
68    fn supports_connect_by(&self) -> bool {
69        true
70    }
71
72    /// Redshift expects the `TOP` option before the `ALL/DISTINCT` option:
73    /// <https://docs.aws.amazon.com/redshift/latest/dg/r_SELECT_list.html#r_SELECT_list-parameters>
74    fn supports_top_before_distinct(&self) -> bool {
75        true
76    }
77
78    /// Redshift supports PartiQL: <https://docs.aws.amazon.com/redshift/latest/dg/super-overview.html>
79    fn supports_partiql(&self) -> bool {
80        true
81    }
82}