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



##########
File path: 
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/sink/ConstraintEnforcer.java
##########
@@ -227,22 +243,34 @@ public void processElement(StreamRecord<RowData> element) 
throws Exception {
 
         final RowData rowData = element.getValue();
 
-        boolean trimmed = false;
+        boolean trimmedOrPadded = false;
         for (int i = 0; i < charFieldIndices.length; i++) {
             final int fieldIdx = charFieldIndices[i];
             final int precision = charFieldPrecisions[i];
             final BinaryStringData stringData = (BinaryStringData) 
rowData.getString(fieldIdx);
+            final int sourceStrLength = stringData.numChars();
 
-            if (stringData.numChars() > precision) {
-                if (!trimmed) {
+            if (charFieldShouldPad.get(i) && sourceStrLength < precision) {
+                if (!trimmedOrPadded) {
+                    reusableRowData.setRow(rowData);
+                }
+                byte[] newString = new byte[precision];

Review comment:
       Thx @twalthr!
   
   I've run some JMH benchmarks using this code: 
https://gist.github.com/matriv/442719776e2b076ad54b2475b178c43a
   
   and decided to do a hybrid approach to avoid creating a `BinaryStringData` 
instance with the spaces but directly set the desired bytes to `32` (space 
character) on the target `byte[]`.




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