Search in sources :

Example 26 with ColumnProjection

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));
}
Also used : SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) Test(org.junit.jupiter.api.Test)

Example 27 with ColumnProjection

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));
}
Also used : SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) Test(org.junit.jupiter.api.Test)

Example 28 with ColumnProjection

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;
}
Also used : Table(com.yahoo.elide.datastores.aggregation.metadata.models.Table) Column(com.yahoo.elide.datastores.aggregation.metadata.models.Column) Attribute(com.yahoo.elide.core.request.Attribute) ValueType(com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType) ParameterizedModel(com.yahoo.elide.core.type.ParameterizedModel) SQLColumnProjection(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.SQLColumnProjection) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection)

Example 29 with ColumnProjection

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()));
        }
    });
}
Also used : Column(com.yahoo.elide.datastores.aggregation.metadata.models.Column) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) InvalidOperationException(com.yahoo.elide.core.exceptions.InvalidOperationException)

Example 30 with ColumnProjection

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));
}
Also used : Argument(com.yahoo.elide.core.request.Argument) HashMap(java.util.HashMap) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) Argument.getArgumentMapFromString(com.yahoo.elide.core.request.Argument.getArgumentMapFromString) ToString(lombok.ToString) HandlebarsException(com.github.jknack.handlebars.HandlebarsException)

Aggregations

ColumnProjection (com.yahoo.elide.datastores.aggregation.query.ColumnProjection)36 Test (org.junit.jupiter.api.Test)22 SQLTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)13 Argument (com.yahoo.elide.core.request.Argument)7 Queryable (com.yahoo.elide.datastores.aggregation.query.Queryable)6 Query (com.yahoo.elide.datastores.aggregation.query.Query)5 Argument.getArgumentMapFromString (com.yahoo.elide.core.request.Argument.getArgumentMapFromString)4 ValueType (com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType)4 Column (com.yahoo.elide.datastores.aggregation.metadata.models.Column)4 ExpressionParser (com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ExpressionParser)4 List (java.util.List)4 Set (java.util.Set)4 Collectors (java.util.stream.Collectors)4 Path (com.yahoo.elide.core.Path)3 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)3 MetaDataStore (com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore)3 Reference (com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.Reference)3 HashMap (java.util.HashMap)3 HandlebarsException (com.github.jknack.handlebars.HandlebarsException)2 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)2