use of com.yahoo.elide.datastores.aggregation.query.ColumnProjection in project elide by yahoo.
the class HasColumnArgsVisitorTest method testJoinWithColumnArgs.
@Test
public void testJoinWithColumnArgs() throws Exception {
SQLTable table = metaDataStore.getTable(ClassType.of(TableA.class));
ColumnProjection projection = table.getColumnProjection("joinWithColumnArgsToPhysical");
assertTrue(matches(table, projection));
}
use of com.yahoo.elide.datastores.aggregation.query.ColumnProjection in project elide by yahoo.
the class HasColumnArgsVisitorTest method testColumnArgs.
@Test
public void testColumnArgs() throws Exception {
SQLTable table = metaDataStore.getTable(ClassType.of(TableC.class));
ColumnProjection projection = table.getColumnProjection("columnArgs");
assertTrue(matches(table, projection));
}
use of com.yahoo.elide.datastores.aggregation.query.ColumnProjection 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.query.ColumnProjection 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()));
}
});
}
use of com.yahoo.elide.datastores.aggregation.query.ColumnProjection in project elide by yahoo.
the class ColumnContext method resolveSQLHandlebar.
private Object resolveSQLHandlebar(final Object context, final Options options) throws UnsupportedEncodingException {
String from = options.hash("from");
String columnName = options.hash("column");
int argsIndex = columnName.indexOf('[');
String invokedColumnName = columnName;
ColumnContext currentCtx = (ColumnContext) context;
// 'from' is optional, so if not provided use the same table context.
ColumnContext invokedCtx = isBlank(from) ? currentCtx : (ColumnContext) currentCtx.get(from);
Map<String, Argument> pinnedArgs = new HashMap<>();
if (argsIndex >= 0) {
pinnedArgs = getArgumentMapFromString(columnName.substring(argsIndex));
invokedColumnName = columnName.substring(0, argsIndex);
}
// Physical References starts with $
if (invokedColumnName.lastIndexOf('$') == 0) {
return resolvePhysicalReference(invokedCtx, invokedColumnName);
}
ColumnProjection column = invokedCtx.getQueryable().getSource().getColumnProjection(invokedColumnName);
if (column != null) {
ColumnProjection newColumn = column.withArguments(mergedArgumentMap(column.getArguments(), invokedCtx.getColumn().getArguments(), pinnedArgs));
return getNewContext(invokedCtx, newColumn).resolve(newColumn.getExpression());
}
throw new HandlebarsException(new Throwable("Couldn't find: " + invokedColumnName));
}
Aggregations