Search in sources :

Example 11 with SortOrder

use of com.facebook.presto.common.block.SortOrder in project presto by prestodb.

the class TestWindowOperator method testLastValuePartition.

@Test(dataProvider = "spillEnabled")
public void testLastValuePartition(boolean spillEnabled, boolean revokeMemoryWhenAddingPages, long memoryLimit) {
    List<Page> input = rowPagesBuilder(VARCHAR, VARCHAR, BIGINT, BOOLEAN, VARCHAR).row("b", "A1", 1L, true, "").row("a", "A2", 1L, false, "").row("a", "B1", 2L, true, "").pageBreak().row("b", "C1", 2L, false, "").row("a", "C2", 3L, true, "").row("c", "A3", 1L, true, "").build();
    DriverContext driverContext = createDriverContext(memoryLimit);
    WindowOperatorFactory operatorFactory = createFactoryUnbounded(ImmutableList.of(VARCHAR, VARCHAR, BIGINT, BOOLEAN, VARCHAR), Ints.asList(0, 1, 2, 3), LAST_VALUE, Ints.asList(0), Ints.asList(2), ImmutableList.copyOf(new SortOrder[] { SortOrder.ASC_NULLS_LAST }), spillEnabled);
    MaterializedResult expected = resultBuilder(driverContext.getSession(), VARCHAR, VARCHAR, BIGINT, BOOLEAN, VARCHAR).row("a", "A2", 1L, false, "C2").row("a", "B1", 2L, true, "C2").row("a", "C2", 3L, true, "C2").row("b", "A1", 1L, true, "C1").row("b", "C1", 2L, false, "C1").row("c", "A3", 1L, true, "A3").build();
    assertOperatorEquals(operatorFactory, driverContext, input, expected, revokeMemoryWhenAddingPages);
}
Also used : WindowOperatorFactory(com.facebook.presto.operator.WindowOperator.WindowOperatorFactory) SortOrder(com.facebook.presto.common.block.SortOrder) Page(com.facebook.presto.common.Page) OperatorAssertion.toMaterializedResult(com.facebook.presto.operator.OperatorAssertion.toMaterializedResult) MaterializedResult(com.facebook.presto.testing.MaterializedResult) Test(org.testng.annotations.Test)

Example 12 with SortOrder

use of com.facebook.presto.common.block.SortOrder in project presto by prestodb.

the class TestWindowOperator method testFirstValuePartition.

@Test(dataProvider = "spillEnabled")
public void testFirstValuePartition(boolean spillEnabled, boolean revokeMemoryWhenAddingPages, long memoryLimit) {
    List<Page> input = rowPagesBuilder(VARCHAR, VARCHAR, BIGINT, BOOLEAN, VARCHAR).row("b", "A1", 1L, true, "").row("a", "A2", 1L, false, "").row("a", "B1", 2L, true, "").pageBreak().row("b", "C1", 2L, false, "").row("a", "C2", 3L, true, "").row("c", "A3", 1L, true, "").build();
    WindowOperatorFactory operatorFactory = createFactoryUnbounded(ImmutableList.of(VARCHAR, VARCHAR, BIGINT, BOOLEAN, VARCHAR), Ints.asList(0, 1, 2, 3), FIRST_VALUE, Ints.asList(0), Ints.asList(2), ImmutableList.copyOf(new SortOrder[] { SortOrder.ASC_NULLS_LAST }), spillEnabled);
    DriverContext driverContext = createDriverContext(memoryLimit);
    MaterializedResult expected = resultBuilder(driverContext.getSession(), VARCHAR, VARCHAR, BIGINT, BOOLEAN, VARCHAR).row("a", "A2", 1L, false, "A2").row("a", "B1", 2L, true, "A2").row("a", "C2", 3L, true, "A2").row("b", "A1", 1L, true, "A1").row("b", "C1", 2L, false, "A1").row("c", "A3", 1L, true, "A3").build();
    assertOperatorEquals(operatorFactory, driverContext, input, expected, revokeMemoryWhenAddingPages);
}
Also used : WindowOperatorFactory(com.facebook.presto.operator.WindowOperator.WindowOperatorFactory) SortOrder(com.facebook.presto.common.block.SortOrder) Page(com.facebook.presto.common.Page) OperatorAssertion.toMaterializedResult(com.facebook.presto.operator.OperatorAssertion.toMaterializedResult) MaterializedResult(com.facebook.presto.testing.MaterializedResult) Test(org.testng.annotations.Test)

Example 13 with SortOrder

use of com.facebook.presto.common.block.SortOrder 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 14 with SortOrder

use of com.facebook.presto.common.block.SortOrder 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 15 with SortOrder

use of com.facebook.presto.common.block.SortOrder in project presto by prestodb.

the class Util method orderingSchemeMatches.

static boolean orderingSchemeMatches(List<Ordering> expectedOrderBy, OrderingScheme orderingScheme, SymbolAliases symbolAliases) {
    if (expectedOrderBy.size() != orderingScheme.getOrderByVariables().size()) {
        return false;
    }
    for (int i = 0; i < expectedOrderBy.size(); ++i) {
        Ordering ordering = expectedOrderBy.get(i);
        Symbol symbol = Symbol.from(symbolAliases.get(ordering.getField()));
        if (!symbol.equals(new Symbol(orderingScheme.getOrderByVariables().get(i).getName()))) {
            return false;
        }
        SortOrder sortOrder = getOnlyElement(Maps.filterKeys(orderingScheme.getOrderingsMap(), variable -> variable.getName().equals(symbol.getName())).values().iterator());
        if (!ordering.getSortOrder().equals(sortOrder)) {
            return false;
        }
    }
    return true;
}
Also used : SortOrder(com.facebook.presto.common.block.SortOrder) Session(com.facebook.presto.Session) Maps(com.google.common.collect.Maps) Domain(com.facebook.presto.common.predicate.Domain) TupleDomain(com.facebook.presto.common.predicate.TupleDomain) List(java.util.List) Ordering(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.Ordering) ColumnHandle(com.facebook.presto.spi.ColumnHandle) Symbol(com.facebook.presto.sql.planner.Symbol) Map(java.util.Map) Optional(java.util.Optional) TableHandle(com.facebook.presto.spi.TableHandle) OrderingScheme(com.facebook.presto.spi.plan.OrderingScheme) Metadata(com.facebook.presto.metadata.Metadata) Iterators.getOnlyElement(com.google.common.collect.Iterators.getOnlyElement) Symbol(com.facebook.presto.sql.planner.Symbol) Ordering(com.facebook.presto.sql.planner.assertions.PlanMatchPattern.Ordering) SortOrder(com.facebook.presto.common.block.SortOrder)

Aggregations

SortOrder (com.facebook.presto.common.block.SortOrder)24 Test (org.testng.annotations.Test)14 Page (com.facebook.presto.common.Page)12 WindowOperatorFactory (com.facebook.presto.operator.WindowOperator.WindowOperatorFactory)11 OperatorAssertion.toMaterializedResult (com.facebook.presto.operator.OperatorAssertion.toMaterializedResult)10 MaterializedResult (com.facebook.presto.testing.MaterializedResult)10 Type (com.facebook.presto.common.type.Type)8 OrderingScheme (com.facebook.presto.spi.plan.OrderingScheme)6 Ordering (com.facebook.presto.spi.plan.Ordering)5 VariableReferenceExpression (com.facebook.presto.spi.relation.VariableReferenceExpression)5 ImmutableList (com.google.common.collect.ImmutableList)5 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)4 NotSupportedException (com.facebook.presto.common.NotSupportedException)3 Block (com.facebook.presto.common.block.Block)3 PrestoException (com.facebook.presto.spi.PrestoException)3 HashMap (java.util.HashMap)3 List (java.util.List)3 BytecodeBlock (com.facebook.presto.bytecode.BytecodeBlock)2 MethodDefinition (com.facebook.presto.bytecode.MethodDefinition)2 Parameter (com.facebook.presto.bytecode.Parameter)2