From 50162faabb0efd2122b8e774ea47152b2dfe3b88 Mon Sep 17 00:00:00 2001 From: singh-viikram Date: Thu, 17 Oct 2024 15:41:29 +0530 Subject: [PATCH 1/4] Adding ast aggregation operator --- .../documentstore/expression/operators/AggregationOperator.java | 1 + .../mongo/query/parser/MongoAggregateExpressionParser.java | 2 ++ 2 files changed, 3 insertions(+) diff --git a/document-store/src/main/java/org/hypertrace/core/documentstore/expression/operators/AggregationOperator.java b/document-store/src/main/java/org/hypertrace/core/documentstore/expression/operators/AggregationOperator.java index f25b5fb7..6d5e8465 100644 --- a/document-store/src/main/java/org/hypertrace/core/documentstore/expression/operators/AggregationOperator.java +++ b/document-store/src/main/java/org/hypertrace/core/documentstore/expression/operators/AggregationOperator.java @@ -11,4 +11,5 @@ public enum AggregationOperator { SUM, MIN, MAX, + LAST, } diff --git a/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoAggregateExpressionParser.java b/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoAggregateExpressionParser.java index f25d4597..e4ce0d0c 100644 --- a/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoAggregateExpressionParser.java +++ b/document-store/src/main/java/org/hypertrace/core/documentstore/mongo/query/parser/MongoAggregateExpressionParser.java @@ -5,6 +5,7 @@ import static org.hypertrace.core.documentstore.expression.operators.AggregationOperator.COUNT; import static org.hypertrace.core.documentstore.expression.operators.AggregationOperator.DISTINCT; import static org.hypertrace.core.documentstore.expression.operators.AggregationOperator.DISTINCT_ARRAY; +import static org.hypertrace.core.documentstore.expression.operators.AggregationOperator.LAST; 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; @@ -30,6 +31,7 @@ final class MongoAggregateExpressionParser extends MongoSelectTypeExpressionPars put(MIN, "$min"); put(MAX, "$max"); put(COUNT, "$push"); + put(LAST, "$last"); } }); From 54983c24ab6d2fc6017fb58dc33dc19dd51cca06 Mon Sep 17 00:00:00 2001 From: singh-viikram Date: Fri, 18 Oct 2024 01:23:13 +0530 Subject: [PATCH 2/4] added integration tests --- .../documentstore/DocStoreQueryV1Test.java | 19 +++++++++++++++++++ .../query/test_last_aggregation_operator.json | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+) create mode 100644 document-store/src/integrationTest/resources/query/test_last_aggregation_operator.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 8f580d08..d3b4757f 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 @@ -11,6 +11,7 @@ import static org.hypertrace.core.documentstore.expression.operators.AggregationOperator.COUNT; import static org.hypertrace.core.documentstore.expression.operators.AggregationOperator.DISTINCT_ARRAY; import static org.hypertrace.core.documentstore.expression.operators.AggregationOperator.DISTINCT_COUNT; +import static org.hypertrace.core.documentstore.expression.operators.AggregationOperator.LAST; 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; @@ -1334,6 +1335,24 @@ public void testQueryV1AggregationFilterWithWhereClause(String dataStoreName) th dataStoreName, resultDocs, "query/test_aggr_filter_and_where_filter_result.json", 2); } + @ParameterizedTest + @ArgumentsSource(AllProvider.class) + public void testQueryV1LastAggregationOperator(String dataStoreName) throws IOException { + Collection collection = getCollection(dataStoreName); + + org.hypertrace.core.documentstore.query.Query query = + org.hypertrace.core.documentstore.query.Query.builder() + .addSelection(IdentifierExpression.of("item")) + .addAggregation(IdentifierExpression.of("item")) + .addSelection( + AggregateExpression.of(LAST, IdentifierExpression.of("price")), "last_price") + .build(); + + Iterator resultDocs = collection.aggregate(query); + assertDocsAndSizeEqualWithoutOrder( + dataStoreName, resultDocs, "query/test_last_aggregation_operator.json", 4); + } + @Nested class StartsWithOperatorTest { diff --git a/document-store/src/integrationTest/resources/query/test_last_aggregation_operator.json b/document-store/src/integrationTest/resources/query/test_last_aggregation_operator.json new file mode 100644 index 00000000..91d2bc3f --- /dev/null +++ b/document-store/src/integrationTest/resources/query/test_last_aggregation_operator.json @@ -0,0 +1,18 @@ +[ + { + "item":"Shampoo", + "last_price":5 + }, + { + "item":"Comb", + "last_price":7.5 + }, + { + "item":"Mirror", + "last_price":20 + }, + { + "item":"Soap", + "last_price":20 + } +] \ No newline at end of file From 6e0d7b3dbf30bade12bf8dd8ff685bb1ef680c22 Mon Sep 17 00:00:00 2001 From: singh-viikram Date: Fri, 18 Oct 2024 15:11:35 +0530 Subject: [PATCH 3/4] resolved comments --- .../core/documentstore/DocStoreQueryV1Test.java | 2 +- .../resources/query/collection_data.json | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) 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 d3b4757f..01e5d1b9 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 @@ -1287,7 +1287,7 @@ public void testQueryV1AggregationWithInFilterWithPrimitiveLhs(final String data } @ParameterizedTest - @ArgumentsSource(AllProvider.class) + @ArgumentsSource(MongoProvider.class) public void testQueryV1AggregationWithInFilterWithArrayLhs(final String dataStoreName) throws IOException { final Collection collection = getCollection(dataStoreName); diff --git a/document-store/src/integrationTest/resources/query/collection_data.json b/document-store/src/integrationTest/resources/query/collection_data.json index bffb0a79..3a389f38 100644 --- a/document-store/src/integrationTest/resources/query/collection_data.json +++ b/document-store/src/integrationTest/resources/query/collection_data.json @@ -6,7 +6,10 @@ "quantity": 2, "date": "2014-03-01T08:00:00Z", "props": { - "colors": ["Blue", "Green"], + "colors": [ + "Blue", + "Green" + ], "brand": "Dettol", "size": "M", "seller": { @@ -70,7 +73,9 @@ "quantity": 10, "date": "2014-03-15T09:00:00Z", "props": { - "colors": ["Black"], + "colors": [ + "Black" + ], "brand": "Sunsilk", "size": "L", "seller": { @@ -133,7 +138,10 @@ "quantity": 5, "date": "2014-04-04T21:23:13.331Z", "props": { - "colors": ["Orange", "Blue"], + "colors": [ + "Orange", + "Blue" + ], "brand": "Lifebuoy", "size": "S", "seller": { @@ -176,4 +184,4 @@ "quantity": 5, "date": "2016-02-06T20:20:13Z" } -] \ No newline at end of file +] From bff2d9c71da9b93e3761ba360529e24ee729dc81 Mon Sep 17 00:00:00 2001 From: singh-viikram Date: Fri, 18 Oct 2024 16:30:36 +0530 Subject: [PATCH 4/4] minor fix --- .../hypertrace/core/documentstore/DocStoreQueryV1Test.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 01e5d1b9..f2527bb0 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 @@ -1287,7 +1287,7 @@ public void testQueryV1AggregationWithInFilterWithPrimitiveLhs(final String data } @ParameterizedTest - @ArgumentsSource(MongoProvider.class) + @ArgumentsSource(AllProvider.class) public void testQueryV1AggregationWithInFilterWithArrayLhs(final String dataStoreName) throws IOException { final Collection collection = getCollection(dataStoreName); @@ -1336,7 +1336,7 @@ public void testQueryV1AggregationFilterWithWhereClause(String dataStoreName) th } @ParameterizedTest - @ArgumentsSource(AllProvider.class) + @ArgumentsSource(MongoProvider.class) public void testQueryV1LastAggregationOperator(String dataStoreName) throws IOException { Collection collection = getCollection(dataStoreName);