Search in sources :

Example 6 with ReflectiveSchema

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

the class RelWriterTest method testReader.

/**
 * Unit test for {@link org.apache.calcite.rel.externalize.RelJsonReader}.
 */
@Test
public void testReader() {
    String s = Frameworks.withPlanner(new Frameworks.PlannerAction<String>() {

        public String apply(RelOptCluster cluster, RelOptSchema relOptSchema, SchemaPlus rootSchema) {
            SchemaPlus schema = rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema()));
            final RelJsonReader reader = new RelJsonReader(cluster, relOptSchema, schema);
            RelNode node;
            try {
                node = reader.read(XX);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            return RelOptUtil.dumpPlan("", node, SqlExplainFormat.TEXT, SqlExplainLevel.EXPPLAN_ATTRIBUTES);
        }
    });
    assertThat(s, isLinux("LogicalAggregate(group=[{0}], agg#0=[COUNT(DISTINCT $1)], agg#1=[COUNT()])\n" + "  LogicalFilter(condition=[=($1, 10)])\n" + "    LogicalTableScan(table=[[hr, emps]])\n"));
}
Also used : SchemaPlus(org.apache.calcite.schema.SchemaPlus) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) IOException(java.io.IOException) RelNode(org.apache.calcite.rel.RelNode) JdbcTest(org.apache.calcite.test.JdbcTest) Frameworks(org.apache.calcite.tools.Frameworks) RelJsonReader(org.apache.calcite.rel.externalize.RelJsonReader) JdbcTest(org.apache.calcite.test.JdbcTest) Test(org.junit.Test)

Example 7 with ReflectiveSchema

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

the class JdbcExample method run.

public void run() throws ClassNotFoundException, SQLException {
    Class.forName("org.apache.calcite.jdbc.Driver");
    Connection connection = DriverManager.getConnection("jdbc:calcite:");
    CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
    SchemaPlus rootSchema = calciteConnection.getRootSchema();
    rootSchema.add("hr", new ReflectiveSchema(new Hr()));
    rootSchema.add("foodmart", new ReflectiveSchema(new Foodmart()));
    Statement statement = connection.createStatement();
    ResultSet resultSet = statement.executeQuery("select *\n" + "from \"foodmart\".\"sales_fact_1997\" as s\n" + "join \"hr\".\"emps\" as e\n" + "on e.\"empid\" = s.\"cust_id\"");
    final StringBuilder buf = new StringBuilder();
    while (resultSet.next()) {
        int n = resultSet.getMetaData().getColumnCount();
        for (int i = 1; i <= n; i++) {
            buf.append(i > 1 ? "; " : "").append(resultSet.getMetaData().getColumnLabel(i)).append("=").append(resultSet.getObject(i));
        }
        System.out.println(buf.toString());
        buf.setLength(0);
    }
    resultSet.close();
    statement.close();
    connection.close();
}
Also used : Statement(java.sql.Statement) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ResultSet(java.sql.ResultSet) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

Example 8 with ReflectiveSchema

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

the class ReflectiveSchemaTest method testView.

/**
 * Tests a view.
 */
@Test
public void testView() throws SQLException, ClassNotFoundException {
    Connection connection = DriverManager.getConnection("jdbc:calcite:");
    CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
    SchemaPlus rootSchema = calciteConnection.getRootSchema();
    SchemaPlus schema = rootSchema.add("s", new AbstractSchema());
    schema.add("emps_view", ViewTable.viewMacro(schema, "select * from \"hr\".\"emps\" where \"deptno\" = 10", null, Arrays.asList("s", "emps_view"), null));
    rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema()));
    ResultSet resultSet = connection.createStatement().executeQuery("select *\n" + "from \"s\".\"emps_view\"\n" + "where \"empid\" < 120");
    assertEquals("empid=100; deptno=10; name=Bill; salary=10000.0; commission=1000\n" + "empid=110; deptno=10; name=Theodore; salary=11500.0; commission=250\n", CalciteAssert.toString(resultSet));
}
Also used : AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) 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 9 with ReflectiveSchema

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

the class ReflectiveSchemaTest method testReflectiveSchemaInUnnamedPackage.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-281">[CALCITE-1919]
 * NPE when target in ReflectiveSchema belongs to the unnamed package</a>.
 */
@Test
public void testReflectiveSchemaInUnnamedPackage() throws Exception {
    final Driver driver = new Driver();
    try (CalciteConnection connection = (CalciteConnection) driver.connect("jdbc:calcite:", new Properties())) {
        SchemaPlus rootSchema = connection.getRootSchema();
        final Class<?> c = Class.forName("RootHr");
        final Object o = c.getDeclaredConstructor().newInstance();
        rootSchema.add("hr", new ReflectiveSchema(o));
        connection.setSchema("hr");
        final Statement statement = connection.createStatement();
        final String sql = "select * from \"emps\"";
        final ResultSet resultSet = statement.executeQuery(sql);
        final String expected = "empid=100; name=Bill\n" + "empid=200; name=Eric\n" + "empid=150; name=Sebastian\n";
        assertThat(CalciteAssert.toString(resultSet), is(expected));
    }
}
Also used : Statement(java.sql.Statement) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ResultSet(java.sql.ResultSet) Driver(org.apache.calcite.jdbc.Driver) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) Properties(java.util.Properties) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 10 with ReflectiveSchema

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

the class ReflectiveSchemaTest method testViewPath.

/**
 * Tests a view with a path.
 */
@Test
public void testViewPath() throws SQLException, ClassNotFoundException {
    Connection connection = DriverManager.getConnection("jdbc:calcite:");
    CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
    SchemaPlus rootSchema = calciteConnection.getRootSchema();
    SchemaPlus schema = rootSchema.add("s", new AbstractSchema());
    // create a view s.emps based on hr.emps. uses explicit schema path "hr".
    schema.add("emps", ViewTable.viewMacro(schema, "select * from \"emps\" where \"deptno\" = 10", ImmutableList.of("hr"), ImmutableList.of("s", "emps"), null));
    schema.add("hr_emps", ViewTable.viewMacro(schema, "select * from \"emps\"", ImmutableList.of("hr"), ImmutableList.of("s", "hr_emps"), null));
    schema.add("s_emps", ViewTable.viewMacro(schema, "select * from \"emps\"", ImmutableList.of("s"), ImmutableList.of("s", "s_emps"), null));
    schema.add("null_emps", ViewTable.viewMacro(schema, "select * from \"emps\"", null, ImmutableList.of("s", "null_emps"), null));
    rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema()));
    final Statement statement = connection.createStatement();
    ResultSet resultSet;
    resultSet = statement.executeQuery("select * from \"s\".\"hr_emps\"");
    // "hr_emps" -> "hr"."emps", 4 rows
    assertEquals(4, count(resultSet));
    resultSet = statement.executeQuery(// "s_emps" -> "s"."emps", 3 rows
    "select * from \"s\".\"s_emps\"");
    assertEquals(3, count(resultSet));
    resultSet = statement.executeQuery(// "null_emps" -> "s"."emps", 3
    "select * from \"s\".\"null_emps\"");
    assertEquals(3, count(resultSet));
    statement.close();
}
Also used : AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) Statement(java.sql.Statement) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) 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)

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