Search in sources :

Example 36 with BinaryRowWriter

use of org.apache.flink.table.data.writer.BinaryRowWriter in project flink by apache.

the class BinaryRowDataTest method anyNullTest.

@Test
public void anyNullTest() {
    {
        BinaryRowData row = new BinaryRowData(3);
        BinaryRowWriter writer = new BinaryRowWriter(row);
        assertFalse(row.anyNull());
        // test header should not compute by anyNull
        row.setRowKind(RowKind.UPDATE_BEFORE);
        assertFalse(row.anyNull());
        writer.setNullAt(2);
        assertTrue(row.anyNull());
        writer.setNullAt(0);
        assertTrue(row.anyNull(new int[] { 0, 1, 2 }));
        assertFalse(row.anyNull(new int[] { 1 }));
        writer.setNullAt(1);
        assertTrue(row.anyNull());
    }
    int numFields = 80;
    for (int i = 0; i < numFields; i++) {
        BinaryRowData row = new BinaryRowData(numFields);
        BinaryRowWriter writer = new BinaryRowWriter(row);
        row.setRowKind(RowKind.DELETE);
        assertFalse(row.anyNull());
        writer.setNullAt(i);
        assertTrue(row.anyNull());
    }
}
Also used : BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) BinaryRowWriter(org.apache.flink.table.data.writer.BinaryRowWriter) Test(org.junit.Test)

Example 37 with BinaryRowWriter

use of org.apache.flink.table.data.writer.BinaryRowWriter in project flink by apache.

the class BinaryRowDataTest method testBinaryArray.

@Test
public void testBinaryArray() {
    // 1. array test
    BinaryArrayData array = new BinaryArrayData();
    BinaryArrayWriter arrayWriter = new BinaryArrayWriter(array, 3, BinaryArrayData.calculateFixLengthPartSize(DataTypes.INT().getLogicalType()));
    arrayWriter.writeInt(0, 6);
    arrayWriter.setNullInt(1);
    arrayWriter.writeInt(2, 666);
    arrayWriter.complete();
    assertEquals(array.getInt(0), 6);
    assertTrue(array.isNullAt(1));
    assertEquals(array.getInt(2), 666);
    // 2. test write array to binary row
    BinaryRowData row = new BinaryRowData(1);
    BinaryRowWriter rowWriter = new BinaryRowWriter(row);
    ArrayDataSerializer serializer = new ArrayDataSerializer(DataTypes.INT().getLogicalType());
    rowWriter.writeArray(0, array, serializer);
    rowWriter.complete();
    BinaryArrayData array2 = (BinaryArrayData) row.getArray(0);
    assertEquals(array, array2);
    assertEquals(6, array2.getInt(0));
    assertTrue(array2.isNullAt(1));
    assertEquals(666, array2.getInt(2));
}
Also used : BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) BinaryRowWriter(org.apache.flink.table.data.writer.BinaryRowWriter) BinaryArrayWriter(org.apache.flink.table.data.writer.BinaryArrayWriter) BinaryArrayData(org.apache.flink.table.data.binary.BinaryArrayData) ArrayDataSerializer(org.apache.flink.table.runtime.typeutils.ArrayDataSerializer) Test(org.junit.Test)

Example 38 with BinaryRowWriter

use of org.apache.flink.table.data.writer.BinaryRowWriter in project flink by apache.

the class BinaryRowDataTest method testGenericArray.

@Test
public void testGenericArray() {
    // 1. array test
    Integer[] javaArray = { 6, null, 666 };
    GenericArrayData array = new GenericArrayData(javaArray);
    assertEquals(array.getInt(0), 6);
    assertTrue(array.isNullAt(1));
    assertEquals(array.getInt(2), 666);
    // 2. test write array to binary row
    BinaryRowData row2 = new BinaryRowData(1);
    BinaryRowWriter writer2 = new BinaryRowWriter(row2);
    ArrayDataSerializer serializer = new ArrayDataSerializer(DataTypes.INT().getLogicalType());
    writer2.writeArray(0, array, serializer);
    writer2.complete();
    ArrayData array2 = row2.getArray(0);
    assertEquals(6, array2.getInt(0));
    assertTrue(array2.isNullAt(1));
    assertEquals(666, array2.getInt(2));
}
Also used : BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) BinaryRowWriter(org.apache.flink.table.data.writer.BinaryRowWriter) ArrayDataSerializer(org.apache.flink.table.runtime.typeutils.ArrayDataSerializer) BinaryArrayData(org.apache.flink.table.data.binary.BinaryArrayData) Test(org.junit.Test)

Example 39 with BinaryRowWriter

use of org.apache.flink.table.data.writer.BinaryRowWriter in project flink by apache.

the class BinaryRowDataTest method testPagesSer.

@Test
public void testPagesSer() throws IOException {
    MemorySegment[] memorySegments = new MemorySegment[5];
    ArrayList<MemorySegment> memorySegmentList = new ArrayList<>();
    for (int i = 0; i < 5; i++) {
        memorySegments[i] = MemorySegmentFactory.wrap(new byte[64]);
        memorySegmentList.add(memorySegments[i]);
    }
    {
        // multi memorySegments
        String str = "啦啦啦啦啦我是快乐的粉刷匠,啦啦啦啦啦我是快乐的粉刷匠," + "啦啦啦啦啦我是快乐的粉刷匠。";
        BinaryRowData row = new BinaryRowData(1);
        BinaryRowWriter writer = new BinaryRowWriter(row);
        writer.writeString(0, fromString(str));
        writer.complete();
        RandomAccessOutputView out = new RandomAccessOutputView(memorySegments, 64);
        BinaryRowDataSerializer serializer = new BinaryRowDataSerializer(1);
        serializer.serializeToPages(row, out);
        BinaryRowData mapRow = serializer.createInstance();
        mapRow = serializer.mapFromPages(mapRow, new RandomAccessInputView(memorySegmentList, 64));
        writer.reset();
        writer.writeString(0, mapRow.getString(0));
        writer.complete();
        assertEquals(str, row.getString(0).toString());
        BinaryRowData deserRow = serializer.deserializeFromPages(new RandomAccessInputView(memorySegmentList, 64));
        writer.reset();
        writer.writeString(0, deserRow.getString(0));
        writer.complete();
        assertEquals(str, row.getString(0).toString());
    }
    {
        // multi memorySegments
        String str1 = "啦啦啦啦啦我是快乐的粉刷匠,啦啦啦啦啦我是快乐的粉刷匠," + "啦啦啦啦啦我是快乐的粉刷匠。";
        String str2 = "啦啦啦啦啦我是快乐的粉刷匠。";
        BinaryRowData row = new BinaryRowData(2);
        BinaryRowWriter writer = new BinaryRowWriter(row);
        writer.writeString(0, fromString(str1));
        writer.writeString(1, fromString(str2));
        writer.complete();
        RandomAccessOutputView out = new RandomAccessOutputView(memorySegments, 64);
        out.skipBytesToWrite(40);
        BinaryRowDataSerializer serializer = new BinaryRowDataSerializer(2);
        serializer.serializeToPages(row, out);
        RandomAccessInputView in = new RandomAccessInputView(memorySegmentList, 64);
        in.skipBytesToRead(40);
        BinaryRowData mapRow = serializer.createInstance();
        mapRow = serializer.mapFromPages(mapRow, in);
        writer.reset();
        writer.writeString(0, mapRow.getString(0));
        writer.writeString(1, mapRow.getString(1));
        writer.complete();
        assertEquals(str1, row.getString(0).toString());
        assertEquals(str2, row.getString(1).toString());
        in = new RandomAccessInputView(memorySegmentList, 64);
        in.skipBytesToRead(40);
        BinaryRowData deserRow = serializer.deserializeFromPages(in);
        writer.reset();
        writer.writeString(0, deserRow.getString(0));
        writer.writeString(1, deserRow.getString(1));
        writer.complete();
        assertEquals(str1, row.getString(0).toString());
        assertEquals(str2, row.getString(1).toString());
    }
}
Also used : RandomAccessInputView(org.apache.flink.runtime.io.disk.RandomAccessInputView) ArrayList(java.util.ArrayList) BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) BinaryRowWriter(org.apache.flink.table.data.writer.BinaryRowWriter) StringData.fromString(org.apache.flink.table.data.StringData.fromString) RandomAccessOutputView(org.apache.flink.runtime.io.disk.RandomAccessOutputView) MemorySegment(org.apache.flink.core.memory.MemorySegment) BinaryRowDataSerializer(org.apache.flink.table.runtime.typeutils.BinaryRowDataSerializer) Test(org.junit.Test)

Example 40 with BinaryRowWriter

use of org.apache.flink.table.data.writer.BinaryRowWriter in project flink by apache.

the class BinaryRowDataTest method testBinaryMap.

@Test
public void testBinaryMap() {
    BinaryArrayData array1 = new BinaryArrayData();
    BinaryArrayWriter writer1 = new BinaryArrayWriter(array1, 4, BinaryArrayData.calculateFixLengthPartSize(DataTypes.INT().getLogicalType()));
    writer1.writeInt(0, 6);
    writer1.writeInt(1, 5);
    writer1.writeInt(2, 666);
    writer1.writeInt(3, 0);
    writer1.complete();
    BinaryArrayData array2 = new BinaryArrayData();
    BinaryArrayWriter writer2 = new BinaryArrayWriter(array2, 4, BinaryArrayData.calculateFixLengthPartSize(DataTypes.STRING().getLogicalType()));
    writer2.writeString(0, fromString("6"));
    writer2.writeString(1, fromString("5"));
    writer2.writeString(2, fromString("666"));
    writer2.setNullAt(3, DataTypes.STRING().getLogicalType());
    writer2.complete();
    BinaryMapData binaryMap = BinaryMapData.valueOf(array1, array2);
    BinaryRowData row = new BinaryRowData(1);
    BinaryRowWriter rowWriter = new BinaryRowWriter(row);
    MapDataSerializer serializer = new MapDataSerializer(DataTypes.STRING().getLogicalType(), DataTypes.INT().getLogicalType());
    rowWriter.writeMap(0, binaryMap, serializer);
    rowWriter.complete();
    BinaryMapData map = (BinaryMapData) row.getMap(0);
    BinaryArrayData key = map.keyArray();
    BinaryArrayData value = map.valueArray();
    assertEquals(binaryMap, map);
    assertEquals(array1, key);
    assertEquals(array2, value);
    assertEquals(5, key.getInt(1));
    assertEquals(fromString("5"), value.getString(1));
    assertEquals(0, key.getInt(3));
    assertTrue(value.isNullAt(3));
}
Also used : BinaryMapData(org.apache.flink.table.data.binary.BinaryMapData) MapDataSerializer(org.apache.flink.table.runtime.typeutils.MapDataSerializer) BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) BinaryRowWriter(org.apache.flink.table.data.writer.BinaryRowWriter) BinaryArrayWriter(org.apache.flink.table.data.writer.BinaryArrayWriter) BinaryArrayData(org.apache.flink.table.data.binary.BinaryArrayData) Test(org.junit.Test)

Aggregations

BinaryRowWriter (org.apache.flink.table.data.writer.BinaryRowWriter)54 BinaryRowData (org.apache.flink.table.data.binary.BinaryRowData)53 Test (org.junit.Test)32 Random (java.util.Random)6 BinaryArrayData (org.apache.flink.table.data.binary.BinaryArrayData)6 BinaryRowDataSerializer (org.apache.flink.table.runtime.typeutils.BinaryRowDataSerializer)6 RowDataSerializer (org.apache.flink.table.runtime.typeutils.RowDataSerializer)6 MemorySegment (org.apache.flink.core.memory.MemorySegment)5 BinaryArrayWriter (org.apache.flink.table.data.writer.BinaryArrayWriter)5 ArrayDataSerializer (org.apache.flink.table.runtime.typeutils.ArrayDataSerializer)5 ArrayList (java.util.ArrayList)4 GenericRowData (org.apache.flink.table.data.GenericRowData)4 MapDataSerializer (org.apache.flink.table.runtime.typeutils.MapDataSerializer)4 LogicalType (org.apache.flink.table.types.logical.LogicalType)4 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)3 StringData.fromString (org.apache.flink.table.data.StringData.fromString)3 TimestampData (org.apache.flink.table.data.TimestampData)3 RawValueDataSerializer (org.apache.flink.table.runtime.typeutils.RawValueDataSerializer)3 BigDecimal (java.math.BigDecimal)2 HashMap (java.util.HashMap)2