-
Notifications
You must be signed in to change notification settings - Fork 14
APIST-1653 : Changes to enhance existing length op #312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
58cae6f
317cc25
3353873
6abda6d
ebdb64c
b7b7439
be9514a
bc24f4c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -919,6 +919,58 @@ public void testAggregateWithMultipleGroupingLevels(String dataStoreName) throws | |
| testCountApi(dataStoreName, query, "query/multi_level_grouping_response.json"); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ArgumentsSource(AllProvider.class) | ||
| public void testSortByListSizeWithMissingField(String dataStoreName) throws IOException { | ||
| Datastore datastore = datastoreMap.get(dataStoreName); | ||
| String collectionName = "list_size_sort_collection"; | ||
| datastore.deleteCollection(collectionName); | ||
| datastore.createCollection(collectionName, null); | ||
| Collection collection = datastore.getCollection(collectionName); | ||
|
Comment on lines
+924
to
+929
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch. Wrapped the test body in try-finally in be9514a so |
||
|
|
||
| try { | ||
| collection.upsert( | ||
| new SingleValueKey(TENANT_ID, "three"), | ||
| new JSONDocument("{\"item\":\"three\",\"tags\":[\"a\",\"b\",\"c\"]}")); | ||
| collection.upsert( | ||
| new SingleValueKey(TENANT_ID, "one"), | ||
| new JSONDocument("{\"item\":\"one\",\"tags\":[\"x\"]}")); | ||
| // Document intentionally missing the "tags" field; LENGTH must resolve to 0 instead of | ||
| // failing | ||
| collection.upsert( | ||
| new SingleValueKey(TENANT_ID, "none"), new JSONDocument("{\"item\":\"none\"}")); | ||
|
|
||
| Query query = | ||
| Query.builder() | ||
| .addSelection(IdentifierExpression.of("item")) | ||
| .addSelection( | ||
| FunctionExpression.builder() | ||
| .operator(LENGTH) | ||
| .operand(IdentifierExpression.of("tags")) | ||
| .build(), | ||
| "tag_count") | ||
| .addSort(IdentifierExpression.of("tag_count"), ASC) | ||
| .build(); | ||
|
|
||
| Iterator<Document> resultDocs = collection.aggregate(query); | ||
| List<Map<String, Object>> results = new ArrayList<>(); | ||
| while (resultDocs.hasNext()) { | ||
| results.add(Utils.convertDocumentToMap(resultDocs.next())); | ||
| } | ||
|
|
||
| assertEquals(3, results.size()); | ||
| // Document without the "tags" field counts as 0 and sorts first in ascending order | ||
| assertEquals("none", results.get(0).get("item")); | ||
| assertEquals(0, ((Number) results.get(0).get("tag_count")).intValue()); | ||
| assertEquals("one", results.get(1).get("item")); | ||
| assertEquals(1, ((Number) results.get(1).get("tag_count")).intValue()); | ||
| assertEquals("three", results.get(2).get("item")); | ||
| assertEquals(3, ((Number) results.get(2).get("tag_count")).intValue()); | ||
| } finally { | ||
| datastore.deleteCollection(collectionName); | ||
| } | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @ArgumentsSource(AllProvider.class) | ||
| public void testAggregateWithFunctionalLeftHandSideFilter(final String dataStoreName) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| import java.util.stream.Collectors; | ||
| import lombok.NoArgsConstructor; | ||
| import org.apache.commons.lang3.StringUtils; | ||
| import org.hypertrace.core.documentstore.DocumentType; | ||
| import org.hypertrace.core.documentstore.expression.impl.FunctionExpression; | ||
| import org.hypertrace.core.documentstore.expression.operators.FunctionOperator; | ||
| import org.hypertrace.core.documentstore.expression.type.SelectTypeExpression; | ||
|
|
@@ -49,10 +50,11 @@ public String visit(final FunctionExpression expression) { | |
| } | ||
|
|
||
| if (numArgs == 1) { | ||
| if (expression.getOperator().equals(FunctionOperator.LENGTH)) { | ||
| return buildLengthExpression(expression.getOperands().get(0)); | ||
| } | ||
| String parsedExpression = getParsedExpression(expression.getOperands().get(0)); | ||
| return expression.getOperator().equals(FunctionOperator.LENGTH) | ||
| ? String.format("ARRAY_LENGTH( %s, %s )", parsedExpression, ARRAY_DIMENSION) | ||
| : String.format("%s( %s )", expression.getOperator(), parsedExpression); | ||
| return String.format("%s( %s )", expression.getOperator(), parsedExpression); | ||
| } | ||
|
|
||
| Collector<String, ?, String> collector = | ||
|
|
@@ -83,6 +85,27 @@ private Collector getCollectorForFunctionOperator(FunctionOperator operator) { | |
| String.format("Query operation:%s not supported", operator)); | ||
| } | ||
|
|
||
| private String buildLengthExpression(final SelectTypeExpression operand) { | ||
| Optional<String> identifier = Optional.ofNullable(operand.accept(identifierExpressionVisitor)); | ||
| Optional<String> resolvedSelection = | ||
| identifier.map(v -> getPostgresQueryParser().getPgSelections().get(v)); | ||
| if (resolvedSelection.isPresent()) { | ||
| return String.format( | ||
| "COALESCE( ARRAY_LENGTH( %s, %s ), 0 )", resolvedSelection.get(), ARRAY_DIMENSION); | ||
| } | ||
|
Comment on lines
+89
to
+95
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In practice, the |
||
| PostgresFieldIdentifierExpressionVisitor fieldVisitor = | ||
| new PostgresFieldIdentifierExpressionVisitor(getPostgresQueryParser()); | ||
| String parsedExpression = operand.accept(fieldVisitor); | ||
| if (getPostgresQueryParser().getPgColTransformer().getDocumentType() == DocumentType.FLAT) { | ||
| return String.format( | ||
| "COALESCE( ARRAY_LENGTH( %s, %s ), 0 )", parsedExpression, ARRAY_DIMENSION); | ||
| } | ||
| return String.format( | ||
| "jsonb_array_length( CASE WHEN jsonb_typeof( %s ) = 'array' THEN %s" | ||
| + " ELSE '[]'::jsonb END )", | ||
| parsedExpression, parsedExpression); | ||
| } | ||
|
|
||
| private String getParsedExpression(final SelectTypeExpression expression) { | ||
| Optional<String> identifier = | ||
| Optional.ofNullable(expression.accept(identifierExpressionVisitor)); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,12 @@ | |
| { | ||
| "$project": { | ||
| "total": { | ||
| "$size": "$total" | ||
| "$size": { | ||
| "$ifNull": [ | ||
| "$total", | ||
| [] | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,12 @@ | |
| { | ||
| "$project": { | ||
| "total": { | ||
| "$size": "$total" | ||
| "$size": { | ||
| "$ifNull": [ | ||
| "$total", | ||
| [] | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| }, | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,7 +10,12 @@ | |
| { | ||
| "$project": { | ||
| "total": { | ||
| "$size": "$total" | ||
| "$size": { | ||
| "$ifNull": [ | ||
| "$total", | ||
| [] | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The integration test runs against nested collections (the standard document store pattern) which exercises the
jsonb_array_lengthpath. The flat collection path (ARRAY_LENGTH) is covered by the existing unit tests inPostgresQueryParserTestwhich validate SQL generation for LENGTH on aggregation aliases. The flat collection LENGTH on a direct column field follows the sameARRAY_LENGTHcodepath and is validated by the existing tests that assert correct SQL output.