private static <T> void assertDictionaryBlock(Block block, T[] expectedValues) { DictionaryBlock dictionaryBlock = createTestDictionaryBlock(block); T[] expectedDictionaryValues = createTestDictionaryExpectedValues(expectedValues); assertBlock(dictionaryBlock, expectedDictionaryValues); assertColumnarArray(dictionaryBlock, expectedDictionaryValues); assertRunLengthEncodedBlock(dictionaryBlock, expectedDictionaryValues); }
public static <T> void assertBlock(Block block, T[] expectedValues) { assertBlockPositions(block, expectedValues); assertBlockPositions(copyBlock(block), expectedValues); }
public static <T> void assertBlockPosition(Block block, int position, T expectedValue) { assertPositionValue(block, position, expectedValue); assertPositionValue(block.getSingleValueBlock(position), 0, expectedValue); }
private static <T> void assertRunLengthEncodedBlock(Block block, T[] expectedValues) { for (int position = 0; position < block.getPositionCount(); position++) { RunLengthEncodedBlock runLengthEncodedBlock = createTestRleBlock(block, position); T[] expectedDictionaryValues = createTestRleExpectedValues(expectedValues, position); assertBlock(runLengthEncodedBlock, expectedDictionaryValues); assertColumnarRow(runLengthEncodedBlock, expectedDictionaryValues); } }
private static <T> void assertPositionValue(Block block, int position, T expectedValue) { if (expectedValue == null) { assertTrue(block.isNull(position)); return; } assertFalse(block.isNull(position)); if (expectedValue instanceof Slice) { Slice expectedSliceValue = (Slice) expectedValue; int length = block.getSliceLength(position); assertEquals(length, expectedSliceValue.length()); Slice actual = block.getSlice(position, 0, length); assertEquals(actual, expectedSliceValue); } else if (expectedValue instanceof Slice[]) { // array or row Block actual = block.getObject(position, Block.class); assertBlock(actual, (Slice[]) expectedValue); } else if (expectedValue instanceof Slice[][]) { // map Block actual = block.getObject(position, Block.class); // a map is exposed as a block alternating key and value entries, so we need to flatten the expected values array assertBlock(actual, flattenMapEntries((Slice[][]) expectedValue)); } else { throw new IllegalArgumentException(expectedValue.getClass().getName()); } }
private static <T> void assertBlockPositions(Block block, T[] expectedValues) { assertEquals(block.getPositionCount(), expectedValues.length); for (int position = 0; position < block.getPositionCount(); position++) { assertBlockPosition(block, position, expectedValues[position]); } }
private static void verifyBlock(Block block, Slice[][][] expectedValues) { assertBlock(block, expectedValues); assertColumnarMap(block, expectedValues); assertDictionaryBlock(block, expectedValues); assertRunLengthEncodedBlock(block, expectedValues); int offset = 1; int length = expectedValues.length - 2; Block blockRegion = block.getRegion(offset, length); Slice[][][] expectedValuesRegion = Arrays.copyOfRange(expectedValues, offset, offset + length); assertBlock(blockRegion, expectedValuesRegion); assertColumnarMap(blockRegion, expectedValuesRegion); assertDictionaryBlock(blockRegion, expectedValuesRegion); assertRunLengthEncodedBlock(blockRegion, expectedValuesRegion); }
@Test public void test() { Slice[][][] expectedValues = new Slice[MAP_SIZES.length][][]; for (int mapIndex = 0; mapIndex < MAP_SIZES.length; mapIndex++) { expectedValues[mapIndex] = new Slice[MAP_SIZES[mapIndex]][]; for (int entryIndex = 0; entryIndex < MAP_SIZES[mapIndex]; entryIndex++) { Slice[] entry = new Slice[2]; entry[0] = Slices.utf8Slice(String.format("key.%d.%d", mapIndex, entryIndex)); if (entryIndex % 3 != 1) { entry[1] = Slices.utf8Slice(String.format("value.%d.%d", mapIndex, entryIndex)); } expectedValues[mapIndex][entryIndex] = entry; } } BlockBuilder blockBuilder = createBlockBuilderWithValues(expectedValues); verifyBlock(blockBuilder, expectedValues); verifyBlock(blockBuilder.build(), expectedValues); Slice[][][] expectedValuesWithNull = alternatingNullValues(expectedValues); BlockBuilder blockBuilderWithNull = createBlockBuilderWithValues(expectedValuesWithNull); verifyBlock(blockBuilderWithNull, expectedValuesWithNull); verifyBlock(blockBuilderWithNull.build(), expectedValuesWithNull); }
public static <T> T[] createTestDictionaryExpectedValues(T[] expectedValues) { int[] dictionaryIndexes = createTestDictionaryIndexes(expectedValues.length); T[] expectedDictionaryValues = Arrays.copyOf(expectedValues, dictionaryIndexes.length); for (int i = 0; i < dictionaryIndexes.length; i++) { int dictionaryIndex = dictionaryIndexes[i]; T expectedValue = expectedValues[dictionaryIndex]; expectedDictionaryValues[i] = expectedValue; } return expectedDictionaryValues; }
private static <T> void assertRunLengthEncodedBlock(Block block, T[] expectedValues) { for (int position = 0; position < block.getPositionCount(); position++) { RunLengthEncodedBlock runLengthEncodedBlock = createTestRleBlock(block, position); T[] expectedDictionaryValues = createTestRleExpectedValues(expectedValues, position); assertBlock(runLengthEncodedBlock, expectedDictionaryValues); assertColumnarArray(runLengthEncodedBlock, expectedDictionaryValues); } }
private static <T> void assertColumnarRow(Block block, T[] expectedValues) { ColumnarRow columnarRow = toColumnarRow(block); assertEquals(columnarRow.getPositionCount(), expectedValues.length); for (int fieldId = 0; fieldId < FIELD_COUNT; fieldId++) { Block fieldBlock = columnarRow.getField(fieldId); int elementsPosition = 0; for (int position = 0; position < expectedValues.length; position++) { T expectedRow = expectedValues[position]; assertEquals(columnarRow.isNull(position), expectedRow == null); if (expectedRow == null) { continue; } Object expectedElement = Array.get(expectedRow, fieldId); assertBlockPosition(fieldBlock, elementsPosition, expectedElement); elementsPosition++; } } }
private static <T> void verifyBlock(Block block, T[] expectedValues) { assertBlock(block, expectedValues); assertColumnarArray(block, expectedValues); assertDictionaryBlock(block, expectedValues); assertRunLengthEncodedBlock(block, expectedValues); int offset = 1; int length = expectedValues.length - 2; Block blockRegion = block.getRegion(offset, length); T[] expectedValuesRegion = Arrays.copyOfRange(expectedValues, offset, offset + length); assertBlock(blockRegion, expectedValuesRegion); assertColumnarArray(blockRegion, expectedValuesRegion); assertDictionaryBlock(blockRegion, expectedValuesRegion); assertRunLengthEncodedBlock(blockRegion, expectedValuesRegion); }
@Test public void test() { Slice[][] expectedValues = new Slice[ARRAY_SIZES.length][]; for (int i = 0; i < ARRAY_SIZES.length; i++) { expectedValues[i] = new Slice[ARRAY_SIZES[i]]; for (int j = 0; j < ARRAY_SIZES[i]; j++) { if (j % 3 != 1) { expectedValues[i][j] = Slices.utf8Slice(String.format("%d.%d", i, j)); } } } BlockBuilder blockBuilder = createBlockBuilderWithValues(expectedValues); verifyBlock(blockBuilder, expectedValues); verifyBlock(blockBuilder.build(), expectedValues); Slice[][] expectedValuesWithNull = alternatingNullValues(expectedValues); BlockBuilder blockBuilderWithNull = createBlockBuilderWithValues(expectedValuesWithNull); verifyBlock(blockBuilderWithNull, expectedValuesWithNull); verifyBlock(blockBuilderWithNull.build(), expectedValuesWithNull); }
public static DictionaryBlock createTestDictionaryBlock(Block block) { int[] dictionaryIndexes = createTestDictionaryIndexes(block.getPositionCount()); return new DictionaryBlock(dictionaryIndexes.length, block, dictionaryIndexes); }
private static void assertDictionaryBlock(Block block, Slice[][][] expectedValues) { DictionaryBlock dictionaryBlock = createTestDictionaryBlock(block); Slice[][][] expectedDictionaryValues = createTestDictionaryExpectedValues(expectedValues); assertBlock(dictionaryBlock, expectedDictionaryValues); assertColumnarMap(dictionaryBlock, expectedDictionaryValues); assertRunLengthEncodedBlock(dictionaryBlock, expectedDictionaryValues); }
private static void assertRunLengthEncodedBlock(Block block, Slice[][][] expectedValues) { for (int position = 0; position < block.getPositionCount(); position++) { RunLengthEncodedBlock runLengthEncodedBlock = createTestRleBlock(block, position); Slice[][][] expectedDictionaryValues = createTestRleExpectedValues(expectedValues, position); assertBlock(runLengthEncodedBlock, expectedDictionaryValues); assertColumnarMap(runLengthEncodedBlock, expectedDictionaryValues); } }
private static <T> void assertColumnarArray(Block block, T[] expectedValues) { ColumnarArray columnarArray = toColumnarArray(block); assertEquals(columnarArray.getPositionCount(), expectedValues.length); Block elementsBlock = columnarArray.getElementsBlock(); int elementsPosition = 0; for (int position = 0; position < expectedValues.length; position++) { T expectedArray = expectedValues[position]; assertEquals(columnarArray.isNull(position), expectedArray == null); assertEquals(columnarArray.getLength(position), expectedArray == null ? 0 : Array.getLength(expectedArray)); for (int i = 0; i < columnarArray.getLength(position); i++) { Object expectedElement = Array.get(expectedArray, i); assertBlockPosition(elementsBlock, elementsPosition, expectedElement); elementsPosition++; } } }
private static <T> void verifyBlock(Block block, T[] expectedValues) { assertBlock(block, expectedValues); assertColumnarRow(block, expectedValues); assertDictionaryBlock(block, expectedValues); assertRunLengthEncodedBlock(block, expectedValues); int offset = 1; int length = expectedValues.length - 2; Block blockRegion = block.getRegion(offset, length); T[] expectedValuesRegion = Arrays.copyOfRange(expectedValues, offset, offset + length); assertBlock(blockRegion, expectedValuesRegion); assertColumnarRow(blockRegion, expectedValuesRegion); assertDictionaryBlock(blockRegion, expectedValuesRegion); assertRunLengthEncodedBlock(blockRegion, expectedValuesRegion); }
@Test public void test() { Slice[][] expectedValues = new Slice[POSITION_COUNT][]; for (int i = 0; i < POSITION_COUNT; i++) { expectedValues[i] = new Slice[FIELD_COUNT]; for (int j = 0; j < FIELD_COUNT; j++) { if (j % 3 != 1) { expectedValues[i][j] = Slices.utf8Slice(String.format("%d.%d", i, j)); } } } BlockBuilder blockBuilder = createBlockBuilderWithValues(expectedValues); verifyBlock(blockBuilder, expectedValues); verifyBlock(blockBuilder.build(), expectedValues); Slice[][] expectedValuesWithNull = alternatingNullValues(expectedValues); BlockBuilder blockBuilderWithNull = createBlockBuilderWithValues(expectedValuesWithNull); verifyBlock(blockBuilderWithNull, expectedValuesWithNull); verifyBlock(blockBuilderWithNull.build(), expectedValuesWithNull); }
private static <T> void assertDictionaryBlock(Block block, T[] expectedValues) { DictionaryBlock dictionaryBlock = createTestDictionaryBlock(block); T[] expectedDictionaryValues = createTestDictionaryExpectedValues(expectedValues); assertBlock(dictionaryBlock, expectedDictionaryValues); assertColumnarRow(dictionaryBlock, expectedDictionaryValues); assertRunLengthEncodedBlock(dictionaryBlock, expectedDictionaryValues); }