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();
}
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;
}
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;
}
Aggregations