use of com.facebook.presto.common.block.BlockBuilder in project presto by prestodb.
the class AggregatedOrcPageSource method getNextPage.
@Override
public Page getNextPage() {
if (completed) {
return null;
}
long start = System.nanoTime();
Block[] blocks = new Block[columnHandles.size()];
for (int fieldId = 0; fieldId < blocks.length; fieldId++) {
HiveColumnHandle columnHandle = columnHandles.get(fieldId);
Aggregation aggregation = columnHandle.getPartialAggregation().get();
int columnIndex = columnHandle.getHiveColumnIndex();
Type type = typeManager.getType(columnHandle.getTypeSignature());
BlockBuilder blockBuilder = type.createBlockBuilder(null, batchSize, 0);
FunctionHandle functionHandle = aggregation.getFunctionHandle();
if (functionResolution.isCountFunction(functionHandle)) {
if (aggregation.getArguments().isEmpty()) {
blockBuilder = blockBuilder.writeLong(footer.getNumberOfRows());
} else {
writeNonNullCount(columnIndex, blockBuilder);
}
completedBytes += INTEGER.getFixedSize();
} else if (functionResolution.isMaxFunction(functionHandle)) {
writeMinMax(columnIndex, type, columnHandle.getHiveType(), blockBuilder, false);
} else if (functionResolution.isMinFunction(functionHandle)) {
writeMinMax(columnIndex, type, columnHandle.getHiveType(), blockBuilder, true);
} else {
throw new UnsupportedOperationException(aggregation.getFunctionHandle().toString() + " is not supported");
}
blocks[fieldId] = blockBuilder.build();
}
completed = true;
readTimeNanos += System.nanoTime() - start;
return new Page(batchSize, blocks);
}
use of com.facebook.presto.common.block.BlockBuilder in project presto by prestodb.
the class ParquetTester method writeValue.
private static void writeValue(Type type, BlockBuilder blockBuilder, Object value) {
if (value == null) {
blockBuilder.appendNull();
} else {
if (BOOLEAN.equals(type)) {
type.writeBoolean(blockBuilder, (Boolean) value);
} else if (TINYINT.equals(type) || SMALLINT.equals(type) || INTEGER.equals(type) || BIGINT.equals(type)) {
type.writeLong(blockBuilder, ((Number) value).longValue());
} else if (Decimals.isShortDecimal(type)) {
type.writeLong(blockBuilder, ((SqlDecimal) value).getUnscaledValue().longValue());
} else if (Decimals.isLongDecimal(type)) {
if (Decimals.overflows(((SqlDecimal) value).getUnscaledValue(), MAX_PRECISION_INT64)) {
type.writeSlice(blockBuilder, Decimals.encodeUnscaledValue(((SqlDecimal) value).toBigDecimal().unscaledValue()));
} else {
type.writeSlice(blockBuilder, Decimals.encodeUnscaledValue(((SqlDecimal) value).getUnscaledValue().longValue()));
}
} else if (DOUBLE.equals(type)) {
type.writeDouble(blockBuilder, ((Number) value).doubleValue());
} else if (REAL.equals(type)) {
float floatValue = ((Number) value).floatValue();
type.writeLong(blockBuilder, Float.floatToIntBits(floatValue));
} else if (type instanceof VarcharType) {
Slice slice = truncateToLength(utf8Slice((String) value), type);
type.writeSlice(blockBuilder, slice);
} else if (type instanceof CharType) {
Slice slice = truncateToLengthAndTrimSpaces(utf8Slice((String) value), type);
type.writeSlice(blockBuilder, slice);
} else if (VARBINARY.equals(type)) {
type.writeSlice(blockBuilder, Slices.wrappedBuffer(((SqlVarbinary) value).getBytes()));
} else if (DATE.equals(type)) {
long days = ((SqlDate) value).getDays();
type.writeLong(blockBuilder, days);
} else if (TIMESTAMP.equals(type)) {
long millis = ((SqlTimestamp) value).getMillisUtc();
type.writeLong(blockBuilder, millis);
} else {
if (type instanceof ArrayType) {
List<?> array = (List<?>) value;
Type elementType = type.getTypeParameters().get(0);
BlockBuilder arrayBlockBuilder = blockBuilder.beginBlockEntry();
for (Object elementValue : array) {
writeValue(elementType, arrayBlockBuilder, elementValue);
}
blockBuilder.closeEntry();
} else if (type instanceof MapType) {
Map<?, ?> map = (Map<?, ?>) value;
Type keyType = type.getTypeParameters().get(0);
Type valueType = type.getTypeParameters().get(1);
BlockBuilder mapBlockBuilder = blockBuilder.beginBlockEntry();
for (Map.Entry<?, ?> entry : map.entrySet()) {
writeValue(keyType, mapBlockBuilder, entry.getKey());
writeValue(valueType, mapBlockBuilder, entry.getValue());
}
blockBuilder.closeEntry();
} else if (type instanceof RowType) {
List<?> array = (List<?>) value;
List<Type> fieldTypes = type.getTypeParameters();
BlockBuilder rowBlockBuilder = blockBuilder.beginBlockEntry();
for (int fieldId = 0; fieldId < fieldTypes.size(); fieldId++) {
Type fieldType = fieldTypes.get(fieldId);
writeValue(fieldType, rowBlockBuilder, array.get(fieldId));
}
blockBuilder.closeEntry();
} else {
throw new IllegalArgumentException("Unsupported type " + type);
}
}
}
}
use of com.facebook.presto.common.block.BlockBuilder 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.BlockBuilder in project presto by prestodb.
the class AbstractTestType method toBlock.
private Block toBlock(Object value) {
BlockBuilder blockBuilder = type.createBlockBuilder(null, 1);
Class<?> javaType = type.getJavaType();
if (value == null) {
blockBuilder.appendNull();
} else if (javaType == boolean.class) {
type.writeBoolean(blockBuilder, (Boolean) value);
} else if (javaType == long.class) {
type.writeLong(blockBuilder, (Long) value);
} else if (javaType == double.class) {
type.writeDouble(blockBuilder, (Double) value);
} else if (javaType == Slice.class) {
Slice slice = (Slice) value;
type.writeSlice(blockBuilder, slice, 0, slice.length());
} else {
type.writeObject(blockBuilder, value);
}
return blockBuilder.build();
}
use of com.facebook.presto.common.block.BlockBuilder in project presto by prestodb.
the class TestBigintArrayType method getGreaterValue.
@Override
protected Object getGreaterValue(Object value) {
Block block = (Block) value;
BlockBuilder blockBuilder = BIGINT.createBlockBuilder(null, block.getPositionCount() + 1);
for (int i = 0; i < block.getPositionCount(); i++) {
BIGINT.appendTo(block, i, blockBuilder);
}
BIGINT.writeLong(blockBuilder, 1L);
return blockBuilder.build();
}
Aggregations