Search in sources :

Example 1 with PreparedStatementParameterDTO

use of io.hops.hopsworks.common.featurestore.query.PreparedStatementParameterDTO in project hopsworks by logicalclocks.

the class PreparedStatementBuilder method buildDTO.

private ServingPreparedStatementDTO buildDTO(Query query, List<Feature> primaryKeys, Integer featureGroupId, Integer statementIndex, boolean batch) throws FeaturestoreException {
    // create primary key prepared statement filters for the query
    List<PreparedStatementParameterDTO> stmtParameters = new ArrayList<>();
    // Change the type of PK to PREPARED_STATEMENT_TYPE. This will avoid having the query constructor
    // adding additional quotes around the ? sign
    primaryKeys.forEach(f -> f.setType(PREPARED_STATEMENT_TYPE));
    // record pk position in the prepared statement - start from 1 as that's how
    // prepared statements work.
    int primaryKeyIndex = 1;
    // First condition doesn't have any "AND"
    // we are guaranteed there is at least one primary key, as no primary key situations are filtered above
    Feature pkFeature = primaryKeys.get(0);
    stmtParameters.add(new PreparedStatementParameterDTO(pkFeature.getName(), primaryKeyIndex++));
    FilterLogic filterLogic;
    if (batch) {
        filterLogic = new FilterLogic(new Filter(primaryKeys, SqlCondition.IN, "?"));
        query.setOrderByFeatures(primaryKeys);
    } else {
        filterLogic = new FilterLogic(new Filter(Arrays.asList(pkFeature), SqlCondition.EQUALS, "?"));
    }
    // Concatenate conditions
    for (int i = 1; i < primaryKeys.size(); i++) {
        pkFeature = primaryKeys.get(i);
        if (!batch) {
            filterLogic = filterLogic.and(new Filter(Arrays.asList(pkFeature), SqlCondition.EQUALS, "?"));
        }
        stmtParameters.add(new PreparedStatementParameterDTO(pkFeature.getName(), primaryKeyIndex++));
    }
    query.setFilter(filterLogic);
    // set prepared statement parameters
    return new ServingPreparedStatementDTO(featureGroupId, statementIndex, stmtParameters, constructorController.generateSQL(query, true).toSqlString(new MysqlSqlDialect(SqlDialect.EMPTY_CONTEXT)).getSql());
}
Also used : Filter(io.hops.hopsworks.common.featurestore.query.filter.Filter) FilterLogic(io.hops.hopsworks.common.featurestore.query.filter.FilterLogic) ServingPreparedStatementDTO(io.hops.hopsworks.common.featurestore.query.ServingPreparedStatementDTO) ArrayList(java.util.ArrayList) MysqlSqlDialect(org.apache.calcite.sql.dialect.MysqlSqlDialect) Feature(io.hops.hopsworks.common.featurestore.query.Feature) TrainingDatasetFeature(io.hops.hopsworks.persistence.entity.featurestore.trainingdataset.TrainingDatasetFeature) PreparedStatementParameterDTO(io.hops.hopsworks.common.featurestore.query.PreparedStatementParameterDTO)

Aggregations

Feature (io.hops.hopsworks.common.featurestore.query.Feature)1 PreparedStatementParameterDTO (io.hops.hopsworks.common.featurestore.query.PreparedStatementParameterDTO)1 ServingPreparedStatementDTO (io.hops.hopsworks.common.featurestore.query.ServingPreparedStatementDTO)1 Filter (io.hops.hopsworks.common.featurestore.query.filter.Filter)1 FilterLogic (io.hops.hopsworks.common.featurestore.query.filter.FilterLogic)1 TrainingDatasetFeature (io.hops.hopsworks.persistence.entity.featurestore.trainingdataset.TrainingDatasetFeature)1 ArrayList (java.util.ArrayList)1 MysqlSqlDialect (org.apache.calcite.sql.dialect.MysqlSqlDialect)1