Skip to content

Commit

Permalink
Optimize null checking in Dictionary Blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
skrzypo987 authored and sopel39 committed Feb 25, 2022
1 parent a585a57 commit 3345adb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@ protected <T> void assertBlock(Block block, Supplier<BlockBuilder> newBlockBuild
assertBlockSize(block);
assertRetainedSize(block);

assertThatThrownBy(() -> block.isNull(-1))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageMatching(format("(position is not valid|Invalid position -1 in block with %d positions)", block.getPositionCount()));

assertThatThrownBy(() -> block.isNull(block.getPositionCount()))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageMatching(format("(position is not valid|Invalid position %d in block with %d positions)", block.getPositionCount(), block.getPositionCount()));
if (block.mayHaveNull()) {
assertThatThrownBy(() -> block.isNull(-1))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageMatching(format("(position is not valid|Invalid position -1 in block with %d positions)", block.getPositionCount()));

assertThatThrownBy(() -> block.isNull(block.getPositionCount()))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageMatching(format("(position is not valid|Invalid position %d in block with %d positions)", block.getPositionCount(), block.getPositionCount()));
}
}

private void assertRetainedSize(Block block)
Expand Down
3 changes: 2 additions & 1 deletion core/trino-spi/src/main/java/io/trino/spi/block/Block.java
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ default boolean mayHaveNull()
/**
* Is the specified position null?
*
* @throws IllegalArgumentException if this position is not valid
* @throws IllegalArgumentException if this position is not valid. The method may return false
* without throwing exception when there are no nulls in the block, even if the position is invalid
*/
boolean isNull(int position);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -462,14 +462,17 @@ public Block copyRegion(int position, int length)
@Override
public boolean mayHaveNull()
{
return positionCount > 0 && dictionary.mayHaveNull();
return mayHaveNull && dictionary.mayHaveNull();
}

@Override
public boolean isNull(int position)
{
if (!mayHaveNull) {
return false;
}
checkValidPosition(position, positionCount);
return mayHaveNull && dictionary.isNull(getIdUnchecked(position));
return dictionary.isNull(getIdUnchecked(position));
}

@Override
Expand Down

0 comments on commit 3345adb

Please sign in to comment.