|
20 | 20 |
|
21 | 21 | import org.apache.flink.api.common.typeutils.SerializerTestBase; |
22 | 22 | import org.apache.flink.api.common.typeutils.TypeSerializer; |
| 23 | +import org.apache.flink.api.common.typeutils.TypeSerializerSchemaCompatibility; |
| 24 | +import org.apache.flink.api.common.typeutils.TypeSerializerSnapshot; |
| 25 | +import org.apache.flink.api.common.typeutils.TypeSerializerSnapshotSerializationUtil; |
23 | 26 | import org.apache.flink.configuration.Configuration; |
| 27 | +import org.apache.flink.core.memory.DataInputViewStreamWrapper; |
| 28 | +import org.apache.flink.core.memory.DataOutputViewStreamWrapper; |
24 | 29 | import org.apache.flink.table.data.RowData; |
25 | 30 | import org.apache.flink.table.runtime.generated.GeneratedHashFunction; |
26 | 31 | import org.apache.flink.table.runtime.generated.GeneratedRecordEqualiser; |
|
29 | 34 | import org.apache.flink.table.runtime.typeutils.RowDataSerializer; |
30 | 35 | import org.apache.flink.table.runtime.util.StreamRecordUtils; |
31 | 36 | import org.apache.flink.table.types.logical.IntType; |
| 37 | +import org.apache.flink.table.types.logical.VarCharType; |
32 | 38 |
|
| 39 | +import org.junit.jupiter.api.Test; |
| 40 | + |
| 41 | +import java.io.ByteArrayInputStream; |
| 42 | +import java.io.ByteArrayOutputStream; |
33 | 43 | import java.util.Objects; |
34 | 44 |
|
| 45 | +import static org.assertj.core.api.Assertions.assertThat; |
| 46 | + |
35 | 47 | /** Test for {@link RowDataKeySerializer}. */ |
36 | 48 | public class RowDataKeySerializerTest extends SerializerTestBase<RowDataKey> { |
37 | 49 |
|
@@ -62,6 +74,51 @@ protected RowDataKey[] getTestData() { |
62 | 74 | return new RowDataKey[] {new RowDataKey(StreamRecordUtils.row(123), equaliser, equaliser)}; |
63 | 75 | } |
64 | 76 |
|
| 77 | + @Test |
| 78 | + void testResolveSchemaCompatibilityWithDifferentSchema() throws Exception { |
| 79 | + // Create a snapshot from the "old" serializer (IntType only) and serialize it |
| 80 | + RowDataKeySerializer oldSerializer = |
| 81 | + new RowDataKeySerializer( |
| 82 | + new RowDataSerializer(new IntType()), |
| 83 | + equaliser, |
| 84 | + equaliser, |
| 85 | + EQUALISER, |
| 86 | + HASH_FUNCTION); |
| 87 | + |
| 88 | + byte[] serializedSnapshot; |
| 89 | + try (ByteArrayOutputStream out = new ByteArrayOutputStream()) { |
| 90 | + TypeSerializerSnapshotSerializationUtil.writeSerializerSnapshot( |
| 91 | + new DataOutputViewStreamWrapper(out), oldSerializer.snapshotConfiguration()); |
| 92 | + serializedSnapshot = out.toByteArray(); |
| 93 | + } |
| 94 | + |
| 95 | + // Restore the "old" snapshot |
| 96 | + TypeSerializerSnapshot<RowDataKey> restoredOldSnapshot; |
| 97 | + try (ByteArrayInputStream in = new ByteArrayInputStream(serializedSnapshot)) { |
| 98 | + restoredOldSnapshot = |
| 99 | + TypeSerializerSnapshotSerializationUtil.readSerializerSnapshot( |
| 100 | + new DataInputViewStreamWrapper(in), |
| 101 | + Thread.currentThread().getContextClassLoader()); |
| 102 | + } |
| 103 | + |
| 104 | + // Create a "new" serializer with a different schema (IntType + VarCharType) |
| 105 | + RowDataKeySerializer newSerializer = |
| 106 | + new RowDataKeySerializer( |
| 107 | + new RowDataSerializer(new IntType(), new VarCharType()), |
| 108 | + equaliser, |
| 109 | + equaliser, |
| 110 | + EQUALISER, |
| 111 | + HASH_FUNCTION); |
| 112 | + |
| 113 | + // The new snapshot should detect incompatibility with the old snapshot |
| 114 | + TypeSerializerSchemaCompatibility<RowDataKey> compatibility = |
| 115 | + newSerializer |
| 116 | + .snapshotConfiguration() |
| 117 | + .resolveSchemaCompatibility(restoredOldSnapshot); |
| 118 | + |
| 119 | + assertThat(compatibility.isIncompatible()).isTrue(); |
| 120 | + } |
| 121 | + |
65 | 122 | static final GeneratedRecordEqualiser EQUALISER = |
66 | 123 | new GeneratedRecordEqualiser("", "", new Object[0]) { |
67 | 124 |
|
|
0 commit comments