Add Redshift dialect, handle square brackets properly (#471)

* Redshift square bracket handling

We need to detect `[` or `"` for Redshift quotes around indentifier and at the same time exclude
treating JSON paths as indentifer

* RedshiftSqlDialect documentation update

Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>

* Renamed _chars to chars

* Fixed warnings

* Missing license

Co-authored-by: Maciej Skrzypkowski <maciej.skrzypkowski@satoricyber.com>
Co-authored-by: Andrew Lamb <andrew@nerdnetworks.org>
This commit is contained in:
Maciej Skrzypkowski 2022-05-04 17:11:00 +02:00 committed by GitHub
parent a9d7f7af1f
commit 7fc6361fe8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 173 additions and 3 deletions

102
tests/sqlparser_redshift.rs Normal file
View file

@ -0,0 +1,102 @@
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#[macro_use]
mod test_utils;
use test_utils::*;
use sqlparser::ast::*;
use sqlparser::dialect::RedshiftSqlDialect;
#[test]
fn test_square_brackets_over_db_schema_table_name() {
let select = redshift().verified_only_select("SELECT [col1] FROM [test_schema].[test_table]");
assert_eq!(
select.projection[0],
SelectItem::UnnamedExpr(Expr::Identifier(Ident {
value: "col1".to_string(),
quote_style: Some('[')
})),
);
assert_eq!(
select.from[0],
TableWithJoins {
relation: TableFactor::Table {
name: ObjectName(vec![
Ident {
value: "test_schema".to_string(),
quote_style: Some('[')
},
Ident {
value: "test_table".to_string(),
quote_style: Some('[')
}
]),
alias: None,
args: vec![],
with_hints: vec![],
},
joins: vec![],
}
);
}
#[test]
fn brackets_over_db_schema_table_name_with_whites_paces() {
match redshift().parse_sql_statements("SELECT [ col1 ] FROM [ test_schema].[ test_table]") {
Ok(statements) => {
assert_eq!(statements.len(), 1);
}
_ => unreachable!(),
}
}
#[test]
fn test_double_quotes_over_db_schema_table_name() {
let select =
redshift().verified_only_select("SELECT \"col1\" FROM \"test_schema\".\"test_table\"");
assert_eq!(
select.projection[0],
SelectItem::UnnamedExpr(Expr::Identifier(Ident {
value: "col1".to_string(),
quote_style: Some('"')
})),
);
assert_eq!(
select.from[0],
TableWithJoins {
relation: TableFactor::Table {
name: ObjectName(vec![
Ident {
value: "test_schema".to_string(),
quote_style: Some('"')
},
Ident {
value: "test_table".to_string(),
quote_style: Some('"')
}
]),
alias: None,
args: vec![],
with_hints: vec![],
},
joins: vec![],
}
);
}
fn redshift() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(RedshiftSqlDialect {})],
}
}