Search in sources :

Example 1 with Hook

use of org.apache.calcite.runtime.Hook in project calcite by apache.

the class CalciteAssert method assertQuery.

static void assertQuery(Connection connection, String sql, int limit, boolean materializationsEnabled, List<Pair<Hook, Function>> hooks, Function<ResultSet, Void> resultChecker, Function<Integer, Void> updateChecker, Function<Throwable, Void> exceptionChecker) throws Exception {
    final String message = "With materializationsEnabled=" + materializationsEnabled + ", limit=" + limit;
    try (final Closer closer = new Closer()) {
        if (connection instanceof CalciteConnection) {
            CalciteConnection calciteConnection = (CalciteConnection) connection;
            calciteConnection.getProperties().setProperty(CalciteConnectionProperty.MATERIALIZATIONS_ENABLED.camelName(), Boolean.toString(materializationsEnabled));
            calciteConnection.getProperties().setProperty(CalciteConnectionProperty.CREATE_MATERIALIZATIONS.camelName(), Boolean.toString(materializationsEnabled));
            if (!calciteConnection.getProperties().containsKey(CalciteConnectionProperty.TIME_ZONE.camelName())) {
                // Do not override id some test has already set this property.
                calciteConnection.getProperties().setProperty(CalciteConnectionProperty.TIME_ZONE.camelName(), DateTimeUtils.UTC_ZONE.getID());
            }
        }
        for (Pair<Hook, Function> hook : hooks) {
            closer.add(hook.left.addThread(hook.right));
        }
        Statement statement = connection.createStatement();
        statement.setMaxRows(limit <= 0 ? limit : Math.max(limit, 1));
        ResultSet resultSet = null;
        Integer updateCount = null;
        try {
            if (updateChecker == null) {
                resultSet = statement.executeQuery(sql);
            } else {
                updateCount = statement.executeUpdate(sql);
            }
            if (exceptionChecker != null) {
                exceptionChecker.apply(null);
                return;
            }
        } catch (Exception | Error e) {
            if (exceptionChecker != null) {
                exceptionChecker.apply(e);
                return;
            }
            throw e;
        }
        if (resultChecker != null) {
            resultChecker.apply(resultSet);
        }
        if (updateChecker != null) {
            updateChecker.apply(updateCount);
        }
        if (resultSet != null) {
            resultSet.close();
        }
        statement.close();
        connection.close();
    } catch (Error | RuntimeException e) {
        // at the very top level of the exception stack.
        throw e;
    } catch (Throwable e) {
        throw new RuntimeException(message, e);
    }
}
Also used : Closer(org.apache.calcite.util.Closer) Hook(org.apache.calcite.runtime.Hook) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) InvocationTargetException(java.lang.reflect.InvocationTargetException) CalciteException(org.apache.calcite.runtime.CalciteException) SQLException(java.sql.SQLException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) SqlValidatorException(org.apache.calcite.sql.validate.SqlValidatorException) ExecutionException(java.util.concurrent.ExecutionException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Function(com.google.common.base.Function) ResultSet(java.sql.ResultSet) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

Aggregations

Function (com.google.common.base.Function)1 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 ExecutionException (java.util.concurrent.ExecutionException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)1 CalciteException (org.apache.calcite.runtime.CalciteException)1 Hook (org.apache.calcite.runtime.Hook)1 SqlValidatorException (org.apache.calcite.sql.validate.SqlValidatorException)1 Closer (org.apache.calcite.util.Closer)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1