Github user ilooner commented on a diff in the pull request: https://github.com/apache/drill/pull/984#discussion_r147009092 --- Diff: exec/java-exec/src/test/java/org/apache/drill/exec/physical/impl/TopN/TopNBatchTest.java --- @@ -0,0 +1,179 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.drill.exec.physical.impl.TopN; + +import java.util.List; +import java.util.Map; +import java.util.Properties; +import java.util.Random; + +import com.google.common.collect.Lists; +import org.apache.drill.test.TestBuilder; +import org.apache.drill.categories.OperatorTest; +import org.apache.drill.common.config.DrillConfig; +import org.apache.drill.common.expression.FieldReference; +import org.apache.drill.common.logical.data.Order; +import org.apache.drill.common.types.TypeProtos; +import org.apache.drill.common.types.Types; +import org.apache.drill.exec.compile.ClassBuilder; +import org.apache.drill.exec.compile.CodeCompiler; +import org.apache.drill.exec.expr.fn.FunctionImplementationRegistry; +import org.apache.drill.exec.memory.RootAllocator; +import org.apache.drill.exec.physical.impl.sort.RecordBatchData; +import org.apache.drill.exec.pop.PopUnitTestBase; +import org.apache.drill.exec.record.BatchSchema; +import org.apache.drill.exec.record.ExpandableHyperContainer; +import org.apache.drill.exec.record.MaterializedField; +import org.apache.drill.exec.record.VectorContainer; +import org.apache.drill.exec.server.options.OptionSet; +import org.apache.drill.test.ClientFixture; +import org.apache.drill.test.ClusterFixture; +import org.apache.drill.test.FixtureBuilder; +import org.apache.drill.test.OperatorFixture; +import org.apache.drill.test.BatchUtils; +import org.apache.drill.test.DirTestWatcher; +import org.apache.drill.test.rowSet.RowSetBuilder; +import org.junit.Rule; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +@Category(OperatorTest.class) +public class TopNBatchTest extends PopUnitTestBase { + static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(TopNBatchTest.class); + + // Allows you to look at generated code after tests execute + @Rule + public DirTestWatcher dirTestWatcher = new DirTestWatcher(false); + + /** + * Priority queue unit test. + * @throws Exception + */ + @Test + public void priorityQueueOrderingTest() throws Exception { + Properties properties = new Properties(); + properties.setProperty(ClassBuilder.CODE_DIR_OPTION, dirTestWatcher.getDirPath()); + + DrillConfig drillConfig = DrillConfig.create(properties); + OptionSet optionSet = new OperatorFixture.TestOptionSet(); + + FieldReference expr = FieldReference.getWithQuotedRef("colA"); + Order.Ordering ordering = new Order.Ordering(Order.Ordering.ORDER_DESC, expr, Order.Ordering.NULLS_FIRST); + List<Order.Ordering> orderings = Lists.newArrayList(ordering); + + MaterializedField colA = MaterializedField.create("colA", Types.required(TypeProtos.MinorType.INT)); + MaterializedField colB = MaterializedField.create("colB", Types.required(TypeProtos.MinorType.INT)); + + List<MaterializedField> cols = Lists.newArrayList(colA, colB); + BatchSchema batchSchema = new BatchSchema(BatchSchema.SelectionVectorMode.NONE, cols); + + try (RootAllocator allocator = new RootAllocator(100_000_000)) { + VectorContainer expectedVectors = new RowSetBuilder(allocator, batchSchema) + .add(110, 10) + .add(109, 9) + .add(108, 8) + .add(107, 7) + .add(106, 6) + .add(105, 5) + .add(104, 4) + .add(103, 3) + .add(102, 2) + .add(101, 1) + .build() + .container(); + + Map<String, List<Object>> expectedTable = BatchUtils.containerToObjects(expectedVectors); + expectedVectors.clear(); + + PriorityQueue queue; + ExpandableHyperContainer hyperContainer; + + { + VectorContainer container = new RowSetBuilder(allocator, batchSchema) + .build() + .container(); + hyperContainer = new ExpandableHyperContainer(container); + + queue = TopNBatch.createNewPriorityQueue( --- End diff -- Agreed. Sounds good.
---