slinkydeveloper commented on a change in pull request #17658:
URL: https://github.com/apache/flink/pull/17658#discussion_r743592146
##########
File path:
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/casting/rules/ArrayToArrayCastRule.java
##########
@@ -68,63 +70,61 @@ protected String generateCodeBlockInternal(
String returnVariable,
LogicalType inputLogicalType,
LogicalType targetLogicalType) {
- LogicalType innerInputType = ((ArrayType)
inputLogicalType).getElementType();
- LogicalType innerTargetType = ((ArrayType)
targetLogicalType).getElementType();
-
- String innerTargetTypeTerm = arrayElementType(innerTargetType);
- String arraySize = inputTerm + ".size()";
- String objArrayTerm = newName("objArray");
-
- StringBuilder result = new StringBuilder();
- result.append(
- innerTargetTypeTerm
- + "[] "
- + objArrayTerm
- + " = new "
- + innerTargetTypeTerm
- + "["
- + arraySize
- + "];\n");
-
- result.append("for (int i = 0; i < " + arraySize + "; i++) {\n");
- CastCodeBlock codeBlock =
- CastRuleProvider.generateCodeBlock(
- context,
- rowFieldReadAccess("i", inputTerm, innerInputType),
- functionCall(inputTerm + ".isNullAt", "i"),
- innerInputType.copy(false), // Null check is done at
the array access level
- innerTargetType);
-
- String innerElementCode =
- codeBlock.getCode()
- + "\n"
- + objArrayTerm
- + "[i] = "
- + codeBlock.getReturnTerm()
- + ";\n";
+ final LogicalType innerInputType = ((ArrayType)
inputLogicalType).getElementType();
+ final LogicalType innerTargetType =
+ sanitizeTargetType((ArrayType) inputLogicalType, (ArrayType)
targetLogicalType);
+
+ final String innerTargetTypeTerm = arrayElementType(innerTargetType);
+ final String arraySize = methodCall(inputTerm, "size");
+ final String objArrayTerm = newName("objArray");
+
+ return new CastRuleUtils.CodeWriter()
+ .declStmt(
+ innerTargetTypeTerm + "[]",
+ objArrayTerm,
+ newArray(innerTargetTypeTerm, arraySize))
+ .forStmt(
+ arraySize,
+ (index, loopWriter) -> {
+ CastCodeBlock codeBlock =
+ CastRuleProvider.generateCodeBlock(
+ context,
+ rowFieldReadAccess(index,
inputTerm, innerInputType),
+ "false",
+ // Null check is done at the array
access level
+ innerInputType.copy(false),
+ innerTargetType);
+
+ if (innerTargetType.isNullable()) {
+ loopWriter.ifStmt(
+ "!" + functionCall(inputTerm +
".isNullAt", index),
+ thenWriter ->
+ thenWriter
+ .append(codeBlock)
+ .assignArrayStmt(
+ objArrayTerm,
+ index,
+
codeBlock.getReturnTerm()));
+ } else {
+ loopWriter
+ .append(codeBlock)
+ .assignArrayStmt(
+ objArrayTerm, index,
codeBlock.getReturnTerm());
+ }
+ })
+ .assignStmt(returnVariable,
constructorCall(GenericArrayData.class, objArrayTerm))
+ .toString();
+ }
- // Add null check if inner type is nullable
- if (innerInputType.isNullable()) {
- result.append("if (" + inputTerm + ".isNullAt(i)) {\n")
- .append(objArrayTerm + "[i] = null;\n")
- .append("} else {\n")
- .append(innerElementCode)
- .append("}\n");
- } else {
- result.append(innerElementCode);
+ private static LogicalType sanitizeTargetType(
+ ArrayType inputArrayType, ArrayType targetArrayType) {
+ LogicalType innerTargetType = targetArrayType.getElementType();
+ // TODO this seems rather a bug of the planner that
generates/allows/doesn't sanitize
Review comment:
Gotcha, I've modified the comment to explain why we need that logic.
--
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]