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);
}
}
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);
}
}
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);
}
}
Aggregations