Java Code Examples for org.dbunit.dataset.ITable#getRowCount()

The following examples show how to use org.dbunit.dataset.ITable#getRowCount() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: KylinTestBase.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
protected static void printResult(ITable resultTable) throws DataSetException {
    StringBuilder sb = new StringBuilder();

    int columnCount = resultTable.getTableMetaData().getColumns().length;
    String[] columns = new String[columnCount];

    for (int i = 0; i < columnCount; i++) {
        sb.append(resultTable.getTableMetaData().getColumns()[i].getColumnName());
        sb.append("-");
        sb.append(resultTable.getTableMetaData().getColumns()[i].getDataType());
        sb.append("\t");
        columns[i] = resultTable.getTableMetaData().getColumns()[i].getColumnName();
    }
    sb.append("\n");

    for (int i = 0; i < resultTable.getRowCount(); i++) {
        for (int j = 0; j < columns.length; j++) {
            sb.append(resultTable.getValue(i, columns[j]));
            sb.append("\t");
        }
        sb.append("\n");
    }
    System.out.println(sb.toString());
}
 
Example 2
Source File: HackedDbUnitAssert.java    From kylin-on-parquet-v2 with Apache License 2.0 6 votes vote down vote up
private void compareDataContains(ITable expectedTable, ITable actualTable, ComparisonColumn[] comparisonCols, FailureHandler failureHandler) throws DataSetException {
    logger.debug("compareData(expectedTable={}, actualTable={}, " + "comparisonCols={}, failureHandler={}) - start", new Object[] { expectedTable, actualTable, comparisonCols, failureHandler });

    if (expectedTable == null) {
        throw new NullPointerException("The parameter 'expectedTable' must not be null");
    }
    if (actualTable == null) {
        throw new NullPointerException("The parameter 'actualTable' must not be null");
    }
    if (comparisonCols == null) {
        throw new NullPointerException("The parameter 'comparisonCols' must not be null");
    }
    if (failureHandler == null) {
        throw new NullPointerException("The parameter 'failureHandler' must not be null");
    }

    for (int index = 0; index < actualTable.getRowCount(); index++) {
        if (!findRowInExpectedTable(expectedTable, actualTable, comparisonCols, failureHandler, index)) {
            throw new IllegalStateException();
        }
    }

}
 
Example 3
Source File: KylinTestBase.java    From kylin with Apache License 2.0 6 votes vote down vote up
protected static void printResult(ITable resultTable) throws DataSetException {
    StringBuilder sb = new StringBuilder();

    int columnCount = resultTable.getTableMetaData().getColumns().length;
    String[] columns = new String[columnCount];

    for (int i = 0; i < columnCount; i++) {
        sb.append(resultTable.getTableMetaData().getColumns()[i].getColumnName());
        sb.append("-");
        sb.append(resultTable.getTableMetaData().getColumns()[i].getDataType());
        sb.append("\t");
        columns[i] = resultTable.getTableMetaData().getColumns()[i].getColumnName();
    }
    sb.append("\n");

    for (int i = 0; i < resultTable.getRowCount(); i++) {
        for (int j = 0; j < columns.length; j++) {
            sb.append(resultTable.getValue(i, columns[j]));
            sb.append("\t");
        }
        sb.append("\n");
    }
    System.out.println(sb.toString());
}
 
Example 4
Source File: HackedDbUnitAssert.java    From kylin with Apache License 2.0 6 votes vote down vote up
private void compareDataContains(ITable expectedTable, ITable actualTable, ComparisonColumn[] comparisonCols, FailureHandler failureHandler) throws DataSetException {
    logger.debug("compareData(expectedTable={}, actualTable={}, " + "comparisonCols={}, failureHandler={}) - start", new Object[] { expectedTable, actualTable, comparisonCols, failureHandler });

    if (expectedTable == null) {
        throw new NullPointerException("The parameter 'expectedTable' must not be null");
    }
    if (actualTable == null) {
        throw new NullPointerException("The parameter 'actualTable' must not be null");
    }
    if (comparisonCols == null) {
        throw new NullPointerException("The parameter 'comparisonCols' must not be null");
    }
    if (failureHandler == null) {
        throw new NullPointerException("The parameter 'failureHandler' must not be null");
    }

    for (int index = 0; index < actualTable.getRowCount(); index++) {
        if (!findRowInExpectedTable(expectedTable, actualTable, comparisonCols, failureHandler, index)) {
            throw new IllegalStateException();
        }
    }

}
 
Example 5
Source File: KylinTestBase.java    From Kylin with Apache License 2.0 6 votes vote down vote up
protected static void printResult(ITable resultTable) throws DataSetException {
    StringBuilder sb = new StringBuilder();

    int columnCount = resultTable.getTableMetaData().getColumns().length;
    String[] columns = new String[columnCount];

    for (int i = 0; i < columnCount; i++) {
        sb.append(resultTable.getTableMetaData().getColumns()[i].getColumnName());
        sb.append("-");
        sb.append(resultTable.getTableMetaData().getColumns()[i].getDataType());
        sb.append("\t");
        columns[i] = resultTable.getTableMetaData().getColumns()[i].getColumnName();
    }
    sb.append("\n");

    for (int i = 0; i < resultTable.getRowCount(); i++) {
        for (int j = 0; j < columns.length; j++) {
            sb.append(resultTable.getValue(i, columns[j]));
            sb.append("\t");
        }
        sb.append("\n");
    }
    System.out.println(sb.toString());
}
 
Example 6
Source File: KylinTestBase.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
protected void execAndCompResultSize(String queryFolder, String[] exclusiveQuerys, boolean needSort)
        throws Exception {
    logger.info("---------- test folder: " + queryFolder);
    Set<String> exclusiveSet = buildExclusiveSet(exclusiveQuerys);

    List<File> sqlFiles = getFilesFromFolder(new File(queryFolder), ".sql");
    for (File sqlFile : sqlFiles) {
        String queryName = StringUtils.split(sqlFile.getName(), '.')[0];
        if (exclusiveSet.contains(queryName)) {
            continue;
        }
        String sql = getTextFromFile(sqlFile);

        // execute Kylin
        logger.info("Query Result from Kylin - " + queryName + "  (" + queryFolder + ")");
        IDatabaseConnection kylinConn = new DatabaseConnection(cubeConnection);
        ITable kylinTable = executeQuery(kylinConn, queryName, sql, needSort);

        // execute H2
        logger.info("Query Result from H2 - " + queryName);
        ITable h2Table = executeQuery(newH2Connection(), queryName, sql, needSort);

        try {
            // compare the result
            Assert.assertEquals(h2Table.getRowCount(), kylinTable.getRowCount());
        } catch (Throwable t) {
            logger.info("execAndCompResultSize failed on: " + sqlFile.getAbsolutePath());
            throw t;
        }

        compQueryCount++;
        if (kylinTable.getRowCount() == 0) {
            zeroResultQueries.add(sql);
        }
    }
}
 
Example 7
Source File: KylinTestBase.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
protected void execLimitAndValidate(String queryFolder) throws Exception {
    logger.info("---------- test folder: " + new File(queryFolder).getAbsolutePath());

    int appendLimitQueries = 0;
    List<File> sqlFiles = getFilesFromFolder(new File(queryFolder), ".sql");
    for (File sqlFile : sqlFiles) {
        String queryName = StringUtils.split(sqlFile.getName(), '.')[0];
        String sql = getTextFromFile(sqlFile);

        String sqlWithLimit;
        if (sql.toLowerCase(Locale.ROOT).contains("limit ")) {
            sqlWithLimit = sql;
        } else {
            sqlWithLimit = sql + " limit 5";
            appendLimitQueries++;
        }

        // execute Kylin
        logger.info("Query Result from Kylin - " + queryName + "  (" + queryFolder + ")");
        IDatabaseConnection kylinConn = new DatabaseConnection(cubeConnection);
        ITable kylinTable = executeQuery(kylinConn, queryName, sqlWithLimit, false);

        // execute H2
        logger.info("Query Result from H2 - " + queryName);
        ITable h2Table = executeQuery(newH2Connection(), queryName, sql, false);

        try {
            assertTableContains(h2Table, kylinTable);
        } catch (Throwable t) {
            logger.info("execAndCompQuery failed on: " + sqlFile.getAbsolutePath());
            throw t;
        }

        compQueryCount++;
        if (kylinTable.getRowCount() == 0) {
            zeroResultQueries.add(sql);
        }
    }
    logger.info("Queries appended with limit: " + appendLimitQueries);
}
 
Example 8
Source File: KylinTestBase.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
protected void execAndCompQuery(String queryFolder, String[] exclusiveQuerys, boolean needSort,
        ICompareQueryTranslator translator) throws Exception {
    logger.info("---------- test folder: " + new File(queryFolder).getAbsolutePath());
    Set<String> exclusiveSet = buildExclusiveSet(exclusiveQuerys);

    List<File> sqlFiles = getFilesFromFolder(new File(queryFolder), ".sql");
    for (File sqlFile : sqlFiles) {
        String queryName = StringUtils.split(sqlFile.getName(), '.')[0];
        if (exclusiveSet.contains(queryName)) {
            continue;
        }
        String sql1 = getTextFromFile(sqlFile);
        String sql2 = translator.transform(sqlFile);

        // execute Kylin
        logger.info("Query Result from Kylin - " + queryName + "  (" + queryFolder + ")");
        IDatabaseConnection kylinConn = new DatabaseConnection(cubeConnection);
        ITable kylinTable = executeQuery(kylinConn, queryName, sql1, needSort);

        // execute H2
        logger.info("Query Result from H2 - " + queryName);
        long currentTime = System.currentTimeMillis();
        ITable h2Table = executeQuery(newH2Connection(), queryName, sql2, needSort);
        logger.info("H2 spent " + (System.currentTimeMillis() - currentTime) + " mili-seconds.");

        try {
            // compare the result
            assertTableEquals(h2Table, kylinTable);
        } catch (Throwable t) {
            logger.info("execAndCompQuery failed on: " + sqlFile.getAbsolutePath());
            throw t;
        }

        compQueryCount++;
        if (kylinTable.getRowCount() == 0) {
            zeroResultQueries.add(sql1);
        }
    }
}
 
Example 9
Source File: KylinTestBase.java    From kylin with Apache License 2.0 5 votes vote down vote up
protected void execAndCompResultSize(String queryFolder, String[] exclusiveQuerys, boolean needSort)
        throws Exception {
    logger.info("---------- test folder: " + queryFolder);
    Set<String> exclusiveSet = buildExclusiveSet(exclusiveQuerys);

    List<File> sqlFiles = getFilesFromFolder(new File(queryFolder), ".sql");
    for (File sqlFile : sqlFiles) {
        String queryName = StringUtils.split(sqlFile.getName(), '.')[0];
        if (exclusiveSet.contains(queryName)) {
            continue;
        }
        String sql = getTextFromFile(sqlFile);

        // execute Kylin
        logger.info("Query Result from Kylin - " + queryName + "  (" + queryFolder + ")");
        IDatabaseConnection kylinConn = new DatabaseConnection(cubeConnection);
        ITable kylinTable = executeQuery(kylinConn, queryName, sql, needSort);

        // execute H2
        logger.info("Query Result from H2 - " + queryName);
        ITable h2Table = executeQuery(newH2Connection(), queryName, sql, needSort);

        try {
            // compare the result
            Assert.assertEquals(h2Table.getRowCount(), kylinTable.getRowCount());
        } catch (Throwable t) {
            logger.info("execAndCompResultSize failed on: " + sqlFile.getAbsolutePath());
            throw t;
        }

        compQueryCount++;
        if (kylinTable.getRowCount() == 0) {
            zeroResultQueries.add(sql);
        }
    }
}
 
Example 10
Source File: KylinTestBase.java    From kylin with Apache License 2.0 5 votes vote down vote up
protected void execLimitAndValidate(String queryFolder) throws Exception {
    logger.info("---------- test folder: " + new File(queryFolder).getAbsolutePath());

    int appendLimitQueries = 0;
    List<File> sqlFiles = getFilesFromFolder(new File(queryFolder), ".sql");
    for (File sqlFile : sqlFiles) {
        String queryName = StringUtils.split(sqlFile.getName(), '.')[0];
        String sql = getTextFromFile(sqlFile);

        String sqlWithLimit;
        if (sql.toLowerCase(Locale.ROOT).contains("limit ")) {
            sqlWithLimit = sql;
        } else {
            sqlWithLimit = sql + " limit 5";
            appendLimitQueries++;
        }

        // execute Kylin
        logger.info("Query Result from Kylin - " + queryName + "  (" + queryFolder + ")");
        IDatabaseConnection kylinConn = new DatabaseConnection(cubeConnection);
        ITable kylinTable = executeQuery(kylinConn, queryName, sqlWithLimit, false);

        // execute H2
        logger.info("Query Result from H2 - " + queryName);
        ITable h2Table = executeQuery(newH2Connection(), queryName, sql, false);

        try {
            assertTableContains(h2Table, kylinTable);
        } catch (Throwable t) {
            logger.info("execAndCompQuery failed on: " + sqlFile.getAbsolutePath());
            throw t;
        }

        compQueryCount++;
        if (kylinTable.getRowCount() == 0) {
            zeroResultQueries.add(sql);
        }
    }
    logger.info("Queries appended with limit: " + appendLimitQueries);
}
 
Example 11
Source File: KylinTestBase.java    From kylin with Apache License 2.0 5 votes vote down vote up
protected void execAndCompQuery(String queryFolder, String[] exclusiveQuerys, boolean needSort,
        ICompareQueryTranslator translator) throws Exception {
    logger.info("---------- test folder: " + new File(queryFolder).getAbsolutePath());
    Set<String> exclusiveSet = buildExclusiveSet(exclusiveQuerys);

    List<File> sqlFiles = getFilesFromFolder(new File(queryFolder), ".sql");
    for (File sqlFile : sqlFiles) {
        String queryName = StringUtils.split(sqlFile.getName(), '.')[0];
        if (exclusiveSet.contains(queryName)) {
            continue;
        }
        String sql1 = getTextFromFile(sqlFile);
        String sql2 = translator.transform(sqlFile);

        // execute Kylin
        logger.info("Query Result from Kylin - " + queryName + "  (" + queryFolder + ")");
        IDatabaseConnection kylinConn = new DatabaseConnection(cubeConnection);
        ITable kylinTable = executeQuery(kylinConn, queryName, sql1, needSort);

        // execute H2
        logger.info("Query Result from H2 - " + queryName);
        long currentTime = System.currentTimeMillis();
        ITable h2Table = executeQuery(newH2Connection(), queryName, sql2, needSort);
        logger.info("H2 spent " + (System.currentTimeMillis() - currentTime) + " mili-seconds.");

        try {
            // compare the result
            assertTableEquals(h2Table, kylinTable);
        } catch (Throwable t) {
            logger.info("execAndCompQuery failed on: " + sqlFile.getAbsolutePath());
            throw t;
        }

        compQueryCount++;
        if (kylinTable.getRowCount() == 0) {
            zeroResultQueries.add(sql1);
        }
    }
}
 
Example 12
Source File: KylinTestBase.java    From Kylin with Apache License 2.0 5 votes vote down vote up
protected void execAndCompQuery(String queryFolder, String[] exclusiveQuerys, boolean needSort) throws Exception {
    printInfo("---------- test folder: " + queryFolder);
    Set<String> exclusiveSet = buildExclusiveSet(exclusiveQuerys);

    List<File> sqlFiles = getFilesFromFolder(new File(queryFolder), ".sql");
    for (File sqlFile : sqlFiles) {
        String queryName = StringUtils.split(sqlFile.getName(), '.')[0];
        if (exclusiveSet.contains(queryName)) {
            continue;
        }
        String sql = getTextFromFile(sqlFile);

        // execute Kylin
        printInfo("Query Result from Kylin - " + queryName + "  (" + queryFolder + ")");
        IDatabaseConnection kylinConn = new DatabaseConnection(cubeConnection);
        ITable kylinTable = executeQuery(kylinConn, queryName, sql, needSort);

        // execute H2
        printInfo("Query Result from H2 - " + queryName);
        H2Connection h2Conn = new H2Connection(h2Connection, null);
        h2Conn.getConfig().setProperty(DatabaseConfig.PROPERTY_DATATYPE_FACTORY, new TestH2DataTypeFactory());
        ITable h2Table = executeQuery(h2Conn, queryName, sql, needSort);

        // compare the result
        Assertion.assertEquals(h2Table, kylinTable);

        compQueryCount++;
        if (kylinTable.getRowCount() == 0) {
            zeroResultQueries.add(sql);
        }
    }
}
 
Example 13
Source File: KylinTestBase.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
protected void execAndCompPlan(String queryFolder, String[] exclusiveQuerys, boolean needSort,
        ICompareQueryTranslator translator) throws Exception {
    logger.info("---------- test folder: " + new File(queryFolder).getAbsolutePath());
    Set<String> exclusiveSet = buildExclusiveSet(exclusiveQuerys);

    List<File> sqlFiles = getFilesFromFolder(new File(queryFolder), ".sql");
    for (File sqlFile : sqlFiles) {
        String queryName = StringUtils.split(sqlFile.getName(), '.')[0];
        if (exclusiveSet.contains(queryName)) {
            continue;
        }
        String sql1 = getTextFromFile(sqlFile);
        String sql2 = translator.transform(sqlFile);

        // execute Kylin
        logger.info("Query Result from Kylin - " + queryName + "  (" + queryFolder + ")");
        IDatabaseConnection kylinConn = new DatabaseConnection(cubeConnection);
        ITable kylinTable = executeQuery(kylinConn, queryName, sql1, needSort);
        RelNode calcitePlan = (RelNode) QueryContextFacade.current().getCalcitePlan();
        if (calcitePlan == null)
            throw new NullPointerException();

        // execute H2
        logger.info("Query Result from H2 - " + queryName);
        long currentTime = System.currentTimeMillis();
        ITable h2Table = executeQuery(newH2Connection(), queryName, sql2, needSort);
        logger.info("H2 spent " + (System.currentTimeMillis() - currentTime) + " mili-seconds.");

        try {
            // compare the result
            assertTableEquals(h2Table, kylinTable);
        } catch (Throwable t) {
            logger.info("execAndCompQuery failed on: " + sqlFile.getAbsolutePath());
            throw t;
        }

        RelToSqlConverter converter = new RelToSqlConverter(CALCITE);
        SqlNode sqlNode = converter.visitChild(0, calcitePlan.getInput(0)).asStatement();
        String optimizedSQL = sqlNode.toSqlString(CALCITE).getSql();
        String expectedSQL = Strings.join(Files.readLines(
                new File(sqlFile.getParent(), sqlFile.getName() + ".expected"), Charset.forName("utf-8")), "\n");
        Assert.assertEquals(expectedSQL, optimizedSQL);
        compQueryCount++;
        if (kylinTable.getRowCount() == 0) {
            zeroResultQueries.add(sql1);
        }
    }
}
 
Example 14
Source File: HackedDbUnitAssert.java    From kylin-on-parquet-v2 with Apache License 2.0 4 votes vote down vote up
private boolean findRowInExpectedTable(ITable expectedTable, ITable actualTable, ComparisonColumn[] comparisonCols, FailureHandler failureHandler, int index) throws DataSetException {

        // iterate over all rows
        for (int i = 0; i < expectedTable.getRowCount(); i++) {

            // iterate over all columns of the current row
            for (int j = 0; j < comparisonCols.length; j++) {
                ComparisonColumn compareColumn = comparisonCols[j];

                String columnName = compareColumn.getColumnName();
                DataType dataType = compareColumn.getDataType();

                Object expectedValue = expectedTable.getValue(i, columnName);
                Object actualValue = actualTable.getValue(index, columnName);

                // Compare the values
                if (skipCompare(columnName, expectedValue, actualValue)) {
                    if (logger.isTraceEnabled()) {
                        logger.trace("ignoring comparison " + expectedValue + "=" + actualValue + " on column " + columnName);
                    }
                    continue;
                }

                if (dataType.compare(expectedValue, actualValue) != 0) {
                    break;

                    //                    Difference diff = new Difference(expectedTable, actualTable, i, columnName, expectedValue, actualValue);
                    //
                    //                    // Handle the difference (throw error immediately or something else)
                    //                    failureHandler.handle(diff);
                } else {
                    if (j == comparisonCols.length - 1) {
                        return true;
                    } else {
                        continue;
                    }
                }
            }
        }
        return false;
    }
 
Example 15
Source File: KylinTestBase.java    From kylin with Apache License 2.0 4 votes vote down vote up
protected void execAndCompPlan(String queryFolder, String[] exclusiveQuerys, boolean needSort,
        ICompareQueryTranslator translator) throws Exception {
    logger.info("---------- test folder: " + new File(queryFolder).getAbsolutePath());
    Set<String> exclusiveSet = buildExclusiveSet(exclusiveQuerys);

    List<File> sqlFiles = getFilesFromFolder(new File(queryFolder), ".sql");
    for (File sqlFile : sqlFiles) {
        String queryName = StringUtils.split(sqlFile.getName(), '.')[0];
        if (exclusiveSet.contains(queryName)) {
            continue;
        }
        String sql1 = getTextFromFile(sqlFile);
        String sql2 = translator.transform(sqlFile);

        // execute Kylin
        logger.info("Query Result from Kylin - " + queryName + "  (" + queryFolder + ")");
        IDatabaseConnection kylinConn = new DatabaseConnection(cubeConnection);
        ITable kylinTable = executeQuery(kylinConn, queryName, sql1, needSort);
        RelNode calcitePlan = (RelNode) QueryContextFacade.current().getCalcitePlan();
        if (calcitePlan == null)
            throw new NullPointerException();

        // execute H2
        logger.info("Query Result from H2 - " + queryName);
        long currentTime = System.currentTimeMillis();
        ITable h2Table = executeQuery(newH2Connection(), queryName, sql2, needSort);
        logger.info("H2 spent " + (System.currentTimeMillis() - currentTime) + " mili-seconds.");

        try {
            // compare the result
            assertTableEquals(h2Table, kylinTable);
        } catch (Throwable t) {
            logger.info("execAndCompQuery failed on: " + sqlFile.getAbsolutePath());
            throw t;
        }

        RelToSqlConverter converter = new RelToSqlConverter(CALCITE);
        SqlNode sqlNode = converter.visitChild(0, calcitePlan.getInput(0)).asStatement();
        String optimizedSQL = sqlNode.toSqlString(CALCITE).getSql();
        String expectedSQL = Strings.join(Files.readLines(
                new File(sqlFile.getParent(), sqlFile.getName() + ".expected"), Charset.forName("utf-8")), "\n");
        Assert.assertEquals(expectedSQL, optimizedSQL);
        compQueryCount++;
        if (kylinTable.getRowCount() == 0) {
            zeroResultQueries.add(sql1);
        }
    }
}
 
Example 16
Source File: HackedDbUnitAssert.java    From kylin with Apache License 2.0 4 votes vote down vote up
private boolean findRowInExpectedTable(ITable expectedTable, ITable actualTable, ComparisonColumn[] comparisonCols, FailureHandler failureHandler, int index) throws DataSetException {

        // iterate over all rows
        for (int i = 0; i < expectedTable.getRowCount(); i++) {

            // iterate over all columns of the current row
            for (int j = 0; j < comparisonCols.length; j++) {
                ComparisonColumn compareColumn = comparisonCols[j];

                String columnName = compareColumn.getColumnName();
                DataType dataType = compareColumn.getDataType();

                Object expectedValue = expectedTable.getValue(i, columnName);
                Object actualValue = actualTable.getValue(index, columnName);

                // Compare the values
                if (skipCompare(columnName, expectedValue, actualValue)) {
                    if (logger.isTraceEnabled()) {
                        logger.trace("ignoring comparison " + expectedValue + "=" + actualValue + " on column " + columnName);
                    }
                    continue;
                }

                if (dataType.compare(expectedValue, actualValue) != 0) {
                    break;

                    //                    Difference diff = new Difference(expectedTable, actualTable, i, columnName, expectedValue, actualValue);
                    //
                    //                    // Handle the difference (throw error immediately or something else)
                    //                    failureHandler.handle(diff);
                } else {
                    if (j == comparisonCols.length - 1) {
                        return true;
                    } else {
                        continue;
                    }
                }
            }
        }
        return false;
    }