Jackie-Jiang commented on a change in pull request #6347:
URL: https://github.com/apache/incubator-pinot/pull/6347#discussion_r541386593



##########
File path: 
pinot-common/src/main/java/org/apache/pinot/common/function/scalar/JsonFunctions.java
##########
@@ -56,4 +57,42 @@ public static String jsonFormat(Object object)
       throws JsonProcessingException {
     return JsonUtils.objectToString(object);
   }
+
+  /**
+   * Extract object based on Json path
+   */
+  @ScalarFunction
+  public static Object jsonPath(Object object, String jsonPath) {
+    if (object instanceof String) {
+      return JsonPath.read((String) object, jsonPath);
+    }
+    return JsonPath.read(object, jsonPath);
+  }
+
+  /**
+   * Extract from Json with path to String
+   */
+  @ScalarFunction
+  public static String jsonPathString(Object object, String jsonPath)
+      throws JsonProcessingException {
+    return JsonUtils.objectToString(jsonPath(object, jsonPath));
+  }
+
+  /**
+   * Extract from Json with path to Long
+   */
+  @ScalarFunction
+  public static Long jsonPathLong(Object object, String jsonPath)

Review comment:
       return primitive long

##########
File path: 
pinot-common/src/main/java/org/apache/pinot/common/function/scalar/JsonFunctions.java
##########
@@ -56,4 +57,42 @@ public static String jsonFormat(Object object)
       throws JsonProcessingException {
     return JsonUtils.objectToString(object);
   }
+
+  /**
+   * Extract object based on Json path
+   */
+  @ScalarFunction
+  public static Object jsonPath(Object object, String jsonPath) {
+    if (object instanceof String) {
+      return JsonPath.read((String) object, jsonPath);
+    }
+    return JsonPath.read(object, jsonPath);
+  }
+
+  /**
+   * Extract from Json with path to String
+   */
+  @ScalarFunction
+  public static String jsonPathString(Object object, String jsonPath)
+      throws JsonProcessingException {
+    return JsonUtils.objectToString(jsonPath(object, jsonPath));
+  }
+
+  /**
+   * Extract from Json with path to Long
+   */
+  @ScalarFunction
+  public static Long jsonPathLong(Object object, String jsonPath)
+      throws JsonProcessingException {
+    return Long.parseLong(jsonPathString(object, jsonPath));
+  }
+
+  /**
+   * Extract from Json with path to Double
+   */
+  @ScalarFunction
+  public static Double jsonPathDouble(Object object, String jsonPath)

Review comment:
       return primitive double

##########
File path: 
pinot-common/src/test/java/org/apache/pinot/common/function/JsonFunctionsTest.java
##########
@@ -0,0 +1,65 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.common.function;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.jayway.jsonpath.JsonPath;
+import org.apache.pinot.common.function.scalar.JsonFunctions;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertEquals;
+
+
+public class JsonFunctionsTest {
+
+  @Test
+  public void testJsonFunction()
+      throws JsonProcessingException {
+    String jsonString = "{" +
+        "  \"id\": \"7044885078\"," +
+        "  \"type\": \"CreateEvent\"," +
+        "  \"actor\": {" +
+        "    \"id\": 33500718," +
+        "    \"login\": \"dipper-github-icn-bom-cdg\"," +
+        "    \"display_login\": \"dipper-github-icn-bom-cdg\"," +
+        "    \"gravatar_id\": \"\"," +
+        "    \"url\": 
\"https://api.github.com/users/dipper-github-icn-bom-cdg\","; +
+        "    \"avatar_url\": 
\"https://avatars.githubusercontent.com/u/33500718?\""; +
+        "  }," +
+        "  \"repo\": {" +
+        "    \"id\": 112368043," +
+        "    \"name\": \"dipper-github-icn-bom-cdg/test-ruby-sample\"," +
+        "    \"url\": 
\"https://api.github.com/repos/dipper-github-icn-bom-cdg/test-ruby-sample\""; +
+        "  }," +
+        "  \"payload\": {" +
+        "    \"ref\": \"canary-test-7f3af0db-3ffa-4259-894f-950d2c76594b\"," +
+        "    \"ref_type\": \"branch\"," +
+        "    \"master_branch\": \"master\"," +
+        "    \"description\": null," +
+        "    \"pusher_type\": \"user\"" +
+        "  }," +
+        "  \"public\": true," +
+        "  \"created_at\": \"2018-01-01T11:12:53Z\"" +
+        "}";
+    assertEquals(JsonFunctions.jsonPathString(jsonString, "$.actor.id"), 
"33500718");
+    assertEquals(JsonFunctions.jsonPathLong(jsonString, "$.actor.id"), 
Long.valueOf(33500718L));

Review comment:
       ```suggestion
       assertEquals(JsonFunctions.jsonPathLong(jsonString, "$.actor.id"), 
33500718L);
   ```

##########
File path: 
pinot-core/src/main/java/org/apache/pinot/core/util/TableConfigUtils.java
##########
@@ -238,7 +238,8 @@ public static void validateIngestionConfig(TableConfig 
tableConfig, @Nullable Sc
           String columnName = transformConfig.getColumnName();
           if (schema != null) {
             Preconditions.checkState(schema.getFieldSpecFor(columnName) != 
null,
-                "The destination column of the transform function must be 
present in the schema");
+                "The destination column '" + columnName
+                    + "'of the transform function must be present in the 
schema");

Review comment:
       ```suggestion
                       + "' of the transform function must be present in the 
schema");
   ```




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to