Skip to content

fix: PG parser fails on LIKE ? ESCAPE '\\' generated by JPA#6627

Open
daguimu wants to merge 1 commit intoalibaba:masterfrom
daguimu:fix/pg-like-escape-parsing-6413
Open

fix: PG parser fails on LIKE ? ESCAPE '\\' generated by JPA#6627
daguimu wants to merge 1 commit intoalibaba:masterfrom
daguimu:fix/pg-like-escape-parsing-6413

Conversation

@daguimu
Copy link
Copy Markdown

@daguimu daguimu commented Mar 25, 2026

Problem

PostgreSQL SQL parser fails when parsing LIKE ? ESCAPE '\' syntax generated by Spring Data JPA/Hibernate Containing queries. The WallFilter throws ParserException: ERROR because the PG lexer incorrectly treats \ as an escape character inside string literals.

Reproduction SQL:

SELECT id, name FROM t WHERE name LIKE ? ESCAPE '\' AND group_id = ?

Root cause: PGLexer.scanString() overrode the base Lexer.scanString() to treat \ as an escape character. When encountering '\', the lexer interprets \' as an escaped quote, so the string literal never terminates — it consumes the rest of the SQL and produces an ERROR token.

In standard PostgreSQL (standard_conforming_strings = on, default since PostgreSQL 9.1 / 2011), backslash is not an escape character in regular string literals. Only E-prefixed strings (E'\\') use backslash escaping.

Fix

  • Remove the scanString() override from PGLexer, so it uses the base Lexer.scanString() which treats \ as a regular character
  • Remove the now-unused LITERAL_CHARS import
  • Update one test resource file (postgresql/0.txt) that used deprecated \' quote escaping to use standard SQL '' escaping

Tests Added

9 new test cases in Issue6413.java:

  • test_like_escape_backslash — exact SQL from bug report (JPA-generated)
  • test_like_escape_backslash_simple — minimal reproduction
  • test_like_escape_non_backslash_still_works — other escape chars (#, !, ~)
  • test_string_with_backslash_in_pg — standard PG string containing \
  • test_string_with_double_backslash_in_pg — string containing \\
  • test_like_escape_backslash_with_wall_filter — verifies WallFilter path (original failure)
  • test_not_like_escape_backslash — NOT LIKE variant
  • test_ilike_with_backslash_string — PG-specific ILIKE
  • test_pg_double_single_quote_still_works — standard '' escape still works

All tests run against postgresql, greenplum, and edb dialects where applicable.

Impact

  • Fixes parsing of JPA/Hibernate generated SQL with ESCAPE '\'
  • Aligns PG string literal parsing with PostgreSQL's default standard_conforming_strings = on
  • No regression in existing PG test suite (39 tests pass)

Fixes #6413

Remove PGLexer.scanString() override that incorrectly treated backslash
as an escape character inside string literals. In standard PostgreSQL
(standard_conforming_strings = on, default since PG 9.1), backslash is
a regular character in string literals. The old behavior caused '\' to
be parsed as an escaped quote instead of a string containing backslash,
breaking LIKE ? ESCAPE '\' syntax generated by Spring Data JPA.

Also updates test resource file to use standard SQL '' quote escaping
instead of deprecated \' escaping.

Fixes alibaba#6413

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Mar 25, 2026

CLA assistant check
All committers have signed the CLA.

@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] 解析jpa生成的containing语法中包含的like ? escape '\' 报错。postgresql数据库

2 participants