rkhachatryan commented on code in PR #27068:
URL: https://github.com/apache/flink/pull/27068#discussion_r2415847583


##########
flink-table/flink-table-runtime/src/test/java/org/apache/flink/table/runtime/operators/sink/SinkUpsertMaterializerTest.java:
##########
@@ -70,83 +103,236 @@ public RecordEqualiser newInstance(ClassLoader 
classLoader) {
                 }
             };
 
+    /**
+     * If the composite serializer in {@link SinkUpsertMaterializer} works on 
projected fields then
+     * it might use the wrong serializer, e.g. the {@link VarCharType} instead 
of the {@link
+     * IntType}. That might cause {@link ArrayIndexOutOfBoundsException} 
because string serializer
+     * expects the first number to be the length of the string.
+     */
     @Test
-    void test() throws Exception {
-        SinkUpsertMaterializer materializer =
-                new SinkUpsertMaterializer(
-                        ttlConfig, serializer, equaliser, upsertKeyEqualiser, 
null);
+    public void testUpsertKeySerializerFailure() throws Exception {
+        LogicalType[] types = new LogicalType[] {new VarCharType(), new 
IntType()};
+        // project int field, while in the original record it's string
+
+        OneInputStreamOperator<RowData, RowData> materializer = 
createOperator(types, 1);
+        try (KeyedOneInputStreamOperatorTestHarness<RowData, RowData, RowData> 
testHarness =
+                createHarness(materializer, stateBackend, types)) {
+            testHarness.open();
+            RowDataHarnessAssertor assertor = new 
RowDataHarnessAssertor(types);
+            // -1 is not a valid string length
+            testHarness.processElement(binaryRecord(RowKind.INSERT, "any 
string", -1));
+            assertor.shouldEmit(testHarness, rowOfKind(RowKind.INSERT, "any 
string", -1));
+
+            // 999 as a string length is too long
+            testHarness.processElement(binaryRecord(RowKind.INSERT, "any 
string", 999));
+            assertor.shouldEmit(testHarness, rowOfKind(RowKind.INSERT, "any 
string", 999));
+        }
+    }
+
+    @Test
+    public void testUpsertKeySerializerSilentCorruption() throws Exception {
+        LogicalType[] types =
+                new LogicalType[] {new VarCharType(), new BigIntType(), new 
IntType()};
+        // project int field, while in the original record it's string
+
+        OneInputStreamOperator<RowData, RowData> materializer = 
createOperator(types, 1);
+        try (KeyedOneInputStreamOperatorTestHarness<RowData, RowData, RowData> 
testHarness =
+                createHarness(materializer, stateBackend, types)) {
+            testHarness.open();
+            RowDataHarnessAssertor assertor = new 
RowDataHarnessAssertor(types);
+
+            // this might serialize upsert key as 32-character string 
potentially including "97"
+            testHarness.processElement(binaryRecord(RowKind.INSERT, "any 
string", 32L, 97));
+            assertor.shouldEmit(testHarness, rowOfKind(RowKind.INSERT, "any 
string", 32L, 97));
+
+            // but here it might include "98" which would result in no output 
and test failure
+            testHarness.processElement(binaryRecord(RowKind.DELETE, "any 
string", 32L, 98));
+            assertor.shouldEmit(testHarness, rowOfKind(RowKind.DELETE, "any 
string", 32L, 98));
+        }
+    }
+
+    @Test
+    public void testUpsertEqualizer() throws Exception {
+        LogicalType[] types = new LogicalType[] {new IntType(), new 
BigIntType()};
+
+        OneInputStreamOperator<RowData, RowData> materializer = 
createOperator(types, 1);
+        try (KeyedOneInputStreamOperatorTestHarness<RowData, RowData, RowData> 
testHarness =
+                createHarness(materializer, stateBackend, types)) {
+            testHarness.open();
+            RowDataHarnessAssertor assertor = new 
RowDataHarnessAssertor(types);
+
+            // upsert key is 33, 0 is unused
+            testHarness.processElement(binaryRecord(RowKind.INSERT, 0, 33L));
+            assertor.shouldEmit(testHarness, rowOfKind(RowKind.INSERT, 0, 
33L));
+
+            // upsert key 33 - should remove AND clear the state involving 
upsert equalizer
+            // equalizer might fail if it's used on un-projected records
+            testHarness.processElement(binaryRecord(RowKind.DELETE, 1, 33L));
+            assertor.shouldEmit(testHarness, rowOfKind(RowKind.DELETE, 1, 
33L));
+        }
+    }

Review Comment:
   Moved to b3dc22b0654a12f48fd2d33978d8cdd2220f124b.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to