Search in sources :

Example 11 with CalciteConnection

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

Example 13 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 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 14 with CalciteConnection

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

the class RelMdColumnOriginsTest method testQueryWithAggregateGroupingSets.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-542">[CALCITE-542]
 * Support for Aggregate with grouping sets in RelMdColumnOrigins</a>.
 */
@Test
public void testQueryWithAggregateGroupingSets() throws Exception {
    Connection connection = DriverManager.getConnection("jdbc:calcite:");
    CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
    calciteConnection.getRootSchema().add("T1", new TableInRootSchemaTest.SimpleTable());
    Statement statement = calciteConnection.createStatement();
    ResultSet resultSet = statement.executeQuery("SELECT TABLE1.ID, TABLE2.ID FROM " + "(SELECT GROUPING(A) AS ID FROM T1 " + "GROUP BY ROLLUP(A,B)) TABLE1 " + "JOIN " + "(SELECT GROUPING(A) AS ID FROM T1 " + "GROUP BY ROLLUP(A,B)) TABLE2 " + "ON TABLE1.ID = TABLE2.ID");
    final String result1 = "ID=0; ID=0";
    final String result2 = "ID=1; ID=1";
    final ImmutableMultiset<String> expectedResult = ImmutableMultiset.<String>builder().addCopies(result1, 25).add(result2).build();
    assertThat(CalciteAssert.toSet(resultSet), equalTo(expectedResult));
    final ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
    assertThat(resultSetMetaData.getColumnName(1), equalTo("ID"));
    assertThat(resultSetMetaData.getTableName(1), nullValue());
    assertThat(resultSetMetaData.getSchemaName(1), nullValue());
    assertThat(resultSetMetaData.getColumnName(2), equalTo("ID"));
    assertThat(resultSetMetaData.getTableName(2), nullValue());
    assertThat(resultSetMetaData.getSchemaName(2), nullValue());
    resultSet.close();
    statement.close();
    connection.close();
}
Also used : ResultSetMetaData(java.sql.ResultSetMetaData) Statement(java.sql.Statement) Connection(java.sql.Connection) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) ResultSet(java.sql.ResultSet) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 15 with CalciteConnection

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

the class CalciteAssert method assertQuery.

static void assertQuery(Connection connection, String sql, int limit, boolean materializationsEnabled, List<Pair<Hook, Function>> hooks, Function<ResultSet, Void> resultChecker, Function<Integer, Void> updateChecker, Function<Throwable, Void> exceptionChecker) throws Exception {
    final String message = "With materializationsEnabled=" + materializationsEnabled + ", limit=" + limit;
    try (final Closer closer = new Closer()) {
        if (connection instanceof CalciteConnection) {
            CalciteConnection calciteConnection = (CalciteConnection) connection;
            calciteConnection.getProperties().setProperty(CalciteConnectionProperty.MATERIALIZATIONS_ENABLED.camelName(), Boolean.toString(materializationsEnabled));
            calciteConnection.getProperties().setProperty(CalciteConnectionProperty.CREATE_MATERIALIZATIONS.camelName(), Boolean.toString(materializationsEnabled));
            if (!calciteConnection.getProperties().containsKey(CalciteConnectionProperty.TIME_ZONE.camelName())) {
                // Do not override id some test has already set this property.
                calciteConnection.getProperties().setProperty(CalciteConnectionProperty.TIME_ZONE.camelName(), DateTimeUtils.UTC_ZONE.getID());
            }
        }
        for (Pair<Hook, Function> hook : hooks) {
            closer.add(hook.left.addThread(hook.right));
        }
        Statement statement = connection.createStatement();
        statement.setMaxRows(limit <= 0 ? limit : Math.max(limit, 1));
        ResultSet resultSet = null;
        Integer updateCount = null;
        try {
            if (updateChecker == null) {
                resultSet = statement.executeQuery(sql);
            } else {
                updateCount = statement.executeUpdate(sql);
            }
            if (exceptionChecker != null) {
                exceptionChecker.apply(null);
                return;
            }
        } catch (Exception | Error e) {
            if (exceptionChecker != null) {
                exceptionChecker.apply(e);
                return;
            }
            throw e;
        }
        if (resultChecker != null) {
            resultChecker.apply(resultSet);
        }
        if (updateChecker != null) {
            updateChecker.apply(updateCount);
        }
        if (resultSet != null) {
            resultSet.close();
        }
        statement.close();
        connection.close();
    } catch (Error | RuntimeException e) {
        // at the very top level of the exception stack.
        throw e;
    } catch (Throwable e) {
        throw new RuntimeException(message, e);
    }
}
Also used : Closer(org.apache.calcite.util.Closer) Hook(org.apache.calcite.runtime.Hook) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) InvocationTargetException(java.lang.reflect.InvocationTargetException) CalciteException(org.apache.calcite.runtime.CalciteException) SQLException(java.sql.SQLException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) SqlValidatorException(org.apache.calcite.sql.validate.SqlValidatorException) ExecutionException(java.util.concurrent.ExecutionException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Function(com.google.common.base.Function) ResultSet(java.sql.ResultSet) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

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