Skip to content

Commit b8a79c1

Browse files
andiemontoyeahandiem-bq
andauthored
[AD-627] Add new method to get aggregate operations as strings (#295)
* [AD-627] Add new getter method in DocumentDbMqlQueryContext * Some formatting changes * Commit Code Coverage Badge * Rename test file * Commit Code Coverage Badge * Improvements from code review - move lambda to one line * Commit Code Coverage Badge * [AD-627] Add additional test cases * [AD-627] Add additional test cases * Commit Code Coverage Badge Co-authored-by: andiem-bq <[email protected]>
1 parent b7ef9c7 commit b8a79c1

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

calcite-adapter/src/main/java/software/amazon/documentdb/jdbc/query/DocumentDbMqlQueryContext.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
import lombok.Builder;
2020
import lombok.Getter;
2121
import org.bson.conversions.Bson;
22+
import org.bson.json.JsonMode;
23+
import org.bson.json.JsonWriterSettings;
2224
import software.amazon.documentdb.jdbc.common.utilities.JdbcColumnMetaData;
2325
import java.util.List;
26+
import java.util.stream.Collectors;
2427

2528
/**
26-
* This is meant to carry
27-
* all the information needed to execute the query in MongoDb and
29+
* This is meant to carry all the information needed to execute the query in DocumentDB and
2830
* construct a ResultSet.
2931
*/
3032
@Getter
@@ -38,4 +40,16 @@ public class DocumentDbMqlQueryContext {
3840
private final String collectionName;
3941
/** The path information for the output documents. Maps column names to field paths.*/
4042
private final List<String> paths;
43+
44+
/**
45+
* Gets the aggregation operations (stages) for the query as a list of strings.
46+
*
47+
* @return the aggregation operations as an ordered list of strings in extended JSON format.
48+
*/
49+
public List<String> getAggregateOperationsAsStrings() {
50+
return aggregateOperations.stream()
51+
.map(doc ->
52+
doc.toBsonDocument().toJson(JsonWriterSettings.builder().outputMode(JsonMode.EXTENDED).build()))
53+
.collect(Collectors.toList());
54+
}
4155
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright <2021> Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*
15+
*/
16+
17+
package software.amazon.documentdb.jdbc.query;
18+
19+
import org.bson.BsonDocument;
20+
import org.junit.jupiter.api.Assertions;
21+
import org.junit.jupiter.api.DisplayName;
22+
import org.junit.jupiter.api.Test;
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
import java.util.stream.Collectors;
26+
27+
public class DocumentDbMqlQueryContextTest {
28+
29+
@Test
30+
@DisplayName("Tests that aggregate operations are correctly converted from BSON document to string in getter.")
31+
void testGetAggregateOperationsAsStrings() {
32+
final List<String> stages = new ArrayList<>();
33+
// Generic stage
34+
stages.add(
35+
"{\"$unwind\": {"
36+
+ "\"path\": \"$array\", "
37+
+ "\"includeArrayIndex\": \"array_index_lvl_0\", "
38+
+ "\"preserveNullAndEmptyArrays\": true}}");
39+
// Stage with 3-valued logic (many null checks)
40+
stages.add(
41+
"{\"$project\": {"
42+
+ "\"booleanField\": "
43+
+ "{\"$cond\": [{\"$and\": [{\"$gt\": [\"$array.field\", null]}, "
44+
+ "{\"$gt\": [\"$array.field2\", null]}]}, "
45+
+ "{\"$eq\": [\"$array.field\", \"$array.field2\"]}, null]}}}");
46+
// Stage with different Bson types
47+
stages.add(
48+
"{\"$project\": {"
49+
+ "\"literalNull\": {\"$literal\": null}, "
50+
+ "\"literalTimestamp\": {\"$date\": {\"$numberLong\": \"1505938660000\"}}, "
51+
+ "\"literalInt\": {\"$literal\": {\"$numberInt\": \"-2147483648\"}}, "
52+
+ "\"literalDecimal\": {\"$literal\": {\"$numberDouble\": \"123.45\"}}, "
53+
+ "\"literalVarchar\": {\"$literal\": \"Hello! 你好!\"}, "
54+
+ "\"literalBinary\": {\"$binary\": {\"base64\": \"RfCr\", \"subType\": \"00\"}}}}");
55+
// Stage with a lot of nesting and aggregate operators
56+
stages.add(
57+
"{\"$project\": {\"EXPR$0\": {\"$substrCP\": [\"$array.field\", "
58+
+ "{\"$subtract\": [\"$array.field2\", {\"$numberInt\": \"1\"}]}, "
59+
+ "{\"$subtract\": [\"$array.field1\", \"$array.field2\"]}]}, "
60+
+ "\"_id\": {\"$numberInt\": \"0\"}}}");
61+
62+
final DocumentDbMqlQueryContext context =
63+
DocumentDbMqlQueryContext.builder()
64+
.aggregateOperations(
65+
stages.stream().map(BsonDocument::parse).collect(Collectors.toList()))
66+
.build();
67+
Assertions.assertEquals(stages.size(), context.getAggregateOperationsAsStrings().size());
68+
for (int i = 0; i < stages.size(); i++) {
69+
Assertions.assertEquals(stages.get(i), context.getAggregateOperationsAsStrings().get(i));
70+
}
71+
}
72+
}

0 commit comments

Comments
 (0)