Search in sources :

Example 16 with ProjectNode

use of com.facebook.presto.sql.planner.plan.ProjectNode in project presto by prestodb.

the class PruneValuesColumns method apply.

@Override
public Optional<PlanNode> apply(PlanNode node, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) {
    if (!(node instanceof ProjectNode)) {
        return Optional.empty();
    }
    ProjectNode parent = (ProjectNode) node;
    PlanNode child = lookup.resolve(parent.getSource());
    if (!(child instanceof ValuesNode)) {
        return Optional.empty();
    }
    ValuesNode values = (ValuesNode) child;
    Optional<List<Symbol>> dependencies = pruneInputs(child.getOutputSymbols(), parent.getAssignments().getExpressions());
    if (!dependencies.isPresent()) {
        return Optional.empty();
    }
    List<Symbol> newOutputs = dependencies.get();
    // for each output of project, the corresponding column in the values node
    int[] mapping = new int[newOutputs.size()];
    for (int i = 0; i < mapping.length; i++) {
        mapping[i] = values.getOutputSymbols().indexOf(newOutputs.get(i));
    }
    ImmutableList.Builder<List<Expression>> rowsBuilder = ImmutableList.builder();
    for (List<Expression> row : values.getRows()) {
        rowsBuilder.add(Arrays.stream(mapping).mapToObj(row::get).collect(Collectors.toList()));
    }
    return Optional.of(new ProjectNode(parent.getId(), new ValuesNode(values.getId(), newOutputs, rowsBuilder.build()), parent.getAssignments()));
}
Also used : ValuesNode(com.facebook.presto.sql.planner.plan.ValuesNode) Symbol(com.facebook.presto.sql.planner.Symbol) ImmutableList(com.google.common.collect.ImmutableList) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) Expression(com.facebook.presto.sql.tree.Expression) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 17 with ProjectNode

use of com.facebook.presto.sql.planner.plan.ProjectNode in project presto by prestodb.

the class TestVerifyOnlyOneOutputNode method testValidateSuccessful.

@Test
public void testValidateSuccessful() throws Exception {
    // random seemingly valid plan
    PlanNode root = new OutputNode(idAllocator.getNextId(), new ProjectNode(idAllocator.getNextId(), new ValuesNode(idAllocator.getNextId(), ImmutableList.of(), ImmutableList.of()), Assignments.of()), ImmutableList.of(), ImmutableList.of());
    new VerifyOnlyOneOutputNode().validate(root, null, null, null, null);
}
Also used : ValuesNode(com.facebook.presto.sql.planner.plan.ValuesNode) OutputNode(com.facebook.presto.sql.planner.plan.OutputNode) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode) Test(org.testng.annotations.Test)

Example 18 with ProjectNode

use of com.facebook.presto.sql.planner.plan.ProjectNode in project presto by prestodb.

the class SimplifyCountOverConstant method apply.

@Override
public Optional<PlanNode> apply(PlanNode node, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) {
    if (!(node instanceof AggregationNode)) {
        return Optional.empty();
    }
    AggregationNode parent = (AggregationNode) node;
    PlanNode input = lookup.resolve(parent.getSource());
    if (!(input instanceof ProjectNode)) {
        return Optional.empty();
    }
    ProjectNode child = (ProjectNode) input;
    boolean changed = false;
    Map<Symbol, AggregationNode.Aggregation> assignments = new LinkedHashMap<>(parent.getAssignments());
    for (Entry<Symbol, AggregationNode.Aggregation> entry : parent.getAssignments().entrySet()) {
        Symbol symbol = entry.getKey();
        AggregationNode.Aggregation aggregation = entry.getValue();
        if (isCountOverConstant(aggregation, child.getAssignments())) {
            changed = true;
            assignments.put(symbol, new AggregationNode.Aggregation(new FunctionCall(QualifiedName.of("count"), ImmutableList.of()), new Signature("count", AGGREGATE, parseTypeSignature(StandardTypes.BIGINT))));
        }
    }
    if (!changed) {
        return Optional.empty();
    }
    return Optional.of(new AggregationNode(node.getId(), child, assignments, parent.getGroupingSets(), parent.getStep(), parent.getHashSymbol(), parent.getGroupIdSymbol()));
}
Also used : PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) Symbol(com.facebook.presto.sql.planner.Symbol) Signature(com.facebook.presto.metadata.Signature) TypeSignature.parseTypeSignature(com.facebook.presto.spi.type.TypeSignature.parseTypeSignature) AggregationNode(com.facebook.presto.sql.planner.plan.AggregationNode) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode) FunctionCall(com.facebook.presto.sql.tree.FunctionCall) LinkedHashMap(java.util.LinkedHashMap)

Example 19 with ProjectNode

use of com.facebook.presto.sql.planner.plan.ProjectNode in project presto by prestodb.

the class TestTypeValidator method testInvalidProject.

@Test(expectedExceptions = IllegalArgumentException.class, expectedExceptionsMessageRegExp = "type of symbol 'expr(_[0-9]+)?' is expected to be bigint, but the actual type is integer")
public void testInvalidProject() throws Exception {
    Expression expression1 = new Cast(columnB.toSymbolReference(), StandardTypes.INTEGER);
    Expression expression2 = new Cast(columnA.toSymbolReference(), StandardTypes.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);
    assertTypesValid(node);
}
Also used : Cast(com.facebook.presto.sql.tree.Cast) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) Expression(com.facebook.presto.sql.tree.Expression) Assignments(com.facebook.presto.sql.planner.plan.Assignments) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode) Test(org.testng.annotations.Test)

Example 20 with ProjectNode

use of com.facebook.presto.sql.planner.plan.ProjectNode in project presto by prestodb.

the class TestTypeValidator method testValidProject.

@Test
public void testValidProject() throws Exception {
    Expression expression1 = new Cast(columnB.toSymbolReference(), StandardTypes.BIGINT);
    Expression expression2 = new Cast(columnC.toSymbolReference(), StandardTypes.BIGINT);
    Assignments assignments = Assignments.builder().put(symbolAllocator.newSymbol(expression1, BIGINT), expression1).put(symbolAllocator.newSymbol(expression2, BIGINT), expression2).build();
    PlanNode node = new ProjectNode(newId(), baseTableScan, assignments);
    assertTypesValid(node);
}
Also used : Cast(com.facebook.presto.sql.tree.Cast) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) Expression(com.facebook.presto.sql.tree.Expression) Assignments(com.facebook.presto.sql.planner.plan.Assignments) ProjectNode(com.facebook.presto.sql.planner.plan.ProjectNode) Test(org.testng.annotations.Test)

Aggregations

ProjectNode (com.facebook.presto.sql.planner.plan.ProjectNode)21 Expression (com.facebook.presto.sql.tree.Expression)14 Assignments (com.facebook.presto.sql.planner.plan.Assignments)11 PlanNode (com.facebook.presto.sql.planner.plan.PlanNode)11 Symbol (com.facebook.presto.sql.planner.Symbol)7 Cast (com.facebook.presto.sql.tree.Cast)7 Test (org.testng.annotations.Test)7 AggregationNode (com.facebook.presto.sql.planner.plan.AggregationNode)5 ValuesNode (com.facebook.presto.sql.planner.plan.ValuesNode)5 ImmutableList (com.google.common.collect.ImmutableList)5 List (java.util.List)5 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)4 FunctionCall (com.facebook.presto.sql.tree.FunctionCall)4 ImmutableMap (com.google.common.collect.ImmutableMap)4 Map (java.util.Map)4 Signature (com.facebook.presto.metadata.Signature)3 Type (com.facebook.presto.spi.type.Type)3 Field (com.facebook.presto.sql.analyzer.Field)3 RelationType (com.facebook.presto.sql.analyzer.RelationType)3 LimitNode (com.facebook.presto.sql.planner.plan.LimitNode)3