use of com.facebook.presto.spi.block.BlockBuilder in project presto by prestodb.
the class ArrayConcatFunction method concat.
@UsedByGeneratedCode
public static Block concat(Type elementType, Object state, Block[] blocks) {
int resultPositionCount = 0;
// fast path when there is at most one non empty block
Block nonEmptyBlock = null;
for (int i = 0; i < blocks.length; i++) {
resultPositionCount += blocks[i].getPositionCount();
if (blocks[i].getPositionCount() > 0) {
nonEmptyBlock = blocks[i];
}
}
if (nonEmptyBlock == null) {
return blocks[0];
}
if (resultPositionCount == nonEmptyBlock.getPositionCount()) {
return nonEmptyBlock;
}
PageBuilder pageBuilder = (PageBuilder) state;
if (pageBuilder.isFull()) {
pageBuilder.reset();
}
BlockBuilder blockBuilder = pageBuilder.getBlockBuilder(0);
for (int blockIndex = 0; blockIndex < blocks.length; blockIndex++) {
Block block = blocks[blockIndex];
for (int i = 0; i < block.getPositionCount(); i++) {
elementType.appendTo(block, i, blockBuilder);
}
}
pageBuilder.declarePositions(resultPositionCount);
return blockBuilder.getRegion(blockBuilder.getPositionCount() - resultPositionCount, resultPositionCount);
}
use of com.facebook.presto.spi.block.BlockBuilder in project presto by prestodb.
the class ArrayConcatUtils method appendElement.
@UsedByGeneratedCode
public static Block appendElement(Type elementType, Block block, boolean value) {
BlockBuilder blockBuilder = elementType.createBlockBuilder(new BlockBuilderStatus(), block.getPositionCount() + 1);
for (int i = 0; i < block.getPositionCount(); i++) {
elementType.appendTo(block, i, blockBuilder);
}
elementType.writeBoolean(blockBuilder, value);
return blockBuilder.build();
}
use of com.facebook.presto.spi.block.BlockBuilder in project presto by prestodb.
the class JsonToArrayCast method toArray.
@UsedByGeneratedCode
public static Block toArray(Type arrayType, ConnectorSession connectorSession, Slice json) {
try {
List<?> array = (List<?>) stackRepresentationToObject(connectorSession, json, arrayType);
if (array == null) {
return null;
}
Type elementType = ((ArrayType) arrayType).getElementType();
BlockBuilder blockBuilder = elementType.createBlockBuilder(new BlockBuilderStatus(), array.size());
for (Object element : array) {
appendToBlockBuilder(elementType, element, blockBuilder);
}
return blockBuilder.build();
} catch (RuntimeException e) {
throw new PrestoException(INVALID_CAST_ARGUMENT, "Value cannot be cast to " + arrayType, e);
}
}
use of com.facebook.presto.spi.block.BlockBuilder in project presto by prestodb.
the class ArrayConcatUtils method appendElement.
@UsedByGeneratedCode
public static Block appendElement(Type elementType, Block block, long value) {
BlockBuilder blockBuilder = elementType.createBlockBuilder(new BlockBuilderStatus(), block.getPositionCount() + 1);
for (int i = 0; i < block.getPositionCount(); i++) {
elementType.appendTo(block, i, blockBuilder);
}
elementType.writeLong(blockBuilder, value);
return blockBuilder.build();
}
use of com.facebook.presto.spi.block.BlockBuilder in project presto by prestodb.
the class ArrayConstructor method generateArrayConstructor.
private static Class<?> generateArrayConstructor(List<Class<?>> stackTypes, Type elementType) {
List<String> stackTypeNames = stackTypes.stream().map(Class::getSimpleName).collect(toImmutableList());
ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), CompilerUtils.makeClassName(Joiner.on("").join(stackTypeNames) + "ArrayConstructor"), type(Object.class));
// Generate constructor
definition.declareDefaultConstructor(a(PRIVATE));
// Generate arrayConstructor()
ImmutableList.Builder<Parameter> parameters = ImmutableList.builder();
for (int i = 0; i < stackTypes.size(); i++) {
Class<?> stackType = stackTypes.get(i);
parameters.add(arg("arg" + i, stackType));
}
MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "arrayConstructor", type(Block.class), parameters.build());
Scope scope = method.getScope();
BytecodeBlock body = method.getBody();
Variable blockBuilderVariable = scope.declareVariable(BlockBuilder.class, "blockBuilder");
CallSiteBinder binder = new CallSiteBinder();
BytecodeExpression createBlockBuilder = blockBuilderVariable.set(constantType(binder, elementType).invoke("createBlockBuilder", BlockBuilder.class, newInstance(BlockBuilderStatus.class), constantInt(stackTypes.size())));
body.append(createBlockBuilder);
for (int i = 0; i < stackTypes.size(); i++) {
if (elementType.getJavaType() == void.class) {
body.append(blockBuilderVariable.invoke("appendNull", BlockBuilder.class));
} else {
Variable argument = scope.getVariable("arg" + i);
IfStatement ifStatement = new IfStatement().condition(equal(argument, constantNull(stackTypes.get(i)))).ifTrue(blockBuilderVariable.invoke("appendNull", BlockBuilder.class).pop()).ifFalse(constantType(binder, elementType).writeValue(blockBuilderVariable, argument.cast(elementType.getJavaType())));
body.append(ifStatement);
}
}
body.append(blockBuilderVariable.invoke("build", Block.class).ret());
return defineClass(definition, Object.class, binder.getBindings(), new DynamicClassLoader(ArrayConstructor.class.getClassLoader()));
}
Aggregations