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



##########
File path: 
flink-table/flink-table-planner/src/main/java/org/apache/flink/table/planner/functions/casting/StringToBinaryCastRule.java
##########
@@ -45,6 +52,28 @@ public String generateExpression(
             String inputTerm,
             LogicalType inputLogicalType,
             LogicalType targetLogicalType) {
-        return methodCall(inputTerm, "toBytes");
+        int inputLength = Integer.MAX_VALUE;
+        if (inputLogicalType instanceof CharType) {
+            inputLength = ((CharType) inputLogicalType).getLength();
+        } else if (inputLogicalType instanceof VarCharType) {
+            inputLength = ((VarCharType) inputLogicalType).getLength();
+        }
+
+        int targetLength = Integer.MAX_VALUE;
+        if (targetLogicalType instanceof BinaryType) {
+            targetLength = ((BinaryType) targetLogicalType).getLength();
+        } else if (targetLogicalType instanceof VarBinaryType) {
+            targetLength = ((VarBinaryType) targetLogicalType).getLength();
+        }
+        targetLength = Math.min(targetLength, inputLength);

Review comment:
       I would remove that, and instead below have a check like:
   ```
   if (targetLength >= inputLength) {
       return methodCall(inputTerm, "toBytes");
   } else {
       return staticCall(
               Arrays.class, "copyOfRange", methodCall(inputTerm, "toBytes"), 
0, targetLength);
   }
   ```




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