Search in sources :

Example 21 with AbstractSchema

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));
}
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 22 with AbstractSchema

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

Example 23 with AbstractSchema

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());
}
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) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 24 with AbstractSchema

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();
}
Also used : TableMacro(org.apache.calcite.schema.TableMacro) 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) ResultSet(java.sql.ResultSet) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 25 with AbstractSchema

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));
}
Also used : HashMap(java.util.HashMap) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) 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) SchemaPlus(org.apache.calcite.schema.SchemaPlus) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

AbstractSchema (org.apache.calcite.schema.impl.AbstractSchema)33 SchemaPlus (org.apache.calcite.schema.SchemaPlus)30 Connection (java.sql.Connection)22 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)22 Test (org.junit.Test)20 ResultSet (java.sql.ResultSet)15 ReflectiveSchema (org.apache.calcite.adapter.java.ReflectiveSchema)10 TableFunction (org.apache.calcite.schema.TableFunction)10 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)8 Schema (org.apache.calcite.schema.Schema)6 PreparedStatement (java.sql.PreparedStatement)5 CalciteSchema (org.apache.calcite.jdbc.CalciteSchema)5 Statement (java.sql.Statement)4 CloneSchema (org.apache.calcite.adapter.clone.CloneSchema)4 AvaticaConnection (org.apache.calcite.avatica.AvaticaConnection)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 JdbcSchema (org.apache.calcite.adapter.jdbc.JdbcSchema)3 ProjectableFilterableTable (org.apache.calcite.schema.ProjectableFilterableTable)3 Table (org.apache.calcite.schema.Table)3