use of org.apache.flink.table.data.binary.BinaryMapData in project flink by apache.
the class MapDataSerializer method serialize.
@Override
public void serialize(MapData map, DataOutputView target) throws IOException {
BinaryMapData binaryMap = toBinaryMap(map);
final int size = binaryMap.size();
target.writeInt(size);
BinaryArrayData keyArray = binaryMap.keyArray();
BinaryArrayData valueArray = binaryMap.valueArray();
for (int i = 0; i < size; i++) {
if (keyArray.isNullAt(i)) {
throw new IllegalArgumentException("The key of BinaryMapData must not be null.");
}
Object key = keyGetter.getElementOrNull(keyArray, i);
keyTypeSerializer.serialize(key, target);
if (valueArray.isNullAt(i)) {
target.writeBoolean(true);
} else {
target.writeBoolean(false);
Object value = valueGetter.getElementOrNull(valueArray, i);
valueTypeSerializer.serialize(value, target);
}
}
}
use of org.apache.flink.table.data.binary.BinaryMapData in project flink by apache.
the class AbstractBinaryWriter method writeMap.
@Override
public void writeMap(int pos, MapData input, MapDataSerializer serializer) {
BinaryMapData binary = serializer.toBinaryMap(input);
writeSegmentsToVarLenPart(pos, binary.getSegments(), binary.getOffset(), binary.getSizeInBytes());
}
use of org.apache.flink.table.data.binary.BinaryMapData 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);
}
use of org.apache.flink.table.data.binary.BinaryMapData in project flink by apache.
the class MapDataSerializer method serialize.
@Override
public void serialize(MapData record, DataOutputView target) throws IOException {
BinaryMapData binaryMap = toBinaryMap(record);
target.writeInt(binaryMap.getSizeInBytes());
BinarySegmentUtils.copyToView(binaryMap.getSegments(), binaryMap.getOffset(), binaryMap.getSizeInBytes(), target);
}
use of org.apache.flink.table.data.binary.BinaryMapData 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));
}
Aggregations