@Test public void createColumnQueriesForSuccessfulChangeSomeColumns() throws Exception { TableMeta oldMeta = new TableMeta(); TableMeta newMeta = new TableMeta(); ColumnInfo colInfo1 = new ColumnInfo("col1", "CHAR(1)", "COMMENT 1"); ColumnInfo colInfo2 = new ColumnInfo("col2", "DECIMAL(10,5)"); ColumnInfo colInfo3 = new ColumnInfo("col3", "STRING", "COMMENT-3"); ColumnInfo colInfo4 = new ColumnInfo("col4", "VARCHAR(10)", "COMMENT 4"); ColumnInfo colInfo5 = new ColumnInfo("col5", "STRING", "COMMENT 5"); ColumnInfo colInfo6 = new ColumnInfo("col6", "INT"); ColumnInfo colInfo7 = new ColumnInfo("col7", "DATE"); ColumnInfo colInfo8 = new ColumnInfo("col8", "BOOLEAN", "COMMENT 8"); List<ColumnInfo> oldColumns = Arrays.asList(colInfo1, colInfo2, colInfo3); oldMeta.setColumns(oldColumns); List<ColumnInfo> newColumns = Arrays.asList(colInfo1, colInfo5, colInfo6); oldMeta.setColumns(newColumns); Optional<List<String>> query = AlterTableQueryGenerator.createColumnQueries(oldColumns, newColumns, false); Assert.assertTrue(query.isPresent()); List<String> queries = query.get(); Assert.assertEquals("Expected number of column update queries were different.", 2, queries.size()); String[] expectedQueries = new String[]{" CHANGE COLUMN `col2` `col5` STRING COMMENT 'COMMENT 5'", " CHANGE COLUMN `col3` `col6` INT"}; Assert.assertArrayEquals("Column change queries were not equal ", expectedQueries, queries.toArray()); }
static Optional<List<String>> createColumnQueries(List<ColumnInfo> oldColumns, List<ColumnInfo> newColumns, boolean cascade) { if (isNullOrEmpty(oldColumns) || isNullOrEmpty(newColumns)) { LOG.error("oldColumns = {} or newColumns = {} was null.", oldColumns, newColumns); throw new IllegalArgumentException("Old or new columns cannot be empty."); } if (oldColumns.size() > newColumns.size()) { LOG.error("removing columns from hive table is not supported yet."); throw new IllegalArgumentException("removing columns is not allowed."); } List<String> queries = new LinkedList<>(); int i = 0; boolean foundChange = false; for (; i < oldColumns.size(); i++) { ColumnInfo oldColumn = oldColumns.get(i); ColumnInfo newColumn = newColumns.get(i); if (!oldColumn.equals(newColumn)) { foundChange = true; StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append(" CHANGE COLUMN `").append(oldColumn.getName()).append("` ") .append(QueryGenerationUtils.getColumnRepresentation(newColumn)); if(cascade){ queryBuilder.append(" CASCADE"); } queries.add(queryBuilder.toString()); } } if (i < newColumns.size()) { StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append(" ADD COLUMNS ( "); boolean first = true; for (; i < newColumns.size(); i++) { foundChange = true; ColumnInfo columnInfo = newColumns.get(i); if (!first) { queryBuilder.append(", "); } else { first = false; } queryBuilder.append(QueryGenerationUtils.getColumnRepresentation(columnInfo)); } queryBuilder.append(" )"); if(cascade){ queryBuilder.append(" CASCADE"); } queries.add(queryBuilder.toString()); } if (foundChange) { return Optional.of(queries); } else { return Optional.absent(); } }
AlterTableQueryGenerator implements QueryGenerator { static Optional<List<String>> createColumnQueries(List<ColumnInfo> oldColumns, List<ColumnInfo> newColumns, boolean cascade) { if (isNullOrEmpty(oldColumns) || isNullOrEmpty(newColumns)) { LOG.error("oldColumns = {} or newColumns = {} was null.", oldColumns, newColumns); throw new IllegalArgumentException("Old or new columns cannot be empty."); } if (oldColumns.size() > newColumns.size()) { LOG.error("removing columns from hive table is not supported yet."); throw new IllegalArgumentException("removing columns is not allowed."); } List<String> queries = new LinkedList<>(); int i = 0; boolean foundChange = false; for (; i < oldColumns.size(); i++) { ColumnInfo oldColumn = oldColumns.get(i); ColumnInfo newColumn = newColumns.get(i); if (!oldColumn.equals(newColumn)) { foundChange = true; StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append(" CHANGE COLUMN `").append(oldColumn.getName()).append("` ") .append(QueryGenerationUtils.getColumnRepresentation(newColumn)); if(cascade){ queryBuilder.append(" CASCADE"); } queries.add(queryBuilder.toString()); } } if (i < newColumns.size()) { StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append(" ADD COLUMNS ( "); boolean first = true; for (; i < newColumns.size(); i++) { foundChange = true; ColumnInfo columnInfo = newColumns.get(i); if (!first) { queryBuilder.append(", "); } else { first = false; } queryBuilder.append(QueryGenerationUtils.getColumnRepresentation(columnInfo)); } queryBuilder.append(" )"); if(cascade){ queryBuilder.append(" CASCADE"); } queries.add(queryBuilder.toString()); } if (foundChange) { return Optional.of(queries); } else { return Optional.absent(); } } }
AlterTableQueryGenerator implements QueryGenerator { static Optional<List<String>> createColumnQueries(List<ColumnInfo> oldColumns, List<ColumnInfo> newColumns, boolean cascade) { if (isNullOrEmpty(oldColumns) || isNullOrEmpty(newColumns)) { LOG.error("oldColumns = {} or newColumns = {} was null.", oldColumns, newColumns); throw new IllegalArgumentException("Old or new columns cannot be empty."); } if (oldColumns.size() > newColumns.size()) { LOG.error("removing columns from hive table is not supported yet."); throw new IllegalArgumentException("removing columns is not allowed."); } List<String> queries = new LinkedList<>(); int i = 0; boolean foundChange = false; for (; i < oldColumns.size(); i++) { ColumnInfo oldColumn = oldColumns.get(i); ColumnInfo newColumn = newColumns.get(i); if (!oldColumn.equals(newColumn)) { foundChange = true; StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append(" CHANGE COLUMN `").append(oldColumn.getName()).append("` ") .append(QueryGenerationUtils.getColumnRepresentation(newColumn)); if(cascade){ queryBuilder.append(" CASCADE"); } queries.add(queryBuilder.toString()); } } if (i < newColumns.size()) { StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append(" ADD COLUMNS ( "); boolean first = true; for (; i < newColumns.size(); i++) { foundChange = true; ColumnInfo columnInfo = newColumns.get(i); if (!first) { queryBuilder.append(", "); } else { first = false; } queryBuilder.append(QueryGenerationUtils.getColumnRepresentation(columnInfo)); } queryBuilder.append(" )"); if(cascade){ queryBuilder.append(" CASCADE"); } queries.add(queryBuilder.toString()); } if (foundChange) { return Optional.of(queries); } else { return Optional.absent(); } } AlterTableQueryGenerator(TableMeta oldMeta, TableMeta newMeta); }
AlterTableQueryGenerator implements QueryGenerator { static Optional<List<String>> createColumnQueries(List<ColumnInfo> oldColumns, List<ColumnInfo> newColumns, boolean cascade) { if (isNullOrEmpty(oldColumns) || isNullOrEmpty(newColumns)) { LOG.error("oldColumns = {} or newColumns = {} was null.", oldColumns, newColumns); throw new IllegalArgumentException("Old or new columns cannot be empty."); } if (oldColumns.size() > newColumns.size()) { LOG.error("removing columns from hive table is not supported yet."); throw new IllegalArgumentException("removing columns is not allowed."); } List<String> queries = new LinkedList<>(); int i = 0; boolean foundChange = false; for (; i < oldColumns.size(); i++) { ColumnInfo oldColumn = oldColumns.get(i); ColumnInfo newColumn = newColumns.get(i); if (!oldColumn.equals(newColumn)) { foundChange = true; StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append(" CHANGE COLUMN `").append(oldColumn.getName()).append("` ") .append(QueryGenerationUtils.getColumnRepresentation(newColumn)); if(cascade){ queryBuilder.append(" CASCADE"); } queries.add(queryBuilder.toString()); } } if (i < newColumns.size()) { StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append(" ADD COLUMNS ( "); boolean first = true; for (; i < newColumns.size(); i++) { foundChange = true; ColumnInfo columnInfo = newColumns.get(i); if (!first) { queryBuilder.append(", "); } else { first = false; } queryBuilder.append(QueryGenerationUtils.getColumnRepresentation(columnInfo)); } queryBuilder.append(" )"); if(cascade){ queryBuilder.append(" CASCADE"); } queries.add(queryBuilder.toString()); } if (foundChange) { return Optional.of(queries); } else { return Optional.absent(); } } AlterTableQueryGenerator(TableMeta oldMeta, TableMeta newMeta); TableMeta getOldMeta(); TableMeta getNewMeta(); String getQueryPerfix(); Optional<String> getQuery(); }
AlterTableQueryGenerator implements QueryGenerator { static Optional<List<String>> createColumnQueries(List<ColumnInfo> oldColumns, List<ColumnInfo> newColumns, boolean cascade) { if (isNullOrEmpty(oldColumns) || isNullOrEmpty(newColumns)) { LOG.error("oldColumns = {} or newColumns = {} was null.", oldColumns, newColumns); throw new IllegalArgumentException("Old or new columns cannot be empty."); } if (oldColumns.size() > newColumns.size()) { LOG.error("removing columns from hive table is not supported yet."); throw new IllegalArgumentException("removing columns is not allowed."); } List<String> queries = new LinkedList<>(); int i = 0; boolean foundChange = false; for (; i < oldColumns.size(); i++) { ColumnInfo oldColumn = oldColumns.get(i); ColumnInfo newColumn = newColumns.get(i); if (!oldColumn.equals(newColumn)) { foundChange = true; StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append(" CHANGE COLUMN `").append(oldColumn.getName()).append("` ") .append(QueryGenerationUtils.getColumnRepresentation(newColumn)); if(cascade){ queryBuilder.append(" CASCADE"); } queries.add(queryBuilder.toString()); } } if (i < newColumns.size()) { StringBuilder queryBuilder = new StringBuilder(); queryBuilder.append(" ADD COLUMNS ( "); boolean first = true; for (; i < newColumns.size(); i++) { foundChange = true; ColumnInfo columnInfo = newColumns.get(i); if (!first) { queryBuilder.append(", "); } else { first = false; } queryBuilder.append(QueryGenerationUtils.getColumnRepresentation(columnInfo)); } queryBuilder.append(" )"); if(cascade){ queryBuilder.append(" CASCADE"); } queries.add(queryBuilder.toString()); } if (foundChange) { return Optional.of(queries); } else { return Optional.absent(); } } AlterTableQueryGenerator(TableMeta oldMeta, TableMeta newMeta); TableMeta getOldMeta(); TableMeta getNewMeta(); String getQueryPerfix(); Optional<String> getQuery(); static List<String> SYSTEM_PROPERTY_LIST; }