JingsongLi commented on a change in pull request #7887: 
[FLINK-11802][table-runtime-blink] Create TypeInfo and TypeSerializer for blink 
data format
URL: https://github.com/apache/flink/pull/7887#discussion_r262001647
 
 

 ##########
 File path: 
flink-table/flink-table-runtime-blink/src/main/java/org/apache/flink/table/typeutils/BaseRowSerializer.java
 ##########
 @@ -0,0 +1,292 @@
+/*
+ * 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.table.typeutils;
+
+import org.apache.flink.api.common.ExecutionConfig;
+import org.apache.flink.api.common.typeutils.CompositeTypeSerializerUtil;
+import org.apache.flink.api.common.typeutils.NestedSerializersSnapshotDelegate;
+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.api.java.typeutils.runtime.DataInputViewStream;
+import org.apache.flink.api.java.typeutils.runtime.DataOutputViewStream;
+import org.apache.flink.core.memory.DataInputView;
+import org.apache.flink.core.memory.DataOutputView;
+import org.apache.flink.table.dataformat.BaseRow;
+import org.apache.flink.table.dataformat.BinaryArray;
+import org.apache.flink.table.dataformat.BinaryMap;
+import org.apache.flink.table.dataformat.BinaryRow;
+import org.apache.flink.table.dataformat.BinaryRowWriter;
+import org.apache.flink.table.dataformat.BinaryString;
+import org.apache.flink.table.dataformat.BinaryWriter;
+import org.apache.flink.table.dataformat.GenericRow;
+import org.apache.flink.table.dataformat.TypeGetterSetters;
+import org.apache.flink.table.type.InternalType;
+import org.apache.flink.table.type.TypeConverters;
+import org.apache.flink.util.InstantiationUtil;
+
+import java.io.IOException;
+import java.util.Arrays;
+
+/**
+ * Serializer for BaseRow.
+ */
+public class BaseRowSerializer extends TypeSerializer<BaseRow> {
+
+       private BinaryRowSerializer binarySerializer;
+       private final InternalType[] types;
+       private final TypeSerializer[] fieldSerializers;
+
+       public BaseRowSerializer(InternalType[] types, ExecutionConfig config) {
+               this(types, Arrays.stream(types)
+                               
.map(TypeConverters::createInternalTypeInfoFromInternalType)
+                               .map(ti -> ti.createSerializer(config))
+                               .toArray(TypeSerializer[]::new));
+       }
+
+       public BaseRowSerializer(InternalType[] types, TypeSerializer[] 
fieldSerializers) {
+               this.types = types;
+               this.fieldSerializers = fieldSerializers;
+               this.binarySerializer = new BinaryRowSerializer(types.length);
+       }
+
+       @Override
+       public TypeSerializer<BaseRow> duplicate() {
+               return new BaseRowSerializer(types, fieldSerializers);
+       }
+
+       @Override
+       public BaseRow createInstance() {
+               // default use binary row to deserializer
+               return new BinaryRow(types.length);
+       }
+
+       @Override
+       public BaseRow copy(BaseRow from) {
+               if (from.getArity() != types.length) {
+                       throw new IllegalArgumentException("Row arity: " + 
from.getArity() +
+                                       ", but serializer arity: " + 
types.length);
+               }
+               if (from instanceof BinaryRow) {
+                       return ((BinaryRow) from).copy();
+               } else {
+                       return copyBaseRow(from, new 
GenericRow(from.getArity()));
+               }
+       }
+
+       @Override
+       public BaseRow copy(BaseRow from, BaseRow reuse) {
+               if (from.getArity() != types.length) {
+                       throw new IllegalArgumentException("Row arity: " + 
from.getArity() +
+                                       ", but serializer arity: " + 
types.length);
+               }
+               if (from instanceof BinaryRow) {
+                       return ((BinaryRow) from).copy(reuse);
+               } else {
+                       return copyBaseRow(from, reuse);
+               }
+       }
+
+       private BaseRow copyBaseRow(BaseRow from, BaseRow reuse) {
+               GenericRow ret;
+               if (reuse instanceof GenericRow) {
+                       ret = (GenericRow) reuse;
+               } else {
+                       ret = new GenericRow(from.getArity());
+               }
+               ret.setHeader(from.getHeader());
+               for (int i = 0; i < from.getArity(); i++) {
+                       if (!from.isNullAt(i)) {
+                               ret.setField(i, 
copyValueNotNull(TypeGetterSetters.get(from, i, types[i]), i));
+                       } else {
+                               ret.setNullAt(i);
+                       }
+               }
+               return ret;
+       }
+
+       @SuppressWarnings("unchecked")
+       private Object copyValueNotNull(Object o, int index) {
+               if (o instanceof BinaryString) {
+                       return ((BinaryString) o).copy();
 
 Review comment:
   Yes, let's use Serializer to copy all type.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to