Skip to content

fix: treat backslash as a literal character when scanning standard SQL strings - #252

Merged
huandu merged 1 commit into
huandu:masterfrom
mansur-akhmadi:fix/interpolate-standard-backslash
Nov 25, 2026
Merged

huandu merged 1 commit into
huandu:masterfrom
mansur-akhmadi:fix/interpolate-standard-backslash

Conversation

@mansur-akhmadi

Copy link
Copy Markdown
Contributor

Fixes #251.

When Interpolate scans a query, it now only treats a backslash inside a quote as an escape in dialects that actually have backslash escapes:

  • MySQL, ClickHouse, Doris: unchanged.
  • SQLite, CQL, Presto, Informix (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.
  • PostgreSQL: a backslash escapes only inside E'...' / e'...'. A plain '...' (with the default standard_conforming_strings = on) and a "..." identifier treat it as an ordinary character. The E must start its own token, so ELSE'\' 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 TestFlavorInterpolate rows 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 does go vet ./....
  • New TestInterpolateBackslashInStandardStrings: 10 cases fail on master and pass with this change. The 4 cases that still use backslash escapes (MySQL, ClickHouse, Doris, PostgreSQL E'') pass both before and after.
  • Ran the interpolated queries against real databases. Table t(id, name) holds (1,'a_1'),(2,'a_2'),(3,'ab'):
    • sqlite3 3.51.0, SELECT id, name FROM t WHERE name LIKE ? ESCAPE '\' AND id = ? with a\_%, 2: on master the output ends in id = ? and returns no rows. With this change it returns 2|a_2. On master, SELECT '\', '?', ? gives a syntax error. With this change it returns \|?|x.
    • postgres:16-alpine (standard_conforming_strings is on), same query with $1/$2: on master it fails with there is no parameter $2. With this change it returns 2|a_2. SELECT CASE WHEN FALSE THEN 'x' ELSE'\' END, E'\'$1', e'\\', "t"."name", $1 gives the same output before and after (\|'$1|\|a_1|v).

Not covered

  • SQL Server, Oracle, Presto, Informix and CQL weren't run against a real server. Those changes follow each dialect's documented string-literal grammar.
  • PostgreSQL with standard_conforming_strings = off treats a backslash in '...' as an escape. This change assumes the default (on, since 9.1). The values Interpolate writes are E'...', so they're correct either way.
  • MySQL and Doris backtick identifiers still treat a backslash as an escape (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.
  • MySQL with NO_BACKSLASH_ESCAPES and Oracle q'[...]' quoting aren't handled, same as before.
  • The CQL value encoder still writes \n, \\ and so on, which Cassandra doesn't unescape. This PR only changes the scanner, not the encoder.

…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 huandu left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@huandu
huandu merged commit 25ae7a8 into huandu:master Nov 25, 2026
@huandu

huandu commented Nov 25, 2026

Copy link
Copy Markdown
Owner

Small correction to my review text: the SQL example is ESCAPE '\\' (a single backslash inside single quotes). The rest of the review stands.

@huandu huandu added the bug label Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Interpolate leaves placeholders after '\' in SQLite, PostgreSQL, SQL Server, Oracle and other standard-string flavors

2 participants