This repository has been archived by the owner on Jun 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
chore: use filter in entities query for selections as well #201
Merged
Merged
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
211038d
chore: use filter in entities query in selections as well
9a97d11
fix test failure
7682c66
added filters for selection for time aggregated queries as well
9f12153
minor cleanup
8e82c04
address review comments
9eae09e
address review comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,25 @@ | ||
package org.hypertrace.gateway.service.entity.query; | ||
|
||
import static java.util.Collections.emptyList; | ||
import static java.util.Collections.emptyMap; | ||
import static java.util.Collections.unmodifiableList; | ||
import static org.hypertrace.core.attribute.service.v1.AttributeSource.EDS; | ||
import static org.hypertrace.core.attribute.service.v1.AttributeSource.QS; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Map.Entry; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
import org.hypertrace.core.attribute.service.v1.AttributeMetadata; | ||
import org.hypertrace.core.attribute.service.v1.AttributeSource; | ||
import org.hypertrace.gateway.service.common.ExpressionContext; | ||
import org.hypertrace.gateway.service.common.util.ExpressionReader; | ||
import org.hypertrace.gateway.service.common.util.TimeRangeFilterUtil; | ||
import org.hypertrace.gateway.service.entity.query.visitor.ExecutionContextBuilderVisitor; | ||
import org.hypertrace.gateway.service.entity.query.visitor.FilterOptimizingVisitor; | ||
import org.hypertrace.gateway.service.entity.query.visitor.PrintVisitor; | ||
import org.hypertrace.gateway.service.v1.common.Expression; | ||
import org.hypertrace.gateway.service.v1.common.Filter; | ||
import org.hypertrace.gateway.service.v1.common.Operator; | ||
import org.hypertrace.gateway.service.v1.common.OrderByExpression; | ||
|
@@ -39,18 +32,11 @@ public class ExecutionTreeBuilder { | |
|
||
private static final Logger LOG = LoggerFactory.getLogger(ExecutionTreeBuilder.class); | ||
|
||
private final Map<String, AttributeMetadata> attributeMetadataMap; | ||
private final EntityExecutionContext executionContext; | ||
private final Set<String> sourceSetsIfFilterAndOrderByAreFromSameSourceSets; | ||
|
||
public ExecutionTreeBuilder(EntityExecutionContext executionContext) { | ||
this.executionContext = executionContext; | ||
this.attributeMetadataMap = | ||
executionContext | ||
.getAttributeMetadataProvider() | ||
.getAttributesMetadata( | ||
executionContext.getEntitiesRequestContext(), | ||
executionContext.getEntitiesRequest().getEntityType()); | ||
|
||
this.sourceSetsIfFilterAndOrderByAreFromSameSourceSets = | ||
ExpressionContext.getSourceSetsIfFilterAndOrderByAreFromSameSourceSets( | ||
|
@@ -132,7 +118,7 @@ public QueryNode build() { | |
|
||
ExecutionTreeUtils.removeDuplicateSelectionAttributes(executionContext, QS.name()); | ||
|
||
QueryNode filterTree = buildFilterTree(executionContext, entitiesRequest.getFilter()); | ||
QueryNode filterTree = buildFilterTreeNode(executionContext, entitiesRequest.getFilter()); | ||
if (LOG.isDebugEnabled()) { | ||
LOG.debug("Filter Tree:{}", filterTree.acceptVisitor(new PrintVisitor())); | ||
} | ||
|
@@ -215,6 +201,8 @@ private QueryNode buildExecutionTreeForEdsFilterAndSelection() { | |
@VisibleForTesting | ||
QueryNode buildExecutionTree(EntityExecutionContext executionContext, QueryNode filterTree) { | ||
QueryNode rootNode = filterTree; | ||
// set up source filter to be used during selections | ||
// filterTree.acceptVisitor(new SourceFilterVisitor(executionContext)); | ||
// Select attributes from sources in order by but not part of the filter tree | ||
Set<String> attrSourcesForOrderBy = executionContext.getPendingSelectionSourcesForOrderBy(); | ||
if (!attrSourcesForOrderBy.isEmpty()) { | ||
|
@@ -268,8 +256,7 @@ QueryNode buildExecutionTree(EntityExecutionContext executionContext, QueryNode | |
return rootNode; | ||
} | ||
|
||
@VisibleForTesting | ||
QueryNode buildFilterTree(EntityExecutionContext context, Filter filter) { | ||
QueryNode buildFilterTreeNode(EntityExecutionContext context, Filter filter) { | ||
EntitiesRequest entitiesRequest = executionContext.getEntitiesRequest(); | ||
// Convert the time range into a filter and set it on the request so that all downstream | ||
// components needn't treat it specially | ||
|
@@ -281,29 +268,29 @@ QueryNode buildFilterTree(EntityExecutionContext context, Filter filter) { | |
entitiesRequest.getEndTimeMillis()); | ||
|
||
boolean isAndFilter = executionContext.getExpressionContext().isAndFilter(); | ||
return isAndFilter | ||
? buildAndFilterTree(entitiesRequest) | ||
: buildFilterTree(entitiesRequest, timeRangeFilter); | ||
return isAndFilter ? buildAndFilterTree(context) : buildFilterTree(context, timeRangeFilter); | ||
} | ||
|
||
@VisibleForTesting | ||
QueryNode buildFilterTree(EntitiesRequest entitiesRequest, Filter filter) { | ||
QueryNode buildFilterTree(EntityExecutionContext context, Filter filter) { | ||
EntitiesRequest entitiesRequest = context.getEntitiesRequest(); | ||
if (filter.equals(Filter.getDefaultInstance())) { | ||
return new NoOpNode(); | ||
} | ||
Operator operator = filter.getOperator(); | ||
if (operator == Operator.AND) { | ||
return new AndNode( | ||
filter.getChildFilterList().stream() | ||
.map(childFilter -> buildFilterTree(entitiesRequest, childFilter)) | ||
.map(childFilter -> buildFilterTree(context, childFilter)) | ||
.collect(Collectors.toList())); | ||
} else if (operator == Operator.OR) { | ||
return new OrNode( | ||
filter.getChildFilterList().stream() | ||
.map(childFilter -> buildFilterTree(entitiesRequest, childFilter)) | ||
.map(childFilter -> buildFilterTree(context, childFilter)) | ||
.collect(Collectors.toList())); | ||
} else { | ||
List<AttributeSource> sources = getAttributeSources(filter.getLhs()); | ||
List<AttributeSource> sources = | ||
context.getExpressionContext().getAttributeSources(filter.getLhs()); | ||
// if the filter by and order by are from QS, pagination can be pushed down to QS | ||
|
||
// There will always be a DataFetcherNode for QS, because the results are always fetched | ||
|
@@ -319,7 +306,8 @@ QueryNode buildFilterTree(EntitiesRequest entitiesRequest, Filter filter) { | |
} | ||
|
||
// filters and order by on QS, but you can still have selection on EDS | ||
QueryNode buildAndFilterTree(EntitiesRequest entitiesRequest) { | ||
QueryNode buildAndFilterTree(EntityExecutionContext context) { | ||
EntitiesRequest entitiesRequest = context.getEntitiesRequest(); | ||
// If the filter by and order by are from QS (and selections are on other sources), pagination | ||
// can be pushed down to QS | ||
// Since the filter and order by are from QS, there won't be any filter on other | ||
|
@@ -330,7 +318,7 @@ QueryNode buildAndFilterTree(EntitiesRequest entitiesRequest) { | |
} | ||
|
||
Map<AttributeSource, Filter> sourceToAndFilterMap = | ||
new HashMap<>(buildSourceToAndFilterMap(entitiesRequest.getFilter())); | ||
Map.copyOf(context.getExpressionContext().getSourceToFilterMap()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. returns an unmodifiable map. The map is being modified on L328 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's correct. Moved it back to HashMap |
||
|
||
// qs node as the pivot node to fetch time range data | ||
QueryNode qsNode = | ||
|
@@ -387,37 +375,6 @@ QueryNode buildAndFilterTree(EntitiesRequest entitiesRequest) { | |
} | ||
} | ||
|
||
private Map<AttributeSource, Filter> buildSourceToAndFilterMap(Filter filter) { | ||
Operator operator = filter.getOperator(); | ||
if (operator == Operator.AND) { | ||
return filter.getChildFilterList().stream() | ||
.map(this::buildSourceToAndFilterMap) | ||
.flatMap(map -> map.entrySet().stream()) | ||
.collect( | ||
Collectors.toUnmodifiableMap( | ||
Entry::getKey, | ||
Entry::getValue, | ||
(value1, value2) -> | ||
Filter.newBuilder() | ||
.setOperator(Operator.AND) | ||
.addChildFilter(value1) | ||
.addChildFilter(value2) | ||
.build())); | ||
|
||
} else if (operator == Operator.OR) { | ||
return Collections.emptyMap(); | ||
} else { | ||
List<AttributeSource> attributeSources = getAttributeSources(filter.getLhs()); | ||
if (attributeSources.isEmpty()) { | ||
return emptyMap(); | ||
} | ||
|
||
return attributeSources.contains(QS) | ||
? Map.of(QS, filter) | ||
: Map.of(attributeSources.get(0), filter); | ||
} | ||
} | ||
|
||
private QueryNode checkAndAddSortAndPaginationNode( | ||
QueryNode childNode, EntityExecutionContext executionContext) { | ||
EntitiesRequest entitiesRequest = executionContext.getEntitiesRequest(); | ||
|
@@ -479,12 +436,4 @@ private QueryNode createQsDataFetcherNodeWithLimitAndOffset(EntitiesRequest enti | |
private QueryNode createPaginateOnlyNode(QueryNode queryNode, EntitiesRequest entitiesRequest) { | ||
return new PaginateOnlyNode(queryNode, entitiesRequest.getLimit(), entitiesRequest.getOffset()); | ||
} | ||
|
||
public List<AttributeSource> getAttributeSources(Expression expression) { | ||
Set<String> attributeIds = ExpressionReader.extractAttributeIds(expression); | ||
return attributeIds.stream() | ||
.map(attributeId -> attributeMetadataMap.get(attributeId).getSourcesList()) | ||
.flatMap(Collection::stream) | ||
.collect(Collectors.toUnmodifiableList()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed this commented section