Search in sources :

Example 16 with ValuesNode

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

the class TestRuleIndex method testWithPlanNodeHierarchy.

@Test
public void testWithPlanNodeHierarchy() {
    Rule projectRule1 = new NoOpRule(Pattern.typeOf(ProjectNode.class));
    Rule projectRule2 = new NoOpRule(Pattern.typeOf(ProjectNode.class));
    Rule filterRule = new NoOpRule(Pattern.typeOf(FilterNode.class));
    Rule anyRule = new NoOpRule(Pattern.any());
    RuleIndex ruleIndex = RuleIndex.builder().register(projectRule1).register(projectRule2).register(filterRule).register(anyRule).build();
    ProjectNode projectNode = planBuilder.project(Assignments.of(), planBuilder.values());
    FilterNode filterNode = planBuilder.filter(BooleanLiteral.TRUE_LITERAL, planBuilder.values());
    ValuesNode valuesNode = planBuilder.values();
    assertEquals(ruleIndex.getCandidates(projectNode).collect(toSet()), ImmutableSet.of(projectRule1, projectRule2, anyRule));
    assertEquals(ruleIndex.getCandidates(filterNode).collect(toSet()), ImmutableSet.of(filterRule, anyRule));
    assertEquals(ruleIndex.getCandidates(valuesNode).collect(toSet()), ImmutableSet.of(anyRule));
}
Also used : ValuesNode(com.facebook.presto.spi.plan.ValuesNode) FilterNode(com.facebook.presto.spi.plan.FilterNode) ProjectNode(com.facebook.presto.spi.plan.ProjectNode) Test(org.testng.annotations.Test)

Example 17 with ValuesNode

use of com.facebook.presto.spi.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<VariableReferenceExpression> outputVariablesBuilder = ImmutableList.builder();
    for (Field field : scope.getRelationType().getVisibleFields()) {
        VariableReferenceExpression variable = variableAllocator.newVariable(field);
        outputVariablesBuilder.add(variable);
    }
    List<VariableReferenceExpression> unnestedVariables = outputVariablesBuilder.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<VariableReferenceExpression> argumentVariables = ImmutableList.builder();
    ImmutableList.Builder<RowExpression> values = ImmutableList.builder();
    ImmutableMap.Builder<VariableReferenceExpression, List<VariableReferenceExpression>> unnestVariables = ImmutableMap.builder();
    Iterator<VariableReferenceExpression> unnestedVariablesIterator = unnestedVariables.iterator();
    for (Expression expression : node.getExpressions()) {
        Type type = analysis.getType(expression);
        Expression rewritten = Coercer.addCoercions(expression, analysis);
        rewritten = ExpressionTreeRewriter.rewriteWith(new ParameterRewriter(analysis.getParameters(), analysis), rewritten);
        values.add(castToRowExpression(rewritten));
        VariableReferenceExpression input = variableAllocator.newVariable(rewritten, type);
        argumentVariables.add(new VariableReferenceExpression(getSourceLocation(rewritten), input.getName(), type));
        if (type instanceof ArrayType) {
            Type elementType = ((ArrayType) type).getElementType();
            if (!SystemSessionProperties.isLegacyUnnest(session) && elementType instanceof RowType) {
                ImmutableList.Builder<VariableReferenceExpression> unnestVariableBuilder = ImmutableList.builder();
                for (int i = 0; i < ((RowType) elementType).getFields().size(); i++) {
                    unnestVariableBuilder.add(unnestedVariablesIterator.next());
                }
                unnestVariables.put(input, unnestVariableBuilder.build());
            } else {
                unnestVariables.put(input, ImmutableList.of(unnestedVariablesIterator.next()));
            }
        } else if (type instanceof MapType) {
            unnestVariables.put(input, ImmutableList.of(unnestedVariablesIterator.next(), unnestedVariablesIterator.next()));
        } else {
            throw new IllegalArgumentException("Unsupported type for UNNEST: " + type);
        }
    }
    Optional<VariableReferenceExpression> ordinalityVariable = node.isWithOrdinality() ? Optional.of(unnestedVariablesIterator.next()) : Optional.empty();
    checkState(!unnestedVariablesIterator.hasNext(), "Not all output variables were matched with input variables");
    ValuesNode valuesNode = new ValuesNode(getSourceLocation(node), idAllocator.getNextId(), argumentVariables.build(), ImmutableList.of(values.build()));
    UnnestNode unnestNode = new UnnestNode(getSourceLocation(node), idAllocator.getNextId(), valuesNode, ImmutableList.of(), unnestVariables.build(), ordinalityVariable);
    return new RelationPlan(unnestNode, scope, unnestedVariables);
}
Also used : ValuesNode(com.facebook.presto.spi.plan.ValuesNode) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) RowType(com.facebook.presto.common.type.RowType) ImmutableMap(com.google.common.collect.ImmutableMap) MapType(com.facebook.presto.common.type.MapType) ArrayType(com.facebook.presto.common.type.ArrayType) Field(com.facebook.presto.sql.analyzer.Field) TypeUtils.isEnumType(com.facebook.presto.common.type.TypeUtils.isEnumType) ArrayType(com.facebook.presto.common.type.ArrayType) RowType(com.facebook.presto.common.type.RowType) MapType(com.facebook.presto.common.type.MapType) Type(com.facebook.presto.common.type.Type) RelationType(com.facebook.presto.sql.analyzer.RelationType) Scope(com.facebook.presto.sql.analyzer.Scope) CoalesceExpression(com.facebook.presto.sql.tree.CoalesceExpression) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) DereferenceExpression(com.facebook.presto.sql.tree.DereferenceExpression) ComparisonExpression(com.facebook.presto.sql.tree.ComparisonExpression) ExpressionTreeUtils.isEqualComparisonExpression(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.isEqualComparisonExpression) Expression(com.facebook.presto.sql.tree.Expression) UnnestNode(com.facebook.presto.sql.planner.plan.UnnestNode) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList)

Example 18 with ValuesNode

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

the class TestVerifyOnlyOneOutputNode method testValidateSuccessful.

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

Aggregations

ValuesNode (com.facebook.presto.spi.plan.ValuesNode)18 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)13 Test (org.testng.annotations.Test)8 PlanNode (com.facebook.presto.spi.plan.PlanNode)7 RowExpression (com.facebook.presto.spi.relation.RowExpression)6 JoinNode (com.facebook.presto.sql.planner.plan.JoinNode)6 ProjectNode (com.facebook.presto.spi.plan.ProjectNode)5 MultiJoinNode (com.facebook.presto.sql.planner.iterative.rule.ReorderJoins.MultiJoinNode)5 MultiJoinNode.toMultiJoinNode (com.facebook.presto.sql.planner.iterative.rule.ReorderJoins.MultiJoinNode.toMultiJoinNode)5 PlanBuilder (com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder)5 ImmutableList (com.google.common.collect.ImmutableList)5 List (java.util.List)5 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)4 Map (java.util.Map)4 Session (com.facebook.presto.Session)3 OutputNode (com.facebook.presto.sql.planner.plan.OutputNode)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 Capture (com.facebook.presto.matching.Capture)2 Capture.newCapture (com.facebook.presto.matching.Capture.newCapture)2 Captures (com.facebook.presto.matching.Captures)2