Skip to content

[ISSUE #8573] Preserve SQL string literals during statement preparation - #8577

Open
yangjj-iso wants to merge 1 commit into
apache:masterfrom
yangjj-iso:feat/8573-preserve-sql-literals
Open

yangjj-iso wants to merge 1 commit into
apache:masterfrom
yangjj-iso:feat/8573-preserve-sql-literals

Conversation

@yangjj-iso

Copy link
Copy Markdown
Contributor

Fixes #8573.

Problem and change

The PostgreSQL and openGauss prepare interceptors lowercase the full statement and replace all backticks. That changes data such as 'Example' and backticks embedded in string literals.

A shared normalizer retains literal contents and continues case and backtick normalization in the surrounding SQL. It handles doubled quotes, explicit escape strings and dollar-quoted strings, and recognizes quoted identifiers and comments when finding literal boundaries. Ordinary single-quoted strings use the default standard-conforming syntax. The helper uses Locale.ROOT at the normalization point also touched by #8564.

IssueSpec

IssueSpec-Mode: lightweight
IssueSpec-Reason: Restore literal values without changing the existing mapper contract. Submitted for maintainer classification as a focused production bug fix.
IssueSpec-Validation: Both dialect regression tests failed on the original interceptors. On Java 17, Maven Wrapper verify for Admin and its reactor dependencies passed with Checkstyle, RAT and 17 targeted PostgreSQL, openGauss and Oracle interceptor tests. The new-code quality check reported zero findings. The full clean install and live database suites were outside this run.

Validation command on Windows

.\mvnw.cmd -B -ntp -T 2 -pl shenyu-admin -am verify '-Dtest=PostgreSQLPrepareInterceptorTest,OpenGaussSQLPrepareInterceptorTest,OracleSQLPrepareInterceptorTest' '-Dsurefire.failIfNoSpecifiedTests=false' '-Dmaven.javadoc.skip=true'
  • I have read the contribution guidelines.
  • I added tests that cover the change.
  • The added production code follows the code quality rules in AGENTS.md.
  • Full local ./mvnw clean install -Dmaven.javadoc.skip=true passed.

@konlue konlue left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Review

The tokenizer targets the right boundary: code keeps the existing normalization (lowercase plus backtick-to-double-quote) and only literal regions are passed through. I traced the scan against the PostgreSQL lexical rules and it holds up: literals are recognized before a quote can open one, so -- and /* */ inside strings are data; E'…' and $tag$…$tag$ are only taken at a non-identifier boundary, so an identifier-like Code$TAG$ stays code; doubled quotes and backslashes inside escape strings are consumed correctly; nested block comments are tracked by depth; and the escape-string continuation (E only before the first quote, newline between segments) matches the documented behaviour.

One cross-PR point worth stating explicitly: #8564 is still open and changes these exact two production lines and the same two test classes. The normalizer's Locale.ROOT already subsumes #8564's production change for PostgreSQL and openGauss, so whichever lands second needs a rebase and we should not lose the tr-TR regression in the process. Adding a tr-TR case next to the literal cases here would make this PR self-sufficient on the locale side regardless of merge order.

I verified locally on 5ec4c3b: the three interceptor test classes pass (17 tests). With the two interceptor lines restored to the previous expression the five new literal cases fail (4 parameterized PostgreSQL + 1 openGauss) and the other 12 pass; checkstyle:check on shenyu-admin reports 0 violations. I also pushed nine adversarial statements through the normalizer — no-literal SQL, doubled quotes, backticks inside a literal, a quoted identifier containing a quote and --, dollar tags including a Code$TAG$ boundary, an escape string with \', an escape-string continuation, nested block comments, a line comment containing a quote, and a doubled-quote identifier — all normalized as expected.

Non-blocking: the tokenizer itself has no direct unit test; both interceptors exercise it through their prepare path and openGauss repeats one PostgreSQL case. A small normalizer test with the cases above would pin the lexer boundary independently of the interceptor wiring. Oracle is explicitly out of scope in #8573, so I am not treating it as a gap here.

Field field = boundSql.getClass().getDeclaredField("sql");
field.setAccessible(true);
field.set(boundSql, boundSql.getSql().replace("`", "\"").toLowerCase());
field.set(boundSql, PostgreSqlNormalizer.normalize(boundSql.getSql()));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Cross-PR coupling: #8564 is still open and replaces this same line with .toLowerCase(Locale.ROOT), and both PRs edit the same two test classes. The normalizer already uses Locale.ROOT, so it subsumes #8564's production change for PostgreSQL and openGauss — but if this lands first, please carry over #8564's tr-TR regression (or add a locale case here) so the locale guarantee survives; if #8564 lands first, this just needs a rebase. Not blocking, just worth coordinating with #8564's author.

This branch has not been deployed

No deployments
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] Preserve SQL string-literal case in PostgreSQL and openGauss preparation

2 participants