forked from xwb1989/sqlparser
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprecedence_test.go
197 lines (181 loc) · 6.2 KB
/
precedence_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
Copyright 2019 The Vitess Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sqlparser
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
)
func readable(node Expr) string {
switch node := node.(type) {
case *OrExpr:
return fmt.Sprintf("(%s or %s)", readable(node.Left), readable(node.Right))
case *AndExpr:
return fmt.Sprintf("(%s and %s)", readable(node.Left), readable(node.Right))
case *XorExpr:
return fmt.Sprintf("(%s xor %s)", readable(node.Left), readable(node.Right))
case *BinaryExpr:
return fmt.Sprintf("(%s %s %s)", readable(node.Left), node.Operator.ToString(), readable(node.Right))
case *IsExpr:
return fmt.Sprintf("(%s %s)", readable(node.Left), node.Right.ToString())
default:
return String(node)
}
}
func TestAndOrPrecedence(t *testing.T) {
validSQL := []struct {
input string
output string
}{{
input: "select * from a where a=b and c=d or e=f",
output: "((a = b and c = d) or e = f)",
}, {
input: "select * from a where a=b or c=d and e=f",
output: "(a = b or (c = d and e = f))",
}}
parser := NewTestParser()
for _, tcase := range validSQL {
tree, err := parser.Parse(tcase.input)
if err != nil {
t.Error(err)
continue
}
expr := readable(tree.(*Select).Where.Expr)
if expr != tcase.output {
t.Errorf("Parse: \n%s, want: \n%s", expr, tcase.output)
}
}
}
func TestPlusStarPrecedence(t *testing.T) {
validSQL := []struct {
input string
output string
}{{
input: "select 1+2*3 from a",
output: "(1 + (2 * 3))",
}, {
input: "select 1*2+3 from a",
output: "((1 * 2) + 3)",
}}
parser := NewTestParser()
for _, tcase := range validSQL {
tree, err := parser.Parse(tcase.input)
if err != nil {
t.Error(err)
continue
}
expr := readable(tree.(*Select).SelectExprs[0].(*AliasedExpr).Expr)
if expr != tcase.output {
t.Errorf("Parse: \n%s, want: \n%s", expr, tcase.output)
}
}
}
func TestIsPrecedence(t *testing.T) {
validSQL := []struct {
input string
output string
}{{
input: "select * from a where a+b is true",
output: "((a + b) is true)",
}, {
input: "select * from a where a=1 and b=2 is true",
output: "(a = 1 and (b = 2 is true))",
}, {
input: "select * from a where (a=1 and b=2) is true",
output: "((a = 1 and b = 2) is true)",
}}
parser := NewTestParser()
for _, tcase := range validSQL {
tree, err := parser.Parse(tcase.input)
if err != nil {
t.Error(err)
continue
}
expr := readable(tree.(*Select).Where.Expr)
if expr != tcase.output {
t.Errorf("Parse: \n%s, want: \n%s", expr, tcase.output)
}
}
}
func TestParens(t *testing.T) {
tests := []struct {
in, expected string
}{
{in: "12", expected: "12"},
{in: "(12)", expected: "12"},
{in: "((12))", expected: "12"},
{in: "((true) and (false))", expected: "true and false"},
{in: "((true) and (false)) and (true)", expected: "true and false and true"},
{in: "((true) and (false))", expected: "true and false"},
{in: "a=b and (c=d or e=f)", expected: "a = b and (c = d or e = f)"},
{in: "(a=b and c=d) or e=f", expected: "a = b and c = d or e = f"},
{in: "a & (b | c)", expected: "a & (b | c)"},
{in: "(a & b) | c", expected: "a & b | c"},
{in: "not (a=b and c=d)", expected: "not (a = b and c = d)"},
{in: "not (a=b) and c=d", expected: "not a = b and c = d"},
{in: "(not (a=b)) and c=d", expected: "not a = b and c = d"},
{in: "-(12)", expected: "-12"},
{in: "-(12 + 12)", expected: "-(12 + 12)"},
{in: "(1 > 2) and (1 = b)", expected: "1 > 2 and 1 = b"},
{in: "(a / b) + c", expected: "a / b + c"},
{in: "a / (b + c)", expected: "a / (b + c)"},
{in: "(1,2,3)", expected: "(1, 2, 3)"},
{in: "(a) between (5) and (7)", expected: "a between 5 and 7"},
{in: "(a | b) between (5) and (7)", expected: "a | b between 5 and 7"},
{in: "(a and b) between (5) and (7)", expected: "(a and b) between 5 and 7"},
{in: "(true is true) is null", expected: "(true is true) is null"},
{in: "3 * (100 div 3)", expected: "3 * (100 div 3)"},
{in: "100 div 2 div 2", expected: "100 div 2 div 2"},
{in: "100 div (2 div 2)", expected: "100 div (2 div 2)"},
{in: "(100 div 2) div 2", expected: "100 div 2 div 2"},
{in: "((((((1000))))))", expected: "1000"},
{in: "100 - (50 + 10)", expected: "100 - (50 + 10)"},
{in: "100 - 50 + 10", expected: "100 - 50 + 10"},
{in: "true and (true and true)", expected: "true and (true and true)"},
{in: "10 - 2 - 1", expected: "10 - 2 - 1"},
{in: "(10 - 2) - 1", expected: "10 - 2 - 1"},
{in: "10 - (2 - 1)", expected: "10 - (2 - 1)"},
{in: "0 <=> (1 and 0)", expected: "0 <=> (1 and 0)"},
}
parser := NewTestParser()
for _, tc := range tests {
t.Run(tc.in, func(t *testing.T) {
stmt, err := parser.Parse("select " + tc.in)
require.NoError(t, err)
out := String(stmt)
require.Equal(t, "select "+tc.expected+" from dual", out)
})
}
}
func TestRandom(t *testing.T) {
// The purpose of this test is to find discrepancies between Format and parsing. If for example our precedence rules are not consistent between the two, this test should find it.
// The idea is to generate random queries, and pass them through the parser and then the unparser, and one more time. The result of the first unparse should be the same as the second result.
g := NewGenerator(5)
endBy := time.Now().Add(1 * time.Second)
parser := NewTestParser()
for {
if time.Now().After(endBy) {
break
}
// Given a random expression
randomExpr := g.Expression(ExprGeneratorConfig{})
inputQ := "select " + String(randomExpr) + " from t"
// When it's parsed and unparsed
parsedInput, err := parser.Parse(inputQ)
require.NoError(t, err, inputQ)
// Then the unparsing should be the same as the input query
outputOfParseResult := String(parsedInput)
require.Equal(t, outputOfParseResult, inputQ)
}
}