use of com.facebook.presto.bytecode.control.IfStatement in project presto by prestodb.
the class CursorProcessorCompiler method createProjectIfStatement.
private static IfStatement createProjectIfStatement(ClassDefinition classDefinition, MethodDefinition method, Parameter properties, Parameter cursor, Parameter pageBuilder, int projections) {
// if (filter(cursor))
IfStatement ifStatement = new IfStatement();
ifStatement.condition().append(method.getThis()).getVariable(properties).getVariable(cursor).invokeVirtual(classDefinition.getType(), "filter", type(boolean.class), type(SqlFunctionProperties.class), type(RecordCursor.class));
// pageBuilder.declarePosition();
ifStatement.ifTrue().getVariable(pageBuilder).invokeVirtual(PageBuilder.class, "declarePosition", void.class);
// this.project_43(session, cursor, pageBuilder.getBlockBuilder(42)));
for (int projectionIndex = 0; projectionIndex < projections; projectionIndex++) {
ifStatement.ifTrue().append(method.getThis()).getVariable(properties).getVariable(cursor);
// pageBuilder.getBlockBuilder(0)
ifStatement.ifTrue().getVariable(pageBuilder).push(projectionIndex).invokeVirtual(PageBuilder.class, "getBlockBuilder", BlockBuilder.class, int.class);
// project(block..., blockBuilder)gen
ifStatement.ifTrue().invokeVirtual(classDefinition.getType(), "project_" + projectionIndex, type(void.class), type(SqlFunctionProperties.class), type(RecordCursor.class), type(BlockBuilder.class));
}
return ifStatement;
}
use of com.facebook.presto.bytecode.control.IfStatement in project presto by prestodb.
the class CursorProcessorCompiler method fieldReferenceCompiler.
static RowExpressionVisitor<BytecodeNode, Scope> fieldReferenceCompiler(Map<VariableReferenceExpression, CommonSubExpressionFields> variableMap) {
return new RowExpressionVisitor<BytecodeNode, Scope>() {
@Override
public BytecodeNode visitInputReference(InputReferenceExpression node, Scope scope) {
int field = node.getField();
Type type = node.getType();
Variable wasNullVariable = scope.getVariable("wasNull");
Variable cursorVariable = scope.getVariable("cursor");
Class<?> javaType = type.getJavaType();
if (!javaType.isPrimitive() && javaType != Slice.class) {
javaType = Object.class;
}
IfStatement ifStatement = new IfStatement();
ifStatement.condition().setDescription(format("cursor.get%s(%d)", type, field)).getVariable(cursorVariable).push(field).invokeInterface(RecordCursor.class, "isNull", boolean.class, int.class);
ifStatement.ifTrue().putVariable(wasNullVariable, true).pushJavaDefault(javaType);
ifStatement.ifFalse().getVariable(cursorVariable).push(field).invokeInterface(RecordCursor.class, "get" + Primitives.wrap(javaType).getSimpleName(), javaType, int.class);
return ifStatement;
}
@Override
public BytecodeNode visitCall(CallExpression call, Scope scope) {
throw new UnsupportedOperationException("not yet implemented");
}
@Override
public BytecodeNode visitConstant(ConstantExpression literal, Scope scope) {
throw new UnsupportedOperationException("not yet implemented");
}
@Override
public BytecodeNode visitLambda(LambdaDefinitionExpression lambda, Scope context) {
throw new UnsupportedOperationException();
}
@Override
public BytecodeNode visitVariableReference(VariableReferenceExpression reference, Scope context) {
CommonSubExpressionFields fields = variableMap.get(reference);
return new BytecodeBlock().append(context.getThis().invoke(fields.getMethodName(), fields.getResultType(), context.getVariable("properties"), context.getVariable("cursor"))).append(unboxPrimitiveIfNecessary(context, Primitives.wrap(reference.getType().getJavaType())));
}
@Override
public BytecodeNode visitSpecialForm(SpecialFormExpression specialForm, Scope context) {
throw new UnsupportedOperationException();
}
};
}
use of com.facebook.presto.bytecode.control.IfStatement in project presto by prestodb.
the class CursorProcessorCompiler method generateCommonSubExpressionMethods.
private List<MethodDefinition> generateCommonSubExpressionMethods(ClassDefinition classDefinition, RowExpressionCompiler compiler, Map<Integer, Map<RowExpression, VariableReferenceExpression>> commonSubExpressionsByLevel, Map<VariableReferenceExpression, CommonSubExpressionFields> commonSubExpressionFieldsMap) {
Parameter properties = arg("properties", SqlFunctionProperties.class);
Parameter cursor = arg("cursor", RecordCursor.class);
ImmutableList.Builder<MethodDefinition> methods = ImmutableList.builder();
Map<VariableReferenceExpression, CommonSubExpressionFields> cseMap = new HashMap<>();
int startLevel = commonSubExpressionsByLevel.keySet().stream().reduce(Math::min).get();
int maxLevel = commonSubExpressionsByLevel.keySet().stream().reduce(Math::max).get();
for (int i = startLevel; i <= maxLevel; i++) {
if (commonSubExpressionsByLevel.containsKey(i)) {
for (Map.Entry<RowExpression, VariableReferenceExpression> entry : commonSubExpressionsByLevel.get(i).entrySet()) {
RowExpression cse = entry.getKey();
Class<?> type = Primitives.wrap(cse.getType().getJavaType());
VariableReferenceExpression cseVariable = entry.getValue();
CommonSubExpressionFields cseFields = commonSubExpressionFieldsMap.get(cseVariable);
MethodDefinition method = classDefinition.declareMethod(a(PRIVATE), "get" + cseVariable.getName(), type(cseFields.getResultType()), properties, cursor);
method.comment("cse: %s", cse);
Scope scope = method.getScope();
BytecodeBlock body = method.getBody();
Variable thisVariable = method.getThis();
scope.declareVariable("wasNull", body, constantFalse());
IfStatement ifStatement = new IfStatement().condition(thisVariable.getField(cseFields.getEvaluatedField())).ifFalse(new BytecodeBlock().append(thisVariable).append(compiler.compile(cse, scope, Optional.empty())).append(boxPrimitiveIfNecessary(scope, type)).putField(cseFields.getResultField()).append(thisVariable.setField(cseFields.getEvaluatedField(), constantBoolean(true))));
body.append(ifStatement).append(thisVariable).getField(cseFields.getResultField()).retObject();
methods.add(method);
cseMap.put(cseVariable, cseFields);
}
}
}
return methods.build();
}
use of com.facebook.presto.bytecode.control.IfStatement in project presto by prestodb.
the class BytecodeUtils method boxPrimitiveIfNecessary.
public static BytecodeNode boxPrimitiveIfNecessary(Scope scope, Class<?> type) {
checkArgument(!type.isPrimitive(), "cannot box into primitive type");
if (!Primitives.isWrapperType(type)) {
return NOP;
}
BytecodeBlock notNull = new BytecodeBlock().comment("box primitive");
Class<?> expectedCurrentStackType;
if (type == Long.class) {
notNull.invokeStatic(Long.class, "valueOf", Long.class, long.class);
expectedCurrentStackType = long.class;
} else if (type == Double.class) {
notNull.invokeStatic(Double.class, "valueOf", Double.class, double.class);
expectedCurrentStackType = double.class;
} else if (type == Boolean.class) {
notNull.invokeStatic(Boolean.class, "valueOf", Boolean.class, boolean.class);
expectedCurrentStackType = boolean.class;
} else {
throw new UnsupportedOperationException("not yet implemented: " + type);
}
BytecodeBlock condition = new BytecodeBlock().append(scope.getVariable("wasNull"));
BytecodeBlock wasNull = new BytecodeBlock().pop(expectedCurrentStackType).pushNull().checkCast(type);
return new IfStatement().condition(condition).ifTrue(wasNull).ifFalse(notNull);
}
use of com.facebook.presto.bytecode.control.IfStatement in project presto by prestodb.
the class BytecodeUtils method generateWrite.
public static BytecodeNode generateWrite(CallSiteBinder callSiteBinder, Scope scope, Variable wasNullVariable, Type type, Variable outputBlockVariable) {
Class<?> valueJavaType = type.getJavaType();
if (!valueJavaType.isPrimitive() && valueJavaType != Slice.class) {
valueJavaType = Object.class;
}
String methodName = "write" + Primitives.wrap(valueJavaType).getSimpleName();
// the value to be written is at the top of stack
Variable tempValue = scope.createTempVariable(valueJavaType);
return new BytecodeBlock().comment("if (wasNull)").append(new IfStatement().condition(wasNullVariable).ifTrue(new BytecodeBlock().comment("output.appendNull();").pop(valueJavaType).getVariable(outputBlockVariable).invokeInterface(BlockBuilder.class, "appendNull", BlockBuilder.class).pop()).ifFalse(new BytecodeBlock().comment("%s.%s(output, %s)", type.getTypeSignature(), methodName, valueJavaType.getSimpleName()).putVariable(tempValue).append(loadConstant(callSiteBinder.bind(type, Type.class))).getVariable(outputBlockVariable).getVariable(tempValue).invokeInterface(Type.class, methodName, void.class, BlockBuilder.class, valueJavaType)));
}
Aggregations