Fix VariableWidthBlockBuilder.copyRegion to return a compact block
@@ -28,6 +28,9 @@ import java.util.function.BiConsumer;
import static com.facebook.presto.spi.block.BlockUtil.MAX_ARRAY_SIZE;
import static com.facebook.presto.spi.block.BlockUtil.calculateBlockResetBytes;
import static com.facebook.presto.spi.block.BlockUtil.calculateBlockResetSize;
+import static com.facebook.presto.spi.block.BlockUtil.compactArray;
+import static com.facebook.presto.spi.block.BlockUtil.compactOffsets;
+import static com.facebook.presto.spi.block.BlockUtil.compactSlice;
import static io.airlift.slice.SizeOf.SIZE_OF_BYTE;
import static io.airlift.slice.SizeOf.SIZE_OF_INT;
import static io.airlift.slice.SizeOf.SIZE_OF_LONG;
@@ -299,9 +302,11 @@ public class VariableWidthBlockBuilder
throw new IndexOutOfBoundsException("Invalid position " + positionOffset + " in block with " + positionCount + " positions");
}
- int[] newOffsets = Arrays.copyOfRange(offsets, positionOffset, positionOffset + length + 1);
- boolean[] newValueIsNull = Arrays.copyOfRange(valueIsNull, positionOffset, positionOffset + length);
- return new VariableWidthBlock(length, sliceOutput.slice(), newOffsets, newValueIsNull);
+ int[] newOffsets = compactOffsets(offsets, positionOffset, length);
+ boolean[] newValueIsNull = compactArray(valueIsNull, positionOffset, length);
+ Slice slice = compactSlice(sliceOutput.getUnderlyingSlice(), offsets[positionOffset], newOffsets[length]);
+
+ return new VariableWidthBlock(length, slice, newOffsets, newValueIsNull);
}
@Override
