cloud-fan commented on code in PR #49351: URL: https://github.com/apache/spark/pull/49351#discussion_r1913182814
########## sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/ResolveWithCTE.scala: ########## @@ -37,23 +39,159 @@ object ResolveWithCTE extends Rule[LogicalPlan] { } } + // Substitute CTERelationRef with UnionLoopRef. + private def transformRefs(plan: LogicalPlan) = { + plan.transformWithPruning(_.containsPattern(CTE)) { + case r: CTERelationRef if r.recursive => + UnionLoopRef(r.cteId, r.output, false) + } + } + + // Update the definition's recursiveAnchor if the anchor is resolved. + private def recursiveAnchorResolved(cteDef: CTERelationDef): Option[LogicalPlan] = { + cteDef.child match { + case SubqueryAlias(_, ul: UnionLoop) => + if (ul.anchor.resolved) { + Some(ul.anchor) + } else { + None + } + case SubqueryAlias(_, Distinct(ul: UnionLoop)) => + if (ul.anchor.resolved) { + Some(ul.anchor) + } else { + None + } + case SubqueryAlias(_, UnresolvedSubqueryColumnAliases(_, ul: UnionLoop)) => + if (ul.anchor.resolved) { + Some(ul.anchor) + } else { + None + } + case SubqueryAlias(_, UnresolvedSubqueryColumnAliases(_, Distinct(ul: UnionLoop))) => + if (ul.anchor.resolved) { + Some(ul.anchor) + } else { + None + } + case _ => + cteDef.failAnalysis( + errorClass = "INVALID_RECURSIVE_CTE", + messageParameters = Map.empty) + throw QueryCompilationErrors.recursiveCteError() + } + } + private def resolveWithCTE( plan: LogicalPlan, cteDefMap: mutable.HashMap[Long, CTERelationDef]): LogicalPlan = { plan.resolveOperatorsDownWithPruning(_.containsAllPatterns(CTE)) { - case w @ WithCTE(_, cteDefs) => - cteDefs.foreach { cteDef => - if (cteDef.resolved) { - cteDefMap.put(cteDef.id, cteDef) + case withCTE @ WithCTE(_, cteDefs) => + val newCTEDefs = cteDefs.map { cteDef => + val newCTEDef = if (cteDef.recursive) { + cteDef.child match { + // Substitutions to UnionLoop and UnionLoopRef. + case alias @ SubqueryAlias(_, Union(Seq(anchor, recursion), false, false)) => Review Comment: shall we do this only when `anchor` is resolved? -- 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: reviews-unsubscr...@spark.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org