@Test public void testGetTimestampObject() throws Exception { CalciteAssert.that() .with(CalciteAssert.Config.JDBC_FOODMART) .doWithConnection(connection -> { try { Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery( "select \"hire_date\" from \"foodmart\".\"employee\" where \"employee_id\" = 1"); assertTrue(rs.next()); assertEquals( Timestamp.valueOf("1994-12-01 00:00:00"), rs.getTimestamp(1)); assertFalse(rs.next()); } catch (SQLException e) { throw new RuntimeException(e); } }); }
/** Test case for bug where if two tables have different element classes * but those classes have identical fields, Calcite would generate code to use * the wrong element class; a {@link ClassCastException} would ensue. */ @Test public void testDifferentTypesSameFields() throws Exception { Connection connection = DriverManager.getConnection("jdbc:calcite:"); CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); final SchemaPlus rootSchema = calciteConnection.getRootSchema(); rootSchema.add("TEST", new ReflectiveSchema(new MySchema())); Statement statement = calciteConnection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT \"myvalue\" from TEST.\"mytable2\""); assertEquals("myvalue=2\n", CalciteAssert.toString(resultSet)); resultSet.close(); statement.close(); connection.close(); }
@Test public void testGetTimestampObject() throws Exception { CalciteAssert.that() .with(CalciteAssert.Config.JDBC_FOODMART) .doWithConnection(connection -> { try { Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery( "select \"hire_date\" from \"foodmart\".\"employee\" where \"employee_id\" = 1"); assertTrue(rs.next()); assertEquals( Timestamp.valueOf("1994-12-01 00:00:00"), rs.getTimestamp(1)); assertFalse(rs.next()); } catch (SQLException e) { throw new RuntimeException(e); } }); }
/** * 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(); }
/** Tests accessing a column in a JDBC source whose type is DATE. */ @Test public void testGetDate() throws Exception { CalciteAssert.that() .with(CalciteAssert.Config.JDBC_FOODMART) .doWithConnection(connection -> { try { Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery( "select min(\"date\") mindate from \"foodmart\".\"currency\""); assertTrue(rs.next()); assertEquals( Date.valueOf("1997-01-01"), rs.getDate(1)); assertFalse(rs.next()); } catch (SQLException e) { throw new RuntimeException(e); } }); }
/** * 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(); }
/** Test case for bug where if two tables have different element classes * but those classes have identical fields, Calcite would generate code to use * the wrong element class; a {@link ClassCastException} would ensue. */ @Test public void testDifferentTypesSameFields() throws Exception { Connection connection = DriverManager.getConnection("jdbc:calcite:"); CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class); final SchemaPlus rootSchema = calciteConnection.getRootSchema(); rootSchema.add("TEST", new ReflectiveSchema(new MySchema())); Statement statement = calciteConnection.createStatement(); ResultSet resultSet = statement.executeQuery("SELECT \"myvalue\" from TEST.\"mytable2\""); assertEquals("myvalue=2\n", CalciteAssert.toString(resultSet)); resultSet.close(); statement.close(); connection.close(); }
/** Tests accessing a column in a JDBC source whose type is DATE. */ @Test public void testGetDate() throws Exception { CalciteAssert.that() .with(CalciteAssert.Config.JDBC_FOODMART) .doWithConnection(connection -> { try { Statement stmt = connection.createStatement(); ResultSet rs = stmt.executeQuery( "select min(\"date\") mindate from \"foodmart\".\"currency\""); assertTrue(rs.next()); assertEquals( Date.valueOf("1997-01-01"), rs.getDate(1)); assertFalse(rs.next()); } catch (SQLException e) { throw new RuntimeException(e); } }); }
/** Tests saving query results into temporary tables, per * {@link org.apache.calcite.avatica.Handler.ResultSink}. */ @Test public void testAutomaticTemporaryTable() throws Exception { final List<Object> objects = new ArrayList<>(); CalciteAssert.that() .with( new CalciteAssert.ConnectionFactory() { public CalciteConnection createConnection() throws SQLException { CalciteConnection connection = (CalciteConnection) new AutoTempDriver(objects) .connect("jdbc:calcite:", new Properties()); final SchemaPlus rootSchema = connection.getRootSchema(); rootSchema.add("hr", new ReflectiveSchema(new HrSchema())); connection.setSchema("hr"); return connection; } }) .doWithConnection(connection -> { try { final String sql = "select * from \"hr\".\"emps\" " + "where \"deptno\" = 10"; connection.createStatement() .executeQuery(sql); assertThat(objects.size(), is(1)); } catch (SQLException e) { throw new RuntimeException(e); } }); }
/** Tests saving query results into temporary tables, per * {@link org.apache.calcite.avatica.Handler.ResultSink}. */ @Test public void testAutomaticTemporaryTable() throws Exception { final List<Object> objects = new ArrayList<>(); CalciteAssert.that() .with( new CalciteAssert.ConnectionFactory() { public CalciteConnection createConnection() throws SQLException { CalciteConnection connection = (CalciteConnection) new AutoTempDriver(objects) .connect("jdbc:calcite:", new Properties()); final SchemaPlus rootSchema = connection.getRootSchema(); rootSchema.add("hr", new ReflectiveSchema(new HrSchema())); connection.setSchema("hr"); return connection; } }) .doWithConnection(connection -> { try { final String sql = "select * from \"hr\".\"emps\" " + "where \"deptno\" = 10"; connection.createStatement() .executeQuery(sql); assertThat(objects.size(), is(1)); } catch (SQLException e) { throw new RuntimeException(e); } }); }
/** Tests that {@link java.sql.Statement#setMaxRows(int)} is honored. */ @Test public void testSetMaxRows() throws Exception { CalciteAssert.hr() .doWithConnection(connection -> { try { final Statement statement = connection.createStatement(); try { statement.setMaxRows(-1); fail("expected error"); } catch (SQLException e) { assertEquals(e.getMessage(), "illegal maxRows value: -1"); } statement.setMaxRows(2); assertEquals(2, statement.getMaxRows()); final ResultSet resultSet = statement.executeQuery( "select * from \"hr\".\"emps\""); assertTrue(resultSet.next()); assertTrue(resultSet.next()); assertFalse(resultSet.next()); resultSet.close(); statement.close(); } catch (SQLException e) { throw new RuntimeException(e); } }); }
/** Tests that {@link java.sql.Statement#setMaxRows(int)} is honored. */ @Test public void testSetMaxRows() throws Exception { CalciteAssert.hr() .doWithConnection(connection -> { try { final Statement statement = connection.createStatement(); try { statement.setMaxRows(-1); fail("expected error"); } catch (SQLException e) { assertEquals(e.getMessage(), "illegal maxRows value: -1"); } statement.setMaxRows(2); assertEquals(2, statement.getMaxRows()); final ResultSet resultSet = statement.executeQuery( "select * from \"hr\".\"emps\""); assertTrue(resultSet.next()); assertTrue(resultSet.next()); assertFalse(resultSet.next()); resultSet.close(); statement.close(); } catch (SQLException e) { throw new RuntimeException(e); } }); }
/** Test case for * <a href="https://issues.apache.org/jira/browse/CALCITE-281">[CALCITE-1919] * NPE when target in ReflectiveSchema belongs to the unnamed package</a>. */ @Test public void testReflectiveSchemaInUnnamedPackage() throws Exception { final Driver driver = new Driver(); try (CalciteConnection connection = (CalciteConnection) driver.connect("jdbc:calcite:", new Properties())) { SchemaPlus rootSchema = connection.getRootSchema(); final Class<?> c = Class.forName("RootHr"); final Object o = c.getDeclaredConstructor().newInstance(); rootSchema.add("hr", new ReflectiveSchema(o)); connection.setSchema("hr"); final Statement statement = connection.createStatement(); final String sql = "select * from \"emps\""; final ResultSet resultSet = statement.executeQuery(sql); final String expected = "empid=100; name=Bill\n" + "empid=200; name=Eric\n" + "empid=150; name=Sebastian\n"; assertThat(CalciteAssert.toString(resultSet), is(expected)); } }
/** Test case for * <a href="https://issues.apache.org/jira/browse/CALCITE-281">[CALCITE-1919] * NPE when target in ReflectiveSchema belongs to the unnamed package</a>. */ @Test public void testReflectiveSchemaInUnnamedPackage() throws Exception { final Driver driver = new Driver(); try (CalciteConnection connection = (CalciteConnection) driver.connect("jdbc:calcite:", new Properties())) { SchemaPlus rootSchema = connection.getRootSchema(); final Class<?> c = Class.forName("RootHr"); final Object o = c.getDeclaredConstructor().newInstance(); rootSchema.add("hr", new ReflectiveSchema(o)); connection.setSchema("hr"); final Statement statement = connection.createStatement(); final String sql = "select * from \"emps\""; final ResultSet resultSet = statement.executeQuery(sql); final String expected = "empid=100; name=Bill\n" + "empid=200; name=Eric\n" + "empid=150; name=Sebastian\n"; assertThat(CalciteAssert.toString(resultSet), is(expected)); } }
rootSchema.add("sale", new ReflectiveSchema(new Smalls.WideSaleSchema())); connection.setSchema("sale"); final Statement statement = connection.createStatement();
rootSchema.add("sale", new ReflectiveSchema(new Smalls.WideSaleSchema())); connection.setSchema("sale"); final Statement statement = connection.createStatement();
Statement statement = calciteConnection.createStatement(); ResultSet resultSet = statement.executeQuery("select A, SUM(B) from SAMPLE group by A");
Statement statement = calciteConnection.createStatement(); ResultSet resultSet = statement.executeQuery("select A, SUM(B) from SAMPLE group by A");
rootSchema.add("hr", new ReflectiveSchema(new HrSchema())); connection.setSchema("hr"); final Statement statement = connection.createStatement(); assertFalse((Boolean) CalciteAssert.call(statement, "isCloseOnCompletion")); CalciteAssert.call(statement, "closeOnCompletion");
rootSchema.add("hr", new ReflectiveSchema(new HrSchema())); connection.setSchema("hr"); final Statement statement = connection.createStatement(); assertFalse((Boolean) CalciteAssert.call(statement, "isCloseOnCompletion")); CalciteAssert.call(statement, "closeOnCompletion");