ilooner commented on a change in pull request #1344: DRILL-6461: Added basic 
data correctness tests for hash agg, and improved operator unit testing 
framework.
URL: https://github.com/apache/drill/pull/1344#discussion_r211041686
 
 

 ##########
 File path: 
exec/java-exec/src/test/java/org/apache/drill/test/OperatorTestBuilder.java
 ##########
 @@ -0,0 +1,300 @@
+/*
+ * 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.test;
+
+import com.google.common.base.Preconditions;
+import org.apache.commons.lang3.mutable.MutableInt;
+import org.apache.drill.exec.physical.base.AbstractBase;
+import org.apache.drill.exec.physical.base.PhysicalOperator;
+import org.apache.drill.exec.physical.impl.BatchCreator;
+import org.apache.drill.exec.physical.impl.MockRecordBatch;
+import org.apache.drill.exec.physical.impl.svremover.Copier;
+import org.apache.drill.exec.physical.impl.svremover.GenericCopier;
+import org.apache.drill.exec.record.CloseableRecordBatch;
+import org.apache.drill.exec.record.RecordBatch;
+import org.apache.drill.exec.record.RecordBatchSizer;
+import org.apache.drill.exec.record.VectorContainer;
+import org.apache.drill.exec.vector.FixedWidthVector;
+import org.apache.drill.exec.vector.ValueVector;
+import org.apache.drill.exec.vector.VariableWidthVector;
+import org.apache.drill.test.rowSet.DirectRowSet;
+import org.apache.drill.test.rowSet.IndirectRowSet;
+import org.apache.drill.test.rowSet.RowSet;
+import org.apache.drill.test.rowSet.RowSetComparison;
+import org.junit.Assert;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Optional;
+import java.util.stream.Collectors;
+
+public class OperatorTestBuilder {
+
+  private static final org.slf4j.Logger logger = 
org.slf4j.LoggerFactory.getLogger(OperatorTestBuilder.class);
+
+  private final List<RowSet> expectedResults = new ArrayList<>();
+  private final List<MockRecordBatch> upstreamBatches = new ArrayList<>();
+  private PhysicalOpUnitTestBase physicalOpUnitTestBase;
+
+  private PhysicalOperator physicalOperator;
+  private long initReservation = AbstractBase.INIT_ALLOCATION;
+  private long maxAllocation = AbstractBase.MAX_ALLOCATION;
+  private Optional<Integer> expectedNumBatchesOpt = Optional.empty();
+  private Optional<Integer> expectedTotalRowsOpt = Optional.empty();
+  private boolean combineOutputBatches;
+
+  public OperatorTestBuilder(PhysicalOpUnitTestBase physicalOpUnitTestBase) {
+    this.physicalOpUnitTestBase = physicalOpUnitTestBase;
+  }
+
+  @SuppressWarnings({"unchecked", "resource"})
+  public void go() throws Exception {
+    final List<RowSet> actualResults = new ArrayList<>();
+    CloseableRecordBatch testOperator = null;
+
+    try {
+      validate();
+      int expectedNumBatches = 
expectedNumBatchesOpt.orElse(expectedResults.size());
+      physicalOpUnitTestBase.mockOpContext(physicalOperator, initReservation, 
maxAllocation);
+
+      final BatchCreator<PhysicalOperator> opCreator = 
(BatchCreator<PhysicalOperator>) 
physicalOpUnitTestBase.opCreatorReg.getOperatorCreator(physicalOperator.getClass());
+      testOperator = opCreator.getBatch(physicalOpUnitTestBase.fragContext, 
physicalOperator, (List)upstreamBatches);
+
+      batchIterator: for (int batchIndex = 0;; batchIndex++) {
+        final RecordBatch.IterOutcome outcome = testOperator.next();
+
+        switch (outcome) {
+          case NONE:
+            if (!combineOutputBatches) {
+              Assert.assertEquals(expectedNumBatches, batchIndex);
+            }
+            // We are done iterating over batches. Now we need to compare them.
+            break batchIterator;
+          case OK_NEW_SCHEMA:
+            boolean skip = true;
+
+            try {
+              skip = testOperator.getContainer().getRecordCount() == 0;
+            } catch (IllegalStateException e) {
+              // We should skip this batch in this case. It means no data was 
included with the okay schema
+            } finally {
+              if (skip) {
+                batchIndex--;
+                break;
+              }
+            }
+          case OK:
+            if (!combineOutputBatches && batchIndex >= expectedNumBatches) {
+              testOperator.getContainer().clear();
+              Assert.fail("More batches received than expected.");
+            } else {
+              final boolean hasSelectionVector = 
testOperator.getSchema().getSelectionVectorMode().hasSelectionVector;
+              final VectorContainer container = testOperator.getContainer();
+
+              if (hasSelectionVector) {
+                
actualResults.add(IndirectRowSet.fromSv2(testOperator.getContainer(), 
testOperator.getSelectionVector2()));
+              } else {
+                actualResults.add(DirectRowSet.fromContainer(container));
+              }
+              break;
+            }
+          default:
+            throw new UnsupportedOperationException("Can't handle this yet");
+        }
+      }
+
+      int actualTotalRows = actualResults.stream()
+        .map(rowSet -> rowSet.rowCount())
+        .reduce((Integer countA, Integer countB) -> countA + countB)
+        .orElse(0);
+
+      if (expectedResults.isEmpty()) {
+        Assert.assertEquals((int) expectedTotalRowsOpt.orElse(0), 
actualTotalRows);
+        // We are done, we don't have any expected result to compare
+        return;
+      }
+
+      if (combineOutputBatches) {
 
 Review comment:
   This is needed for operators like HashAgg. Internally HashAgg maintains 
multiple partitions, and on completion separate output batches are emitted for 
each partition. So if HashAgg is using 32 partitions internally, it will emit 
at least 32 batches even for a small test data set.
   
   It is difficult to predict what records go to what partitions without going 
through the entire hashcode calculation for the test. Additionally I only care 
about the correctness of the final results, and not about what partition got 
what records. So combining all the results into a single batch for comparison 
simplifies testing.
   

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to