Search in sources :

Example 6 with SortOrder

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

the class RowComparator method compare.

@Override
public int compare(Page leftRow, Page rightRow) {
    for (int index = 0; index < sortChannels.size(); index++) {
        Type type = sortTypes.get(index);
        int channel = sortChannels.get(index);
        SortOrder sortOrder = sortOrders.get(index);
        Block left = leftRow.getBlock(channel);
        Block right = rightRow.getBlock(channel);
        int comparison;
        try {
            comparison = sortOrder.compareBlockValue(type, left, 0, right, 0);
        } catch (NotSupportedException e) {
            throw new PrestoException(NOT_SUPPORTED, e.getMessage(), e);
        }
        if (comparison != 0) {
            return comparison;
        }
    }
    return 0;
}
Also used : Type(com.facebook.presto.common.type.Type) SortOrder(com.facebook.presto.common.block.SortOrder) Block(com.facebook.presto.common.block.Block) PrestoException(com.facebook.presto.spi.PrestoException) NotSupportedException(com.facebook.presto.common.NotSupportedException)

Example 7 with SortOrder

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

the class SimplePagesIndexComparator method compareTo.

@Override
public int compareTo(PagesIndex pagesIndex, int leftPosition, int rightPosition) {
    AdaptiveLongBigArray valueAddresses = pagesIndex.getValueAddresses();
    long leftPageAddress = valueAddresses.get(leftPosition);
    long rightPageAddress = valueAddresses.get(rightPosition);
    int leftBlockIndex = decodeSliceIndex(leftPageAddress);
    int leftBlockPosition = decodePosition(leftPageAddress);
    int rightBlockIndex = decodeSliceIndex(rightPageAddress);
    int rightBlockPosition = decodePosition(rightPageAddress);
    for (int i = 0; i < sortChannels.length; i++) {
        int sortChannel = sortChannels[i];
        SortOrder sortOrder = sortOrders[i];
        Type sortType = sortTypes[i];
        List<Block> indexChannel = pagesIndex.getChannel(sortChannel);
        Block leftBlock = indexChannel.get(leftBlockIndex);
        Block rightBlock = indexChannel.get(rightBlockIndex);
        try {
            int compare = sortOrder.compareBlockValue(sortType, leftBlock, leftBlockPosition, rightBlock, rightBlockPosition);
            if (compare != 0) {
                return compare;
            }
        } catch (NotSupportedException e) {
            throw new PrestoException(NOT_SUPPORTED, e.getMessage(), e);
        }
    }
    return 0;
}
Also used : Type(com.facebook.presto.common.type.Type) SortOrder(com.facebook.presto.common.block.SortOrder) Block(com.facebook.presto.common.block.Block) PrestoException(com.facebook.presto.spi.PrestoException) AdaptiveLongBigArray(com.facebook.presto.common.array.AdaptiveLongBigArray) NotSupportedException(com.facebook.presto.common.NotSupportedException)

Example 8 with SortOrder

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

the class PushAggregationThroughOuterJoin method inlineOrderByVariables.

private static OrderingScheme inlineOrderByVariables(Map<VariableReferenceExpression, VariableReferenceExpression> variableMapping, OrderingScheme orderingScheme) {
    // This is a logic expanded from ExpressionTreeRewriter::rewriteSortItems
    ImmutableList.Builder<VariableReferenceExpression> orderBy = ImmutableList.builder();
    ImmutableMap.Builder<VariableReferenceExpression, SortOrder> ordering = new ImmutableMap.Builder<>();
    for (VariableReferenceExpression variable : orderingScheme.getOrderByVariables()) {
        VariableReferenceExpression translated = variableMapping.get(variable);
        orderBy.add(translated);
        ordering.put(translated, orderingScheme.getOrdering(variable));
    }
    ImmutableMap<VariableReferenceExpression, SortOrder> orderingMap = ordering.build();
    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) ImmutableList(com.google.common.collect.ImmutableList) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) VariableReferenceExpression(com.facebook.presto.spi.relation.VariableReferenceExpression) Ordering(com.facebook.presto.spi.plan.Ordering) SortOrder(com.facebook.presto.common.block.SortOrder) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 9 with SortOrder

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

the class TestWindowOperator method testRowNumberPartition.

@Test(dataProvider = "spillEnabled")
public void testRowNumberPartition(boolean spillEnabled, boolean revokeMemoryWhenAddingPages, long memoryLimit) {
    List<Page> input = rowPagesBuilder(VARCHAR, BIGINT, DOUBLE, BOOLEAN).row("b", -1L, -0.1, true).row("a", 2L, 0.3, false).row("a", 4L, 0.2, true).pageBreak().row("b", 5L, 0.4, false).row("a", 6L, 0.1, true).build();
    WindowOperatorFactory operatorFactory = createFactoryUnbounded(ImmutableList.of(VARCHAR, BIGINT, DOUBLE, BOOLEAN), Ints.asList(0, 1, 2, 3), ROW_NUMBER, Ints.asList(0), Ints.asList(1), ImmutableList.copyOf(new SortOrder[] { SortOrder.ASC_NULLS_LAST }), spillEnabled);
    DriverContext driverContext = createDriverContext(memoryLimit);
    MaterializedResult expected = resultBuilder(driverContext.getSession(), VARCHAR, BIGINT, DOUBLE, BOOLEAN, BIGINT).row("a", 2L, 0.3, false, 1L).row("a", 4L, 0.2, true, 2L).row("a", 6L, 0.1, true, 3L).row("b", -1L, -0.1, true, 1L).row("b", 5L, 0.4, false, 2L).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 10 with SortOrder

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

the class TestWindowOperator method testRowNumber.

@Test(dataProvider = "spillEnabled")
public void testRowNumber(boolean spillEnabled, boolean revokeMemoryWhenAddingPages, long memoryLimit) {
    List<Page> input = rowPagesBuilder(BIGINT, DOUBLE).row(2L, 0.3).row(4L, 0.2).row(6L, 0.1).pageBreak().row(-1L, -0.1).row(5L, 0.4).build();
    WindowOperatorFactory operatorFactory = createFactoryUnbounded(ImmutableList.of(BIGINT, DOUBLE), Ints.asList(1, 0), ROW_NUMBER, Ints.asList(), Ints.asList(0), ImmutableList.copyOf(new SortOrder[] { SortOrder.ASC_NULLS_LAST }), spillEnabled);
    DriverContext driverContext = createDriverContext(memoryLimit);
    MaterializedResult expected = resultBuilder(driverContext.getSession(), DOUBLE, BIGINT, BIGINT).row(-0.1, -1L, 1L).row(0.3, 2L, 2L).row(0.2, 4L, 3L).row(0.4, 5L, 4L).row(0.1, 6L, 5L).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)

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