FlattenedTableUtil { public FlattenedTable facadeWithDefaultRowIds(FlattenedTable inputTable, String origTableName, String flattenBy, UUID flattenId) { String newTableName = flattenedTableNameGenerator.createFlattenedTableName(origTableName, flattenBy, flattenId); Collection<TableShard> newTableShards = new ArrayList<>(); List<TableShard> inputTableShardsSorted = new ArrayList<>(inputTable.getShards()); inputTableShardsSorted.sort((s1, s2) -> Long.compare(s1.getLowestRowId(), s2.getLowestRowId())); Iterator<Long> origTableShardFirstRowIdIt = inputTable.getOriginalFirstRowIdsOfShards().stream().sorted().iterator(); for (TableShard inputTableShard : inputTableShardsSorted) { long origFirstRowId = origTableShardFirstRowIdIt.next(); Collection<StandardColumnShard> newColShards = new ArrayList<>(); for (StandardColumnShard inputColumnShard : inputTableShard.getColumns().values()) { NavigableMap<Long, ColumnPage> newPages = new TreeMap<>(); long nextFirstRowId = origFirstRowId; for (ColumnPage inputPage : inputColumnShard.getPages().values()) { ColumnPage newPage; newPage = columnPageFactory.createDefaultColumnPage(inputPage.getColumnPageDict(), inputPage.getValues(), nextFirstRowId, inputColumnShard.getName() + "#" + nextFirstRowId); nextFirstRowId += newPage.size(); newPages.put(newPage.getFirstRowId(), newPage); } StandardColumnShard newColShard = null; switch (inputColumnShard.getColumnType()) { case STRING: newColShard = columnShardFactory.createStandardStringColumnShard(inputColumnShard.getName(), newPages, ((StringStandardColumnShard) inputColumnShard).getColumnShardDictionary()); break; case LONG: newColShard = columnShardFactory.createStandardLongColumnShard(inputColumnShard.getName(), newPages, ((LongStandardColumnShard) inputColumnShard).getColumnShardDictionary()); break; case DOUBLE: newColShard = columnShardFactory.createStandardDoubleColumnShard(inputColumnShard.getName(), newPages, ((DoubleStandardColumnShard) inputColumnShard).getColumnShardDictionary()); break; } newColShards.add(newColShard); } newTableShards.add(tableFactory.createDefaultTableShard(newTableName, newColShards)); } return factory.createFlattenedTable(newTableName, newTableShards, inputTable.getOriginalFirstRowIdsOfShards()); }  FlattenedTable facadeWithDefaultRowIds(FlattenedTable inputTable, String origTableName, String flattenBy,
      UUID flattenId);  }
@Test public void adjustRowIdsDoesNotAffectOriginal() throws LoadException { String json = "[ { " + "\"a\": [ " + "{ \"b\": 1, \"d\":[99, 100] }, " + "{ \"b\": 2, \"d\":[] }" + "]" + ",\"c\" : [ 9, 10 ] }," + "{ " + "\"a\": [ " + "{ \"b\": 3, \"d\":[300,301,302] }, " + "{ \"b\": 4, \"d\":[303,304,305] }, " + "{ \"b\": 5, \"d\":[306,307,308] } " + "]" + ",\"c\" : [ 0 ]}" + " ]"; Table origTable = loadFromJson(100L, json); FlattenedTable flattenedTable = flattener.flattenTable(origTable, null, "a[*]", UUID.randomUUID()); for (StandardColumnShard colShard : flattenedTable.getShards().iterator().next().getColumns().values()) { ((AdjustableStandardColumnShard) colShard).adjustToFirstRowId(200L); } FlattenedTable facadedTable = flattenedTableUtil.facadeWithDefaultRowIds(flattenedTable, TABLE, "a[*]", UUID.randomUUID()); Assert.assertEquals(flattenedTable.getShards().size(), 1L, "Expected correct flattened shards size."); assertValidFirstRowId(flattenedTable.getShards().iterator().next(), 200L); Assert.assertEquals(facadedTable.getShards().size(), 1L, "Expected correct facaded shards size."); assertValidFirstRowId(facadedTable.getShards().iterator().next(), 100L); for (StandardColumnShard colShard : facadedTable.getShards().iterator().next().getColumns().values()) { ((AdjustableStandardColumnShard) colShard).adjustToFirstRowId(500L); } assertValidFirstRowId(flattenedTable.getShards().iterator().next(), 200L); assertValidFirstRowId(facadedTable.getShards().iterator().next(), 500L); }