diff --git a/pkg/sql/parser/parse.go b/pkg/sql/parser/parse.go index 092e94e9aa04..627368214f2d 100644 --- a/pkg/sql/parser/parse.go +++ b/pkg/sql/parser/parse.go @@ -167,7 +167,6 @@ func (p *Parser) scanOneStmt() (sql string, tokens []sqlSymType, done bool) { return p.scanner.In()[startPos:], tokens, true } preValID = lval.id - posBeforeScan := p.scanner.Pos() p.scanner.Scan(&lval) if preValID == BEGIN && lval.id == ATOMIC { @@ -177,7 +176,12 @@ func (p *Parser) scanOneStmt() (sql string, tokens []sqlSymType, done bool) { curFuncBodyCnt-- } if lval.id == 0 || (curFuncBodyCnt == 0 && lval.id == ';') { - return p.scanner.In()[startPos:posBeforeScan], tokens, (lval.id == 0) + endPos := p.scanner.Pos() + if lval.id == ';' { + // Don't include the ending semicolon, if there is one, in the raw SQL. + endPos-- + } + return p.scanner.In()[startPos:endPos], tokens, (lval.id == 0) } lval.pos -= startPos tokens = append(tokens, lval) diff --git a/pkg/sql/parser/parse_test.go b/pkg/sql/parser/parse_test.go index f9042c7dbeb3..b834aa2f6f23 100644 --- a/pkg/sql/parser/parse_test.go +++ b/pkg/sql/parser/parse_test.go @@ -639,10 +639,11 @@ func TestParseSQL(t *testing.T) { {in: ``, exp: nil}, {in: `SELECT 1`, exp: []string{`SELECT 1`}}, {in: `SELECT 1;`, exp: []string{`SELECT 1`}}, - {in: `SELECT 1 /* comment */`, exp: []string{`SELECT 1`}}, + {in: `/* comment */ SELECT 1`, exp: []string{`SELECT 1 /* comment */`}}, + {in: `SELECT 1 /* comment */`, exp: []string{`SELECT 1 /* comment */`}}, {in: `SELECT 1;SELECT 2`, exp: []string{`SELECT 1`, `SELECT 2`}}, - {in: `SELECT 1 /* comment */ ;SELECT 2`, exp: []string{`SELECT 1`, `SELECT 2`}}, - {in: `SELECT 1 /* comment */ ; /* comment */ SELECT 2`, exp: []string{`SELECT 1`, `SELECT 2`}}, + {in: `SELECT 1 /* comment */ ;SELECT 2`, exp: []string{`SELECT 1 /* comment */ `, `SELECT 2`}}, + {in: `SELECT 1 /* comment */ ; /* comment */ SELECT 2`, exp: []string{`SELECT 1 /* comment */ `, `/* comment */ SELECT 2`}}, } var p parser.Parser // Verify that the same parser can be reused. for _, d := range testData { @@ -656,7 +657,7 @@ func TestParseSQL(t *testing.T) { res = append(res, stmts[i].SQL) } if !reflect.DeepEqual(res, d.exp) { - t.Errorf("expected \n%v\n, but found %v", res, d.exp) + t.Errorf("expected \n%v\n, but found %v", d.exp, res) } }) }