Skip to content

Commit a01f451

Browse files
committed
Limit complexity of IntervalQueryBuilderTests#testRandomSource() (#41538)
IntervalsSources can throw IllegalArgumentExceptions if they would produce too many disjunctions. To mitigate against this when building random sources, we limit the depth of the randomly generated source to four nested sources Fixes #41402
1 parent b23709b commit a01f451

File tree

1 file changed

+32
-31
lines changed

1 file changed

+32
-31
lines changed

server/src/test/java/org/elasticsearch/index/query/IntervalQueryBuilderTests.java

Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,14 @@ public class IntervalQueryBuilderTests extends AbstractQueryTestCase<IntervalQue
4949

5050
@Override
5151
protected IntervalQueryBuilder doCreateTestQueryBuilder() {
52-
return new IntervalQueryBuilder(STRING_FIELD_NAME, createRandomSource());
53-
}
54-
55-
@Override
56-
public void testUnknownField() throws IOException {
57-
super.testUnknownField();
52+
return new IntervalQueryBuilder(STRING_FIELD_NAME, createRandomSource(0));
5853
}
5954

6055
private static final String[] filters = new String[]{
6156
"containing", "contained_by", "not_containing", "not_contained_by",
6257
"overlapping", "not_overlapping", "before", "after"
6358
};
6459

65-
private IntervalsSourceProvider.IntervalFilter createRandomFilter() {
66-
if (randomInt(20) > 18) {
67-
return new IntervalsSourceProvider.IntervalFilter(createRandomSource(), randomFrom(filters));
68-
}
69-
return null;
70-
}
71-
7260
private static final String MASKED_FIELD = "masked_field";
7361
private static final String NO_POSITIONS_FIELD = "no_positions_field";
7462

@@ -88,42 +76,56 @@ protected void initializeAdditionalMappings(MapperService mapperService) throws
8876
new CompressedXContent(Strings.toString(mapping)), MapperService.MergeReason.MAPPING_UPDATE);
8977
}
9078

91-
private IntervalsSourceProvider createRandomSource() {
92-
String useField = rarely() ? MASKED_FIELD : null;
79+
private IntervalsSourceProvider createRandomSource(int depth) {
80+
if (depth > 3) {
81+
return createRandomMatch(depth + 1);
82+
}
9383
switch (randomInt(20)) {
9484
case 0:
9585
case 1:
9686
int orCount = randomInt(4) + 1;
9787
List<IntervalsSourceProvider> orSources = new ArrayList<>();
9888
for (int i = 0; i < orCount; i++) {
99-
orSources.add(createRandomSource());
89+
orSources.add(createRandomSource(depth + 1));
10090
}
101-
return new IntervalsSourceProvider.Disjunction(orSources, createRandomFilter());
91+
return new IntervalsSourceProvider.Disjunction(orSources, createRandomFilter(depth + 1));
10292
case 2:
10393
case 3:
10494
int count = randomInt(5) + 1;
10595
List<IntervalsSourceProvider> subSources = new ArrayList<>();
10696
for (int i = 0; i < count; i++) {
107-
subSources.add(createRandomSource());
97+
subSources.add(createRandomSource(depth + 1));
10898
}
10999
boolean ordered = randomBoolean();
110100
int maxGaps = randomInt(5) - 1;
111-
IntervalsSourceProvider.IntervalFilter filter = createRandomFilter();
101+
IntervalsSourceProvider.IntervalFilter filter = createRandomFilter(depth + 1);
112102
return new IntervalsSourceProvider.Combine(subSources, ordered, maxGaps, filter);
113103
default:
114-
int wordCount = randomInt(4) + 1;
115-
List<String> words = new ArrayList<>();
116-
for (int i = 0; i < wordCount; i++) {
117-
words.add(randomRealisticUnicodeOfLengthBetween(4, 20));
118-
}
119-
String text = String.join(" ", words);
120-
boolean mOrdered = randomBoolean();
121-
int maxMGaps = randomInt(5) - 1;
122-
String analyzer = randomFrom("simple", "keyword", "whitespace");
123-
return new IntervalsSourceProvider.Match(text, maxMGaps, mOrdered, analyzer, createRandomFilter(), useField);
104+
return createRandomMatch(depth + 1);
124105
}
125106
}
126107

108+
private IntervalsSourceProvider.IntervalFilter createRandomFilter(int depth) {
109+
if (depth < 3 && randomInt(20) > 18) {
110+
return new IntervalsSourceProvider.IntervalFilter(createRandomSource(depth + 1), randomFrom(filters));
111+
}
112+
return null;
113+
}
114+
115+
private IntervalsSourceProvider createRandomMatch(int depth) {
116+
String useField = rarely() ? MASKED_FIELD : null;
117+
int wordCount = randomInt(4) + 1;
118+
List<String> words = new ArrayList<>();
119+
for (int i = 0; i < wordCount; i++) {
120+
words.add(randomRealisticUnicodeOfLengthBetween(4, 20));
121+
}
122+
String text = String.join(" ", words);
123+
boolean mOrdered = randomBoolean();
124+
int maxMGaps = randomInt(5) - 1;
125+
String analyzer = randomFrom("simple", "keyword", "whitespace");
126+
return new IntervalsSourceProvider.Match(text, maxMGaps, mOrdered, analyzer, createRandomFilter(depth + 1), useField);
127+
}
128+
127129
@Override
128130
protected void doAssertLuceneQuery(IntervalQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException {
129131
assertThat(query, instanceOf(IntervalQuery.class));
@@ -382,6 +384,5 @@ public <FactoryType> FactoryType compile(Script script, ScriptContext<FactoryTyp
382384
assertEquals(expected, q);
383385

384386
}
385-
386-
387+
387388
}

0 commit comments

Comments
 (0)