Skip to content

Commit

Permalink
Add ColumnarRow#mayHaveNull()
Browse files Browse the repository at this point in the history
  • Loading branch information
pettyjamesm authored and martint committed Nov 17, 2021
1 parent d1a5d2c commit 764ba1e
Showing 1 changed file with 25 additions and 9 deletions.
34 changes: 25 additions & 9 deletions core/trino-spi/src/main/java/io/trino/spi/block/ColumnarRow.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,14 @@
*/
package io.trino.spi.block;

import javax.annotation.Nullable;

import static java.util.Objects.requireNonNull;

public final class ColumnarRow
{
private final int positionCount;
@Nullable
private final Block nullCheckBlock;
private final Block[] fields;

Expand Down Expand Up @@ -48,7 +52,7 @@ public static ColumnarRow toColumnarRow(Block block)
fieldBlocks[i] = rowBlock.getRawFieldBlocks()[i].getRegion(firstRowPosition, totalRowCount);
}

return new ColumnarRow(block, fieldBlocks);
return new ColumnarRow(block.getPositionCount(), block, fieldBlocks);
}

private static ColumnarRow toColumnarRow(DictionaryBlock dictionaryBlock)
Expand Down Expand Up @@ -83,7 +87,13 @@ private static ColumnarRow toColumnarRow(DictionaryBlock dictionaryBlock)
for (int i = 0; i < columnarRow.getFieldCount(); i++) {
fields[i] = new DictionaryBlock(nonNullPositionCount, columnarRow.getField(i), dictionaryIds);
}
return new ColumnarRow(dictionaryBlock, fields);

int positionCount = dictionaryBlock.getPositionCount();
if (nonNullPositionCount == positionCount) {
// no null rows are referenced in the dictionary, discard the null check block
dictionaryBlock = null;
}
return new ColumnarRow(positionCount, dictionaryBlock, fields);
}

private static ColumnarRow toColumnarRowFromDictionaryWithoutNulls(DictionaryBlock dictionaryBlock)
Expand All @@ -100,7 +110,7 @@ private static ColumnarRow toColumnarRowFromDictionaryWithoutNulls(DictionaryBlo
false,
DictionaryId.randomDictionaryId());
}
return new ColumnarRow(dictionaryBlock, fields);
return new ColumnarRow(dictionaryBlock.getPositionCount(), null, fields);
}

private static ColumnarRow toColumnarRow(RunLengthEncodedBlock rleBlock)
Expand All @@ -122,23 +132,29 @@ private static ColumnarRow toColumnarRow(RunLengthEncodedBlock rleBlock)
fields[i] = new RunLengthEncodedBlock(nullSuppressedField, rleBlock.getPositionCount());
}
}
return new ColumnarRow(rleBlock, fields);
return new ColumnarRow(rleBlock.getPositionCount(), rleBlock, fields);
}

private ColumnarRow(Block nullCheckBlock, Block[] fields)
private ColumnarRow(int positionCount, @Nullable Block nullCheckBlock, Block[] fields)
{
this.nullCheckBlock = nullCheckBlock;
this.fields = fields.clone();
this.positionCount = positionCount;
this.nullCheckBlock = nullCheckBlock != null && nullCheckBlock.mayHaveNull() ? nullCheckBlock : null;
this.fields = fields;
}

public int getPositionCount()
{
return nullCheckBlock.getPositionCount();
return positionCount;
}

public boolean mayHaveNull()
{
return nullCheckBlock != null;
}

public boolean isNull(int position)
{
return nullCheckBlock.isNull(position);
return nullCheckBlock != null && nullCheckBlock.isNull(position);
}

public int getFieldCount()
Expand Down

0 comments on commit 764ba1e

Please sign in to comment.