slachiewicz commented on code in PR #348:
URL: 
https://github.com/apache/flink-connector-kafka/pull/348#discussion_r4786162913


##########
flink-connector-kafka/src/test/java/org/apache/flink/streaming/connectors/kafka/table/KafkaTableITCase.java:
##########
@@ -1113,6 +1113,67 @@ void testKafkaSourceSinkWithKeyAndFullValue(final String 
format) throws Exceptio
         cleanupTopic(topic);
     }
 
+    @ParameterizedTest(name = "value.fields-include: {0}")
+    @ValueSource(strings = {"ALL", "EXCEPT_KEY"})
+    void testValueFormatMetadataWithKeyFields(final String valueFieldsInclude) 
throws Exception {
+        tEnv.getConfig().set(TableConfigOptions.LOCAL_TIME_ZONE, "UTC");
+        final String topic = "value_format_metadata_topic_" + 
UUID.randomUUID();
+        createTestTopic(topic, 1, 1);
+        final String bootstraps = getBootstrapServers();
+        final String groupId = getStandardProps().getProperty("group.id");
+
+        // write the key and the Debezium value as raw bytes
+        tEnv.executeSql(
+                String.format(
+                        "CREATE TABLE raw_writer (\n"
+                                + "  `k` STRING,\n"
+                                + "  `v` STRING\n"
+                                + ") WITH (\n"
+                                + "  'connector' = 'kafka',\n"
+                                + "  'topic' = '%s',\n"
+                                + "  'properties.bootstrap.servers' = '%s',\n"
+                                + "  'key.format' = 'raw',\n"
+                                + "  'key.fields' = 'k',\n"
+                                + "  'value.format' = 'raw',\n"
+                                + "  'value.fields-include' = 'EXCEPT_KEY'\n"
+                                + ")",
+                        topic, bootstraps));
+        tEnv.executeSql(
+                        "INSERT INTO raw_writer VALUES ('{\"id\":1}', "
+                                + 
"'{\"before\":null,\"after\":{\"id\":1,\"name\":\"a\"},"
+                                + 
"\"source\":{\"ts_ms\":1700000000000,\"db\":\"inventory\"},"
+                                + "\"op\":\"c\"}')")
+                .await();
+
+        tEnv.executeSql(
+                String.format(
+                        "CREATE TABLE kafka (\n"
+                                + "  `id` INT,\n"
+                                + "  `name` STRING,\n"
+                                + "  `src_ts` TIMESTAMP_LTZ(3) METADATA FROM 
'value.source.timestamp' VIRTUAL,\n"
+                                + "  `src_db` STRING METADATA FROM 
'value.source.database' VIRTUAL\n"
+                                + ") WITH (\n"
+                                + "  'connector' = 'kafka',\n"
+                                + "  'topic' = '%s',\n"
+                                + "  'properties.bootstrap.servers' = '%s',\n"
+                                + "  'properties.group.id' = '%s',\n"
+                                + "  'scan.startup.mode' = 
'earliest-offset',\n"
+                                + "  'key.format' = 'json',\n"
+                                + "  'key.fields' = 'id',\n"
+                                + "  'value.format' = 'debezium-json',\n"
+                                + "  'value.fields-include' = '%s'\n"
+                                + ")",
+                        topic, bootstraps, groupId, valueFieldsInclude));
+
+        final List<Row> result = collectRows(tEnv.sqlQuery("SELECT * FROM 
kafka"), 1);

Review Comment:
   Done: both tests now set `'scan.bounded.mode' = 'latest-offset'` and use 
`collectAllRows`, so a duplicate or a missing row fails `containsExactly` 
instead of hanging.



##########
flink-connector-kafka/src/test/java/org/apache/flink/streaming/connectors/kafka/table/DynamicKafkaTableITCase.java:
##########
@@ -187,6 +191,73 @@ void testDynamicKafkaSource() throws Exception {
         cleanupTopic(topic);
     }
 
+    @ParameterizedTest(name = "value.fields-include: {0}")
+    @ValueSource(strings = {"ALL", "EXCEPT_KEY"})
+    void testValueFormatMetadataWithKeyFields(final String valueFieldsInclude) 
throws Exception {
+        tEnv.getConfig().set(TableConfigOptions.LOCAL_TIME_ZONE, "UTC");
+        final String topic = "dynamic_value_format_metadata_topic_" + 
UUID.randomUUID();
+        createTestTopic(topic, 1, 1);
+        final String bootstraps = getBootstrapServers();
+        final String groupId = getStandardProps().getProperty("group.id");
+
+        // write the key and the Debezium value as raw bytes
+        tEnv.executeSql(
+                String.format(
+                        "CREATE TABLE raw_writer (\n"
+                                + "  `k` STRING,\n"
+                                + "  `v` STRING\n"
+                                + ") WITH (\n"
+                                + "  'connector' = 'kafka',\n"
+                                + "  'topic' = '%s',\n"
+                                + "  'properties.bootstrap.servers' = '%s',\n"
+                                + "  'key.format' = 'raw',\n"
+                                + "  'key.fields' = 'k',\n"
+                                + "  'value.format' = 'raw',\n"
+                                + "  'value.fields-include' = 'EXCEPT_KEY'\n"
+                                + ")",
+                        topic, bootstraps));
+        tEnv.executeSql(
+                        "INSERT INTO raw_writer VALUES ('{\"id\":1}', "
+                                + 
"'{\"before\":null,\"after\":{\"id\":1,\"name\":\"a\"},"
+                                + 
"\"source\":{\"ts_ms\":1700000000000,\"db\":\"inventory\"},"
+                                + "\"op\":\"c\"}')")
+                .await();
+
+        tEnv.executeSql(
+                String.format(
+                        "CREATE TABLE dynamic_kafka (\n"
+                                + "  `id` INT,\n"
+                                + "  `name` STRING,\n"
+                                + "  `src_ts` TIMESTAMP_LTZ(3) METADATA FROM 
'value.source.timestamp' VIRTUAL,\n"
+                                + "  `src_db` STRING METADATA FROM 
'value.source.database' VIRTUAL\n"
+                                + ") WITH (\n"
+                                + "  'connector' = '%s',\n"
+                                + "  'stream-ids' = '%s',\n"
+                                + "  'metadata-service' = 'single-cluster',\n"
+                                + "  'metadata-service.cluster-id' = 
'cluster-0',\n"
+                                + "  'properties.bootstrap.servers' = '%s',\n"
+                                + "  'properties.group.id' = '%s',\n"
+                                + "  'scan.startup.mode' = 
'earliest-offset',\n"
+                                + "  'key.format' = 'json',\n"
+                                + "  'key.fields' = 'id',\n"
+                                + "  'value.format' = 'debezium-json',\n"
+                                + "  'value.fields-include' = '%s'\n"
+                                + ")",
+                        DynamicKafkaTableFactory.IDENTIFIER,
+                        topic,
+                        bootstraps,
+                        groupId,
+                        valueFieldsInclude));
+
+        final List<Row> result = collectRows(tEnv.sqlQuery("SELECT * FROM 
dynamic_kafka"), 1);

Review Comment:
   Same change here.



##########
flink-connector-kafka/src/main/java/org/apache/flink/streaming/connectors/kafka/table/KafkaDynamicSource.java:
##########
@@ -534,12 +534,14 @@ private KafkaRecordDeserializationSchema<RowData> 
createKafkaDeserializationSche
         final int adjustedPhysicalArity =
                 DataType.getFieldDataTypes(producedDataType).size() - 
metadataKeys.size();
 
-        // adjust value format projection to include value format's metadata 
columns at the end
+        // adjust value format projection to include value format's metadata 
columns at the end;
+        // they follow the physical columns, which key and value projections 
may share when
+        // 'value.fields-include' is ALL
         final int[] adjustedValueProjection =
                 IntStream.concat(
                                 IntStream.of(valueProjection),
                                 IntStream.range(
-                                        keyProjection.length + 
valueProjection.length,
+                                        
DataType.getFieldCount(physicalDataType),

Review Comment:
   Done: the commit message describes the shift and the `ClassCastException`, 
which the `ALL` case of the new tests hits on main. The PR description says the 
same.



##########
flink-connector-kafka/src/test/java/org/apache/flink/streaming/connectors/kafka/table/KafkaTableITCase.java:
##########
@@ -1113,6 +1113,68 @@ void testKafkaSourceSinkWithKeyAndFullValue(final String 
format) throws Exceptio
         cleanupTopic(topic);
     }
 
+    @ParameterizedTest(name = "value.fields-include: {0}")
+    @ValueSource(strings = {"ALL", "EXCEPT_KEY"})
+    void testValueFormatMetadataWithKeyFields(final String valueFieldsInclude) 
throws Exception {
+        tEnv.getConfig().set(TableConfigOptions.LOCAL_TIME_ZONE, "UTC");
+        final String topic = "value_format_metadata_topic_" + 
UUID.randomUUID();
+        createTestTopic(topic, 1, 1);
+        final String bootstraps = getBootstrapServers();
+        final String groupId = getStandardProps().getProperty("group.id");
+
+        // write the key and the Debezium value as raw bytes
+        tEnv.executeSql(
+                String.format(
+                        "CREATE TABLE raw_writer (\n"
+                                + "  `k` STRING,\n"
+                                + "  `v` STRING\n"
+                                + ") WITH (\n"
+                                + "  'connector' = 'kafka',\n"
+                                + "  'topic' = '%s',\n"
+                                + "  'properties.bootstrap.servers' = '%s',\n"
+                                + "  'key.format' = 'raw',\n"
+                                + "  'key.fields' = 'k',\n"
+                                + "  'value.format' = 'raw',\n"
+                                + "  'value.fields-include' = 'EXCEPT_KEY'\n"
+                                + ")",
+                        topic, bootstraps));
+        tEnv.executeSql(
+                        "INSERT INTO raw_writer VALUES ('{\"id\":1}', "
+                                + 
"'{\"before\":null,\"after\":{\"id\":1,\"name\":\"a\"},"
+                                + 
"\"source\":{\"ts_ms\":1700000000000,\"db\":\"inventory\"},"
+                                + "\"op\":\"c\"}')")
+                .await();
+
+        tEnv.executeSql(
+                String.format(
+                        "CREATE TABLE kafka (\n"
+                                + "  `id` INT,\n"
+                                + "  `name` STRING,\n"
+                                + "  `src_ts` TIMESTAMP_LTZ(3) METADATA FROM 
'value.source.timestamp' VIRTUAL,\n"
+                                + "  `src_db` STRING METADATA FROM 
'value.source.database' VIRTUAL\n"

Review Comment:
   Added `src_offset BIGINT METADATA FROM 'offset'` between the two 
value-format metadata columns in both tests.



-- 
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