hequn8128 commented on a change in pull request #9707: [FLINK-14015][python] 
Introduces PythonScalarFunctionOperator to execute Python user-defined functions
URL: https://github.com/apache/flink/pull/9707#discussion_r326520187
 
 

 ##########
 File path: 
flink-python/src/main/java/org/apache/flink/table/runtime/operators/python/AbstractPythonScalarFunctionOperator.java
 ##########
 @@ -0,0 +1,155 @@
+/*
+ * 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.runtime.operators.python;
+
+import org.apache.flink.annotation.Internal;
+import 
org.apache.flink.streaming.api.operators.python.AbstractPythonFunctionOperator;
+import org.apache.flink.streaming.runtime.streamrecord.StreamRecord;
+import org.apache.flink.table.functions.ScalarFunction;
+import org.apache.flink.table.functions.python.PythonFunctionInfo;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.util.Preconditions;
+
+import java.util.Arrays;
+import java.util.stream.Collectors;
+
+/**
+ * Base class for all stream operators to execute Python {@link 
ScalarFunction}s.
+ *
+ * @param <IN> Type of the input elements.
+ * @param <OUT> Type of the execution results.
+ */
+@Internal
+public abstract class AbstractPythonScalarFunctionOperator<IN, OUT> extends 
AbstractPythonFunctionOperator<IN, OUT> {
+
+       private static final long serialVersionUID = 1L;
+
+       /**
+        * The Python {@link ScalarFunction}s to be executed.
+        */
+       private final PythonFunctionInfo[] scalarFunctions;
+
+       /**
+        * The input logical type.
+        */
+       private final RowType inputType;
+
+       /**
+        * The output logical type.
+        */
+       private final RowType outputType;
+
+       /**
+        * The offsets of udf inputs.
+        */
+       private final int[] udfInputOffsets;
+
+       /**
+        * The number of forwarded fields in the input element.
+        */
+       private final int forwardedFieldCnt;
+
+       /**
+        * The udf input logical type.
+        */
+       private transient RowType udfInputType;
+
+       /**
+        * The udf output logical type.
+        */
+       private transient RowType udfOutputType;
+
+       AbstractPythonScalarFunctionOperator(
+               PythonFunctionInfo[] scalarFunctions,
+               RowType inputType,
+               RowType outputType,
+               int[] udfInputOffsets,
+               int forwardedFieldCnt) {
+               this.scalarFunctions = 
Preconditions.checkNotNull(scalarFunctions);
+               this.inputType = Preconditions.checkNotNull(inputType);
+               this.outputType = Preconditions.checkNotNull(outputType);
+               this.udfInputOffsets = 
Preconditions.checkNotNull(udfInputOffsets);
+               this.forwardedFieldCnt = forwardedFieldCnt;
+       }
+
+       @Override
+       public void open() throws Exception {
+               udfInputType = new RowType(
+                       Arrays.stream(udfInputOffsets)
+                               .mapToObj(i -> inputType.getFields().get(i))
+                               .collect(Collectors.toList()));
+               udfOutputType = new 
RowType(outputType.getFields().subList(forwardedFieldCnt, 
outputType.getFieldCount()));
+               super.open();
+       }
+
+       @Override
+       public void processElement(StreamRecord<IN> element) throws Exception {
+               bufferInput(element.getValue());
+               super.processElement(element);
+               emitResults();
+       }
+
+       /**
+        * Buffers the specified input, it will be used to construct
+        * the operator result together with the udf execution result.
+        */
+       public abstract void bufferInput(IN input);
+
+       /**
+        * Returns the Python scalar functions.
+        */
+       public PythonFunctionInfo[] getScalarFunctions() {
+               return scalarFunctions;
+       }
+
+       /**
+        * Returns the input logical type.
+        */
+       public RowType getInputType() {
+               return inputType;
+       }
+
+       /**
+        * Returns the offsets of udf inputs.
+        */
+       public int[] getUdfInputOffsets() {
+               return udfInputOffsets;
+       }
+
+       /**
+        * Returns the output logical type.
 
 Review comment:
   return cnt?

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