use of org.apache.flink.table.data.binary.BinaryArrayData 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));
}
use of org.apache.flink.table.data.binary.BinaryArrayData 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.BinaryArrayData in project flink by apache.
the class BinaryArrayDataTest method testArray.
@Test
public void testArray() {
// 1.array test
BinaryArrayData array = new BinaryArrayData();
BinaryArrayWriter writer = new BinaryArrayWriter(array, 3, 4);
writer.writeInt(0, 6);
writer.setNullInt(1);
writer.writeInt(2, 666);
writer.complete();
assertEquals(array.getInt(0), 6);
assertTrue(array.isNullAt(1));
assertEquals(array.getInt(2), 666);
// 2.test write to binary row.
{
BinaryRowData row2 = new BinaryRowData(1);
BinaryRowWriter writer2 = new BinaryRowWriter(row2);
writer2.writeArray(0, array, new ArrayDataSerializer(DataTypes.INT().getLogicalType()));
writer2.complete();
BinaryArrayData array2 = (BinaryArrayData) row2.getArray(0);
assertEquals(array2, array);
assertEquals(array2.getInt(0), 6);
assertTrue(array2.isNullAt(1));
assertEquals(array2.getInt(2), 666);
}
// 3.test write var seg array to binary row.
{
BinaryArrayData array3 = splitArray(array);
BinaryRowData row2 = new BinaryRowData(1);
BinaryRowWriter writer2 = new BinaryRowWriter(row2);
writer2.writeArray(0, array3, new ArrayDataSerializer(DataTypes.INT().getLogicalType()));
writer2.complete();
BinaryArrayData array2 = (BinaryArrayData) row2.getArray(0);
assertEquals(array2, array);
assertEquals(array2.getInt(0), 6);
assertTrue(array2.isNullAt(1));
assertEquals(array2.getInt(2), 666);
}
}
use of org.apache.flink.table.data.binary.BinaryArrayData 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));
}
}
use of org.apache.flink.table.data.binary.BinaryArrayData in project flink by apache.
the class BinaryArrayDataTest method splitArray.
private static BinaryArrayData splitArray(BinaryArrayData array) {
BinaryArrayData ret = new BinaryArrayData();
MemorySegment[] segments = splitBytes(BinarySegmentUtils.copyToBytes(array.getSegments(), 0, array.getSizeInBytes()), 0);
ret.pointTo(segments, 0, array.getSizeInBytes());
return ret;
}
Aggregations