Search in sources :

Example 1 with SpecialForm

use of io.prestosql.spi.relation.SpecialForm in project hetu-core by openlookeng.

the class InCodeGeneratorBenchmark method setup.

@Setup
public void setup() {
    Random random = new Random();
    RowExpression[] arguments = new RowExpression[1 + inListCount];
    switch(type) {
        case StandardTypes.BIGINT:
            prestoType = BIGINT;
            for (int i = 1; i <= inListCount; i++) {
                arguments[i] = constant((long) random.nextInt(), BIGINT);
            }
            break;
        case StandardTypes.DOUBLE:
            prestoType = DOUBLE;
            for (int i = 1; i <= inListCount; i++) {
                arguments[i] = constant(random.nextDouble(), DOUBLE);
            }
            break;
        case StandardTypes.VARCHAR:
            prestoType = VARCHAR;
            for (int i = 1; i <= inListCount; i++) {
                arguments[i] = constant(Slices.utf8Slice(Long.toString(random.nextLong())), VARCHAR);
            }
            break;
        default:
            throw new IllegalStateException();
    }
    arguments[0] = field(0, prestoType);
    RowExpression project = field(0, prestoType);
    PageBuilder pageBuilder = new PageBuilder(ImmutableList.of(prestoType));
    for (int i = 0; i < 10_000; i++) {
        pageBuilder.declarePosition();
        switch(type) {
            case StandardTypes.BIGINT:
                BIGINT.writeLong(pageBuilder.getBlockBuilder(0), random.nextInt());
                break;
            case StandardTypes.DOUBLE:
                DOUBLE.writeDouble(pageBuilder.getBlockBuilder(0), random.nextDouble());
                break;
            case StandardTypes.VARCHAR:
                VARCHAR.writeSlice(pageBuilder.getBlockBuilder(0), Slices.utf8Slice(Long.toString(random.nextLong())));
                break;
        }
    }
    inputPage = pageBuilder.build();
    RowExpression filter = new SpecialForm(IN, BOOLEAN, arguments);
    Metadata metadata = createTestMetadataManager();
    processor = new ExpressionCompiler(metadata, new PageFunctionCompiler(metadata, 0)).compilePageProcessor(Optional.of(filter), ImmutableList.of(project)).get();
}
Also used : Random(java.util.Random) Metadata(io.prestosql.metadata.Metadata) RowExpression(io.prestosql.spi.relation.RowExpression) PageBuilder(io.prestosql.spi.PageBuilder) SpecialForm(io.prestosql.spi.relation.SpecialForm) Setup(org.openjdk.jmh.annotations.Setup)

Example 2 with SpecialForm

use of io.prestosql.spi.relation.SpecialForm in project hetu-core by openlookeng.

the class RowExpressionVerifier method visitSimpleCaseExpression.

@Override
protected Boolean visitSimpleCaseExpression(SimpleCaseExpression expected, RowExpression actual) {
    if (!(actual instanceof SpecialForm && ((SpecialForm) actual).getForm().equals(SWITCH))) {
        return false;
    }
    SpecialForm actualCase = (SpecialForm) actual;
    if (!process(expected.getOperand(), actualCase.getArguments().get(0))) {
        return false;
    }
    List<RowExpression> whenClauses;
    Optional<RowExpression> elseValue;
    RowExpression last = actualCase.getArguments().get(actualCase.getArguments().size() - 1);
    if (last instanceof SpecialForm && ((SpecialForm) last).getForm().equals(WHEN)) {
        whenClauses = actualCase.getArguments().subList(1, actualCase.getArguments().size());
        elseValue = Optional.empty();
    } else {
        whenClauses = actualCase.getArguments().subList(1, actualCase.getArguments().size() - 1);
        elseValue = Optional.of(last);
    }
    if (!process(expected.getWhenClauses(), whenClauses)) {
        return false;
    }
    return process(expected.getDefaultValue(), elseValue);
}
Also used : RowExpression(io.prestosql.spi.relation.RowExpression) SpecialForm(io.prestosql.spi.relation.SpecialForm)

Example 3 with SpecialForm

use of io.prestosql.spi.relation.SpecialForm in project hetu-core by openlookeng.

the class RowExpressionVerifier method visitDereferenceExpression.

@Override
protected Boolean visitDereferenceExpression(DereferenceExpression expected, RowExpression actual) {
    if (!(actual instanceof SpecialForm) || !(((SpecialForm) actual).getForm().equals(DEREFERENCE))) {
        return false;
    }
    SpecialForm actualDereference = (SpecialForm) actual;
    if (actualDereference.getArguments().size() == 2 && actualDereference.getArguments().get(0).getType() instanceof RowType && actualDereference.getArguments().get(1) instanceof ConstantExpression) {
        RowType rowType = (RowType) actualDereference.getArguments().get(0).getType();
        Object value = LiteralInterpreter.evaluate((ConstantExpression) actualDereference.getArguments().get(1));
        checkState(value instanceof Long);
        long index = (Long) value;
        checkState(index >= 0 && index < rowType.getFields().size());
        RowType.Field field = rowType.getFields().get(toIntExact(index));
        checkState(field.getName().isPresent());
        return expected.getField().getValue().equals(field.getName().get()) && process(expected.getBase(), actualDereference.getArguments().get(0));
    }
    return false;
}
Also used : ConstantExpression(io.prestosql.spi.relation.ConstantExpression) RowType(io.prestosql.spi.type.RowType) SpecialForm(io.prestosql.spi.relation.SpecialForm)

Example 4 with SpecialForm

use of io.prestosql.spi.relation.SpecialForm in project hetu-core by openlookeng.

the class CursorProcessorCompiler method fieldReferenceCompiler.

private static RowExpressionVisitor<BytecodeNode, Scope> fieldReferenceCompiler(Variable cursorVariable) {
    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");
            Class<?> javaType = type.getJavaType();
            if (!javaType.isPrimitive() && javaType != Slice.class) {
                javaType = Object.class;
            }
            IfStatement ifStatement = new IfStatement();
            ifStatement.condition().setDescription(String.format(Locale.ROOT, "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 visitSpecialForm(SpecialForm specialForm, Scope context) {
            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) {
            throw new UnsupportedOperationException();
        }
    };
}
Also used : InputReferenceExpression(io.prestosql.spi.relation.InputReferenceExpression) Variable(io.airlift.bytecode.Variable) ConstantExpression(io.prestosql.spi.relation.ConstantExpression) IfStatement(io.airlift.bytecode.control.IfStatement) Type(io.prestosql.spi.type.Type) Scope(io.airlift.bytecode.Scope) Slice(io.airlift.slice.Slice) VariableReferenceExpression(io.prestosql.spi.relation.VariableReferenceExpression) RowExpressionVisitor(io.prestosql.spi.relation.RowExpressionVisitor) CallExpression(io.prestosql.spi.relation.CallExpression) SpecialForm(io.prestosql.spi.relation.SpecialForm) LambdaDefinitionExpression(io.prestosql.spi.relation.LambdaDefinitionExpression)

Example 5 with SpecialForm

use of io.prestosql.spi.relation.SpecialForm in project hetu-core by openlookeng.

the class SwitchCodeGenerator method generateExpression.

@Override
public BytecodeNode generateExpression(FunctionHandle functionHandle, BytecodeGeneratorContext generatorContext, Type returnType, List<RowExpression> arguments) {
    // TODO: compile as
    /*
            hashCode = hashCode(<value>)

            // all constant expressions before a non-constant
            switch (hashCode) {
                case ...:
                    if (<value> == <constant1>) {
                       ...
                    }
                    else if (<value> == <constant2>) {
                       ...
                    }
                    else if (...) {
                    }
                case ...:
                    ...
            }

            if (<value> == <non-constant1>) {
                ...
            }
            else if (<value> == <non-constant2>) {
                ...
            }
            ...

            // repeat with next sequence of constant expressions
         */
    Scope scope = generatorContext.getScope();
    // process value, else, and all when clauses
    RowExpression value = arguments.get(0);
    BytecodeNode valueBytecode = generatorContext.generate(value);
    BytecodeNode elseValue;
    List<RowExpression> whenClauses;
    RowExpression last = arguments.get(arguments.size() - 1);
    if (last instanceof SpecialForm && ((SpecialForm) last).getForm() == WHEN) {
        whenClauses = arguments.subList(1, arguments.size());
        elseValue = new BytecodeBlock().append(generatorContext.wasNull().set(constantTrue())).pushJavaDefault(returnType.getJavaType());
    } else {
        whenClauses = arguments.subList(1, arguments.size() - 1);
        elseValue = generatorContext.generate(last);
    }
    // determine the type of the value and result
    Class<?> valueType = value.getType().getJavaType();
    // evaluate the value and store it in a variable
    LabelNode nullValue = new LabelNode("nullCondition");
    Variable tempVariable = scope.createTempVariable(valueType);
    BytecodeBlock block = new BytecodeBlock().append(valueBytecode).append(BytecodeUtils.ifWasNullClearPopAndGoto(scope, nullValue, void.class, valueType)).putVariable(tempVariable);
    BytecodeNode getTempVariableNode = VariableInstruction.loadVariable(tempVariable);
    // build the statements
    elseValue = new BytecodeBlock().visitLabel(nullValue).append(elseValue);
    // reverse list because current if statement builder doesn't support if/else so we need to build the if statements bottom up
    for (RowExpression clause : Lists.reverse(whenClauses)) {
        Preconditions.checkArgument(clause instanceof SpecialForm && ((SpecialForm) clause).getForm() == WHEN);
        RowExpression operand = ((SpecialForm) clause).getArguments().get(0);
        RowExpression result = ((SpecialForm) clause).getArguments().get(1);
        // call equals(value, operand)
        FunctionHandle equalsFunction = generatorContext.getFunctionManager().resolveOperatorFunctionHandle(EQUAL, fromTypes(value.getType(), operand.getType()));
        // TODO: what if operand is null? It seems that the call will return "null" (which is cleared below)
        // and the code only does the right thing because the value in the stack for that scenario is
        // Java's default for boolean == false
        // This code should probably be checking for wasNull after the call and "failing" the equality
        // check if wasNull is true
        BytecodeNode equalsCall = generatorContext.generateCall(EQUAL.name(), generatorContext.getFunctionManager().getBuiltInScalarFunctionImplementation(equalsFunction), ImmutableList.of(generatorContext.generate(operand, Optional.empty()), getTempVariableNode));
        BytecodeBlock condition = new BytecodeBlock().append(equalsCall).append(generatorContext.wasNull().set(constantFalse()));
        elseValue = new IfStatement("when").condition(condition).ifTrue(generatorContext.generate(result)).ifFalse(elseValue);
    }
    return block.append(elseValue);
}
Also used : LabelNode(io.airlift.bytecode.instruction.LabelNode) IfStatement(io.airlift.bytecode.control.IfStatement) Variable(io.airlift.bytecode.Variable) Scope(io.airlift.bytecode.Scope) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) RowExpression(io.prestosql.spi.relation.RowExpression) BytecodeNode(io.airlift.bytecode.BytecodeNode) FunctionHandle(io.prestosql.spi.function.FunctionHandle) SpecialForm(io.prestosql.spi.relation.SpecialForm)

Aggregations

SpecialForm (io.prestosql.spi.relation.SpecialForm)22 RowExpression (io.prestosql.spi.relation.RowExpression)16 VariableReferenceExpression (io.prestosql.spi.relation.VariableReferenceExpression)11 ConstantExpression (io.prestosql.spi.relation.ConstantExpression)9 CallExpression (io.prestosql.spi.relation.CallExpression)8 Test (org.testng.annotations.Test)6 BuiltInFunctionHandle (io.prestosql.spi.function.BuiltInFunctionHandle)5 OperatorType (io.prestosql.spi.function.OperatorType)4 Signature (io.prestosql.spi.function.Signature)4 ArrayList (java.util.ArrayList)4 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)3 Scope (io.airlift.bytecode.Scope)3 Variable (io.airlift.bytecode.Variable)3 FunctionHandle (io.prestosql.spi.function.FunctionHandle)3 IndexMetadata (io.prestosql.spi.heuristicindex.IndexMetadata)3 IfStatement (io.airlift.bytecode.control.IfStatement)2 LabelNode (io.airlift.bytecode.instruction.LabelNode)2 IndexLookUpException (io.prestosql.spi.heuristicindex.IndexLookUpException)2 Pair (io.prestosql.spi.heuristicindex.Pair)2 Symbol (io.prestosql.spi.plan.Symbol)2