Add support for string literal concatenation (#2003)
Some checks failed
license / Release Audit Tool (RAT) (push) Has been cancelled
Rust / codestyle (push) Has been cancelled
Rust / lint (push) Has been cancelled
Rust / benchmark-lint (push) Has been cancelled
Rust / compile (push) Has been cancelled
Rust / docs (push) Has been cancelled
Rust / compile-no-std (push) Has been cancelled
Rust / test (beta) (push) Has been cancelled
Rust / test (nightly) (push) Has been cancelled
Rust / test (stable) (push) Has been cancelled

Co-authored-by: Ifeanyi Ubah <ify1992@yahoo.com>
This commit is contained in:
etgarperets 2025-09-02 14:20:34 +03:00 committed by GitHub
parent eca65742ba
commit cff28334be
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 39 additions and 2 deletions

View file

@ -476,6 +476,12 @@ pub trait Dialect: Debug + Any {
false
}
/// Returns true if the dialect supports concatenating of string literal
/// Example: `SELECT 'Hello ' "world" => SELECT 'Hello world'`
fn supports_string_literal_concatenation(&self) -> bool {
false
}
/// Does the dialect support trailing commas in the projection list?
fn supports_projection_trailing_commas(&self) -> bool {
self.supports_trailing_commas()

View file

@ -71,6 +71,11 @@ impl Dialect for MySqlDialect {
true
}
/// see <https://dev.mysql.com/doc/refman/8.4/en/string-functions.html#function_concat>
fn supports_string_literal_concatenation(&self) -> bool {
true
}
fn ignores_wildcard_escapes(&self) -> bool {
true
}

View file

@ -9914,8 +9914,12 @@ impl<'a> Parser<'a> {
// bigdecimal feature is enabled, and is otherwise a no-op
// (i.e., it returns the input string).
Token::Number(n, l) => ok_value(Value::Number(Self::parse(n, span.start)?, l)),
Token::SingleQuotedString(ref s) => ok_value(Value::SingleQuotedString(s.to_string())),
Token::DoubleQuotedString(ref s) => ok_value(Value::DoubleQuotedString(s.to_string())),
Token::SingleQuotedString(ref s) => ok_value(Value::SingleQuotedString(
self.maybe_concat_string_literal(s.to_string()),
)),
Token::DoubleQuotedString(ref s) => ok_value(Value::DoubleQuotedString(
self.maybe_concat_string_literal(s.to_string()),
)),
Token::TripleSingleQuotedString(ref s) => {
ok_value(Value::TripleSingleQuotedString(s.to_string()))
}
@ -9985,6 +9989,18 @@ impl<'a> Parser<'a> {
}
}
fn maybe_concat_string_literal(&mut self, mut str: String) -> String {
if self.dialect.supports_string_literal_concatenation() {
while let Token::SingleQuotedString(ref s) | Token::DoubleQuotedString(ref s) =
self.peek_token_ref().token
{
str.push_str(s.clone().as_str());
self.advance_token();
}
}
str
}
/// Parse an unsigned numeric literal
pub fn parse_number_value(&mut self) -> Result<ValueWithSpan, ParserError> {
let value_wrapper = self.parse_value()?;

View file

@ -17150,3 +17150,13 @@ fn test_parse_semantic_view_table_factor() {
_ => panic!("Expected Query statement"),
}
}
#[test]
fn parse_adjacent_string_literal_concatenation() {
let sql = r#"SELECT 'M' "y" 'S' "q" 'l'"#;
let dialects = all_dialects_where(|d| d.supports_string_literal_concatenation());
dialects.one_statement_parses_to(sql, r"SELECT 'MySql'");
let sql = "SELECT * FROM t WHERE col = 'Hello' \n ' ' \t 'World!'";
dialects.one_statement_parses_to(sql, r"SELECT * FROM t WHERE col = 'Hello World!'");
}