This is an automated email from the ASF dual-hosted git repository.

jhyde pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/calcite.git


The following commit(s) were added to refs/heads/main by this push:
     new e1991e08a2 [CALCITE-5974] Elasticsearch adapter throws 
ClassCastException when index mapping sets `dynamic_templates` without 
`properties`
e1991e08a2 is described below

commit e1991e08a225ef08c2402ab35c310d88fff3c222
Author: zoovwang <[email protected]>
AuthorDate: Fri Sep 1 18:29:37 2023 +0800

    [CALCITE-5974] Elasticsearch adapter throws ClassCastException when index 
mapping sets `dynamic_templates` without `properties`
    
    Close apache/calcite#3406
---
 .../adapter/elasticsearch/ElasticsearchJson.java    |  4 +++-
 .../elasticsearch/ElasticsearchJsonTest.java        | 21 +++++++++++++++++++++
 2 files changed, 24 insertions(+), 1 deletion(-)

diff --git 
a/elasticsearch/src/main/java/org/apache/calcite/adapter/elasticsearch/ElasticsearchJson.java
 
b/elasticsearch/src/main/java/org/apache/calcite/adapter/elasticsearch/ElasticsearchJson.java
index c717a661dc..f28db95cb7 100644
--- 
a/elasticsearch/src/main/java/org/apache/calcite/adapter/elasticsearch/ElasticsearchJson.java
+++ 
b/elasticsearch/src/main/java/org/apache/calcite/adapter/elasticsearch/ElasticsearchJson.java
@@ -98,7 +98,9 @@ final class ElasticsearchJson {
       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,
diff --git 
a/elasticsearch/src/test/java/org/apache/calcite/adapter/elasticsearch/ElasticsearchJsonTest.java
 
b/elasticsearch/src/test/java/org/apache/calcite/adapter/elasticsearch/ElasticsearchJsonTest.java
index 7496f71e01..3cf7dabcf5 100644
--- 
a/elasticsearch/src/test/java/org/apache/calcite/adapter/elasticsearch/ElasticsearchJsonTest.java
+++ 
b/elasticsearch/src/test/java/org/apache/calcite/adapter/elasticsearch/ElasticsearchJsonTest.java
@@ -33,6 +33,7 @@ import static org.hamcrest.CoreMatchers.instanceOf;
 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;
@@ -197,4 +198,24 @@ class ElasticsearchJsonTest {
     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());
+  }
 }

Reply via email to