junrao commented on code in PR #20614:
URL: https://github.com/apache/kafka/pull/20614#discussion_r2561517694


##########
clients/src/test/java/org/apache/kafka/common/message/ProtocolSerializationConsistencyTest.java:
##########
@@ -0,0 +1,162 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kafka.common.message;
+
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.common.protocol.ByteBufferAccessor;
+import org.apache.kafka.common.protocol.ObjectSerializationCache;
+import org.apache.kafka.common.protocol.types.Field;
+import org.apache.kafka.common.protocol.types.Struct;
+import org.apache.kafka.common.record.MemoryRecords;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.nio.ByteBuffer;
+import java.util.List;
+import java.util.TreeMap;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class ProtocolSerializationConsistencyTest {
+
+    private Struct nonFlexibleStruct;
+
+    private Struct flexibleStruct;
+
+    private AllTypeMessageData messageData;
+
+    @BeforeEach
+    public void setup() {
+        nonFlexibleStruct = new Struct(AllTypeMessageData.SCHEMA_0)
+            .set("my_boolean", false)
+            .set("my_int8", (byte) 12)
+            .set("my_int16", (short) 123)
+            .set("my_uint16", 33000)
+            .set("my_int32", 1234)
+            .set("my_uint32", 1234567L)
+            .set("my_uint64", 0xcafcacafcacafcaL)
+            .set("my_uuid", Uuid.fromString("H3KKO4NTRPaCWtEmm3vW7A"))
+            .set("my_float64", 12.34D)
+            .set("my_string", "string")
+            .set("my_nullable_string", null)
+            .set("my_bytes", ByteBuffer.wrap("bytes".getBytes()))
+            .set("my_nullable_bytes", null)
+            .set("my_records", MemoryRecords.EMPTY)
+            .set("my_nullable_records", null)
+            .set("my_int_array", new Object[] {})
+            .set("my_nullable_int_array", null);
+        nonFlexibleStruct.set("my_common_struct", 
nonFlexibleStruct.instance("my_common_struct")
+            .set("foo", 123)
+            .set("bar", 123));
+
+        flexibleStruct = new Struct(AllTypeMessageData.SCHEMA_1)
+            .set("my_boolean", false)
+            .set("my_int8", (byte) 12)
+            .set("my_int16", (short) 123)
+            .set("my_uint16", 33000)
+            .set("my_int32", 1234)
+            .set("my_uint32", 1234567L)
+            .set("my_uint64", 0xcafcacafcacafcaL)
+            .set("my_uuid", Uuid.fromString("H3KKO4NTRPaCWtEmm3vW7A"))
+            .set("my_float64", 12.34D)
+            .set("my_compact_string", "compact string")
+            .set("my_compact_nullable_string", null)
+            .set("my_compact_bytes", ByteBuffer.wrap("compact 
bytes".getBytes()))
+            .set("my_compact_nullable_bytes", null)
+            .set("my_compact_records", MemoryRecords.EMPTY)
+            .set("my_compact_nullable_records", null)
+            .set("my_int_array", new Object[] {})
+            .set("my_nullable_int_array", null)
+            .set("_tagged_fields", new TreeMap<Integer, Field>());
+        flexibleStruct.set("my_common_struct", 
flexibleStruct.instance("my_common_struct")
+            .set("foo", 123)
+            .set("bar", 123)
+            .set("_tagged_fields", new TreeMap<Integer, Field>()));
+
+        messageData = new AllTypeMessageData();
+    }
+
+    @Test
+    public void testNonFlexibleWithNullValue() {
+        messageData.setMyBytes("bytes".getBytes());
+        messageData.setMyRecords(MemoryRecords.EMPTY);
+
+        checkSchemaAndMessageSerializationConsistency((short) 0, messageData, 
nonFlexibleStruct);
+    }
+
+    @Test
+    public void testNonFlexibleWithNonNullValue() {
+        messageData.setMyBytes("bytes".getBytes());
+        messageData.setMyRecords(MemoryRecords.EMPTY);
+        messageData.setMyNullableString("nullable string");
+        messageData.setMyNullableBytes("nullable bytes".getBytes());
+        messageData.setMyNullableRecords(MemoryRecords.EMPTY);
+        messageData.setMyNullableIntArray(List.of(1, 2, 3));
+
+        nonFlexibleStruct.set("my_nullable_string", "nullable string")
+            .set("my_nullable_bytes", ByteBuffer.wrap("nullable 
bytes".getBytes()))
+            .set("my_nullable_records", MemoryRecords.EMPTY)
+            .set("my_nullable_int_array", new Object[] {1, 2, 3});
+
+        checkSchemaAndMessageSerializationConsistency((short) 0, messageData, 
nonFlexibleStruct);
+    }
+
+    @Test
+    public void testFlexibleWithNullValue() {
+        messageData.setMyCompactBytes("compact bytes".getBytes());
+        messageData.setMyCompactRecords(MemoryRecords.EMPTY);
+        messageData.setMyCommonStruct(null);
+
+        flexibleStruct.set("my_common_struct", null);
+
+        checkSchemaAndMessageSerializationConsistency((short) 1, messageData, 
flexibleStruct);
+    }
+
+    @Test
+    public void testFlexibleWithNonNullValue() {
+        messageData.setMyCompactBytes("compact bytes".getBytes());
+        messageData.setMyCompactRecords(MemoryRecords.EMPTY);
+        messageData.setMyCompactNullableString("compact nullable string");
+        messageData.setMyCompactNullableBytes("compact nullable 
bytes".getBytes());
+        messageData.setMyCompactNullableRecords(MemoryRecords.EMPTY);
+        messageData.setMyNullableIntArray(List.of(1, 2, 3));
+
+        flexibleStruct.set("my_compact_nullable_string", "compact nullable 
string")
+            .set("my_compact_nullable_bytes", ByteBuffer.wrap("compact 
nullable bytes".getBytes()))
+            .set("my_compact_nullable_records", MemoryRecords.EMPTY)
+            .set("my_nullable_int_array", new Object[] {1, 2, 3});
+
+        checkSchemaAndMessageSerializationConsistency((short) 1, messageData, 
flexibleStruct);
+    }
+
+    private void checkSchemaAndMessageSerializationConsistency(short version, 
AllTypeMessageData message, Struct struct) {
+        ObjectSerializationCache cache = new ObjectSerializationCache();
+        ByteBuffer buf = ByteBuffer.allocate(message.size(cache, version));
+        ByteBufferAccessor byteBufferAccessor = new ByteBufferAccessor(buf);
+        // Serialize message
+        message.write(byteBufferAccessor, cache, version);
+
+        ByteBuffer buffer = ByteBuffer.allocate(struct.sizeOf());
+        // Serialize schema
+        struct.writeTo(buffer);
+
+        assertEquals(buffer.position(), 
byteBufferAccessor.buffer().position());

Review Comment:
   Could we add the error message? Ditto below.



##########
clients/src/main/java/org/apache/kafka/common/protocol/types/NullableSchema.java:
##########
@@ -0,0 +1,101 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.kafka.common.protocol.types;
+
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+
+/**
+ * The nullable schema for a compound record definition
+ */
+public final class NullableSchema extends Schema {
+
+    private static final String NULLABLE_STRUCT_TYPE_NAME = "NULLABLE_STRUCT";
+
+    public NullableSchema(Schema schema) {
+        super(schema.tolerateMissingFieldsWithDefaults(), 
Arrays.stream(schema.fields()).map(field -> field.def).toArray(Field[]::new));
+    }
+
+    @Override
+    public boolean isNullable() {
+        return true;
+    }
+
+    /**
+     * Write a struct to the buffer with special handling for null values
+     * If the input object is null, writes a byte value of -1 to the buffer as 
a null indicator.
+     */
+    @Override
+    public void write(ByteBuffer buffer, Object o) {
+        if (o == null) {
+            buffer.put((byte) -1);
+            return;
+        }
+
+        buffer.put((byte) 1);
+        super.write(buffer, o);
+    }
+
+    @Override
+    public Struct read(ByteBuffer buffer) {
+        short nullIndicator = buffer.getShort();

Review Comment:
   This is incorrect. We should read a byte instead of a short. Could we 
improve the unit test to cover reads too?



##########
clients/src/test/java/org/apache/kafka/common/message/ProtocolSerializationConsistencyTest.java:
##########
@@ -0,0 +1,162 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.kafka.common.message;
+
+import org.apache.kafka.common.Uuid;
+import org.apache.kafka.common.protocol.ByteBufferAccessor;
+import org.apache.kafka.common.protocol.ObjectSerializationCache;
+import org.apache.kafka.common.protocol.types.Field;
+import org.apache.kafka.common.protocol.types.Struct;
+import org.apache.kafka.common.record.MemoryRecords;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.nio.ByteBuffer;
+import java.util.List;
+import java.util.TreeMap;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class ProtocolSerializationConsistencyTest {
+
+    private Struct nonFlexibleStruct;
+
+    private Struct flexibleStruct;
+
+    private AllTypeMessageData messageData;
+
+    @BeforeEach
+    public void setup() {
+        nonFlexibleStruct = new Struct(AllTypeMessageData.SCHEMA_0)
+            .set("my_boolean", false)
+            .set("my_int8", (byte) 12)
+            .set("my_int16", (short) 123)
+            .set("my_uint16", 33000)
+            .set("my_int32", 1234)
+            .set("my_uint32", 1234567L)
+            .set("my_uint64", 0xcafcacafcacafcaL)
+            .set("my_uuid", Uuid.fromString("H3KKO4NTRPaCWtEmm3vW7A"))
+            .set("my_float64", 12.34D)
+            .set("my_string", "string")
+            .set("my_nullable_string", null)
+            .set("my_bytes", ByteBuffer.wrap("bytes".getBytes()))
+            .set("my_nullable_bytes", null)
+            .set("my_records", MemoryRecords.EMPTY)
+            .set("my_nullable_records", null)
+            .set("my_int_array", new Object[] {})
+            .set("my_nullable_int_array", null);
+        nonFlexibleStruct.set("my_common_struct", 
nonFlexibleStruct.instance("my_common_struct")
+            .set("foo", 123)
+            .set("bar", 123));
+
+        flexibleStruct = new Struct(AllTypeMessageData.SCHEMA_1)
+            .set("my_boolean", false)
+            .set("my_int8", (byte) 12)
+            .set("my_int16", (short) 123)
+            .set("my_uint16", 33000)
+            .set("my_int32", 1234)
+            .set("my_uint32", 1234567L)
+            .set("my_uint64", 0xcafcacafcacafcaL)
+            .set("my_uuid", Uuid.fromString("H3KKO4NTRPaCWtEmm3vW7A"))
+            .set("my_float64", 12.34D)
+            .set("my_compact_string", "compact string")
+            .set("my_compact_nullable_string", null)
+            .set("my_compact_bytes", ByteBuffer.wrap("compact 
bytes".getBytes()))
+            .set("my_compact_nullable_bytes", null)
+            .set("my_compact_records", MemoryRecords.EMPTY)
+            .set("my_compact_nullable_records", null)
+            .set("my_int_array", new Object[] {})
+            .set("my_nullable_int_array", null)
+            .set("_tagged_fields", new TreeMap<Integer, Field>());
+        flexibleStruct.set("my_common_struct", 
flexibleStruct.instance("my_common_struct")
+            .set("foo", 123)
+            .set("bar", 123)
+            .set("_tagged_fields", new TreeMap<Integer, Field>()));
+
+        messageData = new AllTypeMessageData();
+    }
+
+    @Test
+    public void testNonFlexibleWithNullValue() {

Review Comment:
   WithNullValue => WithNullDefault ?



##########
clients/src/main/java/org/apache/kafka/common/protocol/types/ArrayOf.java:
##########
@@ -119,15 +132,25 @@ public Object[] validate(Object item) {
 
     @Override
     public String typeName() {
-        return ARRAY_TYPE_NAME;
+        return nullable ? NULLABLE_ARRAY_TYPE_NAME : ARRAY_TYPE_NAME;
     }
 
     @Override
     public String documentation() {
-        return "Represents a sequence of objects of a given type T. " +
+        String doc;
+        if (nullable) {
+            doc = "Represents a sequence of objects of a given type T. " +
                 "Type T can be either a primitive type (e.g. " + STRING + ") 
or a structure. " +
                 "First, the length N is given as an " + INT32 + ". Then N 
instances of type T follow. " +
                 "A null array is represented with a length of -1. " +
+                "In protocol documentation a nullable array of T instances is 
referred to as ?[T].";

Review Comment:
   ?[ => leftBracket() 
   ] => rightBracket()
   
   Ditto in a few other places.



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