use of org.apache.flink.table.data.ArrayData in project flink by apache.
the class InternalDataUtils method toGenericMap.
static GenericMapData toGenericMap(MapData mapData, LogicalType logicalType) {
final LogicalType keyType = logicalType.is(LogicalTypeRoot.MULTISET) ? ((MultisetType) logicalType).getElementType() : ((MapType) logicalType).getKeyType();
final LogicalType valueType = logicalType.is(LogicalTypeRoot.MULTISET) ? new IntType(false) : ((MapType) logicalType).getValueType();
final ArrayData.ElementGetter keyGetter = ArrayData.createElementGetter(keyType);
final ArrayData.ElementGetter valueGetter = ArrayData.createElementGetter(valueType);
final ArrayData keys = mapData.keyArray();
final ArrayData values = mapData.valueArray();
final LinkedHashMap<Object, Object> newMap = new LinkedHashMap<>();
for (int i = 0; i < mapData.size(); i++) {
Object key = null;
Object value = null;
if (!keys.isNullAt(i)) {
key = toGenericInternalData(keyGetter.getElementOrNull(keys, i), keyType);
}
if (!values.isNullAt(i)) {
value = toGenericInternalData(valueGetter.getElementOrNull(values, i), valueType);
}
newMap.put(key, value);
}
return new GenericMapData(newMap);
}
use of org.apache.flink.table.data.ArrayData in project flink by apache.
the class MapDataUtil method convertToJavaMap.
/**
* Converts a {@link MapData} into Java {@link Map}, the keys and values of the Java map still
* holds objects of internal data structures.
*/
public static Map<Object, Object> convertToJavaMap(MapData map, LogicalType keyType, LogicalType valueType) {
ArrayData keyArray = map.keyArray();
ArrayData valueArray = map.valueArray();
Map<Object, Object> javaMap = new HashMap<>();
ArrayData.ElementGetter keyGetter = ArrayData.createElementGetter(keyType);
ArrayData.ElementGetter valueGetter = ArrayData.createElementGetter(valueType);
for (int i = 0; i < map.size(); i++) {
Object key = keyGetter.getElementOrNull(keyArray, i);
Object value = valueGetter.getElementOrNull(valueArray, i);
javaMap.put(key, value);
}
return javaMap;
}
use of org.apache.flink.table.data.ArrayData in project flink by apache.
the class MapMapConverter method toExternal.
@Override
public Map<K, V> toExternal(MapData internal) {
final ArrayData keyArray = internal.keyArray();
final ArrayData valueArray = internal.valueArray();
final int length = internal.size();
final Map<K, V> map = new HashMap<>();
for (int pos = 0; pos < length; pos++) {
final Object keyValue = keyConverter.elementGetter.getElementOrNull(keyArray, pos);
final Object valueValue = valueConverter.elementGetter.getElementOrNull(valueArray, pos);
map.put(keyConverter.elementConverter.toExternalOrNull(keyValue), valueConverter.elementConverter.toExternalOrNull(valueValue));
}
return map;
}
use of org.apache.flink.table.data.ArrayData 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.ArrayData in project flink by apache.
the class RowDataToAvroConverters method createArrayConverter.
private static RowDataToAvroConverter createArrayConverter(ArrayType arrayType) {
LogicalType elementType = arrayType.getElementType();
final ArrayData.ElementGetter elementGetter = ArrayData.createElementGetter(elementType);
final RowDataToAvroConverter elementConverter = createConverter(arrayType.getElementType());
return new RowDataToAvroConverter() {
private static final long serialVersionUID = 1L;
@Override
public Object convert(Schema schema, Object object) {
final Schema elementSchema = schema.getElementType();
ArrayData arrayData = (ArrayData) object;
List<Object> list = new ArrayList<>();
for (int i = 0; i < arrayData.size(); ++i) {
list.add(elementConverter.convert(elementSchema, elementGetter.getElementOrNull(arrayData, i)));
}
return list;
}
};
}
Aggregations