You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2022/06/23 11:47:19 UTC

[GitHub] [flink] dianfu commented on a diff in pull request #20040: [FLINK-28182][python][format] Support Avro generic record decoder

dianfu commented on code in PR #20040:
URL: https://github.com/apache/flink/pull/20040#discussion_r904650514


##########
flink-python/pyflink/fn_execution/coder_impl_slow.py:
##########
@@ -834,3 +837,26 @@ def _filter_data_views(self, row):
                 row[i][spec.field_index] = None
             i += 1
         return row
+
+
+class AvroCoderImpl(FieldCoderImpl):
+
+    def __init__(self, schema_string: str):
+        self._bytes_io = io.BytesIO()
+        self._decoder = FlinkAvroDecoder(self._bytes_io)
+        self._schema = avro_schema.parse(schema_string)
+        self._reader = FlinkAvroDatumReader(writer_schema=self._schema, reader_schema=self._schema)
+
+    def encode_to_stream(self, value, out_stream: OutputStream):
+        raise NotImplementedError()
+
+    def decode_from_stream(self, in_stream: InputStream, length: int = 0):
+        length = in_stream.read_int32()
+        if length <= 0:
+            return None
+        data = in_stream.read(length)
+        self._bytes_io.seek(0)
+        self._bytes_io.truncate(0)
+        self._bytes_io.write(data)
+        self._bytes_io.seek(0)

Review Comment:
   this line could be removed



##########
flink-python/src/main/java/org/apache/flink/streaming/api/utils/PythonTypeUtils.java:
##########
@@ -172,6 +176,16 @@ public static FlinkFnApi.TypeInfo toTypeInfoProto(TypeInformation<?> typeInforma
                                 ((ExternalTypeInfo<?>) typeInformation).getDataType()));
             }
 
+            if (typeInformation
+                    .getClass()
+                    .getCanonicalName()
+                    .equals("org.apache.flink.formats.avro.typeutils.GenericRecordAvroTypeInfo")) {
+                try {
+                    return buildAvroTypeProto(typeInformation);
+                } catch (Exception ignore) {

Review Comment:
   Why ignoring this exception?



##########
flink-python/src/main/java/org/apache/flink/streaming/api/utils/PythonTypeUtils.java:
##########
@@ -301,6 +315,23 @@ private static FlinkFnApi.TypeInfo buildListTypeProto(ListTypeInfo<?> listTypeIn
                     .build();
         }
 
+        private static FlinkFnApi.TypeInfo buildAvroTypeProto(TypeInformation<?> avroTypeInfo)
+                throws Exception {
+            Class<?> clazz =
+                    Class.forName(
+                            "org.apache.flink.formats.avro.typeutils.GenericRecordAvroTypeInfo",
+                            true,
+                            userClassLoader);

Review Comment:
   ```suggestion
                               PythonTypeUtils.class.getClassLoader());
   ```



##########
flink-python/src/main/java/org/apache/flink/streaming/api/utils/LengthPrefixWrapperSerializer.java:
##########
@@ -0,0 +1,197 @@
+/*
+ * 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.streaming.api.utils;
+
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.api.common.typeutils.TypeSerializerSchemaCompatibility;
+import org.apache.flink.api.common.typeutils.TypeSerializerSnapshot;
+import org.apache.flink.core.memory.ByteArrayInputStreamWithPos;
+import org.apache.flink.core.memory.ByteArrayOutputStreamWithPos;
+import org.apache.flink.core.memory.DataInputView;
+import org.apache.flink.core.memory.DataInputViewStreamWrapper;
+import org.apache.flink.core.memory.DataOutputView;
+import org.apache.flink.core.memory.DataOutputViewStreamWrapper;
+
+import java.io.IOException;
+
+/**
+ * The {@link TypeSerializer} to serialize data with type {@link T} into length-prefix byte[] that
+ * allows Python side to determine how long the record is. This wraps a {@link TypeSerializer} for
+ * byte[] and a {@link TypeSerializer} for {@link T}.
+ */
+public class LengthPrefixWrapperSerializer<T> extends TypeSerializer<T> {
+

Review Comment:
   private static final long serialVersionUID = 1L;



##########
flink-python/src/main/java/org/apache/flink/streaming/api/utils/LengthPrefixWrapperSerializer.java:
##########
@@ -0,0 +1,197 @@
+/*
+ * 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.streaming.api.utils;
+
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.api.common.typeutils.TypeSerializerSchemaCompatibility;
+import org.apache.flink.api.common.typeutils.TypeSerializerSnapshot;
+import org.apache.flink.core.memory.ByteArrayInputStreamWithPos;
+import org.apache.flink.core.memory.ByteArrayOutputStreamWithPos;
+import org.apache.flink.core.memory.DataInputView;
+import org.apache.flink.core.memory.DataInputViewStreamWrapper;
+import org.apache.flink.core.memory.DataOutputView;
+import org.apache.flink.core.memory.DataOutputViewStreamWrapper;
+
+import java.io.IOException;
+
+/**
+ * The {@link TypeSerializer} to serialize data with type {@link T} into length-prefix byte[] that
+ * allows Python side to determine how long the record is. This wraps a {@link TypeSerializer} for
+ * byte[] and a {@link TypeSerializer} for {@link T}.
+ */
+public class LengthPrefixWrapperSerializer<T> extends TypeSerializer<T> {
+
+    private final TypeSerializer<byte[]> bytesSerializer;
+
+    private final TypeSerializer<T> dataSerializer;
+
+    private final ByteArrayInputStreamWithPos bais;
+
+    private final DataInputViewStreamWrapper baisWrapper;
+
+    private final ByteArrayOutputStreamWithPos baos;
+
+    private final DataOutputViewStreamWrapper baosWrapper;
+
+    public LengthPrefixWrapperSerializer(
+            TypeSerializer<byte[]> bytesSerializer, TypeSerializer<T> dataSerializer) {
+        this.bytesSerializer = bytesSerializer;
+        this.dataSerializer = dataSerializer;
+        this.bais = new ByteArrayInputStreamWithPos();
+        this.baisWrapper = new DataInputViewStreamWrapper(this.bais);
+        this.baos = new ByteArrayOutputStreamWithPos();
+        this.baosWrapper = new DataOutputViewStreamWrapper(this.baos);
+    }
+
+    @Override
+    public boolean isImmutableType() {
+        return dataSerializer.isImmutableType() && bytesSerializer.isImmutableType();
+    }
+
+    @Override
+    public TypeSerializer<T> duplicate() {
+        return new LengthPrefixWrapperSerializer<>(
+                bytesSerializer.duplicate(), dataSerializer.duplicate());
+    }
+
+    @Override
+    public T createInstance() {
+        return dataSerializer.createInstance();
+    }
+
+    @Override
+    public T copy(T from) {
+        return dataSerializer.copy(from);
+    }
+
+    @Override
+    public T copy(T from, T reuse) {
+        return dataSerializer.copy(from, reuse);
+    }
+
+    @Override
+    public int getLength() {
+        return bytesSerializer.getLength();
+    }
+
+    @Override
+    public void serialize(T record, DataOutputView target) throws IOException {
+        dataSerializer.serialize(record, baosWrapper);
+        bytesSerializer.serialize(baos.toByteArray(), target);
+        baos.reset();
+    }
+
+    @Override
+    public T deserialize(DataInputView source) throws IOException {
+        byte[] bytes = bytesSerializer.deserialize(source);
+        bais.setBuffer(bytes, 0, bytes.length);
+        return dataSerializer.deserialize(baisWrapper);
+    }
+
+    @Override
+    public T deserialize(T reuse, DataInputView source) throws IOException {
+        byte[] bytes = bytesSerializer.deserialize(source);
+        bais.setBuffer(bytes, 0, bytes.length);
+        return dataSerializer.deserialize(reuse, baisWrapper);
+    }
+
+    @Override
+    public void copy(DataInputView source, DataOutputView target) throws IOException {
+        bytesSerializer.copy(source, target);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        return obj.getClass().equals(this.getClass()) && bytesSerializer.equals(obj);

Review Comment:
   ```suggestion
           return obj.getClass().equals(this.getClass()) && dataSerializer.equals(obj.getDataSerializer());
   ```



##########
flink-python/src/main/java/org/apache/flink/streaming/api/utils/LengthPrefixWrapperSerializer.java:
##########
@@ -0,0 +1,197 @@
+/*
+ * 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.streaming.api.utils;
+
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.api.common.typeutils.TypeSerializerSchemaCompatibility;
+import org.apache.flink.api.common.typeutils.TypeSerializerSnapshot;
+import org.apache.flink.core.memory.ByteArrayInputStreamWithPos;
+import org.apache.flink.core.memory.ByteArrayOutputStreamWithPos;
+import org.apache.flink.core.memory.DataInputView;
+import org.apache.flink.core.memory.DataInputViewStreamWrapper;
+import org.apache.flink.core.memory.DataOutputView;
+import org.apache.flink.core.memory.DataOutputViewStreamWrapper;
+
+import java.io.IOException;
+
+/**
+ * The {@link TypeSerializer} to serialize data with type {@link T} into length-prefix byte[] that
+ * allows Python side to determine how long the record is. This wraps a {@link TypeSerializer} for
+ * byte[] and a {@link TypeSerializer} for {@link T}.
+ */
+public class LengthPrefixWrapperSerializer<T> extends TypeSerializer<T> {
+
+    private final TypeSerializer<byte[]> bytesSerializer;
+
+    private final TypeSerializer<T> dataSerializer;
+
+    private final ByteArrayInputStreamWithPos bais;
+
+    private final DataInputViewStreamWrapper baisWrapper;
+
+    private final ByteArrayOutputStreamWithPos baos;
+
+    private final DataOutputViewStreamWrapper baosWrapper;
+
+    public LengthPrefixWrapperSerializer(
+            TypeSerializer<byte[]> bytesSerializer, TypeSerializer<T> dataSerializer) {
+        this.bytesSerializer = bytesSerializer;

Review Comment:
   ```suggestion
           this.bytesSerializer = BytePrimitiveArraySerializer.INSTANCE;
   ```
   the parameter bytesSerializer could be removed



##########
flink-python/src/main/java/org/apache/flink/streaming/api/utils/LengthPrefixWrapperSerializer.java:
##########
@@ -0,0 +1,197 @@
+/*
+ * 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.streaming.api.utils;
+
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.api.common.typeutils.TypeSerializerSchemaCompatibility;
+import org.apache.flink.api.common.typeutils.TypeSerializerSnapshot;
+import org.apache.flink.core.memory.ByteArrayInputStreamWithPos;
+import org.apache.flink.core.memory.ByteArrayOutputStreamWithPos;
+import org.apache.flink.core.memory.DataInputView;
+import org.apache.flink.core.memory.DataInputViewStreamWrapper;
+import org.apache.flink.core.memory.DataOutputView;
+import org.apache.flink.core.memory.DataOutputViewStreamWrapper;
+
+import java.io.IOException;
+
+/**
+ * The {@link TypeSerializer} to serialize data with type {@link T} into length-prefix byte[] that
+ * allows Python side to determine how long the record is. This wraps a {@link TypeSerializer} for
+ * byte[] and a {@link TypeSerializer} for {@link T}.
+ */
+public class LengthPrefixWrapperSerializer<T> extends TypeSerializer<T> {
+
+    private final TypeSerializer<byte[]> bytesSerializer;
+
+    private final TypeSerializer<T> dataSerializer;
+
+    private final ByteArrayInputStreamWithPos bais;
+
+    private final DataInputViewStreamWrapper baisWrapper;
+
+    private final ByteArrayOutputStreamWithPos baos;
+
+    private final DataOutputViewStreamWrapper baosWrapper;
+
+    public LengthPrefixWrapperSerializer(
+            TypeSerializer<byte[]> bytesSerializer, TypeSerializer<T> dataSerializer) {
+        this.bytesSerializer = bytesSerializer;
+        this.dataSerializer = dataSerializer;
+        this.bais = new ByteArrayInputStreamWithPos();
+        this.baisWrapper = new DataInputViewStreamWrapper(this.bais);
+        this.baos = new ByteArrayOutputStreamWithPos();
+        this.baosWrapper = new DataOutputViewStreamWrapper(this.baos);
+    }
+
+    @Override
+    public boolean isImmutableType() {
+        return dataSerializer.isImmutableType() && bytesSerializer.isImmutableType();

Review Comment:
   ```suggestion
           return dataSerializer.isImmutableType();
   ```



##########
flink-python/src/main/java/org/apache/flink/streaming/api/utils/LengthPrefixWrapperSerializer.java:
##########
@@ -0,0 +1,197 @@
+/*
+ * 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.streaming.api.utils;
+
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.api.common.typeutils.TypeSerializerSchemaCompatibility;
+import org.apache.flink.api.common.typeutils.TypeSerializerSnapshot;
+import org.apache.flink.core.memory.ByteArrayInputStreamWithPos;
+import org.apache.flink.core.memory.ByteArrayOutputStreamWithPos;
+import org.apache.flink.core.memory.DataInputView;
+import org.apache.flink.core.memory.DataInputViewStreamWrapper;
+import org.apache.flink.core.memory.DataOutputView;
+import org.apache.flink.core.memory.DataOutputViewStreamWrapper;
+
+import java.io.IOException;
+
+/**
+ * The {@link TypeSerializer} to serialize data with type {@link T} into length-prefix byte[] that
+ * allows Python side to determine how long the record is. This wraps a {@link TypeSerializer} for
+ * byte[] and a {@link TypeSerializer} for {@link T}.
+ */
+public class LengthPrefixWrapperSerializer<T> extends TypeSerializer<T> {
+
+    private final TypeSerializer<byte[]> bytesSerializer;
+
+    private final TypeSerializer<T> dataSerializer;
+
+    private final ByteArrayInputStreamWithPos bais;
+
+    private final DataInputViewStreamWrapper baisWrapper;
+
+    private final ByteArrayOutputStreamWithPos baos;
+
+    private final DataOutputViewStreamWrapper baosWrapper;
+
+    public LengthPrefixWrapperSerializer(
+            TypeSerializer<byte[]> bytesSerializer, TypeSerializer<T> dataSerializer) {
+        this.bytesSerializer = bytesSerializer;
+        this.dataSerializer = dataSerializer;
+        this.bais = new ByteArrayInputStreamWithPos();
+        this.baisWrapper = new DataInputViewStreamWrapper(this.bais);
+        this.baos = new ByteArrayOutputStreamWithPos();
+        this.baosWrapper = new DataOutputViewStreamWrapper(this.baos);
+    }
+
+    @Override
+    public boolean isImmutableType() {
+        return dataSerializer.isImmutableType() && bytesSerializer.isImmutableType();
+    }
+
+    @Override
+    public TypeSerializer<T> duplicate() {
+        return new LengthPrefixWrapperSerializer<>(
+                bytesSerializer.duplicate(), dataSerializer.duplicate());
+    }
+
+    @Override
+    public T createInstance() {
+        return dataSerializer.createInstance();
+    }
+
+    @Override
+    public T copy(T from) {
+        return dataSerializer.copy(from);
+    }
+
+    @Override
+    public T copy(T from, T reuse) {
+        return dataSerializer.copy(from, reuse);
+    }
+
+    @Override
+    public int getLength() {
+        return bytesSerializer.getLength();
+    }
+
+    @Override
+    public void serialize(T record, DataOutputView target) throws IOException {
+        dataSerializer.serialize(record, baosWrapper);
+        bytesSerializer.serialize(baos.toByteArray(), target);
+        baos.reset();
+    }
+
+    @Override
+    public T deserialize(DataInputView source) throws IOException {
+        byte[] bytes = bytesSerializer.deserialize(source);
+        bais.setBuffer(bytes, 0, bytes.length);
+        return dataSerializer.deserialize(baisWrapper);
+    }
+
+    @Override
+    public T deserialize(T reuse, DataInputView source) throws IOException {
+        byte[] bytes = bytesSerializer.deserialize(source);
+        bais.setBuffer(bytes, 0, bytes.length);
+        return dataSerializer.deserialize(reuse, baisWrapper);
+    }
+
+    @Override
+    public void copy(DataInputView source, DataOutputView target) throws IOException {
+        bytesSerializer.copy(source, target);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        return obj.getClass().equals(this.getClass()) && bytesSerializer.equals(obj);
+    }
+
+    @Override
+    public int hashCode() {
+        return bytesSerializer.hashCode();
+    }
+
+    @Override
+    public TypeSerializerSnapshot<T> snapshotConfiguration() {
+        return new LengthPrefixWrapperSerializerSnapshot<>(
+                bytesSerializer.snapshotConfiguration(), dataSerializer.snapshotConfiguration());
+    }
+
+    /**
+     * {@link LengthPrefixWrapperSerializerSnapshot} wraps a serializer snapshot for byte[] and
+     * another for {@link T}.
+     */
+    public static class LengthPrefixWrapperSerializerSnapshot<T>
+            implements TypeSerializerSnapshot<T> {
+
+        private final TypeSerializerSnapshot<byte[]> bytesSerializerSnapshot;
+
+        private final TypeSerializerSnapshot<T> dataSerializerSnapshot;
+
+        public LengthPrefixWrapperSerializerSnapshot(
+                TypeSerializerSnapshot<byte[]> bytesSerializerSnapshot,
+                TypeSerializerSnapshot<T> dataSerializerSnapshot) {
+            this.bytesSerializerSnapshot = bytesSerializerSnapshot;
+            this.dataSerializerSnapshot = dataSerializerSnapshot;
+        }
+
+        @Override
+        public int getCurrentVersion() {
+            return dataSerializerSnapshot.getCurrentVersion();

Review Comment:
   ```suggestion
               return 1;
   ```



##########
flink-python/src/main/java/org/apache/flink/streaming/api/utils/LengthPrefixWrapperSerializer.java:
##########
@@ -0,0 +1,197 @@
+/*
+ * 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.streaming.api.utils;
+
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.api.common.typeutils.TypeSerializerSchemaCompatibility;
+import org.apache.flink.api.common.typeutils.TypeSerializerSnapshot;
+import org.apache.flink.core.memory.ByteArrayInputStreamWithPos;
+import org.apache.flink.core.memory.ByteArrayOutputStreamWithPos;
+import org.apache.flink.core.memory.DataInputView;
+import org.apache.flink.core.memory.DataInputViewStreamWrapper;
+import org.apache.flink.core.memory.DataOutputView;
+import org.apache.flink.core.memory.DataOutputViewStreamWrapper;
+
+import java.io.IOException;
+
+/**
+ * The {@link TypeSerializer} to serialize data with type {@link T} into length-prefix byte[] that
+ * allows Python side to determine how long the record is. This wraps a {@link TypeSerializer} for
+ * byte[] and a {@link TypeSerializer} for {@link T}.
+ */
+public class LengthPrefixWrapperSerializer<T> extends TypeSerializer<T> {
+
+    private final TypeSerializer<byte[]> bytesSerializer;
+
+    private final TypeSerializer<T> dataSerializer;
+
+    private final ByteArrayInputStreamWithPos bais;
+
+    private final DataInputViewStreamWrapper baisWrapper;
+
+    private final ByteArrayOutputStreamWithPos baos;
+
+    private final DataOutputViewStreamWrapper baosWrapper;
+
+    public LengthPrefixWrapperSerializer(
+            TypeSerializer<byte[]> bytesSerializer, TypeSerializer<T> dataSerializer) {
+        this.bytesSerializer = bytesSerializer;
+        this.dataSerializer = dataSerializer;
+        this.bais = new ByteArrayInputStreamWithPos();
+        this.baisWrapper = new DataInputViewStreamWrapper(this.bais);
+        this.baos = new ByteArrayOutputStreamWithPos();
+        this.baosWrapper = new DataOutputViewStreamWrapper(this.baos);
+    }
+
+    @Override
+    public boolean isImmutableType() {
+        return dataSerializer.isImmutableType() && bytesSerializer.isImmutableType();
+    }
+
+    @Override
+    public TypeSerializer<T> duplicate() {
+        return new LengthPrefixWrapperSerializer<>(
+                bytesSerializer.duplicate(), dataSerializer.duplicate());
+    }
+
+    @Override
+    public T createInstance() {
+        return dataSerializer.createInstance();
+    }
+
+    @Override
+    public T copy(T from) {
+        return dataSerializer.copy(from);
+    }
+
+    @Override
+    public T copy(T from, T reuse) {
+        return dataSerializer.copy(from, reuse);
+    }
+
+    @Override
+    public int getLength() {
+        return bytesSerializer.getLength();
+    }
+
+    @Override
+    public void serialize(T record, DataOutputView target) throws IOException {
+        dataSerializer.serialize(record, baosWrapper);
+        bytesSerializer.serialize(baos.toByteArray(), target);
+        baos.reset();
+    }
+
+    @Override
+    public T deserialize(DataInputView source) throws IOException {
+        byte[] bytes = bytesSerializer.deserialize(source);
+        bais.setBuffer(bytes, 0, bytes.length);
+        return dataSerializer.deserialize(baisWrapper);
+    }
+
+    @Override
+    public T deserialize(T reuse, DataInputView source) throws IOException {
+        byte[] bytes = bytesSerializer.deserialize(source);
+        bais.setBuffer(bytes, 0, bytes.length);
+        return dataSerializer.deserialize(reuse, baisWrapper);
+    }
+
+    @Override
+    public void copy(DataInputView source, DataOutputView target) throws IOException {
+        bytesSerializer.copy(source, target);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        return obj.getClass().equals(this.getClass()) && bytesSerializer.equals(obj);
+    }
+
+    @Override
+    public int hashCode() {
+        return bytesSerializer.hashCode();
+    }
+
+    @Override
+    public TypeSerializerSnapshot<T> snapshotConfiguration() {
+        return new LengthPrefixWrapperSerializerSnapshot<>(
+                bytesSerializer.snapshotConfiguration(), dataSerializer.snapshotConfiguration());
+    }
+
+    /**
+     * {@link LengthPrefixWrapperSerializerSnapshot} wraps a serializer snapshot for byte[] and
+     * another for {@link T}.
+     */
+    public static class LengthPrefixWrapperSerializerSnapshot<T>
+            implements TypeSerializerSnapshot<T> {
+
+        private final TypeSerializerSnapshot<byte[]> bytesSerializerSnapshot;
+
+        private final TypeSerializerSnapshot<T> dataSerializerSnapshot;
+
+        public LengthPrefixWrapperSerializerSnapshot(
+                TypeSerializerSnapshot<byte[]> bytesSerializerSnapshot,
+                TypeSerializerSnapshot<T> dataSerializerSnapshot) {
+            this.bytesSerializerSnapshot = bytesSerializerSnapshot;
+            this.dataSerializerSnapshot = dataSerializerSnapshot;
+        }
+
+        @Override
+        public int getCurrentVersion() {
+            return dataSerializerSnapshot.getCurrentVersion();
+        }
+
+        @Override
+        public void writeSnapshot(DataOutputView out) throws IOException {
+            bytesSerializerSnapshot.writeSnapshot(out);
+            dataSerializerSnapshot.writeSnapshot(out);
+        }
+
+        @Override
+        public void readSnapshot(int readVersion, DataInputView in, ClassLoader userCodeClassLoader)
+                throws IOException {
+            bytesSerializerSnapshot.readSnapshot(readVersion, in, userCodeClassLoader);

Review Comment:
   I guess we should use *TypeSerializerSnapshot.readVersionedSnapshot* here.
   
   Could we add a test case for this class?



##########
flink-python/pyflink/fn_execution/formats/avro.py:
##########
@@ -0,0 +1,138 @@
+################################################################################
+#  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.
+################################################################################
+from avro.io import DatumReader, SchemaResolutionException, BinaryDecoder
+
+
+class FlinkAvroDecoder(BinaryDecoder):
+

Review Comment:
   Could you add some documentation about this class, e.g. why need to introduce this class instead of using BinaryDecoder?



##########
flink-python/pyflink/fn_execution/formats/avro.py:
##########
@@ -0,0 +1,138 @@
+################################################################################
+#  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.
+################################################################################
+from avro.io import DatumReader, SchemaResolutionException, BinaryDecoder
+
+
+class FlinkAvroDecoder(BinaryDecoder):
+
+    def __init__(self, reader):
+        super(FlinkAvroDecoder, self).__init__(reader)
+
+    def read_int(self):
+        return ((ord(self.read(1)) << 24) +
+                (ord(self.read(1)) << 16) +
+                (ord(self.read(1)) << 8) +
+                ord(self.read(1)))
+
+    def read_long(self):
+        return ((ord(self.read(1)) << 56) +
+                (ord(self.read(1)) << 48) +
+                (ord(self.read(1)) << 40) +
+                (ord(self.read(1)) << 32) +
+                (ord(self.read(1)) << 24) +
+                (ord(self.read(1)) << 16) +
+                (ord(self.read(1)) << 8) +
+                ord(self.read(1)))
+
+    def read_var_long(self):
+        """
+        Flink implementation of variable-sized long serialization does not move sign to lower bit
+        and flip the rest.
+        """
+        b = ord(self.read(1))
+        n = b & 0x7F
+        shift = 7
+        while (b & 0x80) != 0:
+            b = ord(self.read(1))
+            n |= (b & 0x7F) << shift
+            shift += 7
+        return n
+
+    def read_bytes(self):
+        nbytes = self.read_int()
+        assert (nbytes >= 0), nbytes
+        return self.read(nbytes)
+
+    def skip_int(self):
+        self.skip(4)
+
+    def skip_long(self):
+        self.skip(8)
+
+    def skip_bytes(self):
+        nbytes = self.read_int()
+        assert (nbytes >= 0), nbytes
+        self.skip(nbytes)
+
+
+class FlinkAvroDatumReader(DatumReader):

Review Comment:
   Same as the above, it's not clear for me why we need to introduce this class.



##########
flink-python/src/main/java/org/apache/flink/streaming/api/utils/LengthPrefixWrapperSerializer.java:
##########
@@ -0,0 +1,197 @@
+/*
+ * 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.streaming.api.utils;
+
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.api.common.typeutils.TypeSerializerSchemaCompatibility;
+import org.apache.flink.api.common.typeutils.TypeSerializerSnapshot;
+import org.apache.flink.core.memory.ByteArrayInputStreamWithPos;
+import org.apache.flink.core.memory.ByteArrayOutputStreamWithPos;
+import org.apache.flink.core.memory.DataInputView;
+import org.apache.flink.core.memory.DataInputViewStreamWrapper;
+import org.apache.flink.core.memory.DataOutputView;
+import org.apache.flink.core.memory.DataOutputViewStreamWrapper;
+
+import java.io.IOException;
+
+/**
+ * The {@link TypeSerializer} to serialize data with type {@link T} into length-prefix byte[] that
+ * allows Python side to determine how long the record is. This wraps a {@link TypeSerializer} for
+ * byte[] and a {@link TypeSerializer} for {@link T}.
+ */
+public class LengthPrefixWrapperSerializer<T> extends TypeSerializer<T> {

Review Comment:
   Add `@Internal`



##########
flink-python/src/main/java/org/apache/flink/streaming/api/utils/LengthPrefixWrapperSerializer.java:
##########
@@ -0,0 +1,197 @@
+/*
+ * 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.streaming.api.utils;
+
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.api.common.typeutils.TypeSerializerSchemaCompatibility;
+import org.apache.flink.api.common.typeutils.TypeSerializerSnapshot;
+import org.apache.flink.core.memory.ByteArrayInputStreamWithPos;
+import org.apache.flink.core.memory.ByteArrayOutputStreamWithPos;
+import org.apache.flink.core.memory.DataInputView;
+import org.apache.flink.core.memory.DataInputViewStreamWrapper;
+import org.apache.flink.core.memory.DataOutputView;
+import org.apache.flink.core.memory.DataOutputViewStreamWrapper;
+
+import java.io.IOException;
+
+/**
+ * The {@link TypeSerializer} to serialize data with type {@link T} into length-prefix byte[] that
+ * allows Python side to determine how long the record is. This wraps a {@link TypeSerializer} for
+ * byte[] and a {@link TypeSerializer} for {@link T}.
+ */
+public class LengthPrefixWrapperSerializer<T> extends TypeSerializer<T> {
+
+    private final TypeSerializer<byte[]> bytesSerializer;
+
+    private final TypeSerializer<T> dataSerializer;
+
+    private final ByteArrayInputStreamWithPos bais;
+
+    private final DataInputViewStreamWrapper baisWrapper;
+
+    private final ByteArrayOutputStreamWithPos baos;
+
+    private final DataOutputViewStreamWrapper baosWrapper;
+
+    public LengthPrefixWrapperSerializer(
+            TypeSerializer<byte[]> bytesSerializer, TypeSerializer<T> dataSerializer) {
+        this.bytesSerializer = bytesSerializer;
+        this.dataSerializer = dataSerializer;
+        this.bais = new ByteArrayInputStreamWithPos();
+        this.baisWrapper = new DataInputViewStreamWrapper(this.bais);
+        this.baos = new ByteArrayOutputStreamWithPos();
+        this.baosWrapper = new DataOutputViewStreamWrapper(this.baos);
+    }
+
+    @Override
+    public boolean isImmutableType() {
+        return dataSerializer.isImmutableType() && bytesSerializer.isImmutableType();
+    }
+
+    @Override
+    public TypeSerializer<T> duplicate() {
+        return new LengthPrefixWrapperSerializer<>(
+                bytesSerializer.duplicate(), dataSerializer.duplicate());
+    }
+
+    @Override
+    public T createInstance() {
+        return dataSerializer.createInstance();
+    }
+
+    @Override
+    public T copy(T from) {
+        return dataSerializer.copy(from);
+    }
+
+    @Override
+    public T copy(T from, T reuse) {
+        return dataSerializer.copy(from, reuse);
+    }
+
+    @Override
+    public int getLength() {
+        return bytesSerializer.getLength();

Review Comment:
   ```suggestion
           return dataSerializer.getLength();
   ```



##########
flink-python/src/main/java/org/apache/flink/streaming/api/utils/LengthPrefixWrapperSerializer.java:
##########
@@ -0,0 +1,197 @@
+/*
+ * 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.streaming.api.utils;
+
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.api.common.typeutils.TypeSerializerSchemaCompatibility;
+import org.apache.flink.api.common.typeutils.TypeSerializerSnapshot;
+import org.apache.flink.core.memory.ByteArrayInputStreamWithPos;
+import org.apache.flink.core.memory.ByteArrayOutputStreamWithPos;
+import org.apache.flink.core.memory.DataInputView;
+import org.apache.flink.core.memory.DataInputViewStreamWrapper;
+import org.apache.flink.core.memory.DataOutputView;
+import org.apache.flink.core.memory.DataOutputViewStreamWrapper;
+
+import java.io.IOException;
+
+/**
+ * The {@link TypeSerializer} to serialize data with type {@link T} into length-prefix byte[] that
+ * allows Python side to determine how long the record is. This wraps a {@link TypeSerializer} for
+ * byte[] and a {@link TypeSerializer} for {@link T}.
+ */
+public class LengthPrefixWrapperSerializer<T> extends TypeSerializer<T> {
+
+    private final TypeSerializer<byte[]> bytesSerializer;
+
+    private final TypeSerializer<T> dataSerializer;
+
+    private final ByteArrayInputStreamWithPos bais;
+
+    private final DataInputViewStreamWrapper baisWrapper;
+
+    private final ByteArrayOutputStreamWithPos baos;
+
+    private final DataOutputViewStreamWrapper baosWrapper;
+
+    public LengthPrefixWrapperSerializer(
+            TypeSerializer<byte[]> bytesSerializer, TypeSerializer<T> dataSerializer) {
+        this.bytesSerializer = bytesSerializer;
+        this.dataSerializer = dataSerializer;
+        this.bais = new ByteArrayInputStreamWithPos();
+        this.baisWrapper = new DataInputViewStreamWrapper(this.bais);
+        this.baos = new ByteArrayOutputStreamWithPos();
+        this.baosWrapper = new DataOutputViewStreamWrapper(this.baos);
+    }
+
+    @Override
+    public boolean isImmutableType() {
+        return dataSerializer.isImmutableType() && bytesSerializer.isImmutableType();
+    }
+
+    @Override
+    public TypeSerializer<T> duplicate() {
+        return new LengthPrefixWrapperSerializer<>(
+                bytesSerializer.duplicate(), dataSerializer.duplicate());
+    }
+
+    @Override
+    public T createInstance() {
+        return dataSerializer.createInstance();
+    }
+
+    @Override
+    public T copy(T from) {
+        return dataSerializer.copy(from);
+    }
+
+    @Override
+    public T copy(T from, T reuse) {
+        return dataSerializer.copy(from, reuse);
+    }
+
+    @Override
+    public int getLength() {
+        return bytesSerializer.getLength();
+    }
+
+    @Override
+    public void serialize(T record, DataOutputView target) throws IOException {
+        dataSerializer.serialize(record, baosWrapper);
+        bytesSerializer.serialize(baos.toByteArray(), target);
+        baos.reset();
+    }
+
+    @Override
+    public T deserialize(DataInputView source) throws IOException {
+        byte[] bytes = bytesSerializer.deserialize(source);
+        bais.setBuffer(bytes, 0, bytes.length);
+        return dataSerializer.deserialize(baisWrapper);
+    }
+
+    @Override
+    public T deserialize(T reuse, DataInputView source) throws IOException {
+        byte[] bytes = bytesSerializer.deserialize(source);
+        bais.setBuffer(bytes, 0, bytes.length);
+        return dataSerializer.deserialize(reuse, baisWrapper);
+    }
+
+    @Override
+    public void copy(DataInputView source, DataOutputView target) throws IOException {
+        bytesSerializer.copy(source, target);
+    }
+
+    @Override
+    public boolean equals(Object obj) {
+        return obj.getClass().equals(this.getClass()) && bytesSerializer.equals(obj);
+    }
+
+    @Override
+    public int hashCode() {
+        return bytesSerializer.hashCode();

Review Comment:
   ```suggestion
           return dataSerializer.hashCode();
   ```



-- 
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-unsubscribe@flink.apache.org

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