Skip to content

Commit ed0aa06

Browse files
committed
Added testFlatCollectionArrayAnyOnJsonbArray
1 parent 27b3d33 commit ed0aa06

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

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

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4018,6 +4018,34 @@ void testFlatCollectionUnnestJsonbArray(String dataStoreName) throws IOException
40184018
// 2 from Lifebuoy (Orange, Blue)
40194019
assertEquals(5, count, "Should find 5 color entries after unnesting JSONB arrays");
40204020
}
4021+
4022+
@ParameterizedTest
4023+
@ArgumentsSource(PostgresProvider.class)
4024+
void testFlatCollectionArrayAnyOnJsonbArray(String dataStoreName) {
4025+
Datastore datastore = datastoreMap.get(dataStoreName);
4026+
Collection flatCollection =
4027+
datastore.getCollectionForType(FLAT_COLLECTION_NAME, DocumentType.FLAT);
4028+
4029+
// Test ArrayRelationalFilterExpression.ANY on JSONB array (props.colors)
4030+
// This uses jsonb_array_elements() internally
4031+
Query jsonbArrayQuery =
4032+
Query.builder()
4033+
.addSelection(IdentifierExpression.of("item"))
4034+
.setFilter(
4035+
ArrayRelationalFilterExpression.builder()
4036+
.operator(ArrayOperator.ANY)
4037+
.filter(
4038+
RelationalExpression.of(
4039+
JsonIdentifierExpression.of("props", "colors"),
4040+
EQ,
4041+
ConstantExpression.of("Blue")))
4042+
.build())
4043+
.build();
4044+
4045+
long count = flatCollection.count(jsonbArrayQuery);
4046+
// ids 1 and 5 have "Blue" in their colors array
4047+
assertEquals(2, count, "Should find 2 items with 'Blue' color (ids 1, 5)");
4048+
}
40214049
}
40224050

40234051
@Nested

document-store/src/main/java/org/hypertrace/core/documentstore/postgres/query/v1/vistors/PostgresFromTypeExpressionVisitor.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public String visit(UnnestExpression unnestExpression) {
8787
String.format(
8888
UNWIND_EXP_ALIAS_FMT,
8989
nextIndex,
90-
shouldQuoteColName(isFlatCollection && !isJsonbArray, pgColumnName));
90+
getColName(isFlatCollection && !isJsonbArray, pgColumnName));
9191

9292
String fmt =
9393
unnestExpression.isPreserveNullAndEmptyArrays()
@@ -152,9 +152,7 @@ private static String prepareTable0Query(PostgresQueryParser postgresQueryParser
152152
/*
153153
Returns the column name with double quotes if the collection is flat to prevent folding to lower-case by PG
154154
*/
155-
private String shouldQuoteColName(boolean isFlatCollection, String pgColumnName) {
156-
return isFlatCollection
157-
? PostgresUtils.wrapFieldNamesWithDoubleQuotes(pgColumnName)
158-
: pgColumnName;
155+
private String getColName(boolean shouldQuote, String pgColumnName) {
156+
return shouldQuote ? PostgresUtils.wrapFieldNamesWithDoubleQuotes(pgColumnName) : pgColumnName;
159157
}
160158
}

0 commit comments

Comments
 (0)