Skip to content

Commit

Permalink
[CALCITE-5974] Elasticsearch adapter throws ClassCastException when i…
Browse files Browse the repository at this point in the history
…ndex mapping sets `dynamic_templates` without `properties`

Close apache#3406
  • Loading branch information
zoovwang authored and julianhyde committed Sep 21, 2023
1 parent 1cc1eb3 commit e1991e0
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,9 @@ static void visitMappingProperties(ObjectNode mapping,
BiConsumer<String, String> consumer) {
Objects.requireNonNull(mapping, "mapping");
Objects.requireNonNull(consumer, "consumer");
visitMappingProperties(new ArrayDeque<>(), mapping, consumer);
if (mapping.has("properties")) {
visitMappingProperties(new ArrayDeque<>(), mapping, consumer);
}
}

private static void visitMappingProperties(Deque<String> path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.aMapWithSize;
import static org.hamcrest.Matchers.anEmptyMap;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
Expand Down Expand Up @@ -197,4 +198,24 @@ public void setUp() {
assertThat(result.get("keyword"), is("keyword"));
assertThat(result.get("properties"), is("long"));
}


/** Test case for
* <a href="https://issues.apache.org/jira/browse/CALCITE-5974">[CALCITE-5974]
* Elasticsearch adapter throws ClassCastException when index mapping sets
* dynamic_templates without properties</a>. */
@Test void reservedEmptyPropertiesMapping() throws Exception {
// have special property names: type and properties
ObjectNode mapping =
mapper.readValue("{dynamic_templates:["
+ "{integers:"
+ "{match_mapping_type:'long',mapping:{type:'integer'}}"
+ "}]}", ObjectNode.class);

// The 'dynamic_templates' object has no 'properties' field,
// so the result is empty.
Map<String, String> result = new HashMap<>();
ElasticsearchJson.visitMappingProperties(mapping, result::put);
assertThat(result, anEmptyMap());
}
}

0 comments on commit e1991e0

Please sign in to comment.