Skip to content

Commit 059aab1

Browse files
committed
SGA-3801 Added support for concatenating string literals saperated by space in mySQL
1 parent 779dcf9 commit 059aab1

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

src/dialect/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,12 @@ pub trait Dialect: Debug + Any {
476476
false
477477
}
478478

479+
// Does the Dialect support concatenating of string literal
480+
// Example: SELECT 'Hello ' "world" => SELECT 'Hello world'
481+
fn supports_concat_quoted_identifiers(&self) -> bool {
482+
false
483+
}
484+
479485
/// Does the dialect support trailing commas in the projection list?
480486
fn supports_projection_trailing_commas(&self) -> bool {
481487
self.supports_trailing_commas()

src/dialect/mysql.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ impl Dialect for MySqlDialect {
7171
true
7272
}
7373

74+
// see <https://dev.mysql.com/doc/refman/8.4/en/string-functions.html#:~:text=mysql%3E%20SELECT%20%27My%27%20%27S%27%20%27QL%27%3B%0A%20%20%20%20%20%20%20%20%2D%3E%20%27MySQL%27>
75+
fn supports_concat_quoted_identifiers(&self) -> bool {
76+
true
77+
}
78+
7479
fn ignores_wildcard_escapes(&self) -> bool {
7580
true
7681
}

src/parser/mod.rs

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9808,8 +9808,18 @@ impl<'a> Parser<'a> {
98089808
// bigdecimal feature is enabled, and is otherwise a no-op
98099809
// (i.e., it returns the input string).
98109810
Token::Number(n, l) => ok_value(Value::Number(Self::parse(n, span.start)?, l)),
9811-
Token::SingleQuotedString(ref s) => ok_value(Value::SingleQuotedString(s.to_string())),
9812-
Token::DoubleQuotedString(ref s) => ok_value(Value::DoubleQuotedString(s.to_string())),
9811+
Token::SingleQuotedString(ref s) => {
9812+
if self.dialect.supports_concat_quoted_identifiers() {
9813+
return ok_value(Value::SingleQuotedString(self.combine_quoted(next_token)));
9814+
}
9815+
ok_value(Value::SingleQuotedString(s.to_string()))
9816+
}
9817+
Token::DoubleQuotedString(ref s) => {
9818+
if self.dialect.supports_concat_quoted_identifiers() {
9819+
return ok_value(Value::DoubleQuotedString(self.combine_quoted(next_token)));
9820+
}
9821+
ok_value(Value::DoubleQuotedString(s.to_string()))
9822+
}
98139823
Token::TripleSingleQuotedString(ref s) => {
98149824
ok_value(Value::TripleSingleQuotedString(s.to_string()))
98159825
}
@@ -9879,6 +9889,35 @@ impl<'a> Parser<'a> {
98799889
}
98809890
}
98819891

9892+
fn is_quoted_string(&self, token: &Token) -> bool {
9893+
matches!(
9894+
token,
9895+
Token::SingleQuotedString(_) | Token::DoubleQuotedString(_)
9896+
)
9897+
}
9898+
9899+
fn get_quoted_string(&self, token: &Token) -> String {
9900+
match token {
9901+
Token::SingleQuotedString(s) => s.clone(),
9902+
Token::DoubleQuotedString(s) => s.clone(),
9903+
_ => String::new(),
9904+
}
9905+
}
9906+
9907+
fn combine_quoted(&mut self, token: TokenWithSpan) -> String {
9908+
let mut combined_string = self.get_quoted_string(&token.token);
9909+
loop {
9910+
let next_token = self.next_token();
9911+
if !self.is_quoted_string(&next_token.token) {
9912+
self.prev_token();
9913+
break;
9914+
}
9915+
let s = self.get_quoted_string(&next_token.token);
9916+
combined_string.push_str(&s);
9917+
}
9918+
combined_string
9919+
}
9920+
98829921
/// Parse an unsigned numeric literal
98839922
pub fn parse_number_value(&mut self) -> Result<ValueWithSpan, ParserError> {
98849923
let value_wrapper = self.parse_value()?;

tests/sqlparser_mysql.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4247,3 +4247,12 @@ fn test_create_index_options() {
42474247
"CREATE INDEX idx_name ON t(c1, c2) USING BTREE LOCK = EXCLUSIVE ALGORITHM = DEFAULT",
42484248
);
42494249
}
4250+
4251+
#[test]
4252+
fn parse_adjacent_string_literal_concatenation() {
4253+
let sql = r#"SELECT 'M' "y" 'S' "q" 'l'"#;
4254+
mysql().one_statement_parses_to(sql, r"SELECT 'MySql'");
4255+
4256+
let sql = "SELECT * FROM t WHERE col = 'Hello' \n ' ' \t 'World!'";
4257+
mysql().one_statement_parses_to(sql, r"SELECT * FROM t WHERE col = 'Hello World!'");
4258+
}

0 commit comments

Comments
 (0)