Search in sources :

Example 1 with OrderByExpression

use of org.hypertrace.gateway.service.v1.common.OrderByExpression in project gateway-service by hypertrace.

the class ExecutionTreeBuilder method createQsDataFetcherNodeWithLimitAndOffset.

private QueryNode createQsDataFetcherNodeWithLimitAndOffset(EntitiesRequest entitiesRequest) {
    Filter filter = entitiesRequest.getFilter();
    int selectionLimit = entitiesRequest.getLimit();
    int selectionOffset = entitiesRequest.getOffset();
    List<OrderByExpression> orderBys = entitiesRequest.getOrderByList();
    boolean canFetchTotal = entitiesRequest.getFetchTotal();
    // https://github.com/apache/incubator-pinot/issues/111#issuecomment-214810551
    if (selectionOffset > 0) {
        selectionLimit = selectionOffset + selectionLimit;
        selectionOffset = 0;
    }
    return new DataFetcherNode(QS.name(), filter, selectionLimit, selectionOffset, orderBys, canFetchTotal);
}
Also used : Filter(org.hypertrace.gateway.service.v1.common.Filter) OrderByExpression(org.hypertrace.gateway.service.v1.common.OrderByExpression)

Example 2 with OrderByExpression

use of org.hypertrace.gateway.service.v1.common.OrderByExpression in project gateway-service by hypertrace.

the class ExpressionContext method getDataSourceToOrderByExpressionMap.

private ImmutableMap<String, List<OrderByExpression>> getDataSourceToOrderByExpressionMap(List<OrderByExpression> orderByExpressions) {
    Map<String, List<OrderByExpression>> result = new HashMap<>();
    for (OrderByExpression orderByExpression : orderByExpressions) {
        Expression expression = orderByExpression.getExpression();
        Map<String, List<Expression>> map = getDataSourceToExpressionMap(Collections.singletonList(expression));
        for (String source : map.keySet()) {
            result.computeIfAbsent(source, k -> new ArrayList<>()).add(orderByExpression);
        }
    }
    return ImmutableMap.<String, List<OrderByExpression>>builder().putAll(result).build();
}
Also used : Arrays(java.util.Arrays) Expression(org.hypertrace.gateway.service.v1.common.Expression) AttributeMetadata(org.hypertrace.core.attribute.service.v1.AttributeMetadata) OrderByUtil(org.hypertrace.gateway.service.common.util.OrderByUtil) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) ExpressionReader(org.hypertrace.gateway.service.common.util.ExpressionReader) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) OrderByExpression(org.hypertrace.gateway.service.v1.common.OrderByExpression) Map(java.util.Map) AttributeSource(org.hypertrace.core.attribute.service.v1.AttributeSource) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Filter(org.hypertrace.gateway.service.v1.common.Filter) Set(java.util.Set) TimeAggregation(org.hypertrace.gateway.service.v1.common.TimeAggregation) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) List(java.util.List) Stream(java.util.stream.Stream) Optional(java.util.Optional) Predicate.not(java.util.function.Predicate.not) Collections(java.util.Collections) HashMap(java.util.HashMap) Expression(org.hypertrace.gateway.service.v1.common.Expression) OrderByExpression(org.hypertrace.gateway.service.v1.common.OrderByExpression) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) OrderByExpression(org.hypertrace.gateway.service.v1.common.OrderByExpression)

Example 3 with OrderByExpression

use of org.hypertrace.gateway.service.v1.common.OrderByExpression in project gateway-service by hypertrace.

the class ExecutionTreeBuilderTest method test_build_selectAttributesAndFilterWithSameSource_shouldCreateDataFetcherNodeAndPaginateOnlyNode.

@Test
public void test_build_selectAttributesAndFilterWithSameSource_shouldCreateDataFetcherNodeAndPaginateOnlyNode() {
    OrderByExpression orderByExpression = buildOrderByExpression(API_STATE_ATTR);
    EntitiesRequest entitiesRequest = EntitiesRequest.newBuilder().setEntityType(AttributeScope.API.name()).addSelection(buildExpression(API_STATE_ATTR)).addSelection(buildAggregateExpression(API_NUM_CALLS_ATTR, FunctionType.SUM, "SUM_numCalls", List.of())).setFilter(generateAndOrNotFilter(Operator.AND, generateEQFilter(API_DISCOVERY_STATE, "DISCOVERED"), generateFilter(Operator.GE, API_NUM_CALLS_ATTR, Value.newBuilder().setDouble(60).setValueType(ValueType.DOUBLE).build()))).addOrderBy(orderByExpression).setLimit(10).setOffset(10).build();
    EntitiesRequestContext entitiesRequestContext = new EntitiesRequestContext(TENANT_ID, 0L, 10L, "API", "API.startTime", new HashMap<>());
    EntityExecutionContext executionContext = new EntityExecutionContext(attributeMetadataProvider, entityIdColumnsConfigs, entitiesRequestContext, entitiesRequest);
    ExecutionTreeBuilder executionTreeBuilder = new ExecutionTreeBuilder(executionContext);
    QueryNode executionTree = executionTreeBuilder.build();
    assertNotNull(executionTree);
    assertTrue(executionTree instanceof SelectionNode);
    assertTrue(((SelectionNode) executionTree).getAggMetricSelectionSources().contains("QS"));
    QueryNode paginateOnlyNode = ((SelectionNode) executionTree).getChildNode();
    assertTrue(paginateOnlyNode instanceof PaginateOnlyNode);
    assertEquals(10, ((PaginateOnlyNode) paginateOnlyNode).getOffset());
    assertEquals(10, ((PaginateOnlyNode) paginateOnlyNode).getLimit());
    QueryNode dataFetcherNode = ((PaginateOnlyNode) paginateOnlyNode).getChildNode();
    assertTrue(dataFetcherNode instanceof DataFetcherNode);
    assertEquals("QS", ((DataFetcherNode) dataFetcherNode).getSource());
    assertEquals(0, ((DataFetcherNode) dataFetcherNode).getOffset());
    assertEquals(20, ((DataFetcherNode) dataFetcherNode).getLimit());
}
Also used : EntitiesRequest(org.hypertrace.gateway.service.v1.entity.EntitiesRequest) EntitiesRequestContext(org.hypertrace.gateway.service.entity.EntitiesRequestContext) OrderByExpression(org.hypertrace.gateway.service.v1.common.OrderByExpression) EntitiesRequestAndResponseUtils.buildOrderByExpression(org.hypertrace.gateway.service.common.EntitiesRequestAndResponseUtils.buildOrderByExpression) Test(org.junit.jupiter.api.Test)

Example 4 with OrderByExpression

use of org.hypertrace.gateway.service.v1.common.OrderByExpression in project gateway-service by hypertrace.

the class ExecutionTreeBuilderTest method test_build_selectAttributesAndFilterWithDifferentSourceNonZeroOffset_shouldCreateDataFetcherNodeAndPaginateOnlyNode.

@Test
public void test_build_selectAttributesAndFilterWithDifferentSourceNonZeroOffset_shouldCreateDataFetcherNodeAndPaginateOnlyNode() {
    // selections on EDS and QS
    // filters and order by on QS
    OrderByExpression orderByExpression = buildOrderByExpression(API_STATE_ATTR);
    EntitiesRequest entitiesRequest = EntitiesRequest.newBuilder().setEntityType(AttributeScope.API.name()).addSelection(buildExpression(API_TYPE_ATTR)).addSelection(buildExpression(API_STATE_ATTR)).addSelection(buildAggregateExpression(API_NUM_CALLS_ATTR, FunctionType.SUM, "SUM_numCalls", List.of())).setFilter(generateAndOrNotFilter(Operator.AND, generateEQFilter(API_DISCOVERY_STATE, "DISCOVERED"), generateFilter(Operator.GE, API_NUM_CALLS_ATTR, Value.newBuilder().setDouble(60).setValueType(ValueType.DOUBLE).build()))).addOrderBy(orderByExpression).setLimit(10).setOffset(10).build();
    EntitiesRequestContext entitiesRequestContext = new EntitiesRequestContext(TENANT_ID, 0L, 10L, "API", "API.startTime", new HashMap<>());
    EntityExecutionContext executionContext = new EntityExecutionContext(attributeMetadataProvider, entityIdColumnsConfigs, entitiesRequestContext, entitiesRequest);
    ExecutionTreeBuilder executionTreeBuilder = new ExecutionTreeBuilder(executionContext);
    QueryNode executionTree = executionTreeBuilder.build();
    assertNotNull(executionTree);
    assertTrue(executionTree instanceof SelectionNode);
    assertTrue(((SelectionNode) executionTree).getAggMetricSelectionSources().contains("QS"));
    QueryNode selectionNode = ((SelectionNode) executionTree).getChildNode();
    assertTrue(selectionNode instanceof SelectionNode);
    assertTrue(((SelectionNode) selectionNode).getAttrSelectionSources().contains("EDS"));
    QueryNode paginateOnlyNode = ((SelectionNode) selectionNode).getChildNode();
    assertTrue(paginateOnlyNode instanceof PaginateOnlyNode);
    assertEquals(10, ((PaginateOnlyNode) paginateOnlyNode).getOffset());
    assertEquals(10, ((PaginateOnlyNode) paginateOnlyNode).getLimit());
    QueryNode dataFetcherNode = ((PaginateOnlyNode) paginateOnlyNode).getChildNode();
    assertTrue(dataFetcherNode instanceof DataFetcherNode);
    assertEquals("QS", ((DataFetcherNode) dataFetcherNode).getSource());
    assertEquals(0, ((DataFetcherNode) dataFetcherNode).getOffset());
    assertEquals(20, ((DataFetcherNode) dataFetcherNode).getLimit());
}
Also used : EntitiesRequest(org.hypertrace.gateway.service.v1.entity.EntitiesRequest) EntitiesRequestContext(org.hypertrace.gateway.service.entity.EntitiesRequestContext) OrderByExpression(org.hypertrace.gateway.service.v1.common.OrderByExpression) EntitiesRequestAndResponseUtils.buildOrderByExpression(org.hypertrace.gateway.service.common.EntitiesRequestAndResponseUtils.buildOrderByExpression) Test(org.junit.jupiter.api.Test)

Example 5 with OrderByExpression

use of org.hypertrace.gateway.service.v1.common.OrderByExpression in project gateway-service by hypertrace.

the class ExecutionVisitorTest method test_visitPaginateOnlyNode.

@Test
public void test_visitPaginateOnlyNode() {
    List<OrderByExpression> orderByExpressions = List.of(buildOrderByExpression(API_ID_ATTR));
    int limit = 2;
    int offset = 2;
    long startTime = 0;
    long endTime = 10;
    String tenantId = "TENANT_ID";
    Map<String, String> requestHeaders = Map.of("x-tenant-id", tenantId);
    AttributeScope entityType = AttributeScope.API;
    Expression selectionExpression = buildExpression(API_NAME_ATTR);
    Expression metricExpression = buildAggregateExpression(API_DURATION_ATTR, FunctionType.AVG, "AVG_API.duration", List.of());
    TimeAggregation timeAggregation = buildTimeAggregation(30, API_NUM_CALLS_ATTR, FunctionType.SUM, "SUM_API.numCalls", List.of());
    EntitiesRequest entitiesRequest = EntitiesRequest.newBuilder().setEntityType(entityType.name()).setStartTimeMillis(startTime).setEndTimeMillis(endTime).addSelection(selectionExpression).addSelection(metricExpression).addTimeAggregation(timeAggregation).setFilter(generateEQFilter(API_DISCOVERY_STATE, "DISCOVERED")).addAllOrderBy(orderByExpressions).setLimit(limit).setOffset(offset).build();
    EntitiesRequestContext entitiesRequestContext = new EntitiesRequestContext(tenantId, startTime, endTime, entityType.name(), "API.startTime", requestHeaders);
    // Order matters since we will do the pagination ourselves. So we use a LinkedHashMap
    Map<EntityKey, Builder> entityKeyBuilderResponseMap1 = new LinkedHashMap<>();
    entityKeyBuilderResponseMap1.put(EntityKey.of("entity-id-0"), Entity.newBuilder().putAttribute("API.name", getStringValue("entity-0")));
    entityKeyBuilderResponseMap1.put(EntityKey.of("entity-id-1"), Entity.newBuilder().putAttribute("API.name", getStringValue("entity-1")));
    entityKeyBuilderResponseMap1.put(EntityKey.of("entity-id-2"), Entity.newBuilder().putAttribute("API.name", getStringValue("entity-2")));
    entityKeyBuilderResponseMap1.put(EntityKey.of("entity-id-3"), Entity.newBuilder().putAttribute("API.name", getStringValue("entity-3")));
    Map<EntityKey, Builder> entityKeyBuilderResponseMap2 = new LinkedHashMap<>();
    entityKeyBuilderResponseMap2.put(EntityKey.of("entity-id-2"), Entity.newBuilder().putMetric("AVG_API.duration", getAggregatedMetricValue(FunctionType.AVG, 14.0)));
    entityKeyBuilderResponseMap2.put(EntityKey.of("entity-id-3"), Entity.newBuilder().putMetric("AVG_API.duration", getAggregatedMetricValue(FunctionType.AVG, 15.0)));
    Map<EntityKey, Builder> entityKeyBuilderResponseMap3 = new LinkedHashMap<>();
    entityKeyBuilderResponseMap3.put(EntityKey.of("entity-id-2"), Entity.newBuilder().putMetricSeries("SUM_API.numCalls", getMockMetricSeries(30, "SUM")));
    entityKeyBuilderResponseMap3.put(EntityKey.of("entity-id-3"), Entity.newBuilder().putMetricSeries("SUM_API.numCalls", getMockMetricSeries(30, "SUM")));
    Map<EntityKey, Builder> expectedEntityKeyBuilderResponseMap = new LinkedHashMap<>();
    expectedEntityKeyBuilderResponseMap.put(EntityKey.of("entity-id-2"), Entity.newBuilder().putAttribute("API.name", getStringValue("entity-2")).putMetric("AVG_API.duration", getAggregatedMetricValue(FunctionType.AVG, 14.0)).putMetricSeries("SUM_API.numCalls", getMockMetricSeries(30, "SUM")));
    expectedEntityKeyBuilderResponseMap.put(EntityKey.of("entity-id-3"), Entity.newBuilder().putAttribute("API.name", getStringValue("entity-3")).putMetric("AVG_API.duration", getAggregatedMetricValue(FunctionType.AVG, 15.0)).putMetricSeries("SUM_API.numCalls", getMockMetricSeries(30, "SUM")));
    when(executionContext.getEntityIdExpressions()).thenReturn(List.of(buildExpression(API_ID_ATTR)));
    when(expressionContext.getSourceToSelectionExpressionMap()).thenReturn(Map.of("QS", List.of(selectionExpression)));
    when(expressionContext.getSourceToMetricExpressionMap()).thenReturn(Map.of("QS", List.of(metricExpression)));
    when(expressionContext.getSourceToTimeAggregationMap()).thenReturn(Map.of("QS", List.of(timeAggregation)));
    when(executionContext.getEntitiesRequest()).thenReturn(entitiesRequest);
    when(executionContext.getTenantId()).thenReturn(tenantId);
    when(executionContext.getRequestHeaders()).thenReturn(requestHeaders);
    when(executionContext.getTimestampAttributeId()).thenReturn("API.startTime");
    EntitiesRequest entitiesRequestForAttributes = EntitiesRequest.newBuilder(entitiesRequest).clearSelection().clearTimeAggregation().addSelection(selectionExpression).setLimit(limit + offset).setOffset(0).build();
    EntityFetcherResponse attributesResponse = new EntityFetcherResponse(entityKeyBuilderResponseMap1);
    EntitiesRequest entitiesRequestForMetricAggregation = EntitiesRequest.newBuilder(entitiesRequest).clearLimit().clearOffset().clearOrderBy().clearSelection().clearTimeAggregation().addSelection(metricExpression).clearFilter().setFilter(generateInFilter(API_ID_ATTR, List.of("entity-id-3", "entity-id-2"))).build();
    EntitiesRequest entitiesRequestForTimeAggregation = EntitiesRequest.newBuilder(entitiesRequest).clearSelection().clearLimit().clearOffset().clearOrderBy().clearFilter().setFilter(generateInFilter(API_ID_ATTR, List.of("entity-id-3", "entity-id-2"))).build();
    when(queryServiceEntityFetcher.getEntities(entitiesRequestContext, entitiesRequestForAttributes)).thenReturn(attributesResponse);
    when(queryServiceEntityFetcher.getTotal(eq(entitiesRequestContext), eq(entitiesRequest))).thenReturn(100L);
    when(queryServiceEntityFetcher.getEntities(entitiesRequestContext, entitiesRequestForMetricAggregation)).thenReturn(new EntityFetcherResponse(entityKeyBuilderResponseMap2));
    when(queryServiceEntityFetcher.getTimeAggregatedMetrics(entitiesRequestContext, entitiesRequestForTimeAggregation)).thenReturn(new EntityFetcherResponse(entityKeyBuilderResponseMap3));
    DataFetcherNode dataFetcherNode = new DataFetcherNode("QS", entitiesRequest.getFilter(), limit + offset, 0, orderByExpressions, true);
    PaginateOnlyNode paginateOnlyNode = new PaginateOnlyNode(dataFetcherNode, limit, offset);
    SelectionNode childSelectionNode = new SelectionNode.Builder(paginateOnlyNode).setAggMetricSelectionSources(Set.of("QS")).build();
    SelectionNode selectionNode = new SelectionNode.Builder(childSelectionNode).setTimeSeriesSelectionSources(Set.of("QS")).build();
    compareEntityResponses(new EntityResponse(new EntityFetcherResponse(expectedEntityKeyBuilderResponseMap), 100), executionVisitor.visit(selectionNode));
}
Also used : Builder(org.hypertrace.gateway.service.v1.entity.Entity.Builder) LinkedHashMap(java.util.LinkedHashMap) EntityKey(org.hypertrace.gateway.service.entity.EntityKey) EntityFetcherResponse(org.hypertrace.gateway.service.common.datafetcher.EntityFetcherResponse) EntitiesRequest(org.hypertrace.gateway.service.v1.entity.EntitiesRequest) AttributeScope(org.hypertrace.core.attribute.service.v1.AttributeScope) EntitiesRequestAndResponseUtils.buildAggregateExpression(org.hypertrace.gateway.service.common.EntitiesRequestAndResponseUtils.buildAggregateExpression) EntitiesRequestAndResponseUtils.buildOrderByExpression(org.hypertrace.gateway.service.common.EntitiesRequestAndResponseUtils.buildOrderByExpression) EntitiesRequestAndResponseUtils.buildExpression(org.hypertrace.gateway.service.common.EntitiesRequestAndResponseUtils.buildExpression) Expression(org.hypertrace.gateway.service.v1.common.Expression) OrderByExpression(org.hypertrace.gateway.service.v1.common.OrderByExpression) EntityResponse(org.hypertrace.gateway.service.common.datafetcher.EntityResponse) DataFetcherNode(org.hypertrace.gateway.service.entity.query.DataFetcherNode) EntitiesRequestContext(org.hypertrace.gateway.service.entity.EntitiesRequestContext) EntitiesRequestAndResponseUtils.buildOrderByExpression(org.hypertrace.gateway.service.common.EntitiesRequestAndResponseUtils.buildOrderByExpression) OrderByExpression(org.hypertrace.gateway.service.v1.common.OrderByExpression) EntitiesRequestAndResponseUtils.buildTimeAggregation(org.hypertrace.gateway.service.common.EntitiesRequestAndResponseUtils.buildTimeAggregation) TimeAggregation(org.hypertrace.gateway.service.v1.common.TimeAggregation) PaginateOnlyNode(org.hypertrace.gateway.service.entity.query.PaginateOnlyNode) SelectionNode(org.hypertrace.gateway.service.entity.query.SelectionNode) Test(org.junit.jupiter.api.Test)

Aggregations

OrderByExpression (org.hypertrace.gateway.service.v1.common.OrderByExpression)30 Test (org.junit.jupiter.api.Test)23 EntitiesRequestAndResponseUtils.buildOrderByExpression (org.hypertrace.gateway.service.common.EntitiesRequestAndResponseUtils.buildOrderByExpression)16 EntitiesRequest (org.hypertrace.gateway.service.v1.entity.EntitiesRequest)16 EntitiesRequestContext (org.hypertrace.gateway.service.entity.EntitiesRequestContext)14 AttributeScope (org.hypertrace.core.attribute.service.v1.AttributeScope)12 EntityKey (org.hypertrace.gateway.service.entity.EntityKey)9 Builder (org.hypertrace.gateway.service.v1.entity.Entity.Builder)8 ExploreRequest (org.hypertrace.gateway.service.v1.explore.ExploreRequest)7 QueryServiceClient (org.hypertrace.core.query.service.client.QueryServiceClient)6 AttributeMetadataProvider (org.hypertrace.gateway.service.common.AttributeMetadataProvider)6 DataFetcherNode (org.hypertrace.gateway.service.entity.query.DataFetcherNode)6 Expression (org.hypertrace.gateway.service.v1.common.Expression)6 LinkedHashMap (java.util.LinkedHashMap)5 QueryRequest (org.hypertrace.core.query.service.api.QueryRequest)5 ResultSetChunk (org.hypertrace.core.query.service.api.ResultSetChunk)5 QueryServiceRequestAndResponseUtils.getResultSetChunk (org.hypertrace.gateway.service.common.QueryServiceRequestAndResponseUtils.getResultSetChunk)5 TimeAggregation (org.hypertrace.gateway.service.v1.common.TimeAggregation)5 EntitiesRequestAndResponseUtils.buildAggregateExpression (org.hypertrace.gateway.service.common.EntitiesRequestAndResponseUtils.buildAggregateExpression)4 EntitiesRequestAndResponseUtils.buildExpression (org.hypertrace.gateway.service.common.EntitiesRequestAndResponseUtils.buildExpression)4