Search in sources :

Example 1 with Metric

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

the class SQLQueryEngine method toPageTotalSQL.

/**
 * Takes a SQLQuery and creates a new clone that instead returns the total number of records of the original
 * query.
 *
 * @param query The client query
 * @param sql The generated SQL query
 * @param sqlDialect the SQL dialect
 * @return A new query that returns the total number of records.
 */
private NativeQuery toPageTotalSQL(Query query, NativeQuery sql, SQLDialect sqlDialect) {
    // TODO: refactor this method
    String groupByDimensions = query.getAllDimensionProjections().stream().map(SQLColumnProjection.class::cast).filter(SQLColumnProjection::isProjected).map((column) -> column.toSQL(query, metaDataStore)).collect(Collectors.joining(", "));
    if (groupByDimensions.isEmpty()) {
        // Metric projection without group by dimension will return onely 1 record.
        return null;
    }
    NativeQuery innerQuery = NativeQuery.builder().projectionClause(groupByDimensions).fromClause(sql.getFromClause()).joinClause(sql.getJoinClause()).whereClause(sql.getWhereClause()).groupByClause(String.format("GROUP BY %s", groupByDimensions)).havingClause(sql.getHavingClause()).build();
    return NativeQuery.builder().projectionClause("COUNT(*)").fromClause(QueryTranslator.getFromClause("(" + innerQuery + ")", applyQuotes("pagination_subquery", sqlDialect), sqlDialect)).build();
}
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) SQLColumnProjection(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.SQLColumnProjection) NativeQuery(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.NativeQuery)

Example 2 with Metric

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

the class MetaDataStoreTest method testHiddenFields.

@Test
public void testHiddenFields() {
    Table playerStats = dataStore.getTable(ClassType.of(PlayerStats.class));
    Dimension country = playerStats.getDimension("country");
    Dimension playerRank = playerStats.getDimension("playerRank");
    Metric highScore = playerStats.getMetric("highScore");
    Metric hiddenHighScore = playerStats.getMetric("hiddenHighScore");
    TimeDimension recordedDate = playerStats.getTimeDimension("recordedDate");
    TimeDimension hiddenRecordedDate = playerStats.getTimeDimension("hiddenRecordedDate");
    assertTrue(country.isHidden());
    assertFalse(playerRank.isHidden());
    assertTrue(hiddenHighScore.isHidden());
    assertFalse(highScore.isHidden());
    assertTrue(hiddenRecordedDate.isHidden());
    assertFalse(recordedDate.isHidden());
    assertTrue(playerStats.getColumns().contains(highScore));
    assertTrue(playerStats.getColumns().contains(recordedDate));
    assertTrue(playerStats.getColumns().contains(playerRank));
    assertFalse(playerStats.getColumns().contains(country));
    assertFalse(playerStats.getColumns().contains(hiddenHighScore));
    assertFalse(playerStats.getColumns().contains(hiddenRecordedDate));
    assertTrue(playerStats.getAllColumns().contains(highScore));
    assertTrue(playerStats.getAllColumns().contains(recordedDate));
    assertTrue(playerStats.getAllColumns().contains(playerRank));
    assertTrue(playerStats.getAllColumns().contains(country));
    assertTrue(playerStats.getAllColumns().contains(hiddenHighScore));
    assertTrue(playerStats.getAllColumns().contains(hiddenRecordedDate));
    assertFalse(playerStats.getDimensions().contains(country));
    assertFalse(playerStats.getMetrics().contains(hiddenHighScore));
    assertFalse(playerStats.getTimeDimensions().contains(hiddenRecordedDate));
    assertTrue(playerStats.getMetrics().contains(highScore));
    assertTrue(playerStats.getDimensions().contains(playerRank));
    assertTrue(playerStats.getTimeDimensions().contains(recordedDate));
    assertTrue(playerStats.getAllDimensions().contains(country));
    assertTrue(playerStats.getAllMetrics().contains(hiddenHighScore));
    assertTrue(playerStats.getAllTimeDimensions().contains(hiddenRecordedDate));
    assertTrue(playerStats.getAllMetrics().contains(highScore));
    assertTrue(playerStats.getAllDimensions().contains(playerRank));
    assertTrue(playerStats.getAllTimeDimensions().contains(recordedDate));
}
Also used : Table(com.yahoo.elide.datastores.aggregation.metadata.models.Table) Metric(com.yahoo.elide.datastores.aggregation.metadata.models.Metric) PlayerStats(example.PlayerStats) Dimension(com.yahoo.elide.datastores.aggregation.metadata.models.Dimension) TimeDimension(com.yahoo.elide.datastores.aggregation.metadata.models.TimeDimension) TimeDimension(com.yahoo.elide.datastores.aggregation.metadata.models.TimeDimension) Test(org.junit.jupiter.api.Test)

Example 3 with Metric

use of com.yahoo.elide.datastores.aggregation.metadata.models.Metric 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)

Aggregations

Dimension (com.yahoo.elide.datastores.aggregation.metadata.models.Dimension)3 Metric (com.yahoo.elide.datastores.aggregation.metadata.models.Metric)3 Table (com.yahoo.elide.datastores.aggregation.metadata.models.Table)3 TimeDimension (com.yahoo.elide.datastores.aggregation.metadata.models.TimeDimension)3 Preconditions (com.google.common.base.Preconditions)2 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)2 PredicateExtractionVisitor (com.yahoo.elide.core.filter.expression.PredicateExtractionVisitor)2 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)2 Argument (com.yahoo.elide.core.request.Argument)2 Pagination (com.yahoo.elide.core.request.Pagination)2 Type (com.yahoo.elide.core.type.Type)2 TimedFunction (com.yahoo.elide.core.utils.TimedFunction)2 CoerceUtil (com.yahoo.elide.core.utils.coerce.CoerceUtil)2 DefaultQueryValidator (com.yahoo.elide.datastores.aggregation.DefaultQueryValidator)2 QueryEngine (com.yahoo.elide.datastores.aggregation.QueryEngine)2 QueryValidator (com.yahoo.elide.datastores.aggregation.QueryValidator)2 NamespacePackage (com.yahoo.elide.datastores.aggregation.dynamic.NamespacePackage)2 ColumnContext.applyQuotes (com.yahoo.elide.datastores.aggregation.metadata.ColumnContext.applyQuotes)2 FormulaValidator (com.yahoo.elide.datastores.aggregation.metadata.FormulaValidator)2 MetaDataStore (com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore)2