You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "snuyanzin (via GitHub)" <gi...@apache.org> on 2026/09/03 05:44:48 UTC
Re: [PR] [FLINK-40350][table-runtime] LPAD/RPAD split supplementary-plane characters into unpaired surrogates [flink]
snuyanzin commented on code in PR #29004:
URL: https://github.com/apache/flink/pull/29004#discussion_r3921449114
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/SqlFunctionUtils.java:
##########
@@ -345,29 +338,61 @@ public static String rpad(String base, int len, String pad) {
return "";
}
- char[] data = new char[len];
- char[] baseChars = base.toCharArray();
- char[] padChars = pad.toCharArray();
+ final int baseEnd = endOfCodePoints(base, len);
+ final int padCount = len - base.codePointCount(0, baseEnd);
+ if (padCount == 0) {
+ // The base already holds len code points, so it only needs truncating.
+ return base.substring(0, baseEnd);
+ }
+
+ final int padChars = padLength(pad, padCount);
+ final char[] data = new char[baseEnd + padChars];
+
+ base.getChars(0, baseEnd, data, 0);
+ writePad(data, baseEnd, pad, padChars);
- int pos = 0;
+ return new String(data);
+ }
- // copy the base
- while (pos < base.length() && pos < len) {
- data[pos] = baseChars[pos];
- pos += 1;
+ /** Index just past the first count code points of str, or its end if str holds fewer. */
+ private static int endOfCodePoints(String str, int count) {
+ final int length = str.length();
+ int index = 0;
+ for (int i = 0; i < count && index < length; i++) {
+ index += Character.charCount(str.codePointAt(index));
}
+ return index;
+ }
- // copy the padding
- while (pos < len) {
- int i = 0;
- while (i < pad.length() && i < len - pos) {
- data[pos + i] = padChars[i];
- i += 1;
- }
- pos += pad.length();
+ /** Number of chars taken by count code points of pad repeated cyclically. */
+ private static int padLength(String pad, int count) {
+ final int padLen = pad.length();
+ final int cycle = pad.codePointCount(0, padLen);
+ return (count / cycle) * padLen + pad.offsetByCodePoints(0, count % cycle);
+ }
+
+ /** Writes chars characters into data at pos, repeating pad cyclically. */
+ private static void writePad(char[] data, int pos, String pad, int chars) {
+ if (chars == 0) {
+ return;
}
- return new String(data);
+ final int padLen = pad.length();
+ if (padLen == 1) {
+ // A single char fills directly, which is faster than the doubling copy below.
Review Comment:
from this comment it is not clear why do we need doubling copy
need a separate line clarifying this (before the corresponding code)
--
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: issues-unsubscribe@flink.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org