Skip to content

Commit

Permalink
Inline AbstractVariableWidthBlock
Browse files Browse the repository at this point in the history
  • Loading branch information
dain committed Oct 12, 2023
1 parent e92ccdb commit e026add
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 172 deletions.
17 changes: 17 additions & 0 deletions core/trino-spi/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,23 @@
<old>method io.trino.spi.block.Block io.trino.spi.type.RowType::getObject(io.trino.spi.block.Block, int)</old>
<new>method io.trino.spi.block.SqlRow io.trino.spi.type.RowType::getObject(io.trino.spi.block.Block, int)</new>
</item>
<item>
<code>java.class.removed</code>
<old>class io.trino.spi.block.AbstractVariableWidthBlock</old>
</item>
<item>
<code>java.method.removed</code>
<old>method io.airlift.slice.Slice io.trino.spi.block.VariableWidthBlock::getRawSlice(int)</old>
</item>
<item>
<code>java.method.removed</code>
<old>method boolean io.trino.spi.block.VariableWidthBlock::isEntryNull(int)</old>
</item>
<item>
<code>java.class.noLongerInheritsFromClass</code>
<old>class io.trino.spi.block.VariableWidthBlock</old>
<new>class io.trino.spi.block.VariableWidthBlock</new>
</item>
</differences>
</revapi.differences>
</analysisConfiguration>
Expand Down

This file was deleted.

147 changes: 127 additions & 20 deletions core/trino-spi/src/main/java/io/trino/spi/block/VariableWidthBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import io.airlift.slice.Slice;
import io.airlift.slice.SliceOutput;
import io.airlift.slice.Slices;
import io.airlift.slice.XxHash64;
import jakarta.annotation.Nullable;

import java.util.Optional;
Expand All @@ -35,7 +36,7 @@
import static io.trino.spi.block.BlockUtil.copyOffsetsAndAppendNull;

public class VariableWidthBlock
extends AbstractVariableWidthBlock
implements Block
{
private static final int INSTANCE_SIZE = instanceSize(VariableWidthBlock.class);

Expand Down Expand Up @@ -101,7 +102,6 @@ public int getRawSliceOffset(int position)
return getPositionOffset(position);
}

@Override
protected final int getPositionOffset(int position)
{
return offsets[position + arrayOffset];
Expand All @@ -114,18 +114,6 @@ public int getSliceLength(int position)
return getPositionOffset(position + 1) - getPositionOffset(position);
}

@Override
public boolean mayHaveNull()
{
return valueIsNull != null;
}

@Override
protected boolean isEntryNull(int position)
{
return valueIsNull != null && valueIsNull[position + arrayOffset];
}

@Override
public int getPositionCount()
{
Expand Down Expand Up @@ -174,6 +162,12 @@ public long getRetainedSizeInBytes()
return retainedSizeInBytes;
}

@Override
public long getEstimatedDataSizeForStats(int position)
{
return isNull(position) ? 0 : getSliceLength(position);
}

@Override
public void retainedBytesForEachPart(ObjLongConsumer<Object> consumer)
{
Expand All @@ -185,6 +179,119 @@ public void retainedBytesForEachPart(ObjLongConsumer<Object> consumer)
consumer.accept(this, INSTANCE_SIZE);
}

@Override
public byte getByte(int position, int offset)
{
checkReadablePosition(this, position);
return slice.getByte(getPositionOffset(position) + offset);
}

@Override
public short getShort(int position, int offset)
{
checkReadablePosition(this, position);
return slice.getShort(getPositionOffset(position) + offset);
}

@Override
public int getInt(int position, int offset)
{
checkReadablePosition(this, position);
return slice.getInt(getPositionOffset(position) + offset);
}

@Override
public long getLong(int position, int offset)
{
checkReadablePosition(this, position);
return slice.getLong(getPositionOffset(position) + offset);
}

@Override
public Slice getSlice(int position, int offset, int length)
{
checkReadablePosition(this, position);
return slice.slice(getPositionOffset(position) + offset, length);
}

@Override
public void writeSliceTo(int position, int offset, int length, SliceOutput output)
{
checkReadablePosition(this, position);
output.writeBytes(slice, getPositionOffset(position) + offset, length);
}

@Override
public boolean equals(int position, int offset, Block otherBlock, int otherPosition, int otherOffset, int length)
{
checkReadablePosition(this, position);
Slice rawSlice = slice;
if (getSliceLength(position) < length) {
return false;
}
return otherBlock.bytesEqual(otherPosition, otherOffset, rawSlice, getPositionOffset(position) + offset, length);
}

@Override
public boolean bytesEqual(int position, int offset, Slice otherSlice, int otherOffset, int length)
{
checkReadablePosition(this, position);
return slice.equals(getPositionOffset(position) + offset, length, otherSlice, otherOffset, length);
}

@Override
public long hash(int position, int offset, int length)
{
checkReadablePosition(this, position);
return XxHash64.hash(slice, getPositionOffset(position) + offset, length);
}

@Override
public int compareTo(int position, int offset, int length, Block otherBlock, int otherPosition, int otherOffset, int otherLength)
{
checkReadablePosition(this, position);
Slice rawSlice = slice;
if (getSliceLength(position) < length) {
throw new IllegalArgumentException("Length longer than value length");
}
return -otherBlock.bytesCompare(otherPosition, otherOffset, otherLength, rawSlice, getPositionOffset(position) + offset, length);
}

@Override
public int bytesCompare(int position, int offset, int length, Slice otherSlice, int otherOffset, int otherLength)
{
checkReadablePosition(this, position);
return slice.compareTo(getPositionOffset(position) + offset, length, otherSlice, otherOffset, otherLength);
}

@Override
public boolean mayHaveNull()
{
return valueIsNull != null;
}

@Override
public boolean isNull(int position)
{
checkReadablePosition(this, position);
return valueIsNull != null && valueIsNull[position + arrayOffset];
}

@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 = slice.copy(offset, entrySize);

return new VariableWidthBlock(0, 1, copy, new int[] {0, copy.length()}, null);
}

@Override
public Block copyPositions(int[] positions, int offset, int length)
{
Expand Down Expand Up @@ -229,12 +336,6 @@ public Block copyPositions(int[] positions, int offset, int length)
return new VariableWidthBlock(0, length, newSlice.slice(), newOffsets, newValueIsNull);
}

@Override
protected Slice getRawSlice(int position)
{
return slice;
}

@Override
public Block getRegion(int positionOffset, int length)
{
Expand All @@ -259,6 +360,12 @@ public Block copyRegion(int positionOffset, int length)
return new VariableWidthBlock(0, length, newSlice, newOffsets, newValueIsNull);
}

@Override
public String getEncodingName()
{
return VariableWidthBlockEncoding.NAME;
}

@Override
public Block copyWithAppendedNull()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public String getName()
public void writeBlock(BlockEncodingSerde blockEncodingSerde, SliceOutput sliceOutput, Block block)
{
// The down casts here are safe because it is the block itself the provides this encoding implementation.
AbstractVariableWidthBlock variableWidthBlock = (AbstractVariableWidthBlock) block;
VariableWidthBlock variableWidthBlock = (VariableWidthBlock) block;

int positionCount = variableWidthBlock.getPositionCount();
sliceOutput.appendInt(positionCount);
Expand All @@ -64,7 +64,7 @@ public void writeBlock(BlockEncodingSerde blockEncodingSerde, SliceOutput sliceO

sliceOutput
.appendInt(totalLength)
.writeBytes(variableWidthBlock.getRawSlice(0), variableWidthBlock.getPositionOffset(0), totalLength);
.writeBytes(variableWidthBlock.getRawSlice(), variableWidthBlock.getPositionOffset(0), totalLength);
}

@Override
Expand Down

0 comments on commit e026add

Please sign in to comment.