Search in sources :

Example 6 with Column

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;
}
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 7 with Column

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);
                }
            });
        }
    }
}
Also used : Column(com.yahoo.elide.datastores.aggregation.metadata.models.Column) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PredicateExtractionVisitor(com.yahoo.elide.core.filter.expression.PredicateExtractionVisitor) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate)

Example 8 with Column

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()));
        }
    }
}
Also used : ColumnMeta(com.yahoo.elide.datastores.aggregation.annotation.ColumnMeta) Arrays(java.util.Arrays) ArgumentType(com.yahoo.elide.core.dictionary.ArgumentType) Join(com.yahoo.elide.datastores.aggregation.annotation.Join) AccessibleObject(com.yahoo.elide.core.type.AccessibleObject) TableMeta(com.yahoo.elide.datastores.aggregation.annotation.TableMeta) UserCheck(com.yahoo.elide.core.security.checks.UserCheck) PermissionExecutor(com.yahoo.elide.core.security.PermissionExecutor) Function(java.util.function.Function) ClassType(com.yahoo.elide.core.type.ClassType) Column(com.yahoo.elide.datastores.aggregation.metadata.models.Column) ToString(lombok.ToString) ParseTree(org.antlr.v4.runtime.tree.ParseTree) FilterExpressionCheck(com.yahoo.elide.core.security.checks.FilterExpressionCheck) RequestScope(com.yahoo.elide.core.RequestScope) DataStoreTransaction(com.yahoo.elide.core.datastore.DataStoreTransaction) Check(com.yahoo.elide.core.security.checks.Check) Cache(com.yahoo.elide.datastores.aggregation.cache.Cache) FromSubquery(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromSubquery) PermissionExpressionVisitor(com.yahoo.elide.modelconfig.validator.PermissionExpressionVisitor) NonNull(lombok.NonNull) Predicate(java.util.function.Predicate) ValueType(com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType) ArgumentDefinition(com.yahoo.elide.datastores.aggregation.metadata.models.ArgumentDefinition) Set(java.util.Set) FromTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable) AggregationStorePermissionExecutor(com.yahoo.elide.core.security.executors.AggregationStorePermissionExecutor) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Objects(java.util.Objects) List(java.util.List) ReadPermission(com.yahoo.elide.annotation.ReadPermission) Builder(lombok.Builder) DataStore(com.yahoo.elide.core.datastore.DataStore) Type(com.yahoo.elide.core.type.Type) TimeDimension(com.yahoo.elide.datastores.aggregation.metadata.models.TimeDimension) Annotation(java.lang.annotation.Annotation) Table(com.yahoo.elide.datastores.aggregation.metadata.models.Table) QueryLogger(com.yahoo.elide.datastores.aggregation.core.QueryLogger) FromTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable) Table(com.yahoo.elide.datastores.aggregation.metadata.models.Table) Column(com.yahoo.elide.datastores.aggregation.metadata.models.Column) ArgumentDefinition(com.yahoo.elide.datastores.aggregation.metadata.models.ArgumentDefinition) TimeDimension(com.yahoo.elide.datastores.aggregation.metadata.models.TimeDimension) ArgumentType(com.yahoo.elide.core.dictionary.ArgumentType)

Example 9 with Column

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

Aggregations

Column (com.yahoo.elide.datastores.aggregation.metadata.models.Column)9 Argument (com.yahoo.elide.core.request.Argument)5 SQLTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)5 ValueType (com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType)4 ColumnProjection (com.yahoo.elide.datastores.aggregation.query.ColumnProjection)4 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)3 PredicateExtractionVisitor (com.yahoo.elide.core.filter.expression.PredicateExtractionVisitor)3 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)3 Type (com.yahoo.elide.core.type.Type)3 Table (com.yahoo.elide.datastores.aggregation.metadata.models.Table)3 Query (com.yahoo.elide.datastores.aggregation.query.Query)3 List (java.util.List)3 InvalidOperationException (com.yahoo.elide.core.exceptions.InvalidOperationException)2 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)2 ArgumentDefinition (com.yahoo.elide.datastores.aggregation.metadata.models.ArgumentDefinition)2 TimeDimension (com.yahoo.elide.datastores.aggregation.metadata.models.TimeDimension)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2