Search in sources :

Example 1 with VerdictSingleResultFromDbmsQueryResult

use of org.verdictdb.coordinator.VerdictSingleResultFromDbmsQueryResult in project traindb by traindb-project.

the class TrainDBQueryEngine method trainModelInstance.

@Override
public void trainModelInstance(String modelName, String modelInstanceName, String schemaName, String tableName, List<String> columnNames) throws Exception {
    if (catalogContext.modelInstanceExists(modelInstanceName)) {
        throw new CatalogException("model instance '" + modelInstanceName + "' already exist");
    }
    if (schemaName == null) {
        schemaName = conn.getDefaultSchema();
    }
    JSONObject tableMetadata = getTableMetadata(schemaName, tableName, columnNames);
    Path instancePath = catalogContext.getModelInstancePath(modelName, modelInstanceName);
    Files.createDirectories(instancePath);
    String outputPath = instancePath.toString();
    // write metadata for model training scripts in python
    String metadataFilename = outputPath + "/metadata.json";
    FileWriter fileWriter = new FileWriter(metadataFilename);
    fileWriter.write(tableMetadata.toJSONString());
    fileWriter.flush();
    fileWriter.close();
    // FIXME securely pass training data for ML model training
    DbmsQueryResult trainingData = getTrainingData(schemaName, tableName, columnNames);
    String dataFilename = outputPath + "/data.csv";
    FileWriter datafileWriter = new FileWriter(dataFilename);
    datafileWriter.write(new VerdictSingleResultFromDbmsQueryResult(trainingData).toCsv());
    datafileWriter.flush();
    datafileWriter.close();
    MModel mModel = catalogContext.getModel(modelName);
    // train ML model
    ProcessBuilder pb = new ProcessBuilder("python", conf.getModelRunnerPath(), "train", mModel.getClassName(), TrainDBConfiguration.absoluteUri(mModel.getUri()), dataFilename, metadataFilename, outputPath);
    pb.inheritIO();
    Process process = pb.start();
    process.waitFor();
    catalogContext.trainModelInstance(modelName, modelInstanceName, schemaName, tableName, columnNames);
}
Also used : Path(java.nio.file.Path) JSONObject(org.json.simple.JSONObject) VerdictSingleResultFromDbmsQueryResult(org.verdictdb.coordinator.VerdictSingleResultFromDbmsQueryResult) FileWriter(java.io.FileWriter) CatalogException(traindb.catalog.CatalogException) MModel(traindb.catalog.pm.MModel) DbmsQueryResult(org.verdictdb.connection.DbmsQueryResult) VerdictSingleResultFromDbmsQueryResult(org.verdictdb.coordinator.VerdictSingleResultFromDbmsQueryResult)

Example 2 with VerdictSingleResultFromDbmsQueryResult

use of org.verdictdb.coordinator.VerdictSingleResultFromDbmsQueryResult 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

VerdictSingleResultFromDbmsQueryResult (org.verdictdb.coordinator.VerdictSingleResultFromDbmsQueryResult)2 FileWriter (java.io.FileWriter)1 Path (java.nio.file.Path)1 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 JSONObject (org.json.simple.JSONObject)1 CachedDbmsConnection (org.verdictdb.connection.CachedDbmsConnection)1 DbmsConnection (org.verdictdb.connection.DbmsConnection)1 DbmsQueryResult (org.verdictdb.connection.DbmsQueryResult)1 JdbcConnection (org.verdictdb.connection.JdbcConnection)1 JdbcQueryResult (org.verdictdb.connection.JdbcQueryResult)1 CatalogException (traindb.catalog.CatalogException)1