use of com.facebook.presto.common.block.RowBlockBuilder in project presto by prestodb.
the class OperatorAssertion method toRow.
public static Block toRow(List<Type> parameterTypes, Object... values) {
checkArgument(parameterTypes.size() == values.length, "parameterTypes.size(" + parameterTypes.size() + ") does not equal to values.length(" + values.length + ")");
RowType rowType = RowType.anonymous(parameterTypes);
BlockBuilder blockBuilder = new RowBlockBuilder(parameterTypes, null, 1);
BlockBuilder singleRowBlockWriter = blockBuilder.beginBlockEntry();
for (int i = 0; i < values.length; i++) {
appendToBlockBuilder(parameterTypes.get(i), values[i], singleRowBlockWriter);
}
blockBuilder.closeEntry();
return rowType.getObject(blockBuilder, 0);
}
use of com.facebook.presto.common.block.RowBlockBuilder in project presto by prestodb.
the class TestSimpleRowType method createTestBlock.
private static Block createTestBlock() {
RowBlockBuilder blockBuilder = (RowBlockBuilder) TYPE.createBlockBuilder(null, 3);
SingleRowBlockWriter singleRowBlockWriter;
singleRowBlockWriter = blockBuilder.beginBlockEntry();
BIGINT.writeLong(singleRowBlockWriter, 1);
VARCHAR.writeSlice(singleRowBlockWriter, utf8Slice("cat"));
blockBuilder.closeEntry();
singleRowBlockWriter = blockBuilder.beginBlockEntry();
BIGINT.writeLong(singleRowBlockWriter, 2);
VARCHAR.writeSlice(singleRowBlockWriter, utf8Slice("cats"));
blockBuilder.closeEntry();
singleRowBlockWriter = blockBuilder.beginBlockEntry();
BIGINT.writeLong(singleRowBlockWriter, 3);
VARCHAR.writeSlice(singleRowBlockWriter, utf8Slice("dog"));
blockBuilder.closeEntry();
return blockBuilder.build();
}
use of com.facebook.presto.common.block.RowBlockBuilder in project presto by prestodb.
the class TestDirectEntryBlockBuilder method testNestedRow.
@Test
public void testNestedRow() {
MapType mapType = new MapType(BIGINT, VARCHAR, MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"), MethodHandleUtil.methodHandle(TestDirectEntryBlockBuilder.class, "throwUnsupportedOperation"));
ArrayType arrayType = new ArrayType(DOUBLE);
RowType.Field nestedRowField = new RowType.Field(Optional.of("my_struct"), INTEGER);
RowType nestedRowType = RowType.from(ImmutableList.of(nestedRowField));
List<Type> rowType = ImmutableList.of(REAL, mapType, arrayType, nestedRowType);
RowBlockBuilder beginEntryBlockBuilder = new RowBlockBuilder(rowType, null, 1);
for (int i = 0; i < POSITION_COUNT; i++) {
SingleRowBlockWriter singleRowBlockWriter = beginEntryBlockBuilder.beginBlockEntry();
REAL.writeLong(singleRowBlockWriter, i);
// Write Map<Bigint, Varchar> with 5 entries.
BlockBuilder mapWriter = singleRowBlockWriter.beginBlockEntry();
for (int j = 0; j < 5; j++) {
BIGINT.writeLong(mapWriter, i + j);
VARCHAR.writeSlice(mapWriter, utf8Slice("Value1" + j));
}
singleRowBlockWriter.closeEntry();
// Write array .
BlockBuilder arrayWriter = singleRowBlockWriter.beginBlockEntry();
for (int j = 0; j < 8; j++) {
DOUBLE.writeDouble(arrayWriter, i * 3 + j);
}
singleRowBlockWriter.closeEntry();
// Write row type.
BlockBuilder rowWriter = singleRowBlockWriter.beginBlockEntry();
if (i % 2 == 0) {
rowWriter.appendNull();
} else {
INTEGER.writeLong(rowWriter, i);
}
singleRowBlockWriter.closeEntry();
beginEntryBlockBuilder.closeEntry();
beginEntryBlockBuilder.appendNull();
}
RowBlockBuilder directEntryBlockBuilder = new RowBlockBuilder(rowType, null, 1);
for (int i = 0; i < POSITION_COUNT; i++) {
directEntryBlockBuilder.beginDirectEntry();
REAL.writeLong(directEntryBlockBuilder.getBlockBuilder(0), i);
// Write Map<Bigint, Varchar> with 5 entries.
MapBlockBuilder mapBlockBuilder = (MapBlockBuilder) directEntryBlockBuilder.getBlockBuilder(1);
mapBlockBuilder.beginDirectEntry();
BlockBuilder keyBuilder = mapBlockBuilder.getKeyBlockBuilder();
BlockBuilder valueBuilder = mapBlockBuilder.getValueBlockBuilder();
for (int j = 0; j < 5; j++) {
BIGINT.writeLong(keyBuilder, i + j);
VARCHAR.writeSlice(valueBuilder, utf8Slice("Value1" + j));
}
mapBlockBuilder.closeEntry();
// Write array .
ArrayBlockBuilder arrayBuilder = (ArrayBlockBuilder) directEntryBlockBuilder.getBlockBuilder(2);
arrayBuilder.beginDirectEntry();
for (int j = 0; j < 8; j++) {
DOUBLE.writeDouble(arrayBuilder.getElementBlockBuilder(), i * 3 + j);
}
arrayBuilder.closeEntry();
// Write row type.
RowBlockBuilder nestedRowBuilder = (RowBlockBuilder) directEntryBlockBuilder.getBlockBuilder(3);
nestedRowBuilder.beginDirectEntry();
BlockBuilder nestedRowValueBuilder = nestedRowBuilder.getBlockBuilder(0);
if (i % 2 == 0) {
nestedRowValueBuilder.appendNull();
} else {
INTEGER.writeLong(nestedRowValueBuilder, i);
}
nestedRowBuilder.closeEntry();
directEntryBlockBuilder.closeEntry();
directEntryBlockBuilder.appendNull();
}
Slice beginEntrySlice = getSlilce(beginEntryBlockBuilder);
Slice directEntrySlice = getSlilce(directEntryBlockBuilder);
assertEquals(beginEntrySlice.compareTo(directEntrySlice), 0);
}
use of com.facebook.presto.common.block.RowBlockBuilder in project presto by prestodb.
the class TestRowBlock method createBlockBuilderWithValues.
private BlockBuilder createBlockBuilderWithValues(List<Type> fieldTypes, List<Object>[] rows) {
BlockBuilder rowBlockBuilder = new RowBlockBuilder(fieldTypes, null, 1);
for (List<Object> row : rows) {
if (row == null) {
rowBlockBuilder.appendNull();
} else {
BlockBuilder singleRowBlockWriter = rowBlockBuilder.beginBlockEntry();
for (Object fieldValue : row) {
if (fieldValue == null) {
singleRowBlockWriter.appendNull();
} else {
if (fieldValue instanceof Long) {
BIGINT.writeLong(singleRowBlockWriter, ((Long) fieldValue).longValue());
} else if (fieldValue instanceof String) {
VARCHAR.writeSlice(singleRowBlockWriter, utf8Slice((String) fieldValue));
} else {
throw new IllegalArgumentException();
}
}
}
rowBlockBuilder.closeEntry();
}
}
return rowBlockBuilder;
}
use of com.facebook.presto.common.block.RowBlockBuilder in project presto by prestodb.
the class BlockAssertions method createRowBlock.
public static Block createRowBlock(List<Type> fieldTypes, Object[]... rows) {
BlockBuilder rowBlockBuilder = new RowBlockBuilder(fieldTypes, null, 1);
for (Object[] row : rows) {
if (row == null) {
rowBlockBuilder.appendNull();
continue;
}
BlockBuilder singleRowBlockWriter = rowBlockBuilder.beginBlockEntry();
for (Object fieldValue : row) {
if (fieldValue == null) {
singleRowBlockWriter.appendNull();
continue;
}
if (fieldValue instanceof String) {
VARCHAR.writeSlice(singleRowBlockWriter, utf8Slice((String) fieldValue));
} else if (fieldValue instanceof Slice) {
VARBINARY.writeSlice(singleRowBlockWriter, (Slice) fieldValue);
} else if (fieldValue instanceof Double) {
DOUBLE.writeDouble(singleRowBlockWriter, ((Double) fieldValue).doubleValue());
} else if (fieldValue instanceof Long) {
BIGINT.writeLong(singleRowBlockWriter, ((Long) fieldValue).longValue());
} else if (fieldValue instanceof Boolean) {
BOOLEAN.writeBoolean(singleRowBlockWriter, ((Boolean) fieldValue).booleanValue());
} else if (fieldValue instanceof Block) {
singleRowBlockWriter.appendStructure((Block) fieldValue);
} else if (fieldValue instanceof Integer) {
INTEGER.writeLong(singleRowBlockWriter, ((Integer) fieldValue).intValue());
} else {
throw new IllegalArgumentException();
}
}
rowBlockBuilder.closeEntry();
}
return rowBlockBuilder.build();
}
Aggregations