From b8c426e5f33d93ab9e032f55f38f2682252bf767 Mon Sep 17 00:00:00 2001 From: Jiexi Lin Date: Wed, 12 Sep 2018 10:55:39 -0700 Subject: [PATCH] Fix creating non-bucketed empty partition Fix a bug where creating empty parition using CALL statement throws exceptions when using file based metastore implementation. --- .../facebook/presto/hive/HiveMetadata.java | 7 ++++++ .../hive/TestHiveIntegrationSmokeTest.java | 24 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/presto-hive/src/main/java/com/facebook/presto/hive/HiveMetadata.java b/presto-hive/src/main/java/com/facebook/presto/hive/HiveMetadata.java index cd0b7b2357a7..5234bf0f53a6 100644 --- a/presto-hive/src/main/java/com/facebook/presto/hive/HiveMetadata.java +++ b/presto-hive/src/main/java/com/facebook/presto/hive/HiveMetadata.java @@ -1220,6 +1220,13 @@ public Optional finishInsert(ConnectorSession session, Map, ComputedStatistics> partitionComputedStatistics = createComputedStatisticsToPartitionMap(computedStatistics, partitionedBy, columnTypes); for (PartitionUpdate partitionUpdate : partitionUpdates) { + if (partitionUpdate.getFileNames().size() == 0) { + HiveWriteUtils.createDirectory( + new HdfsContext(session, table.get().getDatabaseName(), table.get().getTableName()), + hdfsEnvironment, + partitionUpdate.getWritePath()); + } + if (partitionUpdate.getName().isEmpty()) { // insert into unpartitioned table metastore.finishInsertIntoExistingTable( diff --git a/presto-hive/src/test/java/com/facebook/presto/hive/TestHiveIntegrationSmokeTest.java b/presto-hive/src/test/java/com/facebook/presto/hive/TestHiveIntegrationSmokeTest.java index 2b00a9c2fbaf..b0781ce12264 100644 --- a/presto-hive/src/test/java/com/facebook/presto/hive/TestHiveIntegrationSmokeTest.java +++ b/presto-hive/src/test/java/com/facebook/presto/hive/TestHiveIntegrationSmokeTest.java @@ -937,6 +937,30 @@ public void testCastNullToColumnTypes() assertUpdate("DROP TABLE " + tableName); } + @Test + public void testCreateEmptyPartition() + { + String tableName = "empty_partition_table"; + assertUpdate(format("" + + "CREATE TABLE %s " + + "WITH ( " + + " FORMAT = 'ORC', " + + " partitioned_by = ARRAY['p_varchar'] " + + ") " + + "AS " + + "SELECT c_bigint, p_varchar " + + "FROM ( " + + " VALUES " + + " (BIGINT '7', 'longlonglong')" + + ") AS x (c_bigint, p_varchar)", tableName), 1); + assertQuery(format("SELECT count(*) FROM \"%s$partitions\"", tableName), "SELECT 1"); + + // create an empty partition + assertUpdate(format("CALL system.create_empty_partition('%s', '%s', ARRAY['p_varchar'], ARRAY['%s'])", TPCH_SCHEMA, tableName, "empty")); + assertQuery(format("SELECT count(*) FROM \"%s$partitions\"", tableName), "SELECT 2"); + assertUpdate("DROP TABLE " + tableName); + } + @Test public void testCreateEmptyBucketedPartition() {