Skip to content

Commit

Permalink
datastore: set range format to field[start..end]:step, #TASK-7151, #T…
Browse files Browse the repository at this point in the history
…ASK-7134
  • Loading branch information
jtarraga committed Oct 28, 2024
1 parent 17f83b2 commit 865b94a
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public List<FacetField> convertToDataModelType(Document document) {
}
key = key.substring(0, key.length() - RANGES_SUFFIX.length()).replace(GenericDocumentComplexConverter.TO_REPLACE_DOTS, ".");
if (other != null) {
key += " (out of range = " + other + ")";
key += " (counts out of range: " + other + ")";
}
FacetField facetField = new FacetField(key, "range", facetFieldValues)
.setStart(start)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,14 @@ public class MongoDBQueryUtils {
private static final Pattern OPERATION_DATE_PATTERN = Pattern.compile("^(<=?|>=?|!=|!?=?~|=?=?)([0-9]+)(-?)([0-9]*)");

private static final Pattern FUNC_ACCUMULATOR_PATTERN = Pattern.compile("([a-zA-Z]+)\\(([.a-zA-Z0-9]+)\\)");
private static final Pattern RANGE_PATTERN = Pattern.compile("([.a-zA-Z0-9]+)\\[([.0-9]+):([.0-9]+)\\]:([.0-9]+)");
private static final String RANGE_MARK = "..";
private static final String RANGE_MARK1 = "[";
private static final String RANGE_MARK2 = "]";
private static final String RANGE_SPLIT_MARK = "\\.\\.";
private static final Pattern RANGE_START_PATTERN = Pattern.compile("([.a-zA-Z0-9]+)\\[([.0-9]+)");
private static final Pattern RANGE_END_PATTERN = Pattern.compile("([.0-9]+)\\]:([.0-9]+)");
public static final String INVALID_FORMAT_MSG = "Invalid format ";
public static final String RANGE_FORMAT_MSG = " for range aggregation. Valid format is: field[start..end]:step, e.g: size[0..1000]:200";

public static final String INTERNAL_ID = "_id";
public static final String AND_SEPARATOR = "_and_";
Expand Down Expand Up @@ -704,15 +711,25 @@ private static List<Bson> createFacet(Bson query, List<String> facetFields) {
accumulator = Accumulator.valueOf(matcher.group(1));
field = matcher.group(2);
} else {
matcher = RANGE_PATTERN.matcher(facetField);
if (matcher.matches()) {
accumulator = bucket;
field = matcher.group(1);
double start = Double.parseDouble(matcher.group(2));
double end = Double.parseDouble(matcher.group(3));
double step = Double.parseDouble(matcher.group(4));
for (double i = start; i <= end; i += step) {
boundaries.add(i);
if (facetField.contains(RANGE_MARK) || facetField.contains(RANGE_MARK1) || facetField.contains(RANGE_MARK2)) {
String[] split = facetField.split(RANGE_SPLIT_MARK);
if (split.length == 2) {
Matcher matcher1 = RANGE_START_PATTERN.matcher(split[0]);
Matcher matcher2 = RANGE_END_PATTERN.matcher(split[1]);
if (matcher1.matches() && matcher2.matches()) {
accumulator = bucket;
field = matcher1.group(1);
double start = Double.parseDouble(matcher1.group(2));
double end = Double.parseDouble(matcher2.group(1));
double step = Double.parseDouble(matcher2.group(2));
for (double i = start; i <= end; i += step) {
boundaries.add(i);
}
} else {
throw new IllegalArgumentException(INVALID_FORMAT_MSG + facetField + RANGE_FORMAT_MSG);
}
} else {
throw new IllegalArgumentException(INVALID_FORMAT_MSG + facetField + RANGE_FORMAT_MSG);
}
} else {
accumulator = count;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -480,13 +480,13 @@ public void testFacet() {
Document match = new Document("age", new BasicDBObject("$gt", 2));
// Document match = new Document("house.m2", new BasicDBObject("$gt", 10000));
// List<Bson> facets = MongoDBQueryUtils.createFacet(match, "");
// List<Bson> facets = MongoDBQueryUtils.createFacet(match, "count(name);name,surname;avg(age);min(age);max(age);number[0:1000000]:100000");
// List<Bson> facets = MongoDBQueryUtils.createFacet(match, "count(name);name,surname;avg(age);min(age);max(age);number[0..1000000]100000");
// List<Bson> facets = MongoDBQueryUtils.createFacet(match, "name,surname");
// List<Bson> facets = MongoDBQueryUtils.createFacet(match, "avg(house.numRooms)");
// List<Bson> facets = MongoDBQueryUtils.createFacet(match, "avg(house.m2)");
// List<Bson> facets = MongoDBQueryUtils.createFacet(match, "name,house.color");
List<Bson> facetsWithDots = MongoDBQueryUtils.createFacet(match, "avg(house.numRooms);count(house.color);name,house.color;avg(house.m2);min(house.m2);max(house.m2);house.m2[0:20000]:1000");
// List<Bson> facets = MongoDBQueryUtils.createFacet(match, "house.m2[0:20000]:1000");
List<Bson> facetsWithDots = MongoDBQueryUtils.createFacet(match, "avg(house.numRooms);count(house.color);name,house.color;avg(house.m2);min(house.m2);max(house.m2);house.m2[0..20000]:1000");
// List<Bson> facets = MongoDBQueryUtils.createFacet(match, "house.m2[0..20000]:1000");

System.out.println("facetsWithDots = " + facetsWithDots);

Expand Down Expand Up @@ -532,11 +532,11 @@ public void testFacetUsingConverter() {
Document match = new Document("age", new BasicDBObject("$gt", 2));
// Document match = new Document("house.m2", new BasicDBObject("$gt", 10000));
// List<Bson> facets = MongoDBQueryUtils.createFacet(match, "");
List<Bson> facets = MongoDBQueryUtils.createFacet(match, "count(name);name,surname;avg(age);min(age);max(age);number[0:1000000]:100000");
// List<Bson> facets = MongoDBQueryUtils.createFacet(match, "count(name);name,surname;avg(age);min(age);max(age);number[0..1000000]:100000");
// List<Bson> facets = MongoDBQueryUtils.createFacet(match, "avg(house.m2);name;name,surname");
// List<Bson> facets = MongoDBQueryUtils.createFacet(match, "avg(house.numRooms)");
// List<Bson> facets = MongoDBQueryUtils.createFacet(match, "avg(house.m2);name");
// List<Bson> facets = MongoDBQueryUtils.createFacet(match, "house.m2[0:20000]:1000");
List<Bson> facets = MongoDBQueryUtils.createFacet(match, "house.m2[0..20000]:1000");

MongoDBFacetToFacetFieldsConverter converter = new MongoDBFacetToFacetFieldsConverter();
DataResult<List<FacetField>> aggregate = mongoDBCollection.aggregate(facets, converter, null);
Expand All @@ -548,6 +548,23 @@ public void testFacetUsingConverter() {
}
}

@Test(expected = IllegalArgumentException.class)
public void testFacetInvalidRangeFormat() {
Document match = new Document("age", new BasicDBObject("$gt", 2));
MongoDBQueryUtils.createFacet(match, "house.m2[toto0..20000]:1000");
}

@Test(expected = IllegalArgumentException.class)
public void testFacetInvalidRangeFormat1() {
Document match = new Document("age", new BasicDBObject("$gt", 2));
MongoDBQueryUtils.createFacet(match, "house.m2[0:20000]:1000");
}

@Test(expected = IllegalArgumentException.class)
public void testFacetInvalidRangeFormat2() {
Document match = new Document("age", new BasicDBObject("$gt", 2));
MongoDBQueryUtils.createFacet(match, "house.m2[toto0..20000]..1000");
}

@Test
public void testInsert() throws Exception {
Expand Down

0 comments on commit 865b94a

Please sign in to comment.