@Override public synchronized void updateTableStatistics(String databaseName, String tableName, Function<PartitionStatistics, PartitionStatistics> update) { PartitionStatistics currentStatistics = getTableStatistics(databaseName, tableName); PartitionStatistics updatedStatistics = update.apply(currentStatistics); Table originalTable = getTable(databaseName, tableName) .orElseThrow(() -> new TableNotFoundException(new SchemaTableName(databaseName, tableName))); Table modifiedTable = originalTable.deepCopy(); HiveBasicStatistics basicStatistics = updatedStatistics.getBasicStatistics(); modifiedTable.setParameters(updateStatisticsParameters(modifiedTable.getParameters(), basicStatistics)); alterTable(databaseName, tableName, modifiedTable); com.facebook.presto.hive.metastore.Table table = fromMetastoreApiTable(modifiedTable); OptionalLong rowCount = basicStatistics.getRowCount(); List<ColumnStatisticsObj> metastoreColumnStatistics = updatedStatistics.getColumnStatistics().entrySet().stream() .map(entry -> createMetastoreColumnStatistics(entry.getKey(), table.getColumn(entry.getKey()).get().getType(), entry.getValue(), rowCount)) .collect(toImmutableList()); if (!metastoreColumnStatistics.isEmpty()) { setTableColumnStatistics(databaseName, tableName, metastoreColumnStatistics); } Set<String> removedColumnStatistics = difference(currentStatistics.getColumnStatistics().keySet(), updatedStatistics.getColumnStatistics().keySet()); removedColumnStatistics.forEach(column -> deleteTableColumnStatistics(databaseName, tableName, column)); }
@Test(expected = InvalidOperationException.class) public void testAlterTableNoSuchDatabase() throws Exception { Table originalTable = testTables[2]; Table newTable = originalTable.deepCopy(); client.alter_table("no_such_database", originalTable.getTableName(), newTable); }
@Test(expected = InvalidOperationException.class) public void testAlterTableNoSuchTable() throws Exception { Table originalTable = testTables[2]; Table newTable = originalTable.deepCopy(); client.alter_table(originalTable.getDbName(), "no_such_table_name", newTable); }
@Test(expected = InvalidOperationException.class) public void testAlterTableNoSuchTableInThisDatabase() throws Exception { Table originalTable = testTables[2]; Table newTable = originalTable.deepCopy(); client.alter_table(OTHER_DATABASE, originalTable.getTableName(), newTable); }
@Test(expected = InvalidOperationException.class) public void alterTableBogusCatalog() throws TException { Table t = testTables[0].deepCopy(); t.getParameters().put("a", "b"); client.alter_table("nosuch", t.getDbName(), t.getTableName(), t); }
@Test(expected = InvalidOperationException.class) public void testAlterTableInvalidStorageDescriptorRemovePartitionColumn() throws Exception { Table originalTable = partitionedTable; Table newTable = originalTable.deepCopy(); newTable.getPartitionKeys().remove(0); client.alter_table(originalTable.getDbName(), originalTable.getTableName(), newTable); }
@Test public void testAlterTableNullDatabase() throws Exception { Table originalTable = testTables[0]; Table newTable = originalTable.deepCopy(); try { client.alter_table(null, originalTable.getTableName(), newTable); Assert.fail("Expected exception"); } catch (MetaException | TProtocolException ex) { } }
@Test public void testAlterTableNullTableName() throws Exception { Table originalTable = testTables[0]; Table newTable = originalTable.deepCopy(); try { client.alter_table(originalTable.getDbName(), null, newTable); Assert.fail("Expected exception"); } catch (MetaException | TProtocolException ex) { // Expected. } }
@Test(expected = InvalidOperationException.class) public void testAlterTableInvalidStorageDescriptorAlterPartitionColumnName() throws Exception { Table originalTable = partitionedTable; Table newTable = originalTable.deepCopy(); newTable.getPartitionKeys().get(0).setName("altered_name"); client.alter_table(originalTable.getDbName(), originalTable.getTableName(), newTable); }
@Test(expected = InvalidOperationException.class) public void testAlterTableInvalidTableNameInNew() throws Exception { Table originalTable = testTables[0]; Table newTable = originalTable.deepCopy(); newTable.setTableName("test_table;"); client.alter_table(originalTable.getDbName(), originalTable.getTableName(), newTable); }
@Test(expected = InvalidOperationException.class) public void testAlterTableEmptyTableNameInNew() throws Exception { Table originalTable = testTables[0]; Table newTable = originalTable.deepCopy(); newTable.setTableName(""); client.alter_table(originalTable.getDbName(), originalTable.getTableName(), newTable); }
@Test(expected = InvalidOperationException.class) public void testAlterTableInvalidStorageDescriptorInvalidColumnType() throws Exception { Table originalTable = testTables[0]; Table newTable = originalTable.deepCopy(); newTable.getSd().getCols().get(0).setType("xyz"); client.alter_table(originalTable.getDbName(), originalTable.getTableName(), newTable); }
@Test(expected = MetaException.class) public void testAlterTableNullDatabaseInNew() throws Exception { Table originalTable = testTables[0]; Table newTable = originalTable.deepCopy(); newTable.setDbName(null); client.alter_table(originalTable.getDbName(), originalTable.getTableName(), newTable); }
@Test(expected = MetaException.class) public void testAlterTableInvalidStorageDescriptorNullColumnType() throws Exception { Table originalTable = testTables[0]; Table newTable = originalTable.deepCopy(); newTable.getSd().getCols().get(0).setType(null); client.alter_table(originalTable.getDbName(), originalTable.getTableName(), newTable); }
@Test(expected = MetaException.class) public void testAlterTableNullStorageDescriptorInNew() throws Exception { Table originalTable = testTables[0]; Table newTable = originalTable.deepCopy(); newTable.setSd(null); client.alter_table(originalTable.getDbName(), originalTable.getTableName(), newTable); }
@Test(expected = MetaException.class) public void testAlterTableInvalidStorageDescriptorNullSerdeInfo() throws Exception { Table originalTable = testTables[0]; Table newTable = originalTable.deepCopy(); newTable.getSd().setSerdeInfo(null); client.alter_table(originalTable.getDbName(), originalTable.getTableName(), newTable); }
@Test(expected = MetaException.class) public void testAlterTableInvalidStorageDescriptorNullLocation() throws Exception { Table originalTable = testTables[0]; Table newTable = originalTable.deepCopy(); newTable.getSd().setLocation(null); client.alter_table(originalTable.getDbName(), originalTable.getTableName(), newTable); }
@Test(expected = InvalidOperationException.class) public void testAlterTableInvalidStorageDescriptorAddPartitionColumns() throws Exception { Table originalTable = testTables[0]; Table newTable = originalTable.deepCopy(); newTable.addToPartitionKeys(new FieldSchema("new_part", "int", "comment")); client.alter_table(originalTable.getDbName(), originalTable.getTableName(), newTable); }
@Test public void testAlterTableNullTableNameInNew() throws Exception { Table originalTable = testTables[0]; Table newTable = originalTable.deepCopy(); newTable.setTableName(null); try { client.alter_table(originalTable.getDbName(), originalTable.getTableName(), newTable); Assert.fail("Expected exception"); } catch (MetaException | TProtocolException ex) { // Expected. } }
@Test(expected = MetaException.class) public void testAlterTableInvalidStorageDescriptorNullCols() throws Exception { Table originalTable = testTables[0]; Table newTable = originalTable.deepCopy(); newTable.getSd().setCols(null); client.alter_table(originalTable.getDbName(), originalTable.getTableName(), newTable); }