use of org.apache.flink.table.data.binary.BinaryStringData in project flink by apache.
the class ConstraintEnforcer method processCharConstraint.
private RowData processCharConstraint(RowData rowData) {
if (typeLengthEnforcer == null || typeLengthEnforcer == TypeLengthEnforcer.IGNORE || charFieldIndices == null) {
return rowData;
}
UpdatableRowData updatedRowData = null;
for (int i = 0; i < charFieldIndices.length; i++) {
final int fieldIdx = charFieldIndices[i];
final int length = charFieldLengths[i];
final BinaryStringData stringData = (BinaryStringData) rowData.getString(fieldIdx);
final int sourceStrLength = stringData.numChars();
if (charFieldCouldPad.get(i) && sourceStrLength < length) {
if (updatedRowData == null) {
updatedRowData = new UpdatableRowData(rowData, allFieldNames.length);
}
final int srcSizeInBytes = stringData.getSizeInBytes();
final byte[] newString = new byte[srcSizeInBytes + length - sourceStrLength];
for (int j = srcSizeInBytes; j < newString.length; j++) {
// space
newString[j] = (byte) 32;
}
SegmentsUtil.copyToBytes(stringData.getSegments(), 0, newString, 0, srcSizeInBytes);
updatedRowData.setField(fieldIdx, StringData.fromBytes(newString));
} else if (sourceStrLength > length) {
if (updatedRowData == null) {
updatedRowData = new UpdatableRowData(rowData, allFieldNames.length);
}
updatedRowData.setField(fieldIdx, stringData.substring(0, length));
}
}
return updatedRowData != null ? updatedRowData : rowData;
}
use of org.apache.flink.table.data.binary.BinaryStringData in project flink by apache.
the class SortUtil method putStringNormalizedKey.
/**
* UTF-8 supports bytes comparison.
*/
public static void putStringNormalizedKey(StringData value, MemorySegment target, int offset, int numBytes) {
BinaryStringData binaryString = (BinaryStringData) value;
final int limit = offset + numBytes;
final int end = binaryString.getSizeInBytes();
for (int i = 0; i < end && offset < limit; i++) {
target.put(offset++, binaryString.byteAt(i));
}
for (int i = offset; i < limit; i++) {
target.put(i, (byte) 0);
}
}
use of org.apache.flink.table.data.binary.BinaryStringData in project flink by apache.
the class AbstractBinaryWriter method writeString.
/**
* See {@link BinarySegmentUtils#readStringData(MemorySegment[], int, int, long)}.
*/
@Override
public void writeString(int pos, StringData input) {
BinaryStringData string = (BinaryStringData) input;
if (string.getSegments() == null) {
String javaObject = string.toString();
writeBytes(pos, javaObject.getBytes(StandardCharsets.UTF_8));
} else {
int len = string.getSizeInBytes();
if (len <= 7) {
byte[] bytes = BinarySegmentUtils.allocateReuseBytes(len);
BinarySegmentUtils.copyToBytes(string.getSegments(), string.getOffset(), bytes, 0, len);
writeBytesToFixLenPart(segment, getFieldOffset(pos), bytes, len);
} else {
writeSegmentsToVarLenPart(pos, string.getSegments(), string.getOffset(), len);
}
}
}
use of org.apache.flink.table.data.binary.BinaryStringData in project flink by apache.
the class BinaryRowDataTest method testSerStringToKryo.
@Test
public void testSerStringToKryo() throws IOException {
KryoSerializer<BinaryStringData> serializer = new KryoSerializer<>(BinaryStringData.class, new ExecutionConfig());
BinaryStringData string = BinaryStringData.fromString("hahahahaha");
RandomAccessOutputView out = new RandomAccessOutputView(new MemorySegment[] { MemorySegmentFactory.wrap(new byte[1024]) }, 64);
serializer.serialize(string, out);
RandomAccessInputView input = new RandomAccessInputView(new ArrayList<>(Collections.singletonList(out.getCurrentSegment())), 64, 64);
StringData newStr = serializer.deserialize(input);
assertEquals(string, newStr);
}
use of org.apache.flink.table.data.binary.BinaryStringData in project flink by apache.
the class BinaryStringDataTest method fromString.
private BinaryStringData fromString(String str) {
BinaryStringData string = BinaryStringData.fromString(str);
Mode mode = this.mode;
if (mode == Mode.RANDOM) {
int rnd = new Random().nextInt(3);
if (rnd == 0) {
mode = Mode.ONE_SEG;
} else if (rnd == 1) {
mode = Mode.MULTI_SEGS;
} else if (rnd == 2) {
mode = Mode.STRING;
}
}
if (mode == Mode.STRING) {
return string;
}
if (mode == Mode.ONE_SEG || string.getSizeInBytes() < 2) {
string.ensureMaterialized();
return string;
} else {
int numBytes = string.getSizeInBytes();
int pad = new Random().nextInt(5);
int numBytesWithPad = numBytes + pad;
int segSize = numBytesWithPad / 2 + 1;
byte[] bytes1 = new byte[segSize];
byte[] bytes2 = new byte[segSize];
if (segSize - pad > 0 && numBytes >= segSize - pad) {
string.getSegments()[0].get(0, bytes1, pad, segSize - pad);
}
string.getSegments()[0].get(segSize - pad, bytes2, 0, numBytes - segSize + pad);
return BinaryStringData.fromAddress(new MemorySegment[] { MemorySegmentFactory.wrap(bytes1), MemorySegmentFactory.wrap(bytes2) }, pad, numBytes);
}
}
Aggregations