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/// A [`Dialect`] for [RedShift](https://aws.amazon.com/redshift/)
20#[derive(Debug)]
21pub struct RedshiftSqlDialect {}
22
23// In most cases the redshift dialect is identical to [`PostgresSqlDialect`].
24//
25// Notable differences:
26// 1. Redshift treats brackets `[` and `]` differently. For example, `SQL SELECT a[1][2] FROM b`
27// in the Postgres dialect, the query will be parsed as an array, while in the Redshift dialect it will
28// be a json path
29impl Dialect for RedshiftSqlDialect {
30 fn is_delimited_identifier_start(&self, ch: char) -> bool {
31 ch == '"' || ch == '['
32 }
33
34 /// Determine if quoted characters are proper for identifier
35 /// It's needed to distinguish treating square brackets as quotes from
36 /// treating them as json path. If there is identifier then we assume
37 /// there is no json path.
38 fn is_proper_identifier_inside_quotes(&self, mut chars: Peekable<Chars<'_>>) -> bool {
39 chars.next();
40 let mut not_white_chars = chars.skip_while(|ch| ch.is_whitespace()).peekable();
41 if let Some(&ch) = not_white_chars.peek() {
42 return self.is_identifier_start(ch);
43 }
44 false
45 }
46
47 fn is_identifier_start(&self, ch: char) -> bool {
48 // Extends Postgres dialect with sharp
49 PostgreSqlDialect {}.is_identifier_start(ch) || ch == '#'
50 }
51
52 fn is_identifier_part(&self, ch: char) -> bool {
53 // Extends Postgres dialect with sharp
54 PostgreSqlDialect {}.is_identifier_part(ch) || ch == '#'
55 }
56}