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"));
}
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();
}
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));
}
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));
}
}
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();
}
Aggregations