Skip to content

Commit 595eab3

Browse files
committed
SQL: fix object extraction from sources (#37502)
Throws an exception if hit extractor tries to retrieve unsupported object. For example, selecting "a" from `{"a": {"b": "c"}}` now throws an exception instead of returning null. Relates to #37364
1 parent c76abc2 commit 595eab3

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/extractor/FieldHitExtractor.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,14 @@ Object extractFromSource(Map<String, Object> map) {
166166
sj.add(path[i]);
167167
Object node = subMap.get(sj.toString());
168168
if (node instanceof Map) {
169-
// Add the sub-map to the queue along with the current path index
170-
queue.add(new Tuple<>(i, (Map<String, Object>) node));
169+
if (i < path.length - 1) {
170+
// Add the sub-map to the queue along with the current path index
171+
queue.add(new Tuple<>(i, (Map<String, Object>) node));
172+
} else {
173+
// We exhausted the path and got a map
174+
// If it is an object - it will be handled in the value extractor
175+
value = node;
176+
}
171177
} else if (node != null) {
172178
if (i < path.length - 1) {
173179
// If we reach a concrete value without exhausting the full path, something is wrong with the mapping

x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/extractor/FieldHitExtractorTests.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,24 @@ public void testFieldWithDotsAndSamePathButDifferentHierarchy() {
336336
assertThat(ex.getMessage(), is("Multiple values (returned by [a.b.c.d.e.f.g]) are not supported"));
337337
}
338338

339+
public void testObjectsForSourceValue() throws IOException {
340+
String fieldName = randomAlphaOfLength(5);
341+
FieldHitExtractor fe = new FieldHitExtractor(fieldName, null, false);
342+
SearchHit hit = new SearchHit(1);
343+
XContentBuilder source = JsonXContent.contentBuilder();
344+
source.startObject(); {
345+
source.startObject(fieldName); {
346+
source.field("b", "c");
347+
}
348+
source.endObject();
349+
}
350+
source.endObject();
351+
BytesReference sourceRef = BytesReference.bytes(source);
352+
hit.sourceRef(sourceRef);
353+
SqlException ex = expectThrows(SqlException.class, () -> fe.extract(hit));
354+
assertThat(ex.getMessage(), is("Objects (returned by [" + fieldName + "]) are not supported"));
355+
}
356+
339357
private Object randomValue() {
340358
Supplier<Object> value = randomFrom(Arrays.asList(
341359
() -> randomAlphaOfLength(10),

0 commit comments

Comments
 (0)