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));
}
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());
}
}
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]);
}
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));
}
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);
}
Aggregations