From 0c5d296f19897a1fa435f21fc9efe192c18d8507 Mon Sep 17 00:00:00 2001 From: SJ Date: Fri, 28 Oct 2022 17:43:58 +0530 Subject: [PATCH] refactor(expression-context): expression location based single source --- .../service/common/ExpressionContext.java | 291 +++++++++++++----- .../service/common/ExpressionLocation.java | 11 + .../transformer/ResponsePostProcessor.java | 2 +- .../entity/query/EntityExecutionContext.java | 2 +- .../query/visitor/ExecutionVisitor.java | 2 +- .../service/common/ExpressionContextTest.java | 41 +-- .../ResponsePostProcessorTest.java | 6 +- .../entity/query/ExecutionTreeUtilsTest.java | 35 --- .../query/visitor/ExecutionVisitorTest.java | 6 +- 9 files changed, 248 insertions(+), 148 deletions(-) create mode 100644 gateway-service-impl/src/main/java/org/hypertrace/gateway/service/common/ExpressionLocation.java diff --git a/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/common/ExpressionContext.java b/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/common/ExpressionContext.java index b57a63ab..093b13d1 100644 --- a/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/common/ExpressionContext.java +++ b/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/common/ExpressionContext.java @@ -1,7 +1,5 @@ package org.hypertrace.gateway.service.common; -import static java.util.function.Predicate.not; - import com.google.common.collect.ImmutableMap; import com.google.common.collect.Sets; import java.util.ArrayList; @@ -34,20 +32,28 @@ public class ExpressionContext { // selections private final List selections; - private final List timeAggregations; private ImmutableMap> sourceToSelectionExpressionMap; private ImmutableMap> sourceToSelectionAttributeMap; + private ImmutableMap> selectionAttributeToSourceMap; - private ImmutableMap> sourceToMetricExpressionMap; + private ImmutableMap> sourceToMetricAggregationExpressionMap; + private ImmutableMap> sourceToMetricAggregationAttributeMap; + private ImmutableMap> metricAggregationAttributeToSourceMap; + + private final List timeAggregations; private ImmutableMap> sourceToTimeAggregationMap; + private ImmutableMap> sourceToTimeAggregationAttributeMap; + private ImmutableMap> timeAggregationAttributeToSourceMap; // order bys private final List orderBys; private ImmutableMap> sourceToSelectionOrderByExpressionMap; private ImmutableMap> sourceToSelectionOrderByAttributeMap; + private ImmutableMap> selectionOrderByAttributeToSourceMap; private ImmutableMap> sourceToMetricOrderByExpressionMap; private ImmutableMap> sourceToMetricOrderByAttributeMap; + private ImmutableMap> metricOrderByAttributeToSourceMap; // filters private final Filter filter; @@ -58,10 +64,8 @@ public class ExpressionContext { // group bys private final List groupBys; private ImmutableMap> sourceToGroupByExpressionMap; - - // map of filter, selections (attribute, metrics, aggregations), group bys, order by attributes to - // source map - private final Map> allAttributesToSourcesMap = new HashMap<>(); + private ImmutableMap> sourceToGroupByAttributeMap; + private ImmutableMap> groupByAttributeToSourceMap; public ExpressionContext( Map attributeMetadataMap, @@ -78,7 +82,7 @@ public ExpressionContext( this.orderBys = orderBys; this.groupBys = groupBys; - buildSourceToExpressionMaps(); + buildSourceToSelectionExpressionMaps(); buildSourceToFilterExpressionMaps(); buildSourceToOrderByExpressionMaps(); buildSourceToGroupByExpressionMaps(); @@ -105,16 +109,24 @@ public void setSourceToSelectionAttributeMap( this.sourceToSelectionAttributeMap = buildSourceToAttributesMap(sourceToSelectionExpressionMap); } - public Map> getSourceToMetricExpressionMap() { - return sourceToMetricExpressionMap; + public Map> getSelectionAttributeToSourceMap() { + return selectionAttributeToSourceMap; + } + + public Map> getSourceToMetricAggregationExpressionMap() { + return sourceToMetricAggregationExpressionMap; + } + + public Map> getMetricAggregationAttributeToSourceMap() { + return metricAggregationAttributeToSourceMap; } public Map> getSourceToTimeAggregationMap() { return sourceToTimeAggregationMap; } - public Map> getSourceToGroupByExpressionMap() { - return sourceToGroupByExpressionMap; + public Map> getTimeAggregationAttributeToSourceMap() { + return timeAggregationAttributeToSourceMap; } public Map> getSourceToSelectionOrderByExpressionMap() { @@ -125,6 +137,10 @@ public Map> getSourceToSelectionOrderByAttributeMap() { return sourceToSelectionOrderByAttributeMap; } + public ImmutableMap> getSelectionOrderByAttributeToSourceMap() { + return selectionOrderByAttributeToSourceMap; + } + public Map> getSourceToMetricOrderByExpressionMap() { return sourceToMetricOrderByExpressionMap; } @@ -133,6 +149,10 @@ public Map> getSourceToMetricOrderByAttributeMap() { return sourceToMetricOrderByAttributeMap; } + public Map> getMetricOrderByAttributeToSourceMap() { + return metricOrderByAttributeToSourceMap; + } + public Map> getSourceToFilterExpressionMap() { return sourceToFilterExpressionMap; } @@ -145,32 +165,44 @@ public Map> getFilterAttributeToSourceMap() { return filterAttributeToSourceMap; } - public Map> getAllAttributesToSourcesMap() { - return allAttributesToSourcesMap; + public Map> getSourceToGroupByExpressionMap() { + return sourceToGroupByExpressionMap; } - private void buildSourceToExpressionMaps() { + public ImmutableMap> getGroupByAttributeToSourceMap() { + return groupByAttributeToSourceMap; + } + + private void buildSourceToSelectionExpressionMaps() { List attributeSelections = selections.stream() .filter(ExpressionReader::isAttributeSelection) .collect(Collectors.toUnmodifiableList()); sourceToSelectionExpressionMap = getDataSourceToExpressionMap(attributeSelections); sourceToSelectionAttributeMap = buildSourceToAttributesMap(sourceToSelectionExpressionMap); + selectionAttributeToSourceMap = buildAttributeToSourcesMap(sourceToSelectionAttributeMap); + List functionSelections = selections.stream() .filter(Expression::hasFunction) .collect(Collectors.toUnmodifiableList()); - sourceToMetricExpressionMap = getDataSourceToExpressionMap(functionSelections); + sourceToMetricAggregationExpressionMap = getDataSourceToExpressionMap(functionSelections); + sourceToMetricAggregationAttributeMap = + buildSourceToAttributesMap(sourceToMetricAggregationExpressionMap); + metricAggregationAttributeToSourceMap = + buildAttributeToSourcesMap(sourceToMetricAggregationAttributeMap); + sourceToTimeAggregationMap = getDataSourceToTimeAggregation(timeAggregations); + sourceToTimeAggregationAttributeMap = + buildSourceToTimeAggregationAttributesMap(sourceToTimeAggregationMap); + timeAggregationAttributeToSourceMap = + buildAttributeToSourcesMap(sourceToTimeAggregationAttributeMap); } private void buildSourceToFilterExpressionMaps() { sourceToFilterExpressionMap = getSourceToFilterExpressionMap(filter); sourceToFilterAttributeMap = buildSourceToAttributesMap(sourceToFilterExpressionMap); - filterAttributeToSourceMap = - ImmutableMap.>builder() - .putAll(buildAttributeToSourcesMap(sourceToFilterAttributeMap)) - .build(); + filterAttributeToSourceMap = buildAttributeToSourcesMap(sourceToFilterAttributeMap); } private void buildSourceToOrderByExpressionMaps() { @@ -193,6 +225,8 @@ private void buildSourceToOrderByExpressionMaps() { sourceToSelectionOrderByAttributeMap = buildSourceToAttributesMap( convertOrderByExpressionToExpression(sourceToSelectionOrderByExpressionMap)); + selectionOrderByAttributeToSourceMap = + buildAttributeToSourcesMap(sourceToSelectionOrderByAttributeMap); List functionOrderByExpressions = orderByExpressions.stream() @@ -204,10 +238,14 @@ private void buildSourceToOrderByExpressionMaps() { sourceToMetricOrderByAttributeMap = buildSourceToAttributesMap( convertOrderByExpressionToExpression(sourceToMetricOrderByExpressionMap)); + metricOrderByAttributeToSourceMap = + buildAttributeToSourcesMap(sourceToMetricOrderByAttributeMap); } private void buildSourceToGroupByExpressionMaps() { sourceToGroupByExpressionMap = getDataSourceToExpressionMap(groupBys); + sourceToGroupByAttributeMap = buildSourceToAttributesMap(sourceToGroupByExpressionMap); + groupByAttributeToSourceMap = buildAttributeToSourcesMap(sourceToGroupByAttributeMap); } private ImmutableMap> getDataSourceToOrderByExpressionMap( @@ -289,9 +327,6 @@ private ImmutableMap> getDataSourceToExpressionMap( for (String attributeId : attributeIds) { List sourcesList = attributeMetadataMap.get(attributeId).getSourcesList(); sources.retainAll(sourcesList); - allAttributesToSourcesMap - .computeIfAbsent(attributeId, v -> new HashSet<>()) - .addAll(sourcesList.stream().map(Enum::name).collect(Collectors.toList())); } if (sources.isEmpty()) { LOG.error("Skipping Expression: {}. No source found", expression); @@ -326,6 +361,23 @@ private ImmutableMap> buildSourceToAttributesMap( .build(); } + /** + * Given a source to time aggregation map, builds a source to time aggregation attribute map, + * where the attribute names are extracted out as column names from the time aggregation + */ + private ImmutableMap> buildSourceToTimeAggregationAttributesMap( + Map> sourceToTimeAggregationMap) { + return buildSourceToAttributesMap( + sourceToTimeAggregationMap.entrySet().stream() + .collect( + Collectors.toUnmodifiableMap( + Map.Entry::getKey, + entry -> + entry.getValue().stream() + .map(TimeAggregation::getAggregation) + .collect(Collectors.toUnmodifiableList())))); + } + private Map> convertOrderByExpressionToExpression( Map> sourceToOrderByExpressions) { return sourceToOrderByExpressions.entrySet().stream() @@ -340,17 +392,37 @@ private Map> convertOrderByExpressionToExpression( /** * Returns a non-empty optional if all the attributes in the selection(attributes and - * aggregations), time aggregations, filter and order by can be read from the same source. + * aggregations), time aggregations, filters, order bys and group bys can be read from the same + * source. * * @param expressionContext * @return */ public static Optional getSingleSourceForAllAttributes( ExpressionContext expressionContext) { - Optional singleSourceFromKeySets = getSingleSourceFromKeySets(expressionContext); + Set allLocations = Set.of(ExpressionLocation.values()); + Optional singleSourceFromKeySets = + getSingleSourceFromKeySets(expressionContext, allLocations); return singleSourceFromKeySets.or( - () -> getSingleSourceFromAttributeSourceValueSets(expressionContext)); + () -> getSingleSourceFromAttributeSourceValueSets(expressionContext, allLocations)); + } + + /** + * Returns a non-empty optional if all the attributes in the selection(attributes and + * aggregations), time aggregations, filters, order bys and group bys can be read from the same + * source, depending on the provided locations + * + * @param expressionContext + * @return + */ + public static Optional getSingleSourceForAttributes( + ExpressionContext expressionContext, Set locations) { + Optional singleSourceFromKeySets = + getSingleSourceFromKeySets(expressionContext, locations); + + return singleSourceFromKeySets.or( + () -> getSingleSourceFromAttributeSourceValueSets(expressionContext, locations)); } /** @@ -360,28 +432,54 @@ public static Optional getSingleSourceForAllAttributes( * @param expressionContext * @return */ - private static Optional getSingleSourceFromKeySets(ExpressionContext expressionContext) { - Set selectionsSourceSet = - expressionContext.getSourceToSelectionExpressionMap().keySet(); - Set metricAggregationsSourceSet = - expressionContext.getSourceToMetricExpressionMap().keySet(); - Set timeAggregationsSourceSet = - expressionContext.getSourceToTimeAggregationMap().keySet(); - Set filtersSourceSet = expressionContext.getSourceToFilterExpressionMap().keySet(); - Set groupBysSourceSet = expressionContext.getSourceToGroupByExpressionMap().keySet(); - Set selectionOrderBysSourceSet = - expressionContext.getSourceToSelectionOrderByExpressionMap().keySet(); - Set metricAggregationOrderBysSourceSet = - expressionContext.getSourceToMetricOrderByExpressionMap().keySet(); + private static Optional getSingleSourceFromKeySets( + ExpressionContext expressionContext, Set locations) { + if (locations.isEmpty()) { + return Optional.empty(); + } Set sources = new HashSet<>(); - sources.addAll(selectionsSourceSet); - sources.addAll(metricAggregationsSourceSet); - sources.addAll(timeAggregationsSourceSet); - sources.addAll(filtersSourceSet); - sources.addAll(groupBysSourceSet); - sources.addAll(selectionOrderBysSourceSet); - sources.addAll(metricAggregationOrderBysSourceSet); + for (ExpressionLocation location : locations) { + switch (location) { + case COLUMN_SELECTION: + Set selectionsSourceSet = + expressionContext.getSourceToSelectionExpressionMap().keySet(); + sources.addAll(selectionsSourceSet); + continue; + case METRIC_AGGREGATION: + Set metricAggregationsSourceSet = + expressionContext.getSourceToMetricAggregationExpressionMap().keySet(); + sources.addAll(metricAggregationsSourceSet); + continue; + case TIME_AGGREGATION: + Set timeAggregationsSourceSet = + expressionContext.getSourceToTimeAggregationMap().keySet(); + sources.addAll(timeAggregationsSourceSet); + continue; + case COLUMN_FILTER: + Set filtersSourceSet = + expressionContext.getSourceToFilterExpressionMap().keySet(); + sources.addAll(filtersSourceSet); + continue; + case COLUMN_GROUP_BY: + Set groupBysSourceSet = + expressionContext.getSourceToGroupByExpressionMap().keySet(); + sources.addAll(groupBysSourceSet); + continue; + case COLUMN_ORDER_BY: + Set selectionOrderBysSourceSet = + expressionContext.getSourceToSelectionOrderByExpressionMap().keySet(); + sources.addAll(selectionOrderBysSourceSet); + continue; + case METRIC_ORDER_BY: + Set metricAggregationOrderBysSourceSet = + expressionContext.getSourceToMetricOrderByExpressionMap().keySet(); + sources.addAll(metricAggregationOrderBysSourceSet); + continue; + default: + LOG.error("Unrecognised expression location {}", location); + } + } if (sources.size() == 1) { return sources.stream().findFirst(); } else { @@ -398,25 +496,62 @@ private static Optional getSingleSourceFromKeySets(ExpressionContext exp * @return */ private static Optional getSingleSourceFromAttributeSourceValueSets( - ExpressionContext expressionContext) { - // Compute the intersection of all sources in attributesToSourcesMap and check if it's size is 1 - Set attributeSourcesIntersection = - expressionContext.getAllAttributesToSourcesMap().values().stream() - .filter(not(Set::isEmpty)) - .findFirst() - .orElse(Collections.emptySet()); - - if (attributeSourcesIntersection.isEmpty()) { + ExpressionContext expressionContext, Set locations) { + if (locations.isEmpty()) { return Optional.empty(); } - attributeSourcesIntersection = new HashSet<>(attributeSourcesIntersection); - - for (Set attributeSourcesSet : - expressionContext.getAllAttributesToSourcesMap().values()) { - // retainAll() for sets computes the intersections. - attributeSourcesIntersection.retainAll(attributeSourcesSet); + // retainAll() for sets computes the intersections. + Set attributeSourcesIntersection = new HashSet<>(); + for (ExpressionLocation location : locations) { + switch (location) { + case COLUMN_SELECTION: + expressionContext + .getSelectionAttributeToSourceMap() + .values() + .forEach(attributeSourcesIntersection::retainAll); + continue; + case METRIC_AGGREGATION: + expressionContext + .getMetricAggregationAttributeToSourceMap() + .values() + .forEach(attributeSourcesIntersection::retainAll); + continue; + case TIME_AGGREGATION: + expressionContext + .getTimeAggregationAttributeToSourceMap() + .values() + .forEach(attributeSourcesIntersection::retainAll); + continue; + case COLUMN_FILTER: + expressionContext + .getFilterAttributeToSourceMap() + .values() + .forEach(attributeSourcesIntersection::retainAll); + continue; + case COLUMN_GROUP_BY: + expressionContext + .getGroupByAttributeToSourceMap() + .values() + .forEach(attributeSourcesIntersection::retainAll); + continue; + case COLUMN_ORDER_BY: + expressionContext + .getSelectionOrderByAttributeToSourceMap() + .values() + .forEach(attributeSourcesIntersection::retainAll); + continue; + case METRIC_ORDER_BY: + expressionContext + .getMetricOrderByAttributeToSourceMap() + .values() + .forEach(attributeSourcesIntersection::retainAll); + continue; + default: + LOG.error("Unrecognised expression location {}", location); + } } + attributeSourcesIntersection = new HashSet<>(attributeSourcesIntersection); if (attributeSourcesIntersection.size() == 1) { return attributeSourcesIntersection.stream().findFirst(); @@ -497,7 +632,7 @@ public static Set getSourceSetsIfFilterAndOrderByAreFromSameSourceSets( * *

("API.id" -> ["QS", "EDS"], "API.name" -> "QS") */ - private static Map> buildAttributeToSourcesMap( + private static ImmutableMap> buildAttributeToSourcesMap( Map> sourcesToAttributeMap) { Map> attributeToSourcesMap = new HashMap<>(); for (Map.Entry> entry : sourcesToAttributeMap.entrySet()) { @@ -506,7 +641,7 @@ private static Map> buildAttributeToSourcesMap( attributeToSourcesMap.computeIfAbsent(attribute, k -> new HashSet<>()).add(source); } } - return Collections.unmodifiableMap(attributeToSourcesMap); + return ImmutableMap.>builder().putAll(attributeToSourcesMap).build(); } /** @@ -586,26 +721,40 @@ public String toString() { + attributeMetadataMap + ", selections=" + selections - + ", timeAggregations=" - + timeAggregations + ", sourceToSelectionExpressionMap=" + sourceToSelectionExpressionMap + ", sourceToSelectionAttributeMap=" + sourceToSelectionAttributeMap - + ", sourceToMetricExpressionMap=" - + sourceToMetricExpressionMap + + ", selectionAttributeToSourceMap=" + + selectionAttributeToSourceMap + + ", sourceToMetricAggregationExpressionMap=" + + sourceToMetricAggregationExpressionMap + + ", sourceToMetricAggregationAttributeMap=" + + sourceToMetricAggregationAttributeMap + + ", metricAggregationAttributeToSourceMap=" + + metricAggregationAttributeToSourceMap + + ", timeAggregations=" + + timeAggregations + ", sourceToTimeAggregationMap=" + sourceToTimeAggregationMap + + ", sourceToTimeAggregationAttributeMap=" + + sourceToTimeAggregationAttributeMap + + ", timeAggregationAttributeToSourceMap=" + + timeAggregationAttributeToSourceMap + ", orderBys=" + orderBys + ", sourceToSelectionOrderByExpressionMap=" + sourceToSelectionOrderByExpressionMap + ", sourceToSelectionOrderByAttributeMap=" + sourceToSelectionOrderByAttributeMap + + ", selectionOrderByAttributeToSourceMap=" + + selectionOrderByAttributeToSourceMap + ", sourceToMetricOrderByExpressionMap=" + sourceToMetricOrderByExpressionMap + ", sourceToMetricOrderByAttributeMap=" + sourceToMetricOrderByAttributeMap + + ", metricOrderByAttributeToSourceMap=" + + metricOrderByAttributeToSourceMap + ", filter=" + filter + ", sourceToFilterExpressionMap=" @@ -618,8 +767,10 @@ public String toString() { + groupBys + ", sourceToGroupByExpressionMap=" + sourceToGroupByExpressionMap - + ", allAttributesToSourcesMap=" - + allAttributesToSourcesMap + + ", sourceToGroupByAttributeMap=" + + sourceToGroupByAttributeMap + + ", groupByAttributeToSourceMap=" + + groupByAttributeToSourceMap + '}'; } } diff --git a/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/common/ExpressionLocation.java b/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/common/ExpressionLocation.java new file mode 100644 index 00000000..8d9fafe2 --- /dev/null +++ b/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/common/ExpressionLocation.java @@ -0,0 +1,11 @@ +package org.hypertrace.gateway.service.common; + +public enum ExpressionLocation { + COLUMN_SELECTION, + METRIC_AGGREGATION, + TIME_AGGREGATION, + COLUMN_ORDER_BY, + METRIC_ORDER_BY, + COLUMN_FILTER, + COLUMN_GROUP_BY, +} diff --git a/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/common/transformer/ResponsePostProcessor.java b/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/common/transformer/ResponsePostProcessor.java index 13a600c1..c8aeff1c 100644 --- a/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/common/transformer/ResponsePostProcessor.java +++ b/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/common/transformer/ResponsePostProcessor.java @@ -30,7 +30,7 @@ public List transform( .flatMap(Optional::stream) .collect(Collectors.toSet()); Set aggregations = - executionContext.getExpressionContext().getSourceToMetricExpressionMap().values().stream() + executionContext.getExpressionContext().getSourceToMetricAggregationExpressionMap().values().stream() .flatMap(Collection::stream) .map(expression -> expression.getFunction().getAlias()) .collect(Collectors.toSet()); diff --git a/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/entity/query/EntityExecutionContext.java b/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/entity/query/EntityExecutionContext.java index 3b60f3a6..fb9d02d2 100644 --- a/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/entity/query/EntityExecutionContext.java +++ b/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/entity/query/EntityExecutionContext.java @@ -186,7 +186,7 @@ public void removeSelectionAttributes(String source, Set attributes) { private void buildSelectionPendingSources() { pendingSelectionSources.addAll(expressionContext.getSourceToSelectionExpressionMap().keySet()); pendingMetricAggregationSources.addAll( - expressionContext.getSourceToMetricExpressionMap().keySet()); + expressionContext.getSourceToMetricAggregationExpressionMap().keySet()); pendingTimeAggregationSources.addAll( expressionContext.getSourceToTimeAggregationMap().keySet()); } diff --git a/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/entity/query/visitor/ExecutionVisitor.java b/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/entity/query/visitor/ExecutionVisitor.java index b3c48ed4..36678820 100644 --- a/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/entity/query/visitor/ExecutionVisitor.java +++ b/gateway-service-impl/src/main/java/org/hypertrace/gateway/service/entity/query/visitor/ExecutionVisitor.java @@ -261,7 +261,7 @@ public EntityResponse visit(SelectionNode selectionNode) { .addAllSelection( executionContext .getExpressionContext() - .getSourceToMetricExpressionMap() + .getSourceToMetricAggregationExpressionMap() .get(source)) .setFilter(filter) .build(); diff --git a/gateway-service-impl/src/test/java/org/hypertrace/gateway/service/common/ExpressionContextTest.java b/gateway-service-impl/src/test/java/org/hypertrace/gateway/service/common/ExpressionContextTest.java index 7752568a..34dae5f5 100644 --- a/gateway-service-impl/src/test/java/org/hypertrace/gateway/service/common/ExpressionContextTest.java +++ b/gateway-service-impl/src/test/java/org/hypertrace/gateway/service/common/ExpressionContextTest.java @@ -31,12 +31,7 @@ void testGetSingleSourceForAllAttributes_allSourceExpressionMapKeySetsHaveOneSou createSourceToExpressionsMap(List.of("QS")), createSourceToExpressionsMap(List.of("QS")), createSourceToExpressionsMap(List.of("QS")), - createSourceToExpressionsMap(List.of("QS")), - Map.of( - "API.id", Set.of(QS.name()), - "API.name", Set.of(QS.name()), - "API.duration", Set.of(QS.name()), - "API.startTime", Set.of(QS.name()))); + createSourceToExpressionsMap(List.of("QS"))); assertEquals( Optional.of("QS"), ExpressionContext.getSingleSourceForAllAttributes(expressionContext)); @@ -51,12 +46,7 @@ void testGetSingleSourceForAllAttributes_someSourceExpressionMapKeySetsHaveMulti createSourceToExpressionsMap(List.of("QS", "EDS")), createSourceToExpressionsMap(List.of("QS")), createSourceToExpressionsMap(List.of("QS")), - createSourceToExpressionsMap(List.of("QS")), - Map.of( - "API.id", Set.of(QS.name()), - "API.name", Set.of(QS.name()), - "API.duration", Set.of(QS.name(), EDS.name()), - "API.startTime", Set.of(QS.name()))); + createSourceToExpressionsMap(List.of("QS"))); assertEquals( Optional.of("QS"), ExpressionContext.getSingleSourceForAllAttributes(expressionContext)); @@ -71,12 +61,7 @@ void testGetSingleSourceForAllAttributes_someSourceExpressionMapKeySetsHaveDiffe createSourceToExpressionsMap(List.of("EDS")), createSourceToExpressionsMap(List.of("QS")), createSourceToExpressionsMap(List.of("QS")), - createSourceToExpressionsMap(List.of("QS")), - Map.of( - "API.id", Set.of(QS.name()), - "API.name", Set.of(QS.name()), - "API.duration", Set.of(EDS.name()), - "API.startTime", Set.of(QS.name()))); + createSourceToExpressionsMap(List.of("QS"))); assertEquals( Optional.empty(), ExpressionContext.getSingleSourceForAllAttributes(expressionContext)); @@ -92,12 +77,7 @@ void testGetSingleSourceForAllAttributes_someSourceExpressionMapKeySetsAreEmpty( createSourceToExpressionsMap(List.of()), createSourceToExpressionsMap(List.of("QS")), createSourceToExpressionsMap(List.of("QS")), - createSourceToExpressionsMap(List.of()), - Map.of( - "API.id", Set.of(QS.name()), - "API.name", Set.of(QS.name()), - "API.duration", Set.of(), - "API.startTime", Set.of(QS.name()))); + createSourceToExpressionsMap(List.of())); assertEquals( Optional.of("QS"), ExpressionContext.getSingleSourceForAllAttributes(expressionContext)); @@ -113,12 +93,7 @@ void testGetSingleSourceForAllAttributes_allEmptySourceExpressionMapKeySetsAndAt createSourceToExpressionsMap(List.of()), createSourceToExpressionsMap(List.of()), createSourceToExpressionsMap(List.of()), - createSourceToExpressionsMap(List.of()), - Map.of( - "API.id", Set.of(), - "API.name", Set.of(), - "API.duration", Set.of(), - "API.startTime", Set.of())); + createSourceToExpressionsMap(List.of())); assertEquals( Optional.empty(), ExpressionContext.getSingleSourceForAllAttributes(expressionContext)); @@ -309,12 +284,11 @@ private ExpressionContext getMockExpressionContext( Map> sourceToTimeAggregationMap, Map> sourceToSelectionOrderByExpressionMap, Map> sourceToMetricOrderByExpressionMap, - Map> sourceToFilterExpressionMap, - Map> attributeToSourcesMap) { + Map> sourceToFilterExpressionMap) { ExpressionContext expressionContext = mock(ExpressionContext.class); when(expressionContext.getSourceToSelectionExpressionMap()) .thenReturn(sourceToSelectionExpressionMap); - when(expressionContext.getSourceToMetricExpressionMap()) + when(expressionContext.getSourceToMetricAggregationExpressionMap()) .thenReturn(sourceToMetricExpressionMap); when(expressionContext.getSourceToTimeAggregationMap()).thenReturn(sourceToTimeAggregationMap); when(expressionContext.getSourceToSelectionOrderByExpressionMap()) @@ -323,7 +297,6 @@ private ExpressionContext getMockExpressionContext( .thenReturn(sourceToMetricOrderByExpressionMap); when(expressionContext.getSourceToFilterExpressionMap()) .thenReturn(sourceToFilterExpressionMap); - when(expressionContext.getAllAttributesToSourcesMap()).thenReturn(attributeToSourcesMap); return expressionContext; } diff --git a/gateway-service-impl/src/test/java/org/hypertrace/gateway/service/common/transformer/ResponsePostProcessorTest.java b/gateway-service-impl/src/test/java/org/hypertrace/gateway/service/common/transformer/ResponsePostProcessorTest.java index cbf50c70..c7e06395 100644 --- a/gateway-service-impl/src/test/java/org/hypertrace/gateway/service/common/transformer/ResponsePostProcessorTest.java +++ b/gateway-service-impl/src/test/java/org/hypertrace/gateway/service/common/transformer/ResponsePostProcessorTest.java @@ -39,7 +39,7 @@ public void shouldAddMissingSelections() { List.of(createAttributeExpression("API.id"), createAttributeExpression("API.name")), "QS", List.of(createAttributeExpression("API.serviceId")))); - when(expressionContext.getSourceToMetricExpressionMap()).thenReturn(Collections.emptyMap()); + when(expressionContext.getSourceToMetricAggregationExpressionMap()).thenReturn(Collections.emptyMap()); when(expressionContext.getSourceToTimeAggregationMap()).thenReturn(Collections.emptyMap()); EntityExecutionContext executionContext = mock(EntityExecutionContext.class); @@ -81,7 +81,7 @@ public void shouldAddMissingSelections() { public void shouldAddMissingAggregations() { ExpressionContext expressionContext = mock(ExpressionContext.class); when(expressionContext.getSourceToSelectionExpressionMap()).thenReturn(Collections.emptyMap()); - when(expressionContext.getSourceToMetricExpressionMap()) + when(expressionContext.getSourceToMetricAggregationExpressionMap()) .thenReturn(Map.of("QS", List.of(createAggregateExpression("API.duration")))); when(expressionContext.getSourceToTimeAggregationMap()).thenReturn(Collections.emptyMap()); @@ -119,7 +119,7 @@ public void shouldAddMissingAggregations() { public void shouldAddMissingTimeAggregations() { ExpressionContext expressionContext = mock(ExpressionContext.class); when(expressionContext.getSourceToSelectionExpressionMap()).thenReturn(Collections.emptyMap()); - when(expressionContext.getSourceToMetricExpressionMap()).thenReturn(Collections.emptyMap()); + when(expressionContext.getSourceToMetricAggregationExpressionMap()).thenReturn(Collections.emptyMap()); when(expressionContext.getSourceToTimeAggregationMap()) .thenReturn(Map.of("QS", List.of(createTimeAggregation("API.duration")))); diff --git a/gateway-service-impl/src/test/java/org/hypertrace/gateway/service/entity/query/ExecutionTreeUtilsTest.java b/gateway-service-impl/src/test/java/org/hypertrace/gateway/service/entity/query/ExecutionTreeUtilsTest.java index a3341a91..48035ae1 100644 --- a/gateway-service-impl/src/test/java/org/hypertrace/gateway/service/entity/query/ExecutionTreeUtilsTest.java +++ b/gateway-service-impl/src/test/java/org/hypertrace/gateway/service/entity/query/ExecutionTreeUtilsTest.java @@ -8,14 +8,9 @@ import static org.mockito.Mockito.when; import java.util.Collections; -import java.util.List; import java.util.Map; import java.util.Set; -import java.util.stream.Collectors; import org.hypertrace.gateway.service.common.ExpressionContext; -import org.hypertrace.gateway.service.v1.common.Expression; -import org.hypertrace.gateway.service.v1.common.OrderByExpression; -import org.hypertrace.gateway.service.v1.common.TimeAggregation; import org.junit.jupiter.api.Test; class ExecutionTreeUtilsTest { @@ -54,34 +49,4 @@ void removeDuplicateSelectionAttributes() { verify(executionContext).removeSelectionAttributes("EDS", Set.of("API.name")); verify(executionContext).removeSelectionAttributes("AS", Set.of("API.latency")); } - - private ExpressionContext getMockExpressionContext( - Map> sourceToSelectionExpressionMap, - Map> sourceToMetricExpressionMap, - Map> sourceToTimeAggregationMap, - Map> sourceToSelectionOrderByExpressionMap, - Map> sourceToMetricOrderByExpressionMap, - Map> sourceToFilterExpressionMap, - Map> attributeToSourcesMap) { - ExpressionContext expressionContext = mock(ExpressionContext.class); - when(expressionContext.getSourceToSelectionExpressionMap()) - .thenReturn(sourceToSelectionExpressionMap); - when(expressionContext.getSourceToMetricExpressionMap()) - .thenReturn(sourceToMetricExpressionMap); - when(expressionContext.getSourceToTimeAggregationMap()).thenReturn(sourceToTimeAggregationMap); - when(expressionContext.getSourceToSelectionOrderByExpressionMap()) - .thenReturn(sourceToSelectionOrderByExpressionMap); - when(expressionContext.getSourceToMetricOrderByExpressionMap()) - .thenReturn(sourceToMetricOrderByExpressionMap); - when(expressionContext.getSourceToFilterExpressionMap()) - .thenReturn(sourceToFilterExpressionMap); - when(expressionContext.getAllAttributesToSourcesMap()).thenReturn(attributeToSourcesMap); - - return expressionContext; - } - - // We don't care about the values. Just they keySets. - private Map> createSourceToExpressionsMap(List sourceKeys) { - return sourceKeys.stream().collect(Collectors.toUnmodifiableMap(s -> s, s -> List.of())); - } } diff --git a/gateway-service-impl/src/test/java/org/hypertrace/gateway/service/entity/query/visitor/ExecutionVisitorTest.java b/gateway-service-impl/src/test/java/org/hypertrace/gateway/service/entity/query/visitor/ExecutionVisitorTest.java index eba9a0f1..9964fcaa 100644 --- a/gateway-service-impl/src/test/java/org/hypertrace/gateway/service/entity/query/visitor/ExecutionVisitorTest.java +++ b/gateway-service-impl/src/test/java/org/hypertrace/gateway/service/entity/query/visitor/ExecutionVisitorTest.java @@ -684,7 +684,7 @@ public void test_visitPaginateOnlyNode() { .thenReturn(List.of(buildExpression(API_ID_ATTR))); when(expressionContext.getSourceToSelectionExpressionMap()) .thenReturn(Map.of("QS", List.of(selectionExpression))); - when(expressionContext.getSourceToMetricExpressionMap()) + when(expressionContext.getSourceToMetricAggregationExpressionMap()) .thenReturn(Map.of("QS", List.of(metricExpression))); when(expressionContext.getSourceToTimeAggregationMap()) .thenReturn(Map.of("QS", List.of(timeAggregation))); @@ -854,7 +854,7 @@ public void test_visitOnlySelectionsNode_shouldSetTotalEntityKeys() { .thenReturn(List.of(buildExpression(API_ID_ATTR))); when(expressionContext.getSourceToSelectionExpressionMap()) .thenReturn(Map.of("QS", List.of(selectionExpression))); - when(expressionContext.getSourceToMetricExpressionMap()) + when(expressionContext.getSourceToMetricAggregationExpressionMap()) .thenReturn(Map.of("QS", List.of(metricExpression))); when(expressionContext.getSourceToTimeAggregationMap()) .thenReturn(Map.of("QS", List.of(timeAggregation))); @@ -986,7 +986,7 @@ private EntityExecutionContext mockExecutionContext( when(executionContext.getPendingSelectionSources()).thenReturn(selectionSource); when(executionContext.getPendingMetricAggregationSources()).thenReturn(aggregateSource); when(expressionContext.getSourceToSelectionExpressionMap()).thenReturn(sourceToSelectionMap); - when(expressionContext.getSourceToMetricExpressionMap()).thenReturn(sourceToAggregateMap); + when(expressionContext.getSourceToMetricAggregationExpressionMap()).thenReturn(sourceToAggregateMap); return executionContext; }