Search in sources :

Example 1 with BinaryArrayWriter

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

the class RowDataTest method before.

@Before
public void before() {
    str = StringData.fromString("haha");
    generic = RawValueData.fromObject("haha");
    genericSerializer = new RawValueDataSerializer<>(StringSerializer.INSTANCE);
    decimal1 = DecimalData.fromUnscaledLong(10, 5, 0);
    decimal2 = DecimalData.fromBigDecimal(new BigDecimal(11), 20, 0);
    array = new BinaryArrayData();
    {
        BinaryArrayWriter arrayWriter = new BinaryArrayWriter(array, 2, 4);
        arrayWriter.writeInt(0, 15);
        arrayWriter.writeInt(1, 16);
        arrayWriter.complete();
    }
    map = BinaryMapData.valueOf(array, array);
    underRow = new BinaryRowData(2);
    {
        BinaryRowWriter writer = new BinaryRowWriter(underRow);
        writer.writeInt(0, 15);
        writer.writeInt(1, 16);
        writer.complete();
    }
    bytes = new byte[] { 1, 5, 6 };
    timestamp1 = TimestampData.fromEpochMillis(123L);
    timestamp2 = TimestampData.fromLocalDateTime(LocalDateTime.of(1969, 1, 1, 0, 0, 0, 123456789));
}
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) BigDecimal(java.math.BigDecimal) Before(org.junit.Before)

Example 2 with BinaryArrayWriter

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

the class BinaryArrayDataTest method testTimestampData.

@Test
public void testTimestampData() {
    BinaryArrayData array = new BinaryArrayData();
    BinaryArrayWriter writer = new BinaryArrayWriter(array, 2, 8);
    // 1. compact
    {
        final int precision = 3;
        writer.reset();
        writer.writeTimestamp(0, TimestampData.fromEpochMillis(123L), precision);
        writer.setNullAt(1);
        writer.complete();
        assertEquals("1970-01-01T00:00:00.123", array.getTimestamp(0, 3).toString());
        assertTrue(array.isNullAt(1));
        array.setTimestamp(0, TimestampData.fromEpochMillis(-123L), precision);
        assertEquals("1969-12-31T23:59:59.877", array.getTimestamp(0, 3).toString());
    }
    // 2. not compact
    {
        final int precision = 9;
        TimestampData timestamp1 = TimestampData.fromLocalDateTime(LocalDateTime.of(1970, 1, 1, 0, 0, 0, 123456789));
        TimestampData timestamp2 = TimestampData.fromTimestamp(Timestamp.valueOf("1969-01-01 00:00:00.123456789"));
        writer.reset();
        writer.writeTimestamp(0, timestamp1, precision);
        writer.writeTimestamp(1, null, precision);
        writer.complete();
        assertEquals("1970-01-01T00:00:00.123456789", array.getTimestamp(0, precision).toString());
        assertTrue(array.isNullAt(1));
        array.setTimestamp(0, timestamp2, precision);
        assertEquals("1969-01-01T00:00:00.123456789", array.getTimestamp(0, precision).toString());
    }
}
Also used : BinaryArrayWriter(org.apache.flink.table.data.writer.BinaryArrayWriter) BinaryArrayData(org.apache.flink.table.data.binary.BinaryArrayData) Test(org.junit.Test)

Example 3 with BinaryArrayWriter

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

the class BinaryArrayDataTest method testToArray.

@Test
public void testToArray() {
    BinaryArrayData array = new BinaryArrayData();
    BinaryArrayWriter writer = new BinaryArrayWriter(array, 3, 2);
    writer.writeShort(0, (short) 5);
    writer.writeShort(1, (short) 10);
    writer.writeShort(2, (short) 15);
    writer.complete();
    short[] shorts = array.toShortArray();
    assertEquals(5, shorts[0]);
    assertEquals(10, shorts[1]);
    assertEquals(15, shorts[2]);
    MemorySegment[] segments = splitBytes(writer.getSegments().getArray(), 3);
    array.pointTo(segments, 3, array.getSizeInBytes());
    assertEquals(5, array.getShort(0));
    assertEquals(10, array.getShort(1));
    assertEquals(15, array.getShort(2));
    short[] shorts2 = array.toShortArray();
    assertEquals(5, shorts2[0]);
    assertEquals(10, shorts2[1]);
    assertEquals(15, shorts2[2]);
}
Also used : BinaryArrayWriter(org.apache.flink.table.data.writer.BinaryArrayWriter) BinaryArrayData(org.apache.flink.table.data.binary.BinaryArrayData) MemorySegment(org.apache.flink.core.memory.MemorySegment) Test(org.junit.Test)

Example 4 with BinaryArrayWriter

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

the class BinaryArrayDataTest method testNested.

@Test
public void testNested() {
    BinaryArrayData array = new BinaryArrayData();
    BinaryArrayWriter writer = new BinaryArrayWriter(array, 2, 8);
    writer.writeRow(0, GenericRowData.of(fromString("1"), 1), new RowDataSerializer(RowType.of(VarCharType.STRING_TYPE, new IntType())));
    writer.setNullAt(1);
    writer.complete();
    RowData nestedRow = array.getRow(0, 2);
    assertEquals("1", nestedRow.getString(0).toString());
    assertEquals(1, nestedRow.getInt(1));
    assertTrue(array.isNullAt(1));
}
Also used : BinaryRowData(org.apache.flink.table.data.binary.BinaryRowData) BinaryArrayWriter(org.apache.flink.table.data.writer.BinaryArrayWriter) BinaryArrayData(org.apache.flink.table.data.binary.BinaryArrayData) RowDataSerializer(org.apache.flink.table.runtime.typeutils.RowDataSerializer) IntType(org.apache.flink.table.types.logical.IntType) Test(org.junit.Test)

Example 5 with BinaryArrayWriter

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

the class MapDataSerializer method toBinaryMap.

public BinaryMapData toBinaryMap(MapData from) {
    if (from instanceof BinaryMapData) {
        return (BinaryMapData) from;
    }
    int numElements = from.size();
    if (reuseKeyArray == null) {
        reuseKeyArray = new BinaryArrayData();
    }
    if (reuseValueArray == null) {
        reuseValueArray = new BinaryArrayData();
    }
    if (reuseKeyWriter == null || reuseKeyWriter.getNumElements() != numElements) {
        reuseKeyWriter = new BinaryArrayWriter(reuseKeyArray, numElements, BinaryArrayData.calculateFixLengthPartSize(keyType));
    } else {
        reuseKeyWriter.reset();
    }
    if (reuseValueWriter == null || reuseValueWriter.getNumElements() != numElements) {
        reuseValueWriter = new BinaryArrayWriter(reuseValueArray, numElements, BinaryArrayData.calculateFixLengthPartSize(valueType));
    } else {
        reuseValueWriter.reset();
    }
    ArrayData keyArray = from.keyArray();
    ArrayData valueArray = from.valueArray();
    for (int i = 0; i < from.size(); i++) {
        Object key = keyGetter.getElementOrNull(keyArray, i);
        Object value = valueGetter.getElementOrNull(valueArray, i);
        if (key == null) {
            reuseKeyWriter.setNullAt(i, keyType);
        } else {
            BinaryWriter.write(reuseKeyWriter, i, key, keyType, keySerializer);
        }
        if (value == null) {
            reuseValueWriter.setNullAt(i, valueType);
        } else {
            BinaryWriter.write(reuseValueWriter, i, value, valueType, valueSerializer);
        }
    }
    reuseKeyWriter.complete();
    reuseValueWriter.complete();
    return BinaryMapData.valueOf(reuseKeyArray, reuseValueArray);
}
Also used : BinaryMapData(org.apache.flink.table.data.binary.BinaryMapData) BinaryArrayWriter(org.apache.flink.table.data.writer.BinaryArrayWriter) BinaryArrayData(org.apache.flink.table.data.binary.BinaryArrayData) BinaryArrayData(org.apache.flink.table.data.binary.BinaryArrayData) ArrayData(org.apache.flink.table.data.ArrayData)

Aggregations

BinaryArrayWriter (org.apache.flink.table.data.writer.BinaryArrayWriter)21 BinaryArrayData (org.apache.flink.table.data.binary.BinaryArrayData)19 Test (org.junit.Test)11 BinaryRowData (org.apache.flink.table.data.binary.BinaryRowData)6 BinaryRowWriter (org.apache.flink.table.data.writer.BinaryRowWriter)5 BinaryMapData (org.apache.flink.table.data.binary.BinaryMapData)4 ArrayDataSerializer (org.apache.flink.table.runtime.typeutils.ArrayDataSerializer)3 MapDataSerializer (org.apache.flink.table.runtime.typeutils.MapDataSerializer)3 BigDecimal (java.math.BigDecimal)1 HashMap (java.util.HashMap)1 MemorySegment (org.apache.flink.core.memory.MemorySegment)1 ArrayData (org.apache.flink.table.data.ArrayData)1 GenericMapData (org.apache.flink.table.data.GenericMapData)1 MapData (org.apache.flink.table.data.MapData)1 StringData.fromString (org.apache.flink.table.data.StringData.fromString)1 RawValueDataSerializer (org.apache.flink.table.runtime.typeutils.RawValueDataSerializer)1 RowDataSerializer (org.apache.flink.table.runtime.typeutils.RowDataSerializer)1 IntType (org.apache.flink.table.types.logical.IntType)1 Before (org.junit.Before)1