DRILL-3354: TestBuilder can check if the number of result batches equals some 
expected value


Project: http://git-wip-us.apache.org/repos/asf/drill/repo
Commit: http://git-wip-us.apache.org/repos/asf/drill/commit/f0b29a41
Tree: http://git-wip-us.apache.org/repos/asf/drill/tree/f0b29a41
Diff: http://git-wip-us.apache.org/repos/asf/drill/diff/f0b29a41

Branch: refs/heads/master
Commit: f0b29a41f9bcc3e52630ff354b413139d73ed25e
Parents: ae2a45c
Author: adeneche <[email protected]>
Authored: Wed Jun 24 09:03:27 2015 -0700
Committer: Jason Altekruse <[email protected]>
Committed: Tue Aug 4 08:49:02 2015 -0700

----------------------------------------------------------------------
 .../java/org/apache/drill/DrillTestWrapper.java | 24 +++++++++-
 .../test/java/org/apache/drill/TestBuilder.java | 46 ++++++++++++++------
 .../physical/impl/window/TestWindowFrame.java   | 37 +++++++++-------
 3 files changed, 77 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/drill/blob/f0b29a41/exec/java-exec/src/test/java/org/apache/drill/DrillTestWrapper.java
----------------------------------------------------------------------
diff --git 
a/exec/java-exec/src/test/java/org/apache/drill/DrillTestWrapper.java 
b/exec/java-exec/src/test/java/org/apache/drill/DrillTestWrapper.java
index 77d2a54..ff75274 100644
--- a/exec/java-exec/src/test/java/org/apache/drill/DrillTestWrapper.java
+++ b/exec/java-exec/src/test/java/org/apache/drill/DrillTestWrapper.java
@@ -64,6 +64,9 @@ public class DrillTestWrapper {
   // it will only make the test slower.
   private static boolean VERBOSE_DEBUG = false;
 
+  // Unit test doesn't expect any specific batch count
+  public static final int EXPECTED_BATCH_COUNT_NOT_SET = -1;
+
   // The motivation behind the TestBuilder was to provide a clean API for test 
writers. The model is mostly designed to
   // prepare all of the components necessary for running the tests, before the 
TestWrapper is initialized. There is however
   // one case where the setup for the baseline is driven by the test query 
results, and this is implicit type enforcement
@@ -95,10 +98,12 @@ public class DrillTestWrapper {
   // and translated into a map in the builder
   private List<Map> baselineRecords;
 
+  private int expectedNumBatches;
+
   public DrillTestWrapper(TestBuilder testBuilder, BufferAllocator allocator, 
String query, QueryType queryType,
                           String baselineOptionSettingQueries, String 
testOptionSettingQueries,
                           QueryType baselineQueryType, boolean ordered, 
boolean approximateEquality,
-                          boolean highPerformanceComparison, List<Map> 
baselineRecords) {
+                          boolean highPerformanceComparison, List<Map> 
baselineRecords, int expectedNumBatches) {
     this.testBuilder = testBuilder;
     this.allocator = allocator;
     this.query = query;
@@ -110,6 +115,7 @@ public class DrillTestWrapper {
     this.testOptionSettingQueries = testOptionSettingQueries;
     this.highPerformanceComparison = highPerformanceComparison;
     this.baselineRecords = baselineRecords;
+    this.expectedNumBatches = expectedNumBatches;
   }
 
   public void run() throws Exception {
@@ -279,6 +285,8 @@ public class DrillTestWrapper {
       BaseTestQuery.test(testOptionSettingQueries);
       actual = BaseTestQuery.testRunAndReturn(queryType, query);
 
+      checkNumBatches(actual);
+
       addTypeInfoIfMissing(actual.get(0), testBuilder);
       addToMaterializedResults(actualRecords, actual, loader, schema);
 
@@ -327,6 +335,9 @@ public class DrillTestWrapper {
     try {
     BaseTestQuery.test(testOptionSettingQueries);
     actual = BaseTestQuery.testRunAndReturn(queryType, query);
+
+    checkNumBatches(actual);
+
     // To avoid extra work for test writers, types can optionally be inferred 
from the test query
     addTypeInfoIfMissing(actual.get(0), testBuilder);
 
@@ -365,6 +376,9 @@ public class DrillTestWrapper {
 
     BaseTestQuery.test(testOptionSettingQueries);
     List<QueryDataBatch> results = BaseTestQuery.testRunAndReturn(queryType, 
query);
+
+    checkNumBatches(results);
+
     // To avoid extra work for test writers, types can optionally be inferred 
from the test query
     addTypeInfoIfMissing(results.get(0), testBuilder);
 
@@ -379,6 +393,14 @@ public class DrillTestWrapper {
     cleanupBatches(results, expected);
   }
 
+  private void checkNumBatches(final List<QueryDataBatch> results) {
+    if (expectedNumBatches != EXPECTED_BATCH_COUNT_NOT_SET) {
+      final int actualNumBatches = results.size();
+      assertEquals(String.format("Expected %d batches but query returned %d 
non empty batch(es)%n", expectedNumBatches,
+        actualNumBatches), expectedNumBatches, actualNumBatches);
+    }
+  }
+
   private void addTypeInfoIfMissing(QueryDataBatch batch, TestBuilder 
testBuilder) {
     if (! testBuilder.typeInfoSet()) {
       Map<SchemaPath, TypeProtos.MajorType> typeMap = 
getTypeMapFromBatch(batch);

http://git-wip-us.apache.org/repos/asf/drill/blob/f0b29a41/exec/java-exec/src/test/java/org/apache/drill/TestBuilder.java
----------------------------------------------------------------------
diff --git a/exec/java-exec/src/test/java/org/apache/drill/TestBuilder.java 
b/exec/java-exec/src/test/java/org/apache/drill/TestBuilder.java
index 666ecda..330ff12 100644
--- a/exec/java-exec/src/test/java/org/apache/drill/TestBuilder.java
+++ b/exec/java-exec/src/test/java/org/apache/drill/TestBuilder.java
@@ -80,6 +80,8 @@ public class TestBuilder {
   // use this interface) and then rely on the engine for the rest of the tests 
that will use the baseline queries.
   private List<Map> baselineRecords;
 
+  private int expectedNumBatches = 
DrillTestWrapper.EXPECTED_BATCH_COUNT_NOT_SET;
+
   public TestBuilder(BufferAllocator allocator) {
     this.allocator = allocator;
     reset();
@@ -87,7 +89,8 @@ public class TestBuilder {
 
   public TestBuilder(BufferAllocator allocator, String query, 
UserBitShared.QueryType queryType, Boolean ordered,
                      boolean approximateEquality, Map<SchemaPath, 
TypeProtos.MajorType> baselineTypeMap,
-                     String baselineOptionSettingQueries, String 
testOptionSettingQueries, boolean highPerformanceComparison) {
+                     String baselineOptionSettingQueries, String 
testOptionSettingQueries, boolean highPerformanceComparison,
+                     int expectedNumBatches) {
     this(allocator);
     if (ordered == null) {
       throw new RuntimeException("Ordering not set, when using a baseline file 
or query you must explicitly call the ordered() or unOrdered() method on the " 
+ this.getClass().getSimpleName());
@@ -100,6 +103,7 @@ public class TestBuilder {
     this.baselineOptionSettingQueries = baselineOptionSettingQueries;
     this.testOptionSettingQueries = testOptionSettingQueries;
     this.highPerformanceComparison = highPerformanceComparison;
+    this.expectedNumBatches = expectedNumBatches;
   }
 
   protected TestBuilder reset() {
@@ -118,7 +122,7 @@ public class TestBuilder {
       throw new Exception("High performance comparison only available for 
ordered checks, to enforce this restriction, ordered() must be called first.");
     }
     return new DrillTestWrapper(this, allocator, query, queryType, 
baselineOptionSettingQueries, testOptionSettingQueries,
-        getValidationQueryType(), ordered, approximateEquality, 
highPerformanceComparison, baselineRecords);
+        getValidationQueryType(), ordered, approximateEquality, 
highPerformanceComparison, baselineRecords, expectedNumBatches);
   }
 
   public void go() throws Exception {
@@ -221,12 +225,14 @@ public class TestBuilder {
 
   public JSONTestBuilder jsonBaselineFile(String filePath) {
     return new JSONTestBuilder(filePath, allocator, query, queryType, ordered, 
approximateEquality,
-        baselineTypeMap, baselineOptionSettingQueries, 
testOptionSettingQueries, highPerformanceComparison);
+        baselineTypeMap, baselineOptionSettingQueries, 
testOptionSettingQueries, highPerformanceComparison,
+        expectedNumBatches);
   }
 
   public CSVTestBuilder csvBaselineFile(String filePath) {
     return new CSVTestBuilder(filePath, allocator, query, queryType, ordered, 
approximateEquality,
-        baselineTypeMap, baselineOptionSettingQueries, 
testOptionSettingQueries, highPerformanceComparison);
+        baselineTypeMap, baselineOptionSettingQueries, 
testOptionSettingQueries, highPerformanceComparison,
+        expectedNumBatches);
   }
 
   public TestBuilder baselineTypes(Map<SchemaPath, TypeProtos.MajorType> 
baselineTypeMap) {
@@ -250,6 +256,17 @@ public class TestBuilder {
   }
 
   /**
+   * Sets the number of expected batch count for this query. The test will 
fail if the query returns a different number
+   * of batches
+   *
+   * @param expectedNumBatches expected batch count
+   * @return this test builder
+   */
+  public TestBuilder expectsNumBatches(int expectedNumBatches) {
+    this.expectedNumBatches = expectedNumBatches;
+    return this;
+  }
+  /**
    * This method is used to pass in a simple list of values for a single 
record verification without
    * the need to create a CSV or JSON file to store the baseline.
    *
@@ -326,14 +343,14 @@ public class TestBuilder {
   // provide a SQL query to validate against
   public BaselineQueryTestBuilder sqlBaselineQuery(String baselineQuery) {
     return new BaselineQueryTestBuilder(baselineQuery, 
UserBitShared.QueryType.SQL, allocator, query, queryType, ordered, 
approximateEquality,
-        baselineTypeMap, baselineOptionSettingQueries, 
testOptionSettingQueries, highPerformanceComparison);
+        baselineTypeMap, baselineOptionSettingQueries, 
testOptionSettingQueries, highPerformanceComparison, expectedNumBatches);
   }
 
   // provide a path to a file containing a SQL query to use as a baseline
   public BaselineQueryTestBuilder sqlBaselineQueryFromFile(String 
baselineQueryFilename) throws IOException {
     String baselineQuery = BaseTestQuery.getFile(baselineQueryFilename);
     return new BaselineQueryTestBuilder(baselineQuery, 
UserBitShared.QueryType.SQL, allocator, query, queryType, ordered, 
approximateEquality,
-        baselineTypeMap, baselineOptionSettingQueries, 
testOptionSettingQueries, highPerformanceComparison);
+        baselineTypeMap, baselineOptionSettingQueries, 
testOptionSettingQueries, highPerformanceComparison, expectedNumBatches);
   }
 
   // as physical plans are verbose, this is the only option provided for 
specifying them, we should enforce
@@ -341,7 +358,7 @@ public class TestBuilder {
   public BaselineQueryTestBuilder physicalPlanBaselineQueryFromFile(String 
baselinePhysicalPlanPath) throws IOException {
     String baselineQuery = BaseTestQuery.getFile(baselinePhysicalPlanPath);
     return new BaselineQueryTestBuilder(baselineQuery, 
UserBitShared.QueryType.PHYSICAL, allocator, query, queryType, ordered, 
approximateEquality,
-        baselineTypeMap, baselineOptionSettingQueries, 
testOptionSettingQueries, highPerformanceComparison);
+        baselineTypeMap, baselineOptionSettingQueries, 
testOptionSettingQueries, highPerformanceComparison, expectedNumBatches);
   }
 
   private String getDecimalPrecisionScaleInfo(TypeProtos.MajorType type) {
@@ -371,9 +388,10 @@ public class TestBuilder {
 
     CSVTestBuilder(String baselineFile, BufferAllocator allocator, String 
query, UserBitShared.QueryType queryType, Boolean ordered,
                    boolean approximateEquality, Map<SchemaPath, 
TypeProtos.MajorType> baselineTypeMap,
-                   String baselineOptionSettingQueries, String 
testOptionSettingQueries, boolean highPerformanceComparison) {
+                   String baselineOptionSettingQueries, String 
testOptionSettingQueries, boolean highPerformanceComparison,
+                   int expectedNumBatches) {
       super(allocator, query, queryType, ordered, approximateEquality, 
baselineTypeMap, baselineOptionSettingQueries, testOptionSettingQueries,
-          highPerformanceComparison);
+          highPerformanceComparison, expectedNumBatches);
       this.baselineFilePath = baselineFile;
     }
 
@@ -461,9 +479,10 @@ public class TestBuilder {
 
     JSONTestBuilder(String baselineFile, BufferAllocator allocator, String 
query, UserBitShared.QueryType queryType, Boolean ordered,
                     boolean approximateEquality, Map<SchemaPath, 
TypeProtos.MajorType> baselineTypeMap,
-                    String baselineOptionSettingQueries, String 
testOptionSettingQueries, boolean highPerformanceComparison) {
+                    String baselineOptionSettingQueries, String 
testOptionSettingQueries, boolean highPerformanceComparison,
+                    int expectedNumBatches) {
       super(allocator, query, queryType, ordered, approximateEquality, 
baselineTypeMap, baselineOptionSettingQueries, testOptionSettingQueries,
-          highPerformanceComparison);
+          highPerformanceComparison, expectedNumBatches);
       this.baselineFilePath = baselineFile;
       this.baselineColumns = new String[] {"*"};
     }
@@ -486,9 +505,10 @@ public class TestBuilder {
     BaselineQueryTestBuilder(String baselineQuery, UserBitShared.QueryType 
baselineQueryType, BufferAllocator allocator,
                              String query, UserBitShared.QueryType queryType, 
Boolean ordered,
                              boolean approximateEquality, Map<SchemaPath, 
TypeProtos.MajorType> baselineTypeMap,
-                             String baselineOptionSettingQueries, String 
testOptionSettingQueries, boolean highPerformanceComparison) {
+                             String baselineOptionSettingQueries, String 
testOptionSettingQueries, boolean highPerformanceComparison,
+                             int expectedNumBatches) {
       super(allocator, query, queryType, ordered, approximateEquality, 
baselineTypeMap, baselineOptionSettingQueries, testOptionSettingQueries,
-          highPerformanceComparison);
+          highPerformanceComparison, expectedNumBatches);
       this.baselineQuery = baselineQuery;
       this.baselineQueryType = baselineQueryType;
     }

http://git-wip-us.apache.org/repos/asf/drill/blob/f0b29a41/exec/java-exec/src/test/java/org/apache/drill/exec/physical/impl/window/TestWindowFrame.java
----------------------------------------------------------------------
diff --git 
a/exec/java-exec/src/test/java/org/apache/drill/exec/physical/impl/window/TestWindowFrame.java
 
b/exec/java-exec/src/test/java/org/apache/drill/exec/physical/impl/window/TestWindowFrame.java
index 1d6c517..553c4e8 100644
--- 
a/exec/java-exec/src/test/java/org/apache/drill/exec/physical/impl/window/TestWindowFrame.java
+++ 
b/exec/java-exec/src/test/java/org/apache/drill/exec/physical/impl/window/TestWindowFrame.java
@@ -40,36 +40,41 @@ public class TestWindowFrame extends BaseTestQuery {
     updateTestCluster(1, DrillConfig.create(props));
   }
 
-  private DrillTestWrapper buildWindowQuery(final String tableName, final 
boolean withPartitionBy) throws Exception {
+  private DrillTestWrapper buildWindowQuery(final String tableName, final 
boolean withPartitionBy, final int numBatches)
+      throws Exception {
     return testBuilder()
       .sqlQuery(String.format(getFile("window/q1.sql"), TEST_RES_PATH, 
tableName, withPartitionBy ? "(partition by position_id)":"()"))
       .ordered()
       .csvBaselineFile("window/" + tableName + (withPartitionBy ? ".pby" : "") 
+ ".tsv")
       .baselineColumns("count", "sum")
+      .expectsNumBatches(numBatches)
       .build();
   }
 
-  private DrillTestWrapper buildWindowWithOrderByQuery(final String tableName, 
final boolean withPartitionBy) throws Exception {
+  private DrillTestWrapper buildWindowWithOrderByQuery(final String tableName, 
final boolean withPartitionBy,
+                                                       final int numBatches) 
throws Exception {
     return testBuilder()
-      .sqlQuery(String.format(getFile("window/q2.sql"), TEST_RES_PATH, 
tableName, withPartitionBy ? "(partition by position_id order by sub)":"(order 
by sub)"))
+      .sqlQuery(String.format(getFile("window/q2.sql"), TEST_RES_PATH, 
tableName, withPartitionBy ? "(partition by position_id order by sub)" : 
"(order by sub)"))
       .ordered()
       .csvBaselineFile("window/" + tableName + (withPartitionBy ? ".pby" : "") 
+ ".oby.tsv")
       .baselineColumns("count", "sum", "row_number", "rank", "dense_rank", 
"cume_dist", "percent_rank")
+      .expectsNumBatches(numBatches)
       .build();
   }
 
-  private void runTest(final String tableName, final boolean withPartitionBy, 
final boolean withOrderBy) throws Exception {
+  private void runTest(final String tableName, final boolean withPartitionBy, 
final boolean withOrderBy, final int numBatches) throws Exception {
 
     DrillTestWrapper testWrapper = withOrderBy ?
-      buildWindowWithOrderByQuery(tableName, withPartitionBy) : 
buildWindowQuery(tableName, withPartitionBy);
+      buildWindowWithOrderByQuery(tableName, withPartitionBy, numBatches) : 
buildWindowQuery(tableName, withPartitionBy, numBatches);
     testWrapper.run();
   }
 
-  private void runTest(final String tableName) throws Exception {
-    runTest(tableName, true, true);
-    runTest(tableName, true, false);
-    runTest(tableName, false, true);
-    runTest(tableName, false, false);
+  private void runTest(final String tableName, final int numBatches) throws 
Exception {
+    // we do expect an "extra" empty batch
+    runTest(tableName, true, true, numBatches + 1);
+    runTest(tableName, true, false, numBatches + 1);
+    runTest(tableName, false, true, numBatches + 1);
+    runTest(tableName, false, false, numBatches + 1);
   }
 
   /**
@@ -77,7 +82,7 @@ public class TestWindowFrame extends BaseTestQuery {
    */
   @Test
   public void testB1P1() throws Exception {
-    runTest("b1.p1");
+    runTest("b1.p1", 1);
   }
 
   /**
@@ -85,7 +90,7 @@ public class TestWindowFrame extends BaseTestQuery {
    */
   @Test
   public void testB1P2() throws Exception {
-    runTest("b1.p2");
+    runTest("b1.p2", 1);
   }
 
   /**
@@ -93,7 +98,7 @@ public class TestWindowFrame extends BaseTestQuery {
    */
   @Test
   public void testB2P2() throws Exception {
-    runTest("b2.p2");
+    runTest("b2.p2", 2);
   }
 
   /**
@@ -101,7 +106,7 @@ public class TestWindowFrame extends BaseTestQuery {
    */
   @Test
   public void testB2P4() throws Exception {
-    runTest("b2.p4");
+    runTest("b2.p4", 2);
   }
 
   /**
@@ -109,7 +114,7 @@ public class TestWindowFrame extends BaseTestQuery {
    */
   @Test
   public void testB3P2() throws Exception {
-    runTest("b3.p2");
+    runTest("b3.p2", 3);
   }
 
   /**
@@ -118,7 +123,7 @@ public class TestWindowFrame extends BaseTestQuery {
    */
   @Test
   public void testB4P4() throws Exception {
-    runTest("b4.p4");
+    runTest("b4.p4", 4);
   }
 
   @Test // DRILL-1862

Reply via email to