Skip to content

Commit 4516abb

Browse files
committed
Add tests to TestRowBlock
1 parent 7211304 commit 4516abb

File tree

1 file changed

+146
-15
lines changed

1 file changed

+146
-15
lines changed

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

Lines changed: 146 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -116,32 +116,163 @@ public void testCopyWithAppendedNull()
116116
// Test without startOffset
117117
List<Object>[] testRows = generateTestRows(fieldTypes, 3);
118118
RowBlock rowBlock = (RowBlock) createBlockBuilderWithValues(fieldTypes, testRows).build();
119-
120119
RowBlock appendedBlock = rowBlock.copyWithAppendedNull();
121120
assertThat(appendedBlock.getPositionCount()).isEqualTo(testRows.length + 1);
122121
for (int i = 0; i < testRows.length; i++) {
123122
assertPositionValue(appendedBlock, i, testRows[i]);
124123
}
125124
assertThat(appendedBlock.isNull(appendedBlock.getPositionCount() - 1)).isTrue();
126125

127-
// Test with startOffset
128-
RowBlock offsetBlock = rowBlock.getRegion(1, 2);
129-
RowBlock offsetAppendedBlock = offsetBlock.copyWithAppendedNull();
126+
// Test with existing nulls - create block with nulls manually
127+
RowBlockBuilder builderWithNulls = new RowBlockBuilder(fieldTypes, null, 3);
128+
builderWithNulls.buildEntry(fieldBuilders -> {
129+
VARCHAR.writeString(fieldBuilders.get(0), "test1");
130+
BIGINT.writeLong(fieldBuilders.get(1), 42);
131+
});
132+
builderWithNulls.appendNull();
133+
builderWithNulls.buildEntry(fieldBuilders -> {
134+
VARCHAR.writeString(fieldBuilders.get(0), "test3");
135+
BIGINT.writeLong(fieldBuilders.get(1), 44);
136+
});
137+
RowBlock rowBlockWithNulls = builderWithNulls.buildValueBlock();
138+
RowBlock appendedBlockWithNulls = rowBlockWithNulls.copyWithAppendedNull();
139+
assertThat(appendedBlockWithNulls.getPositionCount()).isEqualTo(4);
140+
assertThat(appendedBlockWithNulls.isNull(3)).isTrue();
141+
142+
// Test without existing nulls
143+
List<Object>[] testRowsNoNulls = generateTestRows(fieldTypes, 2);
144+
RowBlock rowBlockNoNulls = (RowBlock) createBlockBuilderWithValues(fieldTypes, testRowsNoNulls).build();
145+
RowBlock appendedBlockNoNulls = rowBlockNoNulls.copyWithAppendedNull();
146+
assertThat(appendedBlockNoNulls.getPositionCount()).isEqualTo(3);
147+
assertThat(appendedBlockNoNulls.isNull(2)).isTrue();
148+
assertThat(appendedBlockNoNulls.isNull(0)).isFalse();
149+
assertThat(appendedBlockNoNulls.isNull(1)).isFalse();
150+
151+
List<Object>[] largerTestRows = generateTestRows(fieldTypes, 5);
152+
RowBlock largerBlock = (RowBlock) createBlockBuilderWithValues(fieldTypes, largerTestRows).build();
153+
RowBlock regionBlock = largerBlock.getRegion(1, 2);
154+
RowBlock offsetAppendedBlock = regionBlock.copyWithAppendedNull();
130155
assertThat(offsetAppendedBlock.getPositionCount()).isEqualTo(3);
131-
for (int i = 0; i < 2; i++) {
132-
assertPositionValue(offsetAppendedBlock, i, testRows[i + 1]);
133-
}
156+
157+
assertPositionValue(offsetAppendedBlock, 0, largerTestRows[1]);
158+
assertPositionValue(offsetAppendedBlock, 1, largerTestRows[2]);
134159
assertThat(offsetAppendedBlock.isNull(2)).isTrue();
160+
}
161+
162+
@Test
163+
public void testGetFieldBlocks()
164+
{
165+
List<Type> fieldTypes = ImmutableList.of(VARCHAR, BIGINT);
166+
List<Object>[] testRows = generateTestRows(fieldTypes, 3);
167+
RowBlock rowBlock = (RowBlock) createBlockBuilderWithValues(fieldTypes, testRows).build();
135168

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]);
169+
List<Block> fieldBlocks = rowBlock.getFieldBlocks();
170+
assertThat(fieldBlocks.size()).isEqualTo(2);
171+
172+
// Verify field values match original data
173+
for (int pos = 0; pos < 3; pos++) {
174+
List<Object> expectedRow = testRows[pos];
175+
if (expectedRow.get(0) != null) {
176+
assertThat(VARCHAR.getSlice(fieldBlocks.get(0), pos)).isEqualTo(utf8Slice((String) expectedRow.get(0)));
177+
}
178+
if (expectedRow.get(1) != null) {
179+
assertThat(BIGINT.getLong(fieldBlocks.get(1), pos)).isEqualTo((Long) expectedRow.get(1));
180+
}
181+
}
182+
183+
// Test with offset
184+
List<Object>[] largerTestRows = generateTestRows(fieldTypes, 5);
185+
RowBlock originalBlock = (RowBlock) createBlockBuilderWithValues(fieldTypes, largerTestRows).build();
186+
RowBlock regionBlock = originalBlock.getRegion(1, 3);
187+
List<Block> fieldBlocksWithOffset = regionBlock.getFieldBlocks();
188+
assertThat(fieldBlocksWithOffset.size()).isEqualTo(2);
189+
assertThat(fieldBlocksWithOffset.get(0).getPositionCount()).isEqualTo(3);
190+
191+
// Verify values are from the correct region
192+
for (int pos = 0; pos < 3; pos++) {
193+
List<Object> expectedRow = largerTestRows[pos + 1];
194+
if (expectedRow.get(0) != null) {
195+
assertThat(VARCHAR.getSlice(fieldBlocksWithOffset.get(0), pos)).isEqualTo(utf8Slice((String) expectedRow.get(0)));
196+
}
143197
}
144-
assertThat(partialAppendedBlock.isNull(5)).isTrue();
198+
}
199+
200+
@Test
201+
public void testCopyPositions()
202+
{
203+
List<Type> fieldTypes = ImmutableList.of(VARCHAR, BIGINT);
204+
List<Object>[] testRows = generateTestRows(fieldTypes, 5);
205+
RowBlock rowBlock = (RowBlock) createBlockBuilderWithValues(fieldTypes, testRows).build();
206+
207+
RowBlock copiedBlock = rowBlock.copyPositions(new int[] {0, 2, 4}, 0, 3);
208+
assertThat(copiedBlock.getPositionCount()).isEqualTo(3);
209+
assertPositionValue(copiedBlock, 0, testRows[0]);
210+
assertPositionValue(copiedBlock, 1, testRows[2]);
211+
assertPositionValue(copiedBlock, 2, testRows[4]);
212+
213+
// Test with nulls
214+
List<Object>[] testRowsWithNulls = alternatingNullValues(generateTestRows(fieldTypes, 3));
215+
RowBlock rowBlockWithNulls = (RowBlock) createBlockBuilderWithValues(fieldTypes, testRowsWithNulls).build().getRegion(1, testRowsWithNulls.length - 1);
216+
217+
RowBlock copiedBlockWithNulls = rowBlockWithNulls.copyPositions(new int[] {0, 2}, 0, 2);
218+
assertThat(copiedBlockWithNulls.getPositionCount()).isEqualTo(2);
219+
assertPositionValue(copiedBlockWithNulls, 0, testRowsWithNulls[1]);
220+
assertPositionValue(copiedBlockWithNulls, 1, testRowsWithNulls[3]);
221+
222+
// Test with offset
223+
RowBlock regionBlock = rowBlock.getRegion(1, 3);
224+
RowBlock copiedBlockWithOffset = regionBlock.copyPositions(new int[] {0, 2}, 0, 2);
225+
assertThat(copiedBlockWithOffset.getPositionCount()).isEqualTo(2);
226+
assertPositionValue(copiedBlockWithOffset, 0, testRows[1]);
227+
assertPositionValue(copiedBlockWithOffset, 1, testRows[3]);
228+
229+
// Test empty positions
230+
RowBlock emptyBlock = rowBlock.copyPositions(new int[0], 0, 0);
231+
assertThat(emptyBlock.getPositionCount()).isEqualTo(0);
232+
}
233+
234+
@Test
235+
public void testGetRegion()
236+
{
237+
List<Type> fieldTypes = ImmutableList.of(VARCHAR, BIGINT);
238+
List<Object>[] testRows = generateTestRows(fieldTypes, 5);
239+
RowBlock rowBlock = (RowBlock) createBlockBuilderWithValues(fieldTypes, testRows).build();
240+
241+
RowBlock regionBlock = rowBlock.getRegion(1, 3);
242+
assertThat(regionBlock.getPositionCount()).isEqualTo(3);
243+
assertPositionValue(regionBlock, 0, testRows[1]);
244+
assertPositionValue(regionBlock, 1, testRows[2]);
245+
assertPositionValue(regionBlock, 2, testRows[3]);
246+
247+
// Test zero length
248+
RowBlock zeroLengthRegion = rowBlock.getRegion(1, 0);
249+
assertThat(zeroLengthRegion.getPositionCount()).isEqualTo(0);
250+
}
251+
252+
@Test
253+
public void testCopyRegion()
254+
{
255+
List<Type> fieldTypes = ImmutableList.of(VARCHAR, BIGINT);
256+
List<Object>[] testRows = generateTestRows(fieldTypes, 5);
257+
RowBlock rowBlock = (RowBlock) createBlockBuilderWithValues(fieldTypes, testRows).build();
258+
259+
RowBlock copiedRegion = rowBlock.copyRegion(1, 3);
260+
assertThat(copiedRegion.getPositionCount()).isEqualTo(3);
261+
assertPositionValue(copiedRegion, 0, testRows[1]);
262+
assertPositionValue(copiedRegion, 1, testRows[2]);
263+
assertPositionValue(copiedRegion, 2, testRows[3]);
264+
265+
// Test with nulls and offset
266+
List<Object>[] testRowsWithNulls = alternatingNullValues(generateTestRows(fieldTypes, 3));
267+
RowBlock rowBlockWithNulls = (RowBlock) createBlockBuilderWithValues(fieldTypes, testRowsWithNulls).build().getRegion(1, testRowsWithNulls.length - 2);
268+
RowBlock copiedRegionWithNulls = rowBlockWithNulls.copyRegion(0, 2);
269+
assertThat(copiedRegionWithNulls.getPositionCount()).isEqualTo(2);
270+
assertPositionValue(copiedRegionWithNulls, 0, testRowsWithNulls[1]);
271+
assertPositionValue(copiedRegionWithNulls, 1, testRowsWithNulls[2]);
272+
273+
// Test zero length
274+
RowBlock zeroLengthCopy = rowBlock.copyRegion(1, 0);
275+
assertThat(zeroLengthCopy.getPositionCount()).isEqualTo(0);
145276
}
146277

147278
private void testWith(List<Type> fieldTypes, List<Object>[] expectedValues)

0 commit comments

Comments
 (0)