use of com.yahoo.elide.datastores.aggregation.metadata.models.Column in project elide by yahoo.
the class EntityHydrator method coerceObjectToEntity.
/**
* Coerces results from a {@link Query} into an Object.
*
* @param result a fieldName-value map
* @param counter Monotonically increasing number to generate IDs.
* @return A hydrated entity object.
*/
protected Object coerceObjectToEntity(Map<String, Object> result, MutableInt counter) {
Table table = getBaseTable(query);
Type<?> entityClass = entityDictionary.getEntityClass(table.getName(), table.getVersion());
// Construct the object.
Object entityInstance;
try {
entityInstance = entityClass.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
result.forEach((fieldName, value) -> {
ColumnProjection columnProjection = query.getColumnProjection(fieldName);
Column column = table.getColumn(Column.class, columnProjection.getName());
Type<?> fieldType = getType(entityClass, columnProjection);
Attribute attribute = projectionToAttribute(columnProjection, fieldType);
ValueType valueType = column.getValueType();
if (entityInstance instanceof ParameterizedModel) {
// This is an ENUM_TEXT or ENUM_ORDINAL type.
if (// Java enums can be coerced directly via CoerceUtil - so skip them.
!fieldType.isEnum() && valueType == ValueType.TEXT && column.getValues() != null && !column.getValues().isEmpty()) {
value = convertToEnumValue(value, column.getValues());
}
((ParameterizedModel) entityInstance).addAttributeValue(attribute, CoerceUtil.coerce(value, fieldType));
} else {
getEntityDictionary().setValue(entityInstance, fieldName, value);
}
});
// Set the ID (it must be coerced from an integer)
getEntityDictionary().setValue(entityInstance, getEntityDictionary().getIdFieldName(entityClass), counter.getAndIncrement());
return entityInstance;
}
use of com.yahoo.elide.datastores.aggregation.metadata.models.Column in project elide by yahoo.
the class SQLQueryEngine method supplyFilterQueryParameters.
/**
* Given a Prepared Statement, replaces any parameters with their values from client query.
*
* @param query The client query
* @param stmt Customized Prepared Statement
* @param dialect the SQL dialect
*/
private void supplyFilterQueryParameters(Query query, NamedParamPreparedStatement stmt, SQLDialect dialect) {
Collection<FilterPredicate> predicates = new ArrayList<>();
if (query.getWhereFilter() != null) {
predicates.addAll(query.getWhereFilter().accept(new PredicateExtractionVisitor()));
}
if (query.getHavingFilter() != null) {
predicates.addAll(query.getHavingFilter().accept(new PredicateExtractionVisitor()));
}
for (FilterPredicate filterPredicate : predicates) {
Column column = metaDataStore.getColumn(filterPredicate.getEntityType(), filterPredicate.getField());
if (filterPredicate.getOperator().isParameterized()) {
boolean shouldEscape = filterPredicate.isMatchingOperator();
filterPredicate.getParameters().forEach(param -> {
try {
Object value = param.getValue();
value = convertForJdbc(filterPredicate.getEntityType(), column, value, dialect);
stmt.setObject(param.getName(), shouldEscape ? param.escapeMatching() : value);
} catch (SQLException e) {
throw new IllegalStateException(e);
}
});
}
}
}
use of com.yahoo.elide.datastores.aggregation.metadata.models.Column in project elide by yahoo.
the class AggregationDataStore method populateEntityDictionary.
/**
* Populate an {@link EntityDictionary} and use this dictionary to construct a {@link QueryEngine}.
* @param dictionary the dictionary
*/
@Override
public void populateEntityDictionary(EntityDictionary dictionary) {
if (dynamicCompiledClasses != null && dynamicCompiledClasses.size() != 0) {
dynamicCompiledClasses.stream().filter((type) -> !IS_TYPE_HIDDEN.test(type)).forEach(dynamicLoadedClass -> {
dictionary.bindEntity(dynamicLoadedClass, IS_FIELD_HIDDEN);
validateModelExpressionChecks(dictionary, dynamicLoadedClass);
dictionary.bindPermissionExecutor(dynamicLoadedClass, aggPermissionExecutor);
});
}
dictionary.getScanner().getAnnotatedClasses(AGGREGATION_STORE_CLASSES).stream().filter((type) -> !IS_TYPE_HIDDEN.test(ClassType.of(type))).forEach(cls -> {
dictionary.bindEntity(cls, IS_FIELD_HIDDEN);
validateModelExpressionChecks(dictionary, ClassType.of(cls));
dictionary.bindPermissionExecutor(cls, aggPermissionExecutor);
});
for (Table table : queryEngine.getMetaDataStore().getMetaData(ClassType.of(Table.class))) {
/* Add 'grain' argument to each TimeDimensionColumn */
for (TimeDimension timeDim : table.getAllTimeDimensions()) {
dictionary.addArgumentToAttribute(dictionary.getEntityClass(table.getName(), table.getVersion()), timeDim.getName(), new ArgumentType("grain", ClassType.STRING_TYPE, timeDim.getDefaultGrain().getGrain()));
}
/* Add argument to each Column */
for (Column col : table.getAllColumns()) {
for (ArgumentDefinition arg : col.getArgumentDefinitions()) {
dictionary.addArgumentToAttribute(dictionary.getEntityClass(table.getName(), table.getVersion()), col.getName(), new ArgumentType(arg.getName(), ValueType.getType(arg.getType()), arg.getDefaultValue()));
}
}
/* Add argument to each Table */
for (ArgumentDefinition arg : table.getArgumentDefinitions()) {
dictionary.addArgumentToEntity(dictionary.getEntityClass(table.getName(), table.getVersion()), new ArgumentType(arg.getName(), ValueType.getType(arg.getType()), arg.getDefaultValue()));
}
}
}
use of com.yahoo.elide.datastores.aggregation.metadata.models.Column in project elide by yahoo.
the class DefaultQueryValidator method validatePredicate.
protected void validatePredicate(Query query, FilterPredicate predicate) {
SQLTable table = (SQLTable) query.getSource();
Set<ColumnProjection> projections = extractFilterProjections(query, predicate);
if (projections.isEmpty()) {
return;
}
ColumnProjection projection = projections.iterator().next();
validateColumn(query, projection);
Column column = table.getColumn(Column.class, projection.getName());
if (column.getValueType().equals(ValueType.ID)) {
throw new InvalidOperationException("Filtering by ID is not supported on " + query.getSource().getName());
}
if (column.getValues() == null || column.getValues().isEmpty()) {
return;
}
if (REGEX_OPERATORS.contains(predicate.getOperator())) {
return;
}
predicate.getValues().forEach(value -> {
if (!column.getValues().contains(value)) {
throw new InvalidOperationException(String.format("Column '%s' values must match one of these values: %s", projection.getAlias(), column.getValues()));
}
});
}
Aggregations