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

xiangfu pushed a commit to branch adding_scalar_function_to_extract_json_array
in repository https://gitbox.apache.org/repos/asf/incubator-pinot.git

commit 5f08b3a8c1e6e2983b9611eb7979f24d39ab3e81
Author: Xiang Fu <fx19880...@gmail.com>
AuthorDate: Tue Jan 26 10:53:05 2021 -0800

    Adding scalar function JsonPathArray to extract arrays from json
---
 .../common/function/scalar/JsonFunctions.java      | 12 +++++++
 .../pinot/common/function/JsonFunctionsTest.java   | 37 ++++++++++++++++++++++
 2 files changed, 49 insertions(+)

diff --git 
a/pinot-common/src/main/java/org/apache/pinot/common/function/scalar/JsonFunctions.java
 
b/pinot-common/src/main/java/org/apache/pinot/common/function/scalar/JsonFunctions.java
index 3a9f0f9..6b1944d 100644
--- 
a/pinot-common/src/main/java/org/apache/pinot/common/function/scalar/JsonFunctions.java
+++ 
b/pinot-common/src/main/java/org/apache/pinot/common/function/scalar/JsonFunctions.java
@@ -21,6 +21,7 @@ package org.apache.pinot.common.function.scalar;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.jayway.jsonpath.JsonPath;
 import java.util.Map;
+import net.minidev.json.JSONArray;
 import org.apache.pinot.spi.annotations.ScalarFunction;
 import org.apache.pinot.spi.utils.JsonUtils;
 
@@ -70,6 +71,17 @@ public class JsonFunctions {
   }
 
   /**
+   * Extract object based on Json path
+   */
+  @ScalarFunction
+  public static Object jsonPathArray(Object object, String jsonPath) {
+    if (object instanceof String) {
+      return ((JSONArray) JsonPath.read((String) object, jsonPath)).toArray();
+    }
+    return ((JSONArray) JsonPath.read(object, jsonPath)).toArray();
+  }
+
+  /**
    * Extract from Json with path to String
    */
   @ScalarFunction
diff --git 
a/pinot-common/src/test/java/org/apache/pinot/common/function/JsonFunctionsTest.java
 
b/pinot-common/src/test/java/org/apache/pinot/common/function/JsonFunctionsTest.java
index f11f256..998cf62 100644
--- 
a/pinot-common/src/test/java/org/apache/pinot/common/function/JsonFunctionsTest.java
+++ 
b/pinot-common/src/test/java/org/apache/pinot/common/function/JsonFunctionsTest.java
@@ -65,4 +65,41 @@ public class JsonFunctionsTest {
     assertEquals(JsonFunctions.jsonPathDouble(jsonString, "$.actor.aaa", 
53.2), 53.2);
   }
 
+  @Test
+  public void testJsonFunctionExtractingArray()
+      throws JsonProcessingException {
+    String jsonString = "{\n" +
+        "    \"name\": \"Pete\",\n" +
+        "    \"age\": 24,\n" +
+        "    \"subjects\": [\n" +
+        "        {\n" +
+        "            \"name\": \"maths\",\n" +
+        "        \"grade\": \"A\"\n" +
+        "        },\n" +
+        "        {\n" +
+        "            \"name\": \"maths\",\n" +
+        "        \"grade\": \"B\"\n" +
+        "        }\n" +
+        "    ]\n" +
+        "}";
+    assertEquals(JsonFunctions.jsonPathArray(jsonString, 
"$.subjects[*].name"), new String[]{"maths", "maths"});
+    assertEquals(JsonFunctions.jsonPathArray(jsonString, 
"$.subjects[*].grade"), new String[]{"A", "B"});
+  }
+
+  @Test
+  public void testJsonFunctionOnJsonArray() {
+    String jsonArrayString =
+        "[\n" +
+        "        {\n" +
+        "            \"name\": \"maths\",\n" +
+        "        \"grade\": \"A\"\n" +
+        "        },\n" +
+        "        {\n" +
+        "            \"name\": \"maths\",\n" +
+        "        \"grade\": \"B\"\n" +
+        "        }\n" +
+        "]";
+    assertEquals(JsonFunctions.jsonPathArray(jsonArrayString, "$.[*].name"), 
new String[]{"maths", "maths"});
+    assertEquals(JsonFunctions.jsonPathArray(jsonArrayString, "$.[*].grade"), 
new String[]{"A", "B"});
+  }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org
For additional commands, e-mail: commits-h...@pinot.apache.org

Reply via email to