dianfu commented on a change in pull request #12841: URL: https://github.com/apache/flink/pull/12841#discussion_r452034065
########## File path: flink-python/src/main/java/org/apache/flink/table/runtime/arrow/serializers/ArrowSerializer.java ########## @@ -0,0 +1,143 @@ +/* + * 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.arrow.serializers; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.table.runtime.arrow.ArrowReader; +import org.apache.flink.table.runtime.arrow.ArrowUtils; +import org.apache.flink.table.runtime.arrow.ArrowWriter; +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.arrow.vector.ipc.ArrowStreamWriter; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * The base class ArrowSerializer which will serialize/deserialize RowType data to/from arrow bytes. + * + * @param <T> type of the input elements. + */ +@Internal +public abstract class ArrowSerializer<T> { + + /** + * The input RowType. + */ + protected final RowType inputType; + + /** + * The output RowType. + */ + protected final RowType outputType; + + /** + * 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<T> arrowReader; + + /** + * Reader which is responsible for convert the execution result from + * byte array to arrow format. + */ + private transient ArrowStreamReader arrowStreamReader; + + /** + * Container that holds a set of vectors for the input elements + * to be sent to the Python worker. + */ + transient VectorSchemaRoot rootWriter; + + /** + * Writer which is responsible for serialize the input elements to arrow format. + */ + private transient ArrowWriter<T> arrowWriter; + + /** + * Writer which is responsible for convert the arrow format data into byte array. + */ + private transient ArrowStreamWriter arrowStreamWriter; + + public ArrowSerializer( + RowType inputType, + RowType outputType) { + this.inputType = inputType; + this.outputType = outputType; + } + + public void open(InputStream bais, OutputStream baos) throws Exception { + allocator = ArrowUtils.getRootAllocator().newChildAllocator("allocator", 0, Long.MAX_VALUE); + arrowStreamReader = new ArrowStreamReader(bais, allocator); + + rootWriter = VectorSchemaRoot.create(ArrowUtils.toArrowSchema(inputType), allocator); + arrowWriter = createArrowWriter(); + arrowStreamWriter = new ArrowStreamWriter(rootWriter, null, baos); + arrowStreamWriter.start(); + } + + public int load() throws IOException { + arrowStreamReader.loadNextBatch(); + VectorSchemaRoot root = arrowStreamReader.getVectorSchemaRoot(); + if (arrowReader == null) { + arrowReader = createArrowReader(root); + } + return root.getRowCount(); + } + + public T index(int i) { Review comment: rename to **read**? ########## File path: flink-python/src/main/java/org/apache/flink/table/runtime/arrow/serializers/ArrowSerializer.java ########## @@ -0,0 +1,143 @@ +/* + * 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.arrow.serializers; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.table.runtime.arrow.ArrowReader; +import org.apache.flink.table.runtime.arrow.ArrowUtils; +import org.apache.flink.table.runtime.arrow.ArrowWriter; +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.arrow.vector.ipc.ArrowStreamWriter; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +/** + * The base class ArrowSerializer which will serialize/deserialize RowType data to/from arrow bytes. + * + * @param <T> type of the input elements. + */ +@Internal +public abstract class ArrowSerializer<T> { + + /** + * The input RowType. + */ + protected final RowType inputType; + + /** + * The output RowType. + */ + protected final RowType outputType; + + /** + * 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<T> arrowReader; + + /** + * Reader which is responsible for convert the execution result from + * byte array to arrow format. + */ + private transient ArrowStreamReader arrowStreamReader; + + /** + * Container that holds a set of vectors for the input elements + * to be sent to the Python worker. + */ + transient VectorSchemaRoot rootWriter; + + /** + * Writer which is responsible for serialize the input elements to arrow format. + */ + private transient ArrowWriter<T> arrowWriter; + + /** + * Writer which is responsible for convert the arrow format data into byte array. + */ + private transient ArrowStreamWriter arrowStreamWriter; + + public ArrowSerializer( + RowType inputType, + RowType outputType) { + this.inputType = inputType; + this.outputType = outputType; + } + + public void open(InputStream bais, OutputStream baos) throws Exception { + allocator = ArrowUtils.getRootAllocator().newChildAllocator("allocator", 0, Long.MAX_VALUE); + arrowStreamReader = new ArrowStreamReader(bais, allocator); + + rootWriter = VectorSchemaRoot.create(ArrowUtils.toArrowSchema(inputType), allocator); + arrowWriter = createArrowWriter(); + arrowStreamWriter = new ArrowStreamWriter(rootWriter, null, baos); + arrowStreamWriter.start(); + } + + public int load() throws IOException { + arrowStreamReader.loadNextBatch(); + VectorSchemaRoot root = arrowStreamReader.getVectorSchemaRoot(); + if (arrowReader == null) { + arrowReader = createArrowReader(root); + } + return root.getRowCount(); + } + + public T index(int i) { + return arrowReader.read(i); + } + + public void dump(T element) { Review comment: rename to **write**? ########## File path: flink-python/src/main/java/org/apache/flink/table/runtime/operators/python/scalar/arrow/RowDataArrowPythonScalarFunctionOperator.java ########## @@ -47,21 +39,23 @@ private static final long serialVersionUID = 1L; - /** - * Allocator which is used for byte buffer allocation. - */ - private transient BufferAllocator allocator; + private static final String SCHEMA_ARROW_CODER_URN = "flink:coder:schema:scalar_function:arrow:v1"; + + static { Review comment: Move to **ArrowSerializer**? ########## File path: flink-python/src/main/java/org/apache/flink/table/runtime/operators/python/AbstractStatelessFunctionOperator.java ########## @@ -160,10 +190,17 @@ public void processElement(StreamRecord<IN> element) throws Exception { public abstract UDFIN getFunctionInput(IN element); - public abstract PythonFunctionRunner<UDFIN> createPythonFunctionRunner( - FnDataReceiver<byte[]> resultReceiver, - PythonEnvironmentManager pythonEnvironmentManager, - Map<String, String> jobOptions); + /** + * Gets the proto representation of the Python user-defined functions to be executed. + */ + @VisibleForTesting Review comment: remove **VisibleForTesting** ---------------------------------------------------------------- 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]
