Search in sources :

Example 61 with BinaryRowData

use of org.apache.flink.table.data.binary.BinaryRowData in project flink by apache.

the class BinaryRowDataTest method testSetAndGet.

@Test
public void testSetAndGet() {
    MemorySegment segment = MemorySegmentFactory.wrap(new byte[80]);
    BinaryRowData row = new BinaryRowData(9);
    row.pointTo(segment, 0, 80);
    row.setNullAt(0);
    row.setInt(1, 11);
    row.setLong(2, 22);
    row.setDouble(3, 33);
    row.setBoolean(4, true);
    row.setShort(5, (short) 55);
    row.setByte(6, (byte) 66);
    row.setFloat(7, 77f);
    assertEquals(33d, (long) row.getDouble(3), 0);
    assertEquals(11, row.getInt(1));
    assertTrue(row.isNullAt(0));
    assertEquals(55, row.getShort(5));
    assertEquals(22, row.getLong(2));
    assertTrue(row.getBoolean(4));
    assertEquals((byte) 66, row.getByte(6));
    assertEquals(77f, row.getFloat(7), 0);
}
Also used : BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) MemorySegment(org.apache.flink.core.memory.MemorySegment) Test(org.junit.Test)

Example 62 with BinaryRowData

use of org.apache.flink.table.data.binary.BinaryRowData in project flink by apache.

the class BinaryRowDataTest method testGenericMap.

@Test
public void testGenericMap() {
    Map<Object, Object> javaMap = new HashMap<>();
    javaMap.put(6, fromString("6"));
    javaMap.put(5, fromString("5"));
    javaMap.put(666, fromString("666"));
    javaMap.put(0, null);
    GenericMapData genericMap = new GenericMapData(javaMap);
    BinaryRowData row = new BinaryRowData(1);
    BinaryRowWriter rowWriter = new BinaryRowWriter(row);
    MapDataSerializer serializer = new MapDataSerializer(DataTypes.INT().getLogicalType(), DataTypes.STRING().getLogicalType());
    rowWriter.writeMap(0, genericMap, serializer);
    rowWriter.complete();
    Map<Object, Object> map = convertToJavaMap(row.getMap(0), DataTypes.INT().getLogicalType(), DataTypes.STRING().getLogicalType());
    assertEquals(fromString("6"), map.get(6));
    assertEquals(fromString("5"), map.get(5));
    assertEquals(fromString("666"), map.get(666));
    assertTrue(map.containsKey(0));
    assertNull(map.get(0));
}
Also used : HashMap(java.util.HashMap) MapDataSerializer(org.apache.flink.table.runtime.typeutils.MapDataSerializer) BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) BinaryRowWriter(org.apache.flink.table.data.writer.BinaryRowWriter) Test(org.junit.Test)

Example 63 with BinaryRowData

use of org.apache.flink.table.data.binary.BinaryRowData in project flink by apache.

the class BinaryRowDataTest method testSerializeVariousSize.

@Test
public void testSerializeVariousSize() throws IOException {
    // in this test, we are going to start serializing from the i-th byte (i in 0...`segSize`)
    // and the size of the row we're going to serialize is j bytes
    // (j in `rowFixLength` to the maximum length we can write)
    int segSize = 64;
    int segTotalNumber = 3;
    BinaryRowData row = new BinaryRowData(1);
    BinaryRowWriter writer = new BinaryRowWriter(row);
    Random random = new Random();
    byte[] bytes = new byte[1024];
    random.nextBytes(bytes);
    writer.writeBinary(0, bytes);
    writer.complete();
    MemorySegment[] memorySegments = new MemorySegment[segTotalNumber];
    Map<MemorySegment, Integer> msIndex = new HashMap<>();
    for (int i = 0; i < segTotalNumber; i++) {
        memorySegments[i] = MemorySegmentFactory.wrap(new byte[segSize]);
        msIndex.put(memorySegments[i], i);
    }
    BinaryRowDataSerializer serializer = new BinaryRowDataSerializer(1);
    int rowSizeInt = 4;
    // note that as there is only one field in the row, the fixed-length part is 16 bytes
    // (header + 1 field)
    int rowFixLength = 16;
    for (int i = 0; i < segSize; i++) {
        // this is the maximum row size we can serialize
        // if we are going to serialize from the i-th byte of the input view
        int maxRowSize = (segSize * segTotalNumber) - i - rowSizeInt;
        if (segSize - i < rowFixLength + rowSizeInt) {
            // oops, we can't write the whole fixed-length part in the first segment
            // because the remaining space is too small, so we have to start serializing from
            // the second segment.
            // when serializing, we need to first write the length of the row,
            // then write the fixed-length part of the row.
            maxRowSize -= segSize - i;
        }
        for (int j = rowFixLength; j < maxRowSize; j++) {
            // ok, now we're going to serialize a row of j bytes
            testSerialize(row, memorySegments, msIndex, serializer, i, j);
        }
    }
}
Also used : Random(java.util.Random) HashMap(java.util.HashMap) BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) BinaryRowWriter(org.apache.flink.table.data.writer.BinaryRowWriter) MemorySegment(org.apache.flink.core.memory.MemorySegment) BinaryRowDataSerializer(org.apache.flink.table.runtime.typeutils.BinaryRowDataSerializer) Test(org.junit.Test)

Example 64 with BinaryRowData

use of org.apache.flink.table.data.binary.BinaryRowData in project flink by apache.

the class BinaryRowDataTest method testHeader.

@Test
public void testHeader() {
    BinaryRowData row = new BinaryRowData(2);
    BinaryRowWriter writer = new BinaryRowWriter(row);
    writer.writeInt(0, 10);
    writer.setNullAt(1);
    writer.writeRowKind(RowKind.UPDATE_BEFORE);
    writer.complete();
    BinaryRowData newRow = row.copy();
    assertEquals(row, newRow);
    assertEquals(RowKind.UPDATE_BEFORE, newRow.getRowKind());
    newRow.setRowKind(RowKind.DELETE);
    assertEquals(RowKind.DELETE, newRow.getRowKind());
}
Also used : BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) BinaryRowWriter(org.apache.flink.table.data.writer.BinaryRowWriter) Test(org.junit.Test)

Example 65 with BinaryRowData

use of org.apache.flink.table.data.binary.BinaryRowData in project flink by apache.

the class BinaryRowDataTest method testSerializerPages.

@Test
public void testSerializerPages() throws IOException {
    // Boundary tests
    BinaryRowData row24 = DataFormatTestUtil.get24BytesBinaryRow();
    BinaryRowData row160 = DataFormatTestUtil.get160BytesBinaryRow();
    testSerializerPagesInternal(row24, row160);
    testSerializerPagesInternal(row24, DataFormatTestUtil.getMultiSeg160BytesBinaryRow(row160));
}
Also used : BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) Test(org.junit.Test)

Aggregations

BinaryRowData (org.apache.flink.table.data.binary.BinaryRowData)173 Test (org.junit.Test)81 BinaryRowWriter (org.apache.flink.table.data.writer.BinaryRowWriter)54 RowData (org.apache.flink.table.data.RowData)31 ArrayList (java.util.ArrayList)30 MemoryManager (org.apache.flink.runtime.memory.MemoryManager)22 UniformBinaryRowGenerator (org.apache.flink.table.runtime.util.UniformBinaryRowGenerator)21 JoinedRowData (org.apache.flink.table.data.utils.JoinedRowData)16 MemorySegment (org.apache.flink.core.memory.MemorySegment)15 MutableObjectIterator (org.apache.flink.util.MutableObjectIterator)14 GenericRowData (org.apache.flink.table.data.GenericRowData)13 Random (java.util.Random)12 BinaryRowDataSerializer (org.apache.flink.table.runtime.typeutils.BinaryRowDataSerializer)12 HashMap (java.util.HashMap)9 RowDataSerializer (org.apache.flink.table.runtime.typeutils.RowDataSerializer)9 Map (java.util.Map)7 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)7 StreamOperator (org.apache.flink.streaming.api.operators.StreamOperator)7 RandomAccessInputView (org.apache.flink.runtime.io.disk.RandomAccessInputView)6 StreamRecord (org.apache.flink.streaming.runtime.streamrecord.StreamRecord)6