Source:
static Table addTableToDatabase(Database prevDb, Database currentDb, Table table) { if (table == null) { return null; } else { final String tableName = table.getTypeName(); if (currentDb.getTables().get(tableName) == null) { final Table copy = currentDb.getTables().add(tableName); table.copyFields(copy); final Table materializer = table.getMaterializer(); copy.setMaterializer(addTableToDatabase(prevDb, currentDb, materializer)); if (materializer != null) { StreamSupport.stream(((Iterable<MaterializedViewInfo>) materializer.getViews()::iterator).spliterator(), false) .map(MaterializedViewInfo::getTypeName) .forEach(tblName -> addTableToDatabase(prevDb, currentDb, prevDb.getTables().get(tblName))); } final Column partCol = table.getPartitioncolumn(); if (partCol != null) { final Column copyPartCol = copy.getColumns().get(partCol.getTypeName()); copyPartCol.setNullable(false); copy.setPartitioncolumn(copyPartCol); copy.setIsreplicated(false); } assert table.getMaterializer() == null || table.getMaterializer().getViews().size() == copy.getMaterializer().getViews().size(); StreamSupport.stream(((Iterable<Column>)table.getColumns()::iterator).spliterator(), false) .forEach(col -> { final Column copyColumn = copy.getColumns().get(col.getTypeName()); final Column mvSource = col.getMatviewsource(); if (mvSource != null) { copyColumn.setMatviewsource( copy.getMaterializer().getColumns().get(mvSource.getTypeName())); } final MaterializedViewInfo info = col.getMatview(); if (info != null) { copyColumn.setMatview(new MaterializedViewInfo()); info.copyFields(copyColumn.getMatview()); } }); return copy; } else { return table; } } }

Target:
static Table addTableToDatabase(Database prevDb, Database currentDb, Table table) { if (table == null) { return null; } else { final String tableName = table.getTypeName(); Table targetTable = currentDb.getTables().get(tableName); if (targetTable == null) { final Table copy = currentDb.getTables().add(tableName); table.copyFields(copy); final Table materializer = table.getMaterializer(); copy.setMaterializer(addTableToDatabase(prevDb, currentDb, materializer)); if (materializer != null) { StreamSupport.stream(((Iterable<MaterializedViewInfo>) materializer.getViews()::iterator).spliterator(), false) .map(MaterializedViewInfo::getTypeName) .forEach(tblName -> addTableToDatabase(prevDb, currentDb, prevDb.getTables().get(tblName))); } final Column partCol = table.getPartitioncolumn(); if (partCol != null) { final Column copyPartCol = copy.getColumns().get(partCol.getTypeName()); copyPartCol.setNullable(false); copy.setPartitioncolumn(copyPartCol); copy.setIsreplicated(false); } assert table.getMaterializer() == null || table.getMaterializer().getViews().size() == copy.getMaterializer().getViews().size(); StreamSupport.stream(((Iterable<Column>)table.getColumns()::iterator).spliterator(), false) .forEach(col -> { final Column copyColumn = copy.getColumns().get(col.getTypeName()); final Column mvSource = col.getMatviewsource(); if (mvSource != null) { copyColumn.setMatviewsource( copy.getMaterializer().getColumns().get(mvSource.getTypeName())); } final MaterializedViewInfo info = col.getMatview(); if (info != null) { copyColumn.setMatview(new MaterializedViewInfo()); info.copyFields(copyColumn.getMatview()); } }); targetTable = copy; } return targetTable; } }