Skip to content

Commit d0176be

Browse files
authored
make sure to give MEMBER OF the correct precedence (#18237)
Signed-off-by: Andres Taylor <[email protected]>
1 parent 421aca0 commit d0176be

File tree

5 files changed

+7185
-7198
lines changed

5 files changed

+7185
-7198
lines changed

go/vt/sqlparser/parse_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ var (
4242
partialDDL bool
4343
ignoreNormalizerTest bool
4444
}{{
45+
input: "select a and b member of (c) from dual",
46+
}, {
4547
input: "select * from foo limit 5 + 5",
4648
}, {
4749
input: "create table x(location GEOMETRYCOLLECTION DEFAULT (POINT(7.0, 3.0)))",

go/vt/sqlparser/precedence.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,7 @@ func precedenceFor(in Expr) Precendence {
5656
return P13
5757
case *BetweenExpr:
5858
return P12
59-
case *ComparisonExpr:
60-
return P11
61-
case *IsExpr:
59+
case *ComparisonExpr, *IsExpr, *MemberOfExpr:
6260
return P11
6361
case *BinaryExpr:
6462
switch node.Operator {

go/vt/sqlparser/precedence_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ import (
2121
"testing"
2222
"time"
2323

24+
"github.com/stretchr/testify/assert"
25+
2426
"github.com/stretchr/testify/require"
2527
)
2628

@@ -161,6 +163,11 @@ func TestParens(t *testing.T) {
161163
{in: "(~ (1||0)) IS NULL", expected: "~(1 or 0) is null"},
162164
{in: "1 not like ('a' is null)", expected: "1 not like ('a' is null)"},
163165
{in: ":vtg1 not like (:vtg2 is null)", expected: ":vtg1 not like (:vtg2 is null)"},
166+
{in: "a and b member of (c)", expected: "a and b member of (c)"},
167+
{
168+
in: "foo is null and (bar = true or cast('1448364' as unsigned) member of (baz))",
169+
expected: "foo is null and (bar = true or cast('1448364' as unsigned) member of (baz))",
170+
},
164171
}
165172

166173
parser := NewTestParser()
@@ -196,3 +203,25 @@ func TestRandom(t *testing.T) {
196203
require.Equal(t, outputOfParseResult, inputQ)
197204
}
198205
}
206+
207+
func TestPrecedenceOfMemberOfWithAndWithoutParser(t *testing.T) {
208+
// This test was used to expose the difference in precedence between the parser and the ast formatter
209+
expression := "a and b member of (c)"
210+
211+
// hand coded ast with the expected precedence
212+
ast1 := &AndExpr{
213+
Left: NewColName("a"),
214+
Right: &MemberOfExpr{
215+
Value: NewColName("b"),
216+
JSONArr: NewColName("c"),
217+
},
218+
}
219+
220+
assert.Equal(t, expression, String(ast1))
221+
222+
// Now let's try it through the parser
223+
ast2, err := NewTestParser().ParseExpr(expression)
224+
require.NoError(t, err)
225+
226+
assert.Equal(t, expression, String(ast2))
227+
}

0 commit comments

Comments
 (0)