Search in sources :

Example 1 with MapDataSerializer

use of org.apache.flink.table.runtime.typeutils.MapDataSerializer in project flink by apache.

the class RowDataTest method getBinaryRow.

private BinaryRowData getBinaryRow() {
    BinaryRowData row = new BinaryRowData(18);
    BinaryRowWriter writer = new BinaryRowWriter(row);
    writer.writeBoolean(0, true);
    writer.writeByte(1, (byte) 1);
    writer.writeShort(2, (short) 2);
    writer.writeInt(3, 3);
    writer.writeLong(4, 4);
    writer.writeFloat(5, 5);
    writer.writeDouble(6, 6);
    writer.writeString(8, str);
    writer.writeRawValue(9, generic, genericSerializer);
    writer.writeDecimal(10, decimal1, 5);
    writer.writeDecimal(11, decimal2, 20);
    writer.writeArray(12, array, new ArrayDataSerializer(DataTypes.INT().getLogicalType()));
    writer.writeMap(13, map, new MapDataSerializer(DataTypes.INT().getLogicalType(), DataTypes.INT().getLogicalType()));
    writer.writeRow(14, underRow, new RowDataSerializer(RowType.of(new IntType(), new IntType())));
    writer.writeBinary(15, bytes);
    writer.writeTimestamp(16, timestamp1, 3);
    writer.writeTimestamp(17, timestamp2, 9);
    return row;
}
Also used : MapDataSerializer(org.apache.flink.table.runtime.typeutils.MapDataSerializer) BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) BinaryRowWriter(org.apache.flink.table.data.writer.BinaryRowWriter) ArrayDataSerializer(org.apache.flink.table.runtime.typeutils.ArrayDataSerializer) RowDataSerializer(org.apache.flink.table.runtime.typeutils.RowDataSerializer) IntType(org.apache.flink.table.types.logical.IntType)

Example 2 with MapDataSerializer

use of org.apache.flink.table.runtime.typeutils.MapDataSerializer 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 3 with MapDataSerializer

use of org.apache.flink.table.runtime.typeutils.MapDataSerializer in project flink by apache.

the class BinaryArrayDataTest method testArrayTypes.

@Test
public void testArrayTypes() {
    {
        // test bool
        BinaryArrayData array = new BinaryArrayData();
        BinaryArrayWriter writer = new BinaryArrayWriter(array, 2, 1);
        writer.setNullBoolean(0);
        writer.writeBoolean(1, true);
        writer.complete();
        assertTrue(array.isNullAt(0));
        assertTrue(array.getBoolean(1));
        array.setBoolean(0, true);
        assertTrue(array.getBoolean(0));
        array.setNullBoolean(0);
        assertTrue(array.isNullAt(0));
        BinaryArrayData newArray = splitArray(array);
        assertTrue(newArray.isNullAt(0));
        assertTrue(newArray.getBoolean(1));
        newArray.setBoolean(0, true);
        assertTrue(newArray.getBoolean(0));
        newArray.setNullBoolean(0);
        assertTrue(newArray.isNullAt(0));
        newArray.setBoolean(0, true);
        assertEquals(newArray, BinaryArrayData.fromPrimitiveArray(newArray.toBooleanArray()));
    }
    {
        // test byte
        BinaryArrayData array = new BinaryArrayData();
        BinaryArrayWriter writer = new BinaryArrayWriter(array, 2, 1);
        writer.setNullByte(0);
        writer.writeByte(1, (byte) 25);
        writer.complete();
        assertTrue(array.isNullAt(0));
        assertEquals(25, array.getByte(1));
        array.setByte(0, (byte) 5);
        assertEquals(5, array.getByte(0));
        array.setNullByte(0);
        assertTrue(array.isNullAt(0));
        BinaryArrayData newArray = splitArray(array);
        assertTrue(newArray.isNullAt(0));
        assertEquals(25, newArray.getByte(1));
        newArray.setByte(0, (byte) 5);
        assertEquals(5, newArray.getByte(0));
        newArray.setNullByte(0);
        assertTrue(newArray.isNullAt(0));
        newArray.setByte(0, (byte) 3);
        assertEquals(newArray, BinaryArrayData.fromPrimitiveArray(newArray.toByteArray()));
    }
    {
        // test short
        BinaryArrayData array = new BinaryArrayData();
        BinaryArrayWriter writer = new BinaryArrayWriter(array, 2, 2);
        writer.setNullShort(0);
        writer.writeShort(1, (short) 25);
        writer.complete();
        assertTrue(array.isNullAt(0));
        assertEquals(25, array.getShort(1));
        array.setShort(0, (short) 5);
        assertEquals(5, array.getShort(0));
        array.setNullShort(0);
        assertTrue(array.isNullAt(0));
        BinaryArrayData newArray = splitArray(array);
        assertTrue(newArray.isNullAt(0));
        assertEquals(25, newArray.getShort(1));
        newArray.setShort(0, (short) 5);
        assertEquals(5, newArray.getShort(0));
        newArray.setNullShort(0);
        assertTrue(newArray.isNullAt(0));
        newArray.setShort(0, (short) 3);
        assertEquals(newArray, BinaryArrayData.fromPrimitiveArray(newArray.toShortArray()));
    }
    {
        // test int
        BinaryArrayData array = new BinaryArrayData();
        BinaryArrayWriter writer = new BinaryArrayWriter(array, 2, 4);
        writer.setNullInt(0);
        writer.writeInt(1, 25);
        writer.complete();
        assertTrue(array.isNullAt(0));
        assertEquals(25, array.getInt(1));
        array.setInt(0, 5);
        assertEquals(5, array.getInt(0));
        array.setNullInt(0);
        assertTrue(array.isNullAt(0));
        BinaryArrayData newArray = splitArray(array);
        assertTrue(newArray.isNullAt(0));
        assertEquals(25, newArray.getInt(1));
        newArray.setInt(0, 5);
        assertEquals(5, newArray.getInt(0));
        newArray.setNullInt(0);
        assertTrue(newArray.isNullAt(0));
        newArray.setInt(0, 3);
        assertEquals(newArray, BinaryArrayData.fromPrimitiveArray(newArray.toIntArray()));
    }
    {
        // test long
        BinaryArrayData array = new BinaryArrayData();
        BinaryArrayWriter writer = new BinaryArrayWriter(array, 2, 8);
        writer.setNullLong(0);
        writer.writeLong(1, 25);
        writer.complete();
        assertTrue(array.isNullAt(0));
        assertEquals(25, array.getLong(1));
        array.setLong(0, 5);
        assertEquals(5, array.getLong(0));
        array.setNullLong(0);
        assertTrue(array.isNullAt(0));
        BinaryArrayData newArray = splitArray(array);
        assertTrue(newArray.isNullAt(0));
        assertEquals(25, newArray.getLong(1));
        newArray.setLong(0, 5);
        assertEquals(5, newArray.getLong(0));
        newArray.setNullLong(0);
        assertTrue(newArray.isNullAt(0));
        newArray.setLong(0, 3);
        assertEquals(newArray, BinaryArrayData.fromPrimitiveArray(newArray.toLongArray()));
    }
    {
        // test float
        BinaryArrayData array = new BinaryArrayData();
        BinaryArrayWriter writer = new BinaryArrayWriter(array, 2, 4);
        writer.setNullFloat(0);
        writer.writeFloat(1, 25);
        writer.complete();
        assertTrue(array.isNullAt(0));
        assertTrue(25 == array.getFloat(1));
        array.setFloat(0, 5);
        assertTrue(5 == array.getFloat(0));
        array.setNullFloat(0);
        assertTrue(array.isNullAt(0));
        BinaryArrayData newArray = splitArray(array);
        assertTrue(newArray.isNullAt(0));
        assertTrue(25 == newArray.getFloat(1));
        newArray.setFloat(0, 5);
        assertTrue(5 == newArray.getFloat(0));
        newArray.setNullFloat(0);
        assertTrue(newArray.isNullAt(0));
        newArray.setFloat(0, 3);
        assertEquals(newArray, BinaryArrayData.fromPrimitiveArray(newArray.toFloatArray()));
    }
    {
        // test double
        BinaryArrayData array = new BinaryArrayData();
        BinaryArrayWriter writer = new BinaryArrayWriter(array, 2, 8);
        writer.setNullDouble(0);
        writer.writeDouble(1, 25);
        writer.complete();
        assertTrue(array.isNullAt(0));
        assertTrue(25 == array.getDouble(1));
        array.setDouble(0, 5);
        assertTrue(5 == array.getDouble(0));
        array.setNullDouble(0);
        assertTrue(array.isNullAt(0));
        BinaryArrayData newArray = splitArray(array);
        assertTrue(newArray.isNullAt(0));
        assertTrue(25 == newArray.getDouble(1));
        newArray.setDouble(0, 5);
        assertTrue(5 == newArray.getDouble(0));
        newArray.setNullDouble(0);
        assertTrue(newArray.isNullAt(0));
        newArray.setDouble(0, 3);
        assertEquals(newArray, BinaryArrayData.fromPrimitiveArray(newArray.toDoubleArray()));
    }
    {
        // test string
        BinaryArrayData array = new BinaryArrayData();
        BinaryArrayWriter writer = new BinaryArrayWriter(array, 2, 8);
        writer.setNullAt(0);
        writer.writeString(1, fromString("jaja"));
        writer.complete();
        assertTrue(array.isNullAt(0));
        assertEquals(fromString("jaja"), array.getString(1));
        BinaryArrayData newArray = splitArray(array);
        assertTrue(newArray.isNullAt(0));
        assertEquals(fromString("jaja"), newArray.getString(1));
    }
    BinaryArrayData subArray = new BinaryArrayData();
    BinaryArrayWriter subWriter = new BinaryArrayWriter(subArray, 2, 8);
    subWriter.setNullAt(0);
    subWriter.writeString(1, fromString("hehehe"));
    subWriter.complete();
    {
        // test array
        BinaryArrayData array = new BinaryArrayData();
        BinaryArrayWriter writer = new BinaryArrayWriter(array, 2, 8);
        writer.setNullAt(0);
        writer.writeArray(1, subArray, new ArrayDataSerializer(DataTypes.INT().getLogicalType()));
        writer.complete();
        assertTrue(array.isNullAt(0));
        assertEquals(subArray, array.getArray(1));
        BinaryArrayData newArray = splitArray(array);
        assertTrue(newArray.isNullAt(0));
        assertEquals(subArray, newArray.getArray(1));
    }
    {
        // test map
        BinaryArrayData array = new BinaryArrayData();
        BinaryArrayWriter writer = new BinaryArrayWriter(array, 2, 8);
        writer.setNullAt(0);
        writer.writeMap(1, BinaryMapData.valueOf(subArray, subArray), new MapDataSerializer(DataTypes.INT().getLogicalType(), DataTypes.INT().getLogicalType()));
        writer.complete();
        assertTrue(array.isNullAt(0));
        assertEquals(BinaryMapData.valueOf(subArray, subArray), array.getMap(1));
        BinaryArrayData newArray = splitArray(array);
        assertTrue(newArray.isNullAt(0));
        assertEquals(BinaryMapData.valueOf(subArray, subArray), newArray.getMap(1));
    }
}
Also used : MapDataSerializer(org.apache.flink.table.runtime.typeutils.MapDataSerializer) 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 4 with MapDataSerializer

use of org.apache.flink.table.runtime.typeutils.MapDataSerializer 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)

Example 5 with MapDataSerializer

use of org.apache.flink.table.runtime.typeutils.MapDataSerializer in project flink by apache.

the class BinaryArrayDataTest method testMap.

@Test
public void testMap() {
    BinaryArrayData array1 = new BinaryArrayData();
    BinaryArrayWriter writer1 = new BinaryArrayWriter(array1, 3, 4);
    writer1.writeInt(0, 6);
    writer1.writeInt(1, 5);
    writer1.writeInt(2, 666);
    writer1.complete();
    BinaryArrayData array2 = new BinaryArrayData();
    BinaryArrayWriter writer2 = new BinaryArrayWriter(array2, 3, 8);
    writer2.writeString(0, fromString("6"));
    writer2.writeString(1, fromString("5"));
    writer2.writeString(2, fromString("666"));
    writer2.complete();
    BinaryMapData binaryMap = BinaryMapData.valueOf(array1, array2);
    BinaryRowData row = new BinaryRowData(1);
    BinaryRowWriter rowWriter = new BinaryRowWriter(row);
    rowWriter.writeMap(0, binaryMap, new MapDataSerializer(DataTypes.INT().getLogicalType(), DataTypes.INT().getLogicalType()));
    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(key.getInt(1), 5);
    assertEquals(value.getString(1), fromString("5"));
}
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

MapDataSerializer (org.apache.flink.table.runtime.typeutils.MapDataSerializer)5 BinaryRowData (org.apache.flink.table.data.binary.BinaryRowData)4 BinaryRowWriter (org.apache.flink.table.data.writer.BinaryRowWriter)4 Test (org.junit.Test)4 BinaryArrayData (org.apache.flink.table.data.binary.BinaryArrayData)3 BinaryArrayWriter (org.apache.flink.table.data.writer.BinaryArrayWriter)3 BinaryMapData (org.apache.flink.table.data.binary.BinaryMapData)2 ArrayDataSerializer (org.apache.flink.table.runtime.typeutils.ArrayDataSerializer)2 HashMap (java.util.HashMap)1 RowDataSerializer (org.apache.flink.table.runtime.typeutils.RowDataSerializer)1 IntType (org.apache.flink.table.types.logical.IntType)1