Search in sources :

Example 1 with Dimension

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

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

the class EntityProjectionTranslator method resolveNonTimeDimensions.

/**
 * Gets dimensions except time dimensions based on relationships and attributes from {@link EntityProjection}.
 */
private Set<DimensionProjection> resolveNonTimeDimensions() {
    Set<DimensionProjection> attributes = entityProjection.getAttributes().stream().filter(attribute -> queriedTable.getTimeDimension(attribute.getName()) == null).map(dimAttr -> {
        Dimension dimension = queriedTable.getDimension(dimAttr.getName());
        return dimension == null ? null : engine.constructDimensionProjection(dimension, dimAttr.getAlias(), getArgumentMapFromArgumentSet(dimAttr.getArguments()));
    }).filter(Objects::nonNull).collect(Collectors.toSet());
    Set<DimensionProjection> relationships = entityProjection.getRelationships().stream().map(dimAttr -> {
        Dimension dimension = queriedTable.getDimension(dimAttr.getName());
        return dimension == null ? null : engine.constructDimensionProjection(dimension, dimAttr.getAlias(), Collections.emptyMap());
    }).filter(Objects::nonNull).collect(Collectors.toSet());
    return Sets.union(attributes, relationships);
}
Also used : PredicateExtractionVisitor(com.yahoo.elide.core.filter.expression.PredicateExtractionVisitor) DimensionProjection(com.yahoo.elide.datastores.aggregation.query.DimensionProjection) Set(java.util.Set) EntityProjection(com.yahoo.elide.core.request.EntityProjection) Dimension(com.yahoo.elide.datastores.aggregation.metadata.models.Dimension) HashMap(java.util.HashMap) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Objects(java.util.Objects) InvalidOperationException(com.yahoo.elide.core.exceptions.InvalidOperationException) FilterConstraints(com.yahoo.elide.datastores.aggregation.filter.visitor.FilterConstraints) Query(com.yahoo.elide.datastores.aggregation.query.Query) List(java.util.List) ImmutablePagination(com.yahoo.elide.datastores.aggregation.query.ImmutablePagination) Argument.getArgumentMapFromArgumentSet(com.yahoo.elide.core.request.Argument.getArgumentMapFromArgumentSet) TimeDimension(com.yahoo.elide.datastores.aggregation.metadata.models.TimeDimension) Table(com.yahoo.elide.datastores.aggregation.metadata.models.Table) TimeDimensionProjection(com.yahoo.elide.datastores.aggregation.query.TimeDimensionProjection) MetricProjection(com.yahoo.elide.datastores.aggregation.query.MetricProjection) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) Collections(java.util.Collections) LinkedHashSet(java.util.LinkedHashSet) RequestScope(com.yahoo.elide.core.RequestScope) SplitFilterExpressionVisitor(com.yahoo.elide.datastores.aggregation.filter.visitor.SplitFilterExpressionVisitor) DimensionProjection(com.yahoo.elide.datastores.aggregation.query.DimensionProjection) TimeDimensionProjection(com.yahoo.elide.datastores.aggregation.query.TimeDimensionProjection) Dimension(com.yahoo.elide.datastores.aggregation.metadata.models.Dimension) TimeDimension(com.yahoo.elide.datastores.aggregation.metadata.models.TimeDimension)

Example 3 with Dimension

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

Aggregations

Dimension (com.yahoo.elide.datastores.aggregation.metadata.models.Dimension)3 Table (com.yahoo.elide.datastores.aggregation.metadata.models.Table)3 TimeDimension (com.yahoo.elide.datastores.aggregation.metadata.models.TimeDimension)3 PredicateExtractionVisitor (com.yahoo.elide.core.filter.expression.PredicateExtractionVisitor)2 Metric (com.yahoo.elide.datastores.aggregation.metadata.models.Metric)2 DimensionProjection (com.yahoo.elide.datastores.aggregation.query.DimensionProjection)2 MetricProjection (com.yahoo.elide.datastores.aggregation.query.MetricProjection)2 Query (com.yahoo.elide.datastores.aggregation.query.Query)2 TimeDimensionProjection (com.yahoo.elide.datastores.aggregation.query.TimeDimensionProjection)2 List (java.util.List)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Preconditions (com.google.common.base.Preconditions)1 Sets (com.google.common.collect.Sets)1 RequestScope (com.yahoo.elide.core.RequestScope)1 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)1 InvalidOperationException (com.yahoo.elide.core.exceptions.InvalidOperationException)1 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)1 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)1 Argument (com.yahoo.elide.core.request.Argument)1