-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-23942][PYTHON][SQL] Makes collect in PySpark as action for a query executor listener #21007
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 all commits
db1987f
edb5eea
e865c88
deacb17
1a52dbe
7c1b3c6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3189,10 +3189,10 @@ class Dataset[T] private[sql]( | |
|
||
private[sql] def collectToPython(): Int = { | ||
EvaluatePython.registerPicklers() | ||
withNewExecutionId { | ||
withAction("collectToPython", queryExecution) { plan => | ||
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. These changes can cause the behavior changes. Please submit a PR to document it. |
||
val toJava: (Any) => Any = EvaluatePython.toJava(_, schema) | ||
val iter = new SerDeUtil.AutoBatchedPickler( | ||
queryExecution.executedPlan.executeCollect().iterator.map(toJava)) | ||
val iter: Iterator[Array[Byte]] = new SerDeUtil.AutoBatchedPickler( | ||
plan.executeCollect().iterator.map(toJava)) | ||
PythonRDD.serveIterator(iter, "serve-DataFrame") | ||
} | ||
} | ||
|
@@ -3201,8 +3201,9 @@ class Dataset[T] private[sql]( | |
* Collect a Dataset as ArrowPayload byte arrays and serve to PySpark. | ||
*/ | ||
private[sql] def collectAsArrowToPython(): Int = { | ||
withNewExecutionId { | ||
val iter = toArrowPayload.collect().iterator.map(_.asPythonSerializable) | ||
withAction("collectAsArrowToPython", queryExecution) { plan => | ||
val iter: Iterator[Array[Byte]] = | ||
toArrowPayload(plan).collect().iterator.map(_.asPythonSerializable) | ||
PythonRDD.serveIterator(iter, "serve-Arrow") | ||
} | ||
} | ||
|
@@ -3311,14 +3312,19 @@ class Dataset[T] private[sql]( | |
} | ||
|
||
/** Convert to an RDD of ArrowPayload byte arrays */ | ||
private[sql] def toArrowPayload: RDD[ArrowPayload] = { | ||
private[sql] def toArrowPayload(plan: SparkPlan): RDD[ArrowPayload] = { | ||
val schemaCaptured = this.schema | ||
val maxRecordsPerBatch = sparkSession.sessionState.conf.arrowMaxRecordsPerBatch | ||
val timeZoneId = sparkSession.sessionState.conf.sessionLocalTimeZone | ||
queryExecution.toRdd.mapPartitionsInternal { iter => | ||
plan.execute().mapPartitionsInternal { iter => | ||
val context = TaskContext.get() | ||
ArrowConverters.toPayloadIterator( | ||
iter, schemaCaptured, maxRecordsPerBatch, timeZoneId, context) | ||
} | ||
} | ||
|
||
// This is only used in tests, for now. | ||
private[sql] def toArrowPayload: RDD[ArrowPayload] = { | ||
toArrowPayload(queryExecution.executedPlan) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
/* | ||
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. Is it possible to modify this slightly and reuse it? https://github.com/apache/spark/blob/master/sql/core/src/test/scala/org/apache/spark/sql/util/ExecutionListenerManagerSuite.scala#L48 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 think it's possible. Just took a look; however, mind if I had a separate one as is for Python test specifically? maybe I am too much worried but thinking about having a dependency with a class in a suite and I am a bit hesitant. 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, I think that's fine. Thanks for putting a comment in the class for what it is for. |
||
* 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 | ||
|
||
import java.util.concurrent.atomic.AtomicBoolean | ||
|
||
import org.apache.spark.sql.execution.QueryExecution | ||
import org.apache.spark.sql.util.QueryExecutionListener | ||
|
||
|
||
class TestQueryExecutionListener extends QueryExecutionListener { | ||
override def onSuccess(funcName: String, qe: QueryExecution, durationNs: Long): Unit = { | ||
OnSuccessCall.isOnSuccessCalled.set(true) | ||
} | ||
|
||
override def onFailure(funcName: String, qe: QueryExecution, exception: Exception): Unit = { } | ||
} | ||
|
||
/** | ||
* This has a variable to check if `onSuccess` is actually called or not. Currently, this is for | ||
* the test case in PySpark. See SPARK-23942. | ||
*/ | ||
object OnSuccessCall { | ||
val isOnSuccessCalled = new AtomicBoolean(false) | ||
|
||
def isCalled(): Boolean = isOnSuccessCalled.get() | ||
|
||
def clear(): Unit = isOnSuccessCalled.set(false) | ||
} | ||
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. does this need a newline at the end? 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. nope, it already has. github shows a warning and mark on this UI if it doesn't IIRC. |
Uh oh!
There was an error while loading. Please reload this page.
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.
I'm not sure about this part. What is the case we can't find the class?
TestQueryExecutionListener.scala
has been removed or moved? If it happens, should we just silently skip this test like 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.
Ah, nope. It's when we do
sbt package
, according to https://spark.apache.org/docs/latest/building-spark.html#building-with-sbt. In this case, test files are not actually compiled. If we run the tests, it'd hit some exceptions.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.
I admit It's rare. But I believe this is more correct. In fact, there are few test cases actually taking care about 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.
and .. for
Yea, ideally we should warn explicitly in the console. The problem is about our own testing script .. We could make some changes to explicitly warn but seems we need some duplicated changes.
There are some discussions / changes going on here - #20909
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.
Ok. I see. Makes sense.
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.
Thank you @viirya. I know this one is a rather tricky one to judge what's righter. Will maybe cc you when we actually discuss about this further. I believe some people could think differently and I might have to have more discussion. But for now, I feel sure on this.