Skip to content

Commit 11f5d28

Browse files
committed
test: add integration test to verify self join with sub-query works for nested fields as well
1 parent 9ee78fb commit 11f5d28

3 files changed

Lines changed: 162 additions & 0 deletions

File tree

document-store/src/integrationTest/java/org/hypertrace/core/documentstore/DocStoreQueryV1Test.java

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3568,6 +3568,95 @@ SELECT item, MAX(date) AS latest_date
35683568
dataStoreName, iterator, "query/self_join_with_sub_query_response.json", 4);
35693569
}
35703570

3571+
@ParameterizedTest
3572+
@ArgumentsSource(MongoProvider.class)
3573+
void testSelfJoinWithSubQueryWithNestedFields(String dataStoreName) throws IOException {
3574+
createCollectionData(
3575+
"query/items_data_with_nested_fields.json", "items_data_with_nested_fields");
3576+
Collection collection = getCollection(dataStoreName, "items_data_with_nested_fields");
3577+
3578+
/*
3579+
This is the query we want to execute:
3580+
SELECT itemDetails.item, itemDetails.quantity, itemDetails.date
3581+
FROM <implicit_collection>
3582+
JOIN (
3583+
SELECT itemDetails.item, MAX(itemDetails.date) AS latest_date
3584+
FROM <implicit_collection>
3585+
GROUP BY itemDetails.item
3586+
) latest
3587+
ON itemDetails.item = latest.itemDetails.item
3588+
AND itemDetails.date = latest.latest_date
3589+
ORDER BY `itemDetails.item` ASC;
3590+
*/
3591+
3592+
/*
3593+
The right subquery:
3594+
SELECT itemDetails.item, MAX(itemDetails.date) AS latest_date
3595+
FROM <implicit_collection>
3596+
GROUP BY itemDetails.item
3597+
*/
3598+
Query subQuery =
3599+
Query.builder()
3600+
.addSelection(SelectionSpec.of(IdentifierExpression.of("itemDetails.item")))
3601+
.addSelection(
3602+
SelectionSpec.of(
3603+
AggregateExpression.of(
3604+
AggregationOperator.MAX, IdentifierExpression.of("itemDetails.date")),
3605+
"latest_date"))
3606+
.addAggregation(IdentifierExpression.of("itemDetails.item"))
3607+
.build();
3608+
3609+
/*
3610+
The FROM expression representing a join with the right subquery:
3611+
FROM <implicit_collection>
3612+
JOIN (
3613+
SELECT itemDetails.item, MAX(itemDetails.date) AS latest_date
3614+
FROM <implicit_collection>
3615+
GROUP BY itemDetails.item
3616+
) latest
3617+
ON itemDetails.item = latest.itemDetails.item
3618+
AND itemDetails.date = latest.latest_date;
3619+
*/
3620+
SubQueryJoinExpression subQueryJoinExpression =
3621+
SubQueryJoinExpression.builder()
3622+
.subQuery(subQuery)
3623+
.subQueryAlias("latest")
3624+
.joinCondition(
3625+
LogicalExpression.and(
3626+
RelationalExpression.of(
3627+
IdentifierExpression.of("itemDetails.item"),
3628+
RelationalOperator.EQ,
3629+
AliasedIdentifierExpression.builder()
3630+
.name("itemDetails.item")
3631+
.contextAlias("latest")
3632+
.build()),
3633+
RelationalExpression.of(
3634+
IdentifierExpression.of("itemDetails.date"),
3635+
RelationalOperator.EQ,
3636+
AliasedIdentifierExpression.builder()
3637+
.name("latest_date")
3638+
.contextAlias("latest")
3639+
.build())))
3640+
.build();
3641+
3642+
/*
3643+
Now build the top-level Query:
3644+
SELECT itemDetails.item, itemDetails.quantity, itemDetails.date FROM <subQueryJoinExpression> ORDER BY `itemDetails.item` ASC;
3645+
*/
3646+
Query mainQuery =
3647+
Query.builder()
3648+
.addSelection(IdentifierExpression.of("itemDetails.item"))
3649+
.addSelection(IdentifierExpression.of("itemDetails.quantity"))
3650+
.addSelection(IdentifierExpression.of("itemDetails.date"))
3651+
.addFromClause(subQueryJoinExpression)
3652+
.addSort(IdentifierExpression.of("itemDetails.item"), ASC)
3653+
.build();
3654+
3655+
Iterator<Document> iterator = collection.aggregate(mainQuery);
3656+
assertDocsAndSizeEqual(
3657+
dataStoreName, iterator, "query/sub_query_join_response_with_nested_fields.json", 3);
3658+
}
3659+
35713660
private static Collection getCollection(final String dataStoreName) {
35723661
return getCollection(dataStoreName, COLLECTION_NAME);
35733662
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
[
2+
{
3+
"_id": 1,
4+
"itemDetails": {
5+
"item": "suite-1",
6+
"date": 1,
7+
"quantity": 10
8+
}
9+
},
10+
{
11+
"_id": 2,
12+
"itemDetails": {
13+
"item": "suite-2",
14+
"date": 1,
15+
"quantity": 10
16+
}
17+
},
18+
{
19+
"_id": 3,
20+
"itemDetails": {
21+
"item": "suite-2",
22+
"date": 2,
23+
"quantity": 20
24+
}
25+
},
26+
{
27+
"_id": 4,
28+
"itemDetails": {
29+
"item": "suite-2",
30+
"date": 3,
31+
"quantity": 30
32+
}
33+
},
34+
{
35+
"_id": 5,
36+
"itemDetails": {
37+
"item": "suite-3",
38+
"date": 2,
39+
"quantity": 20
40+
}
41+
},
42+
{
43+
"_id": 6,
44+
"itemDetails": {
45+
"item": "suite-3",
46+
"date": 1,
47+
"quantity": 10
48+
}
49+
}
50+
]
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[
2+
{
3+
"itemDetails": {
4+
"item": "suite-1",
5+
"date": 1,
6+
"quantity": 10
7+
}
8+
},
9+
{
10+
"itemDetails": {
11+
"item": "suite-2",
12+
"date": 3,
13+
"quantity": 30
14+
}
15+
},
16+
{
17+
"itemDetails": {
18+
"item": "suite-3",
19+
"date": 2,
20+
"quantity": 20
21+
}
22+
}
23+
]

0 commit comments

Comments
 (0)