Search in sources :

Example 1 with SortOrder

use of io.prestosql.spi.block.SortOrder in project hetu-core by openlookeng.

the class TestSortBuffer method testPageSorterSmallPages.

@Test
public void testPageSorterSmallPages() {
    List<Type> types = ImmutableList.of(BIGINT, DOUBLE, VARCHAR);
    List<Integer> sortChannels = Ints.asList(0);
    List<SortOrder> sortOrders = ImmutableList.of(ASC_NULLS_FIRST);
    SortBuffer sortBuffer = new SortBuffer(new DataSize(1, DataSize.Unit.GIGABYTE), types, sortChannels, sortOrders, sorter);
    RowPagesBuilder inputPagesBuilder = RowPagesBuilder.rowPagesBuilder(types);
    for (long i = 1; i < 100000; i++) {
        inputPagesBuilder.row(i, 1.1, "a").pageBreak();
    }
    List<Page> inputPages = inputPagesBuilder.build();
    RowPagesBuilder expectedPagesBuilder = RowPagesBuilder.rowPagesBuilder(types);
    for (long i = 1; i < 100000; i++) {
        expectedPagesBuilder.row(i, 1.1, "a");
        // based on default max page size, each page will be full when it has 43691 rows
        if (i % 43691 == 0) {
            expectedPagesBuilder.pageBreak();
        }
    }
    List<Page> expectedPages = expectedPagesBuilder.build();
    inputPages.stream().forEach(sortBuffer::add);
    List<Page> actualPages = new LinkedList<>();
    sortBuffer.flushTo(actualPages::add);
    assertSorted(expectedPages, actualPages, types);
}
Also used : RowPagesBuilder(io.prestosql.RowPagesBuilder) SortOrder(io.prestosql.spi.block.SortOrder) Page(io.prestosql.spi.Page) SortBuffer(io.prestosql.plugin.memory.data.SortBuffer) LinkedList(java.util.LinkedList) Type(io.prestosql.spi.type.Type) DataSize(io.airlift.units.DataSize) Test(org.testng.annotations.Test)

Example 2 with SortOrder

use of io.prestosql.spi.block.SortOrder in project hetu-core by openlookeng.

the class MySqlSqlStatementWriter method orderBy.

/**
 * MySql doesn't support NULLS FIRST & NULLS LAST in ORDER BY section, so
 * use ISNULL() replace it.
 * e.g.
 * select * from table order by id ASC NULLS FIRST -> select * from table order by ISNULL(id) DESC, id ASC
 * select * from table order by id ASC NULLS LAST -> select * from table order by ISNULL(id) ASC, id ASC
 *
 * @param table input table
 * @param orderings input ordering scheme
 * @return order by section
 */
@Override
public String orderBy(String table, List<OrderBy> orderings) {
    StringJoiner joiner = new StringJoiner(", ");
    for (OrderBy orderBy : orderings) {
        StringJoiner orderItem = new StringJoiner(" ");
        String orderByColumn = orderBy.getSymbol();
        orderItem.add("ISNULL(" + orderByColumn + ")");
        SortOrder sortOrder = orderBy.getType();
        orderItem.add(sortOrder.isNullsFirst() ? "DESC" : "ASC");
        joiner.merge(orderItem);
        orderItem = new StringJoiner(" ");
        orderItem.add(orderByColumn);
        orderItem.add(sortOrder.isAscending() ? "ASC" : "DESC");
        joiner.merge(orderItem);
    }
    return table + " ORDER BY " + joiner.toString();
}
Also used : OrderBy(io.prestosql.spi.sql.expression.OrderBy) SortOrder(io.prestosql.spi.block.SortOrder) StringJoiner(java.util.StringJoiner)

Example 3 with SortOrder

use of io.prestosql.spi.block.SortOrder in project hetu-core by openlookeng.

the class MemoryTableProperties method sortingColumnFromString.

private static SortingColumn sortingColumnFromString(String name) {
    String newName = name;
    SortOrder order = SortOrder.ASC_NULLS_LAST;
    String lower = newName.toUpperCase(ENGLISH);
    if (lower.endsWith(" ASC")) {
        newName = newName.substring(0, newName.length() - 4).trim();
    } else if (lower.endsWith(" DESC")) {
        // TODO: support DESC
        throw new UnsupportedOperationException("DESC sort is not supported yet.");
    }
    return new SortingColumn(newName, order);
}
Also used : SortOrder(io.prestosql.spi.block.SortOrder)

Example 4 with SortOrder

use of io.prestosql.spi.block.SortOrder in project hetu-core by openlookeng.

the class TopNStatsRule method doCalculate.

@Override
protected Optional<PlanNodeStatsEstimate> doCalculate(TopNNode node, StatsProvider statsProvider, Lookup lookup, Session session, TypeProvider types) {
    PlanNodeStatsEstimate sourceStats = statsProvider.getStats(node.getSource());
    double rowCount = sourceStats.getOutputRowCount();
    if (rowCount <= node.getCount()) {
        return Optional.of(sourceStats);
    }
    long limitCount = node.getCount();
    PlanNodeStatsEstimate resultStats = PlanNodeStatsEstimate.buildFrom(sourceStats).setOutputRowCount(limitCount).build();
    if (limitCount == 0) {
        return Optional.of(resultStats);
    }
    // augment null fraction estimation for first ORDER BY symbol
    // Assuming not empty list
    Symbol firstOrderSymbol = node.getOrderingScheme().getOrderBy().get(0);
    SortOrder sortOrder = node.getOrderingScheme().getOrdering(firstOrderSymbol);
    resultStats = resultStats.mapSymbolColumnStatistics(firstOrderSymbol, symbolStats -> {
        SymbolStatsEstimate.Builder newStats = SymbolStatsEstimate.buildFrom(symbolStats);
        double nullCount = rowCount * symbolStats.getNullsFraction();
        if (sortOrder.isNullsFirst()) {
            if (nullCount > limitCount) {
                newStats.setNullsFraction(1.0);
            } else {
                newStats.setNullsFraction(nullCount / limitCount);
            }
        } else {
            double nonNullCount = (rowCount - nullCount);
            if (nonNullCount > limitCount) {
                newStats.setNullsFraction(0.0);
            } else {
                newStats.setNullsFraction((limitCount - nonNullCount) / limitCount);
            }
        }
        return newStats.build();
    });
    // TopN actually limits (or when there was no row count estimated for source)
    return Optional.of(resultStats);
}
Also used : Symbol(io.prestosql.spi.plan.Symbol) Patterns.topN(io.prestosql.sql.planner.plan.Patterns.topN) Lookup(io.prestosql.sql.planner.iterative.Lookup) TopNNode(io.prestosql.spi.plan.TopNNode) Session(io.prestosql.Session) TypeProvider(io.prestosql.sql.planner.TypeProvider) Optional(java.util.Optional) Pattern(io.prestosql.matching.Pattern) SortOrder(io.prestosql.spi.block.SortOrder) Symbol(io.prestosql.spi.plan.Symbol) SortOrder(io.prestosql.spi.block.SortOrder)

Example 5 with SortOrder

use of io.prestosql.spi.block.SortOrder in project hetu-core by openlookeng.

the class TestWindowOperator method testRowNumberArbitraryWithSpill.

@Test
public void testRowNumberArbitraryWithSpill() {
    List<Page> input = rowPagesBuilder(BIGINT).row(1L).row(3L).row(5L).row(7L).pageBreak().row(2L).row(4L).row(6L).row(8L).build();
    WindowOperatorFactory operatorFactory = createFactoryUnbounded(ImmutableList.of(BIGINT), Ints.asList(0), ROW_NUMBER, Ints.asList(), Ints.asList(), ImmutableList.copyOf(new SortOrder[] {}), true);
    DriverContext driverContext = createDriverContext();
    MaterializedResult expected = resultBuilder(driverContext.getSession(), BIGINT, BIGINT).row(1L, 1L).row(2L, 2L).row(3L, 3L).row(4L, 4L).row(5L, 5L).row(6L, 6L).row(7L, 7L).row(8L, 8L).build();
    assertOperatorEquals(operatorFactory, driverContext, input, expected);
}
Also used : WindowOperatorFactory(io.prestosql.operator.WindowOperator.WindowOperatorFactory) SortOrder(io.prestosql.spi.block.SortOrder) MarkerPage(io.prestosql.spi.snapshot.MarkerPage) Page(io.prestosql.spi.Page) MaterializedResult(io.prestosql.testing.MaterializedResult) OperatorAssertion.toMaterializedResult(io.prestosql.operator.OperatorAssertion.toMaterializedResult) Test(org.testng.annotations.Test)

Aggregations

SortOrder (io.prestosql.spi.block.SortOrder)37 Page (io.prestosql.spi.Page)23 Test (org.testng.annotations.Test)22 WindowOperatorFactory (io.prestosql.operator.WindowOperator.WindowOperatorFactory)19 MarkerPage (io.prestosql.spi.snapshot.MarkerPage)19 OperatorAssertion.toMaterializedResult (io.prestosql.operator.OperatorAssertion.toMaterializedResult)18 MaterializedResult (io.prestosql.testing.MaterializedResult)18 Type (io.prestosql.spi.type.Type)10 ImmutableList (com.google.common.collect.ImmutableList)9 Symbol (io.prestosql.spi.plan.Symbol)5 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)4 OrderingScheme (io.prestosql.spi.plan.OrderingScheme)4 ArrayList (java.util.ArrayList)4 DataSize (io.airlift.units.DataSize)3 InMemoryNodeManager (io.prestosql.metadata.InMemoryNodeManager)3 Block (io.prestosql.spi.block.Block)3 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ImmutableMap.toImmutableMap (com.google.common.collect.ImmutableMap.toImmutableMap)2 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)2