@@ -863,6 +863,117 @@ public void testCreateTableNonSupportedVarcharColumn()
863
863
assertUpdate ("CREATE TABLE test_create_table_non_supported_varchar_column (apple varchar(65536))" );
864
864
}
865
865
866
+ @ Test
867
+ public void testEmptyBucketedTable ()
868
+ {
869
+ // go through all storage formats to make sure the empty buckets are correctly created
870
+ testWithAllStorageFormats (this ::testEmptyBucketedTable );
871
+ }
872
+
873
+ private void testEmptyBucketedTable (Session session , HiveStorageFormat storageFormat )
874
+ {
875
+ testEmptyBucketedTable (session , storageFormat , true , true );
876
+ testEmptyBucketedTable (session , storageFormat , true , false );
877
+ testEmptyBucketedTable (session , storageFormat , false , true );
878
+ testEmptyBucketedTable (session , storageFormat , false , false );
879
+ }
880
+
881
+ private void testEmptyBucketedTable (Session session , HiveStorageFormat storageFormat , boolean optimizedPartitionUpdateSerializationEnabled , boolean createEmpty )
882
+ {
883
+ String tableName = "test_empty_bucketed_table" ;
884
+
885
+ @ Language ("SQL" ) String createTable = "" +
886
+ "CREATE TABLE " + tableName + " " +
887
+ "(bucket_key VARCHAR, col_1 VARCHAR, col2 VARCHAR) " +
888
+ "WITH (" +
889
+ "format = '" + storageFormat + "', " +
890
+ "bucketed_by = ARRAY[ 'bucket_key' ], " +
891
+ "bucket_count = 11 " +
892
+ ") " ;
893
+
894
+ assertUpdate (createTable );
895
+
896
+ TableMetadata tableMetadata = getTableMetadata (catalog , TPCH_SCHEMA , tableName );
897
+ assertEquals (tableMetadata .getMetadata ().getProperties ().get (STORAGE_FORMAT_PROPERTY ), storageFormat );
898
+
899
+ assertNull (tableMetadata .getMetadata ().getProperties ().get (PARTITIONED_BY_PROPERTY ));
900
+ assertEquals (tableMetadata .getMetadata ().getProperties ().get (BUCKETED_BY_PROPERTY ), ImmutableList .of ("bucket_key" ));
901
+ assertEquals (tableMetadata .getMetadata ().getProperties ().get (BUCKET_COUNT_PROPERTY ), 11 );
902
+
903
+ assertEquals (computeActual ("SELECT * from " + tableName ).getRowCount (), 0 );
904
+
905
+ // make sure that we will get one file per bucket regardless of writer count configured
906
+ Session parallelWriter = Session .builder (getTableWriteTestingSession (optimizedPartitionUpdateSerializationEnabled ))
907
+ .setCatalogSessionProperty (catalog , "create_empty_bucket_files" , String .valueOf (createEmpty ))
908
+ .build ();
909
+ assertUpdate (parallelWriter , "INSERT INTO " + tableName + " VALUES ('a0', 'b0', 'c0')" , 1 );
910
+ assertUpdate (parallelWriter , "INSERT INTO " + tableName + " VALUES ('a1', 'b1', 'c1')" , 1 );
911
+
912
+ assertQuery ("SELECT * from " + tableName , "VALUES ('a0', 'b0', 'c0'), ('a1', 'b1', 'c1')" );
913
+
914
+ assertUpdate (session , "DROP TABLE " + tableName );
915
+ assertFalse (getQueryRunner ().tableExists (session , tableName ));
916
+ }
917
+
918
+ @ Test
919
+ public void testBucketedTable ()
920
+ {
921
+ // go through all storage formats to make sure the empty buckets are correctly created
922
+ testWithAllStorageFormats (this ::testBucketedTable );
923
+ }
924
+
925
+ private void testBucketedTable (Session session , HiveStorageFormat storageFormat )
926
+ {
927
+ testBucketedTable (session , storageFormat , true , true );
928
+ testBucketedTable (session , storageFormat , true , false );
929
+ testBucketedTable (session , storageFormat , false , true );
930
+ testBucketedTable (session , storageFormat , false , false );
931
+ }
932
+
933
+ private void testBucketedTable (Session session , HiveStorageFormat storageFormat , boolean optimizedPartitionUpdateSerializationEnabled , boolean createEmpty )
934
+ {
935
+ String tableName = "test_bucketed_table" ;
936
+
937
+ @ Language ("SQL" ) String createTable = "" +
938
+ "CREATE TABLE " + tableName + " " +
939
+ "WITH (" +
940
+ "format = '" + storageFormat + "', " +
941
+ "bucketed_by = ARRAY[ 'bucket_key' ], " +
942
+ "bucket_count = 11 " +
943
+ ") " +
944
+ "AS " +
945
+ "SELECT * " +
946
+ "FROM (" +
947
+ "VALUES " +
948
+ " (VARCHAR 'a', VARCHAR 'b', VARCHAR 'c'), " +
949
+ " ('aa', 'bb', 'cc'), " +
950
+ " ('aaa', 'bbb', 'ccc')" +
951
+ ") t (bucket_key, col_1, col_2)" ;
952
+
953
+ // make sure that we will get one file per bucket regardless of writer count configured
954
+ Session parallelWriter = Session .builder (getTableWriteTestingSession (optimizedPartitionUpdateSerializationEnabled ))
955
+ .setCatalogSessionProperty (catalog , "create_empty_bucket_files" , String .valueOf (createEmpty ))
956
+ .build ();
957
+ assertUpdate (parallelWriter , createTable , 3 );
958
+
959
+ TableMetadata tableMetadata = getTableMetadata (catalog , TPCH_SCHEMA , tableName );
960
+ assertEquals (tableMetadata .getMetadata ().getProperties ().get (STORAGE_FORMAT_PROPERTY ), storageFormat );
961
+
962
+ assertNull (tableMetadata .getMetadata ().getProperties ().get (PARTITIONED_BY_PROPERTY ));
963
+ assertEquals (tableMetadata .getMetadata ().getProperties ().get (BUCKETED_BY_PROPERTY ), ImmutableList .of ("bucket_key" ));
964
+ assertEquals (tableMetadata .getMetadata ().getProperties ().get (BUCKET_COUNT_PROPERTY ), 11 );
965
+
966
+ assertQuery ("SELECT * from " + tableName , "VALUES ('a', 'b', 'c'), ('aa', 'bb', 'cc'), ('aaa', 'bbb', 'ccc')" );
967
+
968
+ assertUpdate (parallelWriter , "INSERT INTO " + tableName + " VALUES ('a0', 'b0', 'c0')" , 1 );
969
+ assertUpdate (parallelWriter , "INSERT INTO " + tableName + " VALUES ('a1', 'b1', 'c1')" , 1 );
970
+
971
+ assertQuery ("SELECT * from " + tableName , "VALUES ('a', 'b', 'c'), ('aa', 'bb', 'cc'), ('aaa', 'bbb', 'ccc'), ('a0', 'b0', 'c0'), ('a1', 'b1', 'c1')" );
972
+
973
+ assertUpdate (session , "DROP TABLE " + tableName );
974
+ assertFalse (getQueryRunner ().tableExists (session , tableName ));
975
+ }
976
+
866
977
@ Test
867
978
public void testCreatePartitionedBucketedTableAsFewRows ()
868
979
{
0 commit comments