Search in sources :

Example 31 with RowExpression

use of io.trino.sql.relational.RowExpression in project trino by trinodb.

the class CoalesceCodeGenerator method generateExpression.

@Override
public BytecodeNode generateExpression(BytecodeGeneratorContext generatorContext) {
    List<BytecodeNode> operands = new ArrayList<>();
    for (RowExpression expression : arguments) {
        operands.add(generatorContext.generate(expression));
    }
    Variable wasNull = generatorContext.wasNull();
    BytecodeNode nullValue = new BytecodeBlock().append(wasNull.set(constantTrue())).pushJavaDefault(returnType.getJavaType());
    // reverse list because current if statement builder doesn't support if/else so we need to build the if statements bottom up
    for (BytecodeNode operand : Lists.reverse(operands)) {
        IfStatement ifStatement = new IfStatement();
        ifStatement.condition().append(operand).append(wasNull);
        // if value was null, pop the null value, clear the null flag, and process the next operand
        ifStatement.ifTrue().pop(returnType.getJavaType()).append(wasNull.set(constantFalse())).append(nullValue);
        nullValue = ifStatement;
    }
    return nullValue;
}
Also used : IfStatement(io.airlift.bytecode.control.IfStatement) Variable(io.airlift.bytecode.Variable) ArrayList(java.util.ArrayList) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) RowExpression(io.trino.sql.relational.RowExpression) BytecodeNode(io.airlift.bytecode.BytecodeNode)

Example 32 with RowExpression

use of io.trino.sql.relational.RowExpression in project trino by trinodb.

the class TestFilterAndProjectOperator method test.

@Test
public void test() {
    List<Page> input = rowPagesBuilder(VARCHAR, BIGINT).addSequencePage(100, 0, 0).build();
    TestingFunctionResolution functionResolution = new TestingFunctionResolution();
    RowExpression filter = call(functionResolution.resolveOperator(LESS_THAN_OR_EQUAL, ImmutableList.of(BIGINT, BIGINT)), field(1, BIGINT), constant(9L, BIGINT));
    RowExpression field0 = field(0, VARCHAR);
    RowExpression add5 = call(functionResolution.resolveOperator(ADD, ImmutableList.of(BIGINT, BIGINT)), field(1, BIGINT), constant(5L, BIGINT));
    ExpressionCompiler compiler = functionResolution.getExpressionCompiler();
    Supplier<PageProcessor> processor = compiler.compilePageProcessor(Optional.of(filter), ImmutableList.of(field0, add5));
    OperatorFactory operatorFactory = FilterAndProjectOperator.createOperatorFactory(0, new PlanNodeId("test"), processor, ImmutableList.of(VARCHAR, BIGINT), DataSize.ofBytes(0), 0);
    MaterializedResult expected = MaterializedResult.resultBuilder(driverContext.getSession(), VARCHAR, BIGINT).row("0", 5L).row("1", 6L).row("2", 7L).row("3", 8L).row("4", 9L).row("5", 10L).row("6", 11L).row("7", 12L).row("8", 13L).row("9", 14L).build();
    assertOperatorEquals(operatorFactory, driverContext, input, expected);
}
Also used : TestingFunctionResolution(io.trino.metadata.TestingFunctionResolution) PlanNodeId(io.trino.sql.planner.plan.PlanNodeId) PageProcessor(io.trino.operator.project.PageProcessor) RowExpression(io.trino.sql.relational.RowExpression) Page(io.trino.spi.Page) ExpressionCompiler(io.trino.sql.gen.ExpressionCompiler) MaterializedResult(io.trino.testing.MaterializedResult) Test(org.testng.annotations.Test)

Example 33 with RowExpression

use of io.trino.sql.relational.RowExpression in project trino by trinodb.

the class AbstractOperatorBenchmark method createHashProjectOperator.

protected final OperatorFactory createHashProjectOperator(int operatorId, PlanNodeId planNodeId, List<Type> types) {
    SymbolAllocator symbolAllocator = new SymbolAllocator();
    ImmutableMap.Builder<Symbol, Integer> symbolToInputMapping = ImmutableMap.builder();
    ImmutableList.Builder<PageProjection> projections = ImmutableList.builder();
    for (int channel = 0; channel < types.size(); channel++) {
        Symbol symbol = symbolAllocator.newSymbol("h" + channel, types.get(channel));
        symbolToInputMapping.put(symbol, channel);
        projections.add(new InputPageProjection(channel, types.get(channel)));
    }
    Map<Symbol, Type> symbolTypes = symbolAllocator.getTypes().allTypes();
    Optional<Expression> hashExpression = HashGenerationOptimizer.getHashExpression(session, localQueryRunner.getMetadata(), symbolAllocator, ImmutableList.copyOf(symbolTypes.keySet()));
    verify(hashExpression.isPresent());
    Map<NodeRef<Expression>, Type> expressionTypes = createTestingTypeAnalyzer(localQueryRunner.getPlannerContext()).getTypes(session, TypeProvider.copyOf(symbolTypes), hashExpression.get());
    RowExpression translated = translate(hashExpression.get(), expressionTypes, symbolToInputMapping.buildOrThrow(), localQueryRunner.getMetadata(), localQueryRunner.getFunctionManager(), session, false);
    PageFunctionCompiler functionCompiler = new PageFunctionCompiler(localQueryRunner.getFunctionManager(), 0);
    projections.add(functionCompiler.compileProjection(translated, Optional.empty()).get());
    return FilterAndProjectOperator.createOperatorFactory(operatorId, planNodeId, () -> new PageProcessor(Optional.empty(), projections.build()), ImmutableList.copyOf(Iterables.concat(types, ImmutableList.of(BIGINT))), getFilterAndProjectMinOutputPageSize(session), getFilterAndProjectMinOutputPageRowCount(session));
}
Also used : SymbolAllocator(io.trino.sql.planner.SymbolAllocator) PageFunctionCompiler(io.trino.sql.gen.PageFunctionCompiler) Symbol(io.trino.sql.planner.Symbol) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) RowExpression(io.trino.sql.relational.RowExpression) ImmutableMap(com.google.common.collect.ImmutableMap) PageProjection(io.trino.operator.project.PageProjection) InputPageProjection(io.trino.operator.project.InputPageProjection) NodeRef(io.trino.sql.tree.NodeRef) Type(io.trino.spi.type.Type) InputPageProjection(io.trino.operator.project.InputPageProjection) PageProcessor(io.trino.operator.project.PageProcessor) Expression(io.trino.sql.tree.Expression) RowExpression(io.trino.sql.relational.RowExpression)

Example 34 with RowExpression

use of io.trino.sql.relational.RowExpression in project trino by trinodb.

the class PredicateFilterBenchmark method createOperatorFactories.

@Override
protected List<? extends OperatorFactory> createOperatorFactories() {
    OperatorFactory tableScanOperator = createTableScanOperator(0, new PlanNodeId("test"), "orders", "totalprice");
    RowExpression filter = call(localQueryRunner.getMetadata().resolveOperator(session, LESS_THAN_OR_EQUAL, ImmutableList.of(DOUBLE, DOUBLE)), constant(50000.0, DOUBLE), field(0, DOUBLE));
    ExpressionCompiler expressionCompiler = new ExpressionCompiler(localQueryRunner.getFunctionManager(), new PageFunctionCompiler(localQueryRunner.getFunctionManager(), 0));
    Supplier<PageProcessor> pageProcessor = expressionCompiler.compilePageProcessor(Optional.of(filter), ImmutableList.of(field(0, DOUBLE)));
    OperatorFactory filterAndProjectOperator = FilterAndProjectOperator.createOperatorFactory(1, new PlanNodeId("test"), pageProcessor, ImmutableList.of(DOUBLE), DataSize.ofBytes(0), 0);
    return ImmutableList.of(tableScanOperator, filterAndProjectOperator);
}
Also used : PlanNodeId(io.trino.sql.planner.plan.PlanNodeId) PageFunctionCompiler(io.trino.sql.gen.PageFunctionCompiler) PageProcessor(io.trino.operator.project.PageProcessor) OperatorFactory(io.trino.operator.OperatorFactory) RowExpression(io.trino.sql.relational.RowExpression) ExpressionCompiler(io.trino.sql.gen.ExpressionCompiler)

Aggregations

RowExpression (io.trino.sql.relational.RowExpression)34 Test (org.testng.annotations.Test)14 PageProcessor (io.trino.operator.project.PageProcessor)13 Page (io.trino.spi.Page)9 PlanNodeId (io.trino.sql.planner.plan.PlanNodeId)9 CursorProcessor (io.trino.operator.project.CursorProcessor)7 CallExpression (io.trino.sql.relational.CallExpression)7 MaterializedResult (io.trino.testing.MaterializedResult)7 ImmutableList (com.google.common.collect.ImmutableList)6 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)6 Variable (io.airlift.bytecode.Variable)6 CatalogName (io.trino.connector.CatalogName)6 Split (io.trino.metadata.Split)6 ExpressionCompiler (io.trino.sql.gen.ExpressionCompiler)6 TestingSplit (io.trino.testing.TestingSplit)6 IfStatement (io.airlift.bytecode.control.IfStatement)5 Type (io.trino.spi.type.Type)5 Expression (io.trino.sql.tree.Expression)5 LabelNode (io.airlift.bytecode.instruction.LabelNode)4 FunctionManager (io.trino.metadata.FunctionManager)4