-
Notifications
You must be signed in to change notification settings - Fork 146
/
Test.hs
1803 lines (1720 loc) · 86.1 KB
/
Test.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
-- Copyright (c) 2017 Uber Technologies, Inc.
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy
-- of this software and associated documentation files (the "Software"), to deal
-- in the Software without restriction, including without limitation the rights
-- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
-- copies of the Software, and to permit persons to whom the Software is
-- furnished to do so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in
-- all copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
-- THE SOFTWARE.
{-# LANGUAGE CPP #-}
module Database.Sql.Hive.Parser.Test where
import Test.HUnit hiding (State)
import Test.HUnit.Ticket
import Database.Sql.Type
import Database.Sql.Info
import Database.Sql.Position
import Database.Sql.Hive.Parser
import Database.Sql.Hive.Type
import Database.Sql.Util.Test (makeSelect)
import Data.List.NonEmpty (NonEmpty(..))
#if !(MIN_VERSION_base(4,11,0))
import Data.Monoid
#endif
import Data.Int (Int64)
import Data.Text.Lazy (Text)
import qualified Data.Text.Lazy as TL
import Control.Monad.State
parsesSuccessfully :: Text -> Assertion
parsesSuccessfully sql = case parse sql of
(Right _) -> return ()
(Left e) -> assertFailure $ unlines
[ "failed to parse:"
, show sql
, show e
]
parsesUnsuccessfully :: Text -> Assertion
parsesUnsuccessfully sql = case parse sql of
(Left _) -> return ()
(Right _) -> assertFailure $ unlines
[ "parsed broken sql:"
, show sql
]
-- "SELECT 1;"
parseExactHelperSelect1 :: Int64 -> Query RawNames Range
parseExactHelperSelect1 offset =
let o7 = offset + 7
o8 = offset + 8
in
(QuerySelect
(Range (Position 1 offset offset) (Position 1 o8 o8))
(Select
{ selectInfo =
(Range (Position 1 offset offset) (Position 1 o8 o8))
, selectCols =
(SelectColumns
(Range (Position 1 o7 o7) (Position 1 o8 o8))
[SelectExpr
(Range (Position 1 o7 o7) (Position 1 o8 o8))
[ColumnAlias
(Range (Position 1 o7 o7) (Position 1 o8 o8))
"_c0"
(ColumnAliasId 1)
]
(ConstantExpr
(Range (Position 1 o7 o7) (Position 1 o8 o8))
(NumericConstant
(Range (Position 1 o7 o7) (Position 1 o8 o8))
"1"
)
)
]
)
, selectFrom = Nothing
, selectWhere = Nothing
, selectTimeseries = Nothing
, selectGroup = Nothing
, selectHaving = Nothing
, selectNamedWindow = Nothing
, selectDistinct = notDistinct
}
)
)
parseExactHelperStar :: State Position (SelectColumns RawNames Range)
parseExactHelperStar = do
star <- do
s <- get
modify $ advance "*"
e <- get
pure $ SelectStar (Range s e) Nothing Unused
pure (SelectColumns (getInfo star) [star])
parseExactHelperTablePotato :: State Position (Tablish RawNames Range)
parseExactHelperTablePotato = do
table <- do
s <- get
modify $ advance "potato"
e <- get
pure $ QTableName (Range s e) Nothing "potato"
pure $ TablishTable (getInfo table) TablishAliasesNone table
parseExactHelperFromTablePotato :: State Position (SelectFrom RawNames Range)
parseExactHelperFromTablePotato = do
s <- get
modify $ advance "FROM "
table <- parseExactHelperTablePotato
e <- get
pure $ SelectFrom (Range s e) [table]
-- "SELECT * FROM potato;"
parseExactHelperSelectStar :: State Position (Query RawNames Range)
parseExactHelperSelectStar = do
select <- do
s <- get
modify $ advance "SELECT "
star <- parseExactHelperStar
modify $ advance " "
from <- parseExactHelperFromTablePotato
e <- get
let selectInfo = Range s e
selectDistinct = notDistinct
selectCols = star
selectFrom = Just from
selectWhere = Nothing
selectTimeseries = Nothing
selectGroup = Nothing
selectHaving = Nothing
selectNamedWindow = Nothing
pure Select{..}
pure (QuerySelect (getInfo select) select)
testInvertedFrom :: Test
testInvertedFrom = test
[ "Test allowed FROM inversions" ~: map (TestCase . parsesSuccessfully)
[ "FROM foo SELECT *;"
, "FROM foo INSERT INTO bar SELECT *;"
, "WITH foo AS (SELECT * FROM bar) FROM foo SELECT *;"
, "WITH foo AS (FROM bar SELECT *) FROM foo SELECT *;"
, "WITH foo AS (SELECT * FROM bar) FROM foo INSERT INTO baz SELECT *;"
, "WITH foo AS (SELECT * FROM bar) INSERT INTO baz SELECT * FROM foo;"
, "INSERT INTO foo (col1, col2) SELECT * FROM bar;"
, "INSERT INTO foo (col1, col2) SELECT * FROM bar UNION SELECT * FROM baz;"
, "FROM bar INSERT OVERWRITE DIRECTORY 'hdfs://thingy' SELECT *;"
, "INSERT OVERWRITE DIRECTORY 'hdfs://thingy' SELECT * FROM bar;"
, "INSERT OVERWRITE DIRECTORY 'hdfs://thingy' SELECT * FROM bar UNION SELECT * FROM baz;"
, "FROM foo INSERT OVERWRITE bar SELECT *;"
, "SELECT * FROM (FROM foo SELECT *) bar;"
]
, "Test forbidden FROM inversions" ~: map (TestCase . parsesUnsuccessfully)
[ "INSERT INTO foo FROM bar SELECT *;"
, "FROM foo INSERT INTO bar FROM foo SELECT *;"
, "FROM foo INSERT INTO bar SELECT * FROM foo;"
, "FROM foo DELETE;"
, "FROM foo DELETE WHERE a is not null;"
, "INSERT INTO foo (SELECT * FROM bar);"
, "INSERT INTO foo (FROM bar SELECT *);"
, "FROM foo WITH foo AS (SELECT * FROM bar) SELECT *;"
, "INSERT INTO foo (col1, col2) FROM bar SELECT *;"
, "FROM bar INSERT INTO foo (col1, col2) SELECT * UNION ALL SELECT * FROM baz;"
, "FROM bar INSERT OVERWRITE DIRECTORY 'hdfs://thingy' SELECT * UNION ALL SELECT * FROM baz;"
, "CREATE TABLE foo AS FROM bar SELECT *;"
, "SELECT 1 FROM foo WHERE EXISTS (FROM bar SELECT a WHERE foo.a=bar.a);"
, "SELECT 1 FROM foo WHERE a IN (FROM bar SELECT a);"
]
, "Test allowed FROM inversion for UNIONs in particular" ~: map (TestCase . parsesSuccessfully)
[ "FROM foo SELECT 1 UNION ALL FROM foo SELECT 2;"
, "SELECT 1 FROM foo UNION SELECT 2 FROM foo;"
, "SELECT 1 FROM foo UNION ALL SELECT 2 FROM foo;"
-- The following are permitted. Note: the FROM only extends to the
-- first SELECT, not the second. So the first SELECT could return many
-- rows but the second SELECT returns only one row.
, "FROM foo SELECT 1 UNION SELECT 2;"
, "FROM foo SELECT 1 UNION ALL SELECT 2;"
-- The following will parse, but will fail to resolve.
, "FROM foo SELECT * UNION ALL SELECT *;"
]
, ticket "T539933"
[ -- why are these forbidden but UNION ALL allowed? I dunno, Hive is cray.
parsesUnsuccessfully "FROM foo SELECT 1 UNION FROM foo SELECT 2;"
, parsesUnsuccessfully "FROM foo SELECT 1 UNION DISTINCT FROM foo SELECT 2;"
-- why can't you mix/match? IDK.
, parsesUnsuccessfully "FROM foo SELECT 1 UNION ALL SELECT 2 FROM foo;"
, parsesUnsuccessfully "SELECT 1 FROM foo UNION ALL FROM foo SELECT 2;"
]
]
testParser_hiveSuite :: Test
testParser_hiveSuite = test
[ "Parse against exhaustive HiveQL definition" ~: map (TestCase . parsesSuccessfully)
[ "SELECT 1;"
, "SELECT * FROM t1;"
, "SELECT current_database();"
, "USE database_name;"
, "SELECT * FROM sales WHERE amount > 10 AND region = \"US\";"
, "SELECT\n1;"
, "SELECT -- I'm a comment!\n1;"
, "SELECT * FROM t1;"
, "SELECT 1 as FROM foo;" -- here, `as` is the column alias.
, "SELECT 1 FROM foo AS localtime;"
, TL.unlines
[ "CREATE TABLE IF NOT EXISTS foo.bar"
, "PARTITIONED BY (baz string)"
, "ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.avro.AvroSerDe'"
, "STORED AS avro"
, "TBLPROPERTIES ('avro.schema.url'='hdfs://thingy')"
, ";"
]
, "SELECT current_database();"
, "SELECT * FROM sales WHERE amount > 10 AND region = \"US\";"
, "SELECT my_col from `my_table`;"
, "select count(*) from `campaigns`;"
, "SELECT /*+ label */ my_col from my_table;"
, TL.unlines
[ "-- comment"
, "SELECT"
, "1 -- comment;"
, ";"
]
-- TODO add Load statements
-- Test for overwrite keyword
, "INSERT OVERWRITE TABLE potato SELECT * FROM tomato;"
, "INSERT OVERWRITE TABLE foo.bar VALUES ('foo', 1, 2), ('bar', 3, 4);"
, "SELECT * FROM potato WHERE datestr >= date_sub(current_timestamp(), 31);"
, "ANALYZE TABLE potato COMPUTE STATISTICS;"
, "ANALYZE TABLE potato COMPUTE STATISTICS FOR COLUMNS CACHE METADATA NOSCAN;"
, "ANALYZE TABLE potato PARTITION (soup) COMPUTE STATISTICS;"
, "SHOW COLUMNS FROM foo;"
, "INSERT INTO etltmp.potato SELECT * FROM tomato;"
, "INSERT INTO etltmp.potato (col1, col2) SELECT * FROM tomato;"
, "INSERT OVERWRITE etltmp.potato SELECT * FROM tomato;"
, "INSERT OVERWRITE DIRECTORY \"/tmp/foo/bar\" SELECT * FROM potato;"
, "INSERT OVERWRITE LOCAL DIRECTORY \"/tmp/foo/bar\" SELECT * FROM potato;"
, "LOAD DATA INPATH '/path' INTO TABLE foo;"
, "LOAD DATA INPATH '/path' OVERWRITE INTO TABLE foo;"
, "LOAD DATA INPATH '/path' INTO TABLE foo PARTITION (col='val');"
, "LOAD DATA INPATH '/path' OVERWRITE INTO TABLE foo PARTITION (col='val');"
, "LOAD DATA LOCAL INPATH '/path' OVERWRITE INTO TABLE foo PARTITION (col='val', col2='val2');"
, "WITH foo AS (SELECT 1) INSERT OVERWRITE DIRECTORY \"/tmp/foo/bar\" SELECT * FROM foo;"
, "WITH foo AS (SELECT 1) INSERT OVERWRITE LOCAL DIRECTORY \"/tmp/foo/bar\" SELECT * FROM foo;"
, "WITH foo AS (SELECT * FROM potato) INSERT INTO TABLE bar SELECT * FROM foo;"
, "WITH foo AS (SELECT * FROM potato) INSERT OVERWRITE TABLE bar SELECT * FROM foo;"
, "WITH foo AS (SELECT * FROM potato) INSERT INTO bar SELECT * FROM foo;"
, "WITH foo AS (SELECT * FROM potato) INSERT OVERWRITE bar SELECT * FROM foo;"
, TL.unlines
[ "INSERT OVERWRITE DIRECTORY '/some/path'"
, "ROW FORMAT DELIMITED"
, "FIELDS TERMINATED BY '\t'"
, "SELECT * FROM foo;"
]
, "CREATE TABLE potato STORED AS ORC AS SELECT * FROM tomato;"
, "CREATE TABLE potato STORED AS SEQUENCEFILE AS SELECT * FROM tomato;"
, "CREATE TABLE potato STORED AS TEXTFILE AS SELECT * FROM tomato;"
, "CREATE TABLE potato STORED AS RCFILE AS SELECT * FROM tomato;"
, "CREATE TABLE potato STORED AS PARQUET AS SELECT * FROM tomato;"
, "CREATE TABLE potato STORED AS AVRO AS SELECT * FROM tomato;"
, TL.unlines
[ "CREATE TABLE potato STORED AS"
, "INPUTFORMAT \"com.hadoop.mapred.DeprecatedLzoTextInputFormat\""
, "OUTPUTFORMAT \"org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat\""
, "AS SELECT * FROM tomato;"
]
, "CREATE TABLE potato TBLPROPERTIES(\'EXTERNAL\'=\'FALSE\') AS SELECT 1"
, "CREATE TABLE potato STORED AS PARQUET LOCATION 'foo' AS SELECT * FROM tomato;"
, "ALTER TABLE foo SET LOCATION 'hdfs://thingy';"
, "ALTER TABLE foo PARTITION (bar='2015-10-15') SET LOCATION 'hdfs://thingy';"
, "ALTER TABLE foo ADD PARTITION (bar='2015-10-15');"
, TL.unwords
[ "ALTER TABLE foo ADD IF NOT EXISTS"
, "PARTITION (bar='2015-10-15',baz='asdf') LOCATION 'hdfs://foo/bar1'"
, "PARTITION (bar='2015-10-16',baz='hjkl') LOCATION 'hdfs://foo/bar2'"
, ";"
]
, "ALTER TABLE foo DROP PARTITION (bar='2015-10-15');"
, TL.unwords
[ "ALTER TABLE foo DROP IF EXISTS"
, "PARTITION (bar='2015-10-15')"
, "PARTITION (bar='2015-10-16')"
, "IGNORE PROTECTION"
, "PURGE"
, ";"
]
, "ALTER TABLE foo.bar RENAME TO baz.quux;"
, "INSERT INTO TABLE foo PARTITION (potato=1) SELECT * FROM bar;"
, "INSERT INTO TABLE foo PARTITION (potato) SELECT * FROM bar;"
, "INSERT INTO TABLE foo PARTITION (potato=1, soup=2) SELECT * FROM bar;"
, "INSERT INTO TABLE foo PARTITION (potato=1, soup) SELECT * FROM bar;"
, "INSERT INTO TABLE foo PARTITION (potato, soup) SELECT * FROM bar;"
, "SELECT s.t.c.x0.x1 FROM s.t;"
, "SELECT t.c.x0.x1 FROM t;"
, "SELECT c.x0.x1 FROM t;"
, "SELECT t.c.x0 from potato t;"
, "EXPLAIN SELECT 1;"
, "EXPLAIN DELETE FROM foo;"
, "EXPLAIN INSERT INTO foo SELECT 1;"
, "SELECT * FROM foo INNER JOIN bar;"
, "SELECT * FROM foo JOIN bar;"
, "SELECT * FROM foo LEFT JOIN bar;"
, "SELECT * FROM foo RIGHT JOIN bar;"
, "SELECT * FROM foo FULL OUTER JOIN bar;"
, "SELECT * FROM foo CROSS JOIN bar;"
, "SELECT datediff('a', 'b');"
, "SELECT datediff('a', current_timestamp());"
, "SELECT * FROM potato ca WHERE USER.uuid IS NOT NULL;"
, "SELECT 'asdf' RLIKE '^[0-9]*$';"
, "SELECT 'asdf' REGEXP '^[0-9]*$';"
, "SELECT 'asdf' ! RLIKE '^[0-9]*$';"
, "SELECT * FROM potato CLUSTER BY a;"
, "SELECT * FROM potato DISTRIBUTE BY a;"
, "SELECT * FROM potato SORT BY a;"
, "SELECT * FROM potato DISTRIBUTE BY a SORT BY b;"
, "SELECT * FROM foo CROSS JOIN bar ON foo.a = bar.a;"
, "SELECT * FROM foo LEFT SEMI JOIN bar;"
, "SELECT * FROM foo LEFT SEMI JOIN bar ON (foo.id = bar.id);"
, "SELECT * FROM foo LATERAL VIEW explode(a) exploded AS x;"
, "SELECT bar.col FROM foo LATERAL VIEW explode(foo.array_col) bar;"
, "SELECT bar.key, bar.val FROM foo LATERAL VIEW explode(foo.map_col) bar;"
, "SELECT bar.col1, bar.col2 FROM foo LATERAL VIEW inline(foo.array_of_struct_col) bar;"
, "SELECT bar.c0, bar.c1, bar.c2 FROM foo LATERAL VIEW json_tuple(foo.json_col, k1, k2, k3) bar;"
, "SELECT bar.c0, bar.c1, bar.c2 FROM foo LATERAL VIEW parse_url_tuple(foo.url_col, 'HOST', 'PORT', 'PROTOCOL') bar;"
, "SELECT bar.pos, bar.val FROM foo LATERAL VIEW posexplode(foo.array_col) bar;"
, "SELECT bar.col0, bar.col1, bar.col2 FROM foo LATERAL VIEW stack(2, 1, 2, 3, 4, 5) bar;"
-- Named windows can substitute for window exprs in OVER clauses
, "SELECT RANK() OVER x FROM potato WINDOW x AS (PARTITION BY a ORDER BY b ASC);"
-- They can also be inherited, as long as orderby is not double-defined
, TL.unlines
[ "SELECT RANK() OVER (x ORDER BY b ASC),"
, "DENSE_RANK() OVER (x ORDER BY b DESC)"
, "FROM potato WINDOW x AS (PARTITION BY a);"
]
, TL.unlines
[ "SELECT RANK() OVER (x ROWS BETWEEN 2 PRECEDING AND CURRENT ROW)"
, "FROM potato WINDOW x AS (PARTITION BY a ORDER BY b ASC);"
]
-- Unlike vertica, hive can have anything in its named window clause
-- Which is vomitous, since frame clauses mean nothing without order
, "SELECT RANK() OVER (x) FROM potato WINDOW x AS (PARTITION BY a);"
, "SELECT RANK() OVER (x) FROM potato WINDOW x AS (ORDER BY a);"
, "SELECT RANK() OVER (x) FROM potato WINDOW x AS (ROWS UNBOUNDED PRECEDING);"
, "SELECT RANK() OVER (x) FROM potato WINDOW x AS ();"
, "SELECT RANK() OVER (x) FROM potato WINDOW x AS (PARTITION BY a ORDER BY a ROWS UNBOUNDED PRECEDING);"
-- Named windows can also inherit from each other,
-- similar to WITH clauses
, TL.unlines
[ "SELECT RANK() OVER (w1 ORDER BY sal DESC),"
, "RANK() OVER w2"
, "FROM EMP"
, "WINDOW w1 AS (PARTITION BY deptno), w2 AS (w1 ORDER BY sal);"
]
-- In hive, they can inherit in all their glory.
-- Ambiguous definitions? Frames without orders? It's got it.
, TL.unlines
[ "SELECT RANK() OVER (w2) FROM EMP"
, "WINDOW w1 AS (PARTITION BY deptno),"
, "w2 AS (w1 PARTITION BY alt ORDER BY sal);"
]
, TL.unlines
[ "SELECT RANK() OVER (w2) FROM EMP"
, "WINDOW w1 AS (PARTITION BY deptno),"
, "w2 AS (w1 ORDER BY sal ROWS UNBOUNDED PRECEDING);"
]
, TL.unlines
[ "SELECT RANK() OVER (w2 PARTITION BY foo) FROM EMP"
, "WINDOW w1 AS (PARTITION BY deptno),"
, "w2 AS (w1 PARTITION BY sal ROWS UNBOUNDED PRECEDING);"
]
, TL.unlines
[ "SELECT RANK() OVER (w2 PARTITION BY a ORDER BY b ROWS UNBOUNDED PRECEDING)"
, "FROM EMP"
, "WINDOW w1 AS (PARTITION BY c ORDER BY d ROWS UNBOUNDED PRECEDING),"
, "w2 AS (w1 PARTITION BY e ORDER BY f ROWS UNBOUNDED PRECEDING);"
]
-- These should parse Successfully, but fail to resolve:
-- Named window uses cannot include already defined components
, TL.unlines
[ "SELECT RANK() OVER (x ORDER BY c) FROM potato"
, "WINDOW x AS (PARTITION BY a ORDER BY b);"
]
-- Named windows must have unique names
, TL.unlines
[ "SELECT RANK() OVER x FROM potato"
, "WINDOW x as (PARTITION BY a), x AS (PARTITION BY b);"
]
, TL.unlines
[ "SELECT RANK() OVER (x) FROM potato"
, "WINDOW x AS (ORDER BY b);"
]
, TL.unlines
[ "FROM foo_stg f"
, "INSERT OVERWRITE TABLE foo PARTITION(dt='2008-06-08', x)"
, "SELECT f.x, f.y, f.z;"
]
, "WITH full AS (SELECT 1) SELECT * FROM full;"
, "SELECT * FROM full;"
, "SELECT * FROM full f;"
, "SELECT * FROM f full;"
, "SELECT * FROM f AS full;"
, "SELECT * FROM full FULL OUTER JOIN bar;"
, "SELECT * FROM full f FULL OUTER JOIN bar;"
, "SELECT * FROM f full FULL OUTER JOIN bar;"
, "SELECT * FROM f AS full FULL OUTER JOIN bar;"
, "SET hive.exec.parallel=true;"
, "SET hive.exec.parallel = true;"
, "SET mapred.output.compression.codec=org.apache.hadoop.io.compress.SnappyCodec;"
, "SET mapreduce.job.queuename=foo-bar-baz;"
, "SET x*x=1*5;"
, "SET foo-bar-baz=foo-bar-baz;"
, "SET mapreduce.job.queuename;"
, "SET;"
, "SET -v;"
, TL.unlines
[ "CREATE TABLE IF NOT EXISTS foo.bar (a int)"
, "CLUSTERED BY (a) SORTED BY (a ASC) INTO 256 BUCKETS"
, "STORED AS ORC;"
]
, "SELECT * FROM foo WHERE ${hiveconf:bar};"
, "CREATE TABLE potato (foo INT COMMENT 'I am a stegosaurus!');"
, "CREATE TABLE potato (foo INT) COMMENT 'I''m not little! I drink milk!';"
, "CREATE TABLE potato (foo STRUCT<bar: STRING COMMENT 'Boop!'>);"
, "ALTER TABLE foo.bar SET TBLPROPERTIES(\"EXTERNAL\"=\"FALSE\");"
, TL.unlines
[ "CREATE VIEW IF NOT EXISTS foo.bar"
, "AS SELECT some_col"
, "FROM foo.baz;"
]
, "ALTER TABLE foo.bar PARTITION(baz) ADD COLUMNS (quux1 int, quux2 int);"
, "SELECT stack(4, 'a', 364, 'b', 42, 'c', 7, 'd', 1) AS (col1, col2);"
, "CREATE FUNCTION foo.bar AS 'class' USING JAR 'hdfs:///path';"
, "CREATE TEMPORARY FUNCTION foo AS 'class';"
, "DROP FUNCTION foo;"
, "DROP TEMPORARY FUNCTION foo;"
]
, "Parse failure on malformed HiveQL statements" ~: map (TestCase . parsesUnsuccessfully)
[ "SELECT;"
, "USE;"
, "HI;"
, "SELECT /* comment */ 1;" -- Block-style comments are not allowed
, TL.unlines
-- no UNIONs in EXISTS clauses
[ "SELECT 1 FROM foo WHERE EXISTS ("
, " SELECT 1 FROM bar WHERE foo.a=bar.a"
, " UNION ALL"
, " SELECT 2 FROM bar where foo.b=bar.b"
, ");"
]
-- Dynamic partitions cannot precede any static partition
, "INSERT INTO TABLE foo PARTITION (potato, soup=2) SELECT * FROM bar;"
-- Degenerate partitions are not allowed
, "INSERT INTO TABLE foo PARTITION () SELECT * FROM bar;"
, "EXPLAIN TRUNCATE foo;"
, "SELECT * FROM foo f NATURAL JOIN bar b;"
, "SELECT * FROM foo INNER JOIN bar USING (a);"
, "SELECT datediff(ww, 'a', 'b');"
, "SELECT * FROM potato CLUSTER BY a DISTRIBUTE BY b;"
, "SELECT * FROM potato CLUSTER BY a SORT BY b;"
, "SELECT * FROM potato CLUSTER BY a DISTRIBUTE BY b SORT BY c;"
-- SubqueryExprs are not permitted in Hive.
, "SELECT (SELECT foo.x) FROM (SELECT 1 x) foo;"
-- nor are OFFSET clauses
, "SELECT * FROM foo ORDER BY a LIMIT 10 OFFSET 5;"
-- nor are TIMESERIES clauses
, TL.unlines
[ "CREATE TABLE times AS"
, "SELECT slice_time as start_time"
, "FROM (SELECT '2015-12-15 23:00:00' d UNION SELECT '2015-12-18 03:09:55' d) a"
, "TIMESERIES slice_time AS '1 hour' OVER (ORDER BY d)"
, ";"
]
-- Normally, named window must have partition, may have order,
-- cannot have frame. Hive does whatever.
-- However, hive cares about frames without order.
, TL.unlines
[ "SELECT RANK() OVER x FROM potato"
, "WINDOW x AS (PARTITION BY a"
, "RANGE UNBOUNDED PRECEDING AND CURRENT ROW);"
]
, "SELECT * FROM foo AS;"
, "SELECT * FROM foo AS full OUTER JOIN bar;"
, "INSERT OVERWRITE TABLE foo.bar VALUES ('foo', 1, 2 + 3);" -- VALUES must be constants
, "SELECT * FROM ${hiveconf:bar};" -- variable substitution only allowed in Exprs
]
, "Parse exact HiveQL statements" ~:
[ parse "USE default;" ~?= Right
( HiveUseStmt
( UseDefault
(Range (Position 1 0 0)
(Position 1 11 11))))
, parse "USE database_name;" ~?= Right
( HiveUseStmt
( UseDatabase
(QSchemaName
(Range (Position 1 0 0)
(Position 1 17 17))
None
"database_name"
NormalSchema)))
, parse "SELECT 1;" ~?= Right
( HiveStandardSqlStatement
(QueryStmt $ parseExactHelperSelect1 0 ))
, parse "SELECT * FROM potato;" ~?= Right
( HiveStandardSqlStatement
(QueryStmt $ evalState parseExactHelperSelectStar (Position 1 0 0)))
, parse "INSERT INTO TABLE foo PARTITION (bar) SELECT 1;" ~?= Right
( HiveStandardSqlStatement
( InsertStmt
( Insert
{ insertInfo =
(Range (Position 1 0 0) (Position 1 46 46))
, insertBehavior = InsertAppendPartition
(Range (Position 1 7 7) (Position 1 11 11))
()
, insertTable = QTableName
(Range (Position 1 18 18) (Position 1 21 21))
Nothing
"foo"
, insertColumns = Nothing
, insertValues =
InsertSelectValues $ parseExactHelperSelect1 38
}
)
)
)
, parse "CREATE TABLE foo (bar String, baz String) TBLPROPERTIES ('bar'='1')" ~?= Right
( HiveStandardSqlStatement
( CreateTableStmt
( CreateTable
{ createTableInfo =
(Range (Position 1 0 0) (Position 1 67 67))
, createTablePersistence = Persistent
, createTableExternality = Internal
, createTableIfNotExists = Nothing
, createTableName =
QTableName
{ tableNameInfo =
(Range (Position 1 13 13) (Position 1 16 16))
, tableNameSchema = Nothing
, tableNameName = "foo"
}
, createTableDefinition = TableColumns
(Range (Position 1 17 17) (Position 1 41 41))
(
(
ColumnOrConstraintColumn
( ColumnDefinition
{ columnDefinitionInfo = (Range (Position 1 18 18) (Position 1 28 28))
, columnDefinitionName =
QColumnName
{ columnNameInfo = (Range (Position 1 18 18) (Position 1 21 21))
, columnNameTable = None
, columnNameName = "bar"
}
, columnDefinitionType =
PrimitiveDataType (Range (Position 1 22 22) (Position 1 28 28)) "string" []
, columnDefinitionNull = Nothing
, columnDefinitionDefault = Nothing
, columnDefinitionExtra = Nothing
}
)
)
:|
[
(
ColumnOrConstraintColumn
( ColumnDefinition
{ columnDefinitionInfo = (Range (Position 1 30 30) (Position 1 40 40))
, columnDefinitionName =
QColumnName
{ columnNameInfo = (Range (Position 1 30 30) (Position 1 33 33))
, columnNameTable = None
, columnNameName = "baz"
}
, columnDefinitionType =
PrimitiveDataType (Range (Position 1 34 34) (Position 1 40 40)) "string" []
, columnDefinitionNull = Nothing
, columnDefinitionDefault = Nothing
, columnDefinitionExtra = Nothing
}
)
)
]
)
, createTableExtra =
Just HiveCreateTableExtra
{ hiveCreateTableExtraInfo = Range (Position 1 17 17) (Position 1 67 67)
, hiveCreateTableExtraTableProperties =
Just (
HiveMetadataProperties
{ hiveMetadataPropertiesInfo = Range (Position 1 42 42) (Position 1 67 67)
, hiveMetadataPropertiesProperties =
[
HiveMetadataProperty
{ hiveMetadataPropertyInfo = Range (Position 1 57 57) (Position 1 66 66)
, hiveMetadataPropertyKey = "bar"
, hiveMetadataPropertyValue = "1"
}
]
}
)
}
}
)
)
)
, parse "INSERT OVERWRITE TABLE foo PARTITION (bar) SELECT 1;" ~?= Right
( HiveStandardSqlStatement
( InsertStmt
( Insert
{ insertInfo =
(Range (Position 1 0 0) (Position 1 51 51))
, insertBehavior = InsertOverwritePartition
(Range (Position 1 7 7) (Position 1 16 16))
()
, insertTable = QTableName
(Range (Position 1 23 23) (Position 1 26 26))
Nothing
"foo"
, insertColumns = Nothing
, insertValues =
InsertSelectValues $ parseExactHelperSelect1 43
}
)
)
)
-- inverted FROM produces normal QuerySelects; the scope of an inverted
-- FROM extends only up to the UNION.
, parse "FROM foo SELECT 1 UNION SELECT 2;" ~?= Right
( HiveStandardSqlStatement
( QueryStmt
( QueryUnion
( Range (Position 1 18 18) (Position 1 23 23) )
( Distinct True )
Unused
( QuerySelect
( Range (Position 1 0 0) (Position 1 17 17) )
( makeSelect
( Range (Position 1 0 0) (Position 1 17 17) )
( SelectColumns
{ selectColumnsInfo = Range (Position 1 16 16) (Position 1 17 17)
, selectColumnsList =
[ SelectExpr
( Range (Position 1 16 16) (Position 1 17 17) )
[ ColumnAlias (Range (Position 1 16 16) (Position 1 17 17)) "_c0" (ColumnAliasId 1) ]
( ConstantExpr (Range (Position 1 16 16) (Position 1 17 17))
( NumericConstant (Range (Position 1 16 16) (Position 1 17 17)) "1")
)
]
}
)
)
{ selectFrom = Just
( SelectFrom (Range (Position 1 0 0) (Position 1 8 8))
[ TablishTable (Range (Position 1 5 5) (Position 1 8 8)) TablishAliasesNone
(QTableName (Range (Position 1 5 5) (Position 1 8 8)) Nothing "foo")
]
)
}
)
( QuerySelect
( Range (Position 1 24 24) (Position 1 32 32) )
( makeSelect
( Range (Position 1 24 24) (Position 1 32 32) )
( SelectColumns
{ selectColumnsInfo = Range (Position 1 31 31)(Position 1 32 32)
, selectColumnsList =
[ SelectExpr
( Range (Position 1 31 31) (Position 1 32 32) )
[ ColumnAlias (Range (Position 1 31 31) (Position 1 32 32)) "_c0" (ColumnAliasId 2) ]
( ConstantExpr (Range (Position 1 31 31) (Position 1 32 32))
( NumericConstant (Range (Position 1 31 31) (Position 1 32 32)) "2" )
)
]
}
)
)
)
)
)
)
-- treat schema qualifiers as column qualifiers
, parse "SELECT s.t FROM t;" ~?= Right
( HiveStandardSqlStatement
(QueryStmt
$ (flip evalState) (Position 1 0 0) $ do
select <- do
s <- get
modify $ advance "SELECT "
selectCols <- do
posS <- get
modify $ advance "s"
pos1 <- get
modify $ advance "."
pos2 <- get
modify $ advance "t"
posE <- get
let exprR = Range posS posE
colR = Range posS pos1
fieldR = Range pos2 posE
colId = ColumnAliasId 1
colAlias = ColumnAlias exprR "_c0" colId
colNameExpr = ColumnExpr colR (QColumnName colR Nothing "s")
fieldName = StructFieldName fieldR "t"
columnExpr = FieldAccessExpr exprR colNameExpr fieldName
selection = SelectExpr exprR [colAlias] columnExpr
pure $ SelectColumns exprR [selection]
modify $ advance " "
selectFrom <- do
posS <- get
modify $ advance "FROM "
pos1 <- get
modify $ advance "t"
posE <- get
let
fromR = Range posS posE
tableR = Range pos1 posE
table = TablishTable tableR TablishAliasesNone (QTableName tableR Nothing "t")
pure $ Just $ SelectFrom fromR [table]
e <- get
let selectInfo = Range s e
selectDistinct = notDistinct
selectWhere = Nothing
selectTimeseries = Nothing
selectGroup = Nothing
selectHaving = Nothing
selectNamedWindow = Nothing
pure Select{..}
pure (QuerySelect (getInfo select) select)
)
)
-- match table qualifiers to table names
, parse "SELECT t.c FROM t;" ~?= Right
( HiveStandardSqlStatement
(QueryStmt
$ (flip evalState) (Position 1 0 0) $ do
select <- do
s <- get
modify $ advance "SELECT "
selectCols <- do
posS <- get
modify $ advance "t"
pos1 <- get
modify $ advance "."
pos2 <- get
modify $ advance "c"
posE <- get
let exprR = Range posS posE
tableR = Range posS pos1
columnR = Range pos2 posE
tableName = QTableName tableR Nothing "t"
colId = ColumnAliasId 1
colAlias = ColumnAlias exprR "c" colId
colNameExpr = ColumnExpr exprR (QColumnName columnR (Just tableName) "c")
selection = SelectExpr exprR [colAlias] colNameExpr
pure $ SelectColumns exprR [selection]
modify $ advance " "
selectFrom <- do
posS <- get
modify $ advance "FROM "
pos1 <- get
modify $ advance "t"
posE <- get
let
fromR = Range posS posE
tableR = Range pos1 posE
table = TablishTable tableR TablishAliasesNone (QTableName tableR Nothing "t")
pure $ Just $ SelectFrom fromR [table]
e <- get
let selectInfo = Range s e
selectDistinct = notDistinct
selectWhere = Nothing
selectTimeseries = Nothing
selectGroup = Nothing
selectHaving = Nothing
selectNamedWindow = Nothing
pure Select{..}
pure (QuerySelect (getInfo select) select)
)
)
-- masochism at its best
, parse "SELECT t.c[1].x[2] FROM t;" ~?= Right
( HiveStandardSqlStatement
(QueryStmt
$ (flip evalState) (Position 1 0 0) $ do
select <- do
s <- get
modify $ advance "SELECT "
selectCols <- do
posS <- get
modify $ advance "t"
pos1 <- get
modify $ advance "."
pos2 <- get
modify $ advance "c"
pos3 <- get
modify $ advance "["
pos4 <- get
modify $ advance "1"
pos5 <- get
modify $ advance "]"
pos6 <- get
modify $ advance "."
pos7 <- get
modify $ advance "x"
pos8 <- get
modify $ advance "["
pos9 <- get
modify $ advance "2"
pos10 <- get
modify $ advance "]"
posE <- get
let exprR = Range posS posE
tableR = Range posS pos1
colR = Range pos2 pos3
colExpR = Range posS pos3
arr0R = Range posS pos6
arrIdx0R = Range pos4 pos5
fieldNameR = Range pos7 pos8
structR = Range posS pos8
arr1R = exprR
arrIdx1R = Range pos9 pos10
tableName = QTableName tableR Nothing "t"
colId = ColumnAliasId 1
colAlias = ColumnAlias exprR "_c0" colId
colNameExpr = ColumnExpr colExpR (QColumnName colR (Just tableName) "c")
arr0Expr = ArrayAccessExpr arr0R colNameExpr (ConstantExpr arrIdx0R (NumericConstant arrIdx0R "1"))
fieldName = StructFieldName fieldNameR "x"
structExpr = FieldAccessExpr structR arr0Expr fieldName
arr1Expr = ArrayAccessExpr arr1R structExpr (ConstantExpr arrIdx1R (NumericConstant arrIdx1R "2"))
selection = SelectExpr exprR [colAlias] arr1Expr
pure $ SelectColumns exprR [selection]
modify $ advance " "
selectFrom <- do
posS <- get
modify $ advance "FROM "
pos1 <- get
modify $ advance "t"
posE <- get
let
fromR = Range posS posE
tableR = Range pos1 posE
table = TablishTable tableR TablishAliasesNone (QTableName tableR Nothing "t")
pure $ Just $ SelectFrom fromR [table]
e <- get
let selectInfo = Range s e
selectDistinct = notDistinct
selectWhere = Nothing
selectTimeseries = Nothing
selectGroup = Nothing
selectHaving = Nothing
selectNamedWindow = Nothing
pure Select{..}
pure (QuerySelect (getInfo select) select)
)
)
-- Why would you ever try to array access a table?
--
-- If the table is a column. (ba-dum tsss)
, parse "SELECT t[0].c FROM t;" ~?= Right
( HiveStandardSqlStatement
(QueryStmt
$ (flip evalState) (Position 1 0 0) $ do
select <- do
s <- get
modify $ advance "SELECT "
selectCols <- do
posS <- get
modify $ advance "t"
pos1 <- get
modify $ advance "["
pos2 <- get
modify $ advance "0"
pos3 <- get
modify $ advance "]"
pos4 <- get
modify $ advance "."
pos5 <- get
modify $ advance "c"
posE <- get
let exprR = Range posS posE
colR = Range posS pos1
arr0R = Range posS pos4
arrIdx0R = Range pos2 pos3
fieldR = Range pos5 posE
colId = ColumnAliasId 1
colAlias = ColumnAlias exprR "_c0" colId
colNameExpr = ColumnExpr colR (QColumnName colR Nothing "t")
arr0Expr = ArrayAccessExpr arr0R colNameExpr (ConstantExpr arrIdx0R (NumericConstant arrIdx0R "0"))
fieldName = StructFieldName fieldR "c"
structExpr = FieldAccessExpr exprR arr0Expr fieldName
selection = SelectExpr exprR [colAlias] structExpr
pure $ SelectColumns exprR [selection]
modify $ advance " "
selectFrom <- do
posS <- get
modify $ advance "FROM "
pos1 <- get
modify $ advance "t"
posE <- get
let
fromR = Range posS posE
tableR = Range pos1 posE
table = TablishTable tableR TablishAliasesNone (QTableName tableR Nothing "t")
pure $ Just $ SelectFrom fromR [table]
e <- get
let selectInfo = Range s e
selectDistinct = notDistinct
selectWhere = Nothing
selectTimeseries = Nothing
selectGroup = Nothing
selectHaving = Nothing
selectNamedWindow = Nothing
pure Select{..}
pure (QuerySelect (getInfo select) select)
)
)
-- It's arrays all the way down
, parse "SELECT c[0][1] FROM t;" ~?= Right
( HiveStandardSqlStatement
(QueryStmt
$ (flip evalState) (Position 1 0 0) $ do
select <- do
s <- get
modify $ advance "SELECT "
selectCols <- do
posS <- get
modify $ advance "c"
pos1 <- get
modify $ advance "["
pos2 <- get
modify $ advance "0"
pos3 <- get
modify $ advance "]"
pos4 <- get
modify $ advance "["
pos5 <- get
modify $ advance "1"
pos6 <- get
modify $ advance "]"
posE <- get