Search in sources :

Example 6 with Table

use of com.yahoo.elide.datastores.aggregation.metadata.models.Table in project elide by yahoo.

the class AggregationDataStoreTransactionTest method testMissingRequiredTableFilter.

@Test
public void testMissingRequiredTableFilter() {
    EntityDictionary dictionary = EntityDictionary.builder().build();
    dictionary.bindEntity(PlayerStatsWithRequiredFilter.class);
    Table table = new SQLTable(new Namespace(DEFAULT_NAMESPACE), ClassType.of(PlayerStatsWithRequiredFilter.class), dictionary);
    AggregationDataStoreTransaction tx = new AggregationDataStoreTransaction(queryEngine, cache, queryLogger);
    assertThrows(BadRequestException.class, () -> tx.addTableFilterArguments(table, query, dictionary));
}
Also used : PlayerStatsWithRequiredFilter(example.PlayerStatsWithRequiredFilter) Table(com.yahoo.elide.datastores.aggregation.metadata.models.Table) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Namespace(com.yahoo.elide.datastores.aggregation.metadata.models.Namespace) SQLUnitTest(com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest) Test(org.junit.jupiter.api.Test)

Example 7 with Table

use of com.yahoo.elide.datastores.aggregation.metadata.models.Table 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 8 with Table

use of com.yahoo.elide.datastores.aggregation.metadata.models.Table in project elide by yahoo.

the class SQLQueryEngine method expandMetricQueryPlans.

/**
 * Transforms a client query into a potentially nested/complex query by expanding each metric into
 * its respective query plan - and then merging the plans together into a consolidated query.
 * @param query The client query.
 * @return A query that reflects each metric's individual query plan.
 */
private Query expandMetricQueryPlans(Query query) {
    // Expand each metric into its own query plan.
    List<QueryPlan> toMerge = query.getMetricProjections().stream().map(projection -> projection.resolve(query)).collect(Collectors.toList());
    // Merge all the queries together.
    List<QueryPlan> mergedPlans = merger.merge(toMerge);
    // TODO - Support joins across plans rather than rejecting plans that don't merge.
    if (mergedPlans.size() != 1) {
        throw new UnsupportedOperationException("Incompatible metrics in client query.  Cannot merge " + "into a single query");
    }
    QueryPlan mergedPlan = mergedPlans.get(0);
    QueryPlanTranslator queryPlanTranslator = new QueryPlanTranslator(query, metaDataStore, merger);
    Query merged = (mergedPlan == null) ? QueryPlanTranslator.addHiddenProjections(metaDataStore, query).build() : queryPlanTranslator.translate(mergedPlan);
    for (Optimizer optimizer : optimizers) {
        SQLTable table = (SQLTable) query.getSource();
        // override table hints.
        if (table.getHints().contains(optimizer.negateHint())) {
            continue;
        }
        if (!table.getHints().contains(optimizer.hint())) {
            continue;
        }
        if (optimizer.canOptimize(merged)) {
            merged = optimizer.optimize(merged);
        }
    }
    return merged;
}
Also used : PredicateExtractionVisitor(com.yahoo.elide.core.filter.expression.PredicateExtractionVisitor) SQLColumnProjection(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.SQLColumnProjection) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) Arrays(java.util.Arrays) Connection(java.sql.Connection) Dimension(com.yahoo.elide.datastores.aggregation.metadata.models.Dimension) NativeQuery(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.NativeQuery) Argument(com.yahoo.elide.core.request.Argument) EnumType(javax.persistence.EnumType) TimedFunction(com.yahoo.elide.core.utils.TimedFunction) Time(com.yahoo.elide.datastores.aggregation.timegrains.Time) NamespacePackage(com.yahoo.elide.datastores.aggregation.dynamic.NamespacePackage) Namespace(com.yahoo.elide.datastores.aggregation.metadata.models.Namespace) ResultSet(java.sql.ResultSet) Map(java.util.Map) Enumerated(javax.persistence.Enumerated) MetricProjection(com.yahoo.elide.datastores.aggregation.query.MetricProjection) DefaultQueryPlanMerger(com.yahoo.elide.datastores.aggregation.query.DefaultQueryPlanMerger) QueryPlanMerger(com.yahoo.elide.datastores.aggregation.query.QueryPlanMerger) Collection(java.util.Collection) ValueType(com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType) Set(java.util.Set) QueryResult(com.yahoo.elide.datastores.aggregation.query.QueryResult) FromTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable) ColumnContext.applyQuotes(com.yahoo.elide.datastores.aggregation.metadata.ColumnContext.applyQuotes) QueryValidator(com.yahoo.elide.datastores.aggregation.QueryValidator) CoerceUtil(com.yahoo.elide.core.utils.coerce.CoerceUtil) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Query(com.yahoo.elide.datastores.aggregation.query.Query) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) TimeDimension(com.yahoo.elide.datastores.aggregation.metadata.models.TimeDimension) Annotation(java.lang.annotation.Annotation) Table(com.yahoo.elide.datastores.aggregation.metadata.models.Table) Optimizer(com.yahoo.elide.datastores.aggregation.query.Optimizer) TimeDimensionProjection(com.yahoo.elide.datastores.aggregation.query.TimeDimensionProjection) MetaDataStore(com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore) SQLDimensionProjection(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.SQLDimensionProjection) VersionQuery(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.VersionQuery) Getter(lombok.Getter) ColumnArgumentValidator(com.yahoo.elide.datastores.aggregation.validator.ColumnArgumentValidator) TableArgumentValidator(com.yahoo.elide.datastores.aggregation.validator.TableArgumentValidator) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) SQLException(java.sql.SQLException) SQLDialect(com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect) QueryTranslator(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.QueryTranslator) Column(com.yahoo.elide.datastores.aggregation.metadata.models.Column) DataSource(javax.sql.DataSource) SQLTimeDimensionProjection(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.SQLTimeDimensionProjection) QueryEngine(com.yahoo.elide.datastores.aggregation.QueryEngine) DimensionProjection(com.yahoo.elide.datastores.aggregation.query.DimensionProjection) FromSubquery(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromSubquery) DefaultQueryValidator(com.yahoo.elide.datastores.aggregation.DefaultQueryValidator) QueryPlan(com.yahoo.elide.datastores.aggregation.query.QueryPlan) QueryPlanTranslator(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.QueryPlanTranslator) FormulaValidator(com.yahoo.elide.datastores.aggregation.metadata.FormulaValidator) Pagination(com.yahoo.elide.core.request.Pagination) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) Type(com.yahoo.elide.core.type.Type) Preconditions(com.google.common.base.Preconditions) Metric(com.yahoo.elide.datastores.aggregation.metadata.models.Metric) NativeQuery(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.NativeQuery) Query(com.yahoo.elide.datastores.aggregation.query.Query) VersionQuery(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.VersionQuery) QueryPlanTranslator(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.QueryPlanTranslator) Optimizer(com.yahoo.elide.datastores.aggregation.query.Optimizer) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) QueryPlan(com.yahoo.elide.datastores.aggregation.query.QueryPlan)

Example 9 with Table

use of com.yahoo.elide.datastores.aggregation.metadata.models.Table 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 10 with Table

use of com.yahoo.elide.datastores.aggregation.metadata.models.Table in project elide by yahoo.

the class SplitFilterExpressionVisitorTest method setupEntityDictionary.

@BeforeAll
public static void setupEntityDictionary() {
    EntityDictionary entityDictionary = EntityDictionary.builder().build();
    entityDictionary.bindEntity(PlayerStats.class);
    entityDictionary.bindEntity(Country.class);
    entityDictionary.bindEntity(SubCountry.class);
    entityDictionary.bindEntity(Player.class);
    Namespace namespace = new Namespace(DEFAULT_NAMESPACE);
    Table table = new SQLTable(namespace, ClassType.of(PlayerStats.class), entityDictionary);
    splitFilterExpressionVisitor = new SplitFilterExpressionVisitor(table);
}
Also used : SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) Table(com.yahoo.elide.datastores.aggregation.metadata.models.Table) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) PlayerStats(example.PlayerStats) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Namespace(com.yahoo.elide.datastores.aggregation.metadata.models.Namespace) BeforeAll(org.junit.jupiter.api.BeforeAll)

Aggregations

Table (com.yahoo.elide.datastores.aggregation.metadata.models.Table)10 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)6 Column (com.yahoo.elide.datastores.aggregation.metadata.models.Column)5 Type (com.yahoo.elide.core.type.Type)4 Namespace (com.yahoo.elide.datastores.aggregation.metadata.models.Namespace)4 TimeDimension (com.yahoo.elide.datastores.aggregation.metadata.models.TimeDimension)4 List (java.util.List)4 RequestScope (com.yahoo.elide.core.RequestScope)3 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)3 ValueType (com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType)3 ToString (lombok.ToString)3 Test (org.junit.jupiter.api.Test)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ReadPermission (com.yahoo.elide.annotation.ReadPermission)2 DataStore (com.yahoo.elide.core.datastore.DataStore)2 ArgumentType (com.yahoo.elide.core.dictionary.ArgumentType)2 Argument (com.yahoo.elide.core.request.Argument)2 PermissionExecutor (com.yahoo.elide.core.security.PermissionExecutor)2 Check (com.yahoo.elide.core.security.checks.Check)2 FilterExpressionCheck (com.yahoo.elide.core.security.checks.FilterExpressionCheck)2