Search in sources :

Example 1 with SqlAdvisorGetHintsFunction

use of org.apache.calcite.sql.advise.SqlAdvisorGetHintsFunction in project calcite by apache.

the class JdbcTest method adviseSql.

private void adviseSql(String sql, Function<ResultSet, Void> checker) throws ClassNotFoundException, SQLException {
    Properties info = new Properties();
    info.put("lex", "JAVA");
    info.put("quoting", "DOUBLE_QUOTE");
    Connection connection = DriverManager.getConnection("jdbc:calcite:", info);
    CalciteConnection calciteConnection = connection.unwrap(CalciteConnection.class);
    SchemaPlus rootSchema = calciteConnection.getRootSchema();
    rootSchema.add("hr", new ReflectiveSchema(new HrSchema()));
    SchemaPlus schema = rootSchema.add("s", new AbstractSchema());
    calciteConnection.setSchema("hr");
    final TableFunction table = new SqlAdvisorGetHintsFunction();
    schema.add("get_hints", table);
    PreparedStatement ps = connection.prepareStatement("select *\n" + "from table(\"s\".\"get_hints\"(?, ?)) as t(id, names, type)");
    SqlParserUtil.StringAndPos sap = SqlParserUtil.findPos(sql);
    ps.setString(1, sap.sql);
    ps.setInt(2, sap.cursor);
    final ResultSet resultSet = ps.executeQuery();
    checker.apply(resultSet);
    resultSet.close();
    connection.close();
}
Also used : SqlAdvisorGetHintsFunction(org.apache.calcite.sql.advise.SqlAdvisorGetHintsFunction) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection) AvaticaConnection(org.apache.calcite.avatica.AvaticaConnection) Connection(java.sql.Connection) SchemaPlus(org.apache.calcite.schema.SchemaPlus) ReflectiveSchema(org.apache.calcite.adapter.java.ReflectiveSchema) PreparedStatement(java.sql.PreparedStatement) Properties(java.util.Properties) SqlParserUtil(org.apache.calcite.sql.parser.SqlParserUtil) AbstractSchema(org.apache.calcite.schema.impl.AbstractSchema) ResultSet(java.sql.ResultSet) TableFunction(org.apache.calcite.schema.TableFunction) CalciteConnection(org.apache.calcite.jdbc.CalciteConnection)

Aggregations

Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 Properties (java.util.Properties)1 ReflectiveSchema (org.apache.calcite.adapter.java.ReflectiveSchema)1 AvaticaConnection (org.apache.calcite.avatica.AvaticaConnection)1 CalciteConnection (org.apache.calcite.jdbc.CalciteConnection)1 SchemaPlus (org.apache.calcite.schema.SchemaPlus)1 TableFunction (org.apache.calcite.schema.TableFunction)1 AbstractSchema (org.apache.calcite.schema.impl.AbstractSchema)1 SqlAdvisorGetHintsFunction (org.apache.calcite.sql.advise.SqlAdvisorGetHintsFunction)1 SqlParserUtil (org.apache.calcite.sql.parser.SqlParserUtil)1