cloud-fan commented on code in PR #41347:
URL: https://github.com/apache/spark/pull/41347#discussion_r1266553718


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/DeduplicateRelations.scala:
##########
@@ -95,59 +102,118 @@ object DeduplicateRelations extends Rule[LogicalPlan] {
     case p: LogicalPlan if p.isStreaming => (plan, false)
 
     case m: MultiInstanceRelation =>
-      val planWrapper = RelationWrapper(m.getClass, m.output.map(_.exprId.id))
-      if (existingRelations.contains(planWrapper)) {
-        val newNode = m.newInstance()
-        newNode.copyTagsFrom(m)
-        (newNode, true)
-      } else {
-        existingRelations.add(planWrapper)
-        (m, false)
-      }
+      deduplicate(existingRelations, m)(Some(_.output.map(_.exprId.id)))(node 
=> node.newInstance()
+        .asInstanceOf[LogicalPlan with MultiInstanceRelation])
+
+    case p: Project =>
+      deduplicate(existingRelations, p)(Some(newProject => 
findAliases(newProject.projectList)
+        .map(_.exprId.id).toSeq))(newProject => 
newProject.copy(newAliases(newProject.projectList)))
+
+    case s: SerializeFromObject =>
+      deduplicate(existingRelations, 
s)(Some(_.serializer.map(_.exprId.id)))(newSer =>
+        newSer.copy(newSer.serializer.map(_.newInstance())))
+
+    case f: FlatMapGroupsInPandas =>
+      deduplicate(existingRelations, 
f)(Some(_.output.map(_.exprId.id)))(newFlatMap =>
+        newFlatMap.copy(output = newFlatMap.output.map(_.newInstance())))
+
+    case f: FlatMapCoGroupsInPandas =>
+      deduplicate(existingRelations, 
f)(Some(_.output.map(_.exprId.id)))(newFlatMap =>
+        newFlatMap.copy(output = newFlatMap.output.map(_.newInstance())))
+
+    case m: MapInPandas =>
+      deduplicate(existingRelations, 
m)(Some(_.output.map(_.exprId.id)))(newMap =>
+        newMap.copy(output = newMap.output.map(_.newInstance())))
+
+    case p: PythonMapInArrow =>
+      deduplicate(existingRelations, 
p)(Some(_.output.map(_.exprId.id)))(newMap =>
+        newMap.copy(output = newMap.output.map(_.newInstance())))
+
+    case a: AttachDistributedSequence =>
+      deduplicate(existingRelations, 
a)(Some(_.producedAttributes.map(_.exprId.id).toSeq))(
+        newAttach => newAttach.copy(sequenceAttr = newAttach.producedAttributes
+          .map(_.newInstance()).head))
+
+    case g: Generate =>
+      deduplicate(existingRelations, 
g)(Some(_.generatorOutput.map(_.exprId.id)))(newGenerate =>
+        newGenerate.copy(generatorOutput = 
newGenerate.generatorOutput.map(_.newInstance())))
+
+    case e: Expand =>
+      deduplicate(existingRelations, 
e)(Some(_.producedAttributes.map(_.exprId.id).toSeq))(
+        newExpand => newExpand.copy(output = 
newExpand.output.map(_.newInstance())))
+
+    case w: Window =>
+      deduplicate(existingRelations, w)(Some(_.windowExpressions
+        .map(_.exprId.id)))(newWindow => newWindow.copy(windowExpressions =
+        newWindow.windowExpressions.map(_.newInstance())))
+
+    case s: ScriptTransformation =>
+      deduplicate(existingRelations, s)(Some(_.output.map(_.exprId.id)))(
+        newScript => newScript.copy(output = 
newScript.output.map(_.newInstance())))
 
     case plan: LogicalPlan =>
-      var planChanged = false
-      val newPlan = if (plan.children.nonEmpty) {
-        val newChildren = mutable.ArrayBuffer.empty[LogicalPlan]
-        for (c <- plan.children) {
-          val (renewed, changed) = renewDuplicatedRelations(existingRelations, 
c)
-          newChildren += renewed
-          if (changed) {
-            planChanged = true
-          }
-        }
+      deduplicate(existingRelations, plan)()(p => p)
+  }
 
-        val planWithNewSubquery = plan.transformExpressions {
-          case subquery: SubqueryExpression =>
-            val (renewed, changed) = 
renewDuplicatedRelations(existingRelations, subquery.plan)
-            if (changed) planChanged = true
-            subquery.withNewPlan(renewed)
+  private def deduplicate[T <: LogicalPlan](
+      existingRelations: mutable.HashSet[RelationWrapper], plan: T)
+      (getExprIds: Option[T => Seq[Long]] = Option.empty)

Review Comment:
   I find the code a bit hard to read with too many lambda parameters. How 
about the following code structure?
   ```
   def deduplicate(...) // deduplicate relations in children and update 
attriubte references
   def deduplicateAndRenew(...) // invokes  `deduplicate` and renew attributes 
in itself
   ```



-- 
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]

Reply via email to