-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support ORDER BY in window functions (WIP)
- Loading branch information
Showing
30 changed files
with
323 additions
and
46 deletions.
There are no files selected for viewing
129 changes: 129 additions & 0 deletions
129
core/trino-main/src/main/java/io/trino/operator/aggregation/OrderingWindowAccumulator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
package io.trino.operator.aggregation; | ||
|
||
import com.google.common.collect.ImmutableList; | ||
import io.trino.operator.PagesIndex; | ||
import io.trino.operator.window.PagesWindowIndex; | ||
import io.trino.spi.PageBuilder; | ||
import io.trino.spi.block.BlockBuilder; | ||
import io.trino.spi.block.ValueBlock; | ||
import io.trino.spi.connector.SortOrder; | ||
import io.trino.spi.function.WindowAccumulator; | ||
import io.trino.spi.function.WindowIndex; | ||
import io.trino.spi.type.Type; | ||
|
||
import java.util.List; | ||
import java.util.stream.IntStream; | ||
|
||
import static com.google.common.base.Preconditions.checkState; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
public class OrderingWindowAccumulator | ||
implements WindowAccumulator | ||
{ | ||
PagesIndex.Factory pagesIndexFactory; | ||
private WindowAccumulator delegate; | ||
private final WindowAccumulator initialDelegate; | ||
private final List<Type> argumentTypes; | ||
private final List<Integer> argumentChannels; | ||
private final List<Integer> sortChannels; | ||
private final List<SortOrder> sortOrders; | ||
|
||
private PageBuilder pageBuilder; | ||
private final PagesIndex pagesIndex; | ||
private boolean pagesIndexSorted; | ||
|
||
public OrderingWindowAccumulator( | ||
PagesIndex.Factory pagesIndexFactory, | ||
WindowAccumulator delegate, | ||
List<Type> argumentTypes, | ||
List<Integer> argumentChannels, | ||
List<SortOrder> sortOrders) | ||
{ | ||
this(pagesIndexFactory, delegate, argumentTypes, argumentChannels, sortOrders, pagesIndexFactory.newPagesIndex(argumentTypes, 10_000)); | ||
} | ||
|
||
private OrderingWindowAccumulator( | ||
PagesIndex.Factory pagesIndexFactory, | ||
WindowAccumulator delegate, | ||
List<Type> argumentTypes, | ||
List<Integer> argumentChannels, | ||
List<SortOrder> sortOrders, | ||
PagesIndex pagesIndex) | ||
{ | ||
this.pagesIndexFactory = requireNonNull(pagesIndexFactory, "pagesIndexFactory is null"); | ||
this.delegate = requireNonNull(delegate, "delegate is null"); | ||
this.initialDelegate = delegate.copy(); | ||
this.argumentTypes = requireNonNull(argumentTypes, "argumentTypes is null"); | ||
this.argumentChannels = ImmutableList.copyOf(argumentChannels); | ||
this.sortOrders = ImmutableList.copyOf(sortOrders); | ||
this.pagesIndex = requireNonNull(pagesIndex, "pagesIndex is null"); | ||
this.sortChannels = IntStream.range(argumentTypes.size() - sortOrders.size(), argumentChannels.size()) | ||
.boxed() | ||
.toList(); | ||
resetPageBuilder(); | ||
} | ||
|
||
private void resetPageBuilder() | ||
{ | ||
pageBuilder = new PageBuilder(argumentTypes); | ||
} | ||
|
||
@Override | ||
public long getEstimatedSize() | ||
{ | ||
return delegate.getEstimatedSize() + initialDelegate.getEstimatedSize() + pagesIndex.getEstimatedSize().toBytes() + pageBuilder.getRetainedSizeInBytes(); | ||
} | ||
|
||
@Override | ||
public WindowAccumulator copy() | ||
{ | ||
PagesIndex pagesIndexCopy = pagesIndexFactory.newPagesIndex(argumentTypes, pagesIndex.getPositionCount()); | ||
pagesIndex.getPages().forEachRemaining(pagesIndexCopy::addPage); | ||
return new OrderingWindowAccumulator(pagesIndexFactory, delegate.copy(), argumentTypes, argumentChannels, sortOrders, pagesIndexCopy); | ||
} | ||
|
||
@Override | ||
public void addInput(WindowIndex index, int startPosition, int endPosition) | ||
{ | ||
if (pagesIndexSorted) { | ||
pagesIndexSorted = false; | ||
// operate on delegate as of start | ||
// nicer would be to add reset() method to WindowAccumulator but it requires reset method in each AccumulatorState class | ||
delegate = initialDelegate.copy(); | ||
} | ||
// index is remapped so just go from 0 to argumentChannels.size() | ||
for (int position = startPosition; position <= endPosition; position++) { | ||
if (pageBuilder.isFull()) { | ||
pagesIndex.addPage(pageBuilder.build()); | ||
resetPageBuilder(); | ||
} | ||
for (int channel = 0; channel < argumentChannels.size(); channel++) { | ||
ValueBlock value = index.getSingleValueBlock(channel, position).getSingleValueBlock(0); | ||
pageBuilder.getBlockBuilder(channel).append(value, 0); | ||
} | ||
pageBuilder.declarePosition(); | ||
} | ||
} | ||
|
||
@Override | ||
public void output(BlockBuilder blockBuilder) | ||
{ | ||
if (!pagesIndexSorted) { | ||
if (!pageBuilder.isEmpty()) { | ||
pagesIndex.addPage(pageBuilder.build()); | ||
resetPageBuilder(); | ||
} | ||
int positionCount = pagesIndex.getPositionCount(); | ||
if (positionCount == 0) { | ||
return; | ||
} | ||
pagesIndex.sort(sortChannels, sortOrders); | ||
WindowIndex sortedWindowIndex = new PagesWindowIndex(pagesIndex, 0, positionCount); | ||
delegate.addInput(sortedWindowIndex, 0, positionCount - 1); | ||
pagesIndexSorted = true; | ||
} | ||
checkState(pageBuilder.isEmpty()); | ||
|
||
delegate.output(blockBuilder); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.