Skip to content

Commit 6143d04

Browse files
committed
Fix copyWithAppendedNull for RowBlock with startOffset
1 parent 59f6866 commit 6143d04

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

core/trino-main/src/test/java/io/trino/block/TestRowBlock.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,42 @@ public void testCompactBlock()
108108
new ByteArrayBlock(6, Optional.of(rowIsNull), createExpectedValue(6).getBytes())}));
109109
}
110110

111+
@Test
112+
public void testCopyWithAppendedNull()
113+
{
114+
List<Type> fieldTypes = ImmutableList.of(VARCHAR, BIGINT);
115+
116+
// Test without startOffset
117+
List<Object>[] testRows = generateTestRows(fieldTypes, 3);
118+
RowBlock rowBlock = (RowBlock) createBlockBuilderWithValues(fieldTypes, testRows).build();
119+
120+
RowBlock appendedBlock = rowBlock.copyWithAppendedNull();
121+
assertThat(appendedBlock.getPositionCount()).isEqualTo(testRows.length + 1);
122+
for (int i = 0; i < testRows.length; i++) {
123+
assertPositionValue(appendedBlock, i, testRows[i]);
124+
}
125+
assertThat(appendedBlock.isNull(appendedBlock.getPositionCount() - 1)).isTrue();
126+
127+
// Test with startOffset
128+
RowBlock offsetBlock = rowBlock.getRegion(1, 2);
129+
RowBlock offsetAppendedBlock = offsetBlock.copyWithAppendedNull();
130+
assertThat(offsetAppendedBlock.getPositionCount()).isEqualTo(3);
131+
for (int i = 0; i < 2; i++) {
132+
assertPositionValue(offsetAppendedBlock, i, testRows[i + 1]);
133+
}
134+
assertThat(offsetAppendedBlock.isNull(2)).isTrue();
135+
136+
// Test startOffset + positionCount < fieldBlock[0].length
137+
List<Object>[] largerTestRows = generateTestRows(fieldTypes, 10);
138+
RowBlock partialBlock = (RowBlock) createBlockBuilderWithValues(fieldTypes, largerTestRows).build().getRegion(2, 5);
139+
RowBlock partialAppendedBlock = partialBlock.copyWithAppendedNull();
140+
assertThat(partialAppendedBlock.getPositionCount()).isEqualTo(6);
141+
for (int i = 0; i < 5; i++) {
142+
assertPositionValue(partialAppendedBlock, i, largerTestRows[i + 2]);
143+
}
144+
assertThat(partialAppendedBlock.isNull(5)).isTrue();
145+
}
146+
111147
private void testWith(List<Type> fieldTypes, List<Object>[] expectedValues)
112148
{
113149
Block block = createBlockBuilderWithValues(fieldTypes, expectedValues).build();

core/trino-spi/src/main/java/io/trino/spi/block/RowBlock.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -249,13 +249,13 @@ public RowBlock copyWithAppendedNull()
249249
{
250250
boolean[] newRowIsNull;
251251
if (rowIsNull != null) {
252-
newRowIsNull = Arrays.copyOf(rowIsNull, positionCount + 1);
252+
newRowIsNull = Arrays.copyOf(rowIsNull, startOffset + positionCount + 1);
253253
}
254254
else {
255-
newRowIsNull = new boolean[positionCount + 1];
255+
newRowIsNull = new boolean[startOffset + positionCount + 1];
256256
}
257257
// mark the (new) last element as null
258-
newRowIsNull[positionCount] = true;
258+
newRowIsNull[startOffset + positionCount] = true;
259259

260260
Block[] newBlocks = new Block[fieldBlocks.length];
261261
for (int i = 0; i < fieldBlocks.length; i++) {

0 commit comments

Comments
 (0)