-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-22355][SQL] Dataset.collect is not threadsafe #19577
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
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 |
|---|---|---|
|
|
@@ -39,6 +39,7 @@ import org.apache.spark.sql.catalyst.analysis._ | |
| import org.apache.spark.sql.catalyst.catalog.HiveTableRelation | ||
| import org.apache.spark.sql.catalyst.encoders._ | ||
| import org.apache.spark.sql.catalyst.expressions._ | ||
| import org.apache.spark.sql.catalyst.expressions.codegen.GenerateSafeProjection | ||
| import org.apache.spark.sql.catalyst.json.{JacksonGenerator, JSONOptions} | ||
| import org.apache.spark.sql.catalyst.optimizer.CombineUnions | ||
| import org.apache.spark.sql.catalyst.parser.{ParseException, ParserUtils} | ||
|
|
@@ -198,15 +199,10 @@ class Dataset[T] private[sql]( | |
| */ | ||
| private[sql] implicit val exprEnc: ExpressionEncoder[T] = encoderFor(encoder) | ||
|
|
||
| /** | ||
| * Encoder is used mostly as a container of serde expressions in Dataset. We build logical | ||
| * plans by these serde expressions and execute it within the query framework. However, for | ||
| * performance reasons we may want to use encoder as a function to deserialize internal rows to | ||
| * custom objects, e.g. collect. Here we resolve and bind the encoder so that we can call its | ||
| * `fromRow` method later. | ||
| */ | ||
| private val boundEnc = | ||
| exprEnc.resolveAndBind(logicalPlan.output, sparkSession.sessionState.analyzer) | ||
| // The deserializer expression which can be used to build a projection and turn rows to objects | ||
| // of type T, after collecting rows to the driver side. | ||
| private val deserializer = | ||
| exprEnc.resolveAndBind(logicalPlan.output, sparkSession.sessionState.analyzer).deserializer | ||
|
|
||
| private implicit def classTag = exprEnc.clsTag | ||
|
|
||
|
|
@@ -2661,7 +2657,12 @@ class Dataset[T] private[sql]( | |
| */ | ||
| def toLocalIterator(): java.util.Iterator[T] = { | ||
| withAction("toLocalIterator", queryExecution) { plan => | ||
| plan.executeToIterator().map(boundEnc.fromRow).asJava | ||
| val objProj = GenerateSafeProjection.generate(deserializer :: Nil) | ||
| plan.executeToIterator().map { row => | ||
| // The row returned by SafeProjection is `SpecificInternalRow`, which ignore the data type | ||
| // parameter of its `get` method, so it's safe to use null here. | ||
| objProj(row).get(0, null).asInstanceOf[T] | ||
| }.asJava | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -3102,7 +3103,12 @@ class Dataset[T] private[sql]( | |
| * Collect all elements from a spark plan. | ||
| */ | ||
| private def collectFromPlan(plan: SparkPlan): Array[T] = { | ||
| plan.executeCollect().map(boundEnc.fromRow) | ||
| val objProj = GenerateSafeProjection.generate(deserializer :: Nil) | ||
|
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.
Contributor
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. it just rethrow the exception, not a big deal
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. Ok. Looks good. |
||
| plan.executeCollect().map { row => | ||
| // The row returned by SafeProjection is `SpecificInternalRow`, which ignore the data type | ||
| // parameter of its `get` method, so it's safe to use null here. | ||
| objProj(row).get(0, null).asInstanceOf[T] | ||
| } | ||
| } | ||
|
|
||
| private def sortInternal(global: Boolean, sortExprs: Seq[Column]): Dataset[T] = { | ||
|
|
||
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.
It should be better to explain we keep the projection inside for thread-safe with a comment.
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.
+1