matriv commented on a change in pull request #17878:
URL: https://github.com/apache/flink/pull/17878#discussion_r755287680



##########
File path: 
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/casting/AbstractCodeGeneratorCastRule.java
##########
@@ -102,12 +105,32 @@ protected AbstractCodeGeneratorCastRule(CastRulePredicate 
predicate) {
         final String functionSignature =
                 "@Override public Object cast(Object _myInputObj) throws "
                         + className(TableException.class);
-        final String inputVarDecl =
-                inputTypeTerm + " " + inputTerm + " = (" + inputTypeTerm + ") 
_myInputObj;\n";
-        final String inputIsNullVarDecl =
-                "boolean " + inputIsNullTerm + " = _myInputObj == null;\n";
 
-        final String returnStmt = "return " + codeBlock.getReturnTerm() + 
";\n";
+        // Write the function body
+        final CastRuleUtils.CodeWriter bodyWriter = new 
CastRuleUtils.CodeWriter();
+        bodyWriter.declStmt(inputTypeTerm, inputTerm, cast(inputTypeTerm, 
"_myInputObj"));
+        bodyWriter.declStmt("boolean", inputIsNullTerm, "_myInputObj == null");
+        ctx.variableDeclarationStatements.forEach(decl -> 
bodyWriter.appendBlock(decl + "\n"));
+
+        if (this.canFail()) {
+            bodyWriter.tryCatchStmt(
+                    tryWriter ->
+                            tryWriter.append(codeBlock).stmt("return " + 
codeBlock.getReturnTerm()),
+                    Throwable.class,
+                    (exceptionTerm, catchWriter) ->
+                            catchWriter.throwStmt(
+                                    constructorCall(

Review comment:
       or even do everything in the `throwStmt`? It's just a suggestion to 
maybe have even more "clean" code here.




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