Search in sources :

Example 1 with AssertThat

use of org.apache.calcite.test.CalciteAssert.AssertThat in project calcite by apache.

the class JdbcAdapterTest method testTableModifyUpdate.

@Test
public void testTableModifyUpdate() 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 = "UPDATE \"foodmart\".\"expense_fact\"\n" + " SET \"account_id\"=888\n" + " WHERE \"store_id\"=666\n";
                final String explain = "PLAN=JdbcToEnumerableConverter\n" + "  JdbcTableModify(table=[[foodmart, expense_fact]], operation=[UPDATE], updateColumnList=[[account_id]], sourceExpressionList=[[888]], flattened=[false])\n" + "    JdbcProject(store_id=[$0], account_id=[$1], exp_date=[$2], time_id=[$3], category_id=[$4], currency_id=[$5], amount=[$6], EXPR$0=[888])\n" + "      JdbcFilter(condition=[=($0, 666)])\n" + "        JdbcTableScan(table=[[foodmart, expense_fact]])";
                final String jdbcSql = "UPDATE \"foodmart\".\"expense_fact\"" + " SET \"account_id\" = 888\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 2 with AssertThat

use of org.apache.calcite.test.CalciteAssert.AssertThat 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 3 with AssertThat

use of org.apache.calcite.test.CalciteAssert.AssertThat 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 4 with AssertThat

use of org.apache.calcite.test.CalciteAssert.AssertThat 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 5 with AssertThat

use of org.apache.calcite.test.CalciteAssert.AssertThat 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

SQLException (java.sql.SQLException)5 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)5 AssertThat (org.apache.calcite.test.CalciteAssert.AssertThat)5 Test (org.junit.Test)5