cloud-fan commented on code in PR #49351: URL: https://github.com/apache/spark/pull/49351#discussion_r1919545884
########## sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/analysis/ResolveRecursiveCTESuite.scala: ########## @@ -0,0 +1,144 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +package org.apache.spark.sql.catalyst.analysis + +import org.apache.spark.sql.catalyst.analysis.SimpleAnalyzer.ResolveSubqueryColumnAliases +import org.apache.spark.sql.catalyst.expressions.{Alias, Literal} +import org.apache.spark.sql.catalyst.plans.logical._ +import org.apache.spark.sql.catalyst.rules.RuleExecutor + +class ResolveRecursiveCTESuite extends AnalysisTest { + // Motivated by: + // WITH RECURSIVE t AS (SELECT 1 UNION ALL SELECT * FROM t) SELECT * FROM t; + test("ResolveWithCTE rule on recursive CTE without UnresolvedSubqueryColumnAliases") { + // The analyzer will repeat ResolveWithCTE rule twice. + val rules = Seq(ResolveWithCTE, ResolveWithCTE) + val analyzer = new RuleExecutor[LogicalPlan] { + override val batches = Seq(Batch("Resolution", Once, rules: _*)) + } + // Since cteDef IDs need to be the same, cteDef for each case will be created by copying + // this one with its child replaced. + val cteDef = CTERelationDef(OneRowRelation()) + + def getBeforePlan(cteDef: CTERelationDef): LogicalPlan = { + val anchor = Project(Seq(Alias(Literal(1), "1")()), OneRowRelation()) + + val recursionPart = Project(anchor.output, + SubqueryAlias("t", + CTERelationRef(cteDef.id, false, Seq(), false, recursive = true))) + + val cteDefFinal = cteDef.copy(child = + SubqueryAlias("t", Union(Seq(anchor, recursionPart)))) + + val outerProject = Project(anchor.output, + SubqueryAlias("t", + CTERelationRef(cteDefFinal.id, false, Seq(), false, recursive = false))) + + val finalPlan = WithCTE(outerProject, Seq(cteDefFinal)) + finalPlan + } + + def getAfterPlan(cteDef: CTERelationDef): LogicalPlan = { + val anchor = Project(Seq(Alias(Literal(1), "1")()), OneRowRelation()) + + val saRecursion = SubqueryAlias("t", + UnionLoopRef(cteDef.id, anchor.output, false)) + val recursionPart = Project(saRecursion.output, saRecursion) + + val cteDefFinal = cteDef.copy(child = + SubqueryAlias("t", UnionLoop(cteDef.id, anchor, recursionPart))) + + val outerCteRef = CTERelationRef(cteDefFinal.id, true, cteDefFinal.output, false, Review Comment: shouldn't this be `UnionLoopRef`? -- 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