Search in sources :

Example 21 with SchemaPlus

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus 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 22 with SchemaPlus

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus in project calcite by apache.

the class JdbcTest method testSimpleCalciteSchemaWithView.

@Test
public void testSimpleCalciteSchemaWithView() throws Exception {
    final SchemaPlus rootSchema = CalciteSchema.createRootSchema(false, false).plus();
    final Multimap<String, org.apache.calcite.schema.Function> functionMap = LinkedListMultimap.create();
    // create schema "/a"
    final SchemaPlus aSchema = rootSchema.add("a", new AbstractSchema() {

        @Override
        protected Multimap<String, org.apache.calcite.schema.Function> getFunctionMultimap() {
            return functionMap;
        }
    });
    // add view definition
    final String viewName = "V";
    final org.apache.calcite.schema.Function view = ViewTable.viewMacro(rootSchema.getSubSchema("a"), "values('1', '2')", null, null, false);
    functionMap.put(viewName, view);
    final CalciteSchema calciteSchema = CalciteSchema.from(aSchema);
    assertThat(calciteSchema.getTableBasedOnNullaryFunction(viewName, true), notNullValue());
    assertThat(calciteSchema.getTableBasedOnNullaryFunction(viewName, false), notNullValue());
    assertThat(calciteSchema.getTableBasedOnNullaryFunction("V1", true), nullValue());
    assertThat(calciteSchema.getTableBasedOnNullaryFunction("V1", false), nullValue());
    assertThat(calciteSchema.getFunctions(viewName, true), hasItem(view));
    assertThat(calciteSchema.getFunctions(viewName, false), hasItem(view));
    assertThat(calciteSchema.getFunctions("V1", true), not(hasItem(view)));
    assertThat(calciteSchema.getFunctions("V1", false), not(hasItem(view)));
}
Also used : SchemaPlus(org.apache.calcite.schema.SchemaPlus) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) TableFunction(org.apache.calcite.schema.TableFunction) SqlAdvisorGetHintsFunction(org.apache.calcite.sql.advise.SqlAdvisorGetHintsFunction) Function(com.google.common.base.Function) LinkedListMultimap(com.google.common.collect.LinkedListMultimap) Multimap(com.google.common.collect.Multimap) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) Test(org.junit.Test)

Example 23 with SchemaPlus

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus in project calcite by apache.

the class JdbcTest method testSimpleCalciteSchema.

@Test
public void testSimpleCalciteSchema() throws Exception {
    final SchemaPlus rootSchema = CalciteSchema.createRootSchema(false, 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/c"
    aSubSchemaMap.put("c", new AbstractSchema());
    assertThat(aSchema.getSubSchema("c"), notNullValue());
    assertThat(aSchema.getSubSchema("b"), notNullValue());
    // add implicit schema "/a/b"
    aSubSchemaMap.put("b", new AbstractSchema());
    // explicit should win implicit.
    assertThat(aSchema.getSubSchemaNames().size(), is(2));
}
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)

Example 24 with SchemaPlus

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus in project calcite by apache.

the class LinqFrontJdbcBackTest method testTableWhere.

@Test
public void testTableWhere() throws SQLException, ClassNotFoundException {
    final Connection connection = CalciteAssert.that(CalciteAssert.Config.JDBC_FOODMART).connect();
    final CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
    final SchemaPlus rootSchema = calciteConnection.getRootSchema();
    ParameterExpression c = Expressions.parameter(JdbcTest.Customer.class, "c");
    String s = Schemas.queryable(Schemas.createDataContext(connection, rootSchema), rootSchema.getSubSchema("foodmart"), JdbcTest.Customer.class, "customer").where(Expressions.<Predicate1<JdbcTest.Customer>>lambda(Expressions.lessThan(Expressions.field(c, "customer_id"), Expressions.constant(5)), c)).toList().toString();
    Util.discard(s);
}
Also used : ParameterExpression(org.apache.calcite.linq4j.tree.ParameterExpression) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 25 with SchemaPlus

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus in project calcite by apache.

the class MultiJdbcSchemaJoinTest method setup.

private Connection setup() throws SQLException {
    // Create a jdbc database & table
    final String db = TempDb.INSTANCE.getUrl();
    Connection c1 = DriverManager.getConnection(db, "", "");
    Statement stmt1 = c1.createStatement();
    // This is a table we can join with the emps from the hr schema
    stmt1.execute("create table table1(id integer not null primary key, " + "field1 varchar(10))");
    stmt1.execute("insert into table1 values(100, 'foo')");
    stmt1.execute("insert into table1 values(200, 'bar')");
    c1.close();
    // Make a Calcite schema with both a jdbc schema and a non-jdbc schema
    Connection connection = DriverManager.getConnection("jdbc:calcite:");
    CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
    SchemaPlus rootSchema = calciteConnection.getRootSchema();
    rootSchema.add("DB", JdbcSchema.create(rootSchema, "DB", JdbcSchema.dataSource(db, "org.hsqldb.jdbcDriver", "", ""), null, null));
    rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema()));
    return connection;
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

Aggregations

SchemaPlus (org.apache.calcite.schema.SchemaPlus)182 Test (org.junit.Test)57 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)42 Connection (java.sql.Connection)39 AbstractSchema (org.apache.calcite.schema.impl.AbstractSchema)33 ResultSet (java.sql.ResultSet)26 ReflectiveSchema (org.apache.calcite.adapter.java.ReflectiveSchema)24 RelNode (org.apache.calcite.rel.RelNode)21 Table (org.apache.calcite.schema.Table)20 SqlNode (org.apache.calcite.sql.SqlNode)19 IOException (java.io.IOException)17 Statement (java.sql.Statement)17 PreparedStatement (java.sql.PreparedStatement)16 JavaTypeFactory (org.apache.calcite.adapter.java.JavaTypeFactory)15 AbstractSchema (org.apache.drill.exec.store.AbstractSchema)15 ArrayList (java.util.ArrayList)14 JavaTypeFactoryImpl (org.apache.calcite.jdbc.JavaTypeFactoryImpl)14 SQLException (java.sql.SQLException)13 Properties (java.util.Properties)13 SchemaPlus (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.schema.SchemaPlus)13