Repository navigation
chore(spanner): validate positional parameter count in StatementFactory.withUnnamedParameters - #15072
sakthivelmanii wants to merge 1 commit into
Conversation
…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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
nit: this can also just use this.length instead of sql.length().
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
If we do choose to keep this change in this PR, then we should also fix the same in eatSingleQuotedString().
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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"));
}There was a problem hiding this comment.
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"));
}
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.?placeholder and comment scanning could miscount placeholders across PostgreSQLE'...'/e'...'escape strings, dollar-quote tag and identifier$boundaries, nested block comments with adjacent delimiters (/*/*/and*/*), unterminated block comments, and bare\rline endings.Solution
Dialect,sql, andvaluesarguments inStatement.StatementFactory, check that the parsed?placeholder count matchesvalues.lengthbefore converting parameter values (throwingSpannerExceptionwithErrorCode.INVALID_ARGUMENTon any mismatch, including when values are passed without?placeholders), and return an unmodifiable parameter map.\rline endings in comments and single-line literals.