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);
}
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();
}
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);
}
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);
}
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);
}
Aggregations