Search in sources :

Example 51 with Assignments

use of io.trino.sql.planner.plan.Assignments in project trino by trinodb.

the class ReplaceJoinOverConstantWithProject method appendProjection.

private ProjectNode appendProjection(PlanNode source, List<Symbol> sourceOutputs, PlanNode constantSource, List<Symbol> constantOutputs, PlanNodeIdAllocator idAllocator) {
    ValuesNode values = (ValuesNode) constantSource;
    Row row = (Row) getOnlyElement(values.getRows().get());
    Map<Symbol, Expression> mapping = new HashMap<>();
    for (int i = 0; i < values.getOutputSymbols().size(); i++) {
        mapping.put(values.getOutputSymbols().get(i), row.getItems().get(i));
    }
    Assignments.Builder assignments = Assignments.builder().putIdentities(sourceOutputs);
    constantOutputs.stream().forEach(symbol -> assignments.put(symbol, mapping.get(symbol)));
    return new ProjectNode(idAllocator.getNextId(), source, assignments.build());
}
Also used : ValuesNode(io.trino.sql.planner.plan.ValuesNode) Expression(io.trino.sql.tree.Expression) HashMap(java.util.HashMap) Symbol(io.trino.sql.planner.Symbol) Assignments(io.trino.sql.planner.plan.Assignments) ProjectNode(io.trino.sql.planner.plan.ProjectNode) Row(io.trino.sql.tree.Row)

Example 52 with Assignments

use of io.trino.sql.planner.plan.Assignments in project trino by trinodb.

the class ReplaceRedundantJoinWithProject method appendNulls.

private static ProjectNode appendNulls(PlanNode source, List<Symbol> sourceOutputs, List<Symbol> nullSymbols, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) {
    Assignments.Builder assignments = Assignments.builder().putIdentities(sourceOutputs);
    nullSymbols.stream().forEach(symbol -> assignments.put(symbol, new Cast(new NullLiteral(), toSqlType(symbolAllocator.getTypes().get(symbol)))));
    return new ProjectNode(idAllocator.getNextId(), source, assignments.build());
}
Also used : Cast(io.trino.sql.tree.Cast) Assignments(io.trino.sql.planner.plan.Assignments) ProjectNode(io.trino.sql.planner.plan.ProjectNode) NullLiteral(io.trino.sql.tree.NullLiteral)

Example 53 with Assignments

use of io.trino.sql.planner.plan.Assignments in project trino by trinodb.

the class RemoveEmptyUnionBranches method apply.

@Override
public Result apply(UnionNode node, Captures captures, Context context) {
    long emptyBranches = node.getSources().stream().filter(source -> isEmpty(source, context.getLookup())).count();
    if (emptyBranches == 0) {
        return Result.empty();
    }
    if (emptyBranches == node.getSources().size()) {
        return Result.ofPlanNode(new ValuesNode(node.getId(), node.getOutputSymbols(), ImmutableList.of()));
    }
    ImmutableList.Builder<PlanNode> newSourcesBuilder = ImmutableList.builder();
    ImmutableListMultimap.Builder<Symbol, Symbol> outputsToInputsBuilder = ImmutableListMultimap.builder();
    for (int i = 0; i < node.getSources().size(); i++) {
        PlanNode source = node.getSources().get(i);
        if (!isEmpty(source, context.getLookup())) {
            newSourcesBuilder.add(source);
            for (Symbol column : node.getOutputSymbols()) {
                outputsToInputsBuilder.put(column, node.getSymbolMapping().get(column).get(i));
            }
        }
    }
    List<PlanNode> newSources = newSourcesBuilder.build();
    ListMultimap<Symbol, Symbol> outputsToInputs = outputsToInputsBuilder.build();
    if (newSources.size() == 1) {
        Assignments.Builder assignments = Assignments.builder();
        outputsToInputs.entries().stream().forEach(entry -> assignments.put(entry.getKey(), entry.getValue().toSymbolReference()));
        return Result.ofPlanNode(new ProjectNode(node.getId(), newSources.get(0), assignments.build()));
    }
    return Result.ofPlanNode(new UnionNode(node.getId(), newSources, outputsToInputs, node.getOutputSymbols()));
}
Also used : Symbol(io.trino.sql.planner.Symbol) UnionNode(io.trino.sql.planner.plan.UnionNode) ListMultimap(com.google.common.collect.ListMultimap) Assignments(io.trino.sql.planner.plan.Assignments) QueryCardinalityUtil.isEmpty(io.trino.sql.planner.optimizations.QueryCardinalityUtil.isEmpty) PlanNode(io.trino.sql.planner.plan.PlanNode) List(java.util.List) Pattern(io.trino.matching.Pattern) ImmutableList(com.google.common.collect.ImmutableList) Captures(io.trino.matching.Captures) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) Rule(io.trino.sql.planner.iterative.Rule) Patterns.union(io.trino.sql.planner.plan.Patterns.union) ValuesNode(io.trino.sql.planner.plan.ValuesNode) ProjectNode(io.trino.sql.planner.plan.ProjectNode) ValuesNode(io.trino.sql.planner.plan.ValuesNode) ImmutableList(com.google.common.collect.ImmutableList) Symbol(io.trino.sql.planner.Symbol) Assignments(io.trino.sql.planner.plan.Assignments) PlanNode(io.trino.sql.planner.plan.PlanNode) UnionNode(io.trino.sql.planner.plan.UnionNode) ImmutableListMultimap(com.google.common.collect.ImmutableListMultimap) ProjectNode(io.trino.sql.planner.plan.ProjectNode)

Example 54 with Assignments

use of io.trino.sql.planner.plan.Assignments in project trino by trinodb.

the class RemoveRedundantExists method apply.

@Override
public Result apply(ApplyNode node, Captures captures, Context context) {
    Assignments.Builder assignments = Assignments.builder();
    assignments.putIdentities(node.getInput().getOutputSymbols());
    Expression result;
    if (QueryCardinalityUtil.isEmpty(node.getSubquery(), context.getLookup())) {
        result = FALSE_LITERAL;
    } else if (QueryCardinalityUtil.isAtLeastScalar(node.getSubquery(), context.getLookup())) {
        result = TRUE_LITERAL;
    } else {
        return Result.empty();
    }
    for (Symbol output : node.getSubqueryAssignments().getOutputs()) {
        assignments.put(output, result);
    }
    return Result.ofPlanNode(new ProjectNode(context.getIdAllocator().getNextId(), node.getInput(), assignments.build()));
}
Also used : Expression(io.trino.sql.tree.Expression) Symbol(io.trino.sql.planner.Symbol) Assignments(io.trino.sql.planner.plan.Assignments) ProjectNode(io.trino.sql.planner.plan.ProjectNode)

Example 55 with Assignments

use of io.trino.sql.planner.plan.Assignments in project trino by trinodb.

the class TestTypeValidator method testInvalidProject.

@Test
public void testInvalidProject() {
    Expression expression1 = new Cast(columnB.toSymbolReference(), toSqlType(INTEGER));
    Expression expression2 = new Cast(columnA.toSymbolReference(), toSqlType(INTEGER));
    Assignments assignments = Assignments.builder().put(symbolAllocator.newSymbol(expression1, BIGINT), // should be INTEGER
    expression1).put(symbolAllocator.newSymbol(expression1, INTEGER), expression2).build();
    PlanNode node = new ProjectNode(newId(), baseTableScan, assignments);
    assertThatThrownBy(() -> assertTypesValid(node)).isInstanceOf(IllegalArgumentException.class).hasMessageMatching("type of symbol 'expr(_[0-9]+)?' is expected to be bigint, but the actual type is integer");
}
Also used : Cast(io.trino.sql.tree.Cast) PlanNode(io.trino.sql.planner.plan.PlanNode) Expression(io.trino.sql.tree.Expression) Assignments(io.trino.sql.planner.plan.Assignments) ProjectNode(io.trino.sql.planner.plan.ProjectNode) Test(org.testng.annotations.Test)

Aggregations

Assignments (io.trino.sql.planner.plan.Assignments)61 ProjectNode (io.trino.sql.planner.plan.ProjectNode)50 Symbol (io.trino.sql.planner.Symbol)39 Expression (io.trino.sql.tree.Expression)39 ImmutableList (com.google.common.collect.ImmutableList)36 Map (java.util.Map)33 PlanNode (io.trino.sql.planner.plan.PlanNode)26 SymbolReference (io.trino.sql.tree.SymbolReference)26 Captures (io.trino.matching.Captures)23 Pattern (io.trino.matching.Pattern)23 Rule (io.trino.sql.planner.iterative.Rule)23 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)21 Objects.requireNonNull (java.util.Objects.requireNonNull)21 Set (java.util.Set)21 Capture (io.trino.matching.Capture)20 Capture.newCapture (io.trino.matching.Capture.newCapture)20 TypeAnalyzer (io.trino.sql.planner.TypeAnalyzer)19 Patterns.source (io.trino.sql.planner.plan.Patterns.source)19 SubscriptExpression (io.trino.sql.tree.SubscriptExpression)18 ImmutableMap (com.google.common.collect.ImmutableMap)17