Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -135,12 +135,6 @@ public void writeBytesTo(int position, int offset, int length, BlockBuilder bloc
block.writeBytesTo(position, offset, length, blockBuilder);
}

@Override
public void writePositionTo(int position, BlockBuilder blockBuilder)
{
block.writePositionTo(position, blockBuilder);
}

@Override
public boolean equals(int position, int offset, Block otherBlock, int otherPosition, int otherOffset, int length)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,21 @@
package io.trino.operator.aggregation;

import io.trino.spi.block.BlockBuilder;
import io.trino.spi.type.Type;

public class BlockBuilderCopier
public final class BlockBuilderCopier
{
private BlockBuilderCopier() {}

public static BlockBuilder copyBlockBuilder(BlockBuilder blockBuilder)
public static BlockBuilder copyBlockBuilder(Type type, BlockBuilder blockBuilder)
{
if (blockBuilder == null) {
return null;
}

BlockBuilder copy = blockBuilder.newBlockBuilderLike(null);
for (int i = 0; i < blockBuilder.getPositionCount(); i++) {
copy.appendStructure(blockBuilder.getSingleValueBlock(i));
type.appendTo(blockBuilder, i, copy);
}

return copy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,6 @@ public void reset()
@Override
public AccumulatorState copy()
{
return new SingleArrayAggregationState(copyBlockBuilder(blockBuilder), type);
return new SingleArrayAggregationState(copyBlockBuilder(type, blockBuilder), type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,6 @@ public void reset()
@Override
public AccumulatorState copy()
{
return new SingleMultimapAggregationState(keyType, valueType, copyBlockBuilder(keyBlockBuilder), copyBlockBuilder(valueBlockBuilder));
return new SingleMultimapAggregationState(keyType, valueType, copyBlockBuilder(keyType, keyBlockBuilder), copyBlockBuilder(valueType, valueBlockBuilder));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

import static io.airlift.slice.SizeOf.SIZE_OF_BYTE;
Expand Down Expand Up @@ -70,12 +69,6 @@ protected <T> void assertBlock(Block block, Supplier<BlockBuilder> newBlockBuild

assertBlockPositions(block, newBlockBuilder, expectedValues);
assertBlockPositions(copyBlockViaBlockSerde(block), newBlockBuilder, expectedValues);
assertBlockPositions(copyBlockViaWritePositionTo(block, newBlockBuilder), newBlockBuilder, expectedValues);
if (expectedValues.getClass().getComponentType().isArray() ||
expectedValues.getClass().getComponentType() == List.class ||
expectedValues.getClass().getComponentType() == Map.class) {
assertBlockPositions(copyBlockViaWriteStructure(block, newBlockBuilder), newBlockBuilder, expectedValues);
}

assertBlockSize(block);
assertRetainedSize(block);
Expand Down Expand Up @@ -249,16 +242,6 @@ protected <T> void assertBlockPosition(Block block, Supplier<BlockBuilder> newBl
assertPositionValue(copyBlockViaBlockSerde(block.getRegion(0, position + 1)), position, expectedValue);
assertPositionValue(copyBlockViaBlockSerde(block.getRegion(position, block.getPositionCount() - position)), 0, expectedValue);

assertPositionValue(copyBlockViaWritePositionTo(block.getRegion(position, 1), newBlockBuilder), 0, expectedValue);
assertPositionValue(copyBlockViaWritePositionTo(block.getRegion(0, position + 1), newBlockBuilder), position, expectedValue);
assertPositionValue(copyBlockViaWritePositionTo(block.getRegion(position, block.getPositionCount() - position), newBlockBuilder), 0, expectedValue);

if (expectedValueType.isArray() || expectedValueType == List.class || expectedValueType == Map.class) {
assertPositionValue(copyBlockViaWriteStructure(block.getRegion(position, 1), newBlockBuilder), 0, expectedValue);
assertPositionValue(copyBlockViaWriteStructure(block.getRegion(0, position + 1), newBlockBuilder), position, expectedValue);
assertPositionValue(copyBlockViaWriteStructure(block.getRegion(position, block.getPositionCount() - position), newBlockBuilder), 0, expectedValue);
}

assertPositionValue(block.copyRegion(position, 1), 0, expectedValue);
assertPositionValue(block.copyRegion(0, position + 1), position, expectedValue);
assertPositionValue(block.copyRegion(position, block.getPositionCount() - position), 0, expectedValue);
Expand Down Expand Up @@ -432,34 +415,6 @@ private static Block copyBlockViaBlockSerde(Block block)
return BLOCK_ENCODING_SERDE.readBlock(sliceOutput.slice().getInput());
}

private static Block copyBlockViaWritePositionTo(Block block, Supplier<BlockBuilder> newBlockBuilder)
{
BlockBuilder blockBuilder = newBlockBuilder.get();
for (int i = 0; i < block.getPositionCount(); i++) {
if (block.isNull(i)) {
blockBuilder.appendNull();
}
else {
block.writePositionTo(i, blockBuilder);
}
}
return blockBuilder.build();
}

private static Block copyBlockViaWriteStructure(Block block, Supplier<BlockBuilder> newBlockBuilder)
{
BlockBuilder blockBuilder = newBlockBuilder.get();
for (int i = 0; i < block.getPositionCount(); i++) {
if (block.isNull(i)) {
blockBuilder.appendNull();
}
else {
blockBuilder.appendStructure(block.getObject(i, Block.class));
}
}
return blockBuilder.build();
}

private static Block toSingeValuedBlock(Slice expectedValue)
{
BlockBuilder blockBuilder = VARBINARY.createBlockBuilder(null, 1, expectedValue.length());
Expand Down Expand Up @@ -504,15 +459,6 @@ protected static <T> T[] alternatingNullValues(T[] objects)
return objectsWithNulls;
}

protected static Slice[] createExpectedUniqueValues(int positionCount)
{
Slice[] expectedValues = new Slice[positionCount];
for (int position = 0; position < positionCount; position++) {
expectedValues[position] = Slices.copyOf(createExpectedValue(position));
}
return expectedValues;
}

protected static void assertEstimatedDataSizeForStats(BlockBuilder blockBuilder, Slice[] expectedSliceValues)
{
Block block = blockBuilder.build();
Expand Down
19 changes: 11 additions & 8 deletions core/trino-main/src/test/java/io/trino/block/BlockAssertions.java
Original file line number Diff line number Diff line change
Expand Up @@ -498,33 +498,36 @@ public static Block createRowBlock(List<Type> fieldTypes, Object[]... rows)
rowBlockBuilder.appendNull();
continue;
}
verify(row.length == fieldTypes.size());
BlockBuilder singleRowBlockWriter = rowBlockBuilder.beginBlockEntry();
for (Object fieldValue : row) {
for (int fieldIndex = 0; fieldIndex < fieldTypes.size(); fieldIndex++) {
Type fieldType = fieldTypes.get(fieldIndex);
Object fieldValue = row[fieldIndex];
if (fieldValue == null) {
singleRowBlockWriter.appendNull();
continue;
}

if (fieldValue instanceof String) {
VARCHAR.writeSlice(singleRowBlockWriter, utf8Slice((String) fieldValue));
fieldType.writeSlice(singleRowBlockWriter, utf8Slice((String) fieldValue));
}
else if (fieldValue instanceof Slice) {
VARBINARY.writeSlice(singleRowBlockWriter, (Slice) fieldValue);
fieldType.writeSlice(singleRowBlockWriter, (Slice) fieldValue);
}
else if (fieldValue instanceof Double) {
DOUBLE.writeDouble(singleRowBlockWriter, (Double) fieldValue);
fieldType.writeDouble(singleRowBlockWriter, (Double) fieldValue);
}
else if (fieldValue instanceof Long) {
BIGINT.writeLong(singleRowBlockWriter, (Long) fieldValue);
fieldType.writeLong(singleRowBlockWriter, (Long) fieldValue);
}
else if (fieldValue instanceof Boolean) {
BOOLEAN.writeBoolean(singleRowBlockWriter, (Boolean) fieldValue);
fieldType.writeBoolean(singleRowBlockWriter, (Boolean) fieldValue);
}
else if (fieldValue instanceof Block) {
singleRowBlockWriter.appendStructure((Block) fieldValue);
fieldType.writeObject(singleRowBlockWriter, fieldValue);
}
else if (fieldValue instanceof Integer) {
INTEGER.writeLong(singleRowBlockWriter, (Integer) fieldValue);
fieldType.writeLong(singleRowBlockWriter, (Integer) fieldValue);
}
else {
throw new IllegalArgumentException();
Expand Down
16 changes: 8 additions & 8 deletions core/trino-main/src/test/java/io/trino/block/TestArrayBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,20 +197,20 @@ private static BlockBuilder createBlockBuilderWithValues(long[][][] expectedValu
blockBuilder.appendNull();
}
else {
BlockBuilder intermediateBlockBuilder = new ArrayBlockBuilder(BIGINT, null, 100, 100);
BlockBuilder intermediateBlockBuilder = blockBuilder.beginBlockEntry();
for (int j = 0; j < expectedValue.length; j++) {
if (expectedValue[j] == null) {
intermediateBlockBuilder.appendNull();
}
else {
BlockBuilder innerMostBlockBuilder = BIGINT.createBlockBuilder(null, expectedValue.length);
BlockBuilder innerMostBlockBuilder = intermediateBlockBuilder.beginBlockEntry();
for (long v : expectedValue[j]) {
BIGINT.writeLong(innerMostBlockBuilder, v);
}
intermediateBlockBuilder.appendStructure(innerMostBlockBuilder.build());
intermediateBlockBuilder.closeEntry();
}
}
blockBuilder.appendStructure(intermediateBlockBuilder.build());
blockBuilder.closeEntry();
}
}
return blockBuilder;
Expand All @@ -229,11 +229,11 @@ private static BlockBuilder writeValues(long[][] expectedValues, BlockBuilder bl
blockBuilder.appendNull();
}
else {
BlockBuilder elementBlockBuilder = BIGINT.createBlockBuilder(null, expectedValue.length);
BlockBuilder elementBlockBuilder = blockBuilder.beginBlockEntry();
for (long v : expectedValue) {
BIGINT.writeLong(elementBlockBuilder, v);
}
blockBuilder.appendStructure(elementBlockBuilder);
blockBuilder.closeEntry();
}
}
return blockBuilder;
Expand All @@ -247,11 +247,11 @@ private static BlockBuilder createBlockBuilderWithValues(Slice[][] expectedValue
blockBuilder.appendNull();
}
else {
BlockBuilder elementBlockBuilder = VARCHAR.createBlockBuilder(null, expectedValue.length);
BlockBuilder elementBlockBuilder = blockBuilder.beginBlockEntry();
for (Slice v : expectedValue) {
VARCHAR.writeSlice(elementBlockBuilder, v);
}
blockBuilder.appendStructure(elementBlockBuilder.build());
blockBuilder.closeEntry();
}
}
return blockBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,9 @@ public void testNewBlockBuilderLike()
for (int i = 0; i < 100; i++) {
BIGINT.writeLong(bigintBlockBuilder, i);
VARCHAR.writeSlice(varcharBlockBuilder, Slices.utf8Slice("test" + i));
Block longArrayBlock = new ArrayType(BIGINT)
.createBlockBuilder(null, 1)
.appendStructure(BIGINT.createBlockBuilder(null, 2).writeLong(i).closeEntry().writeLong(i * 2).closeEntry().build());
arrayBlockBuilder.appendStructure(longArrayBlock);
BlockBuilder blockBuilder = longArrayType.createBlockBuilder(null, 1);
longArrayType.writeObject(blockBuilder, BIGINT.createBlockBuilder(null, 2).writeLong(i).writeLong(i * 2).build());
arrayType.writeObject(arrayBlockBuilder, blockBuilder);
pageBuilder.declarePosition();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@

import io.airlift.slice.Slice;
import io.airlift.slice.Slices;
import io.trino.spi.block.ArrayBlockBuilder;
import io.trino.spi.block.Block;
import io.trino.spi.block.BlockBuilder;
import io.trino.spi.block.ColumnarArray;
import io.trino.spi.block.DictionaryBlock;
import io.trino.spi.block.RunLengthEncodedBlock;
import io.trino.spi.type.ArrayType;
import org.testng.annotations.Test;

import java.lang.reflect.Array;
Expand Down Expand Up @@ -128,7 +128,8 @@ private static <T> void assertColumnarArray(Block block, T[] expectedValues)

public static BlockBuilder createBlockBuilderWithValues(Slice[][] expectedValues)
{
BlockBuilder blockBuilder = new ArrayBlockBuilder(VARCHAR, null, 100, 100);
ArrayType arrayType = new ArrayType(VARCHAR);
BlockBuilder blockBuilder = arrayType.createBlockBuilder(null, 100, 100);
for (Slice[] expectedValue : expectedValues) {
if (expectedValue == null) {
blockBuilder.appendNull();
Expand All @@ -143,7 +144,7 @@ public static BlockBuilder createBlockBuilderWithValues(Slice[][] expectedValues
VARCHAR.writeSlice(elementBlockBuilder, v);
}
}
blockBuilder.appendStructure(elementBlockBuilder.build());
arrayType.writeObject(blockBuilder, elementBlockBuilder.build());
}
}
return blockBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ private Block produceArrayBlock(ArrayType arrayType, int entries, double primiti

for (int i = 0; i < entries; i++) {
int arrayLength = minNestedCardinality + random.nextInt(maxNestedCardinality - minNestedCardinality);
builder.appendStructure(produceBlock(elementType, arrayLength, primitiveNullsRatio, rowNullsRatio));
arrayType.writeObject(builder, produceBlock(elementType, arrayLength, primitiveNullsRatio, rowNullsRatio));
}
return builder.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ public static Block filter(Type type, Block block, MethodHandle function)
throw new RuntimeException(t);
}
if (TRUE.equals(keep)) {
block.writePositionTo(position, resultBuilder);
type.appendTo(block, position, resultBuilder);
}
}
return resultBuilder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

import io.airlift.slice.Slice;
import io.airlift.slice.Slices;
import io.trino.spi.block.ArrayBlockBuilder;
import io.trino.spi.block.Block;
import io.trino.spi.block.BlockBuilder;
import io.trino.spi.type.ArrayType;
import io.trino.spi.type.RowType;

import static com.google.common.base.Preconditions.checkArgument;
Expand Down Expand Up @@ -46,22 +46,23 @@ public static Block createSimpleBlock(Slice[] values)

public static Block createArrayBlock(Slice[][] values)
{
BlockBuilder blockBuilder = new ArrayBlockBuilder(VARCHAR, null, 100, 100);
ArrayType arrayType = new ArrayType(VARCHAR);
BlockBuilder blockBuilder = arrayType.createBlockBuilder(null, 100, 100);
for (Slice[] expectedValue : values) {
if (expectedValue == null) {
blockBuilder.appendNull();
}
else {
Block elementBlock = createSimpleBlock(expectedValue);
blockBuilder.appendStructure(elementBlock);
arrayType.writeObject(blockBuilder, createSimpleBlock(expectedValue));
}
}
return blockBuilder.build();
}

public static Block createArrayBlockOfRowBlocks(Slice[][][] elements, RowType rowType)
{
BlockBuilder arrayBlockBuilder = new ArrayBlockBuilder(rowType, null, 100, 100);
ArrayType arrayType = new ArrayType(rowType);
BlockBuilder arrayBlockBuilder = arrayType.createBlockBuilder(null, 100, 100);
for (int i = 0; i < elements.length; i++) {
if (elements[i] == null) {
arrayBlockBuilder.appendNull();
Expand All @@ -86,7 +87,7 @@ public static Block createArrayBlockOfRowBlocks(Slice[][][] elements, RowType ro
elementBlockBuilder.closeEntry();
}
}
arrayBlockBuilder.appendStructure(elementBlockBuilder.build());
arrayType.writeObject(arrayBlockBuilder, elementBlockBuilder.build());
}
}
return arrayBlockBuilder.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import io.trino.operator.scalar.AbstractTestFunctions;
import io.trino.spi.TrinoException;
import io.trino.spi.block.Block;
import io.trino.spi.block.BlockBuilder;
import io.trino.spi.function.LiteralParameters;
import io.trino.spi.function.ScalarFunction;
import io.trino.spi.function.SqlType;
Expand Down Expand Up @@ -103,15 +104,15 @@ public static Slice uncheckedToJson(@SqlType("varchar(x)") Slice slice)
@Test
public void testStackRepresentation()
{
Block actualBlock = arrayBlockOf(new ArrayType(BIGINT), arrayBlockOf(BIGINT, 1L, 2L), arrayBlockOf(BIGINT, 3L));
ArrayType arrayType = new ArrayType(BIGINT);
Block actualBlock = arrayBlockOf(arrayType, arrayBlockOf(BIGINT, 1L, 2L), arrayBlockOf(BIGINT, 3L));
DynamicSliceOutput actualSliceOutput = new DynamicSliceOutput(100);
writeBlock(functionAssertions.getPlannerContext().getBlockEncodingSerde(), actualSliceOutput, actualBlock);

Block expectedBlock = new ArrayType(BIGINT)
.createBlockBuilder(null, 3)
.appendStructure(BIGINT.createBlockBuilder(null, 2).writeLong(1).closeEntry().writeLong(2).closeEntry().build())
.appendStructure(BIGINT.createBlockBuilder(null, 1).writeLong(3).closeEntry().build())
.build();
BlockBuilder expectedBlockBuilder = arrayType.createBlockBuilder(null, 3);
arrayType.writeObject(expectedBlockBuilder, BIGINT.createBlockBuilder(null, 2).writeLong(1).writeLong(2).build());
arrayType.writeObject(expectedBlockBuilder, BIGINT.createBlockBuilder(null, 1).writeLong(3).build());
Block expectedBlock = expectedBlockBuilder.build();
DynamicSliceOutput expectedSliceOutput = new DynamicSliceOutput(100);
writeBlock(functionAssertions.getPlannerContext().getBlockEncodingSerde(), expectedSliceOutput, expectedBlock);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,6 @@ public <T> T getObject(int position, Class<T> clazz)
return clazz.cast(getRawElementBlock().getRegion(startValueOffset, endValueOffset - startValueOffset));
}

@Override
public void writePositionTo(int position, BlockBuilder blockBuilder)
{
checkReadablePosition(position);
blockBuilder.appendStructureInternal(this, position);
}

@Override
public Block getSingleValueBlock(int position)
{
Expand Down
Loading