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


##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/basicLogicalOperators.scala:
##########
@@ -910,6 +910,30 @@ case class WithCTE(plan: LogicalPlan, cteDefs: 
Seq[CTERelationDef]) extends Logi
   def withNewPlan(newPlan: LogicalPlan): WithCTE = {
     withNewChildren(children.init :+ newPlan).asInstanceOf[WithCTE]
   }
+
+  override def doCanonicalize(): LogicalPlan = {
+    val canonicalized = super.doCanonicalize().asInstanceOf[WithCTE]
+    val defIndex = canonicalized.cteDefs.map(_.id).zipWithIndex.toMap
+
+    def canonicalizeCTE(plan: LogicalPlan): LogicalPlan = {
+      plan.resolveOperatorsUpWithPruning(
+        _.containsAnyPattern(CTE, PLAN_EXPRESSION)) {
+        case ref: CTERelationRef =>
+          ref.copy(cteId = defIndex(ref.cteId).toLong)
+

Review Comment:
   shall we add assert to guarantee this assertion?



##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/plans/logical/basicLogicalOperators.scala:
##########
@@ -910,6 +910,30 @@ case class WithCTE(plan: LogicalPlan, cteDefs: 
Seq[CTERelationDef]) extends Logi
   def withNewPlan(newPlan: LogicalPlan): WithCTE = {
     withNewChildren(children.init :+ newPlan).asInstanceOf[WithCTE]
   }
+
+  override def doCanonicalize(): LogicalPlan = {
+    val canonicalized = super.doCanonicalize().asInstanceOf[WithCTE]
+    val defIndex = canonicalized.cteDefs.map(_.id).zipWithIndex.toMap
+
+    def canonicalizeCTE(plan: LogicalPlan): LogicalPlan = {
+      plan.resolveOperatorsUpWithPruning(
+        _.containsAnyPattern(CTE, PLAN_EXPRESSION)) {
+        case ref: CTERelationRef =>
+          ref.copy(cteId = defIndex(ref.cteId).toLong)
+

Review Comment:
   shall we add assert to guarantee this assumption?



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