peter-toth commented on code in PR #60447:
URL: https://github.com/apache/spark/pull/60447#discussion_r4567046042
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/BatchScanExec.scala:
##########
@@ -69,7 +68,17 @@ case class BatchScanExec(
private def scanOutput: Seq[AttributeReference] =
if (appendsCheckedColumn) output.init else output
- override def scan: Scan = if (plannedScan == null) null else plannedScan.scan
+ // What this node plans from `scan`, see [[PlannedScan]]. It is kept in a
tag, set when the node
+ // first asks for its `Batch` or partitions. `makeCopy`, `clone` and the
tree transforms carry the
+ // tag over to the copies made after that, so they share it. A copy over
another `Scan`, and one
+ // the case-class `copy` makes, plan their own.
+ private def plannedScan: PlannedScan = synchronized {
+ getTagValue(PlannedScan.TAG).filter(_.scan eq scan).getOrElse {
+ val planned = new PlannedScan(scan)
+ setTagValue(PlannedScan.TAG, planned)
Review Comment:
Confirmed, `transform { case b: BatchScanExec => b.copy(runtimeFilters =
...) }` returned a node that had lost the logical link and planned again.
[`33c2435`](https://github.com/apache/spark/pull/60447/commits/33c2435c834a94c2481baa0b8fe4683f7daa7147)
drops the tag. With the private field, `equals` no longer touches the tags,
and that transform keeps them. The copy plans its own partitions, as a `copy`
does on the maintenance branches.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/BatchScanExec.scala:
##########
@@ -69,7 +68,17 @@ case class BatchScanExec(
private def scanOutput: Seq[AttributeReference] =
if (appendsCheckedColumn) output.init else output
- override def scan: Scan = if (plannedScan == null) null else plannedScan.scan
+ // What this node plans from `scan`, see [[PlannedScan]]. It is kept in a
tag, set when the node
+ // first asks for its `Batch` or partitions. `makeCopy`, `clone` and the
tree transforms carry the
+ // tag over to the copies made after that, so they share it. A copy over
another `Scan`, and one
+ // the case-class `copy` makes, plan their own.
Review Comment:
The private field is built in the constructor, so `makeCopy` and `clone`
hand it over whether the node has planned or not. A case-class `copy` plans its
own, inside a transform or outside. The comment says that now.
##########
sql/core/src/test/scala/org/apache/spark/sql/connector/KeyGroupedPartitioningSuite.scala:
##########
@@ -2616,12 +2616,11 @@ class KeyGroupedPartitioningSuite
}
}
try {
- // A new `PlannedScan` makes the copy plan the connector's
replacement partitions, and a
- // rewritten runtime filter makes it distinct for tree replacement,
while the true literal
- // leaves those partitions unchanged.
+ // A case-class copy plans the connector's replacement partitions,
and a rewritten runtime
+ // filter makes it distinct for tree replacement, while the true
literal leaves those
+ // partitions unchanged.
inMemoryScan.data = newData
- val rebuilt = leftScan.copy(plannedScan = new
PlannedScan(leftScan.scan),
- runtimeFilters = Seq(Literal.TrueLiteral))
+ val rebuilt = leftScan.copy(runtimeFilters =
Seq(Literal.TrueLiteral))
assert(rebuilt.outputPartitioning.numPartitions ==
replacementKeys.size)
Review Comment:
With the private field, `copy` always plans its own, so the test no longer
depends on these assertions asking first.
##########
sql/core/src/test/scala/org/apache/spark/sql/execution/SparkPlanSuite.scala:
##########
@@ -333,17 +333,22 @@ class SparkPlanSuite extends SharedSparkSession {
throw new UnsupportedOperationException()
}
}
- val scan = BatchScanExec(Seq.empty, new PlannedScan(new NewBatchScan),
Seq.empty, table = null)
+ val scan = BatchScanExec(Seq.empty, new NewBatchScan, Seq.empty, table =
null)
def sharesPlanning(copy: SparkPlan): Boolean = copy match {
case c: BatchScanExec =>
(c.batch eq scan.batch) && (c.inputPartitions eq scan.inputPartitions)
case _ => false
}
+ val args = scan.productIterator.map(_.asInstanceOf[AnyRef]).toArray
+ // The node plans first, as in `EnsureRequirements`, and later rules copy
it.
+ scan.inputPartitions
- // Copies through `makeCopy`, `clone` or `copy` share what the node
planned.
-
assert(sharesPlanning(scan.makeCopy(scan.productIterator.map(_.asInstanceOf[AnyRef]).toArray)))
+ // Copies through `makeCopy` or `clone` share what the node planned, but
not over another
+ // `Scan`. So does the canonical form, whose `equals` compares the planned
`Batch`.
+ assert(sharesPlanning(scan.makeCopy(args)))
assert(sharesPlanning(scan.clone()))
- assert(sharesPlanning(scan.copy(runtimeFilters = Nil)))
+ assert(!sharesPlanning(scan.makeCopy(args.updated(1, new NewBatchScan))))
+ assert(sharesPlanning(scan.canonicalized))
Review Comment:
The test now makes the copies before the node plans, and also checks that a
`copy` does not share. A `copy` inside a transform behaves the same way, and
the field no longer touches the tags.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/BatchScanExec.scala:
##########
@@ -192,22 +204,19 @@ case class BatchScanExec(
/**
* A `Scan`, with the `Batch` and the input partitions a [[BatchScanExec]]
plans from it once, when
- * a node first asks for them. The copies later rules make of the node share
it, so they read the
- * partitions the first node planned, with a reader factory from the `Batch`
that planned them.
- * The partitions have to be the planned ones, because the `Scan` can be
shared with another node
- * whose runtime filter prunes it first, e.g. the build side of a DPP subquery
(SPARK-60752). A
- * runtime filter replans them, through
`planInputPartitionsWithRuntimeFilters` or from a new
- * `Batch` after a pushed `filter`, but keeps the planned `Batch`'s reader
factory.
- *
- * A rule that rebuilds a node over the same `Scan` passes on its
`plannedScan`, or uses `copy`. A
- * new `PlannedScan` is for a node that is planned again, or one over another
`Scan`.
+ * a node first asks for them. The node keeps it in a tag, so the copies later
rules make of it
Review Comment:
Added to the `PlannedScan` doc: a rule that rebuilds a node over the same
`Scan` keeps the planned partitions through `makeCopy`, e.g. with
`transformExpressions`. The case-class `copy` plans them again.
##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/BatchScanExec.scala:
##########
@@ -69,7 +68,17 @@ case class BatchScanExec(
private def scanOutput: Seq[AttributeReference] =
if (appendsCheckedColumn) output.init else output
- override def scan: Scan = if (plannedScan == null) null else plannedScan.scan
+ // What this node plans from `scan`, see [[PlannedScan]]. It is kept in a
tag, set when the node
+ // first asks for its `Batch` or partitions. `makeCopy`, `clone` and the
tree transforms carry the
+ // tag over to the copies made after that, so they share it. A copy over
another `Scan`, and one
+ // the case-class `copy` makes, plan their own.
+ private def plannedScan: PlannedScan = synchronized {
Review Comment:
This no longer applies. `plannedScan` is a private field set in the
constructor, read without a lock or a tag lookup, and fixed for the instance.
--
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.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]