Search in sources :

Example 6 with ValuesNode

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

the class RelationPlanner method visitUnnest.

@Override
protected RelationPlan visitUnnest(Unnest node, Void context) {
    Scope scope = analysis.getScope(node);
    ImmutableList.Builder<Symbol> outputSymbolsBuilder = ImmutableList.builder();
    for (Field field : scope.getRelationType().getVisibleFields()) {
        Symbol symbol = symbolAllocator.newSymbol(field);
        outputSymbolsBuilder.add(symbol);
    }
    List<Symbol> unnestedSymbols = outputSymbolsBuilder.build();
    // If we got here, then we must be unnesting a constant, and not be in a join (where there could be column references)
    ImmutableList.Builder<Symbol> argumentSymbols = ImmutableList.builder();
    ImmutableList.Builder<Expression> values = ImmutableList.builder();
    ImmutableMap.Builder<Symbol, List<Symbol>> unnestSymbols = ImmutableMap.builder();
    Iterator<Symbol> unnestedSymbolsIterator = unnestedSymbols.iterator();
    for (Expression expression : node.getExpressions()) {
        expression = ExpressionTreeRewriter.rewriteWith(new ParameterRewriter(analysis.getParameters(), analysis), expression);
        Object constantValue = evaluateConstantExpression(expression, analysis.getCoercions(), metadata, session, analysis.getColumnReferences(), analysis.getParameters());
        Type type = analysis.getType(expression);
        values.add(LiteralInterpreter.toExpression(constantValue, type));
        Symbol inputSymbol = symbolAllocator.newSymbol(expression, type);
        argumentSymbols.add(inputSymbol);
        if (type instanceof ArrayType) {
            unnestSymbols.put(inputSymbol, ImmutableList.of(unnestedSymbolsIterator.next()));
        } else if (type instanceof MapType) {
            unnestSymbols.put(inputSymbol, ImmutableList.of(unnestedSymbolsIterator.next(), unnestedSymbolsIterator.next()));
        } else {
            throw new IllegalArgumentException("Unsupported type for UNNEST: " + type);
        }
    }
    Optional<Symbol> ordinalitySymbol = node.isWithOrdinality() ? Optional.of(unnestedSymbolsIterator.next()) : Optional.empty();
    checkState(!unnestedSymbolsIterator.hasNext(), "Not all output symbols were matched with input symbols");
    ValuesNode valuesNode = new ValuesNode(idAllocator.getNextId(), argumentSymbols.build(), ImmutableList.of(values.build()));
    UnnestNode unnestNode = new UnnestNode(idAllocator.getNextId(), valuesNode, ImmutableList.of(), unnestSymbols.build(), ordinalitySymbol);
    return new RelationPlan(unnestNode, scope, unnestedSymbols);
}
Also used : ValuesNode(com.facebook.presto.sql.planner.plan.ValuesNode) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) ImmutableMap(com.google.common.collect.ImmutableMap) MapType(com.facebook.presto.type.MapType) ArrayType(com.facebook.presto.type.ArrayType) Field(com.facebook.presto.sql.analyzer.Field) ComparisonExpressionType(com.facebook.presto.sql.tree.ComparisonExpressionType) ArrayType(com.facebook.presto.type.ArrayType) MapType(com.facebook.presto.type.MapType) Type(com.facebook.presto.spi.type.Type) RelationType(com.facebook.presto.sql.analyzer.RelationType) Scope(com.facebook.presto.sql.analyzer.Scope) ExpressionInterpreter.evaluateConstantExpression(com.facebook.presto.sql.planner.ExpressionInterpreter.evaluateConstantExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) UnnestNode(com.facebook.presto.sql.planner.plan.UnnestNode) ImmutableCollectors.toImmutableList(com.facebook.presto.util.ImmutableCollectors.toImmutableList) List(java.util.List) ArrayList(java.util.ArrayList) ImmutableList(com.google.common.collect.ImmutableList)

Example 7 with ValuesNode

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

the class LogicalPlanner method planStatement.

public PlanNode planStatement(Analysis analysis, Statement statement) {
    if (statement instanceof CreateTableAsSelect && analysis.isCreateTableAsSelectNoOp()) {
        checkState(analysis.getCreateTableDestination().isPresent(), "Table destination is missing");
        List<Expression> emptyRow = ImmutableList.of();
        PlanNode source = new ValuesNode(idAllocator.getNextId(), ImmutableList.of(), ImmutableList.of(emptyRow));
        return new OutputNode(idAllocator.getNextId(), source, ImmutableList.of(), ImmutableList.of());
    }
    return createOutputPlan(planStatementWithoutOutput(analysis, statement), analysis);
}
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) Expression(com.facebook.presto.sql.tree.Expression) CreateTableAsSelect(com.facebook.presto.sql.tree.CreateTableAsSelect)

Example 8 with ValuesNode

use of com.facebook.presto.sql.planner.plan.ValuesNode 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 9 with ValuesNode

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

the class RemoveEmptyDelete method apply.

@Override
public Optional<PlanNode> apply(PlanNode node, Lookup lookup, PlanNodeIdAllocator idAllocator, SymbolAllocator symbolAllocator) {
    if (!(node instanceof TableFinishNode)) {
        return Optional.empty();
    }
    TableFinishNode finish = (TableFinishNode) node;
    PlanNode finishSource = lookup.resolve(finish.getSource());
    if (!(finishSource instanceof ExchangeNode)) {
        return Optional.empty();
    }
    ExchangeNode exchange = (ExchangeNode) finishSource;
    if (exchange.getSources().size() != 1) {
        return Optional.empty();
    }
    PlanNode exchangeSource = lookup.resolve(getOnlyElement(exchange.getSources()));
    if (!(exchangeSource instanceof DeleteNode)) {
        return Optional.empty();
    }
    DeleteNode delete = (DeleteNode) exchangeSource;
    PlanNode deleteSource = lookup.resolve(delete.getSource());
    if (!(deleteSource instanceof ValuesNode)) {
        return Optional.empty();
    }
    ValuesNode values = (ValuesNode) deleteSource;
    if (!values.getRows().isEmpty()) {
        return Optional.empty();
    }
    return Optional.of(new ValuesNode(node.getId(), node.getOutputSymbols(), ImmutableList.of(ImmutableList.of(new LongLiteral("0")))));
}
Also used : DeleteNode(com.facebook.presto.sql.planner.plan.DeleteNode) ValuesNode(com.facebook.presto.sql.planner.plan.ValuesNode) PlanNode(com.facebook.presto.sql.planner.plan.PlanNode) ExchangeNode(com.facebook.presto.sql.planner.plan.ExchangeNode) LongLiteral(com.facebook.presto.sql.tree.LongLiteral) TableFinishNode(com.facebook.presto.sql.planner.plan.TableFinishNode)

Example 10 with ValuesNode

use of com.facebook.presto.sql.planner.plan.ValuesNode 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)

Aggregations

ValuesNode (com.facebook.presto.sql.planner.plan.ValuesNode)11 PlanNode (com.facebook.presto.sql.planner.plan.PlanNode)5 Expression (com.facebook.presto.sql.tree.Expression)5 Symbol (com.facebook.presto.sql.planner.Symbol)4 ProjectNode (com.facebook.presto.sql.planner.plan.ProjectNode)4 Scope (com.facebook.presto.sql.analyzer.Scope)3 OutputNode (com.facebook.presto.sql.planner.plan.OutputNode)3 ImmutableList (com.google.common.collect.ImmutableList)3 List (java.util.List)3 Test (org.testng.annotations.Test)3 Field (com.facebook.presto.sql.analyzer.Field)2 ExpressionInterpreter.evaluateConstantExpression (com.facebook.presto.sql.planner.ExpressionInterpreter.evaluateConstantExpression)2 PlanNodeIdAllocator (com.facebook.presto.sql.planner.PlanNodeIdAllocator)2 SymbolAllocator (com.facebook.presto.sql.planner.SymbolAllocator)2 CoalesceExpression (com.facebook.presto.sql.tree.CoalesceExpression)2 ComparisonExpression (com.facebook.presto.sql.tree.ComparisonExpression)2 LongLiteral (com.facebook.presto.sql.tree.LongLiteral)2 ImmutableCollectors.toImmutableList (com.facebook.presto.util.ImmutableCollectors.toImmutableList)2 ArrayList (java.util.ArrayList)2 Signature (com.facebook.presto.metadata.Signature)1