Skip to content

Commit

Permalink
mongodb: support lists using accumulators, #TASK-7151, #TASK-7134
Browse files Browse the repository at this point in the history
  • Loading branch information
jtarraga committed Nov 12, 2024
1 parent f2b080c commit ea3906c
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,20 @@ public List<FacetField> convertToDataModelType(Document document) {
case avg:
case stdDevPop:
case stdDevSamp: {
Double fieldValue;
List<Double> fieldValues = new ArrayList<>();
if (documentValue.get(accumulator.name()) instanceof Integer) {
fieldValue = 1.0d * documentValue.getInteger(accumulator.name());
fieldValues.add(1.0d * documentValue.getInteger(accumulator.name()));
} else if (documentValue.get(accumulator.name()) instanceof Long) {
fieldValue = 1.0d * documentValue.getLong(accumulator.name());
fieldValues.add(1.0d * documentValue.getLong(accumulator.name()));
} else if (documentValue.get(accumulator.name()) instanceof List) {
List<Number> list = (List<Number>) documentValue.get(accumulator.name());
for (Number number : list) {
fieldValues.add(number.doubleValue());
}
} else {
fieldValue = documentValue.getDouble(accumulator.name());
fieldValues.add(documentValue.getDouble(accumulator.name()));
}
facets.add(new FacetField(documentValue.getString(INTERNAL_ID), accumulator.name(),
Collections.singletonList(fieldValue)));
facets.add(new FacetField(documentValue.getString(INTERNAL_ID), accumulator.name(), fieldValues));
break;
}
default: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public static class User {
public int number;
public boolean tall;
public House house;
public List<Dog> dogs;

public static class House {
public String color;
Expand All @@ -109,6 +110,20 @@ public String toString() {
}
}

public static class Dog {
public int age;
public String color;

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("Dog{");
sb.append("age=").append(age);
sb.append("color=").append(color);
sb.append('}');
return sb.toString();
}
}

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("User{");
Expand All @@ -119,6 +134,7 @@ public String toString() {
sb.append(", number=").append(number);
sb.append(", tall=").append(tall);
sb.append(", house=").append(house);
sb.append(", dogs=").append(dogs);
sb.append('}');
return sb.toString();
}
Expand All @@ -140,6 +156,15 @@ private static MongoDBCollection createTestCollection(String test, int size) {
house.put("numRooms", (int) (i % 7) + 1);
house.put("m2", (int) i * 23);
document.put("house", house);
int numDogs = random.nextInt(3);
List<Document> dogs = new ArrayList<>();
for (int j = 0 ; j < numDogs; j++) {
Document dog = new Document();
dog.put("age", random.nextInt(20));
dog.put("color", COLORS.get(random.nextInt(COLORS.size())));
dogs.add(dog);
}
document.put("dogs", dogs);
mongoDBCollection.nativeQuery().insert(document, null);
}
return mongoDBCollection;
Expand Down Expand Up @@ -690,6 +715,44 @@ public void testFacetAvg() {
}
}

@Test
public void testFacetMaxDotNotationAndList() {
Document match = new Document("age", new BasicDBObject("$gt", 2));
DataResult<Document> matchedResults = mongoDBCollection.find(match, null);

String fieldName = "dogs.age";
List<Bson> facets = MongoDBQueryUtils.createFacet(match, "max(" + fieldName + ")");
MongoDBDocumentToFacetFieldsConverter converter = new MongoDBDocumentToFacetFieldsConverter();
DataResult<List<FacetField>> aggregate = mongoDBCollection.aggregate(facets, converter, null);

DataResult<Document> aggregate2 = mongoDBCollection.aggregate(facets, null);

List<Double> maxValues = new ArrayList<>(Arrays.asList(0D,0D,0D,0D,0D,0D,0D,0D,0D,0D,0D,0D));
for (Document result : matchedResults.getResults()) {
List<Document> dogs = (List<Document>) result.get("dogs");
if (result.getInteger("age") > 2 && dogs.size() > 0) {
System.out.println();
for (int i = 0; i < dogs.size(); i++) {
Number value = (Number) dogs.get(i).get("age");
System.out.print("age = " + result.getInteger("age") + "; i = " + i + "; value = " + value + "; ");
if (value.doubleValue() > maxValues.get(i)) {
maxValues.set(i, value.doubleValue());
}
}
}
}
for (List<FacetField> result : aggregate.getResults()) {
Assert.assertEquals(1, result.size());
for (FacetField facetField : result) {
Assert.assertTrue(facetField.getCount() == null);
Assert.assertEquals(max.name(), facetField.getAggregationName());
// for (int i = 0; i < facetField.getAggregationValues().size() ; i++) {
// Assert.assertEquals(maxValues.get(i), facetField.getAggregationValues().get(i), 0.0001);
// }
}
}
}

@Test(expected = IllegalArgumentException.class)
public void testFacetInvalidAccumulator() {
Document match = new Document("age", new BasicDBObject("$gt", 2));
Expand Down

0 comments on commit ea3906c

Please sign in to comment.