#11903·dolt

String literals drop the backslash in \% and \_, so LIKE '%\_%' matches every row (differs from MySQL)

Author: LaPingvinoCreated Sep 18, 2026Updated Sep 29, 2026
Labelsbugsqlgood reprocustomer issueparser

Summary

In MySQL, the string-literal escape rules make a special exception for \% and \_: the backslash is kept so that the pattern reaches LIKE as a literal-wildcard escape. Dolt drops the backslash while parsing the literal, so 'a\_b' becomes the three-character string a_b and the _ is a wildcard again. This silently changes results of very common queries such as WHERE name LIKE '%\_%' (find names containing an underscore), which in Dolt matches every row.

MySQL reference, "String Literals": "The \% and \_ sequences are used to search for literal instances of % and _ in pattern-matching contexts where they would otherwise be interpreted as wildcard characters. ... If you use \% or \_ outside of pattern-matching contexts, they evaluate to the strings \% and \_, not to % and _."

Related but different: #11558 is about backslash escaping being ignored inside window functions with doubled backslashes; this one is plain string-literal parsing in any context.

Reproduction (fresh dolt init, Dolt 2.3.5, dolt sql)

sql
select 'a_b' like 'a\_b' as under_lit,  'axb' like 'a\_b' as under_wild,
       'a%b' like 'a\%b' as pct_lit,    'axyb' like 'a\%b' as pct_wild;
-- MySQL 8: 1, 0, 1, 0
-- Dolt:    true, true, true, true

select length('a\_b'), 'a\_b';
-- MySQL 8: 4, a\_b
-- Dolt:    3, a_b

Doubling the backslash ('a\\_b') or using an explicit ESCAPE clause works in Dolt, so the LIKE implementation itself handles escapes; it is the literal that arrives without its backslash.

Impact

Anyone porting MySQL queries that search for literal underscores or percent signs gets wrong results with no error. I hit it while writing LIKE examples for a public dataset (lapingvino/kuwikt) and had to document ESCAPE '!' as the workaround.

Expected

'\%' and '\_' inside a string literal should be preserved as two characters, as in MySQL, in every context (not only pattern matching), so that LIKE with the default escape character sees them.