Skip to content

Only support escape literals for Postgres, Redshift and generic dialect #1674

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 7 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 4 additions & 0 deletions src/dialect/generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,8 @@ impl Dialect for GenericDialect {
fn supports_user_host_grantee(&self) -> bool {
true
}

fn supports_string_escape_constant(&self) -> bool {
true
}
}
7 changes: 7 additions & 0 deletions src/dialect/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,13 @@ pub trait Dialect: Debug + Any {
fn supports_set_stmt_without_operator(&self) -> bool {
false
}

/// Returns true if this dialect supports the E'...' syntax for string literals
///
/// Postgres: <https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-STRINGS-ESCAPE>
fn supports_string_escape_constant(&self) -> bool {
false
}
}

/// This represents the operators for which precedence must be defined
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/postgresql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,10 @@ impl Dialect for PostgreSqlDialect {
fn supports_nested_comments(&self) -> bool {
true
}

fn supports_string_escape_constant(&self) -> bool {
true
}
}

pub fn parse_create(parser: &mut Parser) -> Option<Result<Statement, ParserError>> {
Expand Down
4 changes: 4 additions & 0 deletions src/dialect/redshift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,8 @@ impl Dialect for RedshiftSqlDialect {
fn supports_partiql(&self) -> bool {
true
}

fn supports_string_escape_constant(&self) -> bool {
true
}
}
18 changes: 17 additions & 1 deletion src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use core::fmt::Debug;

use crate::dialect::*;
use crate::parser::{Parser, ParserError};
use crate::tokenizer::Tokenizer;
use crate::tokenizer::{Token, Tokenizer};
use crate::{ast::*, parser::ParserOptions};

#[cfg(test)]
Expand Down Expand Up @@ -237,6 +237,22 @@ impl TestedDialects {
pub fn verified_expr(&self, sql: &str) -> Expr {
self.expr_parses_to(sql, sql)
}

/// Check that the tokenizer returns the expected tokens for the given SQL.
pub fn tokenizes_to(&self, sql: &str, expected: Vec<Token>) {
if self.dialects.len() == 0 {
panic!("No dialects to test");
}

self.dialects.iter().for_each(|dialect| {
let mut tokenizer = Tokenizer::new(&**dialect, sql);
if let Some(options) = &self.options {
tokenizer = tokenizer.with_unescape(options.unescape);
}
let tokens = tokenizer.tokenize().unwrap();
assert_eq!(expected, tokens, "Tokenized differently for {:?}", dialect);
});
}
}

/// Returns all available dialects.
Expand Down
63 changes: 62 additions & 1 deletion src/tokenizer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -982,7 +982,7 @@ impl<'a> Tokenizer<'a> {
}
}
// PostgreSQL accepts "escape" string constants, which are an extension to the SQL standard.
x @ 'e' | x @ 'E' => {
x @ 'e' | x @ 'E' if self.dialect.supports_string_escape_constant() => {
let starting_loc = chars.location();
chars.next(); // consume, to check the next char
match chars.peek() {
Expand Down Expand Up @@ -2155,6 +2155,7 @@ mod tests {
use crate::dialect::{
BigQueryDialect, ClickHouseDialect, HiveDialect, MsSqlDialect, MySqlDialect, SQLiteDialect,
};
use crate::test_utils::all_dialects_where;
use core::fmt::Debug;

#[test]
Expand Down Expand Up @@ -3543,4 +3544,64 @@ mod tests {
];
compare(expected, tokens);
}

#[test]
fn test_string_escape_constant_not_supported() {
all_dialects_where(|dialect| {
!dialect.supports_string_escape_constant()
&& !dialect.supports_string_literal_backslash_escape()
})
.tokenizes_to(
"select e'\\n'",
vec![
Token::make_keyword("select"),
Token::Whitespace(Whitespace::Space),
Token::make_word("e", None),
Token::SingleQuotedString("\\n".to_string()),
],
);

all_dialects_where(|dialect| {
!dialect.supports_string_escape_constant()
&& !dialect.supports_string_literal_backslash_escape()
})
.tokenizes_to(
"select E'\\n'",
vec![
Token::make_keyword("select"),
Token::Whitespace(Whitespace::Space),
Token::make_word("E", None),
Token::SingleQuotedString("\\n".to_string()),
],
);
}

#[test]
fn test_string_escape_constant_supported() {
all_dialects_where(|dialect| {
dialect.supports_string_escape_constant()
&& !dialect.supports_string_literal_backslash_escape()
})
.tokenizes_to(
"select e'\\''",
vec![
Token::make_keyword("select"),
Token::Whitespace(Whitespace::Space),
Token::EscapedStringLiteral("'".to_string()),
],
);

all_dialects_where(|dialect| {
dialect.supports_string_escape_constant()
&& !dialect.supports_string_literal_backslash_escape()
})
.tokenizes_to(
"select E'\\''",
vec![
Token::make_keyword("select"),
Token::Whitespace(Whitespace::Space),
Token::EscapedStringLiteral("'".to_string()),
],
);
}
}