Repository navigation
experimental postgres query: allow comments after the terminating ';' - #7678
Conversation
…er ';' Records current behavior: a single statement followed by a comment after its terminating ';' is rejected as multiple statements. Co-authored-by: Isaac <no-reply@databricks.com>
checkSingleStatement treated any ';' not at the very end of the input as a
statement separator, so a single statement followed by a trailing comment
("SELECT 1; -- done", or a .sql file ending in a comment line) was rejected
as multiple statements. Postgres parses the comment-only remainder as an
empty statement and accepts the input, so the pre-flight check now only
rejects a ';' followed by something other than whitespace and comments.
Co-authored-by: Isaac <no-reply@databricks.com>
|
Commit: Regression test reportSelected 1 changed tests (limit=20, 0 not selected) 1 tests: regression=1 unreleased=0 coverage=0 failing=0
TestAccept/experimental/postgres/query/trailing-comment: branch ✅ | main (bf8caad) ❌ | latest (v1.26.0) ❌main (bf8caad): latest (v1.26.0): same output as main (bf8caad) |
✅ Integration test reportCommit: c279f15
|
simonfaltum
left a comment
There was a problem hiding this comment.
🤖 Automated review from simonfaltum's review agent (pr-review skill) at b7ed2df. Each finding was verified against the code; simonfaltum may follow up.
No blocking issues found, approving. 4 non-blocking comment(s) and 2 question(s) below.
This PR lets experimental postgres query accept a single statement followed by -- or /* */ comments after its terminating ;, and adds unit cases plus the first acceptance test for the command. The change is correct: Postgres drops the empty trailing statement, SELECT 1; SELECT 2 is still rejected, and the error now names the right argv index. CI, the unit tests and lint are green. One non-blocking suggestion: end line comments at \r as well as \n, so CR-only inputs can't slip a second statement past the pre-flight check. There are three small nits and two optional questions about structure, a single scan pass and the acceptance test's indirect signal.
Questions
onlyWhitespaceAndCommentsis a second scan that repeats the main loop's--and/*handling, andSELECT 1;;,SELECT 1; ;andSELECT 1; -- x\n;are still rejected, although Postgres drops those empty statements just as it drops the comment-only remainder. Would you consider a single pass instead, setting aterminatedflag on;and rejecting only on real content after it? That keeps one copy of the comment logic and would also make theTrimRightunnecessary. Or is the narrower change intended? (non-blocking)- The acceptance test infers that the pre-flight check accepted an input from the unrelated
not ready for accepting connections (state: STOPPED)targeting error, so a change to that message or to the order of checks inrunQuerywould churn it. Apart from the--filecase, it covers the same inputs as the new unit cases. Is the indirect signal intended, or would you keep only the--filecase (or drop the acceptance test)? (non-blocking)
Comments outside the changed lines
- [nit]
experimental/postgres/cmd/multistatement.go:31:onlyWhitespaceAndCommentsalready skips trailing whitespace, so thestrings.TrimRight(s, " \t\r\n")is no longer what allows a trailing;, despite what its comment says.end := len(s)gives the same result for every input. Keeping the trim also puts the whitespace set in two places (the cutset string and the comparisons at line 112), which can drift apart. Uselen(s)and drop the comment.
|
|
||
| func scanLineComment(s string, start, end int) int { | ||
| i := start + 2 | ||
| for i < end && s[i] != '\n' { |
There was a problem hiding this comment.
[nice to have] scanLineComment stops only at \n, but the Postgres lexer ends -- comments at \r too (non_newline [^\n\r] in scan.l). Since onlyWhitespaceAndComments now reuses it, "SELECT 1; -- x\rSELECT 2" passes the pre-flight check, which rejected it before this PR. With several inputs (e.g. query "INSERT ..." --file cr_endings.sql), the earlier inputs now run, and may write, before the server rejects this one with "cannot insert multiple commands into a prepared statement". The main loop already had the same gap ("SELECT 1 -- x\r; SELECT 2"). Stopping the loop at s[i] != '\n' && s[i] != '\r' fixes both; a CR-only regression case in TestCheckSingleStatement would pin it.
There was a problem hiding this comment.
Fixed in 71ddc47: scanLineComment stops at \r as well as \n, which closes both gaps (SELECT 1; -- x\rSELECT 2 and SELECT 1 -- x\r; SELECT 2); both are unit cases now.
| printf 'SELECT 1;\n-- end of file\n' > q.sql | ||
| trace musterr $CLI experimental postgres query --target my-instance --file q.sql | ||
| rm q.sql |
There was a problem hiding this comment.
[nit] The script creates q.sql with static content via printf and removes it afterwards. .agents/rules/testing.md asks for static inputs to be committed to the test directory instead of created in script. Commit trailing-comment/q.sql with that content and drop the printf and rm lines; output.txt does not change, because neither line is traced.
There was a problem hiding this comment.
Done in 71ddc47: trailing-comment/q.sql is committed and the printf/rm lines are gone; output.txt is unchanged.
| return end | ||
| } | ||
|
|
||
| func onlyWhitespaceAndComments(s string, start, end int) bool { |
There was a problem hiding this comment.
[nit] .agents/rules/comments.md asks for Go functions to have a doc comment that starts with the function name and ends with a period; the new onlyWhitespaceAndComments has none. A line such as // onlyWhitespaceAndComments reports whether s[start:end] holds only whitespace and non-nesting comments; an unterminated /* counts as a comment. would cover it.
There was a problem hiding this comment.
Done in 71ddc47: the helpers (isSQLSpace, startsComment, scanLineComment) have doc comments; onlyWhitespaceAndComments is gone (see the single-pass change).
… pass Address review feedback: a single pass accepts any run of whitespace, comments and ';' after the terminating ';' (Postgres drops those empty statements), so 'SELECT 1;;' is accepted too. Line comments end at '\r' as well as '\n', like the Postgres lexer, so a CR can't hide a second statement. The test's input file is committed instead of written by the script. Co-authored-by: Isaac <no-reply@databricks.com>
|
Thanks @simonfaltum — addressed in 71ddc47:
|
The scanner ended a block comment at the first "*/", while Postgres nests them. In "SELECT 1; /* /* */--*/ SELECT 2" the "--" after the inner "*/" then hid the second statement from the pre-flight check. Track nesting depth, which also accepts "SELECT 1; /* /* */ */". Co-authored-by: Isaac <no-reply@databricks.com>
|
Follow-ups (out of scope for this PR), from an adversarial review — gaps in the pre-flight scanner that already exist on main (the server's extended protocol still rejects a second statement):
|
Changes
A
;followed only by whitespace and comments now ends the single statement inexperimental postgres query.Why
"SELECT 1; -- done","SELECT 1; /* done */"and any--fileending with a comment line were rejected as multiple statements before connecting. Postgres treats the comment-only remainder as an empty statement.Tests
New acceptance test
acceptance/experimental/postgres/query/trailing-commentand unit cases;"SELECT 1; SELECT 2"is still rejected, and the error now points at the right argv index.Found by analyzing test coverage gaps: the code path was not covered by any test.
This pull request and its description were written by Isaac.