-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
TestHiveIntegrationSmokeTest.java
1926 lines (1631 loc) · 87 KB
/
TestHiveIntegrationSmokeTest.java
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 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 com.facebook.presto.hive;
import com.facebook.presto.Session;
import com.facebook.presto.metadata.Metadata;
import com.facebook.presto.metadata.QualifiedObjectName;
import com.facebook.presto.metadata.TableHandle;
import com.facebook.presto.metadata.TableLayout;
import com.facebook.presto.metadata.TableLayoutResult;
import com.facebook.presto.metadata.TableMetadata;
import com.facebook.presto.spi.ColumnMetadata;
import com.facebook.presto.spi.Constraint;
import com.facebook.presto.spi.type.Type;
import com.facebook.presto.spi.type.TypeManager;
import com.facebook.presto.spi.type.TypeSignature;
import com.facebook.presto.testing.MaterializedResult;
import com.facebook.presto.testing.MaterializedRow;
import com.facebook.presto.testing.QueryRunner;
import com.facebook.presto.tests.AbstractTestIntegrationSmokeTest;
import com.facebook.presto.tests.DistributedQueryRunner;
import com.facebook.presto.type.TypeRegistry;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.io.Files;
import org.apache.hadoop.fs.Path;
import org.intellij.lang.annotations.Language;
import org.joda.time.DateTime;
import org.testng.annotations.Test;
import java.io.File;
import java.math.BigDecimal;
import java.sql.Date;
import java.sql.Timestamp;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import static com.facebook.presto.hive.HiveColumnHandle.BUCKET_COLUMN_NAME;
import static com.facebook.presto.hive.HiveColumnHandle.PATH_COLUMN_NAME;
import static com.facebook.presto.hive.HiveQueryRunner.HIVE_CATALOG;
import static com.facebook.presto.hive.HiveQueryRunner.TPCH_SCHEMA;
import static com.facebook.presto.hive.HiveQueryRunner.createBucketedSession;
import static com.facebook.presto.hive.HiveQueryRunner.createQueryRunner;
import static com.facebook.presto.hive.HiveTableProperties.BUCKETED_BY_PROPERTY;
import static com.facebook.presto.hive.HiveTableProperties.BUCKET_COUNT_PROPERTY;
import static com.facebook.presto.hive.HiveTableProperties.PARTITIONED_BY_PROPERTY;
import static com.facebook.presto.hive.HiveTableProperties.STORAGE_FORMAT_PROPERTY;
import static com.facebook.presto.hive.HiveUtil.annotateColumnComment;
import static com.facebook.presto.spi.type.BigintType.BIGINT;
import static com.facebook.presto.spi.type.CharType.createCharType;
import static com.facebook.presto.spi.type.DecimalType.createDecimalType;
import static com.facebook.presto.spi.type.DoubleType.DOUBLE;
import static com.facebook.presto.spi.type.SmallintType.SMALLINT;
import static com.facebook.presto.spi.type.TinyintType.TINYINT;
import static com.facebook.presto.spi.type.VarcharType.createUnboundedVarcharType;
import static com.facebook.presto.spi.type.VarcharType.createVarcharType;
import static com.facebook.presto.testing.MaterializedResult.resultBuilder;
import static com.facebook.presto.tests.QueryAssertions.assertEqualsIgnoreOrder;
import static com.facebook.presto.transaction.TransactionBuilder.transaction;
import static com.google.common.collect.Iterables.getOnlyElement;
import static com.google.common.io.Files.createTempDir;
import static io.airlift.testing.FileUtils.deleteRecursively;
import static io.airlift.tpch.TpchTable.CUSTOMER;
import static io.airlift.tpch.TpchTable.ORDERS;
import static java.lang.String.format;
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.requireNonNull;
import static org.joda.time.DateTimeZone.UTC;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotNull;
import static org.testng.Assert.assertNull;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.fail;
import static org.testng.FileAssert.assertFile;
public class TestHiveIntegrationSmokeTest
extends AbstractTestIntegrationSmokeTest
{
private final String catalog;
private final Session bucketedSession;
private final TypeManager typeManager;
private final TypeTranslator typeTranslator;
@SuppressWarnings("unused")
public TestHiveIntegrationSmokeTest()
throws Exception
{
this(createQueryRunner(ORDERS, CUSTOMER), createBucketedSession(), HIVE_CATALOG, new HiveTypeTranslator());
}
protected TestHiveIntegrationSmokeTest(QueryRunner queryRunner, Session bucketedSession, String catalog, TypeTranslator typeTranslator)
{
super(queryRunner);
this.catalog = requireNonNull(catalog, "catalog is null");
this.bucketedSession = requireNonNull(bucketedSession, "bucketSession is null");
this.typeTranslator = requireNonNull(typeTranslator, "typeTranslator is null");
this.typeManager = new TypeRegistry();
}
protected List<?> getPartitions(HiveTableLayoutHandle tableLayoutHandle)
{
return tableLayoutHandle.getPartitions().get();
}
@Test
public void testSchemaOperations()
{
assertUpdate("CREATE SCHEMA new_schema");
assertUpdate("CREATE TABLE new_schema.test (x bigint)");
assertQueryFails("DROP SCHEMA new_schema", "Schema not empty: new_schema");
assertUpdate("DROP TABLE new_schema.test");
assertUpdate("DROP SCHEMA new_schema");
}
@Test
public void testInformationSchemaTablesWithoutEqualityConstraint()
throws Exception
{
@Language("SQL") String actual = "" +
"SELECT lower(table_name) " +
"FROM information_schema.tables " +
"WHERE table_catalog = '" + catalog + "' AND table_schema LIKE 'tpch' AND table_name LIKE '%orders'";
@Language("SQL") String expected = "" +
"SELECT lower(table_name) " +
"FROM information_schema.tables " +
"WHERE table_name LIKE '%ORDERS'";
assertQuery(actual, expected);
}
@Test
public void testInformationSchemaColumnsWithoutEqualityConstraint()
throws Exception
{
@Language("SQL") String actual = "" +
"SELECT lower(table_name), lower(column_name) " +
"FROM information_schema.columns " +
"WHERE table_catalog = '" + catalog + "' AND table_schema = 'tpch' AND table_name LIKE '%orders%'";
@Language("SQL") String expected = "" +
"SELECT lower(table_name), lower(column_name) " +
"FROM information_schema.columns " +
"WHERE table_name LIKE '%ORDERS%'";
assertQuery(actual, expected);
}
@Test
public void createTableWithEveryType()
throws Exception
{
@Language("SQL") String query = "" +
"CREATE TABLE test_types_table AS " +
"SELECT" +
" 'foo' _varchar" +
", cast('bar' as varbinary) _varbinary" +
", cast(1 as bigint) _bigint" +
", 2 _integer" +
", CAST('3.14' AS DOUBLE) _double" +
", true _boolean" +
", DATE '1980-05-07' _date" +
", TIMESTAMP '1980-05-07 11:22:33.456' _timestamp" +
", CAST('3.14' AS DECIMAL(3,2)) _decimal_short" +
", CAST('12345678901234567890.0123456789' AS DECIMAL(30,10)) _decimal_long" +
", CAST('bar' AS CHAR(10)) _char";
assertUpdate(query, 1);
MaterializedResult results = queryRunner.execute(getSession(), "SELECT * FROM test_types_table").toJdbcTypes();
assertEquals(results.getRowCount(), 1);
MaterializedRow row = results.getMaterializedRows().get(0);
assertEquals(row.getField(0), "foo");
assertEquals(row.getField(1), "bar".getBytes(UTF_8));
assertEquals(row.getField(2), 1L);
assertEquals(row.getField(3), 2);
assertEquals(row.getField(4), 3.14);
assertEquals(row.getField(5), true);
assertEquals(row.getField(6), new Date(new DateTime(1980, 5, 7, 0, 0, 0, UTC).getMillis()));
assertEquals(row.getField(7), new Timestamp(new DateTime(1980, 5, 7, 11, 22, 33, 456, UTC).getMillis()));
assertEquals(row.getField(8), new BigDecimal("3.14"));
assertEquals(row.getField(9), new BigDecimal("12345678901234567890.0123456789"));
assertEquals(row.getField(10), "bar ");
assertUpdate("DROP TABLE test_types_table");
assertFalse(queryRunner.tableExists(getSession(), "test_types_table"));
}
@Test
public void createPartitionedTable()
throws Exception
{
for (TestingHiveStorageFormat storageFormat : getAllTestingHiveStorageFormat()) {
if (insertOperationsSupported(storageFormat.getFormat())) {
createPartitionedTable(storageFormat.getSession(), storageFormat.getFormat());
}
}
}
public void createPartitionedTable(Session session, HiveStorageFormat storageFormat)
throws Exception
{
@Language("SQL") String createTable = "" +
"CREATE TABLE test_partitioned_table (" +
" _string VARCHAR" +
", _varchar VARCHAR(65535)" +
", _char CHAR(10)" +
", _bigint BIGINT" +
", _integer INTEGER" +
", _smallint SMALLINT" +
", _tinyint TINYINT" +
", _real REAL" +
", _double DOUBLE" +
", _boolean BOOLEAN" +
", _decimal_short DECIMAL(3,2)" +
", _decimal_long DECIMAL(30,10)" +
", _partition_string VARCHAR" +
", _partition_varchar VARCHAR(65535)" +
", _partition_char CHAR(10)" +
", _partition_tinyint TINYINT" +
", _partition_smallint SMALLINT" +
", _partition_integer INTEGER" +
", _partition_bigint BIGINT" +
", _partition_decimal_short DECIMAL(3,2)" +
", _partition_decimal_long DECIMAL(30,10)" +
") " +
"WITH (" +
"format = '" + storageFormat + "', " +
"partitioned_by = ARRAY[ '_partition_string', '_partition_varchar', '_partition_char', '_partition_tinyint', '_partition_smallint', '_partition_integer', '_partition_bigint', '_partition_decimal_short', '_partition_decimal_long' ]" +
") ";
if (storageFormat == HiveStorageFormat.AVRO) {
createTable = createTable.replace(" _smallint SMALLINT,", " _smallint INTEGER,");
createTable = createTable.replace(" _tinyint TINYINT,", " _tinyint INTEGER,");
}
assertUpdate(session, createTable);
TableMetadata tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, "test_partitioned_table");
assertEquals(tableMetadata.getMetadata().getProperties().get(STORAGE_FORMAT_PROPERTY), storageFormat);
List<String> partitionedBy = ImmutableList.of("_partition_string", "_partition_varchar", "_partition_char", "_partition_tinyint", "_partition_smallint", "_partition_integer", "_partition_bigint", "_partition_decimal_short", "_partition_decimal_long");
assertEquals(tableMetadata.getMetadata().getProperties().get(PARTITIONED_BY_PROPERTY), partitionedBy);
for (ColumnMetadata columnMetadata : tableMetadata.getColumns()) {
boolean partitionKey = partitionedBy.contains(columnMetadata.getName());
assertEquals(columnMetadata.getComment(), annotateColumnComment(Optional.empty(), partitionKey));
}
assertColumnType(tableMetadata, "_string", createUnboundedVarcharType());
assertColumnType(tableMetadata, "_varchar", createVarcharType(65535));
assertColumnType(tableMetadata, "_char", createCharType(10));
assertColumnType(tableMetadata, "_partition_string", createUnboundedVarcharType());
assertColumnType(tableMetadata, "_partition_varchar", createVarcharType(65535));
MaterializedResult result = computeActual("SELECT * from test_partitioned_table");
assertEquals(result.getRowCount(), 0);
@Language("SQL") String select = "" +
"SELECT" +
" 'foo' _string" +
", 'bar' _varchar" +
", CAST('boo' AS CHAR(10)) _char" +
", CAST(1 AS BIGINT) _bigint" +
", 2 _integer" +
", CAST (3 AS SMALLINT) _smallint" +
", CAST (4 AS TINYINT) _tinyint" +
", CAST('123.45' AS REAL) _real" +
", CAST('3.14' AS DOUBLE) _double" +
", true _boolean" +
", CAST('3.14' AS DECIMAL(3,2)) _decimal_short" +
", CAST('12345678901234567890.0123456789' AS DECIMAL(30,10)) _decimal_long" +
", 'foo' _partition_string" +
", 'bar' _partition_varchar" +
", CAST('boo' AS CHAR(10)) _partition_char" +
", CAST(1 AS TINYINT) _partition_tinyint" +
", CAST(1 AS SMALLINT) _partition_smallint" +
", 1 _partition_integer" +
", CAST (1 AS BIGINT) _partition_bigint" +
", CAST('3.14' AS DECIMAL(3,2)) _partition_decimal_short" +
", CAST('12345678901234567890.0123456789' AS DECIMAL(30,10)) _partition_decimal_long";
if (storageFormat == HiveStorageFormat.AVRO) {
select = select.replace(" CAST (3 AS SMALLINT) _smallint,", " 3 _smallint,");
select = select.replace(" CAST (4 AS TINYINT) _tinyint,", " 4 _tinyint,");
}
assertUpdate(session, "INSERT INTO test_partitioned_table " + select, 1);
assertQuery(session, "SELECT * from test_partitioned_table", select);
assertUpdate(session, "DROP TABLE test_partitioned_table");
assertFalse(queryRunner.tableExists(session, "test_partitioned_table"));
}
@Test
public void createTableLike()
throws Exception
{
createTableLike("", false);
createTableLike("EXCLUDING PROPERTIES", false);
createTableLike("INCLUDING PROPERTIES", true);
}
protected void createTableLike(String likeSuffix, boolean hasPartition)
throws Exception
{
// Create a non-partitioned table
@Language("SQL") String createTable = "" +
"CREATE TABLE test_table_original (" +
" tinyint_col tinyint " +
", smallint_col smallint" +
")";
assertUpdate(createTable);
// Verify the table is correctly created
TableMetadata tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, "test_table_original");
assertColumnType(tableMetadata, "tinyint_col", TINYINT);
assertColumnType(tableMetadata, "smallint_col", SMALLINT);
// Create a partitioned table
@Language("SQL") String createPartitionedTable = "" +
"CREATE TABLE test_partitioned_table_original (" +
" string_col VARCHAR" +
", decimal_long_col DECIMAL(30,10)" +
", partition_bigint BIGINT" +
", partition_decimal_long DECIMAL(30,10)" +
") " +
"WITH (" +
"partitioned_by = ARRAY['partition_bigint', 'partition_decimal_long']" +
")";
assertUpdate(createPartitionedTable);
// Verify the table is correctly created
tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, "test_partitioned_table_original");
// Verify the partition keys are correctly created
List<String> partitionedBy = ImmutableList.of("partition_bigint", "partition_decimal_long");
assertEquals(tableMetadata.getMetadata().getProperties().get(PARTITIONED_BY_PROPERTY), partitionedBy);
for (ColumnMetadata columnMetadata : tableMetadata.getColumns()) {
boolean partitionKey = partitionedBy.contains(columnMetadata.getName());
assertEquals(columnMetadata.getComment(), annotateColumnComment(Optional.empty(), partitionKey));
}
// Verify the column types
assertColumnType(tableMetadata, "string_col", createUnboundedVarcharType());
assertColumnType(tableMetadata, "partition_bigint", BIGINT);
assertColumnType(tableMetadata, "partition_decimal_long", createDecimalType(30, 10));
// Create a table using only one LIKE
@Language("SQL") String createTableSingleLike = "" +
"CREATE TABLE test_partitioned_table_single_like (" +
"LIKE test_partitioned_table_original " + likeSuffix +
")";
assertUpdate(createTableSingleLike);
tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, "test_partitioned_table_single_like");
// Verify the partitioned keys are correctly created if copying partition columns
verifyPartition(hasPartition, tableMetadata, partitionedBy);
// Verify the column types
assertColumnType(tableMetadata, "string_col", createUnboundedVarcharType());
assertColumnType(tableMetadata, "partition_bigint", BIGINT);
assertColumnType(tableMetadata, "partition_decimal_long", createDecimalType(30, 10));
@Language("SQL") String createTableLikeExtra = "" +
"CREATE TABLE test_partitioned_table_like_extra (" +
" bigint_col BIGINT" +
", double_col DOUBLE" +
", LIKE test_partitioned_table_single_like " + likeSuffix +
")";
assertUpdate(createTableLikeExtra);
tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, "test_partitioned_table_like_extra");
// Verify the partitioned keys are correctly created if copying partition columns
verifyPartition(hasPartition, tableMetadata, partitionedBy);
// Verify the column types
assertColumnType(tableMetadata, "bigint_col", BIGINT);
assertColumnType(tableMetadata, "double_col", DOUBLE);
assertColumnType(tableMetadata, "string_col", createUnboundedVarcharType());
assertColumnType(tableMetadata, "partition_bigint", BIGINT);
assertColumnType(tableMetadata, "partition_decimal_long", createDecimalType(30, 10));
@Language("SQL") String createTableDoubleLike = "" +
"CREATE TABLE test_partitioned_table_double_like (" +
" LIKE test_table_original " +
", LIKE test_partitioned_table_like_extra " + likeSuffix +
")";
assertUpdate(createTableDoubleLike);
tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, "test_partitioned_table_double_like");
// Verify the partitioned keys are correctly created if copying partition columns
verifyPartition(hasPartition, tableMetadata, partitionedBy);
// Verify the column types
assertColumnType(tableMetadata, "tinyint_col", TINYINT);
assertColumnType(tableMetadata, "smallint_col", SMALLINT);
assertColumnType(tableMetadata, "string_col", createUnboundedVarcharType());
assertColumnType(tableMetadata, "partition_bigint", BIGINT);
assertColumnType(tableMetadata, "partition_decimal_long", createDecimalType(30, 10));
assertUpdate("DROP TABLE test_table_original");
assertUpdate("DROP TABLE test_partitioned_table_original");
assertUpdate("DROP TABLE test_partitioned_table_single_like");
assertUpdate("DROP TABLE test_partitioned_table_like_extra");
assertUpdate("DROP TABLE test_partitioned_table_double_like");
}
@Test
public void createTableAs()
throws Exception
{
for (TestingHiveStorageFormat storageFormat : getAllTestingHiveStorageFormat()) {
if (insertOperationsSupported(storageFormat.getFormat())) {
createTableAs(storageFormat.getSession(), storageFormat.getFormat());
}
}
}
public void createTableAs(Session session, HiveStorageFormat storageFormat)
throws Exception
{
@Language("SQL") String select = "SELECT" +
" 'foo' _varchar" +
", CAST('bar' AS CHAR(10)) _char" +
", CAST (1 AS BIGINT) _bigint" +
", 2 _integer" +
", CAST (3 AS SMALLINT) _smallint" +
", CAST (4 AS TINYINT) _tinyint" +
", CAST ('123.45' as REAL) _real" +
", CAST('3.14' AS DOUBLE) _double" +
", true _boolean" +
", CAST('3.14' AS DECIMAL(3,2)) _decimal_short" +
", CAST('12345678901234567890.0123456789' AS DECIMAL(30,10)) _decimal_long";
if (storageFormat == HiveStorageFormat.AVRO) {
select = select.replace(" CAST (3 AS SMALLINT) _smallint,", " 3 _smallint,");
select = select.replace(" CAST (4 AS TINYINT) _tinyint,", " 4 _tinyint,");
}
String createTableAs = format("CREATE TABLE test_format_table WITH (format = '%s') AS %s", storageFormat, select);
assertUpdate(session, createTableAs, 1);
TableMetadata tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, "test_format_table");
assertEquals(tableMetadata.getMetadata().getProperties().get(STORAGE_FORMAT_PROPERTY), storageFormat);
assertColumnType(tableMetadata, "_varchar", createVarcharType(3));
assertColumnType(tableMetadata, "_char", createCharType(10));
assertQuery(session, "SELECT * from test_format_table", select);
assertUpdate(session, "DROP TABLE test_format_table");
assertFalse(queryRunner.tableExists(session, "test_format_table"));
}
@Test
public void createPartitionedTableAs()
throws Exception
{
for (TestingHiveStorageFormat storageFormat : getAllTestingHiveStorageFormat()) {
createPartitionedTableAs(storageFormat.getSession(), storageFormat.getFormat());
}
}
public void createPartitionedTableAs(Session session, HiveStorageFormat storageFormat)
throws Exception
{
@Language("SQL") String createTable = "" +
"CREATE TABLE test_create_partitioned_table_as " +
"WITH (" +
"format = '" + storageFormat + "', " +
"partitioned_by = ARRAY[ 'SHIP_PRIORITY', 'ORDER_STATUS' ]" +
") " +
"AS " +
"SELECT orderkey AS order_key, shippriority AS ship_priority, orderstatus AS order_status " +
"FROM tpch.tiny.orders";
assertUpdate(session, createTable, "SELECT count(*) from orders");
TableMetadata tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, "test_create_partitioned_table_as");
assertEquals(tableMetadata.getMetadata().getProperties().get(STORAGE_FORMAT_PROPERTY), storageFormat);
List<String> partitionedBy = ImmutableList.of("ship_priority", "order_status");
assertEquals(tableMetadata.getMetadata().getProperties().get(PARTITIONED_BY_PROPERTY), partitionedBy);
for (ColumnMetadata columnMetadata : tableMetadata.getColumns()) {
boolean partitionKey = partitionedBy.contains(columnMetadata.getName());
assertEquals(columnMetadata.getComment(), annotateColumnComment(Optional.empty(), partitionKey));
}
List<?> partitions = getPartitions("test_create_partitioned_table_as");
assertEquals(partitions.size(), 3);
assertQuery(session, "SELECT * from test_create_partitioned_table_as", "SELECT orderkey, shippriority, orderstatus FROM orders");
assertUpdate(session, "DROP TABLE test_create_partitioned_table_as");
assertFalse(queryRunner.tableExists(session, "test_create_partitioned_table_as"));
}
@Test(expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = "Partition keys must be the last columns in the table and in the same order as the table properties.*")
public void testCreatePartitionedTableInvalidColumnOrdering()
{
assertUpdate("" +
"CREATE TABLE test_create_table_invalid_column_ordering\n" +
"(grape bigint, apple varchar, orange bigint, pear varchar)\n" +
"WITH (partitioned_by = ARRAY['apple'])");
}
@Test(expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = "Partition keys must be the last columns in the table and in the same order as the table properties.*")
public void testCreatePartitionedTableAsInvalidColumnOrdering()
throws Exception
{
assertUpdate("" +
"CREATE TABLE test_create_table_as_invalid_column_ordering " +
"WITH (partitioned_by = ARRAY['SHIP_PRIORITY', 'ORDER_STATUS']) " +
"AS " +
"SELECT shippriority AS ship_priority, orderkey AS order_key, orderstatus AS order_status " +
"FROM tpch.tiny.orders");
}
@Test(expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = "Table contains only partition columns")
public void testCreateTableOnlyPartitionColumns()
{
assertUpdate("" +
"CREATE TABLE test_create_table_only_partition_columns\n" +
"(grape bigint, apple varchar, orange bigint, pear varchar)\n" +
"WITH (partitioned_by = ARRAY['grape', 'apple', 'orange', 'pear'])");
}
@Test(expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = "Partition columns .* not present in schema")
public void testCreateTableNonExistentPartitionColumns()
{
assertUpdate("" +
"CREATE TABLE test_create_table_nonexistent_partition_columns\n" +
"(grape bigint, apple varchar, orange bigint, pear varchar)\n" +
"WITH (partitioned_by = ARRAY['dragonfruit'])");
}
@Test(expectedExceptions = RuntimeException.class, expectedExceptionsMessageRegExp = "Unsupported Hive type: varchar\\(65536\\)\\. Supported VARCHAR types: VARCHAR\\(<=65535\\), VARCHAR\\.")
public void testCreateTableNonSupportedVarcharColumn()
{
assertUpdate("CREATE TABLE test_create_table_non_supported_varchar_column (apple varchar(65536))");
}
@Test
public void testCreatePartitionedBucketedTableAsFewRows()
throws Exception
{
// go through all storage formats to make sure the empty buckets are correctly created
for (TestingHiveStorageFormat storageFormat : getAllTestingHiveStorageFormat()) {
testCreatePartitionedBucketedTableAsFewRows(storageFormat.getSession(), storageFormat.getFormat());
}
}
private void testCreatePartitionedBucketedTableAsFewRows(Session session, HiveStorageFormat storageFormat)
throws Exception
{
String tableName = "test_create_partitioned_bucketed_table_as_few_rows";
@Language("SQL") String createTable = "" +
"CREATE TABLE " + tableName + " " +
"WITH (" +
"format = '" + storageFormat + "', " +
"partitioned_by = ARRAY[ 'partition_key' ], " +
"bucketed_by = ARRAY[ 'bucket_key' ], " +
"bucket_count = 11 " +
") " +
"AS " +
"SELECT * " +
"FROM (" +
"VALUES " +
" (VARCHAR 'a', VARCHAR 'b', VARCHAR 'c'), " +
" ('aa', 'bb', 'cc'), " +
" ('aaa', 'bbb', 'ccc')" +
") t(bucket_key, col, partition_key)";
assertUpdate(
// make sure that we will get one file per bucket regardless of writer count configured
getParallelWriteSession(),
createTable,
3);
verifyPartitionedBucketedTableAsFewRows(storageFormat, tableName);
try {
assertUpdate(session, "INSERT INTO " + tableName + " VALUES ('a0', 'b0', 'c')", 1);
fail("expected failure");
}
catch (Exception e) {
assertEquals(e.getMessage(), "Can not insert into existing partitions of bucketed Hive table");
}
assertUpdate(session, "DROP TABLE " + tableName);
assertFalse(queryRunner.tableExists(session, tableName));
}
@Test
public void testCreatePartitionedBucketedTableAs()
throws Exception
{
testCreatePartitionedBucketedTableAs(HiveStorageFormat.RCBINARY);
}
private void testCreatePartitionedBucketedTableAs(HiveStorageFormat storageFormat)
throws Exception
{
String tableName = "test_create_partitioned_bucketed_table_as";
@Language("SQL") String createTable = "" +
"CREATE TABLE " + tableName + " " +
"WITH (" +
"format = '" + storageFormat + "', " +
"partitioned_by = ARRAY[ 'orderstatus' ], " +
"bucketed_by = ARRAY[ 'custkey', 'custkey2' ], " +
"bucket_count = 11 " +
") " +
"AS " +
"SELECT custkey, custkey AS custkey2, comment, orderstatus " +
"FROM tpch.tiny.orders";
assertUpdate(
// make sure that we will get one file per bucket regardless of writer count configured
getParallelWriteSession(),
createTable,
"SELECT count(*) from orders");
verifyPartitionedBucketedTable(storageFormat, tableName);
assertUpdate("DROP TABLE " + tableName);
assertFalse(queryRunner.tableExists(getSession(), tableName));
}
@Test
public void testCreatePartitionedBucketedTableAsWithUnionAll()
throws Exception
{
testCreatePartitionedBucketedTableAsWithUnionAll(HiveStorageFormat.RCBINARY);
}
private void testCreatePartitionedBucketedTableAsWithUnionAll(HiveStorageFormat storageFormat)
throws Exception
{
String tableName = "test_create_partitioned_bucketed_table_as_with_union_all";
@Language("SQL") String createTable = "" +
"CREATE TABLE " + tableName + " " +
"WITH (" +
"format = '" + storageFormat + "', " +
"partitioned_by = ARRAY[ 'orderstatus' ], " +
"bucketed_by = ARRAY[ 'custkey', 'custkey2' ], " +
"bucket_count = 11 " +
") " +
"AS " +
"SELECT custkey, custkey AS custkey2, comment, orderstatus " +
"FROM tpch.tiny.orders " +
"WHERE length(comment) % 2 = 0 " +
"UNION ALL " +
"SELECT custkey, custkey AS custkey2, comment, orderstatus " +
"FROM tpch.tiny.orders " +
"WHERE length(comment) % 2 = 1";
assertUpdate(
// make sure that we will get one file per bucket regardless of writer count configured
getParallelWriteSession(),
createTable,
"SELECT count(*) from orders");
verifyPartitionedBucketedTable(storageFormat, tableName);
assertUpdate("DROP TABLE " + tableName);
assertFalse(queryRunner.tableExists(getSession(), tableName));
}
private void verifyPartitionedBucketedTable(HiveStorageFormat storageFormat, String tableName)
throws Exception
{
TableMetadata tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, tableName);
assertEquals(tableMetadata.getMetadata().getProperties().get(STORAGE_FORMAT_PROPERTY), storageFormat);
List<String> partitionedBy = ImmutableList.of("orderstatus");
assertEquals(tableMetadata.getMetadata().getProperties().get(PARTITIONED_BY_PROPERTY), partitionedBy);
for (ColumnMetadata columnMetadata : tableMetadata.getColumns()) {
boolean partitionKey = partitionedBy.contains(columnMetadata.getName());
assertEquals(columnMetadata.getComment(), annotateColumnComment(Optional.empty(), partitionKey));
}
assertEquals(tableMetadata.getMetadata().getProperties().get(BUCKETED_BY_PROPERTY), ImmutableList.of("custkey", "custkey2"));
assertEquals(tableMetadata.getMetadata().getProperties().get(BUCKET_COUNT_PROPERTY), 11);
List<?> partitions = getPartitions(tableName);
assertEquals(partitions.size(), 3);
assertQuery("SELECT * from " + tableName, "SELECT custkey, custkey, comment, orderstatus FROM orders");
for (int i = 1; i <= 30; i++) {
assertQuery(
format("SELECT * from " + tableName + " where custkey = %d and custkey2 = %d", i, i),
format("SELECT custkey, custkey, comment, orderstatus FROM orders where custkey = %d", i));
}
try {
assertUpdate("INSERT INTO " + tableName + " VALUES (1, 1, 'comment', 'O')", 1);
fail("expected failure");
}
catch (Exception e) {
assertEquals(e.getMessage(), "Can not insert into existing partitions of bucketed Hive table");
}
}
@Test
public void testCreateInvalidBucketedTable()
throws Exception
{
testCreateInvalidBucketedTable(HiveStorageFormat.RCBINARY);
}
private void testCreateInvalidBucketedTable(HiveStorageFormat storageFormat)
throws Exception
{
String tableName = "test_create_invalid_bucketed_table";
try {
computeActual("" +
"CREATE TABLE " + tableName + " (" +
" a BIGINT," +
" b DOUBLE," +
" p VARCHAR" +
") WITH (" +
"format = '" + storageFormat + "', " +
"partitioned_by = ARRAY[ 'p' ], " +
"bucketed_by = ARRAY[ 'a', 'c' ], " +
"bucket_count = 11 " +
")");
fail();
}
catch (Exception e) {
assertEquals(e.getMessage(), "Bucketing columns [c] not present in schema");
}
try {
computeActual("" +
"CREATE TABLE " + tableName + " " +
"WITH (" +
"format = '" + storageFormat + "', " +
"partitioned_by = ARRAY[ 'orderstatus' ], " +
"bucketed_by = ARRAY[ 'custkey', 'custkey3' ], " +
"bucket_count = 11 " +
") " +
"AS " +
"SELECT custkey, custkey AS custkey2, comment, orderstatus " +
"FROM tpch.tiny.orders");
fail();
}
catch (Exception e) {
assertEquals(e.getMessage(), "INSERT must write all distribution columns: [custkey, custkey3]");
}
assertFalse(queryRunner.tableExists(getSession(), tableName));
}
@Test
public void testInsertPartitionedBucketedTableFewRows()
throws Exception
{
// go through all storage formats to make sure the empty buckets are correctly created
for (TestingHiveStorageFormat storageFormat : getAllTestingHiveStorageFormat()) {
testInsertPartitionedBucketedTableFewRows(storageFormat.getSession(), storageFormat.getFormat());
}
}
private void testInsertPartitionedBucketedTableFewRows(Session session, HiveStorageFormat storageFormat)
throws Exception
{
String tableName = "test_insert_partitioned_bucketed_table_few_rows";
assertUpdate(session, "" +
"CREATE TABLE " + tableName + " (" +
" bucket_key varchar," +
" col varchar," +
" partition_key varchar)" +
"WITH (" +
"format = '" + storageFormat + "', " +
"partitioned_by = ARRAY[ 'partition_key' ], " +
"bucketed_by = ARRAY[ 'bucket_key' ], " +
"bucket_count = 11)");
assertUpdate(
// make sure that we will get one file per bucket regardless of writer count configured
getParallelWriteSession(),
"INSERT INTO " + tableName + " " +
"VALUES " +
" (VARCHAR 'a', VARCHAR 'b', VARCHAR 'c'), " +
" ('aa', 'bb', 'cc'), " +
" ('aaa', 'bbb', 'ccc')",
3);
verifyPartitionedBucketedTableAsFewRows(storageFormat, tableName);
try {
assertUpdate(session, "INSERT INTO test_insert_partitioned_bucketed_table_few_rows VALUES ('a0', 'b0', 'c')", 1);
fail("expected failure");
}
catch (Exception e) {
assertEquals(e.getMessage(), "Can not insert into existing partitions of bucketed Hive table");
}
assertUpdate(session, "DROP TABLE test_insert_partitioned_bucketed_table_few_rows");
assertFalse(queryRunner.tableExists(session, tableName));
}
private void verifyPartitionedBucketedTableAsFewRows(HiveStorageFormat storageFormat, String tableName)
{
TableMetadata tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, tableName);
assertEquals(tableMetadata.getMetadata().getProperties().get(STORAGE_FORMAT_PROPERTY), storageFormat);
List<String> partitionedBy = ImmutableList.of("partition_key");
assertEquals(tableMetadata.getMetadata().getProperties().get(PARTITIONED_BY_PROPERTY), partitionedBy);
for (ColumnMetadata columnMetadata : tableMetadata.getColumns()) {
boolean partitionKey = partitionedBy.contains(columnMetadata.getName());
assertEquals(columnMetadata.getComment(), annotateColumnComment(Optional.empty(), partitionKey));
}
assertEquals(tableMetadata.getMetadata().getProperties().get(BUCKETED_BY_PROPERTY), ImmutableList.of("bucket_key"));
assertEquals(tableMetadata.getMetadata().getProperties().get(BUCKET_COUNT_PROPERTY), 11);
List<?> partitions = getPartitions(tableName);
assertEquals(partitions.size(), 3);
MaterializedResult actual = computeActual("SELECT * from " + tableName);
MaterializedResult expected = resultBuilder(getSession(), canonicalizeType(createUnboundedVarcharType()), canonicalizeType(createUnboundedVarcharType()), canonicalizeType(createUnboundedVarcharType()))
.row("a", "b", "c")
.row("aa", "bb", "cc")
.row("aaa", "bbb", "ccc")
.build();
assertEqualsIgnoreOrder(actual.getMaterializedRows(), expected.getMaterializedRows());
}
@Test
public void testInsertPartitionedBucketedTable()
throws Exception
{
testInsertPartitionedBucketedTable(HiveStorageFormat.RCBINARY);
}
private void testInsertPartitionedBucketedTable(HiveStorageFormat storageFormat)
throws Exception
{
String tableName = "test_insert_partitioned_bucketed_table";
assertUpdate("" +
"CREATE TABLE " + tableName + " (" +
" custkey bigint," +
" custkey2 bigint," +
" comment varchar," +
" orderstatus varchar)" +
"WITH (" +
"format = '" + storageFormat + "', " +
"partitioned_by = ARRAY[ 'orderstatus' ], " +
"bucketed_by = ARRAY[ 'custkey', 'custkey2' ], " +
"bucket_count = 11)");
ImmutableList<String> orderStatusList = ImmutableList.of("F", "O", "P");
for (int i = 0; i < orderStatusList.size(); i++) {
String orderStatus = orderStatusList.get(i);
assertUpdate(
// make sure that we will get one file per bucket regardless of writer count configured
getParallelWriteSession(),
format(
"INSERT INTO " + tableName + " " +
"SELECT custkey, custkey AS custkey2, comment, orderstatus " +
"FROM tpch.tiny.orders " +
"WHERE orderstatus = '%s'",
orderStatus),
format("SELECT count(*) from orders where orderstatus = '%s'", orderStatus));
}
verifyPartitionedBucketedTable(storageFormat, tableName);
assertUpdate("DROP TABLE " + tableName);
assertFalse(queryRunner.tableExists(getSession(), tableName));
}
@Test
public void testInsertPartitionedBucketedTableWithUnionAll()
throws Exception
{
testInsertPartitionedBucketedTableWithUnionAll(HiveStorageFormat.RCBINARY);
}
private void testInsertPartitionedBucketedTableWithUnionAll(HiveStorageFormat storageFormat)
throws Exception
{
String tableName = "test_insert_partitioned_bucketed_table_with_union_all";
assertUpdate("" +
"CREATE TABLE " + tableName + " (" +
" custkey bigint," +
" custkey2 bigint," +
" comment varchar," +
" orderstatus varchar)" +
"WITH (" +
"format = '" + storageFormat + "', " +
"partitioned_by = ARRAY[ 'orderstatus' ], " +
"bucketed_by = ARRAY[ 'custkey', 'custkey2' ], " +
"bucket_count = 11)");
ImmutableList<String> orderStatusList = ImmutableList.of("F", "O", "P");
for (int i = 0; i < orderStatusList.size(); i++) {
String orderStatus = orderStatusList.get(i);
assertUpdate(
// make sure that we will get one file per bucket regardless of writer count configured
getParallelWriteSession(),
format(
"INSERT INTO " + tableName + " " +
"SELECT custkey, custkey AS custkey2, comment, orderstatus " +
"FROM tpch.tiny.orders " +
"WHERE orderstatus = '%s' and length(comment) %% 2 = 0 " +
"UNION ALL " +
"SELECT custkey, custkey AS custkey2, comment, orderstatus " +
"FROM tpch.tiny.orders " +
"WHERE orderstatus = '%s' and length(comment) %% 2 = 1",
orderStatus, orderStatus),
format("SELECT count(*) from orders where orderstatus = '%s'", orderStatus));
}
verifyPartitionedBucketedTable(storageFormat, tableName);
assertUpdate("DROP TABLE " + tableName);
assertFalse(queryRunner.tableExists(getSession(), tableName));
}
@Test
public void insertTable()
throws Exception
{
for (TestingHiveStorageFormat storageFormat : getAllTestingHiveStorageFormat()) {
if (insertOperationsSupported(storageFormat.getFormat())) {
insertTable(storageFormat.getSession(), storageFormat.getFormat());
}
}
}
public void insertTable(Session session, HiveStorageFormat storageFormat)
throws Exception
{
@Language("SQL") String createTable = "" +
"CREATE TABLE test_insert_format_table " +
"(" +
" _string VARCHAR," +
" _varchar VARCHAR(65535)," +
" _char CHAR(10)," +
" _bigint BIGINT," +
" _integer INTEGER," +
" _smallint SMALLINT," +
" _tinyint TINYINT," +
" _real REAL," +
" _double DOUBLE," +
" _boolean BOOLEAN," +
" _decimal_short DECIMAL(3,2)," +
" _decimal_long DECIMAL(30,10)" +
") " +
"WITH (format = '" + storageFormat + "') ";
if (storageFormat == HiveStorageFormat.AVRO) {
createTable = createTable.replace(" _smallint SMALLINT,", " _smallint INTEGER,");
createTable = createTable.replace(" _tinyint TINYINT,", " _tinyint INTEGER,");
}
assertUpdate(session, createTable);
TableMetadata tableMetadata = getTableMetadata(catalog, TPCH_SCHEMA, "test_insert_format_table");
assertEquals(tableMetadata.getMetadata().getProperties().get(STORAGE_FORMAT_PROPERTY), storageFormat);
assertColumnType(tableMetadata, "_string", createUnboundedVarcharType());
assertColumnType(tableMetadata, "_varchar", createVarcharType(65535));