@Override public Block build() { if (currentEntrySize > 0) { throw new IllegalStateException("Current entry must be closed before the block can be built"); } return new VariableWidthBlock(0, positions, sliceOutput.slice(), offsets, hasNullValue ? valueIsNull : null); }
private void setDictionaryBlockData(byte[] dictionaryData, int[] dictionaryOffsets, int positionCount) { verify(positionCount > 0); // only update the block if the array changed to prevent creation of new Block objects, since // the engine currently uses identity equality to test if dictionaries are the same if (currentDictionaryData != dictionaryData) { boolean[] isNullVector = new boolean[positionCount]; isNullVector[positionCount - 1] = true; dictionaryOffsets[positionCount] = dictionaryOffsets[positionCount - 1]; dictionaryBlock = new VariableWidthBlock(positionCount, wrappedBuffer(dictionaryData), dictionaryOffsets, Optional.of(isNullVector)); currentDictionaryData = dictionaryData; } }
@Override public Block getRegion(int positionOffset, int length) { checkValidRegion(getPositionCount(), positionOffset, length); return new VariableWidthBlock(positionOffset + arrayOffset, length, slice, offsets, valueIsNull); }
@Override public Block toBlock(Type desiredType) { checkArgument(desiredType.getJavaType() == Slice.class, "type doesn't match: %s", desiredType); Slice values = bytes == null ? Slices.EMPTY_SLICE : Slices.wrappedBuffer(bytes); int numberOfRecords = numberOfRecords(); return new VariableWidthBlock( numberOfRecords, values, calculateOffsets(sizes, nulls, numberOfRecords), Optional.ofNullable(nulls)); }
@Override public Block getSingleValueBlock(int position) { if (isNull(position)) { return new VariableWidthBlock(0, 1, EMPTY_SLICE, new int[] {0, 0}, new boolean[] {true}); } int offset = getPositionOffset(position); int entrySize = getSliceLength(position); Slice copy = Slices.copyOf(getRawSlice(position), offset, entrySize); return new VariableWidthBlock(0, 1, copy, new int[] {0, copy.length()}, null); }
@Override public Block getRegion(int positionOffset, int length) { int positionCount = getPositionCount(); checkValidRegion(positionCount, positionOffset, length); return new VariableWidthBlock(positionOffset, length, sliceOutput.slice(), offsets, hasNullValue ? valueIsNull : null); }
@Override public Block readBlock(BlockEncodingSerde blockEncodingSerde, SliceInput sliceInput) { int positionCount = sliceInput.readInt(); int[] offsets = new int[positionCount + 1]; sliceInput.readBytes(Slices.wrappedIntArray(offsets), SIZE_OF_INT, positionCount * SIZE_OF_INT); boolean[] valueIsNull = decodeNullBits(sliceInput, positionCount).orElse(null); int blockSize = sliceInput.readInt(); Slice slice = sliceInput.readSlice(blockSize); return new VariableWidthBlock(0, positionCount, slice, offsets, valueIsNull); } }
@Test public void testCompactBlock() { Slice compactSlice = Slices.copyOf(createExpectedValue(16)); Slice incompactSlice = Slices.copyOf(createExpectedValue(20)).slice(0, 16); int[] offsets = {0, 1, 1, 2, 4, 8, 16}; boolean[] valueIsNull = {false, true, false, false, false, false}; testCompactBlock(new VariableWidthBlock(0, EMPTY_SLICE, new int[1], Optional.empty())); testCompactBlock(new VariableWidthBlock(valueIsNull.length, compactSlice, offsets, Optional.of(valueIsNull))); testIncompactBlock(new VariableWidthBlock(valueIsNull.length - 1, compactSlice, offsets, Optional.of(valueIsNull))); // underlying slice is not compact testIncompactBlock(new VariableWidthBlock(valueIsNull.length, incompactSlice, offsets, Optional.of(valueIsNull))); }
@Override public Block copyRegion(int positionOffset, int length) { checkValidRegion(getPositionCount(), positionOffset, length); positionOffset += arrayOffset; int[] newOffsets = compactOffsets(offsets, positionOffset, length); Slice newSlice = compactSlice(slice, offsets[positionOffset], newOffsets[length]); boolean[] newValueIsNull = valueIsNull == null ? null : compactArray(valueIsNull, positionOffset, length); if (newOffsets == offsets && newSlice == slice && newValueIsNull == valueIsNull) { return this; } return new VariableWidthBlock(0, length, newSlice, newOffsets, newValueIsNull); }
@Override public Block copyRegion(int positionOffset, int length) { int positionCount = getPositionCount(); checkValidRegion(positionCount, positionOffset, length); int[] newOffsets = compactOffsets(offsets, positionOffset, length); boolean[] newValueIsNull = null; if (hasNullValue) { newValueIsNull = compactArray(valueIsNull, positionOffset, length); } Slice slice = compactSlice(sliceOutput.getUnderlyingSlice(), offsets[positionOffset], newOffsets[length]); return new VariableWidthBlock(0, length, slice, newOffsets, newValueIsNull); }
private static Block createVariableWidthBlock(int entries) { int[] offsets = new int[entries + 1]; DynamicSliceOutput dynamicSliceOutput = new DynamicSliceOutput(entries); for (int i = 0; i < entries; i++) { dynamicSliceOutput.writeByte(i); offsets[i + 1] = dynamicSliceOutput.size(); } return new VariableWidthBlock(entries, dynamicSliceOutput.slice(), offsets, Optional.empty()); } }
nextBatchSize = 0; if (totalLength == 0) { return new VariableWidthBlock(currentBatchSize, EMPTY_SLICE, offsetVector, Optional.ofNullable(isNullVector)); return new VariableWidthBlock(currentBatchSize, slice, offsetVector, Optional.ofNullable(isNullVector));
@Override public Block copyPositions(int[] positions, int offset, int length) { checkArrayRange(positions, offset, length); int finalLength = 0; for (int i = offset; i < offset + length; i++) { finalLength += getSliceLength(positions[i]); } SliceOutput newSlice = Slices.allocate(finalLength).getOutput(); int[] newOffsets = new int[length + 1]; boolean[] newValueIsNull = null; if (valueIsNull != null) { newValueIsNull = new boolean[length]; } for (int i = 0; i < length; i++) { int position = positions[offset + i]; if (!isEntryNull(position)) { newSlice.writeBytes(slice, getPositionOffset(position), getSliceLength(position)); } else if (newValueIsNull != null) { newValueIsNull[i] = true; } newOffsets[i + 1] = newSlice.size(); } return new VariableWidthBlock(0, length, newSlice.slice(), newOffsets, newValueIsNull); }
@Override public Block copyPositions(int[] positions, int offset, int length) { checkArrayRange(positions, offset, length); int finalLength = 0; for (int i = offset; i < offset + length; i++) { finalLength += getSliceLength(positions[i]); } SliceOutput newSlice = Slices.allocate(finalLength).getOutput(); int[] newOffsets = new int[length + 1]; boolean[] newValueIsNull = null; if (hasNullValue) { newValueIsNull = new boolean[length]; } for (int i = 0; i < length; i++) { int position = positions[offset + i]; if (isEntryNull(position)) { newValueIsNull[i] = true; } else { newSlice.writeBytes(sliceOutput.getUnderlyingSlice(), getPositionOffset(position), getSliceLength(position)); } newOffsets[i + 1] = newSlice.size(); } return new VariableWidthBlock(0, length, newSlice.slice(), newOffsets, newValueIsNull); }
@Override public Block build() { if (currentEntrySize > 0) { throw new IllegalStateException("Current entry must be closed before the block can be built"); } return new VariableWidthBlock(0, positions, sliceOutput.slice(), offsets, hasNullValue ? valueIsNull : null); }
@Override public Block getRegion(int positionOffset, int length) { checkValidRegion(getPositionCount(), positionOffset, length); return new VariableWidthBlock(positionOffset + arrayOffset, length, slice, offsets, valueIsNull); }
@Override public Block getRegion(int positionOffset, int length) { int positionCount = getPositionCount(); checkValidRegion(positionCount, positionOffset, length); return new VariableWidthBlock(positionOffset, length, sliceOutput.slice(), offsets, hasNullValue ? valueIsNull : null); }
@Override public Block getSingleValueBlock(int position) { if (isNull(position)) { return new VariableWidthBlock(0, 1, EMPTY_SLICE, new int[] {0, 0}, new boolean[] {true}); } int offset = getPositionOffset(position); int entrySize = getSliceLength(position); Slice copy = Slices.copyOf(getRawSlice(position), offset, entrySize); return new VariableWidthBlock(0, 1, copy, new int[] {0, copy.length()}, null); }
@Override public Block readBlock(BlockEncodingSerde blockEncodingSerde, SliceInput sliceInput) { int positionCount = sliceInput.readInt(); int[] offsets = new int[positionCount + 1]; sliceInput.readBytes(Slices.wrappedIntArray(offsets), SIZE_OF_INT, positionCount * SIZE_OF_INT); boolean[] valueIsNull = decodeNullBits(sliceInput, positionCount).orElse(null); int blockSize = sliceInput.readInt(); Slice slice = sliceInput.readSlice(blockSize); return new VariableWidthBlock(0, positionCount, slice, offsets, valueIsNull); } }
@Override public Block copyRegion(int positionOffset, int length) { int positionCount = getPositionCount(); checkValidRegion(positionCount, positionOffset, length); int[] newOffsets = compactOffsets(offsets, positionOffset, length); boolean[] newValueIsNull = null; if (hasNullValue) { newValueIsNull = compactArray(valueIsNull, positionOffset, length); } Slice slice = compactSlice(sliceOutput.getUnderlyingSlice(), offsets[positionOffset], newOffsets[length]); return new VariableWidthBlock(0, length, slice, newOffsets, newValueIsNull); }