Search in sources :

Example 61 with CalciteConnection

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project calcite by apache.

the class MultiJdbcSchemaJoinTest method test.

@Test
public void test() throws SQLException, ClassNotFoundException {
    // Create two databases
    // It's two times hsqldb, but imagine they are different rdbms's
    final String db1 = TempDb.INSTANCE.getUrl();
    Connection c1 = DriverManager.getConnection(db1, "", "");
    Statement stmt1 = c1.createStatement();
    stmt1.execute("create table table1(id varchar(10) not null primary key, " + "field1 varchar(10))");
    stmt1.execute("insert into table1 values('a', 'aaaa')");
    c1.close();
    final String db2 = TempDb.INSTANCE.getUrl();
    Connection c2 = DriverManager.getConnection(db2, "", "");
    Statement stmt2 = c2.createStatement();
    stmt2.execute("create table table2(id varchar(10) not null primary key, " + "field1 varchar(10))");
    stmt2.execute("insert into table2 values('a', 'aaaa')");
    c2.close();
    // Connect via calcite to these databases
    Connection connection = DriverManager.getConnection("jdbc:calcite:");
    CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
    SchemaPlus rootSchema = calciteConnection.getRootSchema();
    final DataSource ds1 = JdbcSchema.dataSource(db1, "org.hsqldb.jdbcDriver", "", "");
    rootSchema.add("DB1", JdbcSchema.create(rootSchema, "DB1", ds1, null, null));
    final DataSource ds2 = JdbcSchema.dataSource(db2, "org.hsqldb.jdbcDriver", "", "");
    rootSchema.add("DB2", JdbcSchema.create(rootSchema, "DB2", ds2, null, null));
    Statement stmt3 = connection.createStatement();
    ResultSet rs = stmt3.executeQuery("select table1.id, table1.field1 " + "from db1.table1 join db2.table2 on table1.id = table2.id");
    assertThat(CalciteAssert.toString(rs), equalTo("ID=a; FIELD1=aaaa\n"));
}
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) ResultSet(java.sql.ResultSet) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) DataSource(javax.sql.DataSource) Test(org.junit.Test)

Example 62 with CalciteConnection

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project calcite by apache.

the class JdbcAdapterTest method testTableModifyInsertWithSubQuery.

@Test
public void testTableModifyInsertWithSubQuery() throws Exception {
    final AssertThat that = CalciteAssert.model(JdbcTest.FOODMART_MODEL).enable(CalciteAssert.DB == DatabaseInstance.HSQLDB);
    that.doWithConnection(new Function<CalciteConnection, Void>() {

        public Void apply(CalciteConnection connection) {
            try (LockWrapper ignore = exclusiveCleanDb(connection)) {
                final String sql = "INSERT INTO \"foodmart\".\"expense_fact\"(\n" + " \"store_id\", \"account_id\", \"exp_date\", \"time_id\"," + " \"category_id\", \"currency_id\", \"amount\")\n" + "SELECT  \"store_id\", \"account_id\", \"exp_date\"," + " \"time_id\" + 1, \"category_id\", \"currency_id\"," + " \"amount\"\n" + "FROM \"foodmart\".\"expense_fact\"\n" + "WHERE \"store_id\" = 666";
                final String explain = "PLAN=JdbcToEnumerableConverter\n" + "  JdbcTableModify(table=[[foodmart, expense_fact]], operation=[INSERT], flattened=[false])\n" + "    JdbcProject(store_id=[$0], account_id=[$1], exp_date=[$2], time_id=[+($3, 1)], category_id=[$4], currency_id=[$5], amount=[$6])\n" + "      JdbcFilter(condition=[=($0, 666)])\n" + "        JdbcTableScan(table=[[foodmart, expense_fact]])\n";
                final String jdbcSql = "INSERT INTO \"foodmart\".\"expense_fact\"" + " (\"store_id\", \"account_id\", \"exp_date\", \"time_id\"," + " \"category_id\", \"currency_id\", \"amount\")\n" + "(SELECT \"store_id\", \"account_id\", \"exp_date\"," + " \"time_id\" + 1 AS \"time_id\", \"category_id\"," + " \"currency_id\", \"amount\"\n" + "FROM \"foodmart\".\"expense_fact\"\n" + "WHERE \"store_id\" = 666)";
                that.query(sql).explainContains(explain).planUpdateHasSql(jdbcSql, 1);
                return null;
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    });
}
Also used : SQLException(java.sql.SQLException) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) AssertThat(org.apache.calcite.test.CalciteAssert.AssertThat) Test(org.junit.Test)

Example 63 with CalciteConnection

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project calcite by apache.

the class JdbcAdapterTest method testTableModifyInsert.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-1527">[CALCITE-1527]
 * Support DML in the JDBC adapter</a>.
 */
@Test
public void testTableModifyInsert() throws Exception {
    final String sql = "INSERT INTO \"foodmart\".\"expense_fact\"(\n" + " \"store_id\", \"account_id\", \"exp_date\", \"time_id\"," + " \"category_id\", \"currency_id\", \"amount\")\n" + "VALUES (666, 666, TIMESTAMP '1997-01-01 00:00:00'," + " 666, '666', 666, 666)";
    final String explain = "PLAN=JdbcToEnumerableConverter\n" + "  JdbcTableModify(table=[[foodmart, expense_fact]], operation=[INSERT], flattened=[false])\n" + "    JdbcValues(tuples=[[{ 666, 666, 1997-01-01 00:00:00, 666, '666', 666, 666.0000 }]])\n";
    final String jdbcSql = "INSERT INTO \"foodmart\".\"expense_fact\"" + " (\"store_id\", \"account_id\", \"exp_date\", \"time_id\"," + " \"category_id\", \"currency_id\", \"amount\")\n" + "VALUES  (666, 666, TIMESTAMP '1997-01-01 00:00:00', 666, '666', 666, 666.0000)";
    final AssertThat that = CalciteAssert.model(JdbcTest.FOODMART_MODEL).enable(CalciteAssert.DB == DatabaseInstance.HSQLDB || CalciteAssert.DB == DatabaseInstance.POSTGRESQL);
    that.doWithConnection(new Function<CalciteConnection, Void>() {

        public Void apply(CalciteConnection connection) {
            try (LockWrapper ignore = exclusiveCleanDb(connection)) {
                that.query(sql).explainContains(explain).planUpdateHasSql(jdbcSql, 1);
                return null;
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    });
}
Also used : SQLException(java.sql.SQLException) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) AssertThat(org.apache.calcite.test.CalciteAssert.AssertThat) Test(org.junit.Test)

Example 64 with CalciteConnection

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project calcite by apache.

the class JdbcAdapterTest method testTableModifyDelete.

@Test
public void testTableModifyDelete() throws Exception {
    final AssertThat that = CalciteAssert.model(JdbcTest.FOODMART_MODEL).enable(CalciteAssert.DB == DatabaseInstance.HSQLDB);
    that.doWithConnection(new Function<CalciteConnection, Void>() {

        public Void apply(CalciteConnection connection) {
            try (LockWrapper ignore = exclusiveCleanDb(connection)) {
                final String sql = "DELETE FROM \"foodmart\".\"expense_fact\"\n" + "WHERE \"store_id\"=666\n";
                final String explain = "PLAN=JdbcToEnumerableConverter\n" + "  JdbcTableModify(table=[[foodmart, expense_fact]], operation=[DELETE], flattened=[false])\n" + "    JdbcFilter(condition=[=($0, 666)])\n" + "      JdbcTableScan(table=[[foodmart, expense_fact]]";
                final String jdbcSql = "DELETE FROM \"foodmart\".\"expense_fact\"\n" + "WHERE \"store_id\" = 666";
                that.query(sql).explainContains(explain).planUpdateHasSql(jdbcSql, 1);
                return null;
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    });
}
Also used : SQLException(java.sql.SQLException) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) AssertThat(org.apache.calcite.test.CalciteAssert.AssertThat) Test(org.junit.Test)

Example 65 with CalciteConnection

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection in project calcite by apache.

the class JdbcAdapterTest method testTableModifyInsertMultiValues.

@Test
public void testTableModifyInsertMultiValues() throws Exception {
    final String sql = "INSERT INTO \"foodmart\".\"expense_fact\"(\n" + " \"store_id\", \"account_id\", \"exp_date\", \"time_id\"," + " \"category_id\", \"currency_id\", \"amount\")\n" + "VALUES (666, 666, TIMESTAMP '1997-01-01 00:00:00'," + "   666, '666', 666, 666),\n" + " (666, 777, TIMESTAMP '1997-01-01 00:00:00'," + "   666, '666', 666, 666)";
    final String explain = "PLAN=JdbcToEnumerableConverter\n" + "  JdbcTableModify(table=[[foodmart, expense_fact]], operation=[INSERT], flattened=[false])\n" + "    JdbcValues(tuples=[[{ 666, 666, 1997-01-01 00:00:00, 666, '666', 666, 666.0000 }," + " { 666, 777, 1997-01-01 00:00:00, 666, '666', 666, 666.0000 }]])\n";
    final String jdbcSql = "INSERT INTO \"foodmart\".\"expense_fact\"" + " (\"store_id\", \"account_id\", \"exp_date\", \"time_id\"," + " \"category_id\", \"currency_id\", \"amount\")\n" + "VALUES  (666, 666, TIMESTAMP '1997-01-01 00:00:00', 666, '666', 666, 666.0000),\n" + " (666, 777, TIMESTAMP '1997-01-01 00:00:00', 666, '666', 666, 666.0000)";
    final AssertThat that = CalciteAssert.model(JdbcTest.FOODMART_MODEL).enable(CalciteAssert.DB == DatabaseInstance.HSQLDB || CalciteAssert.DB == DatabaseInstance.POSTGRESQL);
    that.doWithConnection(new Function<CalciteConnection, Void>() {

        public Void apply(CalciteConnection connection) {
            try (LockWrapper ignore = exclusiveCleanDb(connection)) {
                that.query(sql).explainContains(explain).planUpdateHasSql(jdbcSql, 2);
                return null;
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    });
}
Also used : SQLException(java.sql.SQLException) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) AssertThat(org.apache.calcite.test.CalciteAssert.AssertThat) Test(org.junit.Test)

Aggregations

CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)65 Test (org.junit.Test)52 Connection (java.sql.Connection)51 ResultSet (java.sql.ResultSet)42 SchemaPlus (org.apache.calcite.schema.SchemaPlus)42 Statement (java.sql.Statement)32 PreparedStatement (java.sql.PreparedStatement)24 AbstractSchema (org.apache.calcite.schema.impl.AbstractSchema)22 Properties (java.util.Properties)17 ReflectiveSchema (org.apache.calcite.adapter.java.ReflectiveSchema)17 SQLException (java.sql.SQLException)16 AvaticaConnection (org.apache.calcite.avatica.AvaticaConnection)12 CalciteConnection (org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection)11 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)11 TableFunction (org.apache.calcite.schema.TableFunction)10 AvaticaStatement (org.apache.calcite.avatica.AvaticaStatement)9 org.hsqldb.jdbcDriver (org.hsqldb.jdbcDriver)5 AssertThat (org.apache.calcite.test.CalciteAssert.AssertThat)4 IOException (java.io.IOException)3 ResultSetMetaData (java.sql.ResultSetMetaData)3