Search in sources :

Example 1 with CloneSchema

use of org.apache.calcite.adapter.clone.CloneSchema 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();
}
Also used : CloneSchema(org.apache.calcite.adapter.clone.CloneSchema) 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) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 2 with CloneSchema

use of org.apache.calcite.adapter.clone.CloneSchema in project calcite by apache.

the class CalciteAssert method addSchema.

public static SchemaPlus addSchema(SchemaPlus rootSchema, SchemaSpec schema) {
    SchemaPlus foodmart;
    SchemaPlus jdbcScott;
    final ConnectionSpec cs;
    final DataSource dataSource;
    switch(schema) {
        case REFLECTIVE_FOODMART:
            return rootSchema.add("foodmart", new ReflectiveSchema(new JdbcTest.FoodmartSchema()));
        case JDBC_SCOTT:
            cs = DatabaseInstance.HSQLDB.scott;
            dataSource = JdbcSchema.dataSource(cs.url, cs.driver, cs.username, cs.password);
            return rootSchema.add("JDBC_SCOTT", JdbcSchema.create(rootSchema, "JDBC_SCOTT", dataSource, cs.catalog, cs.schema));
        case JDBC_FOODMART:
            cs = DB.foodmart;
            dataSource = JdbcSchema.dataSource(cs.url, cs.driver, cs.username, cs.password);
            return rootSchema.add("foodmart", JdbcSchema.create(rootSchema, "foodmart", dataSource, cs.catalog, cs.schema));
        case JDBC_FOODMART_WITH_LATTICE:
            foodmart = rootSchema.getSubSchema("foodmart");
            if (foodmart == null) {
                foodmart = CalciteAssert.addSchema(rootSchema, SchemaSpec.JDBC_FOODMART);
            }
            foodmart.add("lattice", Lattice.create(foodmart.unwrap(CalciteSchema.class), "select 1 from \"foodmart\".\"sales_fact_1997\" as s\n" + "join \"foodmart\".\"time_by_day\" as t using (\"time_id\")\n" + "join \"foodmart\".\"customer\" as c using (\"customer_id\")\n" + "join \"foodmart\".\"product\" as p using (\"product_id\")\n" + "join \"foodmart\".\"product_class\" as pc on p.\"product_class_id\" = pc.\"product_class_id\"", true));
            return foodmart;
        case SCOTT:
            jdbcScott = rootSchema.getSubSchema("jdbc_scott");
            if (jdbcScott == null) {
                jdbcScott = CalciteAssert.addSchema(rootSchema, SchemaSpec.JDBC_SCOTT);
            }
            return rootSchema.add("scott", new CloneSchema(jdbcScott));
        case CLONE_FOODMART:
            foodmart = rootSchema.getSubSchema("foodmart");
            if (foodmart == null) {
                foodmart = CalciteAssert.addSchema(rootSchema, SchemaSpec.JDBC_FOODMART);
            }
            return rootSchema.add("foodmart2", new CloneSchema(foodmart));
        case GEO:
            ModelHandler.addFunctions(rootSchema, null, ImmutableList.<String>of(), GeoFunctions.class.getName(), "*", true);
            final SchemaPlus s = rootSchema.add("GEO", new AbstractSchema());
            ModelHandler.addFunctions(s, "countries", ImmutableList.<String>of(), CountriesTableFunction.class.getName(), null, false);
            final String sql = "select * from table(\"countries\"(true))";
            final ViewTableMacro viewMacro = ViewTable.viewMacro(rootSchema, sql, ImmutableList.of("GEO"), ImmutableList.<String>of(), false);
            s.add("countries", viewMacro);
            return s;
        case HR:
            return rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema()));
        case LINGUAL:
            return rootSchema.add("SALES", new ReflectiveSchema(new JdbcTest.LingualSchema()));
        case BLANK:
            return rootSchema.add("BLANK", new AbstractSchema());
        case ORINOCO:
            final SchemaPlus orinoco = rootSchema.add("ORINOCO", new AbstractSchema());
            orinoco.add("ORDERS", new StreamTest.OrdersHistoryTable(StreamTest.OrdersStreamTableFactory.getRowList()));
            return orinoco;
        case POST:
            final SchemaPlus post = rootSchema.add("POST", new AbstractSchema());
            post.add("EMP", ViewTable.viewMacro(post, "select * from (values\n" + "    ('Jane', 10, 'F'),\n" + "    ('Bob', 10, 'M'),\n" + "    ('Eric', 20, 'M'),\n" + "    ('Susan', 30, 'F'),\n" + "    ('Alice', 30, 'F'),\n" + "    ('Adam', 50, 'M'),\n" + "    ('Eve', 50, 'F'),\n" + "    ('Grace', 60, 'F'),\n" + "    ('Wilma', cast(null as integer), 'F'))\n" + "  as t(ename, deptno, gender)", ImmutableList.<String>of(), ImmutableList.of("POST", "EMP"), null));
            post.add("DEPT", ViewTable.viewMacro(post, "select * from (values\n" + "    (10, 'Sales'),\n" + "    (20, 'Marketing'),\n" + "    (30, 'Engineering'),\n" + "    (40, 'Empty')) as t(deptno, dname)", ImmutableList.<String>of(), ImmutableList.of("POST", "DEPT"), null));
            post.add("EMPS", ViewTable.viewMacro(post, "select * from (values\n" + "    (100, 'Fred',  10, CAST(NULL AS CHAR(1)), CAST(NULL AS VARCHAR(20)), 40,               25, TRUE,    FALSE, DATE '1996-08-03'),\n" + "    (110, 'Eric',  20, 'M',                   'San Francisco',           3,                80, UNKNOWN, FALSE, DATE '2001-01-01'),\n" + "    (110, 'John',  40, 'M',                   'Vancouver',               2, CAST(NULL AS INT), FALSE,   TRUE,  DATE '2002-05-03'),\n" + "    (120, 'Wilma', 20, 'F',                   CAST(NULL AS VARCHAR(20)), 1,                 5, UNKNOWN, TRUE,  DATE '2005-09-07'),\n" + "    (130, 'Alice', 40, 'F',                   'Vancouver',               2, CAST(NULL AS INT), FALSE,   TRUE,  DATE '2007-01-01'))\n" + " as t(empno, name, deptno, gender, city, empid, age, slacker, manager, joinedat)", ImmutableList.<String>of(), ImmutableList.of("POST", "EMPS"), null));
            return post;
        default:
            throw new AssertionError("unknown schema " + schema);
    }
}
Also used : CloneSchema(org.apache.calcite.adapter.clone.CloneSchema) GeoFunctions(org.apache.calcite.runtime.GeoFunctions) ViewTableMacro(org.apache.calcite.schema.impl.ViewTableMacro) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) DataSource(javax.sql.DataSource) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema)

Aggregations

CloneSchema (org.apache.calcite.adapter.clone.CloneSchema)2 SchemaPlus (org.apache.calcite.schema.SchemaPlus)2 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 DataSource (javax.sql.DataSource)1 ReflectiveSchema (org.apache.calcite.adapter.java.ReflectiveSchema)1 AvaticaConnection (org.apache.calcite.avatica.AvaticaConnection)1 AvaticaStatement (org.apache.calcite.avatica.AvaticaStatement)1 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)1 GeoFunctions (org.apache.calcite.runtime.GeoFunctions)1 AbstractSchema (org.apache.calcite.schema.impl.AbstractSchema)1 ViewTableMacro (org.apache.calcite.schema.impl.ViewTableMacro)1 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)1 Test (org.junit.Test)1