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


##########
clients/src/main/java/org/apache/kafka/common/protocol/types/NullableSchema.java:
##########
@@ -0,0 +1,93 @@
+/*
+ * 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 short value of -1 to the buffer 
as a null indicator.
+     */
+    @Override
+    public void write(ByteBuffer buffer, Object o) {
+        if (o == null) {
+            buffer.putShort((short) -1);
+            return;
+        }
+
+        buffer.putShort((short) 1);
+        super.write(buffer, o);
+    }
+
+    @Override
+    public Struct read(ByteBuffer buffer) {
+        short nullIndicator = buffer.getShort();
+        if (nullIndicator < 0)
+            return null;
+
+        return super.read(buffer);
+    }
+
+    @Override
+    public int sizeOf(Object o) {
+        if (o == null)
+            return 2;
+
+        return 2 + super.sizeOf(o);
+    }
+
+    @Override
+    public String typeName() {
+        return NULLABLE_STRUCT_TYPE_NAME;
+    }
+
+    @Override
+    public String leftBracket() {
+        return "?{";
+    }
+
+    @Override
+    public String rightBracket() {
+        return "}";
+    }
+    
+    @Override
+    public String documentation() {
+        return "A struct is named by a string with a capitalized first letter 
and consists of one or more fields. " +

Review Comment:
   struct => nullable struct



##########
clients/src/main/java/org/apache/kafka/common/protocol/types/NullableSchema.java:
##########
@@ -0,0 +1,93 @@
+/*
+ * 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 short value of -1 to the buffer 
as a null indicator.
+     */
+    @Override
+    public void write(ByteBuffer buffer, Object o) {
+        if (o == null) {
+            buffer.putShort((short) -1);

Review Comment:
   This is incorrect. We should write a byte instead of a short. Could we write 
a unit test to cover this? I am thinking that we could create a schema json 
file with all existing types and verify that the serialized bytes are the same 
between Schema.write() and Message.write().



##########
clients/src/main/java/org/apache/kafka/common/protocol/types/Type.java:
##########
@@ -79,6 +79,20 @@ public final boolean isArray() {
         return arrayElementType().isPresent();
     }
 
+    /**
+     * Start flag for special data structure

Review Comment:
   How about "For annotation in the generated html protocol doc"?



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