Search in sources :

Example 26 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 testSchemaConsistency.

@Test
public void testSchemaConsistency() throws Exception {
    // Create a database
    final String db = TempDb.INSTANCE.getUrl();
    Connection c1 = DriverManager.getConnection(db, "", "");
    Statement stmt1 = c1.createStatement();
    stmt1.execute("create table table1(id varchar(10) not null primary key, " + "field1 varchar(10))");
    // Connect via calcite to these databases
    Connection connection = DriverManager.getConnection("jdbc:calcite:");
    CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
    SchemaPlus rootSchema = calciteConnection.getRootSchema();
    final DataSource ds = JdbcSchema.dataSource(db, "org.hsqldb.jdbcDriver", "", "");
    rootSchema.add("DB", JdbcSchema.create(rootSchema, "DB", ds, null, null));
    Statement stmt3 = connection.createStatement();
    ResultSet rs;
    // fails, table does not exist
    try {
        rs = stmt3.executeQuery("select * from db.table2");
        fail("expected error, got " + rs);
    } catch (SQLException e) {
        assertThat(e.getCause().getCause().getMessage(), equalTo("Object 'TABLE2' not found within 'DB'"));
    }
    stmt1.execute("create table table2(id varchar(10) not null primary key, " + "field1 varchar(10))");
    stmt1.execute("insert into table2 values('a', 'aaaa')");
    PreparedStatement stmt2 = connection.prepareStatement("select * from db.table2");
    stmt1.execute("alter table table2 add column field2 varchar(10)");
    // "field2" not visible to stmt2
    rs = stmt2.executeQuery();
    assertThat(CalciteAssert.toString(rs), equalTo("ID=a; FIELD1=aaaa\n"));
    // "field2" visible to a new query
    rs = stmt3.executeQuery("select * from db.table2");
    assertThat(CalciteAssert.toString(rs), equalTo("ID=a; FIELD1=aaaa; FIELD2=null\n"));
    c1.close();
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 27 with SchemaPlus

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

the class CollectionTypeTest method setupConnectionWithNestedAnyTypeTable.

private Connection setupConnectionWithNestedAnyTypeTable() throws SQLException {
    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("nested", new NestedCollectionWithAnyTypeTable());
    return connection;
}
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) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

Example 28 with SchemaPlus

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

the class JdbcFrontLinqBackTest method makePostProcessor.

/**
 * Creates the post processor routine to be applied against a Connection.
 *
 * <p>Table schema is based on JdbcTest#Employee
 * (refer to {@link JdbcFrontLinqBackTest#mutable}).
 *
 * @param initialData records to be presented in table
 * @return a connection post-processor
 */
private static CalciteAssert.ConnectionPostProcessor makePostProcessor(final List<JdbcTest.Employee> initialData) {
    return new CalciteAssert.ConnectionPostProcessor() {

        public Connection apply(final Connection connection) throws SQLException {
            CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
            SchemaPlus rootSchema = calciteConnection.getRootSchema();
            SchemaPlus mapSchema = rootSchema.add("foo", new AbstractSchema());
            final String tableName = "bar";
            final JdbcTest.AbstractModifiableTable table = mutable(tableName, initialData);
            mapSchema.add(tableName, table);
            return calciteConnection;
        }
    };
}
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) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

Example 29 with SchemaPlus

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

the class SplunkDriver method connect.

@Override
public Connection connect(String url, Properties info) throws SQLException {
    Connection connection = super.connect(url, info);
    CalciteConnection calciteConnection = (CalciteConnection) connection;
    SplunkConnection splunkConnection;
    try {
        String url1 = info.getProperty("url");
        if (url1 == null) {
            throw new IllegalArgumentException("Must specify 'url' property");
        }
        if (url1.equals("mock")) {
            splunkConnection = new MockSplunkConnection();
        } else {
            String user = info.getProperty("user");
            if (user == null) {
                throw new IllegalArgumentException("Must specify 'user' property");
            }
            String password = info.getProperty("password");
            if (password == null) {
                throw new IllegalArgumentException("Must specify 'password' property");
            }
            URL url2 = new URL(url1);
            splunkConnection = new SplunkConnectionImpl(url2, user, password);
        }
    } catch (Exception e) {
        throw new SQLException("Cannot connect", e);
    }
    final SchemaPlus rootSchema = calciteConnection.getRootSchema();
    rootSchema.add("splunk", new SplunkSchema(splunkConnection));
    return connection;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) SplunkConnection(org.apache.calcite.adapter.splunk.search.SplunkConnection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) SplunkConnectionImpl(org.apache.calcite.adapter.splunk.search.SplunkConnectionImpl) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) URL(java.net.URL) SQLException(java.sql.SQLException) SplunkConnection(org.apache.calcite.adapter.splunk.search.SplunkConnection)

Example 30 with SchemaPlus

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

the class SqlCreateSchema method execute.

public void execute(CalcitePrepare.Context context) {
    final Pair<CalciteSchema, String> pair = SqlDdlNodes.schema(context, true, name);
    final SchemaPlus subSchema0 = pair.left.plus().getSubSchema(pair.right);
    if (subSchema0 != null) {
        if (!getReplace() && !ifNotExists) {
            throw SqlUtil.newContextException(name.getParserPosition(), RESOURCE.schemaExists(pair.right));
        }
    }
    final Schema subSchema = new AbstractSchema();
    pair.left.add(pair.right, subSchema);
}
Also used : AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) CalciteSchema(org.apache.calcite.jdbc.CalciteSchema) Schema(org.apache.calcite.schema.Schema) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) SchemaPlus(org.apache.calcite.schema.SchemaPlus)

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