Skip to content

Commit

Permalink
Fix return type of $split aggregation #214
Browse files Browse the repository at this point in the history
This is relevant if the values are processed in other expressions.
  • Loading branch information
bwaldvogel committed May 31, 2023
1 parent 8effe55 commit 5e478bc
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
Expand Up @@ -1379,7 +1379,8 @@ Object apply(List<?> expressionValue, Document document) {
name() + " requires an expression that evaluates to a string as a second argument, found: " + describeType(delimiter));
}

return ((String) string).split(Pattern.quote((String) delimiter));
String[] values = ((String) string).split(Pattern.quote((String) delimiter), -1);
return List.of(values);
}
},

Expand Down
Expand Up @@ -184,7 +184,7 @@ public static byte determineType(Object value) {
return BsonConstants.TYPE_BOOLEAN;
} else if (value instanceof BinData || value instanceof UUID || value instanceof LegacyUUID) {
return BsonConstants.TYPE_DATA;
} else if (value instanceof Collection<?> || value instanceof String[]) {
} else if (value instanceof Collection<?>) {
return BsonConstants.TYPE_ARRAY;
} else if (value instanceof Instant) {
return BsonConstants.TYPE_UTC_DATETIME;
Expand All @@ -206,9 +206,7 @@ public static byte determineType(Object value) {
}

private static List<?> collectionToList(Object value) {
if (value instanceof String[]) {
return List.of((String[]) value);
} else if (value instanceof List<?>) {
if (value instanceof List<?>) {
return (List<?>) value;
} else {
return new ArrayList<>((Collection<?>) value);
Expand Down
Expand Up @@ -1087,8 +1087,8 @@ void testEvaluateSlice() throws Exception {

@Test
void testEvaluateSplit() throws Exception {
assertThat((String[]) Expression.evaluate(json("$split: ['June-15-2013', '-']"), json(""))).containsExactly("June", "15", "2013");
assertThat((String[]) Expression.evaluate(json("$split: ['$a', '$b']"), json("a: 'foo bar', b: ' '"))).containsExactly("foo", "bar");
assertThat((List<String>) Expression.evaluate(json("$split: ['June-15-2013', '-']"), json(""))).containsExactly("June", "15", "2013");
assertThat((List<String>) Expression.evaluate(json("$split: ['$a', '$b']"), json("a: 'foo bar', b: ' '"))).containsExactly("foo", "bar");
assertThat(Expression.evaluate(json("$split: [null, ' ']"), json(""))).isNull();
assertThat(Expression.evaluate(json("$split: ['$doesNotExist', ' ']"), json(""))).isNull();

Expand Down
Expand Up @@ -1177,6 +1177,28 @@ void testAggregateWithSplit() throws Exception {
);
}

// https://github.com/bwaldvogel/mongo-java-server/issues/214
@Test
void testAggregateWithSplitAndArrayElementAt() throws Exception {
List<Document> pipeline = jsonList("$addFields: { pathSegments: { $split: ['$path', '/'] }}",
"$project: { pathSegments: 1, firstSegment: { $arrayElemAt: ['$pathSegments', 0] }}");

assertThat(collection.aggregate(pipeline)).isEmpty();

collection.insertOne(json("_id: 1, path: 'path/to/file'"));
collection.insertOne(json("_id: 2, path: '/path/to/file'"));
collection.insertOne(json("_id: 3, path: '/path/to/file/'"));
collection.insertOne(json("_id: 4"));

assertThat(collection.aggregate(pipeline))
.containsExactly(
json("_id: 1, pathSegments: ['path', 'to', 'file'], firstSegment: 'path'"),
json("_id: 2, pathSegments: ['', 'path', 'to', 'file'], firstSegment: ''"),
json("_id: 3, pathSegments: ['', 'path', 'to', 'file', ''], firstSegment: ''"),
json("_id: 4, pathSegments: null, firstSegment: null")
);
}

@Test
void testAggregateWithUnwind() throws Exception {
testAggregateWithUnwind(json("$unwind: '$sizes'"));
Expand Down

0 comments on commit 5e478bc

Please sign in to comment.