Skip to content

Redshift escape string literal logic. #28

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ use sqlparser_derive::{Visit, VisitMut};

use crate::ast::DollarQuotedString;
use crate::dialect::{
BigQueryDialect, DuckDbDialect, GenericDialect, HiveDialect, SnowflakeDialect,
BigQueryDialect, DuckDbDialect, GenericDialect, HiveDialect, RedshiftSqlDialect,
SnowflakeDialect,
};
use crate::dialect::{Dialect, MySqlDialect};
use crate::keywords::{Keyword, ALL_KEYWORDS, ALL_KEYWORDS_INDEX};
Expand Down Expand Up @@ -1365,7 +1366,7 @@ impl<'a> Tokenizer<'a> {
// consume
chars.next();
// slash escaping is specific to MySQL / BigQuery dialect.
if dialect_of!(self is MySqlDialect | BigQueryDialect) {
if dialect_of!(self is MySqlDialect | BigQueryDialect | RedshiftSqlDialect) {
if let Some(next) = chars.peek() {
if !self.unescape {
// In no-escape mode, the given query has to be saved completely including backslashes.
Expand Down
33 changes: 25 additions & 8 deletions tests/sqlparser_redshift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use test_utils::*;

use sqlparser::ast::*;
use sqlparser::dialect::RedshiftSqlDialect;
use sqlparser::parser::ParserOptions;

#[test]
fn test_square_brackets_over_db_schema_table_name() {
Expand Down Expand Up @@ -202,10 +203,10 @@ fn parse_like() {

// Test with escape char
let sql = &format!(
"SELECT * FROM customers WHERE name {}LIKE '%a' ESCAPE '\\'",
r#"SELECT * FROM customers WHERE name {}LIKE '%a' ESCAPE '\\'"#,
if negated { "NOT " } else { "" }
);
let select = redshift().verified_only_select(sql);
let select = redshift().verified_only_select_with_canonical(sql, "");
assert_eq!(
Expr::Like {
expr: Box::new(Expr::Identifier(Ident::new("name").empty_span())),
Expand All @@ -220,10 +221,10 @@ fn parse_like() {
// This statement tests that LIKE and NOT LIKE have the same precedence.
// This was previously mishandled (#81).
let sql = &format!(
"SELECT * FROM customers WHERE name {}LIKE '%a' IS NULL",
r#"SELECT * FROM customers WHERE name {}LIKE '%a' IS NULL"#,
if negated { "NOT " } else { "" }
);
let select = redshift().verified_only_select(sql);
let select = redshift().verified_only_select_with_canonical(sql, "");
assert_eq!(
Expr::IsNull(Box::new(Expr::Like {
expr: Box::new(Expr::Identifier(Ident::new("name").empty_span())),
Expand Down Expand Up @@ -260,10 +261,10 @@ fn parse_similar_to() {

// Test with escape char
let sql = &format!(
"SELECT * FROM customers WHERE name {}SIMILAR TO '%a' ESCAPE '\\'",
r#"SELECT * FROM customers WHERE name {}SIMILAR TO '%a' ESCAPE '\\'"#,
if negated { "NOT " } else { "" }
);
let select = redshift().verified_only_select(sql);
let select = redshift().verified_only_select_with_canonical(sql, "");
assert_eq!(
Expr::SimilarTo {
expr: Box::new(Expr::Identifier(Ident::new("name").empty_span())),
Expand All @@ -277,10 +278,10 @@ fn parse_similar_to() {

// This statement tests that SIMILAR TO and NOT SIMILAR TO have the same precedence.
let sql = &format!(
"SELECT * FROM customers WHERE name {}SIMILAR TO '%a' ESCAPE '\\' IS NULL",
r#"SELECT * FROM customers WHERE name {}SIMILAR TO '%a' ESCAPE '\\' IS NULL"#,
if negated { "NOT " } else { "" }
);
let select = redshift().verified_only_select(sql);
let select = redshift().verified_only_select_with_canonical(sql, "");
assert_eq!(
Expr::IsNull(Box::new(Expr::SimilarTo {
expr: Box::new(Expr::Identifier(Ident::new("name").empty_span())),
Expand All @@ -303,6 +304,13 @@ fn redshift() -> TestedDialects {
}
}

fn redshift_unescaped() -> TestedDialects {
TestedDialects {
dialects: vec![Box::new(RedshiftSqlDialect {})],
options: Some(ParserOptions::new().with_unescape(false)),
}
}

#[test]
fn test_sharp() {
let sql = "SELECT #_of_values";
Expand Down Expand Up @@ -346,3 +354,12 @@ fn parse_listagg() {
fn parse_quoted_identifier() {
redshift().verified_only_select(r#"SELECT 'foo' AS "123_col""#);
}

#[test]
fn test_escape_string() {
redshift_unescaped().verified_stmt(r"SELECT 'I\'m fine'");
redshift_unescaped().verified_stmt(r"SELECT 'I\\\'m fine'");
redshift_unescaped().verified_stmt(r#"SELECT 'I''m fine'"#);
redshift_unescaped().verified_stmt(r#"SELECT 'I\\\'m fine'"#);
redshift_unescaped().verified_stmt(r#"SELECT '[\'\\[\\]]'"#);
}