X-czh commented on code in PR #24845:
URL: https://github.com/apache/flink/pull/24845#discussion_r1631069777


##########
flink-core/src/main/java/org/apache/flink/api/common/typeutils/base/SetSerializer.java:
##########
@@ -0,0 +1,168 @@
+/*
+ * 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.flink.api.common.typeutils.base;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.api.common.typeutils.TypeSerializerSnapshot;
+import org.apache.flink.core.memory.DataInputView;
+import org.apache.flink.core.memory.DataOutputView;
+
+import java.io.IOException;
+import java.util.HashSet;
+import java.util.Set;
+
+import static org.apache.flink.util.Preconditions.checkNotNull;
+
+/**
+ * A serializer for {@link java.util.Set}. The serializer relies on an element 
serializer for the
+ * serialization of the set's elements.
+ *
+ * <p>The serialization format for the set is as follows: four bytes for the 
length of the set,
+ * followed by the serialized representation of each element.
+ *
+ * @param <T> The type of element in the set.
+ */
+@Internal
+public final class SetSerializer<T> extends TypeSerializer<Set<T>> {
+
+    private static final long serialVersionUID = 1L;
+
+    /** The serializer for the elements of the set. */
+    private final TypeSerializer<T> elementSerializer;
+
+    /**
+     * Creates a set serializer that uses the given serializer to serialize 
the set's elements.
+     *
+     * @param elementSerializer The serializer for the elements of the set
+     */
+    public SetSerializer(TypeSerializer<T> elementSerializer) {
+        this.elementSerializer = checkNotNull(elementSerializer);
+    }
+
+    // ------------------------------------------------------------------------
+    //  SetSerializer specific properties
+    // ------------------------------------------------------------------------
+
+    /**
+     * Gets the serializer for the elements of the set.
+     *
+     * @return The serializer for the elements of the set
+     */
+    public TypeSerializer<T> getElementSerializer() {
+        return elementSerializer;
+    }
+
+    // ------------------------------------------------------------------------
+    //  Type Serializer implementation
+    // ------------------------------------------------------------------------
+
+    @Override
+    public boolean isImmutableType() {
+        return false;
+    }
+
+    @Override
+    public TypeSerializer<Set<T>> duplicate() {
+        TypeSerializer<T> duplicateElement = elementSerializer.duplicate();
+        return duplicateElement == elementSerializer ? this : new 
SetSerializer<>(duplicateElement);
+    }
+
+    @Override
+    public Set<T> createInstance() {
+        return new HashSet<>(0);
+    }
+
+    @Override
+    public Set<T> copy(Set<T> from) {
+        Set<T> newSet = new HashSet<>(from.size());
+        for (T element : from) {
+            newSet.add(elementSerializer.copy(element));
+        }
+        return newSet;
+    }
+
+    @Override
+    public Set<T> copy(Set<T> from, Set<T> reuse) {
+        return copy(from);
+    }
+
+    @Override
+    public int getLength() {
+        return -1; // var length
+    }
+
+    @Override
+    public void serialize(Set<T> set, DataOutputView target) throws 
IOException {
+        final int size = set.size();
+        target.writeInt(size);
+        for (T element : set) {
+            elementSerializer.serialize(element, target);

Review Comment:
   Good catch! To allow null values, we can adopt the approach taken by 
`MapSerializer", prefixing each value by a null marker.
   
   I also found that the serializing a list with null values will throw NPE as 
we reused the existing `ListSerializer` impl, which does not allow null values 
(it is only used for serializing `ListState` before where null value is 
explicitly forbidden in the contract). Directly adding null marker for it will 
break state compatibility, I plan to introduce a new list serializer that 
accepts null values for serializing user objects in a new JIRA, WDYT? 



-- 
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: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to