Skip to content

Commit 8674826

Browse files
author
Hendrik Muhs
authored
[Transform] disable optimizations when using scripts in group_by (#60724)
disable optimizations when using scripts in group_by, when scripts using scripts we can not predict the outcome and we have no query counterpart. Other optimizations for other group_by's are not affected. fixes #57332
1 parent bd4f503 commit 8674826

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

x-pack/plugin/core/src/test/java/org/elasticsearch/xpack/core/transform/transforms/pivot/TermsGroupSourceTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ public static TermsGroupSource randomTermsGroupSource(Version version) {
2828
return new TermsGroupSource(field, scriptConfig, missingBucket);
2929
}
3030

31+
public static TermsGroupSource randomTermsGroupSourceNoScript() {
32+
String field = randomAlphaOfLengthBetween(1, 20);
33+
return new TermsGroupSource(field, null, randomBoolean());
34+
}
35+
3136
@Override
3237
protected TermsGroupSource doParseInstance(XContentParser parser) throws IOException {
3338
return TermsGroupSource.fromXContent(parser, false);
@@ -43,4 +48,9 @@ protected Reader<TermsGroupSource> instanceReader() {
4348
return TermsGroupSource::new;
4449
}
4550

51+
public void testSupportsIncrementalBucketUpdate() {
52+
TermsGroupSource terms = randomTermsGroupSource();
53+
assertEquals(terms.getScriptConfig() == null, terms.supportsIncrementalBucketUpdate());
54+
}
55+
4656
}

x-pack/plugin/transform/src/main/java/org/elasticsearch/xpack/transform/transforms/pivot/CompositeBucketsChangeCollector.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,11 @@ static Map<String, FieldCollector> createFieldCollectors(Map<String, SingleGroup
536536
Map<String, FieldCollector> fieldCollectors = new HashMap<>();
537537

538538
for (Entry<String, SingleGroupSource> entry : groups.entrySet()) {
539+
// skip any fields that use scripts
540+
if (entry.getValue().getScriptConfig() != null) {
541+
continue;
542+
}
543+
539544
switch (entry.getValue().getType()) {
540545
case TERMS:
541546
fieldCollectors.put(

x-pack/plugin/transform/src/test/java/org/elasticsearch/xpack/transform/transforms/pivot/CompositeBucketsChangeCollectorTests.java

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.elasticsearch.xpack.core.transform.transforms.pivot.GroupConfig;
2929
import org.elasticsearch.xpack.core.transform.transforms.pivot.GroupConfigTests;
3030
import org.elasticsearch.xpack.core.transform.transforms.pivot.HistogramGroupSourceTests;
31+
import org.elasticsearch.xpack.core.transform.transforms.pivot.ScriptConfigTests;
3132
import org.elasticsearch.xpack.core.transform.transforms.pivot.SingleGroupSource;
3233
import org.elasticsearch.xpack.core.transform.transforms.pivot.TermsGroupSource;
3334
import org.elasticsearch.xpack.core.transform.transforms.pivot.TermsGroupSourceTests;
@@ -80,7 +81,7 @@ public void testPageSize() throws IOException {
8081
assertEquals(10, getCompositeAggregationBuilder(collector.buildChangesQuery(new SearchSourceBuilder(), null, 10)).size());
8182

8283
// a terms group_by is limited by terms query
83-
SingleGroupSource termsGroupBy = TermsGroupSourceTests.randomTermsGroupSource();
84+
SingleGroupSource termsGroupBy = TermsGroupSourceTests.randomTermsGroupSourceNoScript();
8485
groups.put("terms", termsGroupBy);
8586

8687
collector = CompositeBucketsChangeCollector.buildChangeCollector(getCompositeAggregation(groups), groups, null);
@@ -196,6 +197,24 @@ public void testDateHistogramFieldCollector() throws IOException {
196197
assertNull(queryBuilder);
197198
}
198199

200+
public void testNoTermsFieldCollectorForScripts() throws IOException {
201+
Map<String, SingleGroupSource> groups = new LinkedHashMap<>();
202+
203+
// terms with value script
204+
SingleGroupSource termsGroupBy = new TermsGroupSource("id", ScriptConfigTests.randomScriptConfig(), false);
205+
groups.put("id", termsGroupBy);
206+
207+
Map<String, FieldCollector> fieldCollectors = CompositeBucketsChangeCollector.createFieldCollectors(groups, null);
208+
assertTrue(fieldCollectors.isEmpty());
209+
210+
// terms with only a script
211+
termsGroupBy = new TermsGroupSource(null, ScriptConfigTests.randomScriptConfig(), false);
212+
groups.put("id", termsGroupBy);
213+
214+
fieldCollectors = CompositeBucketsChangeCollector.createFieldCollectors(groups, null);
215+
assertTrue(fieldCollectors.isEmpty());
216+
}
217+
199218
private static CompositeAggregationBuilder getCompositeAggregation(Map<String, SingleGroupSource> groups) throws IOException {
200219
CompositeAggregationBuilder compositeAggregation;
201220
try (XContentBuilder builder = jsonBuilder()) {

0 commit comments

Comments
 (0)