use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project calcite by apache.
the class JdbcTest method testStatementCloseOnCompletion.
/**
* Tests {@link java.sql.Statement}.{@code closeOnCompletion()}.
*/
@Test
public void testStatementCloseOnCompletion() throws Exception {
String javaVersion = System.getProperty("java.version");
if (javaVersion.compareTo("1.7") < 0) {
// Statement.closeOnCompletion was introduced in JDK 1.7.
return;
}
final Driver driver = new Driver();
CalciteConnection connection = (CalciteConnection) driver.connect("jdbc:calcite:", new Properties());
SchemaPlus rootSchema = connection.getRootSchema();
rootSchema.add("hr", new ReflectiveSchema(new HrSchema()));
connection.setSchema("hr");
final Statement statement = connection.createStatement();
assertFalse((Boolean) CalciteAssert.call(statement, "isCloseOnCompletion"));
CalciteAssert.call(statement, "closeOnCompletion");
assertTrue((Boolean) CalciteAssert.call(statement, "isCloseOnCompletion"));
final ResultSet resultSet = statement.executeQuery("select * from \"emps\"");
assertFalse(resultSet.isClosed());
assertFalse(statement.isClosed());
assertFalse(connection.isClosed());
// when result set is closed, statement is closed automatically
resultSet.close();
assertTrue(resultSet.isClosed());
assertTrue(statement.isClosed());
assertFalse(connection.isClosed());
connection.close();
assertTrue(resultSet.isClosed());
assertTrue(statement.isClosed());
assertTrue(connection.isClosed());
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project calcite by apache.
the class JdbcTest method testDifferentTypesSameFields.
/**
* Test case for bug where if two tables have different element classes
* but those classes have identical fields, Calcite would generate code to use
* the wrong element class; a {@link ClassCastException} would ensue.
*/
@Test
public void testDifferentTypesSameFields() throws Exception {
Connection connection = DriverManager.getConnection("jdbc:calcite:");
CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
final SchemaPlus rootSchema = calciteConnection.getRootSchema();
rootSchema.add("TEST", new ReflectiveSchema(new MySchema()));
Statement statement = calciteConnection.createStatement();
ResultSet resultSet = statement.executeQuery("SELECT \"myvalue\" from TEST.\"mytable2\"");
assertEquals("myvalue=2\n", CalciteAssert.toString(resultSet));
resultSet.close();
statement.close();
connection.close();
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project calcite by apache.
the class JdbcTest method adviseSql.
private void adviseSql(String sql, Function<ResultSet, Void> checker) throws ClassNotFoundException, SQLException {
Properties info = new Properties();
info.put("lex", "JAVA");
info.put("quoting", "DOUBLE_QUOTE");
Connection connection = DriverManager.getConnection("jdbc:calcite:", info);
CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
SchemaPlus rootSchema = calciteConnection.getRootSchema();
rootSchema.add("hr", new ReflectiveSchema(new HrSchema()));
SchemaPlus schema = rootSchema.add("s", new AbstractSchema());
calciteConnection.setSchema("hr");
final TableFunction table = new SqlAdvisorGetHintsFunction();
schema.add("get_hints", table);
PreparedStatement ps = connection.prepareStatement("select *\n" + "from table(\"s\".\"get_hints\"(?, ?)) as t(id, names, type)");
SqlParserUtil.StringAndPos sap = SqlParserUtil.findPos(sql);
ps.setString(1, sap.sql);
ps.setInt(2, sap.cursor);
final ResultSet resultSet = ps.executeQuery();
checker.apply(resultSet);
resultSet.close();
connection.close();
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project calcite by apache.
the class JdbcTest method testPrimitiveColumnsWithNullValues.
/**
* Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-596">[CALCITE-596]
* JDBC adapter incorrectly reads null values as 0</a>.
*/
@Test
public void testPrimitiveColumnsWithNullValues() throws Exception {
String hsqldbMemUrl = "jdbc:hsqldb:mem:.";
Connection baseConnection = DriverManager.getConnection(hsqldbMemUrl);
Statement baseStmt = baseConnection.createStatement();
baseStmt.execute("CREATE TABLE T1 (\n" + "ID INTEGER,\n" + "VALS DOUBLE)");
baseStmt.execute("INSERT INTO T1 VALUES (1, 1.0)");
baseStmt.execute("INSERT INTO T1 VALUES (2, null)");
baseStmt.execute("INSERT INTO T1 VALUES (null, 2.0)");
baseStmt.close();
baseConnection.commit();
Properties info = new Properties();
info.put("model", "inline:" + "{\n" + " version: '1.0',\n" + " defaultSchema: 'BASEJDBC',\n" + " schemas: [\n" + " {\n" + " type: 'jdbc',\n" + " name: 'BASEJDBC',\n" + " jdbcDriver: '" + jdbcDriver.class.getName() + "',\n" + " jdbcUrl: '" + hsqldbMemUrl + "',\n" + " jdbcCatalog: null,\n" + " jdbcSchema: null\n" + " }\n" + " ]\n" + "}");
Connection calciteConnection = DriverManager.getConnection("jdbc:calcite:", info);
ResultSet rs = calciteConnection.prepareStatement("select * from t1").executeQuery();
assertThat(rs.next(), is(true));
assertThat((Integer) rs.getObject("ID"), equalTo(1));
assertThat((Double) rs.getObject("VALS"), equalTo(1.0));
assertThat(rs.next(), is(true));
assertThat((Integer) rs.getObject("ID"), equalTo(2));
assertThat(rs.getObject("VALS"), nullValue());
assertThat(rs.next(), is(true));
assertThat(rs.getObject("ID"), nullValue());
assertThat((Double) rs.getObject("VALS"), equalTo(2.0));
rs.close();
calciteConnection.close();
}
use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project calcite by apache.
the class JdbcTest method testCloneSchema.
@Test
public void testCloneSchema() throws ClassNotFoundException, SQLException {
final Connection connection = CalciteAssert.that(CalciteAssert.Config.JDBC_FOODMART).connect();
final CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
final SchemaPlus rootSchema = calciteConnection.getRootSchema();
final SchemaPlus foodmart = rootSchema.getSubSchema("foodmart");
rootSchema.add("foodmart2", new CloneSchema(foodmart));
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery("select count(*) from \"foodmart2\".\"time_by_day\"");
assertTrue(resultSet.next());
assertEquals(730, resultSet.getInt(1));
resultSet.close();
connection.close();
}
Aggregations