Search in sources :

Example 16 with SchemaPlus

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus in project calcite by apache.

the class JdbcTest method testSchemaCaching.

@Test
public void testSchemaCaching() throws Exception {
    final Connection connection = CalciteAssert.that(CalciteAssert.Config.JDBC_FOODMART).connect();
    final CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
    final SchemaPlus rootSchema = calciteConnection.getRootSchema();
    // create schema "/a"
    final Map<String, Schema> aSubSchemaMap = new HashMap<>();
    final SchemaPlus aSchema = rootSchema.add("a", new AbstractSchema() {

        @Override
        protected Map<String, Schema> getSubSchemaMap() {
            return aSubSchemaMap;
        }
    });
    aSchema.setCacheEnabled(true);
    assertThat(aSchema.getSubSchemaNames().size(), is(0));
    // first call, to populate the cache
    assertThat(aSchema.getSubSchemaNames().size(), is(0));
    // create schema "/a/b1". Appears only when we disable caching.
    aSubSchemaMap.put("b1", new AbstractSchema());
    assertThat(aSchema.getSubSchemaNames().size(), is(0));
    assertThat(aSchema.getSubSchema("b1"), nullValue());
    aSchema.setCacheEnabled(false);
    assertThat(aSchema.getSubSchemaNames().size(), is(1));
    assertThat(aSchema.getSubSchema("b1"), notNullValue());
    // create schema "/a/b2". Appears immediately, because caching is disabled.
    aSubSchemaMap.put("b2", new AbstractSchema());
    assertThat(aSchema.getSubSchemaNames().size(), is(2));
    // an explicit sub-schema appears immediately, even if caching is enabled
    aSchema.setCacheEnabled(true);
    assertThat(aSchema.getSubSchemaNames().size(), is(2));
    // explicit
    aSchema.add("b3", new AbstractSchema());
    // implicit
    aSubSchemaMap.put("b4", new AbstractSchema());
    assertThat(aSchema.getSubSchemaNames().size(), is(3));
    aSchema.setCacheEnabled(false);
    assertThat(aSchema.getSubSchemaNames().size(), is(4));
    for (String name : aSchema.getSubSchemaNames()) {
        assertThat(aSchema.getSubSchema(name), notNullValue());
    }
    // create schema "/a2"
    final Map<String, Schema> a2SubSchemaMap = new HashMap<>();
    final SchemaPlus a2Schema = rootSchema.add("a", new AbstractSchema() {

        @Override
        protected Map<String, Schema> getSubSchemaMap() {
            return a2SubSchemaMap;
        }
    });
    a2Schema.setCacheEnabled(true);
    assertThat(a2Schema.getSubSchemaNames().size(), is(0));
    // create schema "/a2/b3". Change not visible since caching is enabled.
    a2SubSchemaMap.put("b3", new AbstractSchema());
    assertThat(a2Schema.getSubSchemaNames().size(), is(0));
    Thread.sleep(1);
    assertThat(a2Schema.getSubSchemaNames().size(), is(0));
    // Change visible after we turn off caching.
    a2Schema.setCacheEnabled(false);
    assertThat(a2Schema.getSubSchemaNames().size(), is(1));
    a2SubSchemaMap.put("b4", new AbstractSchema());
    assertThat(a2Schema.getSubSchemaNames().size(), is(2));
    for (String name : aSchema.getSubSchemaNames()) {
        assertThat(aSchema.getSubSchema(name), notNullValue());
    }
    // add tables and retrieve with various case sensitivities
    final TableInRootSchemaTest.SimpleTable table = new TableInRootSchemaTest.SimpleTable();
    a2Schema.add("table1", table);
    a2Schema.add("TABLE1", table);
    a2Schema.add("tabLe1", table);
    a2Schema.add("tabLe2", table);
    assertThat(a2Schema.getTableNames().size(), equalTo(4));
    final CalciteSchema a2CalciteSchema = CalciteSchema.from(a2Schema);
    assertThat(a2CalciteSchema.getTable("table1", true), notNullValue());
    assertThat(a2CalciteSchema.getTable("table1", false), notNullValue());
    assertThat(a2CalciteSchema.getTable("taBle1", true), nullValue());
    assertThat(a2CalciteSchema.getTable("taBle1", false), notNullValue());
    final TableMacro function = ViewTable.viewMacro(a2Schema, "values 1", null, null, null);
    Util.discard(function);
    connection.close();
}
Also used : TableMacro(org.apache.calcite.schema.TableMacro) HashMap(java.util.HashMap) Schema(org.apache.calcite.schema.Schema) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) JdbcSchema(org.apache.calcite.adapter.jdbc.JdbcSchema) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) CloneSchema(org.apache.calcite.adapter.clone.CloneSchema) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) Connection(java.sql.Connection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) Map(java.util.Map) HashMap(java.util.HashMap) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 17 with SchemaPlus

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus 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 18 with SchemaPlus

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus 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 19 with SchemaPlus

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus 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 20 with SchemaPlus

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus 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)

Aggregations

SchemaPlus (org.apache.calcite.schema.SchemaPlus)182 Test (org.junit.Test)57 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)42 Connection (java.sql.Connection)39 AbstractSchema (org.apache.calcite.schema.impl.AbstractSchema)33 ResultSet (java.sql.ResultSet)26 ReflectiveSchema (org.apache.calcite.adapter.java.ReflectiveSchema)24 RelNode (org.apache.calcite.rel.RelNode)21 Table (org.apache.calcite.schema.Table)20 SqlNode (org.apache.calcite.sql.SqlNode)19 IOException (java.io.IOException)17 Statement (java.sql.Statement)17 PreparedStatement (java.sql.PreparedStatement)16 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)15 AbstractSchema (org.apache.drill.exec.store.AbstractSchema)15 ArrayList (java.util.ArrayList)14 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)14 SQLException (java.sql.SQLException)13 Properties (java.util.Properties)13 SchemaPlus (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus)13