Search in sources :

Example 1 with ReflectiveSchema

use of org.apache.calcite.adapter.java.ReflectiveSchema in project calcite by apache.

the class JdbcTest method testAutomaticTemporaryTable.

/**
 * Tests saving query results into temporary tables, per
 * {@link org.apache.calcite.avatica.Handler.ResultSink}.
 */
@Test
public void testAutomaticTemporaryTable() throws Exception {
    final List<Object> objects = new ArrayList<>();
    CalciteAssert.that().with(new CalciteAssert.ConnectionFactory() {

        public CalciteConnection createConnection() throws SQLException {
            CalciteConnection connection = (CalciteConnection) new AutoTempDriver(objects).connect("jdbc:calcite:", new Properties());
            final SchemaPlus rootSchema = connection.getRootSchema();
            rootSchema.add("hr", new ReflectiveSchema(new HrSchema()));
            connection.setSchema("hr");
            return connection;
        }
    }).doWithConnection(new Function<CalciteConnection, Object>() {

        public Object apply(CalciteConnection a0) {
            try {
                a0.createStatement().executeQuery("select * from \"hr\".\"emps\" " + "where \"deptno\" = 10");
                assertEquals(1, objects.size());
                return null;
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    });
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) Properties(java.util.Properties) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 2 with ReflectiveSchema

use of org.apache.calcite.adapter.java.ReflectiveSchema 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());
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) AvaticaStatement(org.apache.calcite.avatica.AvaticaStatement) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ResultSet(java.sql.ResultSet) Driver(org.apache.calcite.jdbc.Driver) org.hsqldb.jdbcDriver(org.hsqldb.jdbcDriver) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Properties(java.util.Properties) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 3 with ReflectiveSchema

use of org.apache.calcite.adapter.java.ReflectiveSchema 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();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) AvaticaStatement(org.apache.calcite.avatica.AvaticaStatement) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) Connection(java.sql.Connection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ResultSet(java.sql.ResultSet) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 4 with ReflectiveSchema

use of org.apache.calcite.adapter.java.ReflectiveSchema 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();
}
Also used : SqlAdvisorGetHintsFunction(org.apache.calcite.sql.advise.SqlAdvisorGetHintsFunction) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) Connection(java.sql.Connection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) SqlParserUtil(org.apache.calcite.sql.parser.SqlParserUtil) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) ResultSet(java.sql.ResultSet) TableFunction(org.apache.calcite.schema.TableFunction) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

Example 5 with ReflectiveSchema

use of org.apache.calcite.adapter.java.ReflectiveSchema in project calcite by apache.

the class MultiJdbcSchemaJoinTest method setup.

private Connection setup() throws SQLException {
    // Create a jdbc database & table
    final String db = TempDb.INSTANCE.getUrl();
    Connection c1 = DriverManager.getConnection(db, "", "");
    Statement stmt1 = c1.createStatement();
    // This is a table we can join with the emps from the hr schema
    stmt1.execute("create table table1(id integer not null primary key, " + "field1 varchar(10))");
    stmt1.execute("insert into table1 values(100, 'foo')");
    stmt1.execute("insert into table1 values(200, 'bar')");
    c1.close();
    // Make a Calcite schema with both a jdbc schema and a non-jdbc schema
    Connection connection = DriverManager.getConnection("jdbc:calcite:");
    CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
    SchemaPlus rootSchema = calciteConnection.getRootSchema();
    rootSchema.add("DB", JdbcSchema.create(rootSchema, "DB", JdbcSchema.dataSource(db, "org.hsqldb.jdbcDriver", "", ""), null, null));
    rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema()));
    return connection;
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

Aggregations

ReflectiveSchema (org.apache.calcite.adapter.java.ReflectiveSchema)22 SchemaPlus (org.apache.calcite.schema.SchemaPlus)21 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)16 Test (org.junit.Test)16 ResultSet (java.sql.ResultSet)13 Connection (java.sql.Connection)11 Statement (java.sql.Statement)11 PreparedStatement (java.sql.PreparedStatement)7 Properties (java.util.Properties)7 AbstractSchema (org.apache.calcite.schema.impl.AbstractSchema)7 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)6 AvaticaStatement (org.apache.calcite.avatica.AvaticaStatement)5 AvaticaConnection (org.apache.calcite.avatica.AvaticaConnection)4 Driver (org.apache.calcite.jdbc.Driver)3 RelNode (org.apache.calcite.rel.RelNode)3 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)2 SqlNode (org.apache.calcite.sql.SqlNode)2 JdbcTest (org.apache.calcite.test.JdbcTest)2