Search in sources :

Example 56 with CalciteConnection

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

the class JdbcTest method testAggMultipleMeasures.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-1097">[CALCITE-1097]
 * Exception when executing query with too many aggregation columns</a>.
 */
@Test
public void testAggMultipleMeasures() throws SQLException {
    final Driver driver = new Driver();
    CalciteConnection connection = (CalciteConnection) driver.connect("jdbc:calcite:", new Properties());
    SchemaPlus rootSchema = connection.getRootSchema();
    rootSchema.add("sale", new ReflectiveSchema(new Smalls.WideSaleSchema()));
    connection.setSchema("sale");
    final Statement statement = connection.createStatement();
    // 200 columns: sum(sale0) + ... sum(sale199)
    ResultSet resultSet = statement.executeQuery("select s.\"prodId\"" + sums(200, true) + "\n" + "from \"sale\".\"prod\" as s group by s.\"prodId\"\n");
    assertThat(resultSet.next(), is(true));
    assertThat(resultSet.getInt(1), is(100));
    assertThat(resultSet.getInt(2), is(10));
    assertThat(resultSet.getInt(200), is(10));
    assertThat(resultSet.next(), is(false));
    // 800 columns:
    // sum(sale0 + 0) + ... + sum(sale0 + 100) + ... sum(sale99 + 799)
    final int n = 800;
    resultSet = statement.executeQuery("select s.\"prodId\"" + sums(n, false) + "\n" + "from \"sale\".\"prod\" as s group by s.\"prodId\"\n");
    assertThat(resultSet.next(), is(true));
    assertThat(resultSet.getInt(1), is(100));
    assertThat(resultSet.getInt(2), is(10));
    assertThat(resultSet.getInt(n), is(n + 8));
    assertThat(resultSet.next(), is(false));
    connection.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) AvaticaStatement(org.apache.calcite.avatica.AvaticaStatement) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ResultSet(java.sql.ResultSet) Driver(org.apache.calcite.jdbc.Driver) org.hsqldb.jdbcDriver(org.hsqldb.jdbcDriver) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) Properties(java.util.Properties) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 57 with CalciteConnection

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

the class JdbcTest method testUpdateBind.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-2054">[CALCITE-2054]
 * Error while validating UPDATE with dynamic parameter in SET clause</a>.
 */
@Test
public void testUpdateBind() throws Exception {
    String hsqldbMemUrl = "jdbc:hsqldb:mem:.";
    try (Connection baseConnection = DriverManager.getConnection(hsqldbMemUrl);
        Statement baseStmt = baseConnection.createStatement()) {
        baseStmt.execute("CREATE TABLE T2 (\n" + "ID INTEGER,\n" + "VALS DOUBLE)");
        baseStmt.execute("INSERT INTO T2 VALUES (1, 1.0)");
        baseStmt.execute("INSERT INTO T2 VALUES (2, null)");
        baseStmt.execute("INSERT INTO T2 VALUES (null, 2.0)");
        baseStmt.close();
        baseConnection.commit();
        Properties info = new Properties();
        final String 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: '" + hsqldbMemUrl + "',\n" + "       jdbcCatalog: null,\n" + "       jdbcSchema: null\n" + "     }\n" + "  ]\n" + "}";
        info.put("model", model);
        Connection calciteConnection = DriverManager.getConnection("jdbc:calcite:", info);
        ResultSet rs = calciteConnection.prepareStatement("select * from t2").executeQuery();
        assertThat(rs.next(), is(true));
        assertThat((Integer) rs.getObject("ID"), is(1));
        assertThat((Double) rs.getObject("VALS"), is(1.0));
        assertThat(rs.next(), is(true));
        assertThat((Integer) rs.getObject("ID"), is(2));
        assertThat(rs.getObject("VALS"), nullValue());
        assertThat(rs.next(), is(true));
        assertThat(rs.getObject("ID"), nullValue());
        assertThat((Double) rs.getObject("VALS"), equalTo(2.0));
        rs.close();
        final String sql = "update t2 set vals=? where id=?";
        try (PreparedStatement ps = calciteConnection.prepareStatement(sql)) {
            ParameterMetaData pmd = ps.getParameterMetaData();
            assertThat(pmd.getParameterCount(), is(2));
            assertThat(pmd.getParameterType(1), is(Types.DOUBLE));
            assertThat(pmd.getParameterType(2), is(Types.INTEGER));
            ps.close();
        }
        calciteConnection.close();
    }
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) AvaticaStatement(org.apache.calcite.avatica.AvaticaStatement) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Properties(java.util.Properties) ParameterMetaData(java.sql.ParameterMetaData) Test(org.junit.Test)

Example 58 with CalciteConnection

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

the class JdbcTest method testReadme.

/**
 * The example in the README.
 */
@Test
public void testReadme() throws ClassNotFoundException, SQLException {
    Properties info = new Properties();
    info.setProperty("lex", "JAVA");
    Connection connection = DriverManager.getConnection("jdbc:calcite:", info);
    CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
    final SchemaPlus rootSchema = calciteConnection.getRootSchema();
    rootSchema.add("hr", new ReflectiveSchema(new HrSchema()));
    Statement statement = calciteConnection.createStatement();
    ResultSet resultSet = statement.executeQuery("select d.deptno, min(e.empid)\n" + "from hr.emps as e\n" + "join hr.depts as d\n" + "  on e.deptno = d.deptno\n" + "group by d.deptno\n" + "having count(*) > 1");
    final String s = CalciteAssert.toString(resultSet);
    assertThat(s, notNullValue());
    resultSet.close();
    statement.close();
    connection.close();
}
Also used : PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) AvaticaStatement(org.apache.calcite.avatica.AvaticaStatement) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) Connection(java.sql.Connection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ResultSet(java.sql.ResultSet) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Properties(java.util.Properties) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 59 with CalciteConnection

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

the class JdbcTest method testOnConnectionClose.

/**
 * Tests {@link org.apache.calcite.avatica.Handler#onConnectionClose}
 * and  {@link org.apache.calcite.avatica.Handler#onStatementClose}.
 */
@Test
public void testOnConnectionClose() throws Exception {
    final int[] closeCount = { 0 };
    final int[] statementCloseCount = { 0 };
    final HandlerImpl h = new HandlerImpl() {

        @Override
        public void onConnectionClose(AvaticaConnection connection) {
            ++closeCount[0];
            throw new RuntimeException();
        }

        @Override
        public void onStatementClose(AvaticaStatement statement) {
            ++statementCloseCount[0];
            throw new RuntimeException();
        }
    };
    try (final TryThreadLocal.Memo ignore = HandlerDriver.HANDLERS.push(h)) {
        final HandlerDriver driver = new HandlerDriver();
        CalciteConnection connection = (CalciteConnection) driver.connect("jdbc:calcite:", new Properties());
        SchemaPlus rootSchema = connection.getRootSchema();
        rootSchema.add("hr", new ReflectiveSchema(new HrSchema()));
        connection.setSchema("hr");
        final Statement statement = connection.createStatement();
        final ResultSet resultSet = statement.executeQuery("select * from \"emps\"");
        assertEquals(0, closeCount[0]);
        assertEquals(0, statementCloseCount[0]);
        resultSet.close();
        try {
            resultSet.next();
            fail("resultSet.next() should throw SQLException when closed");
        } catch (SQLException e) {
            assertThat(e.getMessage(), containsString("next() called on closed cursor"));
        }
        assertEquals(0, closeCount[0]);
        assertEquals(0, statementCloseCount[0]);
        // Close statement. It throws SQLException, but statement is still closed.
        try {
            statement.close();
            fail("expecting error");
        } catch (SQLException e) {
        // ok
        }
        assertEquals(0, closeCount[0]);
        assertEquals(1, statementCloseCount[0]);
        // Close connection. It throws SQLException, but connection is still closed.
        try {
            connection.close();
            fail("expecting error");
        } catch (SQLException e) {
        // ok
        }
        assertEquals(1, closeCount[0]);
        assertEquals(1, statementCloseCount[0]);
        // Close a closed connection. Handler is not called again.
        connection.close();
        assertEquals(1, closeCount[0]);
        assertEquals(1, statementCloseCount[0]);
    }
}
Also used : SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) AvaticaStatement(org.apache.calcite.avatica.AvaticaStatement) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) TryThreadLocal(org.apache.calcite.util.TryThreadLocal) Properties(java.util.Properties) HandlerImpl(org.apache.calcite.avatica.HandlerImpl) AvaticaStatement(org.apache.calcite.avatica.AvaticaStatement) AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) ResultSet(java.sql.ResultSet) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Test(org.junit.Test)

Example 60 with CalciteConnection

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

the class JdbcTest method testTableMacro.

/**
 * Tests a relation that is accessed via method syntax.
 *
 * <p>The function ({@link Smalls#view(String)} has a return type
 * {@link Table} and the actual returned value implements
 * {@link org.apache.calcite.schema.TranslatableTable}.
 */
@Test
public void testTableMacro() throws SQLException, ClassNotFoundException {
    Connection connection = DriverManager.getConnection("jdbc:calcite:");
    CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
    SchemaPlus rootSchema = calciteConnection.getRootSchema();
    SchemaPlus schema = rootSchema.add("s", new AbstractSchema());
    final TableMacro tableMacro = TableMacroImpl.create(Smalls.VIEW_METHOD);
    schema.add("View", tableMacro);
    ResultSet resultSet = connection.createStatement().executeQuery("select *\n" + "from table(\"s\".\"View\"('(10), (20)')) as t(n)\n" + "where n < 15");
    // The call to "View('(10), (2)')" expands to 'values (1), (3), (10), (20)'.
    assertThat(CalciteAssert.toString(resultSet), equalTo("N=1\n" + "N=3\n" + "N=10\n"));
    connection.close();
}
Also used : TableMacro(org.apache.calcite.schema.TableMacro) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) Connection(java.sql.Connection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ResultSet(java.sql.ResultSet) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) 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