-
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
471 additions
and
47 deletions.
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
core/trino-main/src/main/java/io/trino/operator/aggregation/OrderedWindowAccumulator.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,142 @@ | ||
/* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
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 OrderedWindowAccumulator | ||
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 OrderedWindowAccumulator( | ||
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 OrderedWindowAccumulator( | ||
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 OrderedWindowAccumulator(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.