Skip to content

PHOENIX-8092 String literals in generated SQL do not escape backslashes - #2724

Merged
apurtell merged 2 commits into
apache:masterfrom
apurtell:PHOENIX-8092
Dec 5, 2026
Merged

apurtell merged 2 commits into
apache:masterfrom
apurtell:PHOENIX-8092

Conversation

@apurtell

@apurtell apurtell commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

CREATE INDEX ... WHERE fails at random with ERROR 602 when the table has a character array column. The probe upsert uses a random sample element. When that element is a backslash, the lexer reads the closing quote as escaped, so the literal does not close.

The root cause is general. The string constant escape doubles quotes but not backslashes, so generated SQL text does not always parse back to the same value. The escape now doubles both. Functional index expressions get one escape, not two, so index builds match index maintenance. String literal normalization returns the parsed value. SHOW TABLES and SHOW SCHEMAS escape their patterns. The probe uses a fixed element. Users now see doubled backslashes in the string form of character arrays, in EXPLAIN text and in new column defaults.

🤖 Generated with Claude Code

CREATE INDEX with a WHERE clause makes a probe UPSERT statement. For a
VARCHAR or CHAR ARRAY column, the probe value is a random sample
character. When the sample is a backslash, the literal is ARRAY['\'].
The SQL lexer reads \' as an escaped quote, so the literal does not
close, and the statement fails with ERROR 602.

The root cause is general. StringUtil.escapeStringConstant doubles
single quotes but not backslashes. The lexer reads a backslash as the
start of an escape sequence. Thus generated SQL text does not always
parse back to the same value. This affects view statements, index
WHERE clauses, column defaults and other generated text.

The change:
- escapeStringConstant doubles each backslash and each single quote.
  The parser reads the quoted result back as the original string.
- MetaDataClient no longer escapes backslashes in functional index
  expressions a second time. For string literals, the stored expression
  text does not change. Backslashes in quoted identifiers are no longer
  doubled; the old doubling made such expressions unresolvable.
- PostIndexDDLCompiler uses the stored functional index expression as it
  is. Before, it escaped backslashes again, so an index build by this
  path computed a different value than index maintenance.
- SchemaUtil.normalizeLiteral returns the parsed value of a string
  literal. SHOW TABLES/SCHEMAS LIKE patterns and GRANT/REVOKE names thus
  get the value that the statement gives. A statement can give any
  value, including one with doubled quotes, with escaped characters.
- SHOW TABLES and SHOW SCHEMAS escape the pattern in their SQL text.
- The CREATE INDEX probe uses a fixed element for VARCHAR and CHAR
  arrays, so the probe row is deterministic.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

A regression test is needed to verify consistent escaping when backfilling and maintaining a global functional index with a backslash-containing expression on a transactional table.

1 open issue
What changed in this PR

Fixes PHOENIX-8092 by preserving backslashes in generated Phoenix SQL, preventing intermittent index-creation failures.

Changes:

  • Escape backslashes and quotes consistently, without double-escaping functional index expressions.
  • Preserve parsed literal values and escape SHOW patterns.
  • Make character-array probes deterministic and add serialization regression tests.
File Description
phoenix-core/​src/​test/​java/​org/​apache/​phoenix/​util/​StringUtilTest.java Tests string-literal round trips.
phoenix-core/​src/​test/​java/​org/​apache/​phoenix/​schema/​SchemaUtilTest.java Tests parsed literal normalization.
phoenix-core/​src/​test/​java/​org/​apache/​phoenix/​parse/​QueryParserTest.java Tests SHOW statement round trips.
phoenix-core-client/​src/​main/​java/​org/​apache/​phoenix/​util/​StringUtil.java Escapes backslashes and quotes.
phoenix-core-client/​src/​main/​java/​org/​apache/​phoenix/​util/​SchemaUtil.java Returns parsed string values.
phoenix-core-client/​src/​main/​java/​org/​apache/​phoenix/​schema/​MetaDataClient.java Removes duplicate expression escaping.
phoenix-core-client/​src/​main/​java/​org/​apache/​phoenix/​parse/​ShowTablesStatement.java Escapes table patterns.
phoenix-core-client/​src/​main/​java/​org/​apache/​phoenix/​parse/​ShowSchemasStatement.java Escapes schema patterns.
phoenix-core-client/​src/​main/​java/​org/​apache/​phoenix/​compile/​PostIndexDDLCompiler.java Uses stored expressions without re-escaping.
phoenix-core-client/​src/​main/​java/​org/​apache/​phoenix/​compile/​CreateIndexCompiler.java Uses deterministic character-array probes.

Review effort: Balanced

A transactional table builds a new global index through its own compiler,
while later writes go through index maintenance. A new test commits rows,
creates a functional index whose expression holds a backslash string
literal, and then writes more rows. It checks that the rows from the
index build and from index maintenance get the same index values.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues are reported for the changes preserving backslashes in generated SQL literals.

1 open issue

Review effort: Balanced

@apurtell

apurtell commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

Precommit note: the failures in this run are not related to this change. Each one is a known problem on master.

(Claude Code)

@apurtell
apurtell merged commit 930612d into apache:master Dec 5, 2026
10 of 13 checks passed
@apurtell
apurtell deleted the PHOENIX-8092 branch December 5, 2026 19:40
asf-gitbox-commits pushed a commit that referenced this pull request Dec 5, 2026
…es (#2724)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Andrew Purtell <apurtell@salesforce.com>
asf-gitbox-commits pushed a commit that referenced this pull request Dec 5, 2026
…es (#2724)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: Andrew Purtell <apurtell@salesforce.com>
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.

2 participants