use of com.facebook.presto.bytecode.BytecodeBlock in project presto by prestodb.
the class RowToRowCast method generateRowCast.
private static Class<?> generateRowCast(Type fromType, Type toType, FunctionRegistry functionRegistry) {
List<Type> toTypes = toType.getTypeParameters();
List<Type> fromTypes = fromType.getTypeParameters();
CallSiteBinder binder = new CallSiteBinder();
// Embed the MD5 hash code of input and output types into the generated class name instead of the raw type names,
// which could prevent the class name from hitting the length limitation and invalid characters.
byte[] md5Suffix = Hashing.md5().hashBytes((fromType + "$" + toType).getBytes()).asBytes();
ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), CompilerUtils.makeClassName(Joiner.on("$").join("RowCast", BaseEncoding.base16().encode(md5Suffix))), type(Object.class));
Parameter session = arg("session", ConnectorSession.class);
Parameter value = arg("value", Block.class);
MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "castRow", type(Block.class), session, value);
Scope scope = method.getScope();
BytecodeBlock body = method.getBody();
Variable wasNull = scope.declareVariable(boolean.class, "wasNull");
Variable blockBuilder = scope.createTempVariable(BlockBuilder.class);
body.append(wasNull.set(constantBoolean(false)));
CachedInstanceBinder cachedInstanceBinder = new CachedInstanceBinder(definition, binder);
// create the interleave block builder
body.newObject(InterleavedBlockBuilder.class).dup().append(constantType(binder, toType).invoke("getTypeParameters", List.class)).append(newInstance(BlockBuilderStatus.class)).append(constantInt(toTypes.size())).invokeConstructor(InterleavedBlockBuilder.class, List.class, BlockBuilderStatus.class, int.class).putVariable(blockBuilder);
// loop through to append member blocks
for (int i = 0; i < toTypes.size(); i++) {
Signature signature = internalOperator(CAST.name(), toTypes.get(i).getTypeSignature(), ImmutableList.of(fromTypes.get(i).getTypeSignature()));
ScalarFunctionImplementation function = functionRegistry.getScalarFunctionImplementation(signature);
Type currentFromType = fromTypes.get(i);
if (currentFromType.equals(UNKNOWN)) {
body.append(blockBuilder.invoke("appendNull", BlockBuilder.class).pop());
continue;
}
BytecodeExpression fromElement = constantType(binder, currentFromType).getValue(value, constantInt(i));
BytecodeExpression toElement = invokeFunction(scope, cachedInstanceBinder, signature.getName(), function, fromElement);
IfStatement ifElementNull = new IfStatement("if the element in the row type is null...");
ifElementNull.condition(value.invoke("isNull", boolean.class, constantInt(i))).ifTrue(blockBuilder.invoke("appendNull", BlockBuilder.class).pop()).ifFalse(constantType(binder, toTypes.get(i)).writeValue(blockBuilder, toElement));
body.append(ifElementNull);
}
// call blockBuilder.build()
body.append(blockBuilder.invoke("build", Block.class)).retObject();
// create constructor
MethodDefinition constructorDefinition = definition.declareConstructor(a(PUBLIC));
BytecodeBlock constructorBody = constructorDefinition.getBody();
Variable thisVariable = constructorDefinition.getThis();
constructorBody.comment("super();").append(thisVariable).invokeConstructor(Object.class);
cachedInstanceBinder.generateInitializations(thisVariable, constructorBody);
constructorBody.ret();
return defineClass(definition, Object.class, binder.getBindings(), RowToRowCast.class.getClassLoader());
}
use of com.facebook.presto.bytecode.BytecodeBlock in project presto by prestodb.
the class IfStatement method accept.
@Override
public void accept(MethodVisitor visitor, MethodGenerationContext generationContext) {
checkState(!condition.isEmpty(), "IfStatement does not have a condition set");
checkState(!ifTrue.isEmpty() || !ifFalse.isEmpty(), "IfStatement does not have a true or false block set");
BytecodeBlock block = new BytecodeBlock();
// if !condition goto false;
block.append(new BytecodeBlock().setDescription("condition").append(condition));
block.ifFalseGoto(falseLabel);
if (!ifTrue.isEmpty()) {
block.append(new BytecodeBlock().setDescription("ifTrue").append(ifTrue));
}
if (!ifFalse.isEmpty()) {
// close true case by skipping to end
block.gotoLabel(outLabel);
block.visitLabel(falseLabel);
block.append(new BytecodeBlock().setDescription("ifFalse").append(ifFalse));
block.visitLabel(outLabel);
} else {
block.visitLabel(falseLabel);
}
block.accept(visitor, generationContext);
}
use of com.facebook.presto.bytecode.BytecodeBlock in project presto by prestodb.
the class TryCatch method accept.
@Override
public void accept(MethodVisitor visitor, MethodGenerationContext generationContext) {
LabelNode tryStart = new LabelNode("tryStart");
LabelNode tryEnd = new LabelNode("tryEnd");
LabelNode handler = new LabelNode("handler");
LabelNode done = new LabelNode("done");
BytecodeBlock block = new BytecodeBlock();
// try block
block.visitLabel(tryStart).append(tryNode).visitLabel(tryEnd).gotoLabel(done);
// handler block
block.visitLabel(handler).append(catchNode);
// all done
block.visitLabel(done);
block.accept(visitor, generationContext);
visitor.visitTryCatchBlock(tryStart.getLabel(), tryEnd.getLabel(), handler.getLabel(), exceptionName);
}
use of com.facebook.presto.bytecode.BytecodeBlock in project presto by prestodb.
the class WhileLoop method accept.
@Override
public void accept(MethodVisitor visitor, MethodGenerationContext generationContext) {
checkState(!condition.isEmpty(), "WhileLoop does not have a condition set");
BytecodeBlock block = new BytecodeBlock().visitLabel(continueLabel).append(condition).ifZeroGoto(endLabel).append(body).gotoLabel(continueLabel).visitLabel(endLabel);
block.accept(visitor, generationContext);
}
use of com.facebook.presto.bytecode.BytecodeBlock in project presto by prestodb.
the class AndBytecodeExpression method getBytecode.
@Override
public BytecodeNode getBytecode(MethodGenerationContext generationContext) {
LabelNode falseLabel = new LabelNode("false");
LabelNode endLabel = new LabelNode("end");
return new BytecodeBlock().append(left).ifFalseGoto(falseLabel).append(right).ifFalseGoto(falseLabel).push(true).gotoLabel(endLabel).visitLabel(falseLabel).push(false).visitLabel(endLabel);
}
Aggregations