Skip to content

Commit 529782f

Browse files
etgarperetsiffyio
authored andcommitted
Add support for string literal concatenation (apache#2003)
Co-authored-by: Ifeanyi Ubah <ify1992@yahoo.com>
1 parent 47961ec commit 529782f

4 files changed

Lines changed: 39 additions & 2 deletions

File tree

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+
/// Returns true if the dialect supports concatenating of string literal
480+
/// Example: `SELECT 'Hello ' "world" => SELECT 'Hello world'`
481+
fn supports_string_literal_concatenation(&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#function_concat>
75+
fn supports_string_literal_concatenation(&self) -> bool {
76+
true
77+
}
78+
7479
fn ignores_wildcard_escapes(&self) -> bool {
7580
true
7681
}

src/parser/mod.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9945,8 +9945,12 @@ impl<'a> Parser<'a> {
99459945
// bigdecimal feature is enabled, and is otherwise a no-op
99469946
// (i.e., it returns the input string).
99479947
Token::Number(n, l) => ok_value(Value::Number(Self::parse(n, span.start)?, l)),
9948-
Token::SingleQuotedString(ref s) => ok_value(Value::SingleQuotedString(s.to_string())),
9949-
Token::DoubleQuotedString(ref s) => ok_value(Value::DoubleQuotedString(s.to_string())),
9948+
Token::SingleQuotedString(ref s) => ok_value(Value::SingleQuotedString(
9949+
self.maybe_concat_string_literal(s.to_string()),
9950+
)),
9951+
Token::DoubleQuotedString(ref s) => ok_value(Value::DoubleQuotedString(
9952+
self.maybe_concat_string_literal(s.to_string()),
9953+
)),
99509954
Token::TripleSingleQuotedString(ref s) => {
99519955
ok_value(Value::TripleSingleQuotedString(s.to_string()))
99529956
}
@@ -10016,6 +10020,18 @@ impl<'a> Parser<'a> {
1001610020
}
1001710021
}
1001810022

10023+
fn maybe_concat_string_literal(&mut self, mut str: String) -> String {
10024+
if self.dialect.supports_string_literal_concatenation() {
10025+
while let Token::SingleQuotedString(ref s) | Token::DoubleQuotedString(ref s) =
10026+
self.peek_token_ref().token
10027+
{
10028+
str.push_str(s.clone().as_str());
10029+
self.advance_token();
10030+
}
10031+
}
10032+
str
10033+
}
10034+
1001910035
/// Parse an unsigned numeric literal
1002010036
pub fn parse_number_value(&mut self) -> Result<ValueWithSpan, ParserError> {
1002110037
let value_wrapper = self.parse_value()?;

tests/sqlparser_common.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17176,3 +17176,13 @@ fn test_parse_semantic_view_table_factor() {
1717617176
_ => panic!("Expected Query statement"),
1717717177
}
1717817178
}
17179+
17180+
#[test]
17181+
fn parse_adjacent_string_literal_concatenation() {
17182+
let sql = r#"SELECT 'M' "y" 'S' "q" 'l'"#;
17183+
let dialects = all_dialects_where(|d| d.supports_string_literal_concatenation());
17184+
dialects.one_statement_parses_to(sql, r"SELECT 'MySql'");
17185+
17186+
let sql = "SELECT * FROM t WHERE col = 'Hello' \n ' ' \t 'World!'";
17187+
dialects.one_statement_parses_to(sql, r"SELECT * FROM t WHERE col = 'Hello World!'");
17188+
}

0 commit comments

Comments
 (0)