Search in sources :

Example 1 with JdbcQueryResult

use of org.verdictdb.connection.JdbcQueryResult in project traindb by traindb-project.

the class TrainDBQueryEngine method processQuery.

@Override
public VerdictSingleResult processQuery(String query) throws Exception {
    SqlParser.Config parserConf = SqlParser.config().withParserFactory(TrainDBCalciteSQLParserImpl.FACTORY).withUnquotedCasing(Casing.TO_LOWER);
    FrameworkConfig config = Frameworks.newConfigBuilder().defaultSchema(schemaManager.getCurrentSchema()).parserConfig(parserConf).build();
    Planner planner = Frameworks.getPlanner(config);
    SqlNode parse = planner.parse(query);
    TableNameQualifier.toFullyQualifiedName(schemaManager, conn.getDefaultSchema(), parse);
    LOG.debug("Parsed query: " + parse.toString());
    SqlNode validate = planner.validate(parse);
    RelRoot relRoot = planner.rel(validate);
    LOG.debug(RelOptUtil.dumpPlan("Generated plan: ", relRoot.rel, SqlExplainFormat.TEXT, SqlExplainLevel.ALL_ATTRIBUTES));
    SqlDialect.DatabaseProduct dp = SqlDialect.DatabaseProduct.POSTGRESQL;
    String queryString = validate.toSqlString(dp.getDialect()).getSql();
    LOG.debug("query string: " + queryString);
    try {
        Connection internalConn = DriverManager.getConnection("jdbc:traindb-calcite:");
        PreparedStatement stmt = internalConn.prepareStatement(queryString);
        ResultSet rs = stmt.executeQuery();
        return new VerdictSingleResultFromDbmsQueryResult(new JdbcQueryResult(rs));
    } catch (SQLException e) {
        LOG.debug(ExceptionUtils.getStackTrace(e));
    }
    return null;
}
Also used : SQLException(java.sql.SQLException) SqlParser(org.apache.calcite.sql.parser.SqlParser) Connection(java.sql.Connection) CachedDbmsConnection(org.verdictdb.connection.CachedDbmsConnection) JdbcConnection(org.verdictdb.connection.JdbcConnection) DbmsConnection(org.verdictdb.connection.DbmsConnection) JdbcQueryResult(org.verdictdb.connection.JdbcQueryResult) RelRoot(org.apache.calcite.rel.RelRoot) PreparedStatement(java.sql.PreparedStatement) VerdictSingleResultFromDbmsQueryResult(org.verdictdb.coordinator.VerdictSingleResultFromDbmsQueryResult) ResultSet(java.sql.ResultSet) SqlDialect(org.apache.calcite.sql.SqlDialect) Planner(org.apache.calcite.tools.Planner) FrameworkConfig(org.apache.calcite.tools.FrameworkConfig) SqlNode(org.apache.calcite.sql.SqlNode)

Aggregations

Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 RelRoot (org.apache.calcite.rel.RelRoot)1 SqlDialect (org.apache.calcite.sql.SqlDialect)1 SqlNode (org.apache.calcite.sql.SqlNode)1 SqlParser (org.apache.calcite.sql.parser.SqlParser)1 FrameworkConfig (org.apache.calcite.tools.FrameworkConfig)1 Planner (org.apache.calcite.tools.Planner)1 CachedDbmsConnection (org.verdictdb.connection.CachedDbmsConnection)1 DbmsConnection (org.verdictdb.connection.DbmsConnection)1 JdbcConnection (org.verdictdb.connection.JdbcConnection)1 JdbcQueryResult (org.verdictdb.connection.JdbcQueryResult)1 VerdictSingleResultFromDbmsQueryResult (org.verdictdb.coordinator.VerdictSingleResultFromDbmsQueryResult)1