Search in sources :

Example 76 with Variable

use of com.facebook.presto.bytecode.Variable in project presto by prestodb.

the class PageProcessorCompiler method getExpressionInputBlocks.

private static Map<RowExpression, List<Variable>> getExpressionInputBlocks(List<RowExpression> projections, RowExpression filter, Map<Integer, Variable> channelBlock) {
    Map<RowExpression, List<Variable>> inputBlocksBuilder = new HashMap<>();
    for (RowExpression projection : projections) {
        List<Variable> inputBlocks = getInputChannels(projection).stream().map(channelBlock::get).collect(toList());
        List<Variable> existingVariables = inputBlocksBuilder.get(projection);
        // Constant expressions or expressions that are reused, should reference the same input blocks
        checkState(existingVariables == null || existingVariables.equals(inputBlocks), "malformed RowExpression");
        inputBlocksBuilder.put(projection, inputBlocks);
    }
    List<Variable> filterBlocks = getInputChannels(filter).stream().map(channelBlock::get).collect(toList());
    inputBlocksBuilder.put(filter, filterBlocks);
    return inputBlocksBuilder;
}
Also used : Variable(com.facebook.presto.bytecode.Variable) HashMap(java.util.HashMap) RowExpression(com.facebook.presto.sql.relational.RowExpression) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList)

Example 77 with Variable

use of com.facebook.presto.bytecode.Variable in project presto by prestodb.

the class PageProcessorCompiler method generateConstructor.

private static void generateConstructor(ClassDefinition classDefinition, CachedInstanceBinder cachedInstanceBinder, int projectionCount) {
    MethodDefinition constructorDefinition = classDefinition.declareConstructor(a(PUBLIC));
    FieldDefinition inputDictionaries = classDefinition.declareField(a(PRIVATE, FINAL), "inputDictionaries", Block[].class);
    FieldDefinition outputDictionaries = classDefinition.declareField(a(PRIVATE, FINAL), "outputDictionaries", Block[].class);
    FieldDefinition inputFilterDictionary = classDefinition.declareField(a(PRIVATE), "inputFilterDictionary", Block.class);
    FieldDefinition filterResult = classDefinition.declareField(a(PRIVATE), "filterResult", boolean[].class);
    BytecodeBlock body = constructorDefinition.getBody();
    Variable thisVariable = constructorDefinition.getThis();
    body.comment("super();").append(thisVariable).invokeConstructor(Object.class);
    body.append(thisVariable.setField(inputDictionaries, newArray(type(Block[].class), projectionCount)));
    body.append(thisVariable.setField(outputDictionaries, newArray(type(Block[].class), projectionCount)));
    body.append(thisVariable.setField(inputFilterDictionary, constantNull(Block.class)));
    body.append(thisVariable.setField(filterResult, constantNull(boolean[].class)));
    cachedInstanceBinder.generateInitializations(thisVariable, body);
    body.ret();
}
Also used : Variable(com.facebook.presto.bytecode.Variable) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) FieldDefinition(com.facebook.presto.bytecode.FieldDefinition) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) Block(com.facebook.presto.spi.block.Block) DictionaryBlock(com.facebook.presto.spi.block.DictionaryBlock) LazyBlock(com.facebook.presto.spi.block.LazyBlock) RunLengthEncodedBlock(com.facebook.presto.spi.block.RunLengthEncodedBlock) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock)

Example 78 with Variable

use of com.facebook.presto.bytecode.Variable in project presto by prestodb.

the class PageProcessorCompiler method generateProcessColumnarDictionaryMethod.

private static void generateProcessColumnarDictionaryMethod(ClassDefinition classDefinition, List<RowExpression> projections, List<MethodDefinition> projectDictionaryMethods) {
    Parameter session = arg("session", ConnectorSession.class);
    Parameter page = arg("page", Page.class);
    Parameter types = arg("types", List.class);
    MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), "processColumnarDictionary", type(Page.class), session, page, types);
    Scope scope = method.getScope();
    BytecodeBlock body = method.getBody();
    Variable thisVariable = method.getThis();
    Variable selectedPositions = scope.declareVariable("selectedPositions", body, thisVariable.invoke("filterPage", int[].class, session, page));
    Variable cardinality = scope.declareVariable("cardinality", body, selectedPositions.length());
    Variable dictionarySourceIds = scope.declareVariable(type(Map.class, DictionaryId.class, DictionaryId.class), "dictionarySourceIds");
    body.append(dictionarySourceIds.set(newInstance(type(HashMap.class, DictionaryId.class, DictionaryId.class))));
    body.comment("if no rows selected return null").append(new IfStatement().condition(equal(cardinality, constantInt(0))).ifTrue(constantNull(Page.class).ret()));
    if (projections.isEmpty()) {
        // if no projections, return new page with selected rows
        body.append(newInstance(Page.class, cardinality, newArray(type(Block[].class), 0)).ret());
        return;
    }
    // create PageBuilder
    Variable pageBuilder = scope.declareVariable("pageBuilder", body, newInstance(PageBuilder.class, cardinality, types));
    body.append(page.set(thisVariable.invoke("getNonLazyPage", Page.class, page)));
    // create outputBlocks
    Variable outputBlocks = scope.declareVariable("outputBlocks", body, newArray(type(Block[].class), projections.size()));
    for (int projectionIndex = 0; projectionIndex < projections.size(); projectionIndex++) {
        List<BytecodeExpression> params = ImmutableList.<BytecodeExpression>builder().add(session).add(page).add(selectedPositions).add(pageBuilder).add(constantInt(projectionIndex)).add(dictionarySourceIds).build();
        body.append(outputBlocks.setElement(projectionIndex, thisVariable.invoke(projectDictionaryMethods.get(projectionIndex), params)));
    }
    body.append(newInstance(Page.class, cardinality, outputBlocks).ret());
}
Also used : Variable(com.facebook.presto.bytecode.Variable) HashMap(java.util.HashMap) DictionaryId(com.facebook.presto.spi.block.DictionaryId) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) Page(com.facebook.presto.spi.Page) PageBuilder(com.facebook.presto.spi.PageBuilder) IfStatement(com.facebook.presto.bytecode.control.IfStatement) Scope(com.facebook.presto.bytecode.Scope) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) Parameter(com.facebook.presto.bytecode.Parameter) Block(com.facebook.presto.spi.block.Block) DictionaryBlock(com.facebook.presto.spi.block.DictionaryBlock) LazyBlock(com.facebook.presto.spi.block.LazyBlock) RunLengthEncodedBlock(com.facebook.presto.spi.block.RunLengthEncodedBlock) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) BytecodeExpression(com.facebook.presto.bytecode.expression.BytecodeExpression) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Example 79 with Variable

use of com.facebook.presto.bytecode.Variable in project presto by prestodb.

the class MapFilterFunction method generateFilter.

private static MethodHandle generateFilter(Type keyType, Type valueType) {
    CallSiteBinder binder = new CallSiteBinder();
    MapType mapType = new MapType(keyType, valueType);
    Class<?> keyJavaType = Primitives.wrap(keyType.getJavaType());
    Class<?> valueJavaType = Primitives.wrap(valueType.getJavaType());
    ClassDefinition definition = new ClassDefinition(a(PUBLIC, FINAL), makeClassName("MapFilter"), type(Object.class));
    definition.declareDefaultConstructor(a(PRIVATE));
    Parameter block = arg("block", Block.class);
    Parameter function = arg("function", MethodHandle.class);
    MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "filter", type(Block.class), ImmutableList.of(block, function));
    BytecodeBlock body = method.getBody();
    Scope scope = method.getScope();
    Variable positionCount = scope.declareVariable(int.class, "positionCount");
    Variable position = scope.declareVariable(int.class, "position");
    Variable blockBuilder = scope.declareVariable(BlockBuilder.class, "blockBuilder");
    Variable keyElement = scope.declareVariable(keyJavaType, "keyElement");
    Variable valueElement = scope.declareVariable(valueJavaType, "valueElement");
    Variable keep = scope.declareVariable(Boolean.class, "keep");
    // invoke block.getPositionCount()
    body.append(positionCount.set(block.invoke("getPositionCount", int.class)));
    // create the interleaved block builder
    body.append(blockBuilder.set(newInstance(InterleavedBlockBuilder.class, constantType(binder, mapType).invoke("getTypeParameters", List.class), newInstance(BlockBuilderStatus.class), positionCount)));
    SqlTypeBytecodeExpression keySqlType = constantType(binder, keyType);
    BytecodeNode loadKeyElement;
    if (!keyType.equals(UNKNOWN)) {
        // key element must be non-null
        loadKeyElement = new BytecodeBlock().append(keyElement.set(keySqlType.getValue(block, position).cast(keyJavaType)));
    } else {
        loadKeyElement = new BytecodeBlock().append(keyElement.set(constantNull(keyJavaType)));
    }
    SqlTypeBytecodeExpression valueSqlType = constantType(binder, valueType);
    BytecodeNode loadValueElement;
    if (!valueType.equals(UNKNOWN)) {
        loadValueElement = new IfStatement().condition(block.invoke("isNull", boolean.class, add(position, constantInt(1)))).ifTrue(valueElement.set(constantNull(valueJavaType))).ifFalse(valueElement.set(valueSqlType.getValue(block, add(position, constantInt(1))).cast(valueJavaType)));
    } else {
        loadValueElement = new BytecodeBlock().append(valueElement.set(constantNull(valueJavaType)));
    }
    body.append(new ForLoop().initialize(position.set(constantInt(0))).condition(lessThan(position, positionCount)).update(incrementVariable(position, (byte) 2)).body(new BytecodeBlock().append(loadKeyElement).append(loadValueElement).append(keep.set(function.invoke("invokeExact", Boolean.class, keyElement, valueElement))).append(new IfStatement("if (keep != null && keep) ...").condition(and(notEqual(keep, constantNull(Boolean.class)), keep.cast(boolean.class))).ifTrue(new BytecodeBlock().append(keySqlType.invoke("appendTo", void.class, block, position, blockBuilder)).append(valueSqlType.invoke("appendTo", void.class, block, add(position, constantInt(1)), blockBuilder))))));
    body.append(blockBuilder.invoke("build", Block.class).ret());
    Class<?> generatedClass = defineClass(definition, Object.class, binder.getBindings(), MapFilterFunction.class.getClassLoader());
    return methodHandle(generatedClass, "filter", Block.class, MethodHandle.class);
}
Also used : Variable(com.facebook.presto.bytecode.Variable) Signature.typeVariable(com.facebook.presto.metadata.Signature.typeVariable) VariableInstruction.incrementVariable(com.facebook.presto.bytecode.instruction.VariableInstruction.incrementVariable) ForLoop(com.facebook.presto.bytecode.control.ForLoop) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) ClassDefinition(com.facebook.presto.bytecode.ClassDefinition) MapType(com.facebook.presto.type.MapType) IfStatement(com.facebook.presto.bytecode.control.IfStatement) Scope(com.facebook.presto.bytecode.Scope) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) CallSiteBinder(com.facebook.presto.sql.gen.CallSiteBinder) Parameter(com.facebook.presto.bytecode.Parameter) Block(com.facebook.presto.spi.block.Block) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) BytecodeNode(com.facebook.presto.bytecode.BytecodeNode) BlockBuilderStatus(com.facebook.presto.spi.block.BlockBuilderStatus) SqlTypeBytecodeExpression(com.facebook.presto.sql.gen.SqlTypeBytecodeExpression)

Example 80 with Variable

use of com.facebook.presto.bytecode.Variable in project presto by prestodb.

the class CursorProcessorCompiler method generateProjectMethod.

private void generateProjectMethod(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, CachedInstanceBinder cachedInstanceBinder, String methodName, RowExpression projection) {
    PreGeneratedExpressions preGeneratedExpressions = generateMethodsForLambdaAndTry(classDefinition, callSiteBinder, cachedInstanceBinder, projection, methodName);
    Parameter session = arg("session", ConnectorSession.class);
    Parameter cursor = arg("cursor", RecordCursor.class);
    Parameter output = arg("output", BlockBuilder.class);
    MethodDefinition method = classDefinition.declareMethod(a(PUBLIC), methodName, type(void.class), session, cursor, output);
    method.comment("Projection: %s", projection.toString());
    Scope scope = method.getScope();
    Variable wasNullVariable = scope.declareVariable(type(boolean.class), "wasNull");
    BytecodeExpressionVisitor visitor = new BytecodeExpressionVisitor(callSiteBinder, cachedInstanceBinder, fieldReferenceCompiler(cursor), metadata.getFunctionRegistry(), preGeneratedExpressions);
    method.getBody().comment("boolean wasNull = false;").putVariable(wasNullVariable, false).getVariable(output).comment("evaluate projection: " + projection.toString()).append(projection.accept(visitor, scope)).append(generateWrite(callSiteBinder, scope, wasNullVariable, projection.getType())).ret();
}
Also used : Variable(com.facebook.presto.bytecode.Variable) Scope(com.facebook.presto.bytecode.Scope) MethodDefinition(com.facebook.presto.bytecode.MethodDefinition) Parameter(com.facebook.presto.bytecode.Parameter)

Aggregations

Variable (com.facebook.presto.bytecode.Variable)131 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)104 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)91 Parameter (com.facebook.presto.bytecode.Parameter)75 Scope (com.facebook.presto.bytecode.Scope)68 IfStatement (com.facebook.presto.bytecode.control.IfStatement)68 BytecodeExpression (com.facebook.presto.bytecode.expression.BytecodeExpression)40 BytecodeNode (com.facebook.presto.bytecode.BytecodeNode)36 ImmutableList (com.google.common.collect.ImmutableList)30 LabelNode (com.facebook.presto.bytecode.instruction.LabelNode)28 ClassDefinition (com.facebook.presto.bytecode.ClassDefinition)21 ForLoop (com.facebook.presto.bytecode.control.ForLoop)21 Block (com.facebook.presto.spi.block.Block)19 Block (com.facebook.presto.common.block.Block)18 List (java.util.List)17 CallSiteBinder (com.facebook.presto.bytecode.CallSiteBinder)16 FieldDefinition (com.facebook.presto.bytecode.FieldDefinition)15 Type (com.facebook.presto.common.type.Type)15 RowExpression (com.facebook.presto.spi.relation.RowExpression)15 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)14