Skip to content

Commit 4d474ca

Browse files
[ML] Fix error when a nested field is also incompatible (#71736)
This fixes a bug that was introduced in #71400. The bug occurs when the _explain API is called for a data frame analytics job and there are nested fields that are also incompatible. In particular, we end up calling `iterator.remove()` twice which throws `IllegalStateException`. I also took the chance to move the nested field check first as I think it's more informative to explain a field is not included due to being nested than because it has an incompatible type in this case. The PR is marked as `non-issue` as this has not been released yet.
1 parent 5394d57 commit 4d474ca

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

x-pack/plugin/ml/src/main/java/org/elasticsearch/xpack/ml/dataframe/extractor/ExtractedFieldsDetector.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -253,14 +253,13 @@ private void removeFieldsWithIncompatibleTypes(Set<String> fields, Set<FieldSele
253253
Iterator<String> fieldsIterator = fields.iterator();
254254
while (fieldsIterator.hasNext()) {
255255
String field = fieldsIterator.next();
256-
if (hasCompatibleType(field) == false) {
257-
addExcludedField(field, "unsupported type; supported types are " + getSupportedTypes(), fieldSelection);
258-
fieldsIterator.remove();
259-
}
260256
Optional<String> matchingNestedFieldPattern = findMatchingNestedFieldPattern(field);
261257
if (matchingNestedFieldPattern.isPresent()) {
262258
addExcludedNestedPattern(matchingNestedFieldPattern.get(), fieldSelection);
263259
fieldsIterator.remove();
260+
} else if (hasCompatibleType(field) == false) {
261+
addExcludedField(field, "unsupported type; supported types are " + getSupportedTypes(), fieldSelection);
262+
fieldsIterator.remove();
264263
}
265264
}
266265
}

x-pack/plugin/ml/src/test/java/org/elasticsearch/xpack/ml/dataframe/extractor/ExtractedFieldsDetectorTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -949,6 +949,27 @@ public void testDetect_GivenNestedFields() {
949949
);
950950
}
951951

952+
public void testDetect_GivenNestedFieldThatAlsoHasIncompatibleType() {
953+
FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder()
954+
.addAggregatableField("float_field", "float")
955+
.addNonAggregatableField("nested_field_1", "nested")
956+
.addAggregatableField("nested_field_1.a", "definitely_not_supported")
957+
.build();
958+
959+
ExtractedFieldsDetector extractedFieldsDetector = new ExtractedFieldsDetector(
960+
buildOutlierDetectionConfig(), 100, fieldCapabilities, Collections.emptyMap());
961+
Tuple<ExtractedFields, List<FieldSelection>> fieldExtraction = extractedFieldsDetector.detect();
962+
963+
List<ExtractedField> allFields = fieldExtraction.v1().getAllFields();
964+
assertThat(allFields, hasSize(1));
965+
assertThat(allFields.get(0).getName(), equalTo("float_field"));
966+
967+
assertFieldSelectionContains(fieldExtraction.v2(),
968+
FieldSelection.included("float_field", Collections.singleton("float"), false, FieldSelection.FeatureType.NUMERICAL),
969+
FieldSelection.excluded("nested_field_1.*", Collections.singleton("nested"), "nested fields are not supported")
970+
);
971+
}
972+
952973
public void testDetect_GivenAnalyzedFieldIncludesObjectField() {
953974
FieldCapabilitiesResponse fieldCapabilities = new MockFieldCapsResponseBuilder()
954975
.addAggregatableField("float_field", "float")

0 commit comments

Comments
 (0)