Search in sources :

Example 1 with BinaryStringData

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;
}
Also used : UpdatableRowData(org.apache.flink.table.data.UpdatableRowData) BinaryStringData(org.apache.flink.table.data.binary.BinaryStringData)

Example 2 with BinaryStringData

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);
    }
}
Also used : BinaryStringData(org.apache.flink.table.data.binary.BinaryStringData)

Example 3 with BinaryStringData

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);
        }
    }
}
Also used : BinaryStringData(org.apache.flink.table.data.binary.BinaryStringData)

Example 4 with BinaryStringData

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);
}
Also used : RandomAccessInputView(org.apache.flink.runtime.io.disk.RandomAccessInputView) BinaryStringData(org.apache.flink.table.data.binary.BinaryStringData) ExecutionConfig(org.apache.flink.api.common.ExecutionConfig) BinaryStringData(org.apache.flink.table.data.binary.BinaryStringData) RandomAccessOutputView(org.apache.flink.runtime.io.disk.RandomAccessOutputView) KryoSerializer(org.apache.flink.api.java.typeutils.runtime.kryo.KryoSerializer) Test(org.junit.Test)

Example 5 with BinaryStringData

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);
    }
}
Also used : Random(java.util.Random) BinaryStringData(org.apache.flink.table.data.binary.BinaryStringData)

Aggregations

BinaryStringData (org.apache.flink.table.data.binary.BinaryStringData)15 Test (org.junit.Test)7 BigDecimal (java.math.BigDecimal)2 Random (java.util.Random)2 MemorySegment (org.apache.flink.core.memory.MemorySegment)2 GenericRowData (org.apache.flink.table.data.GenericRowData)2 BinaryStringData.blankString (org.apache.flink.table.data.binary.BinaryStringData.blankString)2 Timestamp (java.sql.Timestamp)1 Arrays (java.util.Arrays)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)1 KryoSerializer (org.apache.flink.api.java.typeutils.runtime.kryo.KryoSerializer)1 MemorySegmentFactory (org.apache.flink.core.memory.MemorySegmentFactory)1 RandomAccessInputView (org.apache.flink.runtime.io.disk.RandomAccessInputView)1 RandomAccessOutputView (org.apache.flink.runtime.io.disk.RandomAccessOutputView)1 DecimalData (org.apache.flink.table.data.DecimalData)1 GenericArrayData (org.apache.flink.table.data.GenericArrayData)1 GenericMapData (org.apache.flink.table.data.GenericMapData)1 StringData (org.apache.flink.table.data.StringData)1