@Override public Block build() { if (!hasNonNullValue) { return new RunLengthEncodedBlock(NULL_VALUE_BLOCK, positionCount); } return new ByteArrayBlock(0, positionCount, hasNullValue ? valueIsNull : null, values); }
@Override public Block getSingleValueBlock(int position) { checkReadablePosition(position); return new ByteArrayBlock( 0, 1, valueIsNull[position] ? new boolean[] {true} : null, new byte[] {values[position]}); }
@Override public Block toBlock(Type desiredType) { checkArgument(BOOLEAN.equals(desiredType), "type doesn't match: %s", desiredType); int numberOfRecords = numberOfRecords(); return new ByteArrayBlock( numberOfRecords, Optional.ofNullable(nulls), booleans == null ? new byte[numberOfRecords] : toByteArray(booleans)); }
@Override public Block getSingleValueBlock(int position) { checkReadablePosition(position); return new ByteArrayBlock( 0, 1, isNull(position) ? new boolean[] {true} : null, new byte[] {values[position + arrayOffset]}); }
@Override public Block getRegion(int positionOffset, int length) { checkValidRegion(getPositionCount(), positionOffset, length); return new ByteArrayBlock(positionOffset + arrayOffset, length, valueIsNull, values); }
@Override public Block copyRegion(int positionOffset, int length) { checkValidRegion(getPositionCount(), positionOffset, length); if (!hasNonNullValue) { return new RunLengthEncodedBlock(NULL_VALUE_BLOCK, length); } boolean[] newValueIsNull = null; if (hasNullValue) { newValueIsNull = Arrays.copyOfRange(valueIsNull, positionOffset, positionOffset + length); } byte[] newValues = Arrays.copyOfRange(values, positionOffset, positionOffset + length); return new ByteArrayBlock(0, length, newValueIsNull, newValues); }
@Override public Block readBlock(BlockEncodingSerde blockEncodingSerde, SliceInput sliceInput) { int positionCount = sliceInput.readInt(); boolean[] valueIsNull = decodeNullBits(sliceInput, positionCount).orElse(null); byte[] values = new byte[positionCount]; for (int position = 0; position < positionCount; position++) { if (valueIsNull == null || !valueIsNull[position]) { values[position] = sliceInput.readByte(); } } return new ByteArrayBlock(0, positionCount, valueIsNull, values); } }
@Override public Block copyPositions(int[] positions, int offset, int length) { checkArrayRange(positions, offset, length); boolean[] newValueIsNull = null; if (valueIsNull != null) { newValueIsNull = new boolean[length]; } byte[] newValues = new byte[length]; for (int i = 0; i < length; i++) { int position = positions[offset + i]; checkReadablePosition(position); if (valueIsNull != null) { newValueIsNull[i] = valueIsNull[position + arrayOffset]; } newValues[i] = values[position + arrayOffset]; } return new ByteArrayBlock(0, length, newValueIsNull, newValues); }
@Override public Block getRegion(int positionOffset, int length) { checkValidRegion(getPositionCount(), positionOffset, length); if (!hasNonNullValue) { return new RunLengthEncodedBlock(NULL_VALUE_BLOCK, length); } return new ByteArrayBlock(positionOffset, length, hasNullValue ? valueIsNull : null, values); }
@Test public void testCompactBlock() { byte[] byteArray = {(byte) 0, (byte) 0, (byte) 1, (byte) 2, (byte) 3, (byte) 4}; boolean[] valueIsNull = {false, true, false, false, false, false}; testCompactBlock(new ByteArrayBlock(0, Optional.empty(), new byte[0])); testCompactBlock(new ByteArrayBlock(byteArray.length, Optional.of(valueIsNull), byteArray)); testIncompactBlock(new ByteArrayBlock(byteArray.length - 1, Optional.of(valueIsNull), byteArray)); }
@Test public void testCompactBlock() { byte[] byteArray = {(byte) 0, (byte) 0, (byte) 1, (byte) 2, (byte) 3, (byte) 4}; boolean[] valueIsNull = {false, true, false, false, false, false}; testCompactBlock(new ByteArrayBlock(0, Optional.empty(), new byte[0])); testCompactBlock(new ByteArrayBlock(byteArray.length, Optional.of(valueIsNull), byteArray)); testIncompactBlock(new ByteArrayBlock(byteArray.length - 1, Optional.of(valueIsNull), byteArray)); }
@Override public Block copyRegion(int positionOffset, int length) { checkValidRegion(getPositionCount(), positionOffset, length); positionOffset += arrayOffset; boolean[] newValueIsNull = valueIsNull == null ? null : compactArray(valueIsNull, positionOffset, length); byte[] newValues = compactArray(values, positionOffset, length); if (newValueIsNull == valueIsNull && newValues == values) { return this; } return new ByteArrayBlock(0, length, newValueIsNull, newValues); }
@Override public Block copyPositions(int[] positions, int offset, int length) { checkArrayRange(positions, offset, length); if (!hasNonNullValue) { return new RunLengthEncodedBlock(NULL_VALUE_BLOCK, length); } boolean[] newValueIsNull = null; if (hasNullValue) { newValueIsNull = new boolean[length]; } byte[] newValues = new byte[length]; for (int i = 0; i < length; i++) { int position = positions[offset + i]; checkReadablePosition(position); if (hasNullValue) { newValueIsNull[i] = valueIsNull[position]; } newValues[i] = values[position]; } return new ByteArrayBlock(0, length, newValueIsNull, newValues); }
public void testCompactBlock() { Block emptyValueBlock = new ByteArrayBlock(0, Optional.empty(), new byte[0]); Block compactValueBlock = new ByteArrayBlock(16, Optional.empty(), createExpectedValue(16).getBytes()); Block inCompactValueBlock = new ByteArrayBlock(16, Optional.empty(), createExpectedValue(17).getBytes()); int[] offsets = {0, 1, 1, 2, 4, 8, 16}; boolean[] valueIsNull = {false, true, false, false, false, false}; testCompactBlock(fromElementBlock(0, Optional.empty(), new int[1], emptyValueBlock)); testCompactBlock(fromElementBlock(valueIsNull.length, Optional.of(valueIsNull), offsets, compactValueBlock)); testIncompactBlock(fromElementBlock(valueIsNull.length - 1, Optional.of(valueIsNull), offsets, compactValueBlock)); // underlying value block is not compact testIncompactBlock(fromElementBlock(valueIsNull.length, Optional.of(valueIsNull), offsets, inCompactValueBlock)); }
public void testCompactBlock() { Block emptyValueBlock = new ByteArrayBlock(0, Optional.empty(), new byte[0]); Block compactValueBlock = new ByteArrayBlock(16, Optional.empty(), createExpectedValue(16).getBytes()); Block inCompactValueBlock = new ByteArrayBlock(16, Optional.empty(), createExpectedValue(17).getBytes()); int[] offsets = {0, 1, 1, 2, 4, 8, 16}; boolean[] valueIsNull = {false, true, false, false, false, false}; testCompactBlock(fromElementBlock(0, Optional.empty(), new int[1], emptyValueBlock)); testCompactBlock(fromElementBlock(valueIsNull.length, Optional.of(valueIsNull), offsets, compactValueBlock)); testIncompactBlock(fromElementBlock(valueIsNull.length - 1, Optional.of(valueIsNull), offsets, compactValueBlock)); // underlying value block is not compact testIncompactBlock(fromElementBlock(valueIsNull.length, Optional.of(valueIsNull), offsets, inCompactValueBlock)); }
@Test public void testCompactBlock() { Block emptyBlock = new ByteArrayBlock(0, Optional.empty(), new byte[0]); Block compactFieldBlock1 = new ByteArrayBlock(5, Optional.empty(), createExpectedValue(5).getBytes()); Block compactFieldBlock2 = new ByteArrayBlock(5, Optional.empty(), createExpectedValue(5).getBytes()); Block incompactFiledBlock1 = new ByteArrayBlock(5, Optional.empty(), createExpectedValue(6).getBytes()); Block incompactFiledBlock2 = new ByteArrayBlock(5, Optional.empty(), createExpectedValue(6).getBytes()); boolean[] rowIsNull = {false, true, false, false, false, false}; assertCompact(fromFieldBlocks(0, Optional.empty(), new Block[] {emptyBlock, emptyBlock})); assertCompact(fromFieldBlocks(rowIsNull.length, Optional.of(rowIsNull), new Block[] {compactFieldBlock1, compactFieldBlock2})); // TODO: add test case for a sliced RowBlock // underlying field blocks are not compact testIncompactBlock(fromFieldBlocks(rowIsNull.length, Optional.of(rowIsNull), new Block[] {incompactFiledBlock1, incompactFiledBlock2})); testIncompactBlock(fromFieldBlocks(rowIsNull.length, Optional.of(rowIsNull), new Block[] {incompactFiledBlock1, incompactFiledBlock2})); }
@Test public void testCompactBlock() { Block emptyBlock = new ByteArrayBlock(0, Optional.empty(), new byte[0]); Block compactFieldBlock1 = new ByteArrayBlock(5, Optional.empty(), createExpectedValue(5).getBytes()); Block compactFieldBlock2 = new ByteArrayBlock(5, Optional.empty(), createExpectedValue(5).getBytes()); Block incompactFiledBlock1 = new ByteArrayBlock(5, Optional.empty(), createExpectedValue(6).getBytes()); Block incompactFiledBlock2 = new ByteArrayBlock(5, Optional.empty(), createExpectedValue(6).getBytes()); boolean[] rowIsNull = {false, true, false, false, false, false}; assertCompact(fromFieldBlocks(0, Optional.empty(), new Block[] {emptyBlock, emptyBlock})); assertCompact(fromFieldBlocks(rowIsNull.length, Optional.of(rowIsNull), new Block[] {compactFieldBlock1, compactFieldBlock2})); // TODO: add test case for a sliced RowBlock // underlying field blocks are not compact testIncompactBlock(fromFieldBlocks(rowIsNull.length, Optional.of(rowIsNull), new Block[] {incompactFiledBlock1, incompactFiledBlock2})); testIncompactBlock(fromFieldBlocks(rowIsNull.length, Optional.of(rowIsNull), new Block[] {incompactFiledBlock1, incompactFiledBlock2})); }
@Test public void testCompactBlock() { Block emptyBlock = new ByteArrayBlock(0, Optional.empty(), new byte[0]); Block compactKeyBlock = new ByteArrayBlock(16, Optional.empty(), createExpectedValue(16).getBytes()); Block compactValueBlock = new ByteArrayBlock(16, Optional.empty(), createExpectedValue(16).getBytes()); Block inCompactKeyBlock = new ByteArrayBlock(16, Optional.empty(), createExpectedValue(17).getBytes()); Block inCompactValueBlock = new ByteArrayBlock(16, Optional.empty(), createExpectedValue(17).getBytes()); int[] offsets = {0, 1, 1, 2, 4, 8, 16}; boolean[] mapIsNull = {false, true, false, false, false, false}; testCompactBlock(mapType(TINYINT, TINYINT).createBlockFromKeyValue(Optional.empty(), new int[1], emptyBlock, emptyBlock)); testCompactBlock(mapType(TINYINT, TINYINT).createBlockFromKeyValue(Optional.of(mapIsNull), offsets, compactKeyBlock, compactValueBlock)); // TODO: Add test case for a sliced MapBlock // underlying key/value block is not compact testIncompactBlock(mapType(TINYINT, TINYINT).createBlockFromKeyValue(Optional.of(mapIsNull), offsets, inCompactKeyBlock, inCompactValueBlock)); }
@Test public void testCompactBlock() { Block emptyBlock = new ByteArrayBlock(0, Optional.empty(), new byte[0]); Block compactKeyBlock = new ByteArrayBlock(16, Optional.empty(), createExpectedValue(16).getBytes()); Block compactValueBlock = new ByteArrayBlock(16, Optional.empty(), createExpectedValue(16).getBytes()); Block inCompactKeyBlock = new ByteArrayBlock(16, Optional.empty(), createExpectedValue(17).getBytes()); Block inCompactValueBlock = new ByteArrayBlock(16, Optional.empty(), createExpectedValue(17).getBytes()); int[] offsets = {0, 1, 1, 2, 4, 8, 16}; boolean[] mapIsNull = {false, true, false, false, false, false}; testCompactBlock(mapType(TINYINT, TINYINT).createBlockFromKeyValue(Optional.empty(), new int[1], emptyBlock, emptyBlock)); testCompactBlock(mapType(TINYINT, TINYINT).createBlockFromKeyValue(Optional.of(mapIsNull), offsets, compactKeyBlock, compactValueBlock)); // TODO: Add test case for a sliced MapBlock // underlying key/value block is not compact testIncompactBlock(mapType(TINYINT, TINYINT).createBlockFromKeyValue(Optional.of(mapIsNull), offsets, inCompactKeyBlock, inCompactValueBlock)); }