Search in sources :

Example 1 with Closer

use of org.apache.calcite.util.Closer in project calcite by apache.

the class QuidemTest method checkRun.

protected void checkRun(String path) throws Exception {
    final File inFile;
    final File outFile;
    final File f = new File(path);
    if (f.isAbsolute()) {
        // e.g. path = "/tmp/foo.iq"
        inFile = f;
        outFile = new File(path + ".out");
    } else {
        // e.g. path = "sql/outer.iq"
        // inUrl = "file:/home/fred/calcite/core/target/test-classes/sql/outer.iq"
        final URL inUrl = JdbcTest.class.getResource("/" + n2u(path));
        String x = u2n(inUrl.getFile());
        assert x.endsWith(path) : "x: " + x + "; path: " + path;
        x = x.substring(0, x.length() - path.length());
        assert x.endsWith(u2n("/test-classes/"));
        x = x.substring(0, x.length() - u2n("/test-classes/").length());
        final File base = new File(x);
        inFile = new File(base, u2n("/test-classes/") + path);
        outFile = new File(base, u2n("/surefire/") + path);
    }
    Util.discard(outFile.getParentFile().mkdirs());
    try (final Reader reader = Util.reader(inFile);
        final Writer writer = Util.printWriter(outFile);
        final Closer closer = new Closer()) {
        new Quidem(reader, writer, env(), createConnectionFactory()).withPropertyHandler(new Quidem.PropertyHandler() {

            public void onSet(String propertyName, Object value) {
                if (propertyName.equals("bindable")) {
                    final boolean b = value instanceof Boolean && (Boolean) value;
                    closer.add(Hook.ENABLE_BINDABLE.addThread(Hook.property(b)));
                }
                if (propertyName.equals("expand")) {
                    final boolean b = value instanceof Boolean && (Boolean) value;
                    closer.add(Prepare.THREAD_EXPAND.push(b));
                }
            }
        }).execute();
    }
    final String diff = DiffTestCase.diff(inFile, outFile);
    if (!diff.isEmpty()) {
        fail("Files differ: " + outFile + " " + inFile + "\n" + diff);
    }
}
Also used : Closer(org.apache.calcite.util.Closer) Reader(java.io.Reader) File(java.io.File) Quidem(net.hydromatic.quidem.Quidem) URL(java.net.URL) Writer(java.io.Writer)

Example 2 with Closer

use of org.apache.calcite.util.Closer 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)

Example 3 with Closer

use of org.apache.calcite.util.Closer in project calcite by apache.

the class CalciteAssert method assertPrepare.

static void assertPrepare(Connection connection, String sql, boolean materializationsEnabled, final Function<RelNode, Void> convertChecker, final Function<RelNode, Void> substitutionChecker) throws Exception {
    final String message = "With materializationsEnabled=" + materializationsEnabled;
    try (Closer closer = new Closer()) {
        if (convertChecker != null) {
            closer.add(Hook.TRIMMED.addThread(new Function<RelNode, Void>() {

                public Void apply(RelNode rel) {
                    convertChecker.apply(rel);
                    return null;
                }
            }));
        }
        if (substitutionChecker != null) {
            closer.add(Hook.SUB.addThread(new Function<RelNode, Void>() {

                public Void apply(RelNode rel) {
                    substitutionChecker.apply(rel);
                    return null;
                }
            }));
        }
        ((CalciteConnection) connection).getProperties().setProperty(CalciteConnectionProperty.MATERIALIZATIONS_ENABLED.camelName(), Boolean.toString(materializationsEnabled));
        ((CalciteConnection) connection).getProperties().setProperty(CalciteConnectionProperty.CREATE_MATERIALIZATIONS.camelName(), Boolean.toString(materializationsEnabled));
        PreparedStatement statement = connection.prepareStatement(sql);
        statement.close();
        connection.close();
    } catch (Throwable e) {
        throw new RuntimeException(message, e);
    }
}
Also used : Closer(org.apache.calcite.util.Closer) Function(com.google.common.base.Function) RelNode(org.apache.calcite.rel.RelNode) PreparedStatement(java.sql.PreparedStatement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString)

Aggregations

Closer (org.apache.calcite.util.Closer)3 Function (com.google.common.base.Function)2 PreparedStatement (java.sql.PreparedStatement)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)1 File (java.io.File)1 Reader (java.io.Reader)1 Writer (java.io.Writer)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 URL (java.net.URL)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 Quidem (net.hydromatic.quidem.Quidem)1 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)1 RelNode (org.apache.calcite.rel.RelNode)1 CalciteException (org.apache.calcite.runtime.CalciteException)1 Hook (org.apache.calcite.runtime.Hook)1