-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquery_test.go
366 lines (332 loc) · 11.8 KB
/
query_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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package sqlparser
import (
"encoding/json"
"fmt"
"github.com/stretchr/testify/assert"
"github.com/viant/parsly"
"github.com/viant/sqlparser/expr"
"github.com/viant/sqlparser/node"
"github.com/viant/sqlparser/query"
"strings"
"testing"
)
func TestBinaryWalk(t *testing.T) {
query, _ := ParseQuery("SELECT * FROM t WHERE a = 1 AND (c IN(1,2))")
binary, ok := query.Qualify.X.(*expr.Binary)
if !assert.True(t, ok) {
return
}
var actualColumns = make([]string, 0)
binary.Walk(func(ident node.Node, values *expr.Values, operator, parentOperator string) error {
actualColumns = append(actualColumns, Stringify(ident))
return nil
})
assert.EqualValues(t, []string{"a", "c"}, actualColumns)
}
func TestParseSelect(t *testing.T) {
{
var testCases = []struct {
description string
SQL string
expect string
hasError bool
options []Option
}{
{
description: "except",
SQL: "SELECT main.* EXCEPT(Id), cast(main AS Record), cardinality(main, 'One') AS main FROM ta",
expect: "SELECT main.* EXCEPT Id, cast(main AS Record), cardinality(main, 'One') AS main FROM ta",
},
{
description: "except group",
SQL: "SELECT main.* EXCEPT (Id,Name), cast(main AS Record), cardinality(main, 'One') AS main FROM ta",
expect: "SELECT main.* EXCEPT (Id, Name), cast(main AS Record), cardinality(main, 'One') AS main FROM ta",
},
{
description: "criteria with expr",
SQL: "SELECT Name FROM BAR WHERE ${predicate}",
expect: "SELECT Name FROM BAR WHERE ${predicate}",
},
{
description: "quoted from expr",
SQL: "SELECT Name,Active FROM `/Records[Active = true]`",
expect: "SELECT Name, Active FROM `/Records[Active = true]`",
},
{
description: "quoted from expr",
SQL: "SELECT * FROM $abc",
expect: "SELECT * FROM $abc",
},
{
description: "with syntax",
SQL: `WITH p AS (SELECT * FROM product), v AS (SELECT * FROM vendor)
SELECT p.*, v.* FROM p JOIN v ON p.VENDOR_ID = v.ID`,
expect: `SELECT p.*, v.* FROM (SELECT * FROM product) p JOIN (SELECT * FROM vendor) v ON p.VENDOR_ID = v.ID`,
},
{
description: "group by",
SQL: `SELECT ID, SUM(amount) FROM product u GROUP BY 1`,
expect: `SELECT ID, SUM(amount) FROM product u GROUP BY 1`,
},
{
description: "group by",
SQL: `SELECT ID, SUM(amount) FROM product u GROUP BY 1`,
expect: `SELECT ID, SUM(amount) FROM product u GROUP BY 1`,
},
{
description: "group by, having",
SQL: `SELECT ID, SUM(amount) FROM product u GROUP BY 1 HAVING COUNT(DISTINCT zz) > 2`,
expect: `SELECT ID, SUM(amount) FROM product u GROUP BY 1 HAVING COUNT(DISTINCT zz) > 2`,
},
{
description: "union all",
SQL: `SELECT user.* FROM user1 u UNION ALL SELECT user.* FROM user2 u`,
expect: `SELECT user.* FROM user1 u UNION ALL SELECT user.* FROM user2 u`,
},
{
description: "expr with comments",
SQL: `SELECT user.* FROM (SELECT t.* FROM USER t ) user /* {"Self":{"Holder":"Team", "Child":"ID", "Parent":"MGR_ID" }} */ `,
expect: `SELECT user.* FROM (SELECT t.* FROM USER t ) user /* {"Self":{"Holder":"Team", "Child":"ID", "Parent":"MGR_ID" }} */`,
},
{
description: "bq table select",
SQL: "SELECT c1 /* comment */, c2 FROM `proj.dataset.table` t",
expect: "SELECT c1 /* comment */, c2 FROM `proj.dataset.table` t",
},
{
description: "start with comments",
SQL: "SELECT t.* /* some comments */ FROM tableX t",
expect: "SELECT t.* /* some comments */ FROM tableX t",
},
{
description: "bq table select",
SQL: "SELECT c1 AS a1 , c2 FROM `proj.dataset.table` t",
expect: "SELECT c1 AS a1, c2 FROM `proj.dataset.table` t",
},
{
description: "except select",
SQL: "SELECT c1 /* comment */, c2 FROM x t",
expect: "SELECT c1 /* comment */, c2 FROM x t",
},
{
description: "except select",
SQL: "SELECT * EXCEPT c1,c2 FROM x t",
expect: "SELECT * EXCEPT (c1, c2) FROM x t",
},
{
description: "except select",
SQL: "SELECT t1.* EXCEPT c1,c2, t2.* EXCEPT c3 FROM x t1 JOIN y AS t2 ON t1.ID=t2.ID",
expect: "SELECT t1.* EXCEPT (c1, c2), t2.* EXCEPT c3 FROM x t1 JOIN y t2 ON t1.ID = t2.ID",
},
{
description: "placeholder",
SQL: `SELECT ISBN, Name FROM Publication t WHERE (ISBN = ?) AND 1=1`,
expect: `SELECT ISBN, Name FROM Publication t WHERE (ISBN = ?) AND 1 = 1`,
},
{
description: "error - extra coma",
SQL: `SELECT TOUPPER(name) AS Name, FROM user u`,
hasError: true,
},
{
description: "fun call",
SQL: `SELECT TOUPPER(name) AS Name FROM user u`,
expect: `SELECT TOUPPER(name) AS Name FROM user u`,
},
{
description: "cast call",
SQL: `SELECT CAST(name AS TEXT) AS Name FROM user u`,
expect: `SELECT CAST(name AS TEXT) AS Name FROM user u`,
},
{
description: "comments",
SQL: `SELECT user.* FROM user u -- extra comments `,
expect: `SELECT user.* FROM user u`,
},
{
description: "expr with comments",
SQL: `SELECT user.* FROM (SELECT t.* FROM USER t ) user /* {"Self":{"Holder":"Team", "Child":"ID", "Parent":"MGR_ID" }} */ `,
expect: `SELECT user.* FROM (SELECT t.* FROM USER t ) user /* {"Self":{"Holder":"Team", "Child":"ID", "Parent":"MGR_ID" }} */`,
},
{
description: "bq table select",
SQL: "SELECT c1 /* comment */, c2 FROM `proj.dataset.table` t",
expect: "SELECT c1 /* comment */, c2 FROM `proj.dataset.table` t",
},
{
description: "start with comments",
SQL: "SELECT t.* /* some comments */ FROM tableX t",
expect: "SELECT t.* /* some comments */ FROM tableX t",
},
{
description: "bq table select",
SQL: "SELECT c1 AS a1 , c2 FROM `proj.dataset.table` t",
expect: "SELECT c1 AS a1, c2 FROM `proj.dataset.table` t",
},
{
description: "except select",
SQL: "SELECT c1 /* comment */, c2 FROM x t",
expect: "SELECT c1 /* comment */, c2 FROM x t",
},
{
description: "except select",
SQL: "SELECT * EXCEPT c1,c2 FROM x t",
expect: "SELECT * EXCEPT (c1, c2) FROM x t",
},
{
description: "except select",
SQL: "SELECT t1.* EXCEPT c1,c2, t2.* EXCEPT c3 FROM x t1 JOIN y AS t2 ON t1.ID=t2.ID",
expect: "SELECT t1.* EXCEPT (c1, c2), t2.* EXCEPT c3 FROM x t1 JOIN y t2 ON t1.ID = t2.ID",
},
{
description: "* select",
SQL: "SELECT * FROM x",
expect: "SELECT * FROM x",
},
{
description: "* select with $",
SQL: "SELECT * FROM x WHERE id= $id",
expect: "SELECT * FROM x WHERE id = $id",
},
{
description: "basic select",
SQL: "SELECT col1, t.col2, col3 AS col FROM x t",
expect: "SELECT col1, t.col2, col3 AS col FROM x t",
},
{
description: "JOIN select",
SQL: "SELECT t.* FROM x1 t join x2 z ON t.ID = z.ID",
expect: "SELECT t.* FROM x1 t join x2 z ON t.ID = z.ID",
},
{
description: "JOIN select",
SQL: "SELECT t.* FROM x1 t join x2 z ON t.ID = z.ID LEFT JOIN x3 y ON t.ID = x3.ID",
expect: "SELECT t.* FROM x1 t join x2 z ON t.ID = z.ID LEFT JOIN x3 y ON t.ID = x3.ID",
},
{
description: "select with WHERE",
SQL: "SELECT t.* FROM x t WHERE 1=1 AND (x=2)",
expect: "SELECT t.* FROM x t WHERE 1 = 1 AND (x=2)",
},
{
description: "func call select",
SQL: "SELECT COALESCE(t.PARENT_ID,0) AS PARENT, t.col2, col3 AS col FROM x t",
expect: "SELECT COALESCE(t.PARENT_ID,0) AS PARENT, t.col2, col3 AS col FROM x t",
},
{
description: "exists select",
SQL: "SELECT 1 FROM x t WHERE col IN (1,2,3)",
expect: "SELECT 1 FROM x t WHERE col IN (1,2,3)",
},
{
description: "unary operand select",
SQL: "SELECT NOT t.col FROM x t",
expect: "SELECT NOT t.col FROM x t",
},
{
description: "basic expr",
SQL: "SELECT col1 + col2 AS z, t.col2, col3 AS col FROM x t",
expect: "SELECT col1 + col2 AS z, t.col2, col3 AS col FROM x t",
},
{
description: "between criteria select",
SQL: "SELECT c1 FROM table t WHERE a BETWEEN 1 AND 2",
expect: "SELECT c1 FROM table t WHERE a BETWEEN 1 AND 2",
},
{
description: "between criteria select 2",
SQL: "SELECT c1 FROM table t WHERE a BETWEEN 1 AND 2 AND 1=1",
expect: "SELECT c1 FROM table t WHERE a BETWEEN 1 AND 2 AND 1 = 1",
},
{
description: "join comments",
SQL: "SELECT * FROM tab1 t1 JOIN tab2 t2 /* my comment */ ON t1.ID = t2.ID ",
expect: "SELECT * FROM tab1 t1 JOIN tab2 t2 /* my comment */ ON t1.ID = t2.ID",
},
{
description: "table comments",
SQL: "SELECT * FROM tab1 t1 /* my comment */ JOIN tab2 t2 ON t1.ID = t2.ID ",
expect: "SELECT * FROM tab1 t1 /* my comment */ JOIN tab2 t2 ON t1.ID = t2.ID",
},
{
description: "criteria with additional expr",
SQL: "SELECT Name FROM BAR WHERE 1 = 1 ${expr}",
expect: "SELECT Name FROM BAR WHERE 1 = 1 ${expr}",
},
{
description: "",
expect: `SELECT ID, Name, Price FROM dataset-name.abc.table-name v GROUP BY 1, 2`,
SQL: `
select ID,
Name,
Price
from dataset-name.abc.table-name v
group by 1, 2
`,
},
{
description: "",
SQL: `select agegroup_id, name from (select id agegroup_id, name from CI_AGEGROUP) v #if($Has.AgeIncl) where $criteria.In("v.agegroup_id", $AgeIncl) #end `,
expect: `SELECT agegroup_id, name FROM (select id agegroup_id, name from CI_AGEGROUP) v #if($Has.AgeIncl) where $criteria.In("v.agegroup_id", $AgeIncl) #end`,
options: []Option{
WithErrorHandler(func(err error, cur *parsly.Cursor, destNode interface{}) error {
fromNode, ok := destNode.(*query.From)
if !ok {
return err
}
input := string(cur.Input[cur.Pos:])
if strings.HasPrefix(input, "#if") {
index := strings.LastIndex(input, "#end")
if index == -1 {
return err
}
input = input[:index+4]
fromNode.Unparsed = input + " "
cur.Pos += index + 4
return nil
}
return err
}),
},
},
{
description: "",
SQL: `SELECT col1, col2 FROM table1 t, UNNEST(b) v`,
expect: `SELECT col1, col2 FROM table1 t , UNNEST(b) v`,
},
{
description: "",
SQL: `SELECT ID,NAME FROM AAA ORDER BY 2 DESC, 1 ASC`,
expect: `SELECT ID, NAME FROM AAA ORDER BY 2 DESC, 1 ASC`,
},
{
description: "",
SQL: `SELECT col1, col2 FROM table1/tt t JOIN xx/e v ON v.ID=t.ID`,
expect: `SELECT col1, col2 FROM table1/tt t JOIN xx/e v ON v.ID = t.ID`,
},
{
description: "",
SQL: `SELECT col1, col2 FROM table1[id=?].tt t JOIN xx/e v ON v.ID=t.ID`,
expect: `SELECT col1, col2 FROM table1[id=?].tt t JOIN xx/e v ON v.ID = t.ID`,
},
}
for _, testCase := range testCases {
//for _, testCase := range testCases {
query, err := ParseQuery(testCase.SQL, testCase.options...)
if testCase.hasError {
assert.NotNilf(t, err, testCase.description)
continue
}
if !assert.Nil(t, err) {
fmt.Println(err)
fmt.Printf("%v\n", testCase.SQL)
continue
}
actual := strings.TrimSpace(Stringify(query))
if !assert.EqualValues(t, testCase.expect, actual) {
data, _ := json.Marshal(query)
fmt.Printf("%s\n", data)
}
}
}
}