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
which result in:
```
Benchmark Mode Cnt Score
Error Units
BinaryStringDataBenchmark.useBinaryStringDataUtils thrpt 30 2109.610 ±
38.216 ops/ms
BinaryStringDataBenchmark.useBytes thrpt 30 2910.367 ±
102.223 ops/ms
BinaryStringDataBenchmark.useBytesWithSegmentUtil thrpt 30 2970.679 ±
56.402 ops/ms
BinaryStringDataBenchmark.useString thrpt 30 262.300 ±
6.606 ops/ms
```
So, 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]