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());
}
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();
}
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");
}
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();
}
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]);
}
}
Aggregations