-
Notifications
You must be signed in to change notification settings - Fork 35
/
QueryExpressions.lhs
389 lines (305 loc) · 11.9 KB
/
QueryExpressions.lhs
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
[[query-expressions]]
= Query expressions
We can now start on the 'select' parser. In the SQL standard, it
refers to these things as 'query expressions' to distinguish then from
'value expressions', so we will reuse this language here.
The subset of SQL we will support is this:
TODO: write lots of examples here
select queries only, no union, intersect or except. No common table
expressions.
we will support all the value expressions that the value expression
parser above supports
we will support select lists with optional aliases, with the 'as'
optional in the alias
we will support the * as in 'select * from t', and the variation
'select t.* from t', but not the alias version select * as (a,b,c)
from t.
we support two part dotted identifiers in value expressions, but no
other sort (such as 3-part dotted value expression identifiers, or
schema qualified table names or function names).
for the from clause, we will only support optional 'from table_name'.
supports where
we will support regular group by lists, but not the new group by
options in SQL2003 (group by (), grouping sets, cube, rollup).
we will support having
we support order by, with multiple columns, but not explicit asc or
desc , and no 'nulls first' or 'nulls last' syntax.
No support for offset and fetch first, or variations.
> {-# LANGUAGE TupleSections #-}
> module QueryExpressions where
>
> --import Text.Groom (groom)
> --import qualified Text.Parsec as P
> import Text.Parsec.String (Parser)
> import Text.Parsec (try,optionMaybe, optional, sepBy1,option)
> import Control.Applicative ((<$>),(*>),(<*>))
> import Control.Monad (void,guard)
> --import Debug.Trace
> --import Data.List (intercalate)
> import Data.Maybe ()
> import qualified Test.HUnit as H
> import FunctionsAndTypesForParsing
> import ValueExpressions (ValueExpr(..), valueExpr, identifier, symbol, keyword, comma)
Here is the datatype for query expressions to get started with. In
this tutorial, we will only support an optional single table in the
from clause, and this will be expanded in the next tutorial.
TODO: rearrange the from/where/groupby/having/orderby to a separate
datatype which is optional in a select, and the from part is mandatory
in this new type. This follows the standard and is more accurate
syntax.
> data QueryExpr
> = Select
> {qeSelectList :: [(ValueExpr,Maybe String)]
> ,qeFrom :: Maybe String
> ,qeWhere :: Maybe ValueExpr
> ,qeGroupBy :: [ValueExpr]
> ,qeHaving :: Maybe ValueExpr
> ,qeOrderBy :: [ValueExpr]
> } deriving (Eq,Show)
Here is a default value which can be used to easily construct query
expression values.
> makeSelect :: QueryExpr
> makeSelect = Select {qeSelectList = []
> ,qeFrom = Nothing
> ,qeWhere = Nothing
> ,qeGroupBy = []
> ,qeHaving = Nothing
> ,qeOrderBy = []}
== select lists
Let's start with something simple:
```
select [value expr]
```
TODO: shorten the names of these examples
> singleSelectItemTests :: [(String,QueryExpr)]
> singleSelectItemTests =
> [("select 1", makeSelect {qeSelectList = [(NumLit 1,Nothing)]})]
Here are a couple of wrappers for `symbol` and `keyword` which wrap
them with void.
> keyword_ :: String -> Parser ()
> keyword_ = void . keyword
> symbol_ :: String -> Parser ()
> symbol_ = void . symbol
> singleSelectItem :: Parser QueryExpr
> singleSelectItem = do
> keyword_ "select"
> e <- valueExpr []
> return $ makeSelect {qeSelectList = [(e,Nothing)]}
You can use the old test runner to check these:
```
*QueryExpressions> H.runTestTT $ H.TestList $ map (makeTest singleSelectItem) parseSingleSelectItemTestData
Cases: 1 Tried: 1 Errors: 0 Failures: 0
Counts {cases = 1, tried = 1, errors = 0, failures = 0}
```
Let's rewrite it in the Applicative and mostly point free style.
> singleSelectItemApplicative :: Parser QueryExpr
> singleSelectItemApplicative =
> (\sl -> makeSelect {qeSelectList = sl})
> <$> (keyword_ "select" *> (((:[]) . (,Nothing)) <$> valueExpr []))
That didn't go so well. Using a wrapper:
> singleSelectItemApplicative' :: Parser QueryExpr
> singleSelectItemApplicative' =
> ms <$> (keyword_ "select" *> valueExpr [])
> where
> ms e = makeSelect {qeSelectList = [(e,Nothing)]}
Now let's write something that supports multiple value expressions,
e.g.
```
select 1+2, 3+4;
```
> multipleSelectItemsTests :: [(String,QueryExpr)]
> multipleSelectItemsTests =
> [("select a"
> ,makeSelect {qeSelectList = [(Iden "a",Nothing)]})
> ,("select a,b"
> ,makeSelect {qeSelectList = [(Iden "a",Nothing)
> ,(Iden "b",Nothing)]})
> ,("select 1+2,3+4"
> ,makeSelect {qeSelectList =
> [(BinOp (NumLit 1) "+" (NumLit 2),Nothing)
> ,(BinOp (NumLit 3) "+" (NumLit 4),Nothing)]})
> ]
> selectMultipleItems :: Parser QueryExpr
> selectMultipleItems = do
> keyword_ "select"
> es <- commaSep1 (valueExpr [])
> return $ makeSelect {qeSelectList = map (,Nothing) es}
> commaSep1 :: Parser a -> Parser [a]
> commaSep1 = (`sepBy1` comma)
=== aliases
We can write names for the columns produced from a select list using
the keyword `as`, and we can miss out the `as`:
```sql
select a as a1, b as b1, f(c) as c1;
-- no as
select a a1, b b1;
```
> selectListTests :: [(String,QueryExpr)]
> selectListTests =
> [("select a as a1, b as b1"
> ,makeSelect {qeSelectList = [(Iden "a", Just "a1")
> ,(Iden "b", Just "b1")]})
> ,("select a a1, b b1"
> ,makeSelect {qeSelectList = [(Iden "a", Just "a1")
> ,(Iden "b", Just "b1")]})
> ] ++ multipleSelectItemsTests
> ++ singleSelectItemTests
Finally, here is the select list parser and the helper for select
items:
> selectItem0 :: Parser (ValueExpr, Maybe String)
> selectItem0 = (,) <$> valueExpr [] <*> optionMaybe (try alias)
> where alias = optional (keyword_ "as") *> identifier
> selectList0 :: Parser [(ValueExpr, Maybe String)]
> selectList0 = keyword_ "select" *> commaSep1 selectItem0
> queryExpr0 :: Parser QueryExpr
> queryExpr0 = mkSelect <$> selectList0
> where mkSelect sl = makeSelect {qeSelectList = sl}
== from clause
> from :: Parser String
> from = keyword_ "from" *> identifier
> fromTests :: [(String,QueryExpr)]
> fromTests =
> [("select a from t"
> ,makeSelect {qeSelectList = [(Iden "a",Nothing)]
> ,qeFrom = (Just "t")})]
> queryExpr1 :: Parser QueryExpr
> queryExpr1 = mkSelect
> <$> selectList0
> <*> optionMaybe from
> where mkSelect sl fr = makeSelect {qeSelectList = sl
> ,qeFrom = fr}
```
*QueryExpressions> H.runTestTT $ H.TestList $ map (makeTest queryExpr1) (selectListTests ++ fromTests)
### Failure in: 6:select a from t
(line 1, column 16):
unexpected end of input
expecting digit, letter, "_", "--" or "/*"
Cases: 7 Tried: 7 Errors: 0 Failures: 1
Counts {cases = 7, tried = 7, errors = 0, failures = 1}
```
This is a keyword issue again. We are parsing the `from` as if it was
a column alias and then getting stuck.
> blackListIdentifier :: [String] -> Parser String
> blackListIdentifier bl = do
> i <- identifier
> guard (i `notElem` bl)
> return i
> selectItem :: Parser (ValueExpr, Maybe String)
> selectItem = (,) <$> valueExpr [] <*> optionMaybe (try alias)
> where alias = optional (keyword_ "as") *> blackListIdentifier ["from"]
> selectList :: Parser [(ValueExpr, Maybe String)]
> selectList = keyword_ "select" *> commaSep1 selectItem
> queryExpr2 :: Parser QueryExpr
> queryExpr2 = mkSelect
> <$> selectList
> <*> optionMaybe from
> where mkSelect sl fr = makeSelect {qeSelectList = sl
> ,qeFrom = fr}
That did the job for now.
== where
The where, group by, having, and order by parsers are simple.
> whereTests :: [(String,QueryExpr)]
> whereTests =
> [("select a from t where a = 5"
> ,makeSelect {qeSelectList = [(Iden "a",Nothing)]
> ,qeFrom = Just "t"
> ,qeWhere = Just $ BinOp (Iden "a") "=" (NumLit 5)})
> ]
> whereClause :: Parser ValueExpr
> whereClause = keyword_ "where" *> valueExpr []
> queryExpr3 :: Parser QueryExpr
> queryExpr3 = mkSelect
> <$> selectList
> <*> optionMaybe from
> <*> optionMaybe whereClause
> where mkSelect sl fr wh =
> makeSelect {qeSelectList = sl
> ,qeFrom = fr
> ,qeWhere = wh}
== group by
> groupByTests :: [(String,QueryExpr)]
> groupByTests =
> [("select a,sum(b) from t group by a"
> ,makeSelect {qeSelectList = [(Iden "a",Nothing)
> ,(App "sum" [Iden "b"],Nothing)]
> ,qeFrom = Just "t"
> ,qeGroupBy = [Iden "a"]
> })
> ,("select a,b,sum(c) from t group by a,b"
> ,makeSelect {qeSelectList = [(Iden "a",Nothing)
> ,(Iden "b",Nothing)
> ,(App "sum" [Iden "c"],Nothing)]
> ,qeFrom = Just "t"
> ,qeGroupBy = [Iden "a",Iden "b"]
> })
> ]
> groupByClause :: Parser [ValueExpr]
> groupByClause = keyword_ "group" *> keyword_ "by"
> *> commaSep1 (valueExpr [])
> queryExpr4 :: Parser QueryExpr
> queryExpr4 = mkSelect
> <$> selectList
> <*> optionMaybe from
> <*> optionMaybe whereClause
> <*> option [] groupByClause
> where mkSelect sl fr wh gr =
> makeSelect {qeSelectList = sl
> ,qeFrom = fr
> ,qeWhere = wh
> ,qeGroupBy = gr}
== having
> havingTests :: [(String,QueryExpr)]
> havingTests =
> [("select a,sum(b) from t group by a having sum(b) > 5"
> ,makeSelect {qeSelectList = [(Iden "a",Nothing)
> ,(App "sum" [Iden "b"],Nothing)]
> ,qeFrom = (Just "t")
> ,qeGroupBy = [Iden "a"]
> ,qeHaving = Just $ BinOp (App "sum" [Iden "b"]) ">" (NumLit 5)
> })
> ]
> having :: Parser ValueExpr
> having = keyword_ "having" *> (valueExpr [])
> queryExpr5 :: Parser QueryExpr
> queryExpr5 = mkSelect
> <$> selectList
> <*> optionMaybe from
> <*> optionMaybe whereClause
> <*> option [] groupByClause
> <*> optionMaybe having
> where mkSelect sl fr wh gr hv =
> makeSelect {qeSelectList = sl
> ,qeFrom = fr
> ,qeWhere = wh
> ,qeGroupBy = gr
> ,qeHaving = hv}
Looking nice so far. Did you run the tests for each stage?
== order by
> orderByTests :: [(String,QueryExpr)]
> orderByTests =
> [("select a from t order by a"
> ,ms [Iden "a"])
> ,("select a from t order by a, b"
> ,ms [Iden "a", Iden "b"])
> ]
> where
> ms o = makeSelect {qeSelectList = [(Iden "a",Nothing)]
> ,qeFrom = (Just "t")
> ,qeOrderBy = o}
> orderBy :: Parser [ValueExpr]
> orderBy = keyword_ "order" *> keyword_ "by"
> *> commaSep1 (valueExpr [])
> queryExpr6 :: Parser QueryExpr
> queryExpr6 = Select
> <$> selectList
> <*> optionMaybe from
> <*> optionMaybe whereClause
> <*> option [] groupByClause
> <*> optionMaybe having
> <*> option [] orderBy
```
*QueryExpressions> H.runTestTT $ H.TestList $ map (makeTest queryExpr6) (selectListTests ++ fromTests ++ whereTests ++ groupByTests ++ havingTests ++ orderByTests)
Cases: 13 Tried: 13 Errors: 0 Failures: 0
Counts {cases = 13, tried = 13, errors = 0, failures = 0}
```
TODO: talk about putting the maybes/default values inside from, where, etc??