-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
117 additions
and
53 deletions.
There are no files selected for viewing
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
104 changes: 104 additions & 0 deletions
104
presto-main/src/main/java/io/prestosql/operator/TopNProcessor.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,104 @@ | ||
/* | ||
* 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.prestosql.operator; | ||
|
||
import io.prestosql.memory.context.AggregatedMemoryContext; | ||
import io.prestosql.memory.context.LocalMemoryContext; | ||
import io.prestosql.spi.Page; | ||
import io.prestosql.spi.block.SortOrder; | ||
import io.prestosql.spi.type.Type; | ||
|
||
import javax.annotation.Nullable; | ||
|
||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import static com.google.common.base.Preconditions.checkArgument; | ||
import static com.google.common.base.Verify.verify; | ||
import static java.util.Collections.emptyIterator; | ||
import static java.util.Objects.requireNonNull; | ||
|
||
/** | ||
* Returns the top N rows from the source sorted according to the specified ordering in the keyChannelIndex channel. | ||
*/ | ||
public class TopNProcessor | ||
{ | ||
private final LocalMemoryContext localUserMemoryContext; | ||
|
||
@Nullable | ||
private GroupedTopNBuilder topNBuilder; | ||
private Iterator<Page> outputIterator; | ||
|
||
public TopNProcessor( | ||
AggregatedMemoryContext aggregatedMemoryContext, | ||
List<Type> types, | ||
int n, | ||
List<Integer> sortChannels, | ||
List<SortOrder> sortOrders) | ||
{ | ||
requireNonNull(aggregatedMemoryContext, "aggregatedMemoryContext is null"); | ||
this.localUserMemoryContext = aggregatedMemoryContext.newLocalMemoryContext(TopNProcessor.class.getSimpleName()); | ||
checkArgument(n >= 0, "n must be positive"); | ||
|
||
if (n == 0) { | ||
outputIterator = emptyIterator(); | ||
} | ||
else { | ||
topNBuilder = new GroupedTopNBuilder( | ||
types, | ||
new SimplePageWithPositionComparator(types, sortChannels, sortOrders), | ||
n, | ||
false, | ||
new NoChannelGroupByHash()); | ||
} | ||
} | ||
|
||
public void addInput(Page page) | ||
{ | ||
requireNonNull(topNBuilder, "topNBuilder is null"); | ||
boolean done = topNBuilder.processPage(requireNonNull(page, "page is null")).process(); | ||
// there is no grouping so work will always be done | ||
verify(done); | ||
updateMemoryReservation(); | ||
} | ||
|
||
public Page getOutput() | ||
{ | ||
if (outputIterator == null) { | ||
// start flushing | ||
outputIterator = topNBuilder.buildResult(); | ||
} | ||
|
||
Page output = null; | ||
if (outputIterator.hasNext()) { | ||
output = outputIterator.next(); | ||
} | ||
else { | ||
outputIterator = emptyIterator(); | ||
} | ||
updateMemoryReservation(); | ||
return output; | ||
} | ||
|
||
public boolean noMoreOutput() | ||
{ | ||
return outputIterator != null && !outputIterator.hasNext(); | ||
} | ||
|
||
private void updateMemoryReservation() | ||
{ | ||
requireNonNull(topNBuilder, "topNBuilder is null"); | ||
localUserMemoryContext.setBytes(topNBuilder.getEstimatedSizeInBytes()); | ||
} | ||
} |