gustavodemorais commented on code in PR #28978:
URL: https://github.com/apache/flink/pull/28978#discussion_r3820511111


##########
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/codegen/JsonCodeGenUtils.java:
##########
@@ -39,40 +41,82 @@ private JsonCodeGenUtils() {}
      */
     public static GeneratedExpression generateJsonType(
             CodeGeneratorContext ctx, LogicalType returnType, 
Seq<GeneratedExpression> operands) {
-        boolean hasPath = operands.length() == 2;
         return GenerateUtils.generateCallWithStmtIfArgsNotNull(
                 ctx,
                 returnType,
                 operands,
                 true,
                 false,
                 argTerms -> {
-                    ParsedJson parsed = getOrCreateParsedJson(ctx, 
argTerms.head() + ".toString()");
-                    String call;
-                    if (hasPath) {
-                        String pathSpec = 
operands.apply(1).literalValue().get().toString();
-                        boolean definite = 
SqlJsonUtils.isPathDefinite(pathSpec);
-                        call =
-                                
CodeGenUtils.qualifyMethod(BuiltInMethods.JSON_TYPE_PATH())
-                                        + "("
-                                        + parsed.varName
-                                        + ", "
-                                        + argTerms.apply(1)
-                                        + ".toString(), "
-                                        + definite
-                                        + ")";
-                    } else {
-                        call =
-                                
CodeGenUtils.qualifyMethod(BuiltInMethods.JSON_TYPE())
-                                        + "("
-                                        + parsed.varName
-                                        + ")";
-                    }
-                    String resultExpr = CodeGenUtils.BINARY_STRING() + 
".fromString(" + call + ")";
-                    return new Tuple2<>(parsed.parseCode, resultExpr);
+                    Tuple2<String, String> parsedCall =
+                            generateCallOnParsedInput(
+                                    ctx,
+                                    operands,
+                                    argTerms,
+                                    BuiltInMethods.JSON_TYPE(),
+                                    BuiltInMethods.JSON_TYPE_PATH());
+                    String resultExpr =
+                            CodeGenUtils.BINARY_STRING() + ".fromString(" + 
parsedCall._2() + ")";
+                    return new Tuple2<>(parsedCall._1(), resultExpr);
                 });
     }
 
+    /**
+     * Generates {@code JSON_LENGTH(jsonValue)} or {@code 
JSON_LENGTH(jsonValue, path)}.
+     *
+     * <p>The parsed context is shared with the other JSON functions over the 
same input, so the
+     * input is parsed only once per record.
+     */
+    public static GeneratedExpression generateJsonLength(
+            CodeGeneratorContext ctx, LogicalType returnType, 
Seq<GeneratedExpression> operands) {
+        return GenerateUtils.generateCallWithStmtIfArgsNotNull(
+                ctx,
+                returnType,
+                operands,
+                true,
+                false,
+                argTerms ->
+                        generateCallOnParsedInput(
+                                ctx,
+                                operands,
+                                argTerms,
+                                BuiltInMethods.JSON_LENGTH(),
+                                BuiltInMethods.JSON_LENGTH_PATH()));
+    }
+
+    /**
+     * Builds the call against the shared parsed input: the whole-document 
overload, or the path
+     * overload with the {@code definite} flag resolved from the path literal 
at plan time.
+     *
+     * @return the parse statement and the call expression
+     */
+    private static Tuple2<String, String> generateCallOnParsedInput(
+            CodeGeneratorContext ctx,
+            Seq<GeneratedExpression> operands,
+            Seq<String> argTerms,
+            Method wholeDocument,
+            Method withPath) {
+        final ParsedJson parsed = getOrCreateParsedJson(ctx, argTerms.head() + 
".toString()");
+        if (argTerms.length() == 1) {
+            return new Tuple2<>(
+                    parsed.parseCode,
+                    CodeGenUtils.qualifyMethod(wholeDocument) + "(" + 
parsed.varName + ")");
+        }
+
+        final String pathSpec = 
operands.apply(1).literalValue().get().toString();
+        final boolean definite = SqlJsonUtils.isPathDefinite(pathSpec);

Review Comment:
   Sorry @VasShabu, I have one more nit. The variable is called definite 
everywhere and one asks itself what it means, it the function definite? Could 
you rename it everywhere?
   
   ```suggestion
           final boolean isPathDefinite = SqlJsonUtils.isPathDefinite(pathSpec);
   ```



-- 
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.

To unsubscribe, e-mail: [email protected]

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

Reply via email to