Search in sources :

Example 11 with ReflectiveSchema

use of org.apache.calcite.adapter.java.ReflectiveSchema in project calcite by apache.

the class ReflectiveSchemaTest method testOperator.

/**
 * Tests a relation that is accessed via method syntax.
 * The function returns a {@link org.apache.calcite.linq4j.Queryable}.
 */
@Ignore
@Test
public void testOperator() 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());
    schema.add("GenerateStrings", TableMacroImpl.create(Smalls.GENERATE_STRINGS_METHOD));
    schema.add("StringUnion", TableMacroImpl.create(Smalls.STRING_UNION_METHOD));
    rootSchema.add("hr", new ReflectiveSchema(new JdbcTest.HrSchema()));
    ResultSet resultSet = connection.createStatement().executeQuery("select *\n" + "from table(s.StringUnion(\n" + "  GenerateStrings(5),\n" + "  cursor (select name from emps)))\n" + "where char_length(s) > 3");
    assertTrue(resultSet.next());
}
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) ResultSet(java.sql.ResultSet) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 12 with ReflectiveSchema

use of org.apache.calcite.adapter.java.ReflectiveSchema 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 13 with ReflectiveSchema

use of org.apache.calcite.adapter.java.ReflectiveSchema in project calcite by apache.

the class JdbcTest method testTrivialSort.

/**
 * Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-1015">[CALCITE-1015]
 * OFFSET 0 causes AssertionError</a>.
 */
@Test
public void testTrivialSort() {
    final String sql = "select a.\"value\", b.\"value\"\n" + "  from \"bools\" a\n" + "     , \"bools\" b\n" + " offset 0";
    CalciteAssert.that().withSchema("s", new ReflectiveSchema(new ReflectiveSchemaTest.CatchallSchema())).query(sql).returnsUnordered("value=T; value=T", "value=T; value=F", "value=T; value=null", "value=F; value=T", "value=F; value=F", "value=F; value=null");
}
Also used : ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) CoreMatchers.containsString(org.hamcrest.CoreMatchers.containsString) Test(org.junit.Test)

Example 14 with ReflectiveSchema

use of org.apache.calcite.adapter.java.ReflectiveSchema 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 15 with ReflectiveSchema

use of org.apache.calcite.adapter.java.ReflectiveSchema 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)

Aggregations

ReflectiveSchema (org.apache.calcite.adapter.java.ReflectiveSchema)22 SchemaPlus (org.apache.calcite.schema.SchemaPlus)21 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)16 Test (org.junit.Test)16 ResultSet (java.sql.ResultSet)13 Connection (java.sql.Connection)11 Statement (java.sql.Statement)11 PreparedStatement (java.sql.PreparedStatement)7 Properties (java.util.Properties)7 AbstractSchema (org.apache.calcite.schema.impl.AbstractSchema)7 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)6 AvaticaStatement (org.apache.calcite.avatica.AvaticaStatement)5 AvaticaConnection (org.apache.calcite.avatica.AvaticaConnection)4 Driver (org.apache.calcite.jdbc.Driver)3 RelNode (org.apache.calcite.rel.RelNode)3 SQLException (java.sql.SQLException)2 ArrayList (java.util.ArrayList)2 RelOptPlanner (org.apache.calcite.plan.RelOptPlanner)2 SqlNode (org.apache.calcite.sql.SqlNode)2 JdbcTest (org.apache.calcite.test.JdbcTest)2