Skip to content

fix(spanner): reject numeric and mid-identifier dollar-quoted string tags - #15071

Open
sakthivelmanii wants to merge 1 commit into
mainfrom
fix-spanner-pg-dollar-quoted-tags
Open

sakthivelmanii wants to merge 1 commit into
mainfrom
fix-spanner-pg-dollar-quoted-tags

Conversation

@sakthivelmanii

Copy link
Copy Markdown
Contributor

Align PostgreSQL-dialect dollar-quoted string tag recognition in the Spanner client statement parser with PostgreSQL lexical rules:

  • Reject non-empty dollar-quote tags that begin with a digit (such as $1$ or $10$) so positional parameters adjacent to $ are not misinterpreted as string literal delimiters.
  • Ignore $ characters immediately preceded by an unquoted identifier character (such as col$tag$a or col$$) so valid PostgreSQL identifiers containing $ do not open false dollar-quoted string literals and mask subsequent query parameters, comments, or RETURNING clauses.

…tags

Align PostgreSQL-dialect dollar-quoted string tag recognition in the Spanner client statement parser with PostgreSQL lexical rules:

- Reject non-empty dollar-quote tags that begin with a digit (such as `$1$` or `$10$`) so positional parameters adjacent to `$` are not misinterpreted as string literal delimiters.
- Ignore `$` characters immediately preceded by an unquoted identifier character (such as `col$tag$a` or `col$$`) so valid PostgreSQL identifiers containing `$` do not open false dollar-quoted string literals and mask subsequent query parameters, comments, or `RETURNING` clauses.
@sakthivelmanii
sakthivelmanii requested review from a team as code owners November 18, 2026 21:44

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request refactors the PostgreSQL dollar-quoted string parsing logic in AbstractStatementParser to prevent positional parameters (such as $1) and mid-identifier dollar signs from being incorrectly parsed as dollar-quote tags. It introduces stricter validation using isValidIdentifierFirstChar and ensures a dollar-quoted literal cannot open immediately after an identifier character. Comprehensive unit tests have been added to StatementParserTest to validate these changes. No review comments were provided, so there is no additional feedback.

&& currentChar == DOLLAR
// In PostgreSQL, '$' is a valid identifier continuation character (e.g. col$tag$a, col$$)
// and cannot open a dollar-quoted literal immediately after an identifier character.
&& (currentIndex == 0 || !isValidIdentifierChar(sql.charAt(currentIndex - 1)))) {

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.

This 1-position lookback is introducing some regressions for dollar-quoted literals that follow directly after a numeric literal or directly after another dollar-quoted literal. These tests were missing in our test coverage, which would have caught this regression:

  @Test
  public void testPostgreSQLDollarQuotedRegressions() {
    assumeTrue(dialect == Dialect.POSTGRESQL);

    // Regression 1: Adjacent dollar-quoted literals (previous closing '$' makes
    // isValidIdentifierChar('$') == true).
    assertEquals(
        "SELECT $$a$$$$--comment\n$$",
        parser.removeCommentsAndTrim("SELECT $$a$$$$--comment\n$$"));
    // Currently throws SpannerException(INVALID_ARGUMENT: unclosed literal) on the single quote.
    assertEquals("SELECT $$a$$$$it's$$", parser.removeCommentsAndTrim("SELECT $$a$$$$it's$$"));

    ParametersInfo adjacentParams =
        parser.convertPositionalParametersToNamedParameters('?', "SELECT $$a$$$$?$$");
    assertEquals(0, adjacentParams.numberOfParameters);
    assertEquals("SELECT $$a$$$$?$$", adjacentParams.sqlWithNamedParameters);

    assertFalse(
        parser
            .parse(Statement.of("UPDATE t SET a = $$a$$$$ RETURNING $$"))
            .hasReturningClause());

    PostgreSQLStatementParser pgParser = (PostgreSQLStatementParser) parser;
    assertEquals(ImmutableSet.of(), pgParser.getQueryParameters("SELECT $$a$$$$1$$"));

    // Regression 2: Dollar-quoted literal immediately following a numeric literal or parameter
    // (preceding digit makes isValidIdentifierChar('1') == true).
    assertEquals(
        "SELECT 1$$--comment\n$$", parser.removeCommentsAndTrim("SELECT 1$$--comment\n$$"));
    // Currently throws SpannerException(INVALID_ARGUMENT: unclosed literal).
    assertEquals("SELECT 1$$it's$$", parser.removeCommentsAndTrim("SELECT 1$$it's$$"));
    assertEquals(ImmutableSet.of("$1"), pgParser.getQueryParameters("SELECT $1$$2$$"));
  }

@@ -292,6 +292,139 @@ public void testPostgresSQLDialectDollarQuoted() {
assertThat(
parser.removeCommentsAndTrim("SELECT FOO, $BAR -- This is a comment\nFROM SOME_TABLE"))
.isEqualTo("SELECT FOO, $BAR \nFROM SOME_TABLE");

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.

There are some additional (existing) shortcomings in the current dollar-quoted literal parsing. These tests show those cases:

  @Test
  public void testPostgreSQLDollarQuotedPreExistingGaps() {
    assumeTrue(dialect == Dialect.POSTGRESQL);
    PostgreSQLStatementParser pgParser = (PostgreSQLStatementParser) parser;

    // Dollar-quoted literal immediately after a positional parameter:
    // Failed on main as tag "1"; still fails now because '1' precedes '$'.
    assertEquals(
        "SELECT $1$tag$--comment\n$tag$",
        parser.removeCommentsAndTrim("SELECT $1$tag$--comment\n$tag$"));

    // Skip still steps char-by-char through unquoted identifiers, so getQueryParameters()
    // falsely extracts $1 from identifier col$1.
    assertEquals(ImmutableSet.of("$2"), pgParser.getQueryParameters("SELECT col$1, $2 FROM t"));

    // Skip still steps char-by-char through col$returning, and checkCharPrecedingReturning()
    // accepts '$', so col$returning falsely triggers hasReturningClause().
    assertFalse(
        parser.parse(Statement.of("UPDATE t SET a = col$returning *")).hasReturningClause());

    // checkReturningClauseInternal() lowercases the whole SQL string before calling skip(),
    // violating PostgreSQL case-sensitivity for dollar-quote tags ($Tag$ != $tag$).
    assertFalse(
        parser
            .parse(Statement.of("UPDATE t SET x = $Tag$ $tag$ RETURNING * $Tag$"))
            .hasReturningClause());
  }

assertEquals("SELECT col$tag$a WHERE x = $1", singleMidIdentifierParam.sqlWithNamedParameters);

// Mid-identifier $$ and chained/digit-preceded $ tags strip comments and detect RETURNING.
assertThat(parser.removeCommentsAndTrim("SELECT col$$ -- comment\nFROM t"))

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.

nit: this pull request uses a mixture of Google Truth assertThat(..) style assertions, and JUnit style assertEquals(..) assertions. It should stick to one style, and the preferred style is normally JUnit.

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.

2 participants