diff --git a/hcatalog/webhcat/java-client/src/main/java/org/apache/hive/hcatalog/api/HCatCreateTableDesc.java b/hcatalog/webhcat/java-client/src/main/java/org/apache/hive/hcatalog/api/HCatCreateTableDesc.java index 234d32f..841c9eb 100644 --- a/hcatalog/webhcat/java-client/src/main/java/org/apache/hive/hcatalog/api/HCatCreateTableDesc.java +++ b/hcatalog/webhcat/java-client/src/main/java/org/apache/hive/hcatalog/api/HCatCreateTableDesc.java @@ -96,11 +96,16 @@ public static Builder create(String dbName, String tableName, List e : tblProps.entrySet()){ + newTable.getParameters().put(e.getKey(), e.getValue()); + } } if (isExternal) { @@ -110,8 +115,15 @@ Table toHiveTable(HiveConf conf) throws HCatException { newTable.setTableType(TableType.MANAGED_TABLE.toString()); } - StorageDescriptor sd = new StorageDescriptor(); - sd.setSerdeInfo(new SerDeInfo()); + // Initialize an sd if one does not exist + if (newTable.getSd() == null) { + newTable.setSd(new StorageDescriptor()); + } + StorageDescriptor sd = newTable.getSd(); + + if (sd.getSerdeInfo() == null){ + sd.setSerdeInfo(new SerDeInfo()); + } if (location != null) { sd.setLocation(location); } @@ -148,7 +160,6 @@ Table toHiveTable(HiveConf conf) throws HCatException { e); } } - newTable.setSd(sd); if(serdeParams != null) { for(Map.Entry param : serdeParams.entrySet()) { sd.getSerdeInfo().putToParameters(param.getKey(), param.getValue()); diff --git a/hcatalog/webhcat/java-client/src/test/java/org/apache/hive/hcatalog/api/TestHCatClient.java b/hcatalog/webhcat/java-client/src/test/java/org/apache/hive/hcatalog/api/TestHCatClient.java index 60195b3..04029ed 100644 --- a/hcatalog/webhcat/java-client/src/test/java/org/apache/hive/hcatalog/api/TestHCatClient.java +++ b/hcatalog/webhcat/java-client/src/test/java/org/apache/hive/hcatalog/api/TestHCatClient.java @@ -33,6 +33,7 @@ import org.apache.hadoop.hive.ql.io.IgnoreKeyTextOutputFormat; import org.apache.hadoop.hive.ql.io.RCFileInputFormat; import org.apache.hadoop.hive.ql.io.RCFileOutputFormat; +import org.apache.hadoop.hive.ql.metadata.Table; import org.apache.hadoop.hive.serde.serdeConstants; import org.apache.hadoop.hive.serde2.columnar.ColumnarSerDe; import org.apache.hadoop.mapred.TextInputFormat; @@ -48,6 +49,7 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -187,6 +189,59 @@ public void testBasicDDLCommands() throws Exception { client.close(); } + /** + * This test tests that a plain table instantiation matches what hive says an + * empty table create should look like. + * @throws Exception + */ + @Test + public void testEmptyTableInstantiation() throws Exception { + HCatClient client = HCatClient.create(new Configuration(hcatConf)); + + + String dbName = "default"; + String tblName = "testEmptyCreate"; + ArrayList cols = new ArrayList(); + cols.add(new HCatFieldSchema("id", Type.INT, "id comment")); + cols.add(new HCatFieldSchema("value", Type.STRING, "value comment")); + + // Create a minimalistic table + client.createTable(HCatCreateTableDesc + .create(dbName, tblName, cols) + .build()); + + HCatTable tCreated = client.getTable(dbName, tblName); + + org.apache.hadoop.hive.metastore.api.Table emptyTable = Table.getEmptyTable(dbName, tblName); + + Map createdProps = tCreated.getTblProps(); + Map emptyProps = emptyTable.getParameters(); + + mapEqualsContainedIn(emptyProps, createdProps); + + // Test sd params - we check that all the parameters in an empty table + // are retained as-is. We may add beyond it, but not change values for + // any parameters that hive defines for an empty table. + + Map createdSdParams = tCreated.getSerdeParams(); + Map emptySdParams = emptyTable.getSd().getSerdeInfo().getParameters(); + + mapEqualsContainedIn(emptySdParams, createdSdParams); + } + + /** + * Verifies that an inner map is present inside an outer map, with + * all values being equal. + */ + private void mapEqualsContainedIn(Map inner, Map outer) { + assertNotNull(inner); + assertNotNull(outer); + for ( Map.Entry e : inner.entrySet()){ + assertTrue(outer.containsKey(e.getKey())); + assertEquals(outer.get(e.getKey()), e.getValue()); + } + } + @Test public void testPartitionsHCatClientImpl() throws Exception { HCatClient client = HCatClient.create(new Configuration(hcatConf)); diff --git a/ql/src/java/org/apache/hadoop/hive/ql/metadata/Table.java b/ql/src/java/org/apache/hadoop/hive/ql/metadata/Table.java index ae3c11b..6b61fb1 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/metadata/Table.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/metadata/Table.java @@ -138,7 +138,7 @@ public void setTTable(org.apache.hadoop.hive.metastore.api.Table tTable) { /** * Initialize an emtpy table. */ - static org.apache.hadoop.hive.metastore.api.Table + public static org.apache.hadoop.hive.metastore.api.Table getEmptyTable(String databaseName, String tableName) { StorageDescriptor sd = new StorageDescriptor(); {