Skip to content

Commit d71df24

Browse files
authored
Trim spaces and empty statements to the right in planbuilder.Parse (#1823)
* Trim spaces and empty statements to the right in planbuilder.Parse * trim spaces and empty statements in sql/parse.parse
1 parent fb838ee commit d71df24

File tree

3 files changed

+20
-15
lines changed

3 files changed

+20
-15
lines changed

sql/parse/parse.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -84,9 +84,10 @@ func parse(ctx *sql.Context, query string, multi bool) (sql.Node, string, string
8484
defer span.End()
8585

8686
s := strings.TrimSpace(query)
87-
if strings.HasSuffix(s, ";") {
88-
s = s[:len(s)-1]
89-
}
87+
// trim spaces and empty statements
88+
s = strings.TrimRightFunc(s, func(r rune) bool {
89+
return r == ';' || unicode.IsSpace(r)
90+
})
9091

9192
var stmt sqlparser.Statement
9293
var err error
@@ -102,9 +103,10 @@ func parse(ctx *sql.Context, query string, multi bool) (sql.Node, string, string
102103
if ri != 0 && ri < len(s) {
103104
parsed = s[:ri]
104105
parsed = strings.TrimSpace(parsed)
105-
if strings.HasSuffix(parsed, ";") {
106-
parsed = parsed[:len(parsed)-1]
107-
}
106+
// trim spaces and empty statements
107+
parsed = strings.TrimRightFunc(parsed, func(r rune) bool {
108+
return r == ';' || unicode.IsSpace(r)
109+
})
108110
remainder = s[ri:]
109111
}
110112
}

sql/parse/parse_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -5252,19 +5252,19 @@ func TestParseOne(t *testing.T) {
52525252
},
52535253
{
52545254
"SELECT 1 /* testing */ ;",
5255-
[]string{"SELECT 1 /* testing */ "},
5255+
[]string{"SELECT 1 /* testing */"},
52565256
},
52575257
{
52585258
"SELECT 1 -- this is a test",
52595259
[]string{"SELECT 1 -- this is a test"},
52605260
},
52615261
{
52625262
"-- empty statement with comment\n; SELECT 1; SELECT 2",
5263-
[]string{"-- empty statement with comment\n", "SELECT 1", "SELECT 2"},
5263+
[]string{"-- empty statement with comment", "SELECT 1", "SELECT 2"},
52645264
},
52655265
{
52665266
"SELECT 1; -- empty statement with comment\n; SELECT 2",
5267-
[]string{"SELECT 1", "-- empty statement with comment\n", "SELECT 2"},
5267+
[]string{"SELECT 1", "-- empty statement with comment", "SELECT 2"},
52685268
},
52695269
{
52705270
"SELECT 1; SELECT 2; -- empty statement with comment\n",

sql/planbuilder/parse.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package planbuilder
33
import (
44
goerrors "errors"
55
"strings"
6+
"unicode"
67

78
"github.com/dolthub/vitess/go/vt/sqlparser"
89
"go.opentelemetry.io/otel/attribute"
@@ -37,9 +38,10 @@ func parse(ctx *sql.Context, cat sql.Catalog, query string, multi bool) (sql.Nod
3738
defer span.End()
3839

3940
s := strings.TrimSpace(query)
40-
if strings.HasSuffix(s, ";") {
41-
s = s[:len(s)-1]
42-
}
41+
// trim spaces and empty statements
42+
s = strings.TrimRightFunc(s, func(r rune) bool {
43+
return r == ';' || unicode.IsSpace(r)
44+
})
4345

4446
var stmt sqlparser.Statement
4547
var err error
@@ -55,9 +57,10 @@ func parse(ctx *sql.Context, cat sql.Catalog, query string, multi bool) (sql.Nod
5557
if ri != 0 && ri < len(s) {
5658
parsed = s[:ri]
5759
parsed = strings.TrimSpace(parsed)
58-
if strings.HasSuffix(parsed, ";") {
59-
parsed = parsed[:len(parsed)-1]
60-
}
60+
// trim spaces and empty statements
61+
parsed = strings.TrimRightFunc(parsed, func(r rune) bool {
62+
return r == ';' || unicode.IsSpace(r)
63+
})
6164
remainder = s[ri:]
6265
}
6366
}

0 commit comments

Comments
 (0)