Repository navigation
fix(spanner): reject numeric and mid-identifier dollar-quoted string tags - #15071
sakthivelmanii wants to merge 1 commit into
Conversation
…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.
There was a problem hiding this comment.
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)))) { |
There was a problem hiding this comment.
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"); | |||
There was a problem hiding this comment.
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")) |
There was a problem hiding this comment.
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.
Align PostgreSQL-dialect dollar-quoted string tag recognition in the Spanner client statement parser with PostgreSQL lexical rules:
$1$or$10$) so positional parameters adjacent to$are not misinterpreted as string literal delimiters.$characters immediately preceded by an unquoted identifier character (such ascol$tag$aorcol$$) so valid PostgreSQL identifiers containing$do not open false dollar-quoted string literals and mask subsequent query parameters, comments, orRETURNINGclauses.