wuchong commented on a change in pull request #13909:
URL: https://github.com/apache/flink/pull/13909#discussion_r518136335



##########
File path: 
flink-table/flink-table-runtime-blink/src/main/java/org/apache/flink/table/formats/singlefield/SingleFieldSerializationSchema.java
##########
@@ -0,0 +1,166 @@
+/*
+ * 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.formats.singlefield;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.serialization.SerializationSchema;
+import org.apache.flink.api.common.typeutils.TypeSerializer;
+import org.apache.flink.api.common.typeutils.base.BooleanSerializer;
+import org.apache.flink.api.common.typeutils.base.ByteSerializer;
+import org.apache.flink.api.common.typeutils.base.DoubleSerializer;
+import org.apache.flink.api.common.typeutils.base.FloatSerializer;
+import org.apache.flink.api.common.typeutils.base.IntSerializer;
+import org.apache.flink.api.common.typeutils.base.LongSerializer;
+import org.apache.flink.api.common.typeutils.base.ShortSerializer;
+import org.apache.flink.core.memory.DataOutputSerializer;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.RowData.FieldGetter;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.types.logical.LogicalType;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.Objects;
+
+/**
+ * Serialization schema that serializes an {@link RowData} object into a 
single field bytes.
+ */
+@Internal
+public class SingleFieldSerializationSchema implements 
SerializationSchema<RowData> {
+
+       private static final long serialVersionUID = 1L;
+
+       private final LogicalType fieldType;
+
+       private final SerializationRuntimeConverter converter;
+
+       private final FieldGetter fieldGetter;
+
+       public SingleFieldSerializationSchema(LogicalType fieldType) {
+               this.fieldType = fieldType;
+               this.fieldGetter = RowData.createFieldGetter(fieldType, 0);
+               this.converter = createConverter(fieldType);
+       }
+
+       @Override
+       public byte[] serialize(RowData row) {
+               try {
+                       return 
converter.convert(fieldGetter.getFieldOrNull(row));
+               } catch (IOException e) {
+                       throw new RuntimeException("Could not serialize row '" 
+ row + "'. ", e);
+               }
+       }
+
+       @Override
+       public boolean equals(Object o) {
+               if (this == o) {
+                       return true;
+               }
+               if (o == null || getClass() != o.getClass()) {
+                       return false;
+               }
+               SingleFieldSerializationSchema that = 
(SingleFieldSerializationSchema) o;
+               return fieldType.equals(that.fieldType);
+       }
+
+       @Override
+       public int hashCode() {
+               return Objects.hash(fieldType);
+       }
+
+       // 
------------------------------------------------------------------------
+
+       /**
+        * Runtime converter that convert an object of internal data structure 
to byte[].
+        */
+       @FunctionalInterface
+       private interface SerializationRuntimeConverter extends Serializable {
+               byte[] convert(Object value) throws IOException;
+       }
+
+       /**
+        * Creates a runtime converter.
+        */
+       private SerializationRuntimeConverter createConverter(LogicalType type) 
{
+               switch (type.getTypeRoot()) {
+                       case CHAR:
+                       case VARCHAR:
+                               return value -> {
+                                       // put null check logic in the lambda 
instead of wrapping outside
+                                       // to avoid virtual method invoking.
+                                       if (value == null) {
+                                               return null;
+                                       }
+                                       return ((StringData) value).toBytes();

Review comment:
       I'm sure this is the same semantic of `"string".getBytes()`, becuase we 
are using `StringData.fromBytes(stringBytes)` to save decoding overhead 
(`stringBytes` to `String`) in the sources. 
   
   The string length is not included in the bytes. The length is appended by 
the `StringDataSerializer`. 




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


Reply via email to