SEPURI-SAI-KRISHNA commented on code in PR #29004:
URL: https://github.com/apache/flink/pull/29004#discussion_r3837871162
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/SqlFunctionUtils.java:
##########
@@ -345,29 +331,39 @@ public static String rpad(String base, int len, String
pad) {
return "";
}
- char[] data = new char[len];
- char[] baseChars = base.toCharArray();
- char[] padChars = pad.toCharArray();
+ int baseEnd = endOfCodePoints(base, len);
+ int padChars = padLength(pad, len - base.codePointCount(0, baseEnd));
+ 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) {
+ int index = 0;
+ for (int i = 0; i < count && index < str.length(); i++) {
Review Comment:
Done.
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/SqlFunctionUtils.java:
##########
@@ -345,29 +331,39 @@ public static String rpad(String base, int len, String
pad) {
return "";
}
- char[] data = new char[len];
- char[] baseChars = base.toCharArray();
- char[] padChars = pad.toCharArray();
+ int baseEnd = endOfCodePoints(base, len);
+ int padChars = padLength(pad, len - base.codePointCount(0, baseEnd));
+ 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) {
+ int index = 0;
+ for (int i = 0; i < count && index < str.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;
- }
+ /** Number of chars taken by count code points of pad repeated cyclically.
*/
+ private static int padLength(String pad, int count) {
+ int cycle = pad.codePointCount(0, pad.length());
+ return (count / cycle) * pad.length() + endOfCodePoints(pad, count %
cycle);
+ }
+
+ /** Writes chars characters into data at pos, repeating pad cyclically. */
+ private static void writePad(char[] data, int pos, String pad, int chars) {
+ int end = pos + chars;
Review Comment:
Done.
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/SqlFunctionUtils.java:
##########
@@ -345,29 +331,39 @@ public static String rpad(String base, int len, String
pad) {
return "";
}
- char[] data = new char[len];
- char[] baseChars = base.toCharArray();
- char[] padChars = pad.toCharArray();
+ int baseEnd = endOfCodePoints(base, len);
+ int padChars = padLength(pad, len - base.codePointCount(0, baseEnd));
+ 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) {
+ int index = 0;
+ for (int i = 0; i < count && index < str.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;
- }
+ /** Number of chars taken by count code points of pad repeated cyclically.
*/
+ private static int padLength(String pad, int count) {
+ int cycle = pad.codePointCount(0, pad.length());
+ return (count / cycle) * pad.length() + endOfCodePoints(pad, count %
cycle);
+ }
+
+ /** Writes chars characters into data at pos, repeating pad cyclically. */
+ private static void writePad(char[] data, int pos, String pad, int chars) {
+ int end = pos + chars;
+ while (end - pos >= pad.length()) {
Review Comment:
Done.
##########
flink-table/flink-table-planner/src/test/scala/org/apache/flink/table/planner/expressions/ScalarFunctionsTest.scala:
##########
@@ -824,6 +824,12 @@ class ScalarFunctionsTest extends ScalarTypesTestBase {
testSqlApi("lpad('ab', 5, '')", "NULL")
testAllApis("äää".lpad(13, "12345"), "lpad('äää',13,'12345')",
"1234512345äää")
+
+ // supplementary-plane characters count as one character and must not be
split
+ testSqlApi("lpad('😀', 1, 'x')", "😀")
+ testSqlApi("lpad('😀', 3, 'x')", "xx😀")
+ testSqlApi("lpad('😀😀', 1, 'x')", "😀")
+ testSqlApi("lpad('a', 3, '😀')", "😀😀a")
Review Comment:
Removed, kept only `StringFunctionsITCase`.
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/SqlFunctionUtils.java:
##########
@@ -310,26 +310,12 @@ public static String lpad(String base, int len, String
pad) {
return "";
}
- char[] data = new char[len];
- char[] baseChars = base.toCharArray();
- char[] padChars = pad.toCharArray();
+ int baseEnd = endOfCodePoints(base, len);
+ int padChars = padLength(pad, len - base.codePointCount(0, baseEnd));
+ char[] data = new char[padChars + baseEnd];
Review Comment:
Done. Two optional follow-ups here, want either?
1. `base.codePointCount(0, baseEnd)` re-walks what `endOfCodePoints` just
walked; folding it in means inlining the loop in both methods.
2. when `padChars == 0`, `base.substring(0, baseEnd)` skips a copy.
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/SqlFunctionUtils.java:
##########
@@ -345,29 +331,39 @@ public static String rpad(String base, int len, String
pad) {
return "";
}
- char[] data = new char[len];
- char[] baseChars = base.toCharArray();
- char[] padChars = pad.toCharArray();
+ int baseEnd = endOfCodePoints(base, len);
+ int padChars = padLength(pad, len - base.codePointCount(0, baseEnd));
+ char[] data = new char[baseEnd + padChars];
Review Comment:
Done.
##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/functions/SqlFunctionUtils.java:
##########
@@ -345,29 +331,39 @@ public static String rpad(String base, int len, String
pad) {
return "";
}
- char[] data = new char[len];
- char[] baseChars = base.toCharArray();
- char[] padChars = pad.toCharArray();
+ int baseEnd = endOfCodePoints(base, len);
+ int padChars = padLength(pad, len - base.codePointCount(0, baseEnd));
+ 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) {
+ int index = 0;
+ for (int i = 0; i < count && index < str.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;
- }
+ /** Number of chars taken by count code points of pad repeated cyclically.
*/
+ private static int padLength(String pad, int count) {
+ int cycle = pad.codePointCount(0, pad.length());
Review Comment:
Done.
--
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]