-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.hs
1008 lines (825 loc) · 31.1 KB
/
Parser.hs
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
module Parser where
import System.Environment
import System.IO.Unsafe
-- some Megaparsec-specific modules
import Text.Megaparsec
import Text.Megaparsec.Expr
import qualified Text.Megaparsec.Char as C
import qualified Text.Megaparsec.Char.Lexer as L
--
import qualified Control.Exception as Exc
import Control.Monad (void,when)
import Debug.Trace
import Data.Bits
import Data.Char
import Data.Either
import Data.List
import qualified Data.Map as M
import qualified Data.Set as S
import Data.Void
-- import Expr directly from Banach.hs, 'qualified' because we want to reuse the names
import qualified Banach as B
import Aexpr
import ErrorMsg
import Expr
import Norms
import Preprocess
import Query
-- Define the parser type
-- 'Void' means 'no custom error messages'
-- 'String' means 'input comes in form of a String'
type Parser = Parsec Void String
-- a small bit, denoting whether we are parsing a query or a norm
-- we define it, since a query and a norm have very similar format
data ParserInstance = QueryParsing | NormParsing
---------------------------------------------------------------------------------------------
-- TODO: parsing of 'expr' and 'tableExpr' is being synchronized with B.Expr and B.TableExpr
-- keywords
allKeyWordList :: [String]
allKeyWordList = ["return",
"const","LN","exp","sqrt","root","scaleNorm","zeroSens","lp","linf","prod","inv","div","min","max","sigmoid","tauoid",
"selectMin","selectMax","selectProd","selectL"]
allKeyWords :: S.Set String -- set of reserved "words"
allKeyWords = S.fromList allKeyWordList
allCaseInsensKeyWords :: S.Set String -- set of reserved "words"
allCaseInsensKeyWords = S.fromList ["select","as","from","where","not","and","min","max","sum","product","count","distinct","group","by","between"]
-- a query expression
queryExpr :: VarName -> Parser [Expr]
queryExpr x = powerExpr
<|> powerLNExpr
<|> invExpr
<|> divExpr x
<|> expExpr
<|> scaleNormExpr
<|> zeroSensExpr
<|> lpNormExpr
<|> linfNormExpr
<|> prodExpr
<|> minExpr
<|> maxExpr
<|> sigmoidExpr
<|> tauoidExpr
<|> sumpExpr
<|> sumInfExpr
<|> constExpr
-- a norm expression
normExpr :: VarName -> Parser [Expr]
normExpr _ = constExpr
<|> scaleNormExpr
<|> scaleNorm2Expr
<|> zeroSensExpr
<|> lpNormExpr
<|> linfNormExpr
<|> lnExpr
-- parsing different expressions, one by one
powerExpr :: Parser [Expr]
powerExpr = do
symbol "^"
a <- varName
b <- float
return [Power a b]
powerLNExpr :: Parser [Expr]
powerLNExpr = do
keyWord "LN"
symbol "^"
a <- varName
b <- signedFloat
return [PowerLN a b]
invExpr :: Parser [Expr]
invExpr = do
keyWord "inv"
a <- varName
let b = -1.0
return [PowerLN a b]
divExpr :: VarName -> Parser [Expr]
divExpr x = do
keyWord "div"
a <- varName
b <- varName
let c = -1.0
return [Prod [a,x ++ "~1"], PowerLN b c]
expExpr :: Parser [Expr]
expExpr = do
keyWord "exp"
a <- signedFloat
b <- varName
return [Exp a b]
prodExpr :: Parser [Expr]
prodExpr = do
keyWord "prod"
bs <- many varName
return [Prod bs]
minExpr :: Parser [Expr]
minExpr = do
keyWord "min"
bs <- many varName
return [Min bs]
maxExpr :: Parser [Expr]
maxExpr = do
keyWord "max"
bs <- many varName
return [Max bs]
sigmoidExpr :: Parser [Expr]
sigmoidExpr = do
keyWord "sigmoid"
a <- float
c <- float
x <- varName
return [Sigmoid a c x]
tauoidExpr :: Parser [Expr]
tauoidExpr = do
keyWord "tauoid"
a <- float
c <- float
x <- varName
return [Tauoid a c x]
sumpExpr :: Parser [Expr]
sumpExpr = do
keyWord "sump"
c <- float
bs <- many varName
return [Sump c bs]
sumInfExpr :: Parser [Expr]
sumInfExpr = do
keyWord "sumInf"
bs <- many varName
return [SumInf bs]
constExpr :: Parser [Expr]
constExpr = do
keyWord "const"
b <- signedFloat
return [Const b]
scaleNormExpr :: Parser [Expr]
scaleNormExpr = do
keyWord "scaleNorm"
a <- float
b <- varName
return [ScaleNorm a b]
scaleNorm2Expr :: Parser [Expr]
scaleNorm2Expr = do
a <- float
symbol "*"
b <- varName
return [ScaleNorm a b]
zeroSensExpr :: Parser [Expr]
zeroSensExpr = do
keyWord "zeroSens"
a <- varName
return [ZeroSens a]
lpNormExpr :: Parser [Expr]
lpNormExpr = do
keyWord "lp"
a <- float
bs <- many varName
return [L a bs]
linfNormExpr :: Parser [Expr]
linfNormExpr = do
keyWord "linf"
bs <- many varName
return [LInf bs]
-- this one is intended only for norms
lnExpr :: Parser [Expr]
lnExpr = do
keyWord "LN"
a <- varName
return [PowerLN a 0.0]
-- a table expression
queryTableExpr :: Parser TableExpr
queryTableExpr = selectProdExpr
<|> selectMinExpr
<|> selectMaxExpr
<|> selectLExpr
<|> selectSumpExpr
<|> selectSumInfExpr
-- a table expression for norms (which norm is applied to the rows)
normTableExpr :: Parser TableExpr
normTableExpr =
linfTableExpr
<|> lpTableExpr
-- parsing different expressions, one by one
selectProdExpr :: Parser TableExpr
selectProdExpr = do
keyWord "selectProd"
a <- varName
return (SelectProd a)
selectMinExpr :: Parser TableExpr
selectMinExpr = do
keyWord "selectMin"
a <- varName
return (SelectMin a)
selectMaxExpr :: Parser TableExpr
selectMaxExpr = do
keyWord "selectMax"
a <- varName
return (SelectMax a)
selectLExpr :: Parser TableExpr
selectLExpr = do
keyWord "selectL"
c <- float
a <- varName
return (SelectL c a)
selectSumpExpr :: Parser TableExpr
selectSumpExpr = do
keyWord "selectSump"
c <- float
a <- varName
return (SelectSump c a)
selectSumInfExpr :: Parser TableExpr
selectSumInfExpr = do
keyWord "selectSumInf"
a <- varName
return (SelectSumInf a)
lpTableExpr :: Parser TableExpr
lpTableExpr = do
keyWord "lp"
a <- float
b <- varName
return (SelectL a b)
linfTableExpr :: Parser TableExpr
linfTableExpr = do
keyWord "linf"
b <- varName
return (SelectMax b)
------------------------------------------------
-- Parsing a function as a complex expression --
------------------------------------------------
-- arithmetic expressions
powerAExpr :: Parser (AExpr a -> AExpr a)
powerAExpr = do
keyWord "^"
a <- signedFloat
return (AUnary (APower a))
sqrtAExpr :: Parser (AExpr a -> AExpr a)
sqrtAExpr = do
keyWord "sqrt"
return (AUnary (APower 0.5))
rootAExpr :: Parser (AExpr a -> AExpr a)
rootAExpr = do
keyWord "root"
r <- float
return (AUnary (APower (1/r)))
lnAExpr :: Parser (AExpr a -> AExpr a)
lnAExpr = do
keyWord "ln"
return (AUnary ALn)
expAExpr :: Parser (AExpr a -> AExpr a)
expAExpr = do
keyWord "exp"
r <- signedFloat
return (AUnary (AExp r))
notAExpr :: Parser (AExpr a -> AExpr a)
notAExpr = do
caseInsensKeyWord "not"
return (AUnary (ANot))
betweenAExpr :: Parser (AExpr VarName -> AExpr VarName)
betweenAExpr = do
caseInsensKeyWord "between"
aexprLB <- aExpr
caseInsensKeyWord "and"
aexprUB <- aExpr
return (\e -> ABinary AAnd (ABinary AGT e aexprLB) (ABinary ALT e aexprUB))
absBeginAExpr :: Parser (AExpr a -> AExpr a)
absBeginAExpr = do
symbol "|"
return (AUnary AAbsBegin)
absEndAExpr :: Parser (AExpr a -> AExpr a)
absEndAExpr = do
symbol "|"
return (AUnary AAbsEnd)
aExpr :: Parser (AExpr VarName)
aExpr = makeExprParser aTerm aOperators
bExpr :: Parser (AExpr VarName)
bExpr = makeExprParser bTerm bOperators
namedAExpr :: Parser (VarName, VarName -> TableExpr, AExpr VarName)
namedAExpr = do
g <- sqlAggregator
aexpr <- aExpr
caseInsensKeyWord "as"
newColName <- varName
return (newColName, g, aexpr)
unnamedAExpr :: Parser (VarName -> TableExpr, AExpr VarName)
unnamedAExpr = do
g <- sqlAggregator
aexpr <- aExpr
return (g, aexpr)
aOperators :: [[Operator Parser (AExpr VarName)]]
aOperators =
[ [ Prefix absBeginAExpr
, Postfix absEndAExpr]
, [ Prefix lnAExpr
, Prefix expAExpr]
, [ Prefix sqrtAExpr
, Prefix rootAExpr
, Postfix powerAExpr]
, [ InfixL (ABinary AMax <$ symbol "\\/")
, InfixL (ABinary AMin <$ symbol "/\\") ]
, [ InfixL (ABinary AMult <$ symbol "*")
, InfixL (ABinary ADiv <$ symbol "/") ]
, [ InfixL (ABinary AAdd <$ symbol "+")
, InfixL (ABinary ASub <$ symbol "-") ]
]
aTerm :: Parser (AExpr VarName)
aTerm = parens aExpr
<|> AVar <$> varName
<|> AConst <$> signedFloat
<|> AConst <$> stringAsInt
<|> aDummy
aDummy = do
symbol "*"
return (AConst 0.0)
bOperators :: [[Operator Parser (AExpr VarName)]]
bOperators =
[
[ Prefix notAExpr
, Postfix betweenAExpr]
, [ InfixL (ABinary ALT <$ symbol "<=")
, InfixL (ABinary ALT <$ symbol "<")
, InfixL (ABinary AEQ <$ symbol "==")
, InfixL (ABinary AEQ <$ symbol "=")
, InfixL (ABinary AGT <$ symbol ">=")
, InfixL (ABinary AGT <$ symbol ">") ]
, [ InfixL (ABinary AAnd <$ caseInsensKeyWord "and")
, InfixL (ABinary AOr <$ caseInsensKeyWord "or") ]
]
bTerm :: Parser (AExpr VarName)
bTerm = try aExpr <|> parens bExpr
------------------------------------------------------------
---- Parsing SQL query (simlpified, could be delegated) ----
------------------------------------------------------------
order :: Parser Ordering
order = ordLE <|> ordLT <|> ordEQ <|> ordGE <|> ordGT
ordLT = do
symbol "<"
return LT
ordLE = do
symbol "<="
return LT
ordEQ = do
symbol "==" <|> symbol "="
return EQ
ordGT = do
symbol ">"
return GT
ordGE = do
symbol ">="
return GT
sqlAggregator :: Parser (VarName -> TableExpr)
sqlAggregator = selectProdAExpr
<|> selectMinAExpr
<|> selectMaxAExpr
<|> selectSumAExpr
<|> selectCountAExpr
<|> selectDistinctAExpr
<|> selectAExpr
selectProdAExpr :: Parser (VarName -> TableExpr)
selectProdAExpr = do
caseInsensKeyWord "product"
return SelectProd
selectMinAExpr :: Parser (VarName -> TableExpr)
selectMinAExpr = do
caseInsensKeyWord "min"
return SelectMin
selectMaxAExpr :: Parser (VarName -> TableExpr)
selectMaxAExpr = do
caseInsensKeyWord "max"
return SelectMax
selectSumAExpr :: Parser (VarName -> TableExpr)
selectSumAExpr = do
caseInsensKeyWord "sum"
return SelectSum
selectCountAExpr :: Parser (VarName -> TableExpr)
selectCountAExpr = do
caseInsensKeyWord "count"
return SelectCount
selectDistinctAExpr :: Parser (VarName -> TableExpr)
selectDistinctAExpr = do
caseInsensKeyWord "distinct"
return SelectDistinct
selectAExpr :: Parser (VarName -> TableExpr)
selectAExpr = do
return Select
filterVarConst :: Bool -> Parser [Function]
filterVarConst pos = do
aexpr <- aExpr
op <- order
c <- signedFloat
let y = "filt"
let as = aexprToExpr y (aexprNormalize $ aexpr)
let b = if pos then Filt op y c else FiltNeg op y c
return [F as b]
filterVarVar :: Bool -> Parser [Function]
filterVarVar pos = do
aexpr1 <- aExpr
op <- order
aexpr2 <- aExpr
let y = "filt~"
let y1 = "filty1~"
let y2 = "filty2~"
let z1 = "filtz1~"
let z2 = "filtz2~"
let as1 = M.toList $ aexprToExpr y1 (aexprNormalize $ aexpr1)
let as2 = M.toList $ aexprToExpr y2 (aexprNormalize $ aexpr2)
let as = M.fromList $ as1 ++ as2 ++ (case op of GT -> [(y, Sum [y1, z2]),(z2, Prod [y2,z1]),(z1, Const (-1.0))]
_ -> [(y, Sum [y2, z2]),(z2, Prod [y1,z1]),(z1, Const (-1.0))])
let b = if pos then Filt op y 0.0 else FiltNeg op y 0.0
return [F as b]
-- this filter makes sense only of applied to booleans
-- otherwise, it treats all values that do not equal 0 as 1
filterNocomp :: Bool -> Parser [Function]
filterNocomp pos = do
aexpr <- aExpr
let y = "filt~"
let as = aexprToExpr y (aexprNormalize $ aexpr)
let b = if pos then FiltNeg EQ y 0.0 else Filt EQ y 0.0
return [F as b]
filterBetween :: Bool -> Parser [Function]
filterBetween pos = do
aexpr <- aExpr
caseInsensKeyWord "between"
aexprLB <- aExpr
caseInsensKeyWord "and"
aexprUB <- aExpr
let y = "filt~"
let y1 = "filty1~"
let y2 = "filty2~"
let z1 = "filtz1~"
let z2 = "filtz2~"
let as1 = M.toList $ aexprToExpr y1 (aexprNormalize $ aexpr)
let asLB2 = M.toList $ aexprToExpr y2 (aexprNormalize $ aexprLB)
let asUB2 = M.toList $ aexprToExpr y2 (aexprNormalize $ aexprUB)
let asLB = M.fromList (as1 ++ asLB2 ++ [(y, Sum [y1, z2]),(z2, Prod [y2,z1]),(z1, Const (-1.0))])
let asUB = M.fromList (as1 ++ asUB2 ++ [(y, Sum [y1, z2]),(z2, Prod [y2,z1]),(z1, Const (-1.0))])
let bLB = if pos then Filt GT y 0.0 else FiltNeg GT y 0.0
let bUB = if pos then Filt LT y 0.0 else FiltNeg LT y 0.0
return [F asLB bLB, F asUB bUB]
-- ======================================================================= --
-----------------------------------------------------------------------------
---- The code below does not need to be updated with Banach.hs ----
-----------------------------------------------------------------------------
sqlFilter :: Parser [Function]
sqlFilter = try sqlFilterNeg <|> sqlFilterPos
sqlFilterExpr :: Parser [Function]
sqlFilterExpr = do
bexpr <- bExpr
let y = "filt"
let as = aexprToExpr y $ aexprNormalize bexpr
-- how many filters we actually have if we split them by "and"?
let last = as ! y
let xs = case last of
Prod xs -> xs
_ -> []
let filters = if length xs == 0 then [F as (Filter y)] else map (\x -> F as (Filter x)) xs
return filters
sqlFilterPos :: Parser [Function]
sqlFilterPos = sqlFilterMain True
sqlFilterNeg :: Parser [Function]
sqlFilterNeg = do
caseInsensKeyWord "not"
qs <- sqlFilterMain False
return qs
sqlFilterMain :: Bool -> Parser [Function]
sqlFilterMain pos = try (filterBetween pos) <|> try (filterVarConst pos) <|> try (filterVarVar pos) <|> filterNocomp pos
sqlQueries :: Parser (TableName, M.Map TableName Query)
sqlQueries = try sqlManyQueries <|> sqlOneQuery
sqlManyQueries :: Parser (TableName, M.Map TableName Query)
sqlManyQueries = do
qs1 <- many (try sqlAsgnQuery)
(tableName, qs2) <- sqlAggrQuery
return $ (tableName, M.union (concatMaps qs1) qs2)
sqlOneQuery :: Parser (TableName, M.Map TableName Query)
sqlOneQuery = do
res <- sqlAggrQuery
return res
sqlAsgnQuery :: Parser (M.Map TableName Query)
sqlAsgnQuery = do
tableName <- tableName
void (asgn)
(gs,colNames,groups,aexprs,tableNames,tableAliases,filters,internalQueries) <- sqlQuery
let fs = zipWith3 (\g x y -> F (aexprToExpr y $ aexprNormalize x) (g y)) gs aexprs colNames
let tableAliasMap = M.fromList $ zip tableAliases tableNames
let subquery = P groups fs tableAliasMap filters
return $ M.insert tableName subquery internalQueries
sqlQuery :: Parser ([VarName -> TableExpr],[VarName],[VarName],[AExpr VarName],[TableName],[TableAlias],[Function],(M.Map TableName Query))
sqlQuery = do
caseInsensKeyWord "select"
namedAExprs <- sepBy1 namedAExpr (symbol ",")
let (names, gs, aexprs) = unzip3 namedAExprs
caseInsensKeyWord "from"
internalTableData <- sepBy1 internalTable (symbol ",")
let (tableNames,tableAliases,internalQueries) = unzip3 internalTableData
filters <- sqlQueryFilter
groups <- sqlQueryGroupBy
return (gs,names,groups,aexprs,tableNames,tableAliases,filters,concatMaps internalQueries)
sqlAggrQuery :: Parser (TableName, M.Map TableName Query)
sqlAggrQuery = sqlAggrQueryNamed <|> sqlAggrQueryUnnamed defaultOutputTableName
sqlAggrQueryNamed :: Parser (TableName, M.Map TableName Query)
sqlAggrQueryNamed = do
tableName <- tableName
void (asgn)
res <- sqlAggrQueryUnnamed tableName
return res
sqlAggrQueryUnnamed :: TableName -> Parser (TableName, M.Map TableName Query)
sqlAggrQueryUnnamed outputTableName = do
caseInsensKeyWord "select"
unnamedAExprs <- sepBy1 unnamedAExpr (symbol ",")
let (gs, aexprs) = unzip unnamedAExprs
caseInsensKeyWord "from"
internalTableData <- sepBy1 internalTable (symbol ",")
let (tableNames,tableAliases,internalQueries) = unzip3 internalTableData
filters <- sqlQueryFilter
groups <- sqlQueryGroupBy
let ys = map (\i -> "y~" ++ show i) [0..length gs - 1]
let queryFuns = zipWith3 (\g aexpr y -> F (aexprToExpr y (aexprNormalize $ aexpr)) (g y)) gs aexprs ys
let tableAliasMap = M.fromList $ zip tableAliases tableNames
let subquery = P groups queryFuns tableAliasMap filters
let tableName = outputTableName
return (outputTableName, M.insert tableName subquery $ concatMaps internalQueries)
internalTable :: Parser (TableName, TableAlias, (M.Map TableName Query))
internalTable = internalTableQuery <|> try internalTableNameAS <|> internalTableName
-- give the table name and the alias explicitly
internalTableNameAS = do
tableName <- identifier
caseInsensKeyWord "as"
tableAlias <- identifier
return (tableName, tableAlias, M.empty)
-- the real name of a nested query equals the alias, and we know that there cannot be multiple copies of it
internalTableQuery = do
(gs,colNames,groups,aexprs,tableNames,tableAliases,filters,internalQueries) <- parens sqlQuery
caseInsensKeyWord "as"
tableAlias <- identifier
let fs = zipWith3 (\g x y -> F (aexprToExpr y $ aexprNormalize x) (g y)) gs aexprs colNames
let tableAliasMap = M.fromList $ zip tableAliases tableNames
let subquery = P groups fs tableAliasMap filters
let tableName = tableAlias
return (tableAlias, tableAlias, M.insert tableName subquery internalQueries)
-- if an alias is not specified, then the table name is used directly
internalTableName = do
tableName <- identifier
return (tableName, tableName, M.empty)
sqlQueryFilter :: Parser [Function]
sqlQueryFilter = sqlQueryWithFilter <|> sqlQueryWithoutFilter
sqlQueryWithoutFilter :: Parser [Function]
sqlQueryWithoutFilter = do
return []
sqlQueryWithFilter :: Parser [Function]
sqlQueryWithFilter = do
caseInsensKeyWord "where"
filters <- sqlFilterExpr
--filters <- fmap concat $ sepBy1 sqlFilter (caseInsensKeyWord "and")
return filters
sqlQueryGroupBy :: Parser [String]
sqlQueryGroupBy = sqlQueryWithGroupBy <|> sqlQueryWithoutGroupBy
sqlQueryWithoutGroupBy :: Parser [String]
sqlQueryWithoutGroupBy = do
return []
sqlQueryWithGroupBy :: Parser [String]
sqlQueryWithGroupBy = do
caseInsensKeyWord "group"
caseInsensKeyWord "by"
colnames <- sepBy1 identifier (symbol ",")
return colnames
--------------------------------------
---- Parsing general input format ----
--------------------------------------
query :: Parser Query
query = do
tablePath <- text
void (delim)
xs <- many varName
void (delim)
f <- function QueryParsing
let tableAliasMap = M.fromList [(tablePath,tablePath)]
return $ P [] [f] tableAliasMap []
-- the first row in the norm file is the list of sensitive rows
-- the second row in the norm file is the list of sensitive columns
norm :: Parser (([Int], [VarName]), Function)
norm = do
is <- many integer
xs <- many varName
void (delim)
f <- try customNorm <|> defaultNorm xs
return ((is, xs), f)
customNorm = do
f <- function NormParsing
return f
defaultNorm xs = do
let f = F (M.fromList [("z",LInf xs)]) (SelectMax "z")
return f
asgnStmt :: ParserInstance -> Parser [(VarName,Expr)]
asgnStmt p = do
a <- varName
void (asgn)
bs <- case p of {QueryParsing -> queryExpr a; NormParsing -> normExpr a}
-- this introduces new temporary variables for complex expressions
-- here "~" can be any symbol that is not allowed to use in variable names
let as = map (\x -> a ++ "~" ++ show x) [1 .. length bs - 1]
void (delim)
return (zip (a:as) bs)
returnStmt :: ParserInstance -> Parser TableExpr
returnStmt p = do
keyWord "return"
a <- case p of {QueryParsing -> queryTableExpr; NormParsing -> normTableExpr}
void (delim)
return a
function :: ParserInstance -> Parser Function
function p = do
ass <- many (asgnStmt p)
b <- returnStmt p
let as = concat ass
return (F (M.fromList as) b)
------------------------------
---- Symbols and keywords ----
------------------------------
-- delimiter of rows
delim :: Parser String
delim = symbol ";"
-- assignment symbol
asgn :: Parser String
asgn = symbol "="
-- line comment
lineComment :: String
lineComment = "//"
-- block comment
blockCommentStart :: String
blockCommentStart = "/*"
blockCommentEnd :: String
blockCommentEnd = "*/"
-------------------------------------
---- Some auxiliary subparsers ----
-------------------------------------
-- this is to extract the actual parsed data
-- seems very ugly, there should be some easier way to extract data from "Either"
parseData :: (Parser a) -> (String -> String) -> String -> a
parseData p err s =
let res = parse p "" s in
case res of
Left x -> error $ err (parseErrorPretty x)
Right x -> x
parseFromFile :: (Parser a) -> (String -> String -> String) -> String -> IO a
parseFromFile p err s = fmap (parseData p (err s)) (readInput s)
parseTestFromFile :: (Show a, ShowErrorComponent e) => Parsec e String a -> FilePath -> IO ()
parseTestFromFile p s = parseTest p (unsafePerformIO (readInput s))
parseNormFromFile fileName = parseFromFile norm error_parseNorm fileName
parseQueryFromFile fileName = parseFromFile query error_parseQuery fileName
parseSqlQueryFromFile fileName = parseFromFile sqlQueries error_parseSqlQuery fileName
-- a keyword
keyWord :: String -> Parser ()
keyWord w = lexeme (C.string w *> notFollowedBy C.alphaNumChar)
caseInsensKeyWord :: String -> Parser ()
caseInsensKeyWord w = lexeme (C.string' w *> notFollowedBy C.alphaNumChar)
-- variable identifier, as taken from the tutorial
-- it checks that the identifier is not a keyword
identifier :: Parser String
identifier = (lexeme . try) (p >>= check)
where
p = (:) <$> C.letterChar <*> many alphaNumCharAndPeriod
check x = if S.member (map toLower x) allCaseInsensKeyWords || S.member x allKeyWords
then fail $ "keyword " ++ show x ++ " cannot be an identifier"
else return x
alphaNumCharAndPeriod :: Parser Char
alphaNumCharAndPeriod = C.char '.'
<|> C.char '_'
<|> C.alphaNumChar
-- we need to read string identifiers and afterwards map them to integers
varName :: Parser VarName
varName = identifier
tableName :: Parser TableName
tableName = identifier
tableAlias :: Parser TableAlias
tableAlias = identifier
constant :: Parser VarName
constant = do
c <- signedFloat
return ("const" ++ show c)
--reads an arbitrary string, all characters up to the first space
text :: Parser String
text = lexeme (C.char '"' >> manyTill L.charLiteral (C.char '"'))
-- this thing eats all spaces and comments
spaceConsumer :: Parser ()
spaceConsumer =
L.space C.space1 lineCmnt blockCmnt
where
lineCmnt = L.skipLineComment lineComment
blockCmnt = L.skipBlockComment blockCommentStart blockCommentEnd
-- reads a lexeme and removes all trailing whitespaces and comments
lexeme :: Parser a -> Parser a
lexeme = L.lexeme spaceConsumer
-- reads a pure string and removes all trailing whitespaces and comments
symbol :: String -> Parser String
symbol = L.symbol spaceConsumer
-- reads an integer
integer :: Parser Int
integer = lexeme L.decimal
-- reads a double
float :: Parser Double
float = try (lexeme L.float) <|> fmap fromIntegral integer
-- reads a signed double
signedFloat :: Parser Double
signedFloat = try (L.signed spaceConsumer float) <|> fmap fromIntegral (L.signed spaceConsumer integer)
stringAsInt :: Parser Double
stringAsInt = hash <$> text
parens :: Parser a -> Parser a
parens = between (symbol "(") (symbol ")")
-- assume that the tables are located in the same place where the query is
readAllTables :: String -> Bool -> [TableName] -> [TableAlias] -> IO (M.Map TableAlias TableData)
readAllTables queryPath usePrefices tableNames tableAliases = do
-- collect all tables and all column names that will be used in our query
-- read table sensitivities from corresponding files
-- mapM is a standard function [IO a] -> IO [a]
let dbData = mapM (\tableName -> readDB $ queryPath ++ tableName ++ ".db") tableNames
let dbNormData = mapM (\tableName -> parseNormFromFile $ queryPath ++ tableName ++ ".nrm") tableNames
(tableColNames, tableValues) <- fmap unzip dbData
(tableSensitives,tableNormFuns) <- fmap unzip dbNormData
let (tableSensitiveRows,tableSensitiveVars) = unzip tableSensitives
-- we put table names in front of column names
let namePrefices = map (\tableAlias -> if usePrefices then tableAlias ++ "." else "") tableAliases
let taggedTableColNames = zipWith (\x ys -> map (\y -> x ++ y) ys) namePrefices tableColNames
let taggedSensitiveVars = zipWith (\x ys -> map (\y -> x ++ y) ys) namePrefices tableSensitiveVars
-- put all table data together
let tableMap = processAllTables tableAliases tableNames taggedTableColNames tableValues tableNormFuns taggedSensitiveVars tableSensitiveRows
return (M.fromList tableMap)
-- putting everything together
--getBanachAnalyserInput :: String -> IO (B.Table, B.TableExpr)
getBanachAnalyserInput :: Bool -> String -> IO (String, Double, B.Table, [(String,[Int])], [(String, String, [Int], B.TableExpr)], [String])
getBanachAnalyserInput debug input = do
let queryPath = reverse $ dropWhile (/= '/') (reverse input)
-- "sqlQuery" parses a single query of the form SELECT ... FROM ... WHERE
(outputTableName,queryMap) <- parseSqlQueryFromFile input
let usePrefices = True
-- "query" parses the old format where the query function is computed line by line
--let queryMap = M.fromList [(outputTableName,parseQueryFromFile input)]
--let usePrefices = False
-- extract the tables that should be read from input files, take into account copies
-- substitute intermediate queries into the aggregated query
let (taskNames, inputTableAliases, inputTableNames, outputQueryFuns, outputFilterFuns) = processQuery outputTableName queryMap "" outputTableName outputTableName
let indexedTaskNames = zip taskNames [0..(length taskNames) - 1]
let taskMaps = concat $ map (\(ts,i) -> (map (\t -> (t,[i])) ) ts) indexedTaskNames
let taskMap = M.toList $ M.fromListWith (++) $ filter (\(t,_) -> t /= "") taskMaps
traceIOIfDebug debug $ "----------------"
traceIOIfDebug debug $ "Input table names: " ++ show inputTableNames
traceIOIfDebug debug $ "Input table aliases: " ++ show inputTableAliases
traceIOIfDebug debug $ "Task names: " ++ show taskNames
traceIOIfDebug debug $ "Task map: " ++ show taskMap
--traceIOIfDebug debug $ "----------------"
--traceIOIfDebug debug $ "TableQ " ++ show outputQueryFuns
traceIOIfDebug debug $ "TableF " ++ show outputFilterFuns
-- inputTableMap maps input table aliases to the actual table data that it reads from file (table contents, column names, norm, sensitivities)
inputTableMap <- readAllTables queryPath usePrefices inputTableNames inputTableAliases
-- we assume that each input table has been copied as many times as it is used, and we take the cross product of all resulting tables
-- the columns of the cross product are ordered according to the list 'inputTableAliases'
if (outputTableName == "ship_arrival_to_port") then do
traceIOIfDebug debug $ "!!!! cheating !!!!"
let tableData = zip4 inputTableNames inputTableAliases (replicate (length inputTableNames) []) (replicate (length inputTableNames) (B.SelectMin []))
--return (outputTableName, 2.72928188207754, [], taskMap, tableData, [])
return (outputTableName, 16.6207701385947, [], taskMap, tableData, [])
else
do
let (crossProductTable, sensitiveRowMatrix, inputVarList, sensitiveVarList) = getTableCrossProductData inputTableAliases inputTableMap
-- assign a unique integer to each column name, which is the order of this column in the cross product table
let inputMap = M.fromList $ zip inputVarList [0..length inputVarList - 1]
let sensitiveColSet = S.fromList $ map (inputMap ! ) sensitiveVarList
traceIOIfDebug debug $ "----------------"
traceIOIfDebug debug $ "All input variables: " ++ show (M.toList inputMap)
traceIOIfDebug debug $ "All sensitive cols: " ++ show sensitiveColSet
traceIOIfDebug debug $ "#rows before filtering: " ++ show (length crossProductTable)
--traceIOIfDebug debug $ "Sensitive row matrix: " ++ show sensitiveRowMatrix
-- we assume that the output query table has only one column
when (length outputQueryFuns > 1) $ error $ error_queryExpr_singleColumn
let outputQueryFun = head outputQueryFuns
let outputQueryExpr = snd $ query2Expr inputMap sensitiveColSet outputQueryFun
traceIOIfDebug debug $ "----------------"
traceIOIfDebug debug $ "Query fun (w/o filter) = " ++ show outputQueryFun
traceIOIfDebug debug $ "Query expr (w/o filter) = " ++ show outputQueryExpr
-- we may now apply the filter
let (filtQueryFuns, filtSensRowMatrix, filtTable) = filteredExpr crossProductTable inputMap sensitiveRowMatrix sensitiveColSet outputFilterFuns [outputQueryFun]
traceIOIfDebug debug $ "----------------"
traceIOIfDebug debug $ "#rows after filtering: " ++ show (length filtTable)
--traceIOIfDebug debug $ "Filt. sens. row matrix: " ++ show filtSensRowMatrix
-- now transform the main query to a banach expression
let mainQueryFun = head filtQueryFuns
let mainQueryExpr = snd $ query2Expr inputMap sensitiveColSet mainQueryFun
let mainQueryAggr = queryExprAggregate mainQueryFun mainQueryExpr
traceIOIfDebug debug ("----------------")
traceIOIfDebug debug ("Query funs (w/ filter) = " ++ show mainQueryFun)
traceIOIfDebug debug ("Query expr (w/ filter) = " ++ show mainQueryExpr)
traceIOIfDebug debug ("Query aggr (w/ filter) = " ++ show mainQueryAggr)
--bring the input to the form [(String, String, [Int], TableExpr)]
let dataWrtEachTable = inputWrtEachTable debug usePrefices inputTableAliases outputQueryExpr mainQueryExpr mainQueryAggr filtSensRowMatrix inputMap inputTableMap
let (allTableNames, allTableAliases, allSensitiveInputs, allQueries, minQueries, maxQueries) = unzip6 dataWrtEachTable
-- for min/max filters over private values, need find min/max after public rows have been already filtered out
let minExprData = map (\(x,y) -> B.analyzeTableExpr filtTable x y) $ zip allSensitiveInputs minQueries
let maxExprData = map (\(x,y) -> B.analyzeTableExpr filtTable x y) $ zip allSensitiveInputs maxQueries
-- replace ARMin and ARMax inside the queries with actual precomputed data
let tableExprData = zip4 allTableNames allTableAliases allSensitiveInputs (precAggr minExprData maxExprData allQueries)
traceIOIfDebug debug $ "----------------"
traceIOIfDebug debug $ "tableExprData:" ++ show tableExprData
traceIOIfDebug debug $ "----------------"
traceIOIfDebug debug $ "MIN: " ++ show minExprData
traceIOIfDebug debug $ "MAX: " ++ show maxExprData ++ "\n"
traceIOIfDebug debug $ "filtered table = " ++ show filtTable
traceIOIfDebug debug $ "----------------"