Search in sources :

Example 16 with CalciteConnection

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection 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 17 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 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 18 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 testJdbcAggregate.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-657">[CALCITE-657]
 * NullPointerException when executing JdbcAggregate implement method</a>.
 */
@Test
public void testJdbcAggregate() throws Exception {
    final String url = MultiJdbcSchemaJoinTest.TempDb.INSTANCE.getUrl();
    Connection baseConnection = DriverManager.getConnection(url);
    Statement baseStmt = baseConnection.createStatement();
    baseStmt.execute("CREATE TABLE T2 (\n" + "ID INTEGER,\n" + "VALS INTEGER)");
    baseStmt.execute("INSERT INTO T2 VALUES (1, 1)");
    baseStmt.execute("INSERT INTO T2 VALUES (2, null)");
    baseStmt.close();
    baseConnection.commit();
    Properties info = new Properties();
    info.put("model", "inline:" + "{\n" + "  version: '1.0',\n" + "  defaultSchema: 'BASEJDBC',\n" + "  schemas: [\n" + "     {\n" + "       type: 'jdbc',\n" + "       name: 'BASEJDBC',\n" + "       jdbcDriver: '" + jdbcDriver.class.getName() + "',\n" + "       jdbcUrl: '" + url + "',\n" + "       jdbcCatalog: null,\n" + "       jdbcSchema: null\n" + "     }\n" + "  ]\n" + "}");
    final Connection calciteConnection = DriverManager.getConnection("jdbc:calcite:", info);
    ResultSet rs = calciteConnection.prepareStatement("select 10 * count(ID) from t2").executeQuery();
    assertThat(rs.next(), is(true));
    assertThat((Long) rs.getObject(1), equalTo(20L));
    assertThat(rs.next(), is(false));
    rs.close();
    calciteConnection.close();
}
Also used : org.hsqldb.jdbcDriver(org.hsqldb.jdbcDriver) Statement(java.sql.Statement) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) ResultSet(java.sql.ResultSet) Properties(java.util.Properties) Test(org.junit.Test)

Example 19 with CalciteConnection

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection 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 20 with CalciteConnection

use of org.apache.beam.vendor.calcite.v1_28_0.org.apache.calcite.jdbc.CalciteConnection 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)

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