From c30a356196bee885b8fcf1f60b69aaabe645f929 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Sun, 15 Nov 2026 09:17:07 +0000 Subject: [PATCH 1/4] experimental postgres query: acceptance test for trailing comment after ';' Records current behavior: a single statement followed by a comment after its terminating ';' is rejected as multiple statements. Co-authored-by: Isaac --- .../query/trailing-comment/out.test.toml | 1 + .../query/trailing-comment/output.txt | 32 +++++++++++++++++++ .../postgres/query/trailing-comment/script | 18 +++++++++++ .../postgres/query/trailing-comment/test.toml | 8 +++++ 4 files changed, 59 insertions(+) create mode 100644 acceptance/experimental/postgres/query/trailing-comment/out.test.toml create mode 100644 acceptance/experimental/postgres/query/trailing-comment/output.txt create mode 100644 acceptance/experimental/postgres/query/trailing-comment/script create mode 100644 acceptance/experimental/postgres/query/trailing-comment/test.toml diff --git a/acceptance/experimental/postgres/query/trailing-comment/out.test.toml b/acceptance/experimental/postgres/query/trailing-comment/out.test.toml new file mode 100644 index 00000000000..18b1a88417e --- /dev/null +++ b/acceptance/experimental/postgres/query/trailing-comment/out.test.toml @@ -0,0 +1 @@ +Cloud = false diff --git a/acceptance/experimental/postgres/query/trailing-comment/output.txt b/acceptance/experimental/postgres/query/trailing-comment/output.txt new file mode 100644 index 00000000000..76bbba6a07e --- /dev/null +++ b/acceptance/experimental/postgres/query/trailing-comment/output.txt @@ -0,0 +1,32 @@ + +=== Trailing line comment after ';' + +>>> musterr [CLI] experimental postgres query --target my-instance SELECT 1; -- done +Error: argv[1]: input contains multiple statements (a ';' separates two or more statements) +This command runs one statement per input. To run multiple statements: + - Pass each as a separate positional: query "SELECT 1" "SELECT 2" + - Pass each in its own --file: query --file q1.sql --file q2.sql + +=== Trailing block comment after ';' + +>>> musterr [CLI] experimental postgres query --target my-instance SELECT 1; /* done */ +Error: argv[1]: input contains multiple statements (a ';' separates two or more statements) +This command runs one statement per input. To run multiple statements: + - Pass each as a separate positional: query "SELECT 1" "SELECT 2" + - Pass each in its own --file: query --file q1.sql --file q2.sql + +=== File ending with a comment line after the statement + +>>> musterr [CLI] experimental postgres query --target my-instance --file q.sql +Error: --file q.sql: input contains multiple statements (a ';' separates two or more statements) +This command runs one statement per input. To run multiple statements: + - Pass each as a separate positional: query "SELECT 1" "SELECT 2" + - Pass each in its own --file: query --file q1.sql --file q2.sql + +=== Two statements are still rejected + +>>> musterr [CLI] experimental postgres query --target my-instance SELECT 1; -- one SELECT 1; SELECT 2 +Error: argv[1]: input contains multiple statements (a ';' separates two or more statements) +This command runs one statement per input. To run multiple statements: + - Pass each as a separate positional: query "SELECT 1" "SELECT 2" + - Pass each in its own --file: query --file q1.sql --file q2.sql diff --git a/acceptance/experimental/postgres/query/trailing-comment/script b/acceptance/experimental/postgres/query/trailing-comment/script new file mode 100644 index 00000000000..703f5b99feb --- /dev/null +++ b/acceptance/experimental/postgres/query/trailing-comment/script @@ -0,0 +1,18 @@ +# A single statement followed by a comment after its terminating ';' is one +# statement as far as Postgres is concerned, so the pre-flight check must let +# it through. The instance is STOPPED, so an accepted input fails at target +# resolution instead of the multi-statement check. + +title "Trailing line comment after ';'" +trace musterr $CLI experimental postgres query --target my-instance "SELECT 1; -- done" + +title "Trailing block comment after ';'" +trace musterr $CLI experimental postgres query --target my-instance "SELECT 1; /* done */" + +title "File ending with a comment line after the statement" +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 + +title "Two statements are still rejected" +trace musterr $CLI experimental postgres query --target my-instance "SELECT 1; -- one" "SELECT 1; SELECT 2" diff --git a/acceptance/experimental/postgres/query/trailing-comment/test.toml b/acceptance/experimental/postgres/query/trailing-comment/test.toml new file mode 100644 index 00000000000..0ddf42ff007 --- /dev/null +++ b/acceptance/experimental/postgres/query/trailing-comment/test.toml @@ -0,0 +1,8 @@ +[[Server]] +Pattern = "GET /api/2.0/database/instances/my-instance" +Response.Body = ''' +{ + "name": "my-instance", + "state": "STOPPED" +} +''' From b7ed2dffed3e8cff430cb9071dccef26715f9fc3 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Sun, 15 Nov 2026 09:21:28 +0000 Subject: [PATCH 2/4] experimental postgres query: allow comments after the terminating ';' 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 --- .../query/trailing-comment/output.txt | 17 +++-------- experimental/postgres/cmd/multistatement.go | 29 +++++++++++++++---- .../postgres/cmd/multistatement_test.go | 6 ++++ 3 files changed, 34 insertions(+), 18 deletions(-) diff --git a/acceptance/experimental/postgres/query/trailing-comment/output.txt b/acceptance/experimental/postgres/query/trailing-comment/output.txt index 76bbba6a07e..f14586a2906 100644 --- a/acceptance/experimental/postgres/query/trailing-comment/output.txt +++ b/acceptance/experimental/postgres/query/trailing-comment/output.txt @@ -2,31 +2,22 @@ === Trailing line comment after ';' >>> musterr [CLI] experimental postgres query --target my-instance SELECT 1; -- done -Error: argv[1]: input contains multiple statements (a ';' separates two or more statements) -This command runs one statement per input. To run multiple statements: - - Pass each as a separate positional: query "SELECT 1" "SELECT 2" - - Pass each in its own --file: query --file q1.sql --file q2.sql +Error: database instance "my-instance" is not ready for accepting connections (state: STOPPED) === Trailing block comment after ';' >>> musterr [CLI] experimental postgres query --target my-instance SELECT 1; /* done */ -Error: argv[1]: input contains multiple statements (a ';' separates two or more statements) -This command runs one statement per input. To run multiple statements: - - Pass each as a separate positional: query "SELECT 1" "SELECT 2" - - Pass each in its own --file: query --file q1.sql --file q2.sql +Error: database instance "my-instance" is not ready for accepting connections (state: STOPPED) === File ending with a comment line after the statement >>> musterr [CLI] experimental postgres query --target my-instance --file q.sql -Error: --file q.sql: input contains multiple statements (a ';' separates two or more statements) -This command runs one statement per input. To run multiple statements: - - Pass each as a separate positional: query "SELECT 1" "SELECT 2" - - Pass each in its own --file: query --file q1.sql --file q2.sql +Error: database instance "my-instance" is not ready for accepting connections (state: STOPPED) === Two statements are still rejected >>> musterr [CLI] experimental postgres query --target my-instance SELECT 1; -- one SELECT 1; SELECT 2 -Error: argv[1]: input contains multiple statements (a ';' separates two or more statements) +Error: argv[2]: input contains multiple statements (a ';' separates two or more statements) This command runs one statement per input. To run multiple statements: - Pass each as a separate positional: query "SELECT 1" "SELECT 2" - Pass each in its own --file: query --file q1.sql --file q2.sql diff --git a/experimental/postgres/cmd/multistatement.go b/experimental/postgres/cmd/multistatement.go index 12032d1608b..e50d927b3cc 100644 --- a/experimental/postgres/cmd/multistatement.go +++ b/experimental/postgres/cmd/multistatement.go @@ -12,7 +12,8 @@ import ( var errMultipleStatements = errors.New("input contains multiple statements (a ';' separates two or more statements)") // checkSingleStatement walks sql and returns errMultipleStatements if a -// statement-terminating ';' is found anywhere except trailing whitespace. +// statement-terminating ';' is followed by anything other than whitespace +// and comments. // // The scanner ignores ';' inside: // - single-quoted strings ('a;b', SQL standard doubled-quote escape) @@ -35,12 +36,13 @@ func checkSingleStatement(sql string) error { switch c { case ';': - // A ';' that's not at end-of-trimmed-input is a separator. - if i < end-1 { + // A ';' followed only by whitespace and comments terminates the + // sole statement; Postgres does not count the empty remainder + // as another statement. + if !onlyWhitespaceAndComments(s, i+1, end) { return errMultipleStatements } - // Trailing ';' is fine. - i++ + return nil case '\'': // Single-quoted string. SQL standard escape is '' (doubled). @@ -103,6 +105,23 @@ func scanQuoted(s string, start, end int, quote byte) int { return end } +func onlyWhitespaceAndComments(s string, start, end int) bool { + i := start + for i < end { + switch { + case s[i] == ' ' || s[i] == '\t' || s[i] == '\r' || s[i] == '\n': + i++ + case s[i] == '-' && i+1 < end && s[i+1] == '-': + i = scanLineComment(s, i, end) + case s[i] == '/' && i+1 < end && s[i+1] == '*': + i = scanBlockComment(s, i, end) + default: + return false + } + } + return true +} + func scanLineComment(s string, start, end int) int { i := start + 2 for i < end && s[i] != '\n' { diff --git a/experimental/postgres/cmd/multistatement_test.go b/experimental/postgres/cmd/multistatement_test.go index baeec020dd9..eb3cf0a75f2 100644 --- a/experimental/postgres/cmd/multistatement_test.go +++ b/experimental/postgres/cmd/multistatement_test.go @@ -35,6 +35,12 @@ func TestCheckSingleStatement(t *testing.T) { {name: "semi inside line comment then real semi", input: "SELECT 1 -- ; \n; SELECT 2", wantErr: true}, {name: "semi inside dollar then real semi", input: "SELECT $$a;b$$; SELECT 2", wantErr: true}, + {name: "trailing line comment after semi", input: "SELECT 1; -- done", wantErr: false}, + {name: "trailing block comment after semi", input: "SELECT 1; /* done */\n", wantErr: false}, + {name: "comment line after semi", input: "SELECT 1;\n-- end\n", wantErr: false}, + {name: "comment then second statement", input: "SELECT 1; -- x\nSELECT 2", wantErr: true}, + {name: "block comment then second statement", input: "SELECT 1; /* x */ SELECT 2", wantErr: true}, + {name: "leading whitespace", input: " ;", wantErr: false}, {name: "empty input", input: "", wantErr: false}, {name: "only whitespace", input: " \n\t ", wantErr: false}, From 71ddc47f02522c0c7754c3ac85344100c09ef3a7 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Mon, 16 Nov 2026 13:47:22 +0000 Subject: [PATCH 3/4] experimental postgres query: scan the remainder after ';' in the same 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 --- .../postgres/query/trailing-comment/q.sql | 2 + .../postgres/query/trailing-comment/script | 2 - experimental/postgres/cmd/multistatement.go | 48 ++++++++----------- .../postgres/cmd/multistatement_test.go | 6 +++ 4 files changed, 29 insertions(+), 29 deletions(-) create mode 100644 acceptance/experimental/postgres/query/trailing-comment/q.sql diff --git a/acceptance/experimental/postgres/query/trailing-comment/q.sql b/acceptance/experimental/postgres/query/trailing-comment/q.sql new file mode 100644 index 00000000000..ec0021d89c4 --- /dev/null +++ b/acceptance/experimental/postgres/query/trailing-comment/q.sql @@ -0,0 +1,2 @@ +SELECT 1; +-- end of file diff --git a/acceptance/experimental/postgres/query/trailing-comment/script b/acceptance/experimental/postgres/query/trailing-comment/script index 703f5b99feb..dbc871eb4f3 100644 --- a/acceptance/experimental/postgres/query/trailing-comment/script +++ b/acceptance/experimental/postgres/query/trailing-comment/script @@ -10,9 +10,7 @@ title "Trailing block comment after ';'" trace musterr $CLI experimental postgres query --target my-instance "SELECT 1; /* done */" title "File ending with a comment line after the statement" -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 title "Two statements are still rejected" trace musterr $CLI experimental postgres query --target my-instance "SELECT 1; -- one" "SELECT 1; SELECT 2" diff --git a/experimental/postgres/cmd/multistatement.go b/experimental/postgres/cmd/multistatement.go index e50d927b3cc..f470797588c 100644 --- a/experimental/postgres/cmd/multistatement.go +++ b/experimental/postgres/cmd/multistatement.go @@ -12,8 +12,8 @@ import ( var errMultipleStatements = errors.New("input contains multiple statements (a ';' separates two or more statements)") // checkSingleStatement walks sql and returns errMultipleStatements if a -// statement-terminating ';' is followed by anything other than whitespace -// and comments. +// statement-terminating ';' is followed by anything other than whitespace, +// comments and further ';'. Postgres drops the empty statements those form. // // The scanner ignores ';' inside: // - single-quoted strings ('a;b', SQL standard doubled-quote escape) @@ -27,22 +27,21 @@ var errMultipleStatements = errors.New("input contains multiple statements (a '; // a real Postgres tokenizer. func checkSingleStatement(sql string) error { s := sql - // Trim trailing whitespace once so a single trailing ';' is allowed. - end := len(strings.TrimRight(s, " \t\r\n")) + end := len(s) + terminated := false i := 0 for i < end { c := s[i] + if terminated && !isSQLSpace(c) && c != ';' && !startsComment(s, i, end) { + return errMultipleStatements + } + switch c { case ';': - // A ';' followed only by whitespace and comments terminates the - // sole statement; Postgres does not count the empty remainder - // as another statement. - if !onlyWhitespaceAndComments(s, i+1, end) { - return errMultipleStatements - } - return nil + terminated = true + i++ case '\'': // Single-quoted string. SQL standard escape is '' (doubled). @@ -105,26 +104,21 @@ func scanQuoted(s string, start, end int, quote byte) int { return end } -func onlyWhitespaceAndComments(s string, start, end int) bool { - i := start - for i < end { - switch { - case s[i] == ' ' || s[i] == '\t' || s[i] == '\r' || s[i] == '\n': - i++ - case s[i] == '-' && i+1 < end && s[i+1] == '-': - i = scanLineComment(s, i, end) - case s[i] == '/' && i+1 < end && s[i+1] == '*': - i = scanBlockComment(s, i, end) - default: - return false - } - } - return true +// isSQLSpace reports whether c is whitespace to the Postgres lexer. +func isSQLSpace(c byte) bool { + return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\f' || c == '\v' +} + +// startsComment reports whether a "--" or "/*" comment starts at s[i]. +func startsComment(s string, i, end int) bool { + return i+1 < end && ((s[i] == '-' && s[i+1] == '-') || (s[i] == '/' && s[i+1] == '*')) } +// scanLineComment advances past a "--" comment opened at s[start]. Like the +// Postgres lexer, it ends the comment at either '\n' or '\r'. func scanLineComment(s string, start, end int) int { i := start + 2 - for i < end && s[i] != '\n' { + for i < end && s[i] != '\n' && s[i] != '\r' { i++ } return i diff --git a/experimental/postgres/cmd/multistatement_test.go b/experimental/postgres/cmd/multistatement_test.go index eb3cf0a75f2..d56b127b0c4 100644 --- a/experimental/postgres/cmd/multistatement_test.go +++ b/experimental/postgres/cmd/multistatement_test.go @@ -40,6 +40,12 @@ func TestCheckSingleStatement(t *testing.T) { {name: "comment line after semi", input: "SELECT 1;\n-- end\n", wantErr: false}, {name: "comment then second statement", input: "SELECT 1; -- x\nSELECT 2", wantErr: true}, {name: "block comment then second statement", input: "SELECT 1; /* x */ SELECT 2", wantErr: true}, + {name: "CR ends line comment after semi", input: "SELECT 1; -- x\rSELECT 2", wantErr: true}, + {name: "CR ends line comment before semi", input: "SELECT 1 -- x\r; SELECT 2", wantErr: true}, + {name: "repeated trailing semicolons", input: "SELECT 1;;", wantErr: false}, + {name: "spaced trailing semicolons", input: "SELECT 1; ;", wantErr: false}, + {name: "semicolon after trailing comment", input: "SELECT 1; -- a\n;", wantErr: false}, + {name: "form feed after semi", input: "SELECT 1;\f", wantErr: false}, {name: "leading whitespace", input: " ;", wantErr: false}, {name: "empty input", input: "", wantErr: false}, From c279f15928666f835673e4719403f8270a9967e7 Mon Sep 17 00:00:00 2001 From: "denis.bilenko" Date: Mon, 16 Nov 2026 13:55:24 +0000 Subject: [PATCH 4/4] experimental postgres query: nest block comments like Postgres 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 --- experimental/postgres/cmd/multistatement.go | 23 +++++++++++++++---- .../postgres/cmd/multistatement_test.go | 4 ++++ 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/experimental/postgres/cmd/multistatement.go b/experimental/postgres/cmd/multistatement.go index f470797588c..4913f40762d 100644 --- a/experimental/postgres/cmd/multistatement.go +++ b/experimental/postgres/cmd/multistatement.go @@ -18,8 +18,8 @@ var errMultipleStatements = errors.New("input contains multiple statements (a '; // The scanner ignores ';' inside: // - single-quoted strings ('a;b', SQL standard doubled-quote escape) // - double-quoted identifiers ("col;name") -// - line comments (-- ... \n) -// - block comments (/* ... */, non-nesting) +// - line comments (-- ... up to \n or \r) +// - block comments (/* ... */, nesting as in Postgres) // - dollar-quoted bodies ($tag$ ... $tag$, optional tag) // // Over-rejection on weird syntactic edge cases is acceptable: users get a @@ -124,13 +124,26 @@ func scanLineComment(s string, start, end int) int { return i } +// scanBlockComment advances past a "/*" comment opened at s[start]. Postgres +// nests block comments, so the comment ends at the "*/" that closes the +// outermost "/*". Returns end if unterminated. func scanBlockComment(s string, start, end int) int { + depth := 1 i := start + 2 for i+1 < end { - if s[i] == '*' && s[i+1] == '/' { - return i + 2 + switch { + case s[i] == '/' && s[i+1] == '*': + depth++ + i += 2 + case s[i] == '*' && s[i+1] == '/': + depth-- + i += 2 + if depth == 0 { + return i + } + default: + i++ } - i++ } return end } diff --git a/experimental/postgres/cmd/multistatement_test.go b/experimental/postgres/cmd/multistatement_test.go index d56b127b0c4..8df5d8aca75 100644 --- a/experimental/postgres/cmd/multistatement_test.go +++ b/experimental/postgres/cmd/multistatement_test.go @@ -46,6 +46,10 @@ func TestCheckSingleStatement(t *testing.T) { {name: "spaced trailing semicolons", input: "SELECT 1; ;", wantErr: false}, {name: "semicolon after trailing comment", input: "SELECT 1; -- a\n;", wantErr: false}, {name: "form feed after semi", input: "SELECT 1;\f", wantErr: false}, + {name: "nested block comment after semi", input: "SELECT 1; /* /* */ */", wantErr: false}, + {name: "nested block comment hides line comment after semi", input: "SELECT 1; /* /* */--*/ SELECT 2", wantErr: true}, + {name: "nested block comment hides line comment before semi", input: "SELECT 1 /* /* */--*/; SELECT 2", wantErr: true}, + {name: "semicolon in nested block comment", input: "SELECT 1 /* a /* ; */ ; */", wantErr: false}, {name: "leading whitespace", input: " ;", wantErr: false}, {name: "empty input", input: "", wantErr: false},