Repository navigation
fix: treat backslash as a literal character when scanning standard SQL strings - #252
Conversation
…L strings Interpolate skipped over a quoted string as if a backslash escaped the next character in every flavor. SQLite, CQL, Presto, Informix, SQL Server and Oracle don't have backslash escapes, and PostgreSQL has them only in E'...' strings. A literal ending with a backslash, like ESCAPE '\', kept the scanner inside a string, so the placeholders after it were left untouched and a placeholder inside a later literal was replaced instead. Backslash escapes are now honored only by MySQL, ClickHouse and Doris, and by PostgreSQL inside E'...' strings.
huandu
left a comment
There was a problem hiding this comment.
LGTM under the project’s best-effort interpolation principle.
This change makes the scanner follow each dialect’s default string-literal grammar instead of assuming MySQL-style backslash escapes everywhere. That matches normal user expectations: ESCAPE '\ and backslashes in plain strings work in SQLite/PostgreSQL/SQL Server/Oracle, while MySQL/ClickHouse/Doris keep their existing escape behavior and PostgreSQL E'...' strings keep theirs.
The documented edge cases are acceptable and do not block the merge: PostgreSQL with standard_conforming_strings=off, MySQL backtick identifiers under NO_BACKSLASH_ESCAPES, Oracle q'[...]' quoting, and the pre-existing CQL encoder behavior. These require non-default server settings or are pre-existing limitations, and the PR description records them clearly. I will add a short note to the public Flavor.Interpolate doc comment after merge so the best-effort scope is visible in the API docs too.
Thanks @mansur-akhmadi for the detailed analysis and the real-database verification.
|
Small correction to my review text: the SQL example is |
Fixes #251.
When
Interpolatescans a query, it now only treats a backslash inside a quote as an escape in dialects that actually have backslash escapes:mysqlLikeInterpolate), SQL Server, Oracle: a backslash is an ordinary character in strings and quoted identifiers. Quotes are still escaped by doubling them, and the existing toggle already handles that.E'...'/e'...'. A plain'...'(with the defaultstandard_conforming_strings = on) and a"..."identifier treat it as an ordinary character. TheEmust start its own token, soELSE'\'is still a plain string.So
LIKE ? ESCAPE '\' AND id = ?now gets both placeholders replaced, and a?inside a literal after'\'stays as it is.Existing
TestFlavorInterpolaterows that wrote'\'?'/"\"?"for these flavors relied on backslash escapes these dialects don't have, so those inputs weren't valid SQL there. I rewrote them with doubled quotes ('''?',"""?") and a'\'literal, so each row still checks that placeholders inside quotes are skipped. The MySQL and ClickHouse rows are unchanged.How it was verified
go test ./...passes, and so doesgo vet ./....TestInterpolateBackslashInStandardStrings: 10 cases fail on master and pass with this change. The 4 cases that still use backslash escapes (MySQL, ClickHouse, Doris, PostgreSQLE'') pass both before and after.t(id, name)holds(1,'a_1'),(2,'a_2'),(3,'ab'):SELECT id, name FROM t WHERE name LIKE ? ESCAPE '\' AND id = ?witha\_%,2: on master the output ends inid = ?and returns no rows. With this change it returns2|a_2. On master,SELECT '\', '?', ?gives a syntax error. With this change it returns\|?|x.postgres:16-alpine(standard_conforming_stringsison), same query with$1/$2: on master it fails withthere is no parameter $2. With this change it returns2|a_2.SELECT CASE WHEN FALSE THEN 'x' ELSE'\' END, E'\'$1', e'\\', "t"."name", $1gives the same output before and after (\|'$1|\|a_1|v).Not covered
standard_conforming_strings = offtreats a backslash in'...'as an escape. This change assumes the default (on, since 9.1). The valuesInterpolatewrites areE'...', so they're correct either way.SELECT `a\`, ?keeps the?), even though MySQL doesn't. I left it alone because the existing MySQL test row (`\`?`) asserts that behavior, and changing the default flavor seemed better as a separate decision.NO_BACKSLASH_ESCAPESand Oracleq'[...]'quoting aren't handled, same as before.\n,\\and so on, which Cassandra doesn't unescape. This PR only changes the scanner, not the encoder.