Search in sources :

Example 1 with InputReferenceExpression

use of com.facebook.presto.spi.relation.InputReferenceExpression in project presto by prestodb.

the class PageFunctionCompiler method compileProjection.

private Supplier<PageProjection> compileProjection(SqlFunctionProperties sqlFunctionProperties, Map<SqlFunctionId, SqlInvokedFunction> sessionFunctions, RowExpression projection, Optional<String> classNameSuffix) {
    if (projection instanceof InputReferenceExpression) {
        InputReferenceExpression input = (InputReferenceExpression) projection;
        InputPageProjection projectionFunction = new InputPageProjection(input.getField());
        return () -> projectionFunction;
    }
    if (projection instanceof ConstantExpression) {
        ConstantExpression constant = (ConstantExpression) projection;
        ConstantPageProjection projectionFunction = new ConstantPageProjection(constant.getValue(), constant.getType());
        return () -> projectionFunction;
    }
    return compileProjectionCached(sqlFunctionProperties, sessionFunctions, ImmutableList.of(projection), false, classNameSuffix);
}
Also used : InputReferenceExpression(com.facebook.presto.spi.relation.InputReferenceExpression) InputPageProjection(com.facebook.presto.operator.project.InputPageProjection) ConstantPageProjection(com.facebook.presto.operator.project.ConstantPageProjection) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression)

Example 2 with InputReferenceExpression

use of com.facebook.presto.spi.relation.InputReferenceExpression in project presto by prestodb.

the class RemoteProjectOperator method addInput.

@Override
public void addInput(Page page) {
    checkState(!finishing, "Operator is already finishing");
    checkState(!processingPage(), "Still processing previous input");
    requireNonNull(page, "page is null");
    for (int channel = 0; channel < projections.size(); channel++) {
        RowExpression projection = projections.get(channel);
        if (projection instanceof InputReferenceExpression) {
            result[channel] = completedFuture(new SqlFunctionResult(page.getBlock(((InputReferenceExpression) projection).getField()), 0));
        } else if (projection instanceof CallExpression) {
            CallExpression remoteCall = (CallExpression) projection;
            result[channel] = functionAndTypeManager.executeFunction(operatorContext.getDriverContext().getTaskId().toString(), remoteCall.getFunctionHandle(), page, remoteCall.getArguments().stream().map(InputReferenceExpression.class::cast).map(InputReferenceExpression::getField).collect(toImmutableList()));
        } else {
            checkState(projection instanceof ConstantExpression, format("Does not expect expression type %s", projection.getClass()));
        }
    }
}
Also used : InputReferenceExpression(com.facebook.presto.spi.relation.InputReferenceExpression) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) SqlFunctionResult(com.facebook.presto.common.function.SqlFunctionResult) CallExpression(com.facebook.presto.spi.relation.CallExpression)

Example 3 with InputReferenceExpression

use of com.facebook.presto.spi.relation.InputReferenceExpression in project presto by prestodb.

the class TestRowExpressionPredicateCompiler method test.

@Test
public void test() {
    InputReferenceExpression a = new InputReferenceExpression(Optional.empty(), 0, BIGINT);
    InputReferenceExpression b = new InputReferenceExpression(Optional.empty(), 1, BIGINT);
    Block aBlock = createLongBlock(5, 5, 5, 5, 5);
    Block bBlock = createLongBlock(1, 3, 5, 7, 0);
    // b - a >= 0
    RowExpression sum = call("<", functionResolution.comparisonFunction(GREATER_THAN_OR_EQUAL, BIGINT, BIGINT), BOOLEAN, call("b - a", functionResolution.arithmeticFunction(SUBTRACT, BIGINT, BIGINT), BIGINT, b, a), constant(0L, BIGINT));
    PredicateCompiler compiler = new RowExpressionPredicateCompiler(metadata, 10_000);
    Predicate compiledSum = compiler.compilePredicate(SESSION.getSqlFunctionProperties(), SESSION.getSessionFunctions(), sum).get();
    assertEquals(Arrays.asList(1, 0), Ints.asList(compiledSum.getInputChannels()));
    Page page = new Page(bBlock, aBlock);
    assertFalse(compiledSum.evaluate(SESSION.getSqlFunctionProperties(), page, 0));
    assertFalse(compiledSum.evaluate(SESSION.getSqlFunctionProperties(), page, 1));
    assertTrue(compiledSum.evaluate(SESSION.getSqlFunctionProperties(), page, 2));
    assertTrue(compiledSum.evaluate(SESSION.getSqlFunctionProperties(), page, 3));
    assertFalse(compiledSum.evaluate(SESSION.getSqlFunctionProperties(), page, 4));
    // b * 2 < 10
    RowExpression timesTwo = call("=", functionResolution.comparisonFunction(LESS_THAN, BIGINT, BIGINT), BOOLEAN, call("b * 2", functionResolution.arithmeticFunction(MULTIPLY, BIGINT, BIGINT), BIGINT, b, constant(2L, BIGINT)), constant(10L, BIGINT));
    Predicate compiledTimesTwo = compiler.compilePredicate(SESSION.getSqlFunctionProperties(), SESSION.getSessionFunctions(), timesTwo).get();
    assertEquals(Arrays.asList(1), Ints.asList(compiledTimesTwo.getInputChannels()));
    page = new Page(bBlock);
    assertTrue(compiledTimesTwo.evaluate(SESSION.getSqlFunctionProperties(), page, 0));
    assertTrue(compiledTimesTwo.evaluate(SESSION.getSqlFunctionProperties(), page, 1));
    assertFalse(compiledTimesTwo.evaluate(SESSION.getSqlFunctionProperties(), page, 2));
    assertFalse(compiledTimesTwo.evaluate(SESSION.getSqlFunctionProperties(), page, 3));
    assertTrue(compiledTimesTwo.evaluate(SESSION.getSqlFunctionProperties(), page, 4));
}
Also used : InputReferenceExpression(com.facebook.presto.spi.relation.InputReferenceExpression) Block(com.facebook.presto.common.block.Block) RowExpression(com.facebook.presto.spi.relation.RowExpression) Page(com.facebook.presto.common.Page) PredicateCompiler(com.facebook.presto.spi.relation.PredicateCompiler) Predicate(com.facebook.presto.common.relation.Predicate) Test(org.testng.annotations.Test)

Example 4 with InputReferenceExpression

use of com.facebook.presto.spi.relation.InputReferenceExpression 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();
        }
    };
}
Also used : InputReferenceExpression(com.facebook.presto.spi.relation.InputReferenceExpression) Variable(com.facebook.presto.bytecode.Variable) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) BytecodeBlock(com.facebook.presto.bytecode.BytecodeBlock) IfStatement(com.facebook.presto.bytecode.control.IfStatement) Type(com.facebook.presto.common.type.Type) CommonSubExpressionFields.initializeCommonSubExpressionFields(com.facebook.presto.sql.gen.CommonSubExpressionRewriter.CommonSubExpressionFields.initializeCommonSubExpressionFields) CommonSubExpressionFields.declareCommonSubExpressionFields(com.facebook.presto.sql.gen.CommonSubExpressionRewriter.CommonSubExpressionFields.declareCommonSubExpressionFields) CommonSubExpressionFields(com.facebook.presto.sql.gen.CommonSubExpressionRewriter.CommonSubExpressionFields) Scope(com.facebook.presto.bytecode.Scope) Slice(io.airlift.slice.Slice) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) RowExpressionVisitor(com.facebook.presto.spi.relation.RowExpressionVisitor) CallExpression(com.facebook.presto.spi.relation.CallExpression) SpecialFormExpression(com.facebook.presto.spi.relation.SpecialFormExpression) LambdaDefinitionExpression(com.facebook.presto.spi.relation.LambdaDefinitionExpression)

Example 5 with InputReferenceExpression

use of com.facebook.presto.spi.relation.InputReferenceExpression in project presto by prestodb.

the class TestPageProcessorCompiler method testNonDeterministicProject.

@Test
public void testNonDeterministicProject() {
    FunctionAndTypeManager functionAndTypeManager = createTestMetadataManager().getFunctionAndTypeManager();
    FunctionHandle lessThan = functionAndTypeManager.resolveOperator(LESS_THAN, fromTypes(BIGINT, BIGINT));
    CallExpression random = new CallExpression("random", functionAndTypeManager.lookupFunction("random", fromTypes(BIGINT)), BIGINT, singletonList(constant(10L, BIGINT)));
    InputReferenceExpression col0 = field(0, BIGINT);
    CallExpression lessThanRandomExpression = new CallExpression(LESS_THAN.name(), lessThan, BOOLEAN, ImmutableList.of(col0, random));
    PageProcessor processor = compiler.compilePageProcessor(TEST_SESSION.getSqlFunctionProperties(), Optional.empty(), ImmutableList.of(lessThanRandomExpression), false, MAX_BATCH_SIZE).get();
    assertFalse(new RowExpressionDeterminismEvaluator(metadataManager.getFunctionAndTypeManager()).isDeterministic(lessThanRandomExpression));
    Page page = new Page(createLongDictionaryBlock(1, 100));
    Page outputPage = getOnlyElement(processor.process(null, new DriverYieldSignal(), newSimpleAggregatedMemoryContext().newLocalMemoryContext(PageProcessor.class.getSimpleName()), page)).orElseThrow(() -> new AssertionError("page is not present"));
    assertFalse(outputPage.getBlock(0) instanceof DictionaryBlock);
}
Also used : RowExpressionDeterminismEvaluator(com.facebook.presto.sql.relational.RowExpressionDeterminismEvaluator) InputReferenceExpression(com.facebook.presto.spi.relation.InputReferenceExpression) PageProcessor(com.facebook.presto.operator.project.PageProcessor) FunctionAndTypeManager(com.facebook.presto.metadata.FunctionAndTypeManager) DictionaryBlock(com.facebook.presto.common.block.DictionaryBlock) BlockAssertions.createLongDictionaryBlock(com.facebook.presto.block.BlockAssertions.createLongDictionaryBlock) DriverYieldSignal(com.facebook.presto.operator.DriverYieldSignal) Page(com.facebook.presto.common.Page) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle) CallExpression(com.facebook.presto.spi.relation.CallExpression) Test(org.testng.annotations.Test)

Aggregations

InputReferenceExpression (com.facebook.presto.spi.relation.InputReferenceExpression)9 CallExpression (com.facebook.presto.spi.relation.CallExpression)6 ConstantExpression (com.facebook.presto.spi.relation.ConstantExpression)5 RowExpression (com.facebook.presto.spi.relation.RowExpression)4 Test (org.testng.annotations.Test)4 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)3 Scope (com.facebook.presto.bytecode.Scope)3 Page (com.facebook.presto.common.Page)3 LambdaDefinitionExpression (com.facebook.presto.spi.relation.LambdaDefinitionExpression)3 RowExpressionVisitor (com.facebook.presto.spi.relation.RowExpressionVisitor)3 SpecialFormExpression (com.facebook.presto.spi.relation.SpecialFormExpression)3 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)3 Parameter (com.facebook.presto.bytecode.Parameter)2 Variable (com.facebook.presto.bytecode.Variable)2 IfStatement (com.facebook.presto.bytecode.control.IfStatement)2 FunctionAndTypeManager (com.facebook.presto.metadata.FunctionAndTypeManager)2 FunctionHandle (com.facebook.presto.spi.function.FunctionHandle)2 CommonSubExpressionFields (com.facebook.presto.sql.gen.CommonSubExpressionRewriter.CommonSubExpressionFields)2 CommonSubExpressionFields.declareCommonSubExpressionFields (com.facebook.presto.sql.gen.CommonSubExpressionRewriter.CommonSubExpressionFields.declareCommonSubExpressionFields)2 CommonSubExpressionFields.initializeCommonSubExpressionFields (com.facebook.presto.sql.gen.CommonSubExpressionRewriter.CommonSubExpressionFields.initializeCommonSubExpressionFields)2