Java Code Examples for org.apache.calcite.jdbc.CalciteConnection#createStatement()

The following examples show how to use org.apache.calcite.jdbc.CalciteConnection#createStatement() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TableInRootSchemaTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** Test case for
 * <a href="https://issues.apache.org/jira/browse/CALCITE-85">[CALCITE-85]
 * Adding a table to the root schema causes breakage in
 * CalcitePrepareImpl</a>. */
@Test void testAddingTableInRootSchema() throws Exception {
  Connection connection = DriverManager.getConnection("jdbc:calcite:");
  CalciteConnection calciteConnection =
      connection.unwrap(CalciteConnection.class);

  calciteConnection.getRootSchema().add("SAMPLE", new SimpleTable());
  Statement statement = calciteConnection.createStatement();
  ResultSet resultSet =
      statement.executeQuery("select A, SUM(B) from SAMPLE group by A");

  assertThat(
      ImmutableMultiset.of(
          "A=foo; EXPR$1=8",
          "A=bar; EXPR$1=4"),
      equalTo(CalciteAssert.toSet(resultSet)));

  final ResultSetMetaData resultSetMetaData = resultSet.getMetaData();
  assertThat(resultSetMetaData.getColumnName(1), equalTo("A"));
  assertThat(resultSetMetaData.getTableName(1), equalTo("SAMPLE"));
  assertThat(resultSetMetaData.getSchemaName(1), nullValue());
  assertThat(resultSetMetaData.getColumnClassName(1),
      equalTo("java.lang.String"));
  // Per JDBC, column name should be null. But DBUnit requires every column
  // to have a name, so the driver uses the label.
  assertThat(resultSetMetaData.getColumnName(2), equalTo("EXPR$1"));
  assertThat(resultSetMetaData.getTableName(2), nullValue());
  assertThat(resultSetMetaData.getSchemaName(2), nullValue());
  assertThat(resultSetMetaData.getColumnClassName(2),
      equalTo("java.lang.Integer"));
  resultSet.close();
  statement.close();
  connection.close();
}
 
Example 2
Source File: RelMdColumnOriginsTest.java    From calcite with Apache License 2.0 5 votes vote down vote up
/** 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 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();
}