You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2020/02/28 01:34:58 UTC

[GitHub] [flink] dianfu commented on a change in pull request #11208: [FLINK-16271][python] Introduce ArrowPythonScalarFunctionOperator for vectorized Python UDF execution

dianfu commented on a change in pull request #11208: [FLINK-16271][python] Introduce ArrowPythonScalarFunctionOperator for vectorized Python UDF execution
URL: https://github.com/apache/flink/pull/11208#discussion_r385465708
 
 

 ##########
 File path: flink-python/src/main/java/org/apache/flink/table/runtime/operators/python/scalar/arrow/BaseRowArrowPythonScalarFunctionOperator.java
 ##########
 @@ -0,0 +1,132 @@
+/*
+ * 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.scalar.arrow;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.python.PythonFunctionRunner;
+import org.apache.flink.python.env.PythonEnvironmentManager;
+import org.apache.flink.table.dataformat.BaseRow;
+import org.apache.flink.table.dataformat.JoinedRow;
+import org.apache.flink.table.functions.ScalarFunction;
+import org.apache.flink.table.functions.python.PythonFunctionInfo;
+import org.apache.flink.table.runtime.arrow.ArrowReader;
+import org.apache.flink.table.runtime.arrow.ArrowUtils;
+import org.apache.flink.table.runtime.operators.python.scalar.AbstractBaseRowPythonScalarFunctionOperator;
+import org.apache.flink.table.runtime.runners.python.scalar.arrow.BaseRowArrowPythonScalarFunctionRunner;
+import org.apache.flink.table.types.logical.RowType;
+
+import org.apache.arrow.memory.BufferAllocator;
+import org.apache.arrow.vector.VectorSchemaRoot;
+import org.apache.arrow.vector.ipc.ArrowStreamReader;
+import org.apache.beam.sdk.fn.data.FnDataReceiver;
+
+import java.io.IOException;
+
+/**
+ * Arrow Python {@link ScalarFunction} operator for the blink planner.
+ */
+@Internal
+public class BaseRowArrowPythonScalarFunctionOperator extends AbstractBaseRowPythonScalarFunctionOperator {
+
+	private static final long serialVersionUID = 1L;
+
+	/**
+	 * Allocator which is used for byte buffer allocation.
+	 */
+	private transient BufferAllocator allocator;
+
+	/**
+	 * Reader which is responsible for deserialize the Arrow format data to the Flink rows.
+	 */
+	private transient ArrowReader<BaseRow> arrowReader;
+
+	/**
+	 * Reader which is responsible for convert the execution result from
+	 * byte array to arrow format.
+	 */
+	private transient ArrowStreamReader reader;
+
+	/**
+	 * The JoinedRow reused holding the execution result.
+	 */
+	private transient JoinedRow reuseJoinedRow;
+
+	public BaseRowArrowPythonScalarFunctionOperator(
+		Configuration config,
+		PythonFunctionInfo[] scalarFunctions,
+		RowType inputType,
+		RowType outputType,
+		int[] udfInputOffsets,
+		int[] forwardedFields) {
+		super(config, scalarFunctions, inputType, outputType, udfInputOffsets, forwardedFields);
+	}
+
+	@Override
+	public void open() throws Exception {
+		super.open();
+		allocator = ArrowUtils.ROOT_ALLOCATOR.newChildAllocator(
+			"reader", 0, Long.MAX_VALUE);
+		reader = new ArrowStreamReader(bais, allocator);
+		reuseJoinedRow = new JoinedRow();
+	}
+
+	@Override
+	public void close() throws Exception {
+		try {
+			super.close();
+		} finally {
+			reader.close();
+			allocator.close();
+		}
+	}
+
+	@Override
+	public PythonFunctionRunner<BaseRow> createPythonFunctionRunner(
+		FnDataReceiver<byte[]> resultReceiver,
+		PythonEnvironmentManager pythonEnvironmentManager) {
+		return new BaseRowArrowPythonScalarFunctionRunner(
+			getRuntimeContext().getTaskName(),
+			resultReceiver,
+			scalarFunctions,
+			pythonEnvironmentManager,
+			userDefinedFunctionInputType,
+			userDefinedFunctionOutputType,
+			getPythonConfig().getMaxArrowBatchSize());
+	}
+
+	@Override
+	@SuppressWarnings("ConstantConditions")
+	public void emitResults() throws IOException {
+		byte[] udfResult;
+		while ((udfResult = userDefinedFunctionResultQueue.poll()) != null) {
+			bais.setBuffer(udfResult, 0, udfResult.length);
+			reader.loadNextBatch();
+			VectorSchemaRoot root = reader.getVectorSchemaRoot();
+			if (arrowReader == null) {
+				arrowReader = ArrowUtils.createBaseRowArrowReader(root);
 
 Review comment:
   It assumes that the execution results are already stored in the `VectorSchemaRoot` before creating the ArrowReader. So I'm afraid that we cannot move it to open.

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services