@@ -46,28 +46,29 @@ object CTESubstitution extends Rule[LogicalPlan] {
4646 */
4747 private def assertNoNameConflictsInCTE (
4848 plan : LogicalPlan ,
49- namesInChildren : Set [String ] = Set .empty,
50- namesInExpressions : Set [String ] = Set .empty): Unit = {
49+ outerCTERelationNames : Set [String ] = Set .empty,
50+ namesInSubqueries : Set [String ] = Set .empty): Unit = {
5151 plan match {
5252 case w @ With (child, relations) =>
5353 val newNames = relations.map {
5454 case (cteName, _) =>
55- if (namesInChildren .contains(cteName)) {
55+ if (outerCTERelationNames .contains(cteName)) {
5656 throw new AnalysisException (s " Name $cteName is ambiguous in nested CTE. " +
5757 s " Please set ${LEGACY_CTE_PRECEDENCE_POLICY .key} to CORRECTED so that name " +
5858 " defined in inner CTE takes precedence. If set it to LEGACY, outer CTE " +
5959 " definitions will take precedence. See more details in SPARK-28228." )
6060 } else {
6161 cteName
6262 }
63- }.toSet ++ namesInExpressions
64- assertNoNameConflictsInCTE(child, namesInChildren , newNames)
63+ }.toSet ++ namesInSubqueries
64+ assertNoNameConflictsInCTE(child, outerCTERelationNames , newNames)
6565 w.innerChildren.foreach(assertNoNameConflictsInCTE(_, newNames, newNames))
6666
6767 case other =>
6868 other.subqueries.foreach(
69- assertNoNameConflictsInCTE(_, namesInExpressions, namesInExpressions))
70- other.children.foreach(assertNoNameConflictsInCTE(_, namesInChildren, namesInExpressions))
69+ assertNoNameConflictsInCTE(_, namesInSubqueries, namesInSubqueries))
70+ other.children.foreach(
71+ assertNoNameConflictsInCTE(_, outerCTERelationNames, namesInSubqueries))
7172 }
7273 }
7374
0 commit comments