-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.Tests.fs
More file actions
471 lines (401 loc) · 14.2 KB
/
Parser.Tests.fs
File metadata and controls
471 lines (401 loc) · 14.2 KB
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
module OpenDiffix.Parser.ParserTests
open OpenDiffix.Core
open Xunit
open FsUnit.Xunit
open ParserTypes
open Parser
open Parser.QueryParser
let defaultSelect =
{
SelectDistinct = false
Expressions = []
From = Table("table", None)
Where = None
GroupBy = []
Having = None
Limit = None
OrderBy = []
}
let expectFail query =
try
let result = parse query
failwith $"Expected query to fail parsing. Got successfully parsed: %A{result}"
with _e ->
()
let expectFailWithParser parser fragment =
try
let result = FParsec.CharParsers.run parser fragment
failwith $"Expected query fragment parser to fail. Got successfully parsed: %A{result}"
with _e ->
()
let parseFragment parser fragment =
match FParsec.CharParsers.run parser fragment with
| FParsec.CharParsers.Success(result, _, _) -> result
| _ -> failwith "Expected a successfully parsed query fragment"
[<Fact>]
let ``Parses simple identifiers`` () =
parseFragment expr "hello" |> should equal (Identifier(None, "hello"))
parseFragment expr "hello12" |> should equal (Identifier(None, "hello12"))
parseFragment expr "hello_12" |> should equal (Identifier(None, "hello_12")) // Allows underscores
parseFragment expr "hello.bar12"
|> should equal (Identifier(Some "hello", "bar12")) // Allows qualified names
[<Fact>]
let ``Parses expressions`` () =
[
"1", Integer 1L
"-1", Integer -1L
"1.1", Float 1.1
"1.01", Float 1.01
"1.001", Float 1.001
"1.101", Float 1.101
"-1.101", Float -1.101
"1.123456789012345678", Float 1.123456789012345678
"1.123456789012345678e18", Float 1.123456789012345678e18
"1.0", Float 1.0
"'hello'", String "hello"
"true", Boolean true
"false", Boolean false
]
|> List.iter (fun (value, expected) -> parseFragment expr value |> should equal expected)
[ "+"; "-"; "*"; "/"; "%" ]
|> List.iter (fun op ->
parseFragment expr $"1 %s{op} 1"
|> should equal (Function(op, [ Integer 1L; Integer 1L ]))
)
[ "and", And; "or", Or; "<", Lt; "<=", LtE; ">", Gt; ">=", GtE; "=", Equals; "<>", Not << Equals ]
|> List.iter (fun (op, expected) ->
parseFragment expr $"1 %s{op} 1"
|> should equal (expected (Integer 1L, Integer 1L))
)
parseFragment expr "not 1" |> should equal (Not(Integer 1L))
parseFragment expr "value is null"
|> should equal (IsNull(Identifier(None, "value")))
parseFragment expr "value is not null"
|> should equal (Not(IsNull(Identifier(None, "value"))))
parseFragment expr $"'ab' || 'bc'"
|> should equal (Function("||", [ String "ab"; String "bc" ]))
[<Fact>]
let ``Parses columns`` () =
parseFragment commaSepExpressions "hello"
|> should equal [ As(Identifier(None, "hello"), None) ]
parseFragment commaSepExpressions "hello as alias"
|> should equal [ As(Identifier(None, "hello"), Some "alias") ]
parseFragment commaSepExpressions "hello, world"
|> should equal [ As(Identifier(None, "hello"), None); As(Identifier(None, "world"), None) ]
parseFragment commaSepExpressions "hello,world"
|> should equal [ As(Identifier(None, "hello"), None); As(Identifier(None, "world"), None) ]
parseFragment commaSepExpressions "hello ,world"
|> should equal [ As(Identifier(None, "hello"), None); As(Identifier(None, "world"), None) ]
[<Fact>]
let ``Parses functions`` () =
parseFragment expr "hello(world)"
|> should equal (Function("hello", [ Identifier(None, "world") ]))
parseFragment expr "hello ( world )"
|> should equal (Function("hello", [ Identifier(None, "world") ]))
parseFragment commaSepExpressions "hello(world), hello(moon)"
|> should
equal
[
As(Function("hello", [ Identifier(None, "world") ]), None)
As(Function("hello", [ Identifier(None, "moon") ]), None)
]
parseFragment expr "hello('world', 1, 2.5)"
|> should equal (Function("hello", [ String("world"); Integer(1L); Float(2.5) ]))
[<Fact>]
let ``Parses casts`` () =
parseFragment expr "cast(1 as boolean)"
|> should equal (Function("cast", [ Integer 1L; String "boolean" ]))
parseFragment expr "cast('0' as real)"
|> should equal (Function("cast", [ String "0"; String "real" ]))
[<Fact>]
let ``Precedence is as expected`` () =
parseFragment expr "1 + 2 * 3 % 2 < 1 AND a or not b IS NULL"
|> should
equal
(And(
Lt(
Function("+", [ Integer 1L; Function("*", [ Integer 2L; Function("%", [ Integer 3L; Integer 2L ]) ]) ]),
Integer 1L
),
Or(Identifier(None, "a"), Not(IsNull(Identifier(None, "b"))))
))
[<Fact>]
let ``Parses count(*)`` () =
let expected = Function("count", [ Star ])
parseFragment expr "count(*)" |> should equal expected
parseFragment expr "count( * )" |> should equal expected
[<Fact>]
let ``Parses count(distinct col)`` () =
let expected = Function("count", [ Distinct(Identifier(None, "col")) ])
parseFragment expr "count(distinct col)" |> should equal expected
parseFragment expr "count ( distinct col )" |> should equal expected
[<Fact>]
let ``Parses complex functions`` () =
parseFragment expr "length(sum(rain + sun))"
|> should
equal
(Function(
"length",
[ Function("sum", [ Function("+", [ Identifier(None, "rain"); Identifier(None, "sun") ]) ]) ]
))
[<Fact>]
let ``Parses WHERE clause conditions`` () =
parseFragment whereClause "WHERE a = 1"
|> should equal (Equals(Identifier(None, "a"), Integer 1L))
parseFragment whereClause "WHERE a = '1'"
|> should equal (Equals(Identifier(None, "a"), String "1"))
[<Fact>]
let ``Parses GROUP BY statement`` () =
parseFragment groupBy "GROUP BY a, b, c"
|> should equal [ Identifier(None, "a"); Identifier(None, "b"); Identifier(None, "c") ]
parseFragment groupBy "GROUP BY a" |> should equal [ Identifier(None, "a") ]
expectFailWithParser groupBy "GROUP BY"
[<Fact>]
let ``Parses ORDER BY statement`` () =
parseFragment orderBy "ORDER BY a, b, c"
|> should
equal
[
OrderSpec(Identifier(None, "a"), Asc, NullsLast)
OrderSpec(Identifier(None, "b"), Asc, NullsLast)
OrderSpec(Identifier(None, "c"), Asc, NullsLast)
]
parseFragment orderBy "ORDER BY a"
|> should equal [ OrderSpec(Identifier(None, "a"), Asc, NullsLast) ]
expectFailWithParser orderBy "ORDER BY"
expectFailWithParser orderBy "ORDER BY a DESZCZ"
// bad order
expectFailWithParser orderBy "ORDER BY a NULLS FIRST ASC"
[<Fact>]
let ``Parses SELECT by itself`` () =
parseFragment selectQuery "SELECT col FROM table"
|> should equal (SelectQuery { defaultSelect with Expressions = [ As(Identifier(None, "col"), None) ] })
[<Fact>]
let ``Parses SELECT DISTINCT`` () =
parseFragment selectQuery "SELECT DISTINCT col FROM table"
|> should
equal
(SelectQuery
{ defaultSelect with
SelectDistinct = true
Expressions = [ As(Identifier(None, "col"), None) ]
})
[<Fact>]
let ``Fails on unexpected input`` () = expectFail "Foo"
[<Fact>]
let ``Parse SELECT query with columns and table`` () =
parse "SELECT col1, col2 FROM table"
|> should
equal
{ defaultSelect with
Expressions = [ As(Identifier(None, "col1"), None); As(Identifier(None, "col2"), None) ]
}
parse "SELECT col1, col2 FROM table ;"
|> should
equal
{ defaultSelect with
Expressions = [ As(Identifier(None, "col1"), None); As(Identifier(None, "col2"), None) ]
}
[<Fact>]
let ``Multiline select`` () =
parse
"""
SELECT
col1
FROM
table
"""
|> should equal { defaultSelect with Expressions = [ As(Identifier(None, "col1"), None) ] }
[<Fact>]
let ``Parse aggregate query`` () =
parse
"""
SELECT col1, count(distinct aid)
FROM table
GROUP BY col1
"""
|> should
equal
{ defaultSelect with
Expressions =
[
As(Identifier(None, "col1"), None)
As(Function("count", [ Distinct(Identifier(None, "aid")) ]), None)
]
GroupBy = [ Identifier(None, "col1") ]
}
[<Fact>]
let ``Parse complex aggregate query`` () =
parse
"""
SELECT col1 as colAlias, count(distinct aid)
FROM table
WHERE col1 = 1 AND col2 = 2 or col2 = 3
GROUP BY col1
HAVING count(distinct aid) > 1
ORDER BY col2 DESC NULLS FIRST
LIMIT 42
"""
|> should
equal
{ defaultSelect with
Expressions =
[
As(Identifier(None, "col1"), Some "colAlias")
As(Function("count", [ Distinct(Identifier(None, "aid")) ]), None)
]
Where =
Some(
And(
Equals(Identifier(None, "col1"), Integer 1L),
(Or(Equals(Identifier(None, "col2"), Integer 2L), Equals(Identifier(None, "col2"), Integer 3L)))
)
)
GroupBy = [ Identifier(None, "col1") ]
Having = Some <| Gt(Function("count", [ Distinct(Identifier(None, "aid")) ]), Integer 1L)
OrderBy = [ OrderSpec(Identifier(None, "col2"), Desc, NullsFirst) ]
Limit = Some(42u)
}
[<Fact>]
let ``Parses simple JOINs`` () =
parseFragment from "from t1, t2"
|> should equal (Join(InnerJoin, Table("t1", None), Table("t2", None), Boolean true))
parseFragment from "from t1 inner join t2 on true"
|> should equal (Join(InnerJoin, Table("t1", None), Table("t2", None), Boolean true))
parseFragment from "from t1 left join t2 on true"
|> should equal (Join(LeftJoin, Table("t1", None), Table("t2", None), Boolean true))
parseFragment from "from t1 right join t2 on true"
|> should equal (Join(RightJoin, Table("t1", None), Table("t2", None), Boolean true))
parseFragment from "from t1 full join t2 on true"
|> should equal (Join(FullJoin, Table("t1", None), Table("t2", None), Boolean true))
parseFragment from "from t1 JOIN t2 ON t1.a = t2.b"
|> should
equal
(Join(
InnerJoin,
Table("t1", None),
Table("t2", None),
Equals(Identifier(Some "t1", "a"), Identifier(Some "t2", "b"))
))
[<Fact>]
let ``Parses multiple JOINs`` () =
parseFragment from "from t1, t2, t3"
|> should
equal
(Join(
InnerJoin,
Join(InnerJoin, Table("t1", None), Table("t2", None), Boolean true),
Table("t3", None),
Boolean true
))
parseFragment from "from t1 join t2 on true join t3 on true"
|> should
equal
(Join(
InnerJoin,
Join(InnerJoin, Table("t1", None), Table("t2", None), Boolean true),
Table("t3", None),
Boolean true
))
parseFragment from "from t1 left join t2 on a right join t3 on b"
|> should
equal
(Join(
RightJoin,
Join(LeftJoin, Table("t1", None), Table("t2", None), Identifier(None, "a")),
Table("t3", None),
Identifier(None, "b")
))
[<Fact>]
let ``Rejects invalid JOINs`` () =
expectFailWithParser from "from t1 join t2"
expectFailWithParser from "from t1 join t2 where a = b"
[<Fact>]
let ``Failed Paul attack query 1`` () =
parse
"""
select count(distinct aid1)
from tab where t1='y' and t2 = 'm'
"""
|> should
equal
{ defaultSelect with
Expressions = [ As(Function("count", [ Distinct(Identifier(None, "aid1")) ]), None) ]
From = Table("tab", None)
Where = Some(And(Equals(Identifier(None, "t1"), String "y"), Equals(Identifier(None, "t2"), String "m")))
}
[<Fact>]
let ``Failed Paul attack query 2`` () =
parse
"""
select count(distinct aid1)
from tab where t1 = 'y' and not (i1 = 100 and t2 = 'x')
"""
|> should
equal
{ defaultSelect with
Expressions = [ As(Function("count", [ Distinct(Identifier(None, "aid1")) ]), None) ]
From = Table("tab", None)
Where =
Some(
And(
Equals(Identifier(None, "t1"), String "y"),
Not(And(Equals(Identifier(None, "i1"), Integer 100L), Equals(Identifier(None, "t2"), String "x")))
)
)
}
[<Fact>]
let ``Parse sub-query`` () =
let query = { defaultSelect with Expressions = [ As(Identifier(None, "col"), None) ] }
parseFragment from "FROM (SELECT col FROM table) t"
|> should equal (SubQuery(query, "t"))
parseFragment from "FROM (SELECT col FROM table) t INNER JOIN (SELECT col FROM table) s ON t.col = s.col"
|> should
equal
(Join(
InnerJoin,
SubQuery(query, "t"),
SubQuery(query, "s"),
Equals(Identifier(Some "t", "col"), Identifier(Some "s", "col"))
))
[<Fact>]
let ``Parses star select`` () =
parseFragment selectQuery "SELECT * FROM table"
|> should equal (SelectQuery { defaultSelect with Expressions = [ Star ] })
[<Fact>]
let ``Parses limit`` () =
parseFragment selectQuery "SELECT * FROM table LIMIT 10"
|> should equal (SelectQuery { defaultSelect with Expressions = [ Star ]; Limit = Some(10u) })
[<Fact>]
let ``Parses quoted identifier 1`` () =
parseFragment selectQuery "SELECT \"*-\\(\" FROM table"
|> should equal (SelectQuery { defaultSelect with Expressions = [ As(Identifier(None, "*-\\("), None) ] })
[<Fact>]
let ``Parses quoted identifier 2`` () =
parseFragment selectQuery "SELECT \"(a)\".\"(b)\" FROM table"
|> should equal (SelectQuery { defaultSelect with Expressions = [ As(Identifier(Some "(a)", "(b)"), None) ] })
[<Fact>]
let ``Parses quoted identifier 3`` () =
parseFragment selectQuery "SELECT * FROM \"table\""
|> should equal (SelectQuery { defaultSelect with Expressions = [ Star ] })
[<Fact>]
let ``Parses quoted identifier 4`` () =
parseFragment selectQuery "SELECT a FROM table GROUP BY \"a\" LIMIT 1"
|> should
equal
(SelectQuery
{ defaultSelect with
Expressions = [ As(Identifier(None, "a"), None) ]
GroupBy = [ Identifier(None, "a") ]
Limit = Some 1u
})
[<Fact>]
let ``Parses quoted alias`` () =
parseFragment selectQuery "SELECT a AS \"a b\" FROM table AS \"a b\""
|> should
equal
(SelectQuery
{ defaultSelect with
Expressions = [ As(Identifier(None, "a"), Some "a b") ]
From = Table("table", Some "a b")
})