@Test public void diffTablesTestRowCount() { List<ColumnMetadata> columns = ImmutableList.of( new ColumnMetadata("key", Schema.STRING, "key", true, false), new ColumnMetadata("value", Schema.STRING, "value", false, true)); TableMetadata metadata = new TableMetadata(columns, "desc"); Row row1 = Row.of("key", "sameKey", "value", "value1"); Row row2 = Row.of("key", "sameKey", "value", "value2"); Row row3 = Row.of("key", "diffKey", "value", "value3"); TableAnswerElement table0 = new TableAnswerElement(metadata); TableAnswerElement table1 = new TableAnswerElement(metadata).addRow(row1); TableAnswerElement table2 = new TableAnswerElement(metadata).addRow(row2); TableAnswerElement table13 = new TableAnswerElement(metadata).addRow(row1).addRow(row3); TableAnswerElement table123 = new TableAnswerElement(metadata).addRow(row1).addRow(row2).addRow(row3); assertThat(TableDiff.diffTables(table123, table0, true).getRows().size(), equalTo(3)); assertThat(TableDiff.diffTables(table0, table123, true).getRows().size(), equalTo(3)); assertThat(TableDiff.diffTables(table13, table2, true).getRows().size(), equalTo(2)); assertThat(TableDiff.diffTables(table2, table13, true).getRows().size(), equalTo(2)); assertThat(TableDiff.diffTables(table1, table13, true).getRows().size(), equalTo(1)); assertThat(TableDiff.diffTables(table13, table1, true).getRows().size(), equalTo(1)); }
public static TableAnswerElement diffTables( TableAnswerElement baseTable, TableAnswerElement deltaTable, boolean includeOneTableKeys) { checkArgument( baseTable .getMetadata() .getColumnMetadata() .equals(deltaTable.getMetadata().getColumnMetadata()), "Cannot diff tables with different column metadatas"); TableMetadata inputMetadata = baseTable.getMetadata(); TableAnswerElement diffTable = new TableAnswerElement(diffMetadata(inputMetadata)); Map<String, ColumnMetadata> diffColumnMap = diffTable.getMetadata().toColumnMap(); List<String> keyColumns = inputMetadata.getColumnMetadata().stream() .filter(ColumnMetadata::getIsKey) .map(ColumnMetadata::getName) .collect(Collectors.toList()); List<ColumnMetadata> valueColumns = inputMetadata.getColumnMetadata().stream() .filter(cm -> (!cm.getIsKey() && cm.getIsValue())) .collect(Collectors.toList()); Set<Object> baseKeys = new HashSet<>(); Iterator<Row> baseRows = baseTable.getRows().iterator(); Map<List<Object>, List<Row>> deltaMap = buildMap(deltaTable.getRows(), inputMetadata.getColumnMetadata()); while (baseRows.hasNext()) { Row baseRow = baseRows.next(); Object baseKey = baseRow.getKey(inputMetadata.getColumnMetadata()); baseKeys.add(baseKey); List<Row> deltaRows = deltaMap.get(baseKey); if (deltaRows == null) { if (includeOneTableKeys) { RowBuilder diffRowBuilder = Row.builder(diffColumnMap).putAll(baseRow, keyColumns); diffRowValues(diffRowBuilder, baseRow, null, inputMetadata); diffTable.addRow(diffRowBuilder.build()); } } else { for (Row deltaRow : deltaRows) { if (!baseRow.getValue(valueColumns).equals(deltaRow.getValue(valueColumns))) { RowBuilder diffRowBuilder = Row.builder(diffColumnMap).putAll(baseRow, keyColumns); diffRowValues(diffRowBuilder, baseRow, deltaRow, inputMetadata); diffTable.addRow(diffRowBuilder.build()); } } } } if (includeOneTableKeys) { Iterator<Row> deltaRows = deltaTable.getRows().iterator(); while (deltaRows.hasNext()) { Row deltaRow = deltaRows.next(); Object deltaKey = deltaRow.getKey(inputMetadata.getColumnMetadata()); if (baseKeys.contains(deltaKey)) { continue; } RowBuilder diffRowBuilder = Row.builder(diffColumnMap).putAll(deltaRow, keyColumns); diffRowValues(diffRowBuilder, null, deltaRow, inputMetadata); diffTable.addRow(diffRowBuilder.build()); } } return diffTable; }
TableDiff { public static TableAnswerElement diffTables( TableAnswerElement baseTable, TableAnswerElement deltaTable, boolean includeOneTableKeys) { checkArgument( baseTable .getMetadata() .getColumnMetadata() .equals(deltaTable.getMetadata().getColumnMetadata()), "Cannot diff tables with different column metadatas"); TableMetadata inputMetadata = baseTable.getMetadata(); TableAnswerElement diffTable = new TableAnswerElement(diffMetadata(inputMetadata)); Map<String, ColumnMetadata> diffColumnMap = diffTable.getMetadata().toColumnMap(); List<String> keyColumns = inputMetadata.getColumnMetadata().stream() .filter(ColumnMetadata::getIsKey) .map(ColumnMetadata::getName) .collect(Collectors.toList()); List<ColumnMetadata> valueColumns = inputMetadata.getColumnMetadata().stream() .filter(cm -> (!cm.getIsKey() && cm.getIsValue())) .collect(Collectors.toList()); Set<Object> baseKeys = new HashSet<>(); Iterator<Row> baseRows = baseTable.getRows().iterator(); Map<List<Object>, List<Row>> deltaMap = buildMap(deltaTable.getRows(), inputMetadata.getColumnMetadata()); while (baseRows.hasNext()) { Row baseRow = baseRows.next(); Object baseKey = baseRow.getKey(inputMetadata.getColumnMetadata()); baseKeys.add(baseKey); List<Row> deltaRows = deltaMap.get(baseKey); if (deltaRows == null) { if (includeOneTableKeys) { RowBuilder diffRowBuilder = Row.builder(diffColumnMap).putAll(baseRow, keyColumns); diffRowValues(diffRowBuilder, baseRow, null, inputMetadata); diffTable.addRow(diffRowBuilder.build()); } } else { for (Row deltaRow : deltaRows) { if (!baseRow.getValue(valueColumns).equals(deltaRow.getValue(valueColumns))) { RowBuilder diffRowBuilder = Row.builder(diffColumnMap).putAll(baseRow, keyColumns); diffRowValues(diffRowBuilder, baseRow, deltaRow, inputMetadata); diffTable.addRow(diffRowBuilder.build()); } } } } if (includeOneTableKeys) { Iterator<Row> deltaRows = deltaTable.getRows().iterator(); while (deltaRows.hasNext()) { Row deltaRow = deltaRows.next(); Object deltaKey = deltaRow.getKey(inputMetadata.getColumnMetadata()); if (baseKeys.contains(deltaKey)) { continue; } RowBuilder diffRowBuilder = Row.builder(diffColumnMap).putAll(deltaRow, keyColumns); diffRowValues(diffRowBuilder, null, deltaRow, inputMetadata); diffTable.addRow(diffRowBuilder.build()); } } return diffTable; } }
TableDiff { public static TableAnswerElement diffTables( TableAnswerElement baseTable, TableAnswerElement deltaTable, boolean includeOneTableKeys) { checkArgument( baseTable .getMetadata() .getColumnMetadata() .equals(deltaTable.getMetadata().getColumnMetadata()), "Cannot diff tables with different column metadatas"); TableMetadata inputMetadata = baseTable.getMetadata(); TableAnswerElement diffTable = new TableAnswerElement(diffMetadata(inputMetadata)); Map<String, ColumnMetadata> diffColumnMap = diffTable.getMetadata().toColumnMap(); List<String> keyColumns = inputMetadata.getColumnMetadata().stream() .filter(ColumnMetadata::getIsKey) .map(ColumnMetadata::getName) .collect(Collectors.toList()); List<ColumnMetadata> valueColumns = inputMetadata.getColumnMetadata().stream() .filter(cm -> (!cm.getIsKey() && cm.getIsValue())) .collect(Collectors.toList()); Set<Object> baseKeys = new HashSet<>(); Iterator<Row> baseRows = baseTable.getRows().iterator(); Map<List<Object>, List<Row>> deltaMap = buildMap(deltaTable.getRows(), inputMetadata.getColumnMetadata()); while (baseRows.hasNext()) { Row baseRow = baseRows.next(); Object baseKey = baseRow.getKey(inputMetadata.getColumnMetadata()); baseKeys.add(baseKey); List<Row> deltaRows = deltaMap.get(baseKey); if (deltaRows == null) { if (includeOneTableKeys) { RowBuilder diffRowBuilder = Row.builder(diffColumnMap).putAll(baseRow, keyColumns); diffRowValues(diffRowBuilder, baseRow, null, inputMetadata); diffTable.addRow(diffRowBuilder.build()); } } else { for (Row deltaRow : deltaRows) { if (!baseRow.getValue(valueColumns).equals(deltaRow.getValue(valueColumns))) { RowBuilder diffRowBuilder = Row.builder(diffColumnMap).putAll(baseRow, keyColumns); diffRowValues(diffRowBuilder, baseRow, deltaRow, inputMetadata); diffTable.addRow(diffRowBuilder.build()); } } } } if (includeOneTableKeys) { Iterator<Row> deltaRows = deltaTable.getRows().iterator(); while (deltaRows.hasNext()) { Row deltaRow = deltaRows.next(); Object deltaKey = deltaRow.getKey(inputMetadata.getColumnMetadata()); if (baseKeys.contains(deltaKey)) { continue; } RowBuilder diffRowBuilder = Row.builder(diffColumnMap).putAll(deltaRow, keyColumns); diffRowValues(diffRowBuilder, null, deltaRow, inputMetadata); diffTable.addRow(diffRowBuilder.build()); } } return diffTable; } private  TableDiff(); }
TableDiff { public static TableAnswerElement diffTables( TableAnswerElement baseTable, TableAnswerElement deltaTable, boolean includeOneTableKeys) { checkArgument( baseTable .getMetadata() .getColumnMetadata() .equals(deltaTable.getMetadata().getColumnMetadata()), "Cannot diff tables with different column metadatas"); TableMetadata inputMetadata = baseTable.getMetadata(); TableAnswerElement diffTable = new TableAnswerElement(diffMetadata(inputMetadata)); Map<String, ColumnMetadata> diffColumnMap = diffTable.getMetadata().toColumnMap(); List<String> keyColumns = inputMetadata.getColumnMetadata().stream() .filter(ColumnMetadata::getIsKey) .map(ColumnMetadata::getName) .collect(Collectors.toList()); List<ColumnMetadata> valueColumns = inputMetadata.getColumnMetadata().stream() .filter(cm -> (!cm.getIsKey() && cm.getIsValue())) .collect(Collectors.toList()); Set<Object> baseKeys = new HashSet<>(); Iterator<Row> baseRows = baseTable.getRows().iterator(); Map<List<Object>, List<Row>> deltaMap = buildMap(deltaTable.getRows(), inputMetadata.getColumnMetadata()); while (baseRows.hasNext()) { Row baseRow = baseRows.next(); Object baseKey = baseRow.getKey(inputMetadata.getColumnMetadata()); baseKeys.add(baseKey); List<Row> deltaRows = deltaMap.get(baseKey); if (deltaRows == null) { if (includeOneTableKeys) { RowBuilder diffRowBuilder = Row.builder(diffColumnMap).putAll(baseRow, keyColumns); diffRowValues(diffRowBuilder, baseRow, null, inputMetadata); diffTable.addRow(diffRowBuilder.build()); } } else { for (Row deltaRow : deltaRows) { if (!baseRow.getValue(valueColumns).equals(deltaRow.getValue(valueColumns))) { RowBuilder diffRowBuilder = Row.builder(diffColumnMap).putAll(baseRow, keyColumns); diffRowValues(diffRowBuilder, baseRow, deltaRow, inputMetadata); diffTable.addRow(diffRowBuilder.build()); } } } } if (includeOneTableKeys) { Iterator<Row> deltaRows = deltaTable.getRows().iterator(); while (deltaRows.hasNext()) { Row deltaRow = deltaRows.next(); Object deltaKey = deltaRow.getKey(inputMetadata.getColumnMetadata()); if (baseKeys.contains(deltaKey)) { continue; } RowBuilder diffRowBuilder = Row.builder(diffColumnMap).putAll(deltaRow, keyColumns); diffRowValues(diffRowBuilder, null, deltaRow, inputMetadata); diffTable.addRow(diffRowBuilder.build()); } } return diffTable; } private  TableDiff(); static String baseColumnName(String originalColumnName); static String deltaColumnName(String originalColumnName); static TableMetadata diffMetadata(TableMetadata inputMetadata); static TableAnswerElement diffTables(
      TableAnswerElement baseTable, TableAnswerElement deltaTable, boolean includeOneTableKeys); }
TableDiff { public static TableAnswerElement diffTables( TableAnswerElement baseTable, TableAnswerElement deltaTable, boolean includeOneTableKeys) { checkArgument( baseTable .getMetadata() .getColumnMetadata() .equals(deltaTable.getMetadata().getColumnMetadata()), "Cannot diff tables with different column metadatas"); TableMetadata inputMetadata = baseTable.getMetadata(); TableAnswerElement diffTable = new TableAnswerElement(diffMetadata(inputMetadata)); Map<String, ColumnMetadata> diffColumnMap = diffTable.getMetadata().toColumnMap(); List<String> keyColumns = inputMetadata.getColumnMetadata().stream() .filter(ColumnMetadata::getIsKey) .map(ColumnMetadata::getName) .collect(Collectors.toList()); List<ColumnMetadata> valueColumns = inputMetadata.getColumnMetadata().stream() .filter(cm -> (!cm.getIsKey() && cm.getIsValue())) .collect(Collectors.toList()); Set<Object> baseKeys = new HashSet<>(); Iterator<Row> baseRows = baseTable.getRows().iterator(); Map<List<Object>, List<Row>> deltaMap = buildMap(deltaTable.getRows(), inputMetadata.getColumnMetadata()); while (baseRows.hasNext()) { Row baseRow = baseRows.next(); Object baseKey = baseRow.getKey(inputMetadata.getColumnMetadata()); baseKeys.add(baseKey); List<Row> deltaRows = deltaMap.get(baseKey); if (deltaRows == null) { if (includeOneTableKeys) { RowBuilder diffRowBuilder = Row.builder(diffColumnMap).putAll(baseRow, keyColumns); diffRowValues(diffRowBuilder, baseRow, null, inputMetadata); diffTable.addRow(diffRowBuilder.build()); } } else { for (Row deltaRow : deltaRows) { if (!baseRow.getValue(valueColumns).equals(deltaRow.getValue(valueColumns))) { RowBuilder diffRowBuilder = Row.builder(diffColumnMap).putAll(baseRow, keyColumns); diffRowValues(diffRowBuilder, baseRow, deltaRow, inputMetadata); diffTable.addRow(diffRowBuilder.build()); } } } } if (includeOneTableKeys) { Iterator<Row> deltaRows = deltaTable.getRows().iterator(); while (deltaRows.hasNext()) { Row deltaRow = deltaRows.next(); Object deltaKey = deltaRow.getKey(inputMetadata.getColumnMetadata()); if (baseKeys.contains(deltaKey)) { continue; } RowBuilder diffRowBuilder = Row.builder(diffColumnMap).putAll(deltaRow, keyColumns); diffRowValues(diffRowBuilder, null, deltaRow, inputMetadata); diffTable.addRow(diffRowBuilder.build()); } } return diffTable; } private  TableDiff(); static String baseColumnName(String originalColumnName); static String deltaColumnName(String originalColumnName); static TableMetadata diffMetadata(TableMetadata inputMetadata); static TableAnswerElement diffTables(
      TableAnswerElement baseTable, TableAnswerElement deltaTable, boolean includeOneTableKeys); static final String COL_BASE_PREFIX; static final String COL_DELTA_PREFIX; static final String COL_KEY_PRESENCE; static final String COL_KEY_PRESENCE_DESC; static final String COL_KEY_STATUS_BOTH; static final String COL_KEY_STATUS_ONLY_BASE; static final String COL_KEY_STATUS_ONLY_DELTA; }