use of org.apache.flink.table.data.writer.BinaryArrayWriter 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.writer.BinaryArrayWriter 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.writer.BinaryArrayWriter in project flink by apache.
the class BinaryArrayDataTest method testDecimal.
@Test
public void testDecimal() {
BinaryArrayData array = new BinaryArrayData();
BinaryArrayWriter writer = new BinaryArrayWriter(array, 2, 8);
// 1.compact
{
int precision = 4;
int scale = 2;
writer.reset();
writer.writeDecimal(0, DecimalData.fromUnscaledLong(5, precision, scale), precision);
writer.setNullAt(1);
writer.complete();
assertEquals("0.05", array.getDecimal(0, precision, scale).toString());
assertTrue(array.isNullAt(1));
array.setDecimal(0, DecimalData.fromUnscaledLong(6, precision, scale), precision);
assertEquals("0.06", array.getDecimal(0, precision, scale).toString());
}
// 2.not compact
{
int precision = 25;
int scale = 5;
DecimalData decimal1 = DecimalData.fromBigDecimal(BigDecimal.valueOf(5.55), precision, scale);
DecimalData decimal2 = DecimalData.fromBigDecimal(BigDecimal.valueOf(6.55), precision, scale);
writer.reset();
writer.writeDecimal(0, decimal1, precision);
writer.writeDecimal(1, null, precision);
writer.complete();
assertEquals("5.55000", array.getDecimal(0, precision, scale).toString());
assertTrue(array.isNullAt(1));
array.setDecimal(0, decimal2, precision);
assertEquals("6.55000", array.getDecimal(0, precision, scale).toString());
}
}
use of org.apache.flink.table.data.writer.BinaryArrayWriter in project flink by apache.
the class ArrayDataSerializer method deserializeInternal.
private ArrayData deserializeInternal(DataInputView source, BinaryArrayData array) throws IOException {
int len = source.readInt();
BinaryArrayWriter writer = new BinaryArrayWriter(array, len, elementSize);
for (int i = 0; i < len; i++) {
boolean isNonNull = source.readBoolean();
if (isNonNull) {
Object element = elementTypeSerializer.deserialize(source);
BinaryWriter.write(writer, i, element, elementType, elementTypeSerializer);
} else {
nullSetter.setNull(writer, i);
}
}
writer.complete();
return array;
}
use of org.apache.flink.table.data.writer.BinaryArrayWriter in project flink by apache.
the class MapDataSerializer method deserializeInternal.
private MapData deserializeInternal(DataInputView source, BinaryArrayData keyArray, BinaryArrayData valueArray) throws IOException {
final int size = source.readInt();
BinaryArrayWriter keyWriter = new BinaryArrayWriter(keyArray, size, keySize);
BinaryArrayWriter valueWriter = new BinaryArrayWriter(valueArray, size, valueSize);
for (int i = 0; i < size; i++) {
Object key = keyTypeSerializer.deserialize(source);
keySetter.setValue(keyWriter, i, key);
boolean isNull = source.readBoolean();
if (isNull) {
nullValueSetter.setNull(valueWriter, i);
} else {
Object value = valueTypeSerializer.deserialize(source);
valueSetter.setValue(valueWriter, i, value);
}
}
keyWriter.complete();
valueWriter.complete();
return BinaryMapData.valueOf(keyArray, valueArray);
}
Aggregations