Skip to content
This repository was archived by the owner on Jun 26, 2024. It is now read-only.

Commit 7c57ce4

Browse files
committed
optimize metric aggregations order by
1 parent 2a2068a commit 7c57ce4

File tree

4 files changed

+34
-6
lines changed

4 files changed

+34
-6
lines changed

gateway-service-impl/src/main/java/org/hypertrace/gateway/service/entity/query/ExecutionContext.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,14 +206,18 @@ public void removePendingSelectionSource(String source) {
206206
pendingSelectionSources.remove(source);
207207
}
208208

209-
public void removePendingMetricAggregationSources(String source) {
209+
public void removePendingMetricAggregationSource(String source) {
210210
pendingMetricAggregationSources.remove(source);
211211
}
212212

213213
public void removePendingSelectionSourceForOrderBy(String source) {
214214
pendingSelectionSourcesForOrderBy.remove(source);
215215
}
216216

217+
public void removePendingMetricAggregationSourceForOrderBy(String source) {
218+
pendingMetricAggregationSourcesForOrderBy.remove(source);
219+
}
220+
217221
public Map<String, List<Expression>> getSourceToFilterExpressionMap() {
218222
return sourceToFilterExpressionMap;
219223
}

gateway-service-impl/src/main/java/org/hypertrace/gateway/service/entity/query/ExecutionTreeBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ QueryNode buildExecutionTree(ExecutionContext executionContext, QueryNode filter
197197
new SelectionNode.Builder(rootNode)
198198
.setAggMetricSelectionSources(metricSourcesForOrderBy)
199199
.build();
200-
metricSourcesForOrderBy.forEach(executionContext::removePendingMetricAggregationSources);
200+
metricSourcesForOrderBy.forEach(executionContext::removePendingMetricAggregationSource);
201201
}
202202

203203
// Try adding SortAndPaginateNode

gateway-service-impl/src/main/java/org/hypertrace/gateway/service/entity/query/visitor/ExecutionContextBuilderVisitor.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ public Void visit(DataFetcherNode dataFetcherNode) {
5858
executionContext.removePendingSelectionSource(source);
5959
// TODO: Currently, assumes that the order by attribute is also present in the selection set
6060
executionContext.removePendingSelectionSourceForOrderBy(source);
61+
// TODO: Remove redundant attributes for metric aggregation source for order by
62+
// The current metric aggregation source is only QS
63+
64+
// The order by on metric aggregations will also be added in the selections list of
65+
// DataFetcherNode, so that the order by metric aggregations can be fetched before
66+
// and only the required data set is paginated
67+
executionContext.removePendingMetricAggregationSourceForOrderBy(source);
6168

6269
// set of attributes which were fetched from the source
6370
Map<String, Set<String>> sourceToSelectionAttributeMap =

gateway-service-impl/src/main/java/org/hypertrace/gateway/service/entity/query/visitor/ExecutionVisitor.java

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import java.util.ArrayList;
88
import java.util.Arrays;
9+
import java.util.Collection;
910
import java.util.Collections;
1011
import java.util.LinkedHashMap;
1112
import java.util.LinkedList;
@@ -16,6 +17,7 @@
1617
import java.util.concurrent.Executors;
1718
import java.util.stream.Collectors;
1819
import java.util.stream.IntStream;
20+
import java.util.stream.Stream;
1921

2022
import com.google.common.collect.Sets;
2123
import org.hypertrace.gateway.service.common.datafetcher.EntityFetcherResponse;
@@ -39,6 +41,7 @@
3941
import org.hypertrace.gateway.service.v1.common.Filter;
4042
import org.hypertrace.gateway.service.v1.common.LiteralConstant;
4143
import org.hypertrace.gateway.service.v1.common.Operator;
44+
import org.hypertrace.gateway.service.v1.common.OrderByExpression;
4245
import org.hypertrace.gateway.service.v1.common.Value;
4346
import org.hypertrace.gateway.service.v1.common.ValueType;
4447
import org.hypertrace.gateway.service.v1.entity.EntitiesRequest;
@@ -143,6 +146,23 @@ public EntityResponse visit(DataFetcherNode dataFetcherNode) {
143146
executionContext.getTimestampAttributeId(),
144147
executionContext.getRequestHeaders());
145148

149+
// fetching both attribute selections and metric order by selections for
150+
// optimized pagination
151+
List<Expression> attributeSelections =
152+
executionContext
153+
.getSourceToSelectionExpressionMap()
154+
.getOrDefault(source, executionContext.getEntityIdExpressions());
155+
List<Expression> metricOrderBySelections =
156+
executionContext
157+
.getSourceToMetricOrderByExpressionMap()
158+
.getOrDefault(source, Collections.emptyList())
159+
.stream()
160+
.map(OrderByExpression::getExpression)
161+
.collect(Collectors.toList());
162+
List<Expression> selections = Stream.of(attributeSelections, metricOrderBySelections)
163+
.flatMap(Collection::stream)
164+
.collect(Collectors.toList());
165+
146166
EntitiesRequest.Builder requestBuilder =
147167
EntitiesRequest.newBuilder(entitiesRequest)
148168
.clearSelection()
@@ -151,10 +171,7 @@ public EntityResponse visit(DataFetcherNode dataFetcherNode) {
151171
.clearOrderBy()
152172
.clearLimit()
153173
.clearOffset()
154-
.addAllSelection(
155-
executionContext
156-
.getSourceToSelectionExpressionMap()
157-
.getOrDefault(source, executionContext.getEntityIdExpressions()))
174+
.addAllSelection(selections)
158175
.setFilter(dataFetcherNode.getFilter());
159176

160177
if (dataFetcherNode.getLimit() != null) {

0 commit comments

Comments
 (0)