JingsongLi commented on a change in pull request #11896:
URL: https://github.com/apache/flink/pull/11896#discussion_r476362981



##########
File path: 
flink-table/flink-table-runtime-blink/src/main/java/org/apache/flink/table/format/single/SingleValueRowDataDeserialization.java
##########
@@ -0,0 +1,124 @@
+/*
+ * 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.format.single;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.api.common.serialization.DeserializationSchema;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.RowType;
+
+import java.io.IOException;
+import java.io.Serializable;
+import java.nio.ByteBuffer;
+import java.util.Objects;
+
+/**
+ * Deserialization schema from SINGLE-VALUE to Flink Table/SQL internal data 
structure {@link RowData}.
+ */
+@Internal
+public class SingleValueRowDataDeserialization implements 
DeserializationSchema<RowData> {
+
+       private DeserializationRuntimeConverter converter;
+       private RowType rowType;
+       private TypeInformation<RowData> typeInfo;
+
+       public SingleValueRowDataDeserialization(RowType rowType,
+               TypeInformation<RowData> resultTypeInfo) {
+               this.rowType = rowType;
+               this.typeInfo = resultTypeInfo;
+               this.converter = createConverter(rowType.getTypeAt(0));
+       }
+
+       @Override
+       public RowData deserialize(byte[] message) throws IOException {
+               GenericRowData genericRowData = new GenericRowData(1);
+               genericRowData.setField(0, converter.convert(message));
+               return genericRowData;
+       }
+
+       @Override
+       public boolean isEndOfStream(RowData nextElement) {
+               return false;
+       }
+
+       @Override
+       public TypeInformation<RowData> getProducedType() {
+               return typeInfo;
+       }
+
+       /**
+        * Runtime converter that convert byte[] to a single value.
+        */
+       @FunctionalInterface
+       private interface DeserializationRuntimeConverter extends Serializable {
+               Object convert(byte[] message);
+       }
+
+       /**
+        *  Creates a runtime converter.
+        */
+       private DeserializationRuntimeConverter createConverter(LogicalType 
type) {
+               switch (type.getTypeRoot()) {
+                       case CHAR:
+                       case VARCHAR:
+                               return bytes -> StringData.fromBytes(bytes);
+                       case VARBINARY:
+                       case BINARY:
+                               return bytes -> bytes;
+                       case TINYINT:
+                               return bytes -> ByteBuffer.wrap(bytes).get();
+                       case SMALLINT:
+                               return bytes -> 
ByteBuffer.wrap(bytes).getShort();
+                       case INTEGER:
+                               return bytes -> ByteBuffer.wrap(bytes).getInt();
+                       case BIGINT:
+                               return bytes -> 
ByteBuffer.wrap(bytes).getLong();
+                       case FLOAT:
+                               return bytes -> 
ByteBuffer.wrap(bytes).getFloat();
+                       case DOUBLE:
+                               return bytes -> 
ByteBuffer.wrap(bytes).getDouble();
+                       case BOOLEAN:
+                               return bytes -> ByteBuffer.wrap(bytes).get() != 
0;

Review comment:
       More lightweight approach may be `MemoryUtils.UNSAFE`...




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