use of com.facebook.presto.common.block.BlockBuilder in project presto by prestodb.
the class TestHiveBucketing method toNativeContainerValue.
private static Object toNativeContainerValue(Type type, Object hiveValue) {
String typeBase = type.getTypeSignature().getBase();
if (hiveValue == null) {
return null;
}
switch(typeBase) {
case StandardTypes.ARRAY:
{
BlockBuilder blockBuilder = type.createBlockBuilder(null, 1);
BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
for (Object subElement : (Iterable<?>) hiveValue) {
appendToBlockBuilder(type.getTypeParameters().get(0), subElement, subBlockBuilder);
}
blockBuilder.closeEntry();
return type.getObject(blockBuilder, 0);
}
case StandardTypes.ROW:
{
BlockBuilder blockBuilder = type.createBlockBuilder(null, 1);
BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
int field = 0;
for (Object subElement : (Iterable<?>) hiveValue) {
appendToBlockBuilder(type.getTypeParameters().get(field), subElement, subBlockBuilder);
field++;
}
blockBuilder.closeEntry();
return type.getObject(blockBuilder, 0);
}
case StandardTypes.MAP:
{
BlockBuilder blockBuilder = type.createBlockBuilder(null, 1);
BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
for (Entry<?, ?> entry : ((Map<?, ?>) hiveValue).entrySet()) {
appendToBlockBuilder(type.getTypeParameters().get(0), entry.getKey(), subBlockBuilder);
appendToBlockBuilder(type.getTypeParameters().get(1), entry.getValue(), subBlockBuilder);
}
blockBuilder.closeEntry();
return type.getObject(blockBuilder, 0);
}
case StandardTypes.BOOLEAN:
return hiveValue;
case StandardTypes.TINYINT:
return (long) (byte) hiveValue;
case StandardTypes.SMALLINT:
return (long) (short) hiveValue;
case StandardTypes.INTEGER:
return (long) (int) hiveValue;
case StandardTypes.BIGINT:
return hiveValue;
case StandardTypes.REAL:
return (long) Float.floatToRawIntBits((float) hiveValue);
case StandardTypes.DOUBLE:
return hiveValue;
case StandardTypes.VARCHAR:
return Slices.utf8Slice(hiveValue.toString());
case StandardTypes.DATE:
long daysSinceEpochInLocalZone = ((Date) hiveValue).toLocalDate().toEpochDay();
assertEquals(daysSinceEpochInLocalZone, DateWritable.dateToDays((Date) hiveValue));
return daysSinceEpochInLocalZone;
case StandardTypes.TIMESTAMP:
Instant instant = ((Timestamp) hiveValue).toInstant();
long epochSecond = instant.getEpochSecond();
int nano = instant.getNano();
assertEquals(nano % 1_000_000, 0);
return epochSecond * 1000 + nano / 1_000_000;
default:
throw new UnsupportedOperationException("unknown type");
}
}
use of com.facebook.presto.common.block.BlockBuilder in project presto by prestodb.
the class TableWriterOperator method createFragmentsPage.
// Fragments page layout:
//
// row fragments context
// X null X
// null X X
// null X X
// null X X
// ...
private Page createFragmentsPage() {
Collection<Slice> fragments = getFutureValue(finishFuture);
int positionCount = fragments.size() + 1;
committed = true;
updateWrittenBytes();
// Output page will only be constructed once, and the table commit context channel will be constructed using RunLengthEncodedBlock.
// Thus individual BlockBuilder is used for each channel, instead of using PageBuilder.
BlockBuilder rowsBuilder = BIGINT.createBlockBuilder(null, positionCount);
BlockBuilder fragmentBuilder = VARBINARY.createBlockBuilder(null, positionCount);
// write row count
BIGINT.writeLong(rowsBuilder, rowCount);
fragmentBuilder.appendNull();
// write fragments
for (Slice fragment : fragments) {
rowsBuilder.appendNull();
VARBINARY.writeSlice(fragmentBuilder, fragment);
}
return new Page(positionCount, rowsBuilder.build(), fragmentBuilder.build(), RunLengthEncodedBlock.create(VARBINARY, createTableCommitContext(true), positionCount));
}
use of com.facebook.presto.common.block.BlockBuilder in project presto by prestodb.
the class BigQueryResultPageSource method getNextPage.
@Override
public Page getNextPage() {
checkState(pageBuilder.isEmpty(), "PageBuilder is not empty at the beginning of a new page");
Storage.ReadRowsResponse response = responses.next();
Iterable<GenericRecord> records = parse(response);
for (GenericRecord record : records) {
pageBuilder.declarePosition();
for (int column = 0; column < columnTypes.size(); column++) {
BlockBuilder output = pageBuilder.getBlockBuilder(column);
appendTo(columnTypes.get(column), record.get(column), output);
}
}
Page page = pageBuilder.build();
completedPositions += page.getPositionCount();
pageBuilder.reset();
return page;
}
use of com.facebook.presto.common.block.BlockBuilder in project presto by prestodb.
the class BigQueryResultPageSource method writeBlock.
private void writeBlock(BlockBuilder output, Type type, Object value) {
if (type instanceof ArrayType && value instanceof List<?>) {
BlockBuilder builder = output.beginBlockEntry();
for (Object element : (List<?>) value) {
appendTo(type.getTypeParameters().get(0), element, builder);
}
output.closeEntry();
return;
}
if (type instanceof RowType && value instanceof GenericRecord) {
GenericRecord record = (GenericRecord) value;
BlockBuilder builder = output.beginBlockEntry();
List<String> fieldNames = new ArrayList<>();
for (int i = 0; i < type.getTypeSignature().getParameters().size(); i++) {
TypeSignatureParameter parameter = type.getTypeSignature().getParameters().get(i);
fieldNames.add(parameter.getNamedTypeSignature().getName().orElse("field" + i));
}
checkState(fieldNames.size() == type.getTypeParameters().size(), "fieldName doesn't match with type size : %s", type);
for (int index = 0; index < type.getTypeParameters().size(); index++) {
appendTo(type.getTypeParameters().get(index), record.get(fieldNames.get(index)), builder);
}
output.closeEntry();
return;
}
throw new BigQueryException(BIGQUERY_UNSUPPORTED_TYPE_FOR_BLOCK, "Unhandled type for Block: " + type.getTypeSignature());
}
use of com.facebook.presto.common.block.BlockBuilder in project presto by prestodb.
the class TestGeoFunctions method testGeometryGetObjectValue.
@Test
public void testGeometryGetObjectValue() {
BlockBuilder builder = GEOMETRY.createBlockBuilder(null, 1);
GEOMETRY.writeSlice(builder, GeoFunctions.stPoint(1.2, 3.4));
Block block = builder.build();
assertEquals("POINT (1.2 3.4)", GEOMETRY.getObjectValue(null, block, 0));
}
Aggregations