Search in sources :

Example 1 with QueryPlan

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

the class QueryPlanTranslator method translate.

/**
 * Translate a query plan into a query.
 * @param plan the plan to translate.
 * @return A runnable query incorporating elements from the client's original query.
 */
public Query translate(QueryPlan plan) {
    // Convert the client query into a dimension-only plan that can be nested.
    QueryPlan clientQueryPlan = QueryPlan.builder().source(clientQuery.getSource()).timeDimensionProjections(clientQuery.getTimeDimensionProjections()).dimensionProjections(clientQuery.getDimensionProjections()).build();
    // Merge it with the metric plan.
    if (plan.isNested() && !clientQueryPlan.canNest(metaDataStore)) {
        throw new UnsupportedOperationException("Cannot nest one or more dimensions from the client query");
    }
    QueryPlan merged = merger.merge(clientQueryPlan, plan);
    // Decorate the result with filters, sorting, and pagination.
    return merged.accept(this).build();
}
Also used : QueryPlan(com.yahoo.elide.datastores.aggregation.query.QueryPlan)

Example 2 with QueryPlan

use of com.yahoo.elide.datastores.aggregation.query.QueryPlan 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 QueryPlan

use of com.yahoo.elide.datastores.aggregation.query.QueryPlan 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

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