diff --git a/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/common/QueryRequestContext.java b/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/common/QueryRequestContext.java index 6f161639..04694bbb 100644 --- a/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/common/QueryRequestContext.java +++ b/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/common/QueryRequestContext.java @@ -9,6 +9,7 @@ // Hold some of request fields and mappings needed when parsing the query service response public class QueryRequestContext extends RequestContext { private final Map aliasToFunctionExpressionMap = new HashMap<>(); + private final Map functionExpressionToAliasMap = new HashMap<>(); private final long startTimeMillis; private final long endTimeMillis; private final Map aliasToTimeAggregation = new HashMap<>(); @@ -24,6 +25,11 @@ public QueryRequestContext( public void mapAliasToFunctionExpression(String alias, FunctionExpression functionExpression) { aliasToFunctionExpressionMap.put(alias, functionExpression); + functionExpressionToAliasMap.put(functionExpression, alias); + } + + public String getAliasForFunctionExpression(FunctionExpression functionExpression) { + return functionExpressionToAliasMap.get(functionExpression); } public boolean containsFunctionExpression(String alias) { diff --git a/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/common/util/ExpressionReader.java b/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/common/util/ExpressionReader.java index 5219a97a..867e9113 100644 --- a/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/common/util/ExpressionReader.java +++ b/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/common/util/ExpressionReader.java @@ -10,14 +10,31 @@ import java.util.stream.Collectors; import org.hypertrace.gateway.service.v1.common.Expression; import org.hypertrace.gateway.service.v1.common.FunctionExpression; +import org.hypertrace.gateway.service.v1.common.OrderByExpression; public class ExpressionReader { + public static List getFunctionOrderByExpressions( + List expressions) { + return expressions.stream() + .filter( + expression -> + expression.getExpression().getValueCase() == Expression.ValueCase.FUNCTION) + .collect(Collectors.toUnmodifiableList()); + } + public static List getFunctionExpressions(List expressions) { return expressions.stream() .filter(expression -> expression.getValueCase() == Expression.ValueCase.FUNCTION) .collect(Collectors.toUnmodifiableList()); } + public static List getAttributeOrderByExpressions( + List expressions) { + return expressions.stream() + .filter(expression -> isAttributeSelection(expression.getExpression())) + .collect(Collectors.toUnmodifiableList()); + } + public static List getAttributeExpressions(List expressions) { return expressions.stream() .filter(ExpressionReader::isAttributeSelection) diff --git a/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/explore/ExploreService.java b/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/explore/ExploreService.java index 7b4f5088..fb98a8e0 100644 --- a/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/explore/ExploreService.java +++ b/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/explore/ExploreService.java @@ -26,9 +26,12 @@ import org.hypertrace.gateway.service.explore.entity.EntityServiceEntityFetcher; import org.hypertrace.gateway.service.v1.explore.ExploreRequest; import org.hypertrace.gateway.service.v1.explore.ExploreResponse; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class ExploreService { + private static final Logger log = LoggerFactory.getLogger(ExploreService.class); private final GatewayServiceConfig gatewayServiceConfig; private final AttributeMetadataProvider attributeMetadataProvider; private final ExploreRequestValidator exploreRequestValidator = new ExploreRequestValidator(); @@ -93,7 +96,6 @@ private void initMetrics() { } public ExploreResponse explore(RequestContext requestContext, ExploreRequest request) { - final Instant start = Instant.now(); try { ExploreRequestContext exploreRequestContext = diff --git a/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/explore/entity/EntityServiceEntityFetcher.java b/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/explore/entity/EntityServiceEntityFetcher.java index 2565c6eb..331f738a 100644 --- a/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/explore/entity/EntityServiceEntityFetcher.java +++ b/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/explore/entity/EntityServiceEntityFetcher.java @@ -25,9 +25,11 @@ import org.hypertrace.gateway.service.common.util.AttributeMetadataUtil; import org.hypertrace.gateway.service.common.util.ExpressionReader; import org.hypertrace.gateway.service.common.util.MetricAggregationFunctionUtil; +import org.hypertrace.gateway.service.common.util.OrderByUtil; import org.hypertrace.gateway.service.entity.config.EntityIdColumnsConfig; import org.hypertrace.gateway.service.explore.ExploreRequestContext; import org.hypertrace.gateway.service.v1.common.FunctionExpression; +import org.hypertrace.gateway.service.v1.common.OrderByExpression; import org.hypertrace.gateway.service.v1.common.Row; import org.hypertrace.gateway.service.v1.explore.ExploreRequest; import org.slf4j.Logger; @@ -165,19 +167,21 @@ private EntityQueryRequest buildRequest( EntityQueryRequest.newBuilder() .setEntityType(entityType) .setFilter(buildFilter(exploreRequest, entityIdAttributeIds, entityIds)); - + addSelections(requestContext, exploreRequest, builder); addGroupBys(exploreRequest, builder); addSortBy(exploreRequest, builder); - addSelections(requestContext, exploreRequest, builder); builder.setLimit(exploreRequest.getLimit()); builder.setOffset(exploreRequest.getOffset()); return builder.build(); } private void addSortBy(ExploreRequest exploreRequest, EntityQueryRequest.Builder builder) { - List orderBys = - exploreRequest.getOrderByList(); - orderBys.forEach( + List orderByExpressions = + OrderByUtil.matchOrderByExpressionsAliasToSelectionAlias( + exploreRequest.getOrderByList(), + exploreRequest.getSelectionList(), + exploreRequest.getTimeAggregationList()); + orderByExpressions.forEach( orderBy -> builder.addOrderBy( EntityServiceAndGatewayServiceConverter.convertToEntityServiceOrderByExpression(