You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@drill.apache.org by ilooner <gi...@git.apache.org> on 2017/11/01 00:03:19 UTC

[GitHub] drill pull request #984: DRILL-5783 Made a unit test for generated Priority ...

Github user ilooner commented on a diff in the pull request:

    https://github.com/apache/drill/pull/984#discussion_r148157850
  
    --- Diff: exec/java-exec/src/test/java/org/apache/drill/test/BatchUtils.java ---
    @@ -0,0 +1,280 @@
    +/*
    + * 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.collect.Lists;
    +import com.google.common.collect.Maps;
    +import org.apache.drill.exec.record.VectorContainer;
    +import org.apache.drill.exec.record.VectorWrapper;
    +import org.apache.drill.exec.record.selection.SelectionVector4;
    +import org.apache.drill.exec.vector.ValueVector;
    +import org.junit.Assert;
    +
    +import java.io.UnsupportedEncodingException;
    +import java.util.Arrays;
    +import java.util.Collections;
    +import java.util.Comparator;
    +import java.util.List;
    +import java.util.Map;
    +
    +public class BatchUtils {
    +  static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(BatchUtils.class);
    +
    +  public static Map<String, List<Object>> containerToObjects(VectorContainer vectorContainer) {
    +    Map<String, List<Object>> rows = Maps.newHashMap();
    +    int numCols = vectorContainer.getNumberOfColumns();
    +    int recordCount = vectorContainer.getRecordCount();
    +
    +    for (int columnIndex = 0; columnIndex < numCols; columnIndex++) {
    +      String columnName = vectorContainer.getSchema().getColumn(columnIndex).getName();
    +      List<Object> data = Lists.newArrayList();
    +
    +      ValueVector.Accessor valueVectorAccessor = vectorContainer
    +        .getValueVector(columnIndex)
    +        .getValueVector()
    +        .getAccessor();
    +
    +      for (int recordIndex = 0; recordIndex < recordCount; recordIndex++) {
    +        data.add(valueVectorAccessor.getObject(recordIndex));
    +      }
    +
    +      rows.put(columnName, data);
    +    }
    +
    +    return rows;
    +  }
    +
    +  public static Map<String, List<Object>> hyperBatchAndSelectorToObjects(VectorContainer vectorContainer, SelectionVector4 selectionVector4) {
    +    Map<String, List<Object>> rows = Maps.newHashMap();
    +    int numCols = vectorContainer.getNumberOfColumns();
    +    int numIndices = selectionVector4.getCount();
    +
    +    for (int columnIndex = 0; columnIndex < numCols; columnIndex++) {
    +      String columnName = vectorContainer.getSchema().getColumn(columnIndex).getName();
    +      List<Object> data = Lists.newArrayList();
    +
    +      VectorWrapper vectorWrapper = vectorContainer.getValueVector(columnIndex);
    +
    +      for (int indexIndex = 0; indexIndex < numIndices; indexIndex++) {
    +        int sv4Index = selectionVector4.get(indexIndex);
    +        int batchIndex = SelectionVector4.getBatchIndex(sv4Index);
    +        int recordIndex = SelectionVector4.getRecordIndex(sv4Index);
    +
    +        ValueVector valueVector = vectorWrapper.getValueVectors()[batchIndex];
    +        Object columnValue = valueVector.getAccessor().getObject(recordIndex);
    +        data.add(columnValue);
    +      }
    +
    +      rows.put(columnName, data);
    +    }
    +
    +    return rows;
    +  }
    +
    +  public static String toString(Map<String, List<Object>> table) {
    +    if (table.isEmpty()) {
    +      return "[ empty table ]";
    +    }
    +
    +    List<String> columnNames = Lists.newArrayList(table.keySet());
    +    Collections.sort(columnNames);
    +    int numRecords = table.get(columnNames.get(0)).size();
    +
    +    StringBuilder sb = new StringBuilder();
    +
    +    {
    +      sb.append("[ ");
    +      String separator = "";
    +
    +      for (String columnName : columnNames) {
    +        sb.append(separator);
    +        separator = ", ";
    +        sb.append(columnName);
    +      }
    +
    +      sb.append(" ]\n");
    +    }
    +
    +    for (int recordIndex = 0; recordIndex < numRecords; recordIndex++) {
    +      sb.append("{");
    +      String separator = "";
    +
    +      for (String columnName : columnNames) {
    +        sb.append(separator);
    +        separator = ", ";
    +        sb.append(table.get(columnName).get(recordIndex));
    +      }
    +
    +      sb.append("}\n");
    +    }
    +
    +    return sb.toString();
    +  }
    +
    +  public static void assertEqual(Map<String, List<Object>> expected, Map<String, List<Object>> actual) {
    +    if (expected.isEmpty() && actual.isEmpty()) {
    +      // both tables are empty
    +      return;
    +    }
    +
    +    if (!expected.keySet().equals(actual.keySet())) {
    +      List<String> columnNamesExpected = Lists.newArrayList(expected.keySet());
    +      List<String> columnNamesActual = Lists.newArrayList(actual.keySet());
    +
    +      Collections.sort(columnNamesExpected);
    +      Collections.sort(columnNamesActual);
    +
    +      String message = String.format("The columns in the expected table %s don't match the columns in actual table %s",
    +        columnNamesExpected.toString(), columnNamesActual.toString());
    +      Assert.fail(message);
    +    }
    +
    +    List<String> columnNames = Lists.newArrayList(expected.keySet());
    +    Collections.sort(columnNames);
    +
    +    int numRows = expected.get(columnNames.get(0)).size();
    +
    +    checkTableRowCount(expected, numRows, "expected");
    +    checkTableRowCount(actual, numRows, "actual");
    +
    +    if (numRows == 0) {
    +      // The tables are empty
    +      return;
    +    }
    +
    +    // The tables are non-empty
    +
    +    // Validate each row of the two tables are equal
    +    for (int rowIndex = 0; rowIndex < numRows; rowIndex++) {
    +      for (String columnName: columnNames) {
    +        Object expectedObject = expected.get(columnName).get(rowIndex);
    +        Object actualObject = actual.get(columnName).get(rowIndex);
    +        compareValuesErrorOnMismatch(expectedObject, actualObject, rowIndex, columnName);
    +      }
    +    }
    +  }
    +
    +  public static boolean compareValuesErrorOnMismatch(Object expected, Object actual, int counter, String column) {
    +    if (compareValues(expected, actual)) {
    +      return true;
    +    }
    +
    +    if (expected == null) {
    +      String message = String.format("at row %s column '%s' mismatched values, expected: null " +
    +        "but received %s (%s)", counter, column, actual, actual.getClass().getSimpleName());
    +      Assert.fail(message);
    +    }
    +
    +    if (actual == null) {
    +      String message = String.format("unexpected null at row %s column '%s' should have been: %s", counter, column, expected);
    +      Assert.fail(message);
    +    }
    +
    +    if (actual instanceof byte[]) {
    +      try {
    +        String message = String.format("at row %s column '%s' mismatched values, expected: %s but received %s",
    +          counter, column, new String((byte[])expected, "UTF-8"), new String((byte[])actual, "UTF-8"));
    +        Assert.fail(message);
    +      } catch (UnsupportedEncodingException e) {
    +        throw new RuntimeException("This should never happen", e);
    +      }
    +    }
    +
    +    if (!expected.equals(actual)) {
    +      String message = String.format("at row %s column '%s' mismatched values, expected: %s (%s) but received %s (%s)",
    +        counter, column, expected, expected.getClass().getSimpleName(), actual, actual.getClass().getSimpleName());
    +      Assert.fail(message);
    +    }
    +    return true;
    +  }
    +
    +  public static boolean compareValues(Object expected, Object actual) {
    +    if (expected == null) {
    +      if (actual == null) {
    +        return true;
    +      } else {
    +        return false;
    +      }
    +    }
    +
    +    if (actual == null) {
    +      return false;
    +    }
    +
    +    if (actual instanceof byte[]) {
    +      if ( ! Arrays.equals((byte[]) expected, (byte[]) actual)) {
    +        return false;
    +      } else {
    +        return true;
    +      }
    +    }
    +
    +    if (!expected.equals(actual)) {
    +      return false;
    +    }
    +
    +    return true;
    +  }
    +
    +  private static void checkTableRowCount(Map<String, List<Object>> table, int numRows, String tableName) {
    +    for (Map.Entry<String, List<Object>> entry: table.entrySet()) {
    +      List<Object> data = entry.getValue();
    +
    +      if (numRows != data.size()) {
    +        String message = String.format("Table %s does not have %s rows. Instead it has %s rows",
    +          tableName, numRows, data.size());
    +        Assert.fail(message);
    +      }
    +    }
    +  }
    +
    +  public static class ObjectComparator implements Comparator<Object> {
    --- End diff --
    
    I looked at RowSetComparison. It had code for testing equality, but not for ordering (less than, greater than) which is what I needed for my test. I've moved this class into RowSetComparison without any changes for now. We need to modify DrillTestWrapper to use the RowSet classes first, then we can make ObjectComparator to be something native to RowSets.


---