Skip to content

Commit

Permalink
Remove unused parameters in block assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
findepi committed Sep 19, 2022
1 parent d6a7ba9 commit 147fe24
Show file tree
Hide file tree
Showing 13 changed files with 103 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;

import static io.airlift.slice.SizeOf.SIZE_OF_BYTE;
import static io.airlift.slice.SizeOf.SIZE_OF_INT;
Expand All @@ -62,17 +61,17 @@ public abstract class AbstractTestBlock
{
private static final BlockEncodingSerde BLOCK_ENCODING_SERDE = new TestingBlockEncodingSerde(TESTING_TYPE_MANAGER::getType);

protected <T> void assertBlock(Block block, Supplier<BlockBuilder> newBlockBuilder, T[] expectedValues)
protected <T> void assertBlock(Block block, T[] expectedValues)
{
assertBlockSize(block);
assertRetainedSize(block);

assertBlockPositions(block, newBlockBuilder, expectedValues);
assertBlockPositions(copyBlockViaBlockSerde(block), newBlockBuilder, expectedValues);
assertBlockPositions(block, expectedValues);
assertBlockPositions(copyBlockViaBlockSerde(block), expectedValues);

Block blockWithNull = copyBlockViaBlockSerde(block).copyWithAppendedNull();
T[] expectedValuesWithNull = Arrays.copyOf(expectedValues, expectedValues.length + 1);
assertBlockPositions(blockWithNull, newBlockBuilder, expectedValuesWithNull);
assertBlockPositions(blockWithNull, expectedValuesWithNull);

assertBlockSize(block);
assertRetainedSize(block);
Expand Down Expand Up @@ -169,12 +168,12 @@ else if (type == MethodHandle.class) {
assertEquals(block.getRetainedSizeInBytes(), retainedSize);
}

protected <T> void assertBlockFilteredPositions(T[] expectedValues, Block block, Supplier<BlockBuilder> newBlockBuilder, int... positions)
protected <T> void assertBlockFilteredPositions(T[] expectedValues, Block block, int... positions)
{
Block filteredBlock = block.copyPositions(positions, 0, positions.length);
T[] filteredExpectedValues = filter(expectedValues, positions);
assertEquals(filteredBlock.getPositionCount(), positions.length);
assertBlock(filteredBlock, newBlockBuilder, filteredExpectedValues);
assertBlock(filteredBlock, filteredExpectedValues);
}

private static <T> T[] filter(T[] expectedValues, int[] positions)
Expand All @@ -187,11 +186,11 @@ private static <T> T[] filter(T[] expectedValues, int[] positions)
return prunedExpectedValues;
}

private <T> void assertBlockPositions(Block block, Supplier<BlockBuilder> newBlockBuilder, T[] expectedValues)
private <T> void assertBlockPositions(Block block, T[] expectedValues)
{
assertEquals(block.getPositionCount(), expectedValues.length);
for (int position = 0; position < block.getPositionCount(); position++) {
assertBlockPosition(block, newBlockBuilder, position, expectedValues[position], expectedValues.getClass().getComponentType());
assertBlockPosition(block, position, expectedValues[position]);
}
}

Expand Down Expand Up @@ -234,8 +233,7 @@ private void assertBlockSize(Block block)
assertEquals(block.getPositionsSizeInBytes(positions, positions.length - firstHalf.getPositionCount()), expectedSecondHalfSize);
}

// expectedValueType is required since otherwise the expected value type is unknown when expectedValue is null.
protected <T> void assertBlockPosition(Block block, Supplier<BlockBuilder> newBlockBuilder, int position, T expectedValue, Class<?> expectedValueType)
protected <T> void assertBlockPosition(Block block, int position, T expectedValue)
{
assertPositionValue(block, position, expectedValue);
assertPositionValue(block.getSingleValueBlock(position), 0, expectedValue);
Expand Down
48 changes: 24 additions & 24 deletions core/trino-main/src/test/java/io/trino/block/TestArrayBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,17 +46,17 @@ public void testWithFixedWidthBlock()
}

BlockBuilder blockBuilder = createBlockBuilderWithValues(expectedValues);
assertBlock(blockBuilder, () -> blockBuilder.newBlockBuilderLike(null), expectedValues);
assertBlock(blockBuilder.build(), () -> blockBuilder.newBlockBuilderLike(null), expectedValues);
assertBlockFilteredPositions(expectedValues, blockBuilder.build(), () -> blockBuilder.newBlockBuilderLike(null), 0, 1, 3, 4, 7);
assertBlockFilteredPositions(expectedValues, blockBuilder.build(), () -> blockBuilder.newBlockBuilderLike(null), 2, 3, 5, 6);
assertBlock(blockBuilder, expectedValues);
assertBlock(blockBuilder.build(), expectedValues);
assertBlockFilteredPositions(expectedValues, blockBuilder.build(), 0, 1, 3, 4, 7);
assertBlockFilteredPositions(expectedValues, blockBuilder.build(), 2, 3, 5, 6);

long[][] expectedValuesWithNull = alternatingNullValues(expectedValues);
BlockBuilder blockBuilderWithNull = createBlockBuilderWithValues(expectedValuesWithNull);
assertBlock(blockBuilderWithNull, () -> blockBuilder.newBlockBuilderLike(null), expectedValuesWithNull);
assertBlock(blockBuilderWithNull.build(), () -> blockBuilder.newBlockBuilderLike(null), expectedValuesWithNull);
assertBlockFilteredPositions(expectedValuesWithNull, blockBuilderWithNull.build(), () -> blockBuilder.newBlockBuilderLike(null), 0, 1, 5, 6, 7, 10, 11, 12, 15);
assertBlockFilteredPositions(expectedValuesWithNull, blockBuilderWithNull.build(), () -> blockBuilder.newBlockBuilderLike(null), 2, 3, 4, 9, 13, 14);
assertBlock(blockBuilderWithNull, expectedValuesWithNull);
assertBlock(blockBuilderWithNull.build(), expectedValuesWithNull);
assertBlockFilteredPositions(expectedValuesWithNull, blockBuilderWithNull.build(), 0, 1, 5, 6, 7, 10, 11, 12, 15);
assertBlockFilteredPositions(expectedValuesWithNull, blockBuilderWithNull.build(), 2, 3, 4, 9, 13, 14);
}

@Test
Expand All @@ -72,17 +72,17 @@ public void testWithVariableWidthBlock()

BlockBuilder blockBuilder = createBlockBuilderWithValues(expectedValues);

assertBlock(blockBuilder, () -> blockBuilder.newBlockBuilderLike(null), expectedValues);
assertBlock(blockBuilder.build(), () -> blockBuilder.newBlockBuilderLike(null), expectedValues);
assertBlockFilteredPositions(expectedValues, blockBuilder.build(), () -> blockBuilder.newBlockBuilderLike(null), 0, 1, 3, 4, 7);
assertBlockFilteredPositions(expectedValues, blockBuilder.build(), () -> blockBuilder.newBlockBuilderLike(null), 2, 3, 5, 6);
assertBlock(blockBuilder, expectedValues);
assertBlock(blockBuilder.build(), expectedValues);
assertBlockFilteredPositions(expectedValues, blockBuilder.build(), 0, 1, 3, 4, 7);
assertBlockFilteredPositions(expectedValues, blockBuilder.build(), 2, 3, 5, 6);

Slice[][] expectedValuesWithNull = alternatingNullValues(expectedValues);
BlockBuilder blockBuilderWithNull = createBlockBuilderWithValues(expectedValuesWithNull);
assertBlock(blockBuilderWithNull, () -> blockBuilder.newBlockBuilderLike(null), expectedValuesWithNull);
assertBlock(blockBuilderWithNull.build(), () -> blockBuilder.newBlockBuilderLike(null), expectedValuesWithNull);
assertBlockFilteredPositions(expectedValuesWithNull, blockBuilderWithNull.build(), () -> blockBuilder.newBlockBuilderLike(null), 0, 1, 5, 6, 7, 10, 11, 12, 15);
assertBlockFilteredPositions(expectedValuesWithNull, blockBuilderWithNull.build(), () -> blockBuilder.newBlockBuilderLike(null), 2, 3, 4, 9, 13, 14);
assertBlock(blockBuilderWithNull, expectedValuesWithNull);
assertBlock(blockBuilderWithNull.build(), expectedValuesWithNull);
assertBlockFilteredPositions(expectedValuesWithNull, blockBuilderWithNull.build(), 0, 1, 5, 6, 7, 10, 11, 12, 15);
assertBlockFilteredPositions(expectedValuesWithNull, blockBuilderWithNull.build(), 2, 3, 4, 9, 13, 14);
}

@Test
Expand All @@ -92,17 +92,17 @@ public void testWithArrayBlock()

BlockBuilder blockBuilder = createBlockBuilderWithValues(expectedValues);

assertBlock(blockBuilder, () -> blockBuilder.newBlockBuilderLike(null), expectedValues);
assertBlock(blockBuilder.build(), () -> blockBuilder.newBlockBuilderLike(null), expectedValues);
assertBlockFilteredPositions(expectedValues, blockBuilder.build(), () -> blockBuilder.newBlockBuilderLike(null), 0, 1, 3, 4, 7);
assertBlockFilteredPositions(expectedValues, blockBuilder.build(), () -> blockBuilder.newBlockBuilderLike(null), 2, 3, 5, 6);
assertBlock(blockBuilder, expectedValues);
assertBlock(blockBuilder.build(), expectedValues);
assertBlockFilteredPositions(expectedValues, blockBuilder.build(), 0, 1, 3, 4, 7);
assertBlockFilteredPositions(expectedValues, blockBuilder.build(), 2, 3, 5, 6);

long[][][] expectedValuesWithNull = alternatingNullValues(expectedValues);
BlockBuilder blockBuilderWithNull = createBlockBuilderWithValues(expectedValuesWithNull);
assertBlock(blockBuilderWithNull, () -> blockBuilder.newBlockBuilderLike(null), expectedValuesWithNull);
assertBlock(blockBuilderWithNull.build(), () -> blockBuilder.newBlockBuilderLike(null), expectedValuesWithNull);
assertBlockFilteredPositions(expectedValuesWithNull, blockBuilderWithNull.build(), () -> blockBuilder.newBlockBuilderLike(null), 0, 1, 5, 6, 7, 10, 11, 12, 15);
assertBlockFilteredPositions(expectedValuesWithNull, blockBuilderWithNull.build(), () -> blockBuilder.newBlockBuilderLike(null), 2, 3, 4, 9, 13, 14);
assertBlock(blockBuilderWithNull, expectedValuesWithNull);
assertBlock(blockBuilderWithNull.build(), expectedValuesWithNull);
assertBlockFilteredPositions(expectedValuesWithNull, blockBuilderWithNull.build(), 0, 1, 5, 6, 7, 10, 11, 12, 15);
assertBlockFilteredPositions(expectedValuesWithNull, blockBuilderWithNull.build(), 2, 3, 4, 9, 13, 14);
}

private static long[][][] createExpectedValues()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void testCopyPositions()
{
Slice[] expectedValues = alternatingNullValues(createTestValue(17));
BlockBuilder blockBuilder = createBlockBuilderWithValues(expectedValues);
assertBlockFilteredPositions(expectedValues, blockBuilder.build(), () -> blockBuilder.newBlockBuilderLike(null), 0, 2, 4, 6, 7, 9, 10, 16);
assertBlockFilteredPositions(expectedValues, blockBuilder.build(), 0, 2, 4, 6, 7, 9, 10, 16);
}

@Test
Expand Down Expand Up @@ -84,8 +84,8 @@ public void testCompactBlock()
private void assertFixedWithValues(Slice[] expectedValues)
{
BlockBuilder blockBuilder = createBlockBuilderWithValues(expectedValues);
assertBlock(blockBuilder, () -> blockBuilder.newBlockBuilderLike(null), expectedValues);
assertBlock(blockBuilder.build(), () -> blockBuilder.newBlockBuilderLike(null), expectedValues);
assertBlock(blockBuilder, expectedValues);
assertBlock(blockBuilder.build(), expectedValues);
}

private static BlockBuilder createBlockBuilderWithValues(Slice[] expectedValues)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void testConstructionUnnestDictionary()

Block block = DictionaryBlock.create(2, dictionary, new int[] {1, 3});
assertThat(block).isInstanceOf(DictionaryBlock.class);
assertBlock(block, TestDictionaryBlock::createBlockBuilder, new Slice[] {expectedValues[3], expectedValues[7]});
assertBlock(block, new Slice[] {expectedValues[3], expectedValues[7]});

Block actualDictionary = ((DictionaryBlock) block).getDictionary();
assertThat(actualDictionary).isSameAs(innerDictionary);
Expand Down Expand Up @@ -134,8 +134,8 @@ public void testCopyPositionsWithCompaction()

assertEquals(copiedBlock.getDictionary().getPositionCount(), 1);
assertEquals(copiedBlock.getPositionCount(), positionsToCopy.length);
assertBlock(copiedBlock.getDictionary(), TestDictionaryBlock::createBlockBuilder, new Slice[] {firstExpectedValue});
assertBlock(copiedBlock, TestDictionaryBlock::createBlockBuilder, new Slice[] {
assertBlock(copiedBlock.getDictionary(), new Slice[] {firstExpectedValue});
assertBlock(copiedBlock, new Slice[] {
firstExpectedValue, firstExpectedValue, firstExpectedValue, firstExpectedValue, firstExpectedValue});
}

Expand All @@ -151,7 +151,7 @@ public void testCopyPositionsWithCompactionsAndReorder()
assertEquals(copiedBlock.getDictionary().getPositionCount(), 2);
assertEquals(copiedBlock.getPositionCount(), positionsToCopy.length);

assertBlock(copiedBlock.getDictionary(), TestDictionaryBlock::createBlockBuilder, new Slice[] {expectedValues[0], expectedValues[5]});
assertBlock(copiedBlock.getDictionary(), new Slice[] {expectedValues[0], expectedValues[5]});
assertDictionaryIds(copiedBlock, 0, 1, 0, 1, 0);
}

Expand All @@ -167,7 +167,7 @@ public void testCopyPositionsSamePosition()
assertEquals(copiedBlock.getDictionary().getPositionCount(), 1);
assertEquals(copiedBlock.getPositionCount(), positionsToCopy.length);

assertBlock(copiedBlock.getDictionary(), TestDictionaryBlock::createBlockBuilder, new Slice[] {expectedValues[2]});
assertBlock(copiedBlock.getDictionary(), new Slice[] {expectedValues[2]});
assertDictionaryIds(copiedBlock, 0, 0, 0);
}

Expand All @@ -181,7 +181,7 @@ public void testCopyPositionsNoCompaction()
DictionaryBlock copiedBlock = (DictionaryBlock) dictionaryBlock.copyPositions(positionsToCopy, 0, positionsToCopy.length);

assertEquals(copiedBlock.getPositionCount(), positionsToCopy.length);
assertBlock(copiedBlock.getDictionary(), TestDictionaryBlock::createBlockBuilder, expectedValues);
assertBlock(copiedBlock.getDictionary(), expectedValues);
}

@Test
Expand All @@ -195,7 +195,7 @@ public void testCompact()
assertNotEquals(dictionaryBlock.getDictionarySourceId(), compactBlock.getDictionarySourceId());

assertEquals(compactBlock.getDictionary().getPositionCount(), (expectedValues.length / 2) + 1);
assertBlock(compactBlock.getDictionary(), TestDictionaryBlock::createBlockBuilder, new Slice[] {expectedValues[0], expectedValues[1], expectedValues[3]});
assertBlock(compactBlock.getDictionary(), new Slice[] {expectedValues[0], expectedValues[1], expectedValues[3]});
assertDictionaryIds(compactBlock, 0, 1, 1, 2, 2, 0, 1, 1, 2, 2);
assertEquals(compactBlock.isCompact(), true);

Expand Down Expand Up @@ -224,33 +224,33 @@ public void testBasicGetPositions()
{
Slice[] expectedValues = createExpectedValues(10);
Block dictionaryBlock = DictionaryBlock.create(6, createSlicesBlock(expectedValues), new int[] {0, 1, 2, 3, 4, 5});
assertBlock(dictionaryBlock, TestDictionaryBlock::createBlockBuilder, new Slice[] {
assertBlock(dictionaryBlock, new Slice[] {
expectedValues[0], expectedValues[1], expectedValues[2], expectedValues[3], expectedValues[4], expectedValues[5]});
DictionaryId dictionaryId = ((DictionaryBlock) dictionaryBlock).getDictionarySourceId();

// first getPositions
dictionaryBlock = dictionaryBlock.getPositions(new int[] {0, 8, 1, 2, 4, 5, 7, 9}, 2, 4);
assertBlock(dictionaryBlock, TestDictionaryBlock::createBlockBuilder, new Slice[] {expectedValues[1], expectedValues[2], expectedValues[4], expectedValues[5]});
assertBlock(dictionaryBlock, new Slice[] {expectedValues[1], expectedValues[2], expectedValues[4], expectedValues[5]});
assertEquals(((DictionaryBlock) dictionaryBlock).getDictionarySourceId(), dictionaryId);

// second getPositions
dictionaryBlock = dictionaryBlock.getPositions(new int[] {0, 1, 3, 0, 0}, 0, 3);
assertBlock(dictionaryBlock, TestDictionaryBlock::createBlockBuilder, new Slice[] {expectedValues[1], expectedValues[2], expectedValues[5]});
assertBlock(dictionaryBlock, new Slice[] {expectedValues[1], expectedValues[2], expectedValues[5]});
assertEquals(((DictionaryBlock) dictionaryBlock).getDictionarySourceId(), dictionaryId);

// third getPositions; we do not validate if -1 is an invalid position
dictionaryBlock = dictionaryBlock.getPositions(new int[] {-1, -1, 0, 1, 2}, 2, 3);
assertBlock(dictionaryBlock, TestDictionaryBlock::createBlockBuilder, new Slice[] {expectedValues[1], expectedValues[2], expectedValues[5]});
assertBlock(dictionaryBlock, new Slice[] {expectedValues[1], expectedValues[2], expectedValues[5]});
assertEquals(((DictionaryBlock) dictionaryBlock).getDictionarySourceId(), dictionaryId);

// mixed getPositions
dictionaryBlock = dictionaryBlock.getPositions(new int[] {0, 2, 2}, 0, 3);
assertBlock(dictionaryBlock, TestDictionaryBlock::createBlockBuilder, new Slice[] {expectedValues[1], expectedValues[5], expectedValues[5]});
assertBlock(dictionaryBlock, new Slice[] {expectedValues[1], expectedValues[5], expectedValues[5]});
assertEquals(((DictionaryBlock) dictionaryBlock).getDictionarySourceId(), dictionaryId);

// duplicated getPositions
dictionaryBlock = dictionaryBlock.getPositions(new int[] {1, 1, 1, 1, 1}, 0, 5);
assertBlock(dictionaryBlock, TestDictionaryBlock::createBlockBuilder, new Slice[] {
assertBlock(dictionaryBlock, new Slice[] {
expectedValues[5], expectedValues[5], expectedValues[5], expectedValues[5], expectedValues[5]});
assertEquals(((DictionaryBlock) dictionaryBlock).getDictionarySourceId(), dictionaryId);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void testCopyPositions()
{
Slice[] expectedValues = alternatingNullValues(createTestValue(17));
BlockBuilder blockBuilder = createBlockBuilderWithValues(expectedValues);
assertBlockFilteredPositions(expectedValues, blockBuilder.build(), () -> blockBuilder.newBlockBuilderLike(null), 0, 2, 4, 6, 7, 9, 10, 16);
assertBlockFilteredPositions(expectedValues, blockBuilder.build(), 0, 2, 4, 6, 7, 9, 10, 16);
}

@Test
Expand Down Expand Up @@ -85,8 +85,8 @@ public void testCompactBlock()
private void assertFixedWithValues(Slice[] expectedValues)
{
BlockBuilder blockBuilder = createBlockBuilderWithValues(expectedValues);
assertBlock(blockBuilder, () -> blockBuilder.newBlockBuilderLike(null), expectedValues);
assertBlock(blockBuilder.build(), () -> blockBuilder.newBlockBuilderLike(null), expectedValues);
assertBlock(blockBuilder, expectedValues);
assertBlock(blockBuilder.build(), expectedValues);
}

private static BlockBuilder createBlockBuilderWithValues(Slice[] expectedValues)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void testCopyPositions()
{
Slice[] expectedValues = alternatingNullValues(createTestValue(17));
BlockBuilder blockBuilder = createBlockBuilderWithValues(expectedValues);
assertBlockFilteredPositions(expectedValues, blockBuilder.build(), () -> blockBuilder.newBlockBuilderLike(null), 0, 2, 4, 6, 7, 9, 10, 16);
assertBlockFilteredPositions(expectedValues, blockBuilder.build(), 0, 2, 4, 6, 7, 9, 10, 16);
}

@Test
Expand Down Expand Up @@ -87,8 +87,8 @@ public void testCompactBlock()
private void assertFixedWithValues(Slice[] expectedValues)
{
BlockBuilder blockBuilder = createBlockBuilderWithValues(expectedValues);
assertBlock(blockBuilder, () -> blockBuilder.newBlockBuilderLike(null), expectedValues);
assertBlock(blockBuilder.build(), () -> blockBuilder.newBlockBuilderLike(null), expectedValues);
assertBlock(blockBuilder, expectedValues);
assertBlock(blockBuilder.build(), expectedValues);
}

private static BlockBuilder createBlockBuilderWithValues(Slice[] expectedValues)
Expand Down
Loading

0 comments on commit 147fe24

Please sign in to comment.