/
DDLParserSuite.scala
2527 lines (2297 loc) · 93.7 KB
/
DDLParserSuite.scala
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.spark.sql.catalyst.parser
import java.util.Locale
import org.apache.spark.sql.AnalysisException
import org.apache.spark.sql.catalyst.analysis.{AnalysisTest, GlobalTempView, LocalTempView, PersistedView, UnresolvedAttribute, UnresolvedFieldName, UnresolvedFieldPosition, UnresolvedFunc, UnresolvedInlineTable, UnresolvedNamespace, UnresolvedRelation, UnresolvedStar, UnresolvedTable, UnresolvedTableOrView, UnresolvedView}
import org.apache.spark.sql.catalyst.catalog.{ArchiveResource, BucketSpec, FileResource, FunctionResource, JarResource}
import org.apache.spark.sql.catalyst.expressions.{EqualTo, Hex, Literal}
import org.apache.spark.sql.catalyst.plans.logical._
import org.apache.spark.sql.connector.catalog.TableChange.ColumnPosition.{after, first}
import org.apache.spark.sql.connector.expressions.{ApplyTransform, BucketTransform, DaysTransform, FieldReference, HoursTransform, IdentityTransform, LiteralValue, MonthsTransform, Transform, YearsTransform}
import org.apache.spark.sql.types.{IntegerType, LongType, StringType, StructType, TimestampType}
import org.apache.spark.unsafe.types.{CalendarInterval, UTF8String}
class DDLParserSuite extends AnalysisTest {
import CatalystSqlParser._
private def assertUnsupported(sql: String, containsThesePhrases: Seq[String] = Seq()): Unit = {
val e = intercept[ParseException] {
parsePlan(sql)
}
assert(e.getMessage.toLowerCase(Locale.ROOT).contains("operation not allowed"))
containsThesePhrases.foreach { p =>
assert(e.getMessage.toLowerCase(Locale.ROOT).contains(p.toLowerCase(Locale.ROOT)))
}
}
private def intercept(sqlCommand: String, messages: String*): Unit =
interceptParseException(parsePlan)(sqlCommand, messages: _*)
private def parseCompare(sql: String, expected: LogicalPlan): Unit = {
comparePlans(parsePlan(sql), expected, checkAnalysis = false)
}
test("create/replace table using - schema") {
val createSql = "CREATE TABLE my_tab(a INT COMMENT 'test', b STRING NOT NULL) USING parquet"
val replaceSql = "REPLACE TABLE my_tab(a INT COMMENT 'test', b STRING NOT NULL) USING parquet"
val expectedTableSpec = TableSpec(
Seq("my_tab"),
Some(new StructType()
.add("a", IntegerType, nullable = true, "test")
.add("b", StringType, nullable = false)),
Seq.empty[Transform],
None,
Map.empty[String, String],
Some("parquet"),
Map.empty[String, String],
None,
None,
None)
Seq(createSql, replaceSql).foreach { sql =>
testCreateOrReplaceDdl(sql, expectedTableSpec, expectedIfNotExists = false)
}
intercept("CREATE TABLE my_tab(a: INT COMMENT 'test', b: STRING) USING parquet",
"extraneous input ':'")
}
test("create/replace table - with IF NOT EXISTS") {
val sql = "CREATE TABLE IF NOT EXISTS my_tab(a INT, b STRING) USING parquet"
testCreateOrReplaceDdl(
sql,
TableSpec(
Seq("my_tab"),
Some(new StructType().add("a", IntegerType).add("b", StringType)),
Seq.empty[Transform],
None,
Map.empty[String, String],
Some("parquet"),
Map.empty[String, String],
None,
None,
None),
expectedIfNotExists = true)
}
test("create/replace table - with partitioned by") {
val createSql = "CREATE TABLE my_tab(a INT comment 'test', b STRING) " +
"USING parquet PARTITIONED BY (a)"
val replaceSql = "REPLACE TABLE my_tab(a INT comment 'test', b STRING) " +
"USING parquet PARTITIONED BY (a)"
val expectedTableSpec = TableSpec(
Seq("my_tab"),
Some(new StructType()
.add("a", IntegerType, nullable = true, "test")
.add("b", StringType)),
Seq(IdentityTransform(FieldReference("a"))),
None,
Map.empty[String, String],
Some("parquet"),
Map.empty[String, String],
None,
None,
None)
Seq(createSql, replaceSql).foreach { sql =>
testCreateOrReplaceDdl(sql, expectedTableSpec, expectedIfNotExists = false)
}
}
test("create/replace table - partitioned by transforms") {
val createSql =
"""
|CREATE TABLE my_tab (a INT, b STRING, ts TIMESTAMP) USING parquet
|PARTITIONED BY (
| a,
| bucket(16, b),
| years(ts),
| months(ts),
| days(ts),
| hours(ts),
| foo(a, "bar", 34))
""".stripMargin
val replaceSql =
"""
|REPLACE TABLE my_tab (a INT, b STRING, ts TIMESTAMP) USING parquet
|PARTITIONED BY (
| a,
| bucket(16, b),
| years(ts),
| months(ts),
| days(ts),
| hours(ts),
| foo(a, "bar", 34))
""".stripMargin
val expectedTableSpec = TableSpec(
Seq("my_tab"),
Some(new StructType()
.add("a", IntegerType)
.add("b", StringType)
.add("ts", TimestampType)),
Seq(
IdentityTransform(FieldReference("a")),
BucketTransform(LiteralValue(16, IntegerType), Seq(FieldReference("b"))),
YearsTransform(FieldReference("ts")),
MonthsTransform(FieldReference("ts")),
DaysTransform(FieldReference("ts")),
HoursTransform(FieldReference("ts")),
ApplyTransform("foo", Seq(
FieldReference("a"),
LiteralValue(UTF8String.fromString("bar"), StringType),
LiteralValue(34, IntegerType)))),
None,
Map.empty[String, String],
Some("parquet"),
Map.empty[String, String],
None,
None,
None)
Seq(createSql, replaceSql).foreach { sql =>
testCreateOrReplaceDdl(sql, expectedTableSpec, expectedIfNotExists = false)
}
}
test("create/replace table - with bucket") {
val createSql = "CREATE TABLE my_tab(a INT, b STRING) USING parquet " +
"CLUSTERED BY (a) SORTED BY (b) INTO 5 BUCKETS"
val replaceSql = "REPLACE TABLE my_tab(a INT, b STRING) USING parquet " +
"CLUSTERED BY (a) SORTED BY (b) INTO 5 BUCKETS"
val expectedTableSpec = TableSpec(
Seq("my_tab"),
Some(new StructType().add("a", IntegerType).add("b", StringType)),
Seq.empty[Transform],
Some(BucketSpec(5, Seq("a"), Seq("b"))),
Map.empty[String, String],
Some("parquet"),
Map.empty[String, String],
None,
None,
None)
Seq(createSql, replaceSql).foreach { sql =>
testCreateOrReplaceDdl(sql, expectedTableSpec, expectedIfNotExists = false)
}
}
test("create/replace table - with comment") {
val createSql = "CREATE TABLE my_tab(a INT, b STRING) USING parquet COMMENT 'abc'"
val replaceSql = "REPLACE TABLE my_tab(a INT, b STRING) USING parquet COMMENT 'abc'"
val expectedTableSpec = TableSpec(
Seq("my_tab"),
Some(new StructType().add("a", IntegerType).add("b", StringType)),
Seq.empty[Transform],
None,
Map.empty[String, String],
Some("parquet"),
Map.empty[String, String],
None,
Some("abc"),
None)
Seq(createSql, replaceSql).foreach{ sql =>
testCreateOrReplaceDdl(sql, expectedTableSpec, expectedIfNotExists = false)
}
}
test("create/replace table - with table properties") {
val createSql = "CREATE TABLE my_tab(a INT, b STRING) USING parquet" +
" TBLPROPERTIES('test' = 'test')"
val replaceSql = "REPLACE TABLE my_tab(a INT, b STRING) USING parquet" +
" TBLPROPERTIES('test' = 'test')"
val expectedTableSpec = TableSpec(
Seq("my_tab"),
Some(new StructType().add("a", IntegerType).add("b", StringType)),
Seq.empty[Transform],
None,
Map("test" -> "test"),
Some("parquet"),
Map.empty[String, String],
None,
None,
None)
Seq(createSql, replaceSql).foreach { sql =>
testCreateOrReplaceDdl(sql, expectedTableSpec, expectedIfNotExists = false)
}
}
test("create/replace table - with location") {
val createSql = "CREATE TABLE my_tab(a INT, b STRING) USING parquet LOCATION '/tmp/file'"
val replaceSql = "REPLACE TABLE my_tab(a INT, b STRING) USING parquet LOCATION '/tmp/file'"
val expectedTableSpec = TableSpec(
Seq("my_tab"),
Some(new StructType().add("a", IntegerType).add("b", StringType)),
Seq.empty[Transform],
None,
Map.empty[String, String],
Some("parquet"),
Map.empty[String, String],
Some("/tmp/file"),
None,
None)
Seq(createSql, replaceSql).foreach { sql =>
testCreateOrReplaceDdl(sql, expectedTableSpec, expectedIfNotExists = false)
}
}
test("create/replace table - byte length literal table name") {
val createSql = "CREATE TABLE 1m.2g(a INT) USING parquet"
val replaceSql = "REPLACE TABLE 1m.2g(a INT) USING parquet"
val expectedTableSpec = TableSpec(
Seq("1m", "2g"),
Some(new StructType().add("a", IntegerType)),
Seq.empty[Transform],
None,
Map.empty[String, String],
Some("parquet"),
Map.empty[String, String],
None,
None,
None)
Seq(createSql, replaceSql).foreach { sql =>
testCreateOrReplaceDdl(sql, expectedTableSpec, expectedIfNotExists = false)
}
}
test("create/replace table - partition column definitions") {
val createSql = "CREATE TABLE my_tab (id bigint) PARTITIONED BY (part string)"
val replaceSql = "REPLACE TABLE my_tab (id bigint) PARTITIONED BY (part string)"
val expectedTableSpec = TableSpec(
Seq("my_tab"),
Some(new StructType().add("id", LongType).add("part", StringType)),
Seq(IdentityTransform(FieldReference("part"))),
None,
Map.empty[String, String],
None,
Map.empty[String, String],
None,
None,
None)
Seq(createSql, replaceSql).foreach { sql =>
testCreateOrReplaceDdl(sql, expectedTableSpec, expectedIfNotExists = false)
}
}
test("create/replace table - empty columns list") {
val createSql = "CREATE TABLE my_tab PARTITIONED BY (part string)"
val replaceSql = "REPLACE TABLE my_tab PARTITIONED BY (part string)"
val expectedTableSpec = TableSpec(
Seq("my_tab"),
Some(new StructType().add("part", StringType)),
Seq(IdentityTransform(FieldReference("part"))),
None,
Map.empty[String, String],
None,
Map.empty[String, String],
None,
None,
None)
Seq(createSql, replaceSql).foreach { sql =>
testCreateOrReplaceDdl(sql, expectedTableSpec, expectedIfNotExists = false)
}
}
test("create/replace table - using with partition column definitions") {
val createSql = "CREATE TABLE my_tab (id bigint) USING parquet PARTITIONED BY (part string)"
val replaceSql = "REPLACE TABLE my_tab (id bigint) USING parquet PARTITIONED BY (part string)"
val expectedTableSpec = TableSpec(
Seq("my_tab"),
Some(new StructType().add("id", LongType).add("part", StringType)),
Seq(IdentityTransform(FieldReference("part"))),
None,
Map.empty[String, String],
Some("parquet"),
Map.empty[String, String],
None,
None,
None)
Seq(createSql, replaceSql).foreach { sql =>
testCreateOrReplaceDdl(sql, expectedTableSpec, expectedIfNotExists = false)
}
}
test("create/replace table - mixed partition references and column definitions") {
val createSql = "CREATE TABLE my_tab (id bigint, p1 string) PARTITIONED BY (p1, p2 string)"
val replaceSql = createSql.replaceFirst("CREATE", "REPLACE")
Seq(createSql, replaceSql).foreach { sql =>
assertUnsupported(sql, Seq(
"PARTITION BY: Cannot mix partition expressions and partition columns",
"Expressions: p1",
"Columns: p2 string"))
}
val createSqlWithExpr =
"CREATE TABLE my_tab (id bigint, p1 string) PARTITIONED BY (p2 string, truncate(p1, 16))"
val replaceSqlWithExpr = createSqlWithExpr.replaceFirst("CREATE", "REPLACE")
Seq(createSqlWithExpr, replaceSqlWithExpr).foreach { sql =>
assertUnsupported(sql, Seq(
"PARTITION BY: Cannot mix partition expressions and partition columns",
"Expressions: truncate(p1, 16)",
"Columns: p2 string"))
}
}
test("create/replace table - stored as") {
val createSql =
"""CREATE TABLE my_tab (id bigint)
|PARTITIONED BY (part string)
|STORED AS parquet
""".stripMargin
val replaceSql = createSql.replaceFirst("CREATE", "REPLACE")
val expectedTableSpec = TableSpec(
Seq("my_tab"),
Some(new StructType().add("id", LongType).add("part", StringType)),
Seq(IdentityTransform(FieldReference("part"))),
None,
Map.empty[String, String],
None,
Map.empty[String, String],
None,
None,
Some(SerdeInfo(storedAs = Some("parquet"))))
Seq(createSql, replaceSql).foreach { sql =>
testCreateOrReplaceDdl(sql, expectedTableSpec, expectedIfNotExists = false)
}
}
test("create/replace table - stored as format with serde") {
Seq("sequencefile", "textfile", "rcfile").foreach { format =>
val createSql =
s"""CREATE TABLE my_tab (id bigint)
|PARTITIONED BY (part string)
|STORED AS $format
|ROW FORMAT SERDE 'customSerde'
|WITH SERDEPROPERTIES ('prop'='value')
""".stripMargin
val replaceSql = createSql.replaceFirst("CREATE", "REPLACE")
val expectedTableSpec = TableSpec(
Seq("my_tab"),
Some(new StructType().add("id", LongType).add("part", StringType)),
Seq(IdentityTransform(FieldReference("part"))),
None,
Map.empty[String, String],
None,
Map.empty[String, String],
None,
None,
Some(SerdeInfo(storedAs = Some(format), serde = Some("customSerde"), serdeProperties = Map(
"prop" -> "value"
))))
Seq(createSql, replaceSql).foreach { sql =>
testCreateOrReplaceDdl(sql, expectedTableSpec, expectedIfNotExists = false)
}
}
val createSql =
s"""CREATE TABLE my_tab (id bigint)
|PARTITIONED BY (part string)
|STORED AS otherFormat
|ROW FORMAT SERDE 'customSerde'
|WITH SERDEPROPERTIES ('prop'='value')
""".stripMargin
val replaceSql = createSql.replaceFirst("CREATE", "REPLACE")
Seq(createSql, replaceSql).foreach { sql =>
assertUnsupported(sql, Seq("ROW FORMAT SERDE is incompatible with format 'otherFormat'"))
}
}
test("create/replace table - stored as format with delimited clauses") {
val createSql =
s"""CREATE TABLE my_tab (id bigint)
|PARTITIONED BY (part string)
|STORED AS textfile
|ROW FORMAT DELIMITED
|FIELDS TERMINATED BY ',' ESCAPED BY '\\\\' -- double escape for Scala and for SQL
|COLLECTION ITEMS TERMINATED BY '#'
|MAP KEYS TERMINATED BY '='
|LINES TERMINATED BY '\\n'
""".stripMargin
val replaceSql = createSql.replaceFirst("CREATE", "REPLACE")
val expectedTableSpec = TableSpec(
Seq("my_tab"),
Some(new StructType().add("id", LongType).add("part", StringType)),
Seq(IdentityTransform(FieldReference("part"))),
None,
Map.empty[String, String],
None,
Map.empty[String, String],
None,
None,
Some(SerdeInfo(storedAs = Some("textfile"), serdeProperties = Map(
"field.delim" -> ",", "serialization.format" -> ",", "escape.delim" -> "\\",
"colelction.delim" -> "#", "mapkey.delim" -> "=", "line.delim" -> "\n"
))))
Seq(createSql, replaceSql).foreach { sql =>
testCreateOrReplaceDdl(sql, expectedTableSpec, expectedIfNotExists = false)
}
val createFailSql =
s"""CREATE TABLE my_tab (id bigint)
|PARTITIONED BY (part string)
|STORED AS otherFormat
|ROW FORMAT DELIMITED
|FIELDS TERMINATED BY ','
""".stripMargin
val replaceFailSql = createFailSql.replaceFirst("CREATE", "REPLACE")
Seq(createFailSql, replaceFailSql).foreach { sql =>
assertUnsupported(sql, Seq(
"ROW FORMAT DELIMITED is only compatible with 'textfile', not 'otherFormat'"))
}
}
test("create/replace table - stored as inputformat/outputformat") {
val createSql =
"""CREATE TABLE my_tab (id bigint)
|PARTITIONED BY (part string)
|STORED AS INPUTFORMAT 'inFormat' OUTPUTFORMAT 'outFormat'
""".stripMargin
val replaceSql = createSql.replaceFirst("CREATE", "REPLACE")
val expectedTableSpec = TableSpec(
Seq("my_tab"),
Some(new StructType().add("id", LongType).add("part", StringType)),
Seq(IdentityTransform(FieldReference("part"))),
None,
Map.empty[String, String],
None,
Map.empty[String, String],
None,
None,
Some(SerdeInfo(formatClasses = Some(FormatClasses("inFormat", "outFormat")))))
Seq(createSql, replaceSql).foreach { sql =>
testCreateOrReplaceDdl(sql, expectedTableSpec, expectedIfNotExists = false)
}
}
test("create/replace table - stored as inputformat/outputformat with serde") {
val createSql =
"""CREATE TABLE my_tab (id bigint)
|PARTITIONED BY (part string)
|STORED AS INPUTFORMAT 'inFormat' OUTPUTFORMAT 'outFormat'
|ROW FORMAT SERDE 'customSerde'
""".stripMargin
val replaceSql = createSql.replaceFirst("CREATE", "REPLACE")
val expectedTableSpec = TableSpec(
Seq("my_tab"),
Some(new StructType().add("id", LongType).add("part", StringType)),
Seq(IdentityTransform(FieldReference("part"))),
None,
Map.empty[String, String],
None,
Map.empty[String, String],
None,
None,
Some(SerdeInfo(
formatClasses = Some(FormatClasses("inFormat", "outFormat")),
serde = Some("customSerde"))))
Seq(createSql, replaceSql).foreach { sql =>
testCreateOrReplaceDdl(sql, expectedTableSpec, expectedIfNotExists = false)
}
}
test("create/replace table - using with stored as") {
val createSql =
"""CREATE TABLE my_tab (id bigint, part string)
|USING parquet
|STORED AS parquet
""".stripMargin
val replaceSql = createSql.replaceFirst("CREATE", "REPLACE")
Seq(createSql, replaceSql).foreach { sql =>
assertUnsupported(sql, Seq("CREATE TABLE ... USING ... STORED AS"))
}
}
test("create/replace table - using with row format serde") {
val createSql =
"""CREATE TABLE my_tab (id bigint, part string)
|USING parquet
|ROW FORMAT SERDE 'customSerde'
""".stripMargin
val replaceSql = createSql.replaceFirst("CREATE", "REPLACE")
Seq(createSql, replaceSql).foreach { sql =>
assertUnsupported(sql, Seq("CREATE TABLE ... USING ... ROW FORMAT SERDE"))
}
}
test("create/replace table - using with row format delimited") {
val createSql =
"""CREATE TABLE my_tab (id bigint, part string)
|USING parquet
|ROW FORMAT DELIMITED FIELDS TERMINATED BY ','
""".stripMargin
val replaceSql = createSql.replaceFirst("CREATE", "REPLACE")
Seq(createSql, replaceSql).foreach { sql =>
assertUnsupported(sql, Seq("CREATE TABLE ... USING ... ROW FORMAT DELIMITED"))
}
}
test("create/replace table - stored by") {
val createSql =
"""CREATE TABLE my_tab (id bigint, p1 string)
|STORED BY 'handler'
""".stripMargin
val replaceSql = createSql.replaceFirst("CREATE", "REPLACE")
Seq(createSql, replaceSql).foreach { sql =>
assertUnsupported(sql, Seq("stored by"))
}
}
test("Unsupported skew clause - create/replace table") {
intercept("CREATE TABLE my_tab (id bigint) SKEWED BY (id) ON (1,2,3)",
"CREATE TABLE ... SKEWED BY")
intercept("REPLACE TABLE my_tab (id bigint) SKEWED BY (id) ON (1,2,3)",
"CREATE TABLE ... SKEWED BY")
}
test("Duplicate clauses - create/replace table") {
def createTableHeader(duplicateClause: String): String = {
s"CREATE TABLE my_tab(a INT, b STRING) $duplicateClause $duplicateClause"
}
def replaceTableHeader(duplicateClause: String): String = {
s"CREATE TABLE my_tab(a INT, b STRING) $duplicateClause $duplicateClause"
}
intercept(createTableHeader("TBLPROPERTIES('test' = 'test2')"),
"Found duplicate clauses: TBLPROPERTIES")
intercept(createTableHeader("LOCATION '/tmp/file'"),
"Found duplicate clauses: LOCATION")
intercept(createTableHeader("COMMENT 'a table'"),
"Found duplicate clauses: COMMENT")
intercept(createTableHeader("CLUSTERED BY(b) INTO 256 BUCKETS"),
"Found duplicate clauses: CLUSTERED BY")
intercept(createTableHeader("PARTITIONED BY (b)"),
"Found duplicate clauses: PARTITIONED BY")
intercept(createTableHeader("PARTITIONED BY (c int)"),
"Found duplicate clauses: PARTITIONED BY")
intercept(createTableHeader("STORED AS parquet"),
"Found duplicate clauses: STORED AS")
intercept(createTableHeader("STORED AS INPUTFORMAT 'in' OUTPUTFORMAT 'out'"),
"Found duplicate clauses: STORED AS")
intercept(createTableHeader("ROW FORMAT SERDE 'serde'"),
"Found duplicate clauses: ROW FORMAT")
intercept(replaceTableHeader("TBLPROPERTIES('test' = 'test2')"),
"Found duplicate clauses: TBLPROPERTIES")
intercept(replaceTableHeader("LOCATION '/tmp/file'"),
"Found duplicate clauses: LOCATION")
intercept(replaceTableHeader("COMMENT 'a table'"),
"Found duplicate clauses: COMMENT")
intercept(replaceTableHeader("CLUSTERED BY(b) INTO 256 BUCKETS"),
"Found duplicate clauses: CLUSTERED BY")
intercept(replaceTableHeader("PARTITIONED BY (b)"),
"Found duplicate clauses: PARTITIONED BY")
intercept(replaceTableHeader("PARTITIONED BY (c int)"),
"Found duplicate clauses: PARTITIONED BY")
intercept(replaceTableHeader("STORED AS parquet"),
"Found duplicate clauses: STORED AS")
intercept(replaceTableHeader("STORED AS INPUTFORMAT 'in' OUTPUTFORMAT 'out'"),
"Found duplicate clauses: STORED AS")
intercept(replaceTableHeader("ROW FORMAT SERDE 'serde'"),
"Found duplicate clauses: ROW FORMAT")
}
test("support for other types in OPTIONS") {
val createSql =
"""
|CREATE TABLE table_name USING json
|OPTIONS (a 1, b 0.1, c TRUE)
""".stripMargin
val replaceSql =
"""
|REPLACE TABLE table_name USING json
|OPTIONS (a 1, b 0.1, c TRUE)
""".stripMargin
Seq(createSql, replaceSql).foreach { sql =>
testCreateOrReplaceDdl(
sql,
TableSpec(
Seq("table_name"),
Some(new StructType),
Seq.empty[Transform],
Option.empty[BucketSpec],
Map.empty[String, String],
Some("json"),
Map("a" -> "1", "b" -> "0.1", "c" -> "true"),
None,
None,
None),
expectedIfNotExists = false)
}
}
test("Test CTAS against native tables") {
val s1 =
"""
|CREATE TABLE IF NOT EXISTS mydb.page_view
|USING parquet
|COMMENT 'This is the staging page view table'
|LOCATION '/user/external/page_view'
|TBLPROPERTIES ('p1'='v1', 'p2'='v2')
|AS SELECT * FROM src
""".stripMargin
val s2 =
"""
|CREATE TABLE IF NOT EXISTS mydb.page_view
|USING parquet
|LOCATION '/user/external/page_view'
|COMMENT 'This is the staging page view table'
|TBLPROPERTIES ('p1'='v1', 'p2'='v2')
|AS SELECT * FROM src
""".stripMargin
val s3 =
"""
|CREATE TABLE IF NOT EXISTS mydb.page_view
|USING parquet
|COMMENT 'This is the staging page view table'
|LOCATION '/user/external/page_view'
|TBLPROPERTIES ('p1'='v1', 'p2'='v2')
|AS SELECT * FROM src
""".stripMargin
val s4 =
"""
|REPLACE TABLE mydb.page_view
|USING parquet
|COMMENT 'This is the staging page view table'
|LOCATION '/user/external/page_view'
|TBLPROPERTIES ('p1'='v1', 'p2'='v2')
|AS SELECT * FROM src
""".stripMargin
val expectedTableSpec = TableSpec(
Seq("mydb", "page_view"),
None,
Seq.empty[Transform],
None,
Map("p1" -> "v1", "p2" -> "v2"),
Some("parquet"),
Map.empty[String, String],
Some("/user/external/page_view"),
Some("This is the staging page view table"),
None)
Seq(s1, s2, s3, s4).foreach { sql =>
testCreateOrReplaceDdl(sql, expectedTableSpec, expectedIfNotExists = true)
}
}
test("drop view") {
val cmd = "DROP VIEW"
val hint = Some("Please use DROP TABLE instead.")
parseCompare(s"DROP VIEW testcat.db.view",
DropView(UnresolvedView(Seq("testcat", "db", "view"), cmd, true, hint), ifExists = false))
parseCompare(s"DROP VIEW db.view",
DropView(UnresolvedView(Seq("db", "view"), cmd, true, hint), ifExists = false))
parseCompare(s"DROP VIEW IF EXISTS db.view",
DropView(UnresolvedView(Seq("db", "view"), cmd, true, hint), ifExists = true))
parseCompare(s"DROP VIEW view",
DropView(UnresolvedView(Seq("view"), cmd, true, hint), ifExists = false))
parseCompare(s"DROP VIEW IF EXISTS view",
DropView(UnresolvedView(Seq("view"), cmd, true, hint), ifExists = true))
}
private def testCreateOrReplaceDdl(
sqlStatement: String,
tableSpec: TableSpec,
expectedIfNotExists: Boolean): Unit = {
val parsedPlan = parsePlan(sqlStatement)
val newTableToken = sqlStatement.split(" ")(0).trim.toUpperCase(Locale.ROOT)
parsedPlan match {
case create: CreateTableStatement if newTableToken == "CREATE" =>
assert(create.ifNotExists == expectedIfNotExists)
case ctas: CreateTableAsSelectStatement if newTableToken == "CREATE" =>
assert(ctas.ifNotExists == expectedIfNotExists)
case replace: ReplaceTableStatement if newTableToken == "REPLACE" =>
case replace: ReplaceTableAsSelectStatement if newTableToken == "REPLACE" =>
case other =>
fail("First token in statement does not match the expected parsed plan; CREATE TABLE" +
" should create a CreateTableStatement, and REPLACE TABLE should create a" +
s" ReplaceTableStatement. Statement: $sqlStatement, plan type:" +
s" ${parsedPlan.getClass.getName}.")
}
assert(TableSpec(parsedPlan) === tableSpec)
}
// ALTER VIEW view_name SET TBLPROPERTIES ('comment' = new_comment);
// ALTER VIEW view_name UNSET TBLPROPERTIES [IF EXISTS] ('comment', 'key');
test("alter view: alter view properties") {
val sql1_view = "ALTER VIEW table_name SET TBLPROPERTIES ('test' = 'test', " +
"'comment' = 'new_comment')"
val sql2_view = "ALTER VIEW table_name UNSET TBLPROPERTIES ('comment', 'test')"
val sql3_view = "ALTER VIEW table_name UNSET TBLPROPERTIES IF EXISTS ('comment', 'test')"
val hint = Some("Please use ALTER TABLE instead.")
comparePlans(parsePlan(sql1_view),
SetViewProperties(
UnresolvedView(Seq("table_name"), "ALTER VIEW ... SET TBLPROPERTIES", false, hint),
Map("test" -> "test", "comment" -> "new_comment")))
comparePlans(parsePlan(sql2_view),
UnsetViewProperties(
UnresolvedView(Seq("table_name"), "ALTER VIEW ... UNSET TBLPROPERTIES", false, hint),
Seq("comment", "test"),
ifExists = false))
comparePlans(parsePlan(sql3_view),
UnsetViewProperties(
UnresolvedView(Seq("table_name"), "ALTER VIEW ... UNSET TBLPROPERTIES", false, hint),
Seq("comment", "test"),
ifExists = true))
}
// ALTER TABLE table_name SET TBLPROPERTIES ('comment' = new_comment);
// ALTER TABLE table_name UNSET TBLPROPERTIES [IF EXISTS] ('comment', 'key');
test("alter table: alter table properties") {
val sql1_table = "ALTER TABLE table_name SET TBLPROPERTIES ('test' = 'test', " +
"'comment' = 'new_comment')"
val sql2_table = "ALTER TABLE table_name UNSET TBLPROPERTIES ('comment', 'test')"
val sql3_table = "ALTER TABLE table_name UNSET TBLPROPERTIES IF EXISTS ('comment', 'test')"
val hint = Some("Please use ALTER VIEW instead.")
comparePlans(
parsePlan(sql1_table),
SetTableProperties(
UnresolvedTable(Seq("table_name"), "ALTER TABLE ... SET TBLPROPERTIES", hint),
Map("test" -> "test", "comment" -> "new_comment")))
comparePlans(
parsePlan(sql2_table),
UnsetTableProperties(
UnresolvedTable(Seq("table_name"), "ALTER TABLE ... UNSET TBLPROPERTIES", hint),
Seq("comment", "test"),
ifExists = false))
comparePlans(
parsePlan(sql3_table),
UnsetTableProperties(
UnresolvedTable(Seq("table_name"), "ALTER TABLE ... UNSET TBLPROPERTIES", hint),
Seq("comment", "test"),
ifExists = true))
}
test("alter table: add column") {
comparePlans(
parsePlan("ALTER TABLE table_name ADD COLUMN x int"),
AlterTableAddColumnsStatement(Seq("table_name"), Seq(
QualifiedColType(Seq("x"), IntegerType, true, None, None)
)))
}
test("alter table: add multiple columns") {
comparePlans(
parsePlan("ALTER TABLE table_name ADD COLUMNS x int, y string"),
AlterTableAddColumnsStatement(Seq("table_name"), Seq(
QualifiedColType(Seq("x"), IntegerType, true, None, None),
QualifiedColType(Seq("y"), StringType, true, None, None)
)))
}
test("alter table: add column with COLUMNS") {
comparePlans(
parsePlan("ALTER TABLE table_name ADD COLUMNS x int"),
AlterTableAddColumnsStatement(Seq("table_name"), Seq(
QualifiedColType(Seq("x"), IntegerType, true, None, None)
)))
}
test("alter table: add column with COLUMNS (...)") {
comparePlans(
parsePlan("ALTER TABLE table_name ADD COLUMNS (x int)"),
AlterTableAddColumnsStatement(Seq("table_name"), Seq(
QualifiedColType(Seq("x"), IntegerType, true, None, None)
)))
}
test("alter table: add column with COLUMNS (...) and COMMENT") {
comparePlans(
parsePlan("ALTER TABLE table_name ADD COLUMNS (x int COMMENT 'doc')"),
AlterTableAddColumnsStatement(Seq("table_name"), Seq(
QualifiedColType(Seq("x"), IntegerType, true, Some("doc"), None)
)))
}
test("alter table: add non-nullable column") {
comparePlans(
parsePlan("ALTER TABLE table_name ADD COLUMN x int NOT NULL"),
AlterTableAddColumnsStatement(Seq("table_name"), Seq(
QualifiedColType(Seq("x"), IntegerType, false, None, None)
)))
}
test("alter table: add column with COMMENT") {
comparePlans(
parsePlan("ALTER TABLE table_name ADD COLUMN x int COMMENT 'doc'"),
AlterTableAddColumnsStatement(Seq("table_name"), Seq(
QualifiedColType(Seq("x"), IntegerType, true, Some("doc"), None)
)))
}
test("alter table: add column with position") {
comparePlans(
parsePlan("ALTER TABLE table_name ADD COLUMN x int FIRST"),
AlterTableAddColumnsStatement(Seq("table_name"), Seq(
QualifiedColType(Seq("x"), IntegerType, true, None, Some(first()))
)))
comparePlans(
parsePlan("ALTER TABLE table_name ADD COLUMN x int AFTER y"),
AlterTableAddColumnsStatement(Seq("table_name"), Seq(
QualifiedColType(Seq("x"), IntegerType, true, None, Some(after("y")))
)))
}
test("alter table: add column with nested column name") {
comparePlans(
parsePlan("ALTER TABLE table_name ADD COLUMN x.y.z int COMMENT 'doc'"),
AlterTableAddColumnsStatement(Seq("table_name"), Seq(
QualifiedColType(Seq("x", "y", "z"), IntegerType, true, Some("doc"), None)
)))
}
test("alter table: add multiple columns with nested column name") {
comparePlans(
parsePlan("ALTER TABLE table_name ADD COLUMN x.y.z int COMMENT 'doc', a.b string FIRST"),
AlterTableAddColumnsStatement(Seq("table_name"), Seq(
QualifiedColType(Seq("x", "y", "z"), IntegerType, true, Some("doc"), None),
QualifiedColType(Seq("a", "b"), StringType, true, None, Some(first()))
)))
}
test("alter table: set location") {
val hint = Some("Please use ALTER VIEW instead.")
comparePlans(
parsePlan("ALTER TABLE a.b.c SET LOCATION 'new location'"),
SetTableLocation(
UnresolvedTable(Seq("a", "b", "c"), "ALTER TABLE ... SET LOCATION ...", hint),
None,
"new location"))
comparePlans(
parsePlan("ALTER TABLE a.b.c PARTITION(ds='2017-06-10') SET LOCATION 'new location'"),
SetTableLocation(
UnresolvedTable(Seq("a", "b", "c"), "ALTER TABLE ... SET LOCATION ...", hint),
Some(Map("ds" -> "2017-06-10")),
"new location"))
}
test("alter table: rename column") {
comparePlans(
parsePlan("ALTER TABLE table_name RENAME COLUMN a.b.c TO d"),
AlterTableRenameColumn(
UnresolvedTable(Seq("table_name"), "ALTER TABLE ... RENAME COLUMN", None),
UnresolvedFieldName(Seq("a", "b", "c")),
"d"))
}
test("alter table: update column type using ALTER") {
comparePlans(
parsePlan("ALTER TABLE table_name ALTER COLUMN a.b.c TYPE bigint"),
AlterTableAlterColumn(
UnresolvedTable(Seq("table_name"), "ALTER TABLE ... ALTER COLUMN", None),
UnresolvedFieldName(Seq("a", "b", "c")),
Some(LongType),
None,
None,
None))
}
test("alter table: update column type invalid type") {
val msg = intercept[ParseException] {
parsePlan("ALTER TABLE table_name ALTER COLUMN a.b.c TYPE bad_type")
}.getMessage
assert(msg.contains("DataType bad_type is not supported"))
}
test("alter table: update column type") {
comparePlans(
parsePlan("ALTER TABLE table_name CHANGE COLUMN a.b.c TYPE bigint"),
AlterTableAlterColumn(
UnresolvedTable(Seq("table_name"), "ALTER TABLE ... CHANGE COLUMN", None),
UnresolvedFieldName(Seq("a", "b", "c")),
Some(LongType),
None,
None,
None))
}
test("alter table: update column comment") {
comparePlans(
parsePlan("ALTER TABLE table_name CHANGE COLUMN a.b.c COMMENT 'new comment'"),
AlterTableAlterColumn(
UnresolvedTable(Seq("table_name"), "ALTER TABLE ... CHANGE COLUMN", None),
UnresolvedFieldName(Seq("a", "b", "c")),
None,
None,
Some("new comment"),
None))
}
test("alter table: update column position") {
comparePlans(
parsePlan("ALTER TABLE table_name CHANGE COLUMN a.b.c FIRST"),
AlterTableAlterColumn(
UnresolvedTable(Seq("table_name"), "ALTER TABLE ... CHANGE COLUMN", None),
UnresolvedFieldName(Seq("a", "b", "c")),
None,
None,
None,
Some(UnresolvedFieldPosition(Seq("a", "b", "c"), first()))))
}
test("alter table: multiple property changes are not allowed") {
intercept[ParseException] {
parsePlan("ALTER TABLE table_name ALTER COLUMN a.b.c " +
"TYPE bigint COMMENT 'new comment'")}
intercept[ParseException] {
parsePlan("ALTER TABLE table_name ALTER COLUMN a.b.c " +
"TYPE bigint COMMENT AFTER d")}
intercept[ParseException] {
parsePlan("ALTER TABLE table_name ALTER COLUMN a.b.c " +
"TYPE bigint COMMENT 'new comment' AFTER d")}
}
test("alter table: SET/DROP NOT NULL") {
comparePlans(
parsePlan("ALTER TABLE table_name ALTER COLUMN a.b.c SET NOT NULL"),
AlterTableAlterColumn(
UnresolvedTable(Seq("table_name"), "ALTER TABLE ... ALTER COLUMN", None),
UnresolvedFieldName(Seq("a", "b", "c")),
None,
Some(false),
None,
None))
comparePlans(
parsePlan("ALTER TABLE table_name ALTER COLUMN a.b.c DROP NOT NULL"),
AlterTableAlterColumn(
UnresolvedTable(Seq("table_name"), "ALTER TABLE ... ALTER COLUMN", None),
UnresolvedFieldName(Seq("a", "b", "c")),
None,
Some(true),
None,
None))
}
test("alter table: drop column") {
comparePlans(
parsePlan("ALTER TABLE table_name DROP COLUMN a.b.c"),
AlterTableDropColumns(
UnresolvedTable(Seq("table_name"), "ALTER TABLE ... DROP COLUMNS", None),
Seq(UnresolvedFieldName(Seq("a", "b", "c")))))
}
test("alter table: drop multiple columns") {