zhztheplayer commented on a change in pull request #1070: [CALCITE-2808]Add the 
JSON_LENGTH function
URL: https://github.com/apache/calcite/pull/1070#discussion_r261671016
 
 

 ##########
 File path: core/src/main/java/org/apache/calcite/runtime/SqlFunctions.java
 ##########
 @@ -2755,6 +2755,31 @@ private static Integer getJsonDepth(Object o) {
     return depth;
   }
 
+  public static Integer jsonLength(Object input) {
+    final PathContext context = (PathContext) input;
+    final Integer result;
+    try {
+      Object value = context.pathReturned;
+      if (value == null) {
+        result = null;
+      } else {
+        if (value instanceof Collection) {
+          result = ((Collection) value).size();
+        } else if (value instanceof Map) {
+          result = ((LinkedHashMap) value).size();
+        } else if (isScalarObject(value)) {
+          result = 1;
+        } else {
+          result = 0;
+        }
+      }
+    } catch (Exception ex) {
+      throw RESOURCE.unknownObjectOfJsonLength(
 
 Review comment:
   The method name and error message indicate that `input.toString()` is an 
"object". However it is actually a composite context evaluated by jsonpath 
expression. I suggest to rename `unknownObjectOfJsonLength` to 
`unknownContextOfJsonLength`, also make some similar changes to the error 
message.

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to