Skip to content

chore(spanner): validate positional parameter count in StatementFactory.withUnnamedParameters - #15072

Open
sakthivelmanii wants to merge 1 commit into
mainfrom
validate-statement-factory-unnamed-parameters
Open

sakthivelmanii wants to merge 1 commit into
mainfrom
validate-statement-factory-unnamed-parameters

Conversation

@sakthivelmanii

Copy link
Copy Markdown
Contributor

Problem

  • Statement.StatementFactory.withUnnamedParameters(String, Object...) did not validate that the number of provided parameter values matched the number of positional ? placeholders in the SQL string, allowing extra values to be silently ignored or missing values to fail only after a server round-trip, and returned a mutable parameter map.
  • Positional ? placeholder and comment scanning could miscount placeholders across PostgreSQL E'...' / e'...' escape strings, dollar-quote tag and identifier $ boundaries, nested block comments with adjacent delimiters (/*/*/ and */*), unterminated block comments, and bare \r line endings.

Solution

  • Validate non-null Dialect, sql, and values arguments in Statement.StatementFactory, check that the parsed ? placeholder count matches values.length before converting parameter values (throwing SpannerException with ErrorCode.INVALID_ARGUMENT on any mismatch, including when values are passed without ? placeholders), and return an unmodifiable parameter map.
  • Align client-side SQL lexical scanning for GoogleSQL and PostgreSQL across PostgreSQL escape string constants, dollar-quote tag and identifier boundaries, nested and unterminated block comments, and bare \r line endings in comments and single-line literals.

…ry.withUnnamedParameters

## Problem
- `Statement.StatementFactory.withUnnamedParameters(String, Object...)` did not validate that the number of provided parameter values matched the number of positional `?` placeholders in the SQL string, allowing extra values to be silently ignored or missing values to fail only after a server round-trip, and returned a mutable parameter map.
- Positional `?` placeholder and comment scanning could miscount placeholders across PostgreSQL `E'...'` / `e'...'` escape strings, dollar-quote tag and identifier `$` boundaries, nested block comments with adjacent delimiters (`/*/*/` and `*/*`), unterminated block comments, and bare `\r` line endings.

## Solution
- Validate non-null `Dialect`, `sql`, and `values` arguments in `Statement.StatementFactory`, check that the parsed `?` placeholder count matches `values.length` before converting parameter values (throwing `SpannerException` with `ErrorCode.INVALID_ARGUMENT` on any mismatch, including when values are passed without `?` placeholders), and return an unmodifiable parameter map.
- Align client-side SQL lexical scanning for GoogleSQL and PostgreSQL across PostgreSQL escape string constants, dollar-quote tag and identifier boundaries, nested and unterminated block comments, and bare `\r` line endings in comments and single-line literals.
@sakthivelmanii
sakthivelmanii requested review from a team as code owners November 18, 2026 21:50
@sakthivelmanii sakthivelmanii self-assigned this Nov 18, 2026

@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 enhances SQL parsing and parameter validation in the Cloud Spanner Java client. It introduces strict validation in Statement.withUnnamedParameters to ensure the number of positional parameters matches the provided values, adds support for PostgreSQL escape string constants (E'...'), and improves the handling of carriage returns (\r), dollar-quoted string tags, and nested multi-line comments. Comprehensive unit tests have been added to verify these changes. No review comments were provided, so there is no additional feedback to address.

@olavloite olavloite 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.

The fix for E'..' strings is mostly good, but this introduces the same regression for dollar-quoted strings as #15071, and that needs to be removed before this can be merged.

Comment on lines 993 to 997

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 change here overlaps with the fix in #15071 , and has the same flaws. I think that it would be better to keep these two fixes strictly separated in separate pull requests, so remove it here.

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 can also just use this.length instead of sql.length().

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: The fix in this method + the other fixes for '\r' could (should) have been a separate pull request. This affects more than just the parameter counting that is the main topic of this pull request.

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.

If we do choose to keep this change in this PR, then we should also fix the same in eatSingleQuotedString().

Comment on lines 316 to 318

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: The tests in this file also mix Google Truth and JUnit assertions. We should preferably stick to one type within an entire project, or at least within a test file.

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 lookback here has the same limitation as the one for dollar-quoted strings. This however does not introduce a regression, as we did not support E'...' strings before this, so in that sense we could just accept this as a known limitation.

Otherwise, this test shows what is technically a valid SQL string, but now fails:

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

    // Currently fails because '$' precedes 'E' and isValidIdentifierChar('$') == true, so E'...'
    // is parsed as a standard single-quoted string and closes at \'.
    ParametersInfo escapeAfterDollarQuote =
        parser.convertPositionalParametersToNamedParameters('?', "SELECT $$a$$E'\\'?\\''");
    assertEquals(0, escapeAfterDollarQuote.numberOfParameters);
    assertEquals("SELECT $$a$$E'\\'?\\''", escapeAfterDollarQuote.sqlWithNamedParameters);

    assertEquals(
        "SELECT $$a$$E'\\' -- not a comment' \nFROM t",
        parser.removeCommentsAndTrim(
            "SELECT $$a$$E'\\' -- not a comment' -- real comment\nFROM t"));

    assertEquals(
        ImmutableSet.of("$2"),
        ((PostgreSQLStatementParser) parser).getQueryParameters("SELECT $$a$$E'\\' $1', $2"));
  }

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.

Can we add these tests for full coverage:

  @Test
  public void withUnnamedParametersUnclosedLiterals() {
    Statement.StatementFactory gsqlFactory =
        new Statement.StatementFactory(Dialect.GOOGLE_STANDARD_SQL);
    for (String invalidSql :
        new String[] {
          "SELECT 'unclosed",
          "SELECT \"unclosed",
          "SELECT `unclosed",
          "SELECT 'line1\rline2', ?",
          "SELECT ? WHERE x = 'unclosed"
        }) {
      SpannerException exception =
          assertThrows(
              SpannerException.class, () -> gsqlFactory.withUnnamedParameters(invalidSql, 1L));
      assertEquals(ErrorCode.INVALID_ARGUMENT, exception.getErrorCode());
    }

    Statement.StatementFactory pgFactory = new Statement.StatementFactory(Dialect.POSTGRESQL);
    for (String invalidSql :
        new String[] {
          "SELECT 'unclosed",
          "SELECT \"unclosed",
          "SELECT E'unclosed\\'",
          "SELECT e'unclosed\\'",
          "SELECT $$unclosed",
          "SELECT $tag$unclosed",
          "SELECT ? WHERE x = E'\\'"
        }) {
      SpannerException exception =
          assertThrows(
              SpannerException.class, () -> pgFactory.withUnnamedParameters(invalidSql, 1L));
      assertEquals(ErrorCode.INVALID_ARGUMENT, exception.getErrorCode());
    }
  }

  @Test
  public void withUnnamedParametersPostgreSqlEscapeStringAtStart() {
    Statement.StatementFactory pgFactory = new Statement.StatementFactory(Dialect.POSTGRESQL);

    Statement upper = pgFactory.withUnnamedParameters("E'\\'?\\'' || ?", "val");
    assertEquals("E'\\'?\\'' || $1", upper.getSql());
    assertEquals(1, upper.getParameters().size());
    assertEquals(Value.string("val"), upper.getParameters().get("p1"));

    Statement lower = pgFactory.withUnnamedParameters("e'\\'?\\'' || ?", "val");
    assertEquals("e'\\'?\\'' || $1", lower.getSql());
    assertEquals(1, lower.getParameters().size());
    assertEquals(Value.string("val"), lower.getParameters().get("p1"));
  }

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