use of org.apache.calcite.schema.impl.AbstractSchema 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.schema.impl.AbstractSchema 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();
}
use of org.apache.calcite.schema.impl.AbstractSchema in project calcite by apache.
the class ReflectiveSchemaTest method testOperator.
/**
* Tests a relation that is accessed via method syntax.
* The function returns a {@link org.apache.calcite.linq4j.Queryable}.
*/
@Ignore
@Test
public void testOperator() 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("GenerateStrings", TableMacroImpl.create(Smalls.GENERATE_STRINGS_METHOD));
schema.add("StringUnion", TableMacroImpl.create(Smalls.STRING_UNION_METHOD));
rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema()));
ResultSet resultSet = connection.createStatement().executeQuery("select *\n" + "from table(s.StringUnion(\n" + " GenerateStrings(5),\n" + " cursor (select name from emps)))\n" + "where char_length(s) > 3");
assertTrue(resultSet.next());
}
use of org.apache.calcite.schema.impl.AbstractSchema in project calcite by apache.
the class JdbcTest method testTableMacroMap.
/**
* Table macro that takes a MAP as a parameter.
*
* <p>Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-588">[CALCITE-588]
* Allow TableMacro to consume Maps and Collections</a>.
*/
@Test
public void testTableMacroMap() 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());
final TableMacro tableMacro = TableMacroImpl.create(Smalls.STR_METHOD);
schema.add("Str", tableMacro);
ResultSet resultSet = connection.createStatement().executeQuery("select *\n" + "from table(\"s\".\"Str\"(MAP['a', 1, 'baz', 2],\n" + " ARRAY[3, 4, CAST(null AS INTEGER)])) as t(n)");
// The call to "View('(10), (2)')" expands to 'values (1), (3), (10), (20)'.
assertThat(CalciteAssert.toString(resultSet), equalTo("N={'a'=1, 'baz'=2}\n" + "N=[3, 4, null] \n"));
connection.close();
}
use of org.apache.calcite.schema.impl.AbstractSchema in project calcite by apache.
the class JdbcTest method testExplicitImplicitSchemaSameName.
@Test
public void testExplicitImplicitSchemaSameName() throws Exception {
final SchemaPlus rootSchema = CalciteSchema.createRootSchema(false).plus();
// 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;
}
});
// add explicit schema "/a/b".
aSchema.add("b", new AbstractSchema());
// add implicit schema "/a/b"
aSubSchemaMap.put("b", new AbstractSchema());
aSchema.setCacheEnabled(true);
// explicit should win implicit.
assertThat(aSchema.getSubSchemaNames().size(), is(1));
}
Aggregations