From 93fdf172a73d66625ccf0b59dbdc2736cddb47ca Mon Sep 17 00:00:00 2001 From: RishabhB99 Date: Fri, 28 Jun 2024 11:41:52 +0530 Subject: [PATCH 1/3] fix : group by on functional expressions in mongo --- .../documentstore/DocStoreQueryV1Test.java | 50 +++++++++++++++++++ ...function_expression_group_by_response.json | 14 ++++++ .../core/documentstore/mongo/MongoUtils.java | 19 +++++++ .../mongo/query/MongoQueryExecutor.java | 4 +- .../parser/MongoFunctionExpressionParser.java | 3 +- .../MongoGroupTypeExpressionParser.java | 46 +++++++++++++++-- .../MongoSelectTypeExpressionParser.java | 15 ++++++ 7 files changed, 145 insertions(+), 6 deletions(-) create mode 100644 document-store/src/integrationTest/resources/query/function_expression_group_by_response.json diff --git a/document-store/src/integrationTest/java/org/hypertrace/core/documentstore/DocStoreQueryV1Test.java b/document-store/src/integrationTest/java/org/hypertrace/core/documentstore/DocStoreQueryV1Test.java index f684b118..5cdac2c4 100644 --- a/document-store/src/integrationTest/java/org/hypertrace/core/documentstore/DocStoreQueryV1Test.java +++ b/document-store/src/integrationTest/java/org/hypertrace/core/documentstore/DocStoreQueryV1Test.java @@ -14,8 +14,11 @@ import static org.hypertrace.core.documentstore.expression.operators.AggregationOperator.MAX; import static org.hypertrace.core.documentstore.expression.operators.AggregationOperator.MIN; import static org.hypertrace.core.documentstore.expression.operators.AggregationOperator.SUM; +import static org.hypertrace.core.documentstore.expression.operators.FunctionOperator.DIVIDE; +import static org.hypertrace.core.documentstore.expression.operators.FunctionOperator.FLOOR; import static org.hypertrace.core.documentstore.expression.operators.FunctionOperator.LENGTH; import static org.hypertrace.core.documentstore.expression.operators.FunctionOperator.MULTIPLY; +import static org.hypertrace.core.documentstore.expression.operators.FunctionOperator.SUBTRACT; import static org.hypertrace.core.documentstore.expression.operators.LogicalOperator.AND; import static org.hypertrace.core.documentstore.expression.operators.LogicalOperator.OR; import static org.hypertrace.core.documentstore.expression.operators.RelationalOperator.CONTAINS; @@ -87,6 +90,7 @@ import org.hypertrace.core.documentstore.model.options.UpdateOptions; import org.hypertrace.core.documentstore.model.subdoc.SubDocumentUpdate; import org.hypertrace.core.documentstore.model.subdoc.SubDocumentValue; +import org.hypertrace.core.documentstore.query.Aggregation; import org.hypertrace.core.documentstore.query.Filter; import org.hypertrace.core.documentstore.query.Pagination; import org.hypertrace.core.documentstore.query.Query; @@ -3312,6 +3316,52 @@ public void testNotExistsOperatorWithFindUsingBooleanRhs(String dataStoreName) t testCountApi(dataStoreName, query, "query/not_exists_filter_response.json"); } + @ParameterizedTest + @ArgumentsSource(MongoProvider.class) + public void testMongoFunctionExpressionGroupBy(String dataStoreName) throws Exception { + Collection collection = getCollection(dataStoreName); + + FunctionExpression functionExpression = + FunctionExpression.builder() + .operator(FLOOR) + .operand( + FunctionExpression.builder() + .operator(DIVIDE) + .operand( + FunctionExpression.builder() + .operator(SUBTRACT) + .operand(IdentifierExpression.of("price")) + .operand(ConstantExpression.of(5)) + .build()) + .operand(ConstantExpression.of(5)) + .build()) + .build(); + List selectionSpecs = + List.of( + SelectionSpec.of(functionExpression, "function"), + SelectionSpec.of( + AggregateExpression.of(COUNT, IdentifierExpression.of("function")), + "functionCount")); + Selection selection = Selection.builder().selectionSpecs(selectionSpecs).build(); + + Query query = + Query.builder() + .setSelection(selection) + .setAggregation( + Aggregation.builder().expression(IdentifierExpression.of("function")).build()) + .setSort( + Sort.builder() + .sortingSpec(SortingSpec.of(IdentifierExpression.of("function"), ASC)) + .build()) + .build(); + + Iterator resultDocs = collection.aggregate(query); + assertDocsAndSizeEqualWithoutOrder( + dataStoreName, resultDocs, "query/function_expression_group_by_response.json", 3); + + testCountApi(dataStoreName, query, "query/function_expression_group_by_response.json"); + } + private static Collection getCollection(final String dataStoreName) { return getCollection(dataStoreName, COLLECTION_NAME); } diff --git a/document-store/src/integrationTest/resources/query/function_expression_group_by_response.json b/document-store/src/integrationTest/resources/query/function_expression_group_by_response.json new file mode 100644 index 00000000..3d6e270d --- /dev/null +++ b/document-store/src/integrationTest/resources/query/function_expression_group_by_response.json @@ -0,0 +1,14 @@ +[ + { + "function": 0.0, + "functionCount": 4 + }, + { + "function": 1.0, + "functionCount": 2 + }, + { + "function": 3.0, + "functionCount": 2 + } +] \ No newline at end of file diff --git a/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/MongoUtils.java b/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/MongoUtils.java index 6efb08d9..7210e62f 100644 --- a/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/MongoUtils.java +++ b/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/MongoUtils.java @@ -26,11 +26,16 @@ import java.util.Optional; import java.util.Set; import java.util.function.UnaryOperator; +import java.util.stream.Collectors; import org.bson.json.JsonMode; import org.bson.json.JsonWriterSettings; import org.hypertrace.core.documentstore.Document; import org.hypertrace.core.documentstore.JSONDocument; +import org.hypertrace.core.documentstore.expression.impl.FunctionExpression; +import org.hypertrace.core.documentstore.expression.impl.IdentifierExpression; +import org.hypertrace.core.documentstore.expression.type.GroupTypeExpression; import org.hypertrace.core.documentstore.model.options.ReturnDocumentType; +import org.hypertrace.core.documentstore.query.SelectionSpec; public final class MongoUtils { public static final String FIELD_SEPARATOR = "."; @@ -145,6 +150,20 @@ public static ReturnDocument getReturnDocument(final ReturnDocumentType returnDo String.format("Unhandled return document type: %s", returnDocumentType))); } + public static boolean isFunctionExpressionSelectionWithGroupBy( + final SelectionSpec selectionSpec, final List groupByAliases) { + return selectionSpec.getAlias() != null + && groupByAliases.contains(selectionSpec.getAlias()) + && selectionSpec.getExpression().getClass().equals(FunctionExpression.class); + } + + public static List getGroupByAliases(final List expressions) { + return expressions.stream() + .filter(expression -> expression.getClass().equals(IdentifierExpression.class)) + .map(expression -> ((IdentifierExpression) expression).getName()) + .collect(Collectors.toUnmodifiableList()); + } + private static ObjectNode wrapInLiteral(final ObjectNode objectNode) { /* Wrapping the subDocument with $literal to be able to provide empty object "{}" as value * Throws error otherwise if empty object is provided as value. diff --git a/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/MongoQueryExecutor.java b/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/MongoQueryExecutor.java index c78fdf08..578039fb 100644 --- a/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/MongoQueryExecutor.java +++ b/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/MongoQueryExecutor.java @@ -12,7 +12,6 @@ import static org.hypertrace.core.documentstore.mongo.query.MongoPaginationHelper.getSkipClause; import static org.hypertrace.core.documentstore.mongo.query.parser.MongoFilterTypeExpressionParser.getFilter; import static org.hypertrace.core.documentstore.mongo.query.parser.MongoFilterTypeExpressionParser.getFilterClause; -import static org.hypertrace.core.documentstore.mongo.query.parser.MongoGroupTypeExpressionParser.getGroupClause; import static org.hypertrace.core.documentstore.mongo.query.parser.MongoNonProjectedSortTypeExpressionParser.getNonProjectedSortClause; import static org.hypertrace.core.documentstore.mongo.query.parser.MongoSelectTypeExpressionParser.getProjectClause; import static org.hypertrace.core.documentstore.mongo.query.parser.MongoSelectTypeExpressionParser.getSelections; @@ -41,6 +40,7 @@ import org.hypertrace.core.documentstore.model.config.ConnectionConfig; import org.hypertrace.core.documentstore.mongo.query.parser.AliasParser; import org.hypertrace.core.documentstore.mongo.query.parser.MongoFromTypeExpressionParser; +import org.hypertrace.core.documentstore.mongo.query.parser.MongoGroupTypeExpressionParser; import org.hypertrace.core.documentstore.mongo.query.transformer.MongoQueryTransformer; import org.hypertrace.core.documentstore.parser.AggregateExpressionChecker; import org.hypertrace.core.documentstore.parser.FunctionExpressionChecker; @@ -58,7 +58,7 @@ public class MongoQueryExecutor { List.of( query -> singleton(getFilterClause(query, Query::getFilter)), MongoFromTypeExpressionParser::getFromClauses, - query -> singleton(getGroupClause(query)), + MongoGroupTypeExpressionParser::getGroupClauses, query -> singleton(getProjectClause(query)), query -> singleton(getFilterClause(query, Query::getAggregationFilter)), query -> singleton(getSortClause(query)), diff --git a/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoFunctionExpressionParser.java b/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoFunctionExpressionParser.java index 6a807370..c4953886 100644 --- a/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoFunctionExpressionParser.java +++ b/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoFunctionExpressionParser.java @@ -62,7 +62,8 @@ Map parse(final FunctionExpression expression) { SelectTypeExpressionVisitor parser = new MongoIdentifierPrefixingParser( - new MongoIdentifierExpressionParser(new MongoConstantExpressionParser())); + new MongoIdentifierExpressionParser( + new MongoFunctionExpressionParser(new MongoConstantExpressionParser()))); if (numArgs == 1) { Object value = expression.getOperands().get(0).accept(parser); diff --git a/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoGroupTypeExpressionParser.java b/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoGroupTypeExpressionParser.java index 801bf470..6d207c48 100644 --- a/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoGroupTypeExpressionParser.java +++ b/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoGroupTypeExpressionParser.java @@ -3,12 +3,16 @@ import static org.hypertrace.core.documentstore.mongo.MongoCollection.ID_KEY; import static org.hypertrace.core.documentstore.mongo.MongoUtils.PREFIX; import static org.hypertrace.core.documentstore.mongo.MongoUtils.encodeKey; +import static org.hypertrace.core.documentstore.mongo.MongoUtils.getGroupByAliases; +import static org.hypertrace.core.documentstore.mongo.MongoUtils.isFunctionExpressionSelectionWithGroupBy; import com.mongodb.BasicDBObject; +import java.util.ArrayList; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.hypertrace.core.documentstore.expression.impl.FunctionExpression; @@ -22,6 +26,7 @@ public final class MongoGroupTypeExpressionParser implements GroupTypeExpressionVisitor { private static final String GROUP_CLAUSE = "$group"; + private static final String ADD_FIELDS_CLAUSE = "$addFields"; @SuppressWarnings("unchecked") @Override @@ -41,10 +46,32 @@ public Map visit(final IdentifierExpression expression) { return Map.of(key, PREFIX + identifier); } - public static BasicDBObject getGroupClause(final Query query) { + public static List getGroupClauses(final Query query) { final List selectionSpecs = query.getSelections(); final List expressions = query.getAggregations(); + final List basicDBObjects = new ArrayList<>(); + + final List functionExpressionSelectionWithGroupBys = + getFunctionExpressionSelectionWithGroupBys(selectionSpecs, expressions); + + if (!functionExpressionSelectionWithGroupBys.isEmpty()) { + MongoSelectTypeExpressionParser parser = + new MongoIdentifierPrefixingParser( + new MongoIdentifierExpressionParser(new MongoFunctionExpressionParser())); + Map addFields = + functionExpressionSelectionWithGroupBys.stream() + .map(spec -> MongoGroupTypeExpressionParser.parse(parser, spec)) + .reduce( + new LinkedHashMap<>(), + (first, second) -> { + first.putAll(second); + return first; + }); + + basicDBObjects.add(new BasicDBObject(ADD_FIELDS_CLAUSE, addFields)); + } + MongoGroupTypeExpressionParser parser = new MongoGroupTypeExpressionParser(); Map groupExp; @@ -82,11 +109,13 @@ public static BasicDBObject getGroupClause(final Query query) { }); if (MapUtils.isEmpty(definition) && CollectionUtils.isEmpty(expressions)) { - return new BasicDBObject(); + return basicDBObjects; } definition.putAll(groupExp); - return new BasicDBObject(GROUP_CLAUSE, definition); + + basicDBObjects.add(new BasicDBObject(GROUP_CLAUSE, definition)); + return basicDBObjects; } private static Map parse( @@ -99,4 +128,15 @@ private Map parse(final GroupTypeExpression expression) { MongoGroupTypeExpressionParser parser = new MongoGroupTypeExpressionParser(); return expression.accept(parser); } + + private static List getFunctionExpressionSelectionWithGroupBys( + final List selectionSpecs, final List expressions) { + List groupByAliases = getGroupByAliases(expressions); + + return selectionSpecs.stream() + .filter( + selectionSpec -> + isFunctionExpressionSelectionWithGroupBy(selectionSpec, groupByAliases)) + .collect(Collectors.toUnmodifiableList()); + } } diff --git a/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoSelectTypeExpressionParser.java b/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoSelectTypeExpressionParser.java index eedc1ed3..79b0380c 100644 --- a/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoSelectTypeExpressionParser.java +++ b/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoSelectTypeExpressionParser.java @@ -1,7 +1,11 @@ package org.hypertrace.core.documentstore.mongo.query.parser; import static java.util.stream.Collectors.toMap; +import static org.hypertrace.core.documentstore.mongo.MongoCollection.ID_KEY; +import static org.hypertrace.core.documentstore.mongo.MongoUtils.getGroupByAliases; +import static org.hypertrace.core.documentstore.mongo.MongoUtils.isFunctionExpressionSelectionWithGroupBy; +import com.google.common.base.Joiner; import com.mongodb.BasicDBObject; import java.util.List; import java.util.Map; @@ -20,6 +24,8 @@ public abstract class MongoSelectTypeExpressionParser implements SelectTypeExpre protected final MongoSelectTypeExpressionParser baseParser; + private static final Joiner DOT_JOINER = Joiner.on("."); + protected MongoSelectTypeExpressionParser() { this(MongoUnsupportedSelectTypeExpressionParser.INSTANCE); } @@ -59,8 +65,17 @@ public static BasicDBObject getSelections(final Query query) { new MongoIdentifierPrefixingParser( new MongoIdentifierExpressionParser(new MongoFunctionExpressionParser())); + List groupByAliases = getGroupByAliases(query.getAggregations()); + Map projectionMap = selectionSpecs.stream() + .map( + spec -> + isFunctionExpressionSelectionWithGroupBy(spec, groupByAliases) + ? SelectionSpec.of( + IdentifierExpression.of(DOT_JOINER.join(ID_KEY, spec.getAlias())), + spec.getAlias()) + : spec) .map(spec -> MongoSelectTypeExpressionParser.parse(parser, spec)) .flatMap(map -> map.entrySet().stream()) .collect( From 46737e152e7296d8229511227a12543e5cb3de18 Mon Sep 17 00:00:00 2001 From: RishabhB99 Date: Tue, 2 Jul 2024 11:33:26 +0530 Subject: [PATCH 2/3] addressed pr comments --- ...function_expression_group_by_response.json | 2 +- .../core/documentstore/mongo/MongoUtils.java | 19 ---------------- .../MongoGroupTypeExpressionParser.java | 22 +++++++++++++++++-- .../MongoSelectTypeExpressionParser.java | 4 ++-- .../parser/GroupByAliasGetter.java | 18 +++++++++++++++ 5 files changed, 41 insertions(+), 24 deletions(-) create mode 100644 document-store/src/main/java/org/hypertrace/core/documentstore/parser/GroupByAliasGetter.java diff --git a/document-store/src/integrationTest/resources/query/function_expression_group_by_response.json b/document-store/src/integrationTest/resources/query/function_expression_group_by_response.json index 3d6e270d..5adbf5a6 100644 --- a/document-store/src/integrationTest/resources/query/function_expression_group_by_response.json +++ b/document-store/src/integrationTest/resources/query/function_expression_group_by_response.json @@ -11,4 +11,4 @@ "function": 3.0, "functionCount": 2 } -] \ No newline at end of file +] diff --git a/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/MongoUtils.java b/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/MongoUtils.java index 7210e62f..6efb08d9 100644 --- a/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/MongoUtils.java +++ b/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/MongoUtils.java @@ -26,16 +26,11 @@ import java.util.Optional; import java.util.Set; import java.util.function.UnaryOperator; -import java.util.stream.Collectors; import org.bson.json.JsonMode; import org.bson.json.JsonWriterSettings; import org.hypertrace.core.documentstore.Document; import org.hypertrace.core.documentstore.JSONDocument; -import org.hypertrace.core.documentstore.expression.impl.FunctionExpression; -import org.hypertrace.core.documentstore.expression.impl.IdentifierExpression; -import org.hypertrace.core.documentstore.expression.type.GroupTypeExpression; import org.hypertrace.core.documentstore.model.options.ReturnDocumentType; -import org.hypertrace.core.documentstore.query.SelectionSpec; public final class MongoUtils { public static final String FIELD_SEPARATOR = "."; @@ -150,20 +145,6 @@ public static ReturnDocument getReturnDocument(final ReturnDocumentType returnDo String.format("Unhandled return document type: %s", returnDocumentType))); } - public static boolean isFunctionExpressionSelectionWithGroupBy( - final SelectionSpec selectionSpec, final List groupByAliases) { - return selectionSpec.getAlias() != null - && groupByAliases.contains(selectionSpec.getAlias()) - && selectionSpec.getExpression().getClass().equals(FunctionExpression.class); - } - - public static List getGroupByAliases(final List expressions) { - return expressions.stream() - .filter(expression -> expression.getClass().equals(IdentifierExpression.class)) - .map(expression -> ((IdentifierExpression) expression).getName()) - .collect(Collectors.toUnmodifiableList()); - } - private static ObjectNode wrapInLiteral(final ObjectNode objectNode) { /* Wrapping the subDocument with $literal to be able to provide empty object "{}" as value * Throws error otherwise if empty object is provided as value. diff --git a/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoGroupTypeExpressionParser.java b/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoGroupTypeExpressionParser.java index 6d207c48..d6c6f631 100644 --- a/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoGroupTypeExpressionParser.java +++ b/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoGroupTypeExpressionParser.java @@ -3,8 +3,6 @@ import static org.hypertrace.core.documentstore.mongo.MongoCollection.ID_KEY; import static org.hypertrace.core.documentstore.mongo.MongoUtils.PREFIX; import static org.hypertrace.core.documentstore.mongo.MongoUtils.encodeKey; -import static org.hypertrace.core.documentstore.mongo.MongoUtils.getGroupByAliases; -import static org.hypertrace.core.documentstore.mongo.MongoUtils.isFunctionExpressionSelectionWithGroupBy; import com.mongodb.BasicDBObject; import java.util.ArrayList; @@ -12,12 +10,15 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.stream.Collectors; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; import org.hypertrace.core.documentstore.expression.impl.FunctionExpression; import org.hypertrace.core.documentstore.expression.impl.IdentifierExpression; import org.hypertrace.core.documentstore.expression.type.GroupTypeExpression; +import org.hypertrace.core.documentstore.parser.FunctionExpressionChecker; +import org.hypertrace.core.documentstore.parser.GroupByAliasGetter; import org.hypertrace.core.documentstore.parser.GroupTypeExpressionVisitor; import org.hypertrace.core.documentstore.parser.SelectTypeExpressionVisitor; import org.hypertrace.core.documentstore.query.Query; @@ -27,6 +28,9 @@ public final class MongoGroupTypeExpressionParser implements GroupTypeExpression private static final String GROUP_CLAUSE = "$group"; private static final String ADD_FIELDS_CLAUSE = "$addFields"; + private static final FunctionExpressionChecker FUNCTION_EXPRESSION_CHECKER = + new FunctionExpressionChecker(); + private static final GroupByAliasGetter GROUP_BY_ALIAS_GETTER = new GroupByAliasGetter(); @SuppressWarnings("unchecked") @Override @@ -139,4 +143,18 @@ private static List getFunctionExpressionSelectionWithGroupBys( isFunctionExpressionSelectionWithGroupBy(selectionSpec, groupByAliases)) .collect(Collectors.toUnmodifiableList()); } + + public static boolean isFunctionExpressionSelectionWithGroupBy( + final SelectionSpec selectionSpec, final List groupByAliases) { + return selectionSpec.getAlias() != null + && groupByAliases.contains(selectionSpec.getAlias()) + && (Boolean) selectionSpec.getExpression().accept(FUNCTION_EXPRESSION_CHECKER); + } + + public static List getGroupByAliases(final List expressions) { + return expressions.stream() + .map(expression -> (String) expression.accept(GROUP_BY_ALIAS_GETTER)) + .filter(Objects::nonNull) + .collect(Collectors.toUnmodifiableList()); + } } diff --git a/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoSelectTypeExpressionParser.java b/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoSelectTypeExpressionParser.java index 79b0380c..3a561fc0 100644 --- a/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoSelectTypeExpressionParser.java +++ b/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoSelectTypeExpressionParser.java @@ -2,8 +2,8 @@ import static java.util.stream.Collectors.toMap; import static org.hypertrace.core.documentstore.mongo.MongoCollection.ID_KEY; -import static org.hypertrace.core.documentstore.mongo.MongoUtils.getGroupByAliases; -import static org.hypertrace.core.documentstore.mongo.MongoUtils.isFunctionExpressionSelectionWithGroupBy; +import static org.hypertrace.core.documentstore.mongo.query.parser.MongoGroupTypeExpressionParser.getGroupByAliases; +import static org.hypertrace.core.documentstore.mongo.query.parser.MongoGroupTypeExpressionParser.isFunctionExpressionSelectionWithGroupBy; import com.google.common.base.Joiner; import com.mongodb.BasicDBObject; diff --git a/document-store/src/main/java/org/hypertrace/core/documentstore/parser/GroupByAliasGetter.java b/document-store/src/main/java/org/hypertrace/core/documentstore/parser/GroupByAliasGetter.java new file mode 100644 index 00000000..0721d9be --- /dev/null +++ b/document-store/src/main/java/org/hypertrace/core/documentstore/parser/GroupByAliasGetter.java @@ -0,0 +1,18 @@ +package org.hypertrace.core.documentstore.parser; + +import org.hypertrace.core.documentstore.expression.impl.FunctionExpression; +import org.hypertrace.core.documentstore.expression.impl.IdentifierExpression; + +@SuppressWarnings("unchecked") +public class GroupByAliasGetter implements GroupTypeExpressionVisitor { + + @Override + public String visit(FunctionExpression expression) { + return null; + } + + @Override + public String visit(IdentifierExpression expression) { + return expression.getName(); + } +} From 948d214f68f4d0a1e24bf3218119b8dff7cb3ef5 Mon Sep 17 00:00:00 2001 From: RishabhB99 Date: Tue, 2 Jul 2024 15:55:22 +0530 Subject: [PATCH 3/3] addressed pr comment --- .../query/parser/MongoGroupTypeExpressionParser.java | 8 +++++--- .../core/documentstore/parser/GroupByAliasGetter.java | 9 +++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoGroupTypeExpressionParser.java b/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoGroupTypeExpressionParser.java index d6c6f631..520520ce 100644 --- a/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoGroupTypeExpressionParser.java +++ b/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoGroupTypeExpressionParser.java @@ -10,7 +10,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.Objects; +import java.util.Optional; import java.util.stream.Collectors; import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.MapUtils; @@ -151,10 +151,12 @@ public static boolean isFunctionExpressionSelectionWithGroupBy( && (Boolean) selectionSpec.getExpression().accept(FUNCTION_EXPRESSION_CHECKER); } + @SuppressWarnings("unchecked") public static List getGroupByAliases(final List expressions) { return expressions.stream() - .map(expression -> (String) expression.accept(GROUP_BY_ALIAS_GETTER)) - .filter(Objects::nonNull) + .map(expression -> (Optional) expression.accept(GROUP_BY_ALIAS_GETTER)) + .filter(Optional::isPresent) + .map(Optional::get) .collect(Collectors.toUnmodifiableList()); } } diff --git a/document-store/src/main/java/org/hypertrace/core/documentstore/parser/GroupByAliasGetter.java b/document-store/src/main/java/org/hypertrace/core/documentstore/parser/GroupByAliasGetter.java index 0721d9be..defb7e86 100644 --- a/document-store/src/main/java/org/hypertrace/core/documentstore/parser/GroupByAliasGetter.java +++ b/document-store/src/main/java/org/hypertrace/core/documentstore/parser/GroupByAliasGetter.java @@ -1,5 +1,6 @@ package org.hypertrace.core.documentstore.parser; +import java.util.Optional; import org.hypertrace.core.documentstore.expression.impl.FunctionExpression; import org.hypertrace.core.documentstore.expression.impl.IdentifierExpression; @@ -7,12 +8,12 @@ public class GroupByAliasGetter implements GroupTypeExpressionVisitor { @Override - public String visit(FunctionExpression expression) { - return null; + public Optional visit(FunctionExpression expression) { + return Optional.empty(); } @Override - public String visit(IdentifierExpression expression) { - return expression.getName(); + public Optional visit(IdentifierExpression expression) { + return Optional.of(expression.getName()); } }