Search in sources :

Example 6 with Ordering

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

the class TestPruneWindowColumns method buildProjectedWindow.

private static PlanNode buildProjectedWindow(PlanBuilder p, Predicate<VariableReferenceExpression> projectionFilter, Predicate<VariableReferenceExpression> sourceFilter) {
    VariableReferenceExpression orderKey = p.variable("orderKey");
    VariableReferenceExpression partitionKey = p.variable("partitionKey");
    VariableReferenceExpression hash = p.variable("hash");
    VariableReferenceExpression startValue1 = p.variable("startValue1");
    VariableReferenceExpression startValue2 = p.variable("startValue2");
    VariableReferenceExpression endValue1 = p.variable("endValue1");
    VariableReferenceExpression endValue2 = p.variable("endValue2");
    VariableReferenceExpression input1 = p.variable("input1");
    VariableReferenceExpression input2 = p.variable("input2");
    VariableReferenceExpression unused = p.variable("unused");
    VariableReferenceExpression output1 = p.variable("output1");
    VariableReferenceExpression output2 = p.variable("output2");
    List<VariableReferenceExpression> inputs = ImmutableList.of(orderKey, partitionKey, hash, startValue1, startValue2, endValue1, endValue2, input1, input2, unused);
    List<VariableReferenceExpression> outputs = ImmutableList.<VariableReferenceExpression>builder().addAll(inputs).add(output1, output2).build();
    List<VariableReferenceExpression> filteredInputs = inputs.stream().filter(sourceFilter).collect(toImmutableList());
    return p.project(identityAssignmentsAsSymbolReferences(outputs.stream().filter(projectionFilter).collect(toImmutableList())), p.window(new WindowNode.Specification(ImmutableList.of(partitionKey), Optional.of(new OrderingScheme(ImmutableList.of(new Ordering(orderKey, SortOrder.ASC_NULLS_FIRST))))), ImmutableMap.of(output1, new WindowNode.Function(call(FUNCTION_NAME, FUNCTION_HANDLE, BIGINT, input1), new WindowNode.Frame(RANGE, UNBOUNDED_PRECEDING, Optional.of(startValue1), CURRENT_ROW, Optional.of(endValue1), Optional.of(new SymbolReference(startValue1.getName())).map(Expression::toString), Optional.of(new SymbolReference(endValue2.getName())).map(Expression::toString)), false), output2, new WindowNode.Function(call(FUNCTION_NAME, FUNCTION_HANDLE, BIGINT, input2), new WindowNode.Frame(RANGE, UNBOUNDED_PRECEDING, Optional.of(startValue2), CURRENT_ROW, Optional.of(endValue2), Optional.of(new SymbolReference(startValue2.getName())).map(Expression::toString), Optional.of(new SymbolReference(endValue2.getName())).map(Expression::toString)), false)), hash, p.values(filteredInputs, ImmutableList.of())));
}
Also used : OrderingScheme(com.facebook.presto.spi.plan.OrderingScheme) WindowNode(com.facebook.presto.sql.planner.plan.WindowNode) PlanMatchPattern.windowFrame(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.windowFrame) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) Ordering(com.facebook.presto.spi.plan.Ordering)

Example 7 with Ordering

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

the class TestWindowNode method testSerializationRoundtrip.

@Test
public void testSerializationRoundtrip() {
    VariableReferenceExpression windowVariable = variableAllocator.newVariable("sum", BIGINT);
    FunctionHandle functionHandle = createTestMetadataManager().getFunctionAndTypeManager().lookupFunction("sum", fromTypes(BIGINT));
    WindowNode.Frame frame = new WindowNode.Frame(RANGE, UNBOUNDED_PRECEDING, Optional.empty(), UNBOUNDED_FOLLOWING, Optional.empty(), Optional.empty(), Optional.empty());
    PlanNodeId id = newId();
    WindowNode.Specification specification = new WindowNode.Specification(ImmutableList.of(columnA), Optional.of(new OrderingScheme(ImmutableList.of(new Ordering(columnB, SortOrder.ASC_NULLS_FIRST)))));
    CallExpression call = call("sum", functionHandle, BIGINT, new VariableReferenceExpression(Optional.empty(), columnC.getName(), BIGINT));
    Map<VariableReferenceExpression, WindowNode.Function> functions = ImmutableMap.of(windowVariable, new WindowNode.Function(call, frame, false));
    Optional<VariableReferenceExpression> hashVariable = Optional.of(columnB);
    Set<VariableReferenceExpression> prePartitionedInputs = ImmutableSet.of(columnA);
    WindowNode windowNode = new WindowNode(Optional.empty(), id, sourceNode, specification, functions, hashVariable, prePartitionedInputs, 0);
    String json = codec.toJson(windowNode);
    WindowNode actualNode = codec.fromJson(json);
    assertEquals(actualNode.getId(), windowNode.getId());
    assertEquals(actualNode.getSpecification(), windowNode.getSpecification());
    assertEquals(actualNode.getWindowFunctions(), windowNode.getWindowFunctions());
    assertEquals(actualNode.getFrames(), windowNode.getFrames());
    assertEquals(actualNode.getHashVariable(), windowNode.getHashVariable());
    assertEquals(actualNode.getPrePartitionedInputs(), windowNode.getPrePartitionedInputs());
    assertEquals(actualNode.getPreSortedOrderPrefix(), windowNode.getPreSortedOrderPrefix());
}
Also used : OrderingScheme(com.facebook.presto.spi.plan.OrderingScheme) PlanNodeId(com.facebook.presto.spi.plan.PlanNodeId) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Ordering(com.facebook.presto.spi.plan.Ordering) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle) CallExpression(com.facebook.presto.spi.relation.CallExpression) Test(org.testng.annotations.Test)

Example 8 with Ordering

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

the class SymbolMapper method map.

public TopNNode map(TopNNode node, PlanNode source, PlanNodeId newNodeId) {
    ImmutableList.Builder<VariableReferenceExpression> variables = ImmutableList.builder();
    ImmutableMap.Builder<VariableReferenceExpression, SortOrder> orderings = ImmutableMap.builder();
    Set<VariableReferenceExpression> seenCanonicals = new HashSet<>(node.getOrderingScheme().getOrderByVariables().size());
    for (VariableReferenceExpression variable : node.getOrderingScheme().getOrderByVariables()) {
        VariableReferenceExpression canonical = map(variable);
        if (seenCanonicals.add(canonical)) {
            seenCanonicals.add(canonical);
            variables.add(canonical);
            orderings.put(canonical, node.getOrderingScheme().getOrdering(variable));
        }
    }
    ImmutableMap<VariableReferenceExpression, SortOrder> orderingMap = orderings.build();
    return new TopNNode(node.getSourceLocation(), newNodeId, source, node.getCount(), new OrderingScheme(variables.build().stream().map(variable -> new Ordering(variable, orderingMap.get(variable))).collect(toImmutableList())), node.getStep());
}
Also used : ExpressionTreeRewriter(com.facebook.presto.sql.tree.ExpressionTreeRewriter) WarningCollector(com.facebook.presto.spi.WarningCollector) PlanNodeId(com.facebook.presto.spi.plan.PlanNodeId) AggregationNode(com.facebook.presto.spi.plan.AggregationNode) RowExpressionRewriter(com.facebook.presto.expressions.RowExpressionRewriter) OriginalExpressionUtils.isExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.isExpression) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) HashMap(java.util.HashMap) TableWriterNode(com.facebook.presto.sql.planner.plan.TableWriterNode) StatisticAggregationsDescriptor(com.facebook.presto.sql.planner.plan.StatisticAggregationsDescriptor) TableWriterMergeNode(com.facebook.presto.sql.planner.plan.TableWriterMergeNode) HashSet(java.util.HashSet) OriginalExpressionUtils.castToExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToExpression) ImmutableList(com.google.common.collect.ImmutableList) Symbol(com.facebook.presto.sql.planner.Symbol) TypeProvider(com.facebook.presto.sql.planner.TypeProvider) StatisticAggregations(com.facebook.presto.sql.planner.plan.StatisticAggregations) Map(java.util.Map) Objects.requireNonNull(java.util.Objects.requireNonNull) PrestoWarning(com.facebook.presto.spi.PrestoWarning) PartitioningScheme(com.facebook.presto.sql.planner.PartitioningScheme) CallExpression(com.facebook.presto.spi.relation.CallExpression) OrderingScheme(com.facebook.presto.spi.plan.OrderingScheme) RowExpression(com.facebook.presto.spi.relation.RowExpression) SymbolReference(com.facebook.presto.sql.tree.SymbolReference) RowExpressionTreeRewriter(com.facebook.presto.expressions.RowExpressionTreeRewriter) ImmutableSet(com.google.common.collect.ImmutableSet) PlanNodeIdAllocator(com.facebook.presto.spi.plan.PlanNodeIdAllocator) SortOrder(com.facebook.presto.common.block.SortOrder) ImmutableMap(com.google.common.collect.ImmutableMap) Ordering(com.facebook.presto.spi.plan.Ordering) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) Set(java.util.Set) ExpressionTreeUtils.getNodeLocation(com.facebook.presto.sql.analyzer.ExpressionTreeUtils.getNodeLocation) MULTIPLE_ORDER_BY(com.facebook.presto.spi.StandardWarningCode.MULTIPLE_ORDER_BY) ExpressionRewriter(com.facebook.presto.sql.tree.ExpressionRewriter) PlanNode(com.facebook.presto.spi.plan.PlanNode) List(java.util.List) AggregationNode.groupingSets(com.facebook.presto.spi.plan.AggregationNode.groupingSets) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) Expression(com.facebook.presto.sql.tree.Expression) TopNNode(com.facebook.presto.spi.plan.TopNNode) Aggregation(com.facebook.presto.spi.plan.AggregationNode.Aggregation) Entry(java.util.Map.Entry) TableFinishNode(com.facebook.presto.sql.planner.plan.TableFinishNode) StatisticsWriterNode(com.facebook.presto.sql.planner.plan.StatisticsWriterNode) OriginalExpressionUtils.castToRowExpression(com.facebook.presto.sql.relational.OriginalExpressionUtils.castToRowExpression) OrderingScheme(com.facebook.presto.spi.plan.OrderingScheme) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) SortOrder(com.facebook.presto.common.block.SortOrder) ImmutableMap(com.google.common.collect.ImmutableMap) ImmutableMap.toImmutableMap(com.google.common.collect.ImmutableMap.toImmutableMap) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Ordering(com.facebook.presto.spi.plan.Ordering) TopNNode(com.facebook.presto.spi.plan.TopNNode) HashSet(java.util.HashSet)

Example 9 with Ordering

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

the class SymbolMapper method map.

public OrderingScheme map(OrderingScheme orderingScheme) {
    // SymbolMapper inlines symbol with multiple level reference (SymbolInliner only inline single level).
    ImmutableList.Builder<VariableReferenceExpression> orderBy = ImmutableList.builder();
    HashMap<VariableReferenceExpression, SortOrder> orderingMap = new HashMap<>();
    for (VariableReferenceExpression variable : orderingScheme.getOrderByVariables()) {
        VariableReferenceExpression translated = map(variable);
        // Some variables may become duplicates after canonicalization, so we put them only once.
        if (!orderingMap.containsKey(translated)) {
            orderBy.add(translated);
            orderingMap.put(translated, orderingScheme.getOrdering(variable));
        } else if (orderingMap.get(translated) != orderingScheme.getOrdering(variable)) {
            warningCollector.add(new PrestoWarning(MULTIPLE_ORDER_BY, "Multiple ORDER BY for a variable were given, only first provided will be considered"));
        }
    }
    return new OrderingScheme(orderBy.build().stream().map(variable -> new Ordering(variable, orderingMap.get(variable))).collect(toImmutableList()));
}
Also used : OrderingScheme(com.facebook.presto.spi.plan.OrderingScheme) HashMap(java.util.HashMap) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) PrestoWarning(com.facebook.presto.spi.PrestoWarning) Ordering(com.facebook.presto.spi.plan.Ordering) SortOrder(com.facebook.presto.common.block.SortOrder)

Example 10 with Ordering

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

the class TestEffectivePredicateExtractor method testTopN.

@Test
public void testTopN() {
    PlanNode node = new TopNNode(Optional.empty(), newId(), filter(baseTableScan, and(equals(AV, BV), equals(BV, CV), lessThan(CV, bigintLiteral(10)))), 1, new OrderingScheme(ImmutableList.of(new Ordering(AV, SortOrder.ASC_NULLS_FIRST))), TopNNode.Step.PARTIAL);
    RowExpression effectivePredicate = effectivePredicateExtractor.extract(node);
    // Pass through
    assertEquals(normalizeConjuncts(effectivePredicate), normalizeConjuncts(equals(AV, BV), equals(BV, CV), lessThan(CV, bigintLiteral(10))));
}
Also used : OrderingScheme(com.facebook.presto.spi.plan.OrderingScheme) PlanNode(com.facebook.presto.spi.plan.PlanNode) Ordering(com.facebook.presto.spi.plan.Ordering) RowExpression(com.facebook.presto.spi.relation.RowExpression) TopNNode(com.facebook.presto.spi.plan.TopNNode) Test(org.testng.annotations.Test)

Aggregations

Ordering (com.facebook.presto.spi.plan.Ordering)15 OrderingScheme (com.facebook.presto.spi.plan.OrderingScheme)15 Test (org.testng.annotations.Test)9 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)8 SortOrder (com.facebook.presto.common.block.SortOrder)6 ImmutableList (com.google.common.collect.ImmutableList)6 AggregationNode (com.facebook.presto.spi.plan.AggregationNode)5 TopNNode (com.facebook.presto.spi.plan.TopNNode)5 RowExpression (com.facebook.presto.spi.relation.RowExpression)5 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)5 PlanNode (com.facebook.presto.spi.plan.PlanNode)4 CallExpression (com.facebook.presto.spi.relation.CallExpression)4 WindowNode (com.facebook.presto.sql.planner.plan.WindowNode)4 PlanNodeId (com.facebook.presto.spi.plan.PlanNodeId)3 TableScanNode (com.facebook.presto.spi.plan.TableScanNode)3 SymbolReference (com.facebook.presto.sql.tree.SymbolReference)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 PrestoWarning (com.facebook.presto.spi.PrestoWarning)2 FunctionHandle (com.facebook.presto.spi.function.FunctionHandle)2 PlanBuilder (com.facebook.presto.sql.planner.iterative.rule.test.PlanBuilder)2