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 @@ -72,7 +72,7 @@ public String getEncodingName()
}

@Override
public final Block copyPositions(int[] positions, int offset, int length)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: separate commit

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO it's easier to see the reason for this change if it's part of the commit that overrides this method.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would remove this change for now as it causes test failure and create an issue in Github that SPI checker shouldn't fail for such case

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, done. #12053

public Block copyPositions(int[] positions, int offset, int length)
{
checkArrayRange(positions, offset, length);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@

import static io.airlift.slice.SizeOf.sizeOf;
import static io.trino.spi.block.BlockUtil.calculateBlockResetSize;
import static io.trino.spi.block.BlockUtil.checkArrayRange;
import static io.trino.spi.block.BlockUtil.checkValidRegion;
import static io.trino.spi.block.RowBlock.createRowBlockInternal;
import static java.lang.String.format;
import static java.util.Objects.requireNonNull;
Expand All @@ -46,6 +48,7 @@ public class RowBlockBuilder

private boolean currentEntryOpened;
private boolean hasNullRow;
private boolean hasNonNullRow;

public RowBlockBuilder(List<Type> fieldTypes, BlockBuilderStatus blockBuilderStatus, int expectedEntries)
{
Expand Down Expand Up @@ -201,6 +204,7 @@ private void entryAdded(boolean isNull)
}
rowIsNull[positionCount] = isNull;
hasNullRow |= isNull;
hasNonNullRow |= !isNull;
positionCount++;

for (int i = 0; i < numFields; i++) {
Expand All @@ -220,6 +224,9 @@ public Block build()
if (currentEntryOpened) {
throw new IllegalStateException("Current entry must be closed before the block can be built");
}
if (!hasNonNullRow) {
return nullRle(positionCount);
}
Block[] fieldBlocks = new Block[numFields];
for (int i = 0; i < numFields; i++) {
fieldBlocks[i] = fieldBlockBuilders[i].build();
Expand All @@ -243,4 +250,50 @@ public BlockBuilder newBlockBuilderLike(BlockBuilderStatus blockBuilderStatus)
}
return new RowBlockBuilder(blockBuilderStatus, newBlockBuilders, new int[newSize + 1], new boolean[newSize]);
}

@Override
public Block copyPositions(int[] positions, int offset, int length)
{
checkArrayRange(positions, offset, length);

if (!hasNonNullRow) {
return nullRle(length);
}
return super.copyPositions(positions, offset, length);
}

@Override
public Block getRegion(int position, int length)
{
int positionCount = getPositionCount();
checkValidRegion(positionCount, position, length);

if (!hasNonNullRow) {
return nullRle(length);
}
return super.getRegion(position, length);
}

@Override
public Block copyRegion(int position, int length)
{
int positionCount = getPositionCount();
checkValidRegion(positionCount, position, length);

if (!hasNonNullRow) {
return nullRle(length);
}
return super.copyRegion(position, length);
}

private RunLengthEncodedBlock nullRle(int length)
{
Block[] fieldBlocks = new Block[numFields];
for (int i = 0; i < numFields; i++) {
fieldBlocks[i] = fieldBlockBuilders[i].newBlockBuilderLike(null).build();
}

RowBlock nullRowBlock = createRowBlockInternal(0, 1, new boolean[] {true}, new int[] {0, 0}, fieldBlocks);
return new RunLengthEncodedBlock(nullRowBlock, length);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.trino.spi.block;

import com.google.common.collect.ImmutableList;
import org.testng.annotations.Test;

import static io.trino.spi.type.BigintType.BIGINT;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;

public class TestRowBlockBuilder
{
@Test
public void testBuilderProducesNullRleForNullRows()
{
// empty block
assertIsNullRle(blockBuilder().build(), 0);

// single null
assertIsNullRle(blockBuilder().appendNull().build(), 1);

// multiple nulls
assertIsNullRle(blockBuilder().appendNull().appendNull().build(), 2);

BlockBuilder blockBuilder = blockBuilder().appendNull().appendNull();
assertIsNullRle(blockBuilder.copyPositions(new int[] {0}, 0, 1), 1);
assertIsNullRle(blockBuilder.getRegion(0, 1), 1);
assertIsNullRle(blockBuilder.copyRegion(0, 1), 1);
}

private static BlockBuilder blockBuilder()
{
return new RowBlockBuilder(ImmutableList.of(BIGINT), null, 10);
}

private void assertIsNullRle(Block block, int expectedPositionCount)
{
assertEquals(block.getPositionCount(), expectedPositionCount);
assertEquals(block.getClass(), RunLengthEncodedBlock.class);
if (expectedPositionCount > 0) {
assertTrue(block.isNull(0));
}
}
}