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

Commit

Permalink
fixing the order by
Browse files Browse the repository at this point in the history
  • Loading branch information
aman-bansal committed Jun 17, 2024
1 parent 1141646 commit 0ef4cb6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, FunctionExpression> aliasToFunctionExpressionMap = new HashMap<>();
private final Map<FunctionExpression, String> functionExpressionToAliasMap = new HashMap<>();
private final long startTimeMillis;
private final long endTimeMillis;
private final Map<String, TimeAggregation> aliasToTimeAggregation = new HashMap<>();
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<OrderByExpression> getFunctionOrderByExpressions(
List<OrderByExpression> expressions) {
return expressions.stream()
.filter(
expression ->
expression.getExpression().getValueCase() == Expression.ValueCase.FUNCTION)
.collect(Collectors.toUnmodifiableList());
}

public static List<Expression> getFunctionExpressions(List<Expression> expressions) {
return expressions.stream()
.filter(expression -> expression.getValueCase() == Expression.ValueCase.FUNCTION)
.collect(Collectors.toUnmodifiableList());
}

public static List<OrderByExpression> getAttributeOrderByExpressions(
List<OrderByExpression> expressions) {
return expressions.stream()
.filter(expression -> isAttributeSelection(expression.getExpression()))
.collect(Collectors.toUnmodifiableList());
}

public static List<Expression> getAttributeExpressions(List<Expression> expressions) {
return expressions.stream()
.filter(ExpressionReader::isAttributeSelection)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -93,7 +96,6 @@ private void initMetrics() {
}

public ExploreResponse explore(RequestContext requestContext, ExploreRequest request) {

final Instant start = Instant.now();
try {
ExploreRequestContext exploreRequestContext =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<org.hypertrace.gateway.service.v1.common.OrderByExpression> orderBys =
exploreRequest.getOrderByList();
orderBys.forEach(
List<OrderByExpression> orderByExpressions =
OrderByUtil.matchOrderByExpressionsAliasToSelectionAlias(
exploreRequest.getOrderByList(),
exploreRequest.getSelectionList(),
exploreRequest.getTimeAggregationList());
orderByExpressions.forEach(
orderBy ->
builder.addOrderBy(
EntityServiceAndGatewayServiceConverter.convertToEntityServiceOrderByExpression(
Expand Down

0 comments on commit 0ef4cb6

Please sign in to comment.