dianfu commented on a change in pull request #9653: [FLINK-14014][python] 
Introduce PythonScalarFunctionRunner to handle the communication with Python 
worker for Python ScalarFunction execution
URL: https://github.com/apache/flink/pull/9653#discussion_r324628899
 
 

 ##########
 File path: 
flink-python/src/main/java/org/apache/flink/table/functions/python/coders/BaseRowCoder.java
 ##########
 @@ -0,0 +1,145 @@
+/*
+ * 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.functions.python.coders;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.core.memory.DataOutputView;
+import org.apache.flink.table.dataformat.BaseRow;
+import org.apache.flink.table.dataformat.GenericRow;
+import org.apache.flink.table.dataformat.TypeGetterSetters;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.util.Preconditions;
+
+import org.apache.beam.sdk.coders.Coder;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Collections;
+import java.util.List;
+
+import static 
org.apache.flink.api.java.typeutils.runtime.NullMaskUtils.readIntoNullMask;
+
+/**
+ * A {@link Coder} for {@link BaseRow}.
+ */
+@Internal
+public class BaseRowCoder extends Coder<BaseRow> {
+
+       private static final long serialVersionUID = 1L;
+
+       private final Coder<Object>[] fieldCoders;
+       private final LogicalType[] fieldTypes;
+
+       private transient ReusableDataInputView reusableInputStream;
+       private transient ReusableDataOutputView reusableOutputStream;
+
+       private transient boolean[] nullMask;
+
+       @SuppressWarnings("unchecked")
+       public BaseRowCoder(Coder<?>[] fieldCoders, LogicalType[] fieldTypes) {
+               this.fieldCoders = (Coder<Object>[]) 
Preconditions.checkNotNull(fieldCoders);
+               this.fieldTypes = Preconditions.checkNotNull(fieldTypes);
+               this.reusableInputStream = new ReusableDataInputView();
+               this.reusableOutputStream = new ReusableDataOutputView();
+               this.nullMask = new boolean[fieldCoders.length];
+       }
+
+       public Coder<?>[] getFieldCoders() {
+               return this.fieldCoders;
+       }
+
+       @Override
+       public void encode(BaseRow row, OutputStream outStream) throws 
IOException {
+               int len = fieldCoders.length;
+
+               if (row.getArity() != len) {
+                       throw new RuntimeException("Row arity of input element 
does not match coders.");
+               }
+
+               // write a null mask
+               reusableOutputStream.reset(outStream);
+               writeNullMask(len, row, reusableOutputStream);
+
+               for (int i = 0; i < row.getArity(); i++) {
+                       if (!row.isNullAt(i)) {
+                               
fieldCoders[i].encode(TypeGetterSetters.get(row, i, fieldTypes[i]), outStream);
 
 Review comment:
   +1

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to