-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-21190][PYSPARK] Python Vectorized UDFs #18659
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
be81ef6
8569736
9236e99
cc7ed5a
cf764b0
4f6c950
91dead2
4a2fec2
518126e
3b4465c
25e3a71
dc237e7
4a0691b
d49a3db
69112a5
f451d65
44a20f6
53926cc
b8ffa50
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -187,8 +187,14 @@ class ArrowSerializer(FramedSerializer): | |
| Serializes an Arrow stream. | ||
| """ | ||
|
|
||
| def dumps(self, obj): | ||
| raise NotImplementedError | ||
| def dumps(self, batch): | ||
| import pyarrow as pa | ||
| import io | ||
| sink = io.BytesIO() | ||
| writer = pa.RecordBatchFileWriter(sink, batch.schema) | ||
| writer.write_batch(batch) | ||
| writer.close() | ||
| return sink.getvalue() | ||
|
|
||
| def loads(self, obj): | ||
| import pyarrow as pa | ||
|
|
@@ -199,6 +205,28 @@ def __repr__(self): | |
| return "ArrowSerializer" | ||
|
|
||
|
|
||
| class ArrowPandasSerializer(ArrowSerializer): | ||
|
|
||
| def __init__(self): | ||
| super(ArrowPandasSerializer, self).__init__() | ||
|
|
||
| # make an ArrowRecordBatch from a Pandas Series and serialize | ||
| def dumps(self, series): | ||
| import pyarrow as pa | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we catch
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it would probably be best to handle it the same way as in That got me thinking that it is a little weird to have an SQLConf "spark.sql.execution.arrow.enable" that is set for
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, hm .. let me check the previous discussions and think about this a bit more.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am okay with leaving it as is here. I think we should catch and throw it with better messages in all cases (probably at entry points) later but let's talk about this in another place later. |
||
| # TODO: iterator could be a tuple | ||
| arr = pa.Array.from_pandas(series) | ||
| batch = pa.RecordBatch.from_arrays([arr], ["_0"]) | ||
| return super(ArrowPandasSerializer, self).dumps(batch) | ||
|
|
||
| # deserialize an ArrowRecordBatch to an Arrow table and return as a list of pandas.Series | ||
| def loads(self, obj): | ||
| table = super(ArrowPandasSerializer, self).loads(obj) | ||
| return [c.to_pandas() for c in table.itercolumns()] | ||
|
|
||
| def __repr__(self): | ||
| return "ArrowPandasSerializer" | ||
|
|
||
|
|
||
| class BatchedSerializer(Serializer): | ||
|
|
||
| """ | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -82,7 +82,6 @@ private[sql] object ArrowConverters { | |
|
|
||
| val root = VectorSchemaRoot.create(arrowSchema, allocator) | ||
| val arrowWriter = ArrowWriter.create(root) | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (Looks unrelated change) |
||
| var closed = false | ||
|
|
||
| context.addTaskCompletionListener { _ => | ||
|
|
@@ -203,4 +202,20 @@ private[sql] object ArrowConverters { | |
| reader.close() | ||
| } | ||
| } | ||
|
|
||
| private[arrow] def execByteArrayAsVectors( | ||
| batchBytes: Array[Byte], | ||
| allocator: BufferAllocator)(block: (VectorSchemaRoot) => Unit): Unit = { | ||
| val in = new ByteArrayReadableSeekableByteChannel(batchBytes) | ||
| val reader = new ArrowFileReader(in, allocator) | ||
|
|
||
| // Read a batch from a byte stream, ensure the reader is closed | ||
| Utils.tryWithSafeFinally { | ||
| val root = reader.getVectorSchemaRoot // throws IOException | ||
| reader.loadNextBatch() // throws IOException | ||
| block(root) | ||
| } { | ||
| reader.close() | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| /* | ||
| * 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.spark.sql.execution.python | ||
|
|
||
| import java.io.{DataOutputStream, File} | ||
|
|
||
| import org.apache.spark.api.python.{ChainedPythonFunctions, PythonRunner} | ||
| import org.apache.spark.rdd.RDD | ||
| import org.apache.spark.sql.execution.arrow.{ArrowConverters, ArrowPayload} | ||
| import org.apache.spark.sql.catalyst.InternalRow | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.execution.SparkPlan | ||
| import org.apache.spark.sql.types.{DataType, StructField, StructType} | ||
| import org.apache.spark.util.Utils | ||
| import org.apache.spark.{SparkEnv, TaskContext} | ||
|
|
||
| import scala.collection.mutable.ArrayBuffer | ||
|
|
||
|
|
||
| /** | ||
| * A physical plan that evaluates a [[PythonUDF]], | ||
| */ | ||
| case class ArrowEvalPythonExec(udfs: Seq[PythonUDF], output: Seq[Attribute], child: SparkPlan) | ||
| extends SparkPlan { | ||
|
|
||
| def children: Seq[SparkPlan] = child :: Nil | ||
|
|
||
| override def producedAttributes: AttributeSet = AttributeSet(output.drop(child.output.length)) | ||
|
|
||
| private def collectFunctions(udf: PythonUDF): (ChainedPythonFunctions, Seq[Expression]) = { | ||
|
||
| udf.children match { | ||
| case Seq(u: PythonUDF) => | ||
| val (chained, children) = collectFunctions(u) | ||
| (ChainedPythonFunctions(chained.funcs ++ Seq(udf.func)), children) | ||
| case children => | ||
| // There should not be any other UDFs, or the children can't be evaluated directly. | ||
| assert(children.forall(_.find(_.isInstanceOf[PythonUDF]).isEmpty)) | ||
| (ChainedPythonFunctions(Seq(udf.func)), udf.children) | ||
| } | ||
| } | ||
|
|
||
| protected override def doExecute(): RDD[InternalRow] = { | ||
| val inputRDD = child.execute().map(_.copy()) | ||
| val bufferSize = inputRDD.conf.getInt("spark.buffer.size", 65536) | ||
| val reuseWorker = inputRDD.conf.getBoolean("spark.python.worker.reuse", defaultValue = true) | ||
|
|
||
| inputRDD.mapPartitions { iter => | ||
|
|
||
| // The queue used to buffer input rows so we can drain it to | ||
| // combine input with output from Python. | ||
| val queue = HybridRowQueue(TaskContext.get().taskMemoryManager(), | ||
| new File(Utils.getLocalDir(SparkEnv.get.conf)), child.output.length) | ||
| TaskContext.get().addTaskCompletionListener({ ctx => | ||
| queue.close() | ||
| }) | ||
|
|
||
| val (pyFuncs, inputs) = udfs.map(collectFunctions).unzip | ||
|
|
||
| // flatten all the arguments | ||
| val allInputs = new ArrayBuffer[Expression] | ||
| val dataTypes = new ArrayBuffer[DataType] | ||
| val argOffsets = inputs.map { input => | ||
| input.map { e => | ||
| if (allInputs.exists(_.semanticEquals(e))) { | ||
| allInputs.indexWhere(_.semanticEquals(e)) | ||
| } else { | ||
| allInputs += e | ||
| dataTypes += e.dataType | ||
| allInputs.length - 1 | ||
| } | ||
| }.toArray | ||
| }.toArray | ||
| val projection = newMutableProjection(allInputs, child.output) | ||
| val schema = StructType(dataTypes.map(dt => StructField("", dt))) | ||
|
|
||
| // Input iterator to Python: input rows are grouped so we send them in batches to Python. | ||
| // For each row, add it to the queue. | ||
|
||
| val projectedRowIter = iter.map { inputRow => | ||
| queue.add(inputRow.asInstanceOf[UnsafeRow]) | ||
| projection(inputRow) | ||
| } | ||
|
|
||
| val inputIterator = ArrowConverters.toPayloadIterator(projectedRowIter, schema, 0). | ||
| map(_.asPythonSerializable) | ||
|
|
||
| val context = TaskContext.get() | ||
|
|
||
| // Output iterator for results from Python. | ||
| val outputIterator = new PythonRunner(pyFuncs, bufferSize, reuseWorker, true, argOffsets). | ||
| compute(inputIterator, context.partitionId(), context) | ||
|
||
|
|
||
| val joined = new JoinedRow | ||
| val resultProj = UnsafeProjection.create(output, output) | ||
|
|
||
| val outputRowIterator = ArrowConverters.fromPayloadIterator( | ||
| outputIterator.map(ArrowPayload(_))) | ||
|
|
||
| outputRowIterator.map { outputRow => | ||
| resultProj(joined(queue.remove(), outputRow)) | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, that was leftovers.. I'll remove it in a followup.