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());
}
Aggregations