Search in sources :

Example 1 with MetricProjection

use of com.yahoo.elide.datastores.aggregation.query.MetricProjection in project elide by yahoo.

the class EntityProjectionTranslatorTest method testBasicTranslation.

@Test
public void testBasicTranslation() {
    EntityProjectionTranslator translator = new EntityProjectionTranslator(engine, playerStatsTable, basicProjection, scope, true);
    Query query = translator.getQuery();
    assertEquals(playerStatsTable, query.getSource());
    assertEquals(1, query.getMetricProjections().size());
    String actual = query.getMetricProjections().stream().map(MetricProjection::getAlias).findFirst().orElse(null);
    assertEquals("lowScore", actual);
    assertEquals(1, query.getAllDimensionProjections().size());
    List<ColumnProjection> dimensions = new ArrayList<>(query.getAllDimensionProjections());
    assertEquals("overallRating", dimensions.get(0).getName());
}
Also used : MetricProjection(com.yahoo.elide.datastores.aggregation.query.MetricProjection) Query(com.yahoo.elide.datastores.aggregation.query.Query) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) ArrayList(java.util.ArrayList) SQLUnitTest(com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest) Test(org.junit.jupiter.api.Test)

Example 2 with MetricProjection

use of com.yahoo.elide.datastores.aggregation.query.MetricProjection in project elide by yahoo.

the class DailyAverageScorePerPeriod method resolve.

@Override
public QueryPlan resolve(Query query) {
    SQLTable table = (SQLTable) query.getSource();
    MetricProjection innerMetric = table.getMetricProjection("highScore");
    TimeDimension innerTimeGrain = table.getTimeDimension("recordedDate");
    Map<String, Argument> arguments = new HashMap<>();
    arguments.put("grain", Argument.builder().name("grain").value(TimeGrain.DAY).build());
    QueryPlan innerQuery = QueryPlan.builder().source(query.getSource()).metricProjection(innerMetric).timeDimensionProjection(new SQLTimeDimensionProjection(innerTimeGrain, innerTimeGrain.getTimezone(), "recordedDate_DAY", arguments, true)).build();
    QueryPlan outerQuery = QueryPlan.builder().source(innerQuery).metricProjection(new DailyAverageScorePerPeriod(this, "AVG({{$highScore}})")).build();
    return outerQuery;
}
Also used : SQLMetricProjection(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.SQLMetricProjection) MetricProjection(com.yahoo.elide.datastores.aggregation.query.MetricProjection) Argument(com.yahoo.elide.core.request.Argument) HashMap(java.util.HashMap) SQLTimeDimensionProjection(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.SQLTimeDimensionProjection) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) TimeDimension(com.yahoo.elide.datastores.aggregation.metadata.models.TimeDimension) QueryPlan(com.yahoo.elide.datastores.aggregation.query.QueryPlan)

Example 3 with MetricProjection

use of com.yahoo.elide.datastores.aggregation.query.MetricProjection in project elide by yahoo.

the class EntityProjectionTranslator method addHavingMetrics.

/**
 * Adds to the list of queried metrics any metric in the HAVING filter that has not been explicitly requested
 * by the client.
 */
private void addHavingMetrics() {
    if (havingFilter == null) {
        return;
    }
    // Flatten the HAVING filter expression into a list of predicates...
    havingFilter.accept(new PredicateExtractionVisitor()).forEach(filterPredicate -> {
        String fieldName = filterPredicate.getField();
        // If the predicate field is a metric
        if (queriedTable.getMetric(fieldName) != null) {
            // If the query doesn't contain this metric.
            if (metrics.stream().noneMatch((metric -> metric.getName().equals(fieldName)))) {
                // Construct a new projection and add it to the query.
                MetricProjection havingMetric = engine.constructMetricProjection(queriedTable.getMetric(fieldName), fieldName, new HashMap<>());
                metrics.add(havingMetric);
            }
        }
    });
}
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) MetricProjection(com.yahoo.elide.datastores.aggregation.query.MetricProjection) PredicateExtractionVisitor(com.yahoo.elide.core.filter.expression.PredicateExtractionVisitor)

Aggregations

MetricProjection (com.yahoo.elide.datastores.aggregation.query.MetricProjection)3 TimeDimension (com.yahoo.elide.datastores.aggregation.metadata.models.TimeDimension)2 Query (com.yahoo.elide.datastores.aggregation.query.Query)2 HashMap (java.util.HashMap)2 Sets (com.google.common.collect.Sets)1 RequestScope (com.yahoo.elide.core.RequestScope)1 InvalidOperationException (com.yahoo.elide.core.exceptions.InvalidOperationException)1 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)1 PredicateExtractionVisitor (com.yahoo.elide.core.filter.expression.PredicateExtractionVisitor)1 Argument (com.yahoo.elide.core.request.Argument)1 Argument.getArgumentMapFromArgumentSet (com.yahoo.elide.core.request.Argument.getArgumentMapFromArgumentSet)1 EntityProjection (com.yahoo.elide.core.request.EntityProjection)1 FilterConstraints (com.yahoo.elide.datastores.aggregation.filter.visitor.FilterConstraints)1 SplitFilterExpressionVisitor (com.yahoo.elide.datastores.aggregation.filter.visitor.SplitFilterExpressionVisitor)1 SQLUnitTest (com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest)1 Dimension (com.yahoo.elide.datastores.aggregation.metadata.models.Dimension)1 Table (com.yahoo.elide.datastores.aggregation.metadata.models.Table)1 ColumnProjection (com.yahoo.elide.datastores.aggregation.query.ColumnProjection)1 DimensionProjection (com.yahoo.elide.datastores.aggregation.query.DimensionProjection)1 ImmutablePagination (com.yahoo.elide.datastores.aggregation.query.ImmutablePagination)1