Repository navigation
fix(spanner): enforce read-only DML returning guard and align hint and dollar-quote lexing - #15022
sakthivelmanii wants to merge 3 commits into
Conversation
…d dollar-quote lexing - Enforce !isReadOnly() in SingleUseTransaction.executeQueryAsync and ConnectionImpl (checkDmlReturningAllowed) before routing DML with THEN RETURN / RETURNING or AnalyzeMode. - Update AbstractStatementParser.internalParse to classify hint-stripped statement.getSql(), delegate SimpleParser.eatSingleQuotedString to skipQuoted (handling escapes, newlines, triple quotes, and PostgreSQL hint comment delimiters), and advance pos += 2 on nested /* and */ in skipMultiLineComment. - Require isValidIdentifierFirstChar at tag start in parseDollarQuotedString and guard dollar-quote entry in skip when preceded by an identifier character.
There was a problem hiding this comment.
Code Review
This pull request refactors and improves SQL parsing and validation logic in the Spanner connection API. Key changes include consolidating and enforcing checks that prevent DML statements with returning clauses (and update statements in general) from executing in read-only mode or read-only transactions. It also enhances the SQL parser to correctly handle dialect-specific quote-escaping rules, triple-quoted strings, nested multi-line comments, and PostgreSQL dollar-quoted string boundaries. Comprehensive unit tests have been added to verify these parsing, escaping, and read-only transaction enforcement behaviors. There are no review comments, so I have no additional feedback to provide.
olavloite
left a comment
There was a problem hiding this comment.
This PR contains three different fixes and should be split into separate PRs:
- Read-only enforcement
- Hint parsing/stripping
- PostgreSQL dollar-quoted parsing
Problem
readonly=trueconnection in autocommit mode, DML withTHEN RETURNorRETURNINGviaexecute,executeAsync, orSingleUseTransaction.executeQueryAsyncbypassed the read-only guard.StatementHintParserandSimpleParserwhen strippingSTATEMENT_TAG/RPC_PRIORITYhints could misclassify DML asStatementType.QUERY.AbstractStatementParser.parseDollarQuotedStringaccepted numeric first characters ($1$) andAbstractStatementParser.skiptreated mid-identifier$(col$tag$a,col$$) as opening a dollar-quoted string.Solution
!isReadOnly()inSingleUseTransaction.executeQueryAsyncandConnectionImpl(checkDmlReturningAllowed) before routing DML withTHEN RETURN/RETURNINGorAnalyzeMode.AbstractStatementParser.internalParseto classify hint-strippedstatement.getSql(), delegateSimpleParser.eatSingleQuotedStringtoskipQuoted(handling escapes, newlines, triple quotes, and PostgreSQL hint comment delimiters), and advancepos += 2on nested/*and*/inskipMultiLineComment.isValidIdentifierFirstCharat tag start inparseDollarQuotedStringand guard dollar-quote entry inskipwhen preceded by an identifier character.Behavior Changes
@{statement_tag='x'} SET AUTOCOMMIT=true) are now classified after hint stripping.