sqlparser/dialect/
redshift.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5// http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12
13use crate::dialect::Dialect;
14use core::iter::Peekable;
15use core::str::Chars;
16
17use super::PostgreSqlDialect;
18
19#[derive(Debug)]
20pub struct RedshiftSqlDialect {}
21
22// In most cases the redshift dialect is identical to [`PostgresSqlDialect`].
23//
24// Notable differences:
25// 1. Redshift treats brackets `[` and `]` differently. For example, `SQL SELECT a[1][2] FROM b`
26// in the Postgres dialect, the query will be parsed as an array, while in the Redshift dialect it will
27// be a json path
28impl Dialect for RedshiftSqlDialect {
29    fn is_delimited_identifier_start(&self, ch: char) -> bool {
30        ch == '"' || ch == '['
31    }
32
33    /// Determine if quoted characters are proper for identifier
34    /// It's needed to distinguish treating square brackets as quotes from
35    /// treating them as json path. If there is identifier then we assume
36    /// there is no json path.
37    fn is_proper_identifier_inside_quotes(&self, mut chars: Peekable<Chars<'_>>) -> bool {
38        chars.next();
39        let mut not_white_chars = chars.skip_while(|ch| ch.is_whitespace()).peekable();
40        if let Some(&ch) = not_white_chars.peek() {
41            return self.is_identifier_start(ch);
42        }
43        false
44    }
45
46    fn is_identifier_start(&self, ch: char) -> bool {
47        // Extends Postgres dialect with sharp
48        PostgreSqlDialect {}.is_identifier_start(ch) || ch == '#'
49    }
50
51    fn is_identifier_part(&self, ch: char) -> bool {
52        // Extends Postgres dialect with sharp
53        PostgreSqlDialect {}.is_identifier_part(ch) || ch == '#'
54    }
55}