@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, 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 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 boolean isNull(int position) { checkReadablePosition(position); return valueIsNull != null && valueIsNull[position + arrayOffset]; }
private void checkReadablePosition(int position) { if (position < 0 || position >= getPositionCount()) { throw new IllegalArgumentException("position is not valid"); } } }
@Override public long getEstimatedDataSizeForStats(int position) { return isNull(position) ? 0 : Byte.BYTES; }
@Override public byte getByte(int position, int offset) { checkReadablePosition(position); if (offset != 0) { throw new IllegalArgumentException("offset must be zero"); } return values[position + arrayOffset]; }
@Override public String toString() { StringBuilder sb = new StringBuilder("ByteArrayBlock{"); sb.append("positionCount=").append(getPositionCount()); sb.append('}'); return sb.toString(); }
@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 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 void writePositionTo(int position, BlockBuilder blockBuilder) { checkReadablePosition(position); blockBuilder.writeByte(values[position + arrayOffset]); blockBuilder.closeEntry(); }
@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 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 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 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)); }