cloud-fan commented on code in PR #36146:
URL: https://github.com/apache/spark/pull/36146#discussion_r854225967
##########
sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/analysis/CTESubstitution.scala:
##########
@@ -221,31 +227,18 @@ object CTESubstitution extends Rule[LogicalPlan] {
// WITH t3 AS (SELECT * FROM t1)
// )
// t3 should resolve the t1 to `SELECT 2` instead of `SELECT 1`.
- traverseAndSubstituteCTE(relation, isCommand, cteDefs)._1
- }
-
- if (cteDefs.length > lastCTEDefCount) {
- // We have added more CTE relations to the `cteDefs` from the inner
CTE, and these relations
- // should also be substituted with `resolvedCTERelations` as inner CTE
relation can refer to
- // outer CTE relation. For example:
- // WITH t1 AS (SELECT 1)
- // t2 AS (
- // WITH t3 AS (SELECT * FROM t1)
- // )
- for (i <- lastCTEDefCount until cteDefs.length) {
- val substituted =
- substituteCTE(cteDefs(i).child, isLegacy || isCommand,
resolvedCTERelations.toSeq)
- cteDefs(i) = cteDefs(i).copy(child = substituted)
- }
+ traverseAndSubstituteCTE(relation, isCommand, resolvedCTERelations,
cteDefs)._1
}
-
// CTE definition can reference a previous one
- val substituted =
- substituteCTE(innerCTEResolved, isLegacy || isCommand,
resolvedCTERelations.toSeq)
+ val substituted = substituteCTE(innerCTEResolved, isLegacy || isCommand,
resolvedCTERelations)
val cteRelation = CTERelationDef(substituted)
- resolvedCTERelations += (name -> cteRelation)
+ if (!(isLegacy || isCommand)) {
+ cteDefs += cteRelation
+ }
+ // Prepending new CTEs makes sure that those have higher priority over
outer ones.
Review Comment:
It seems safer to keep `resolvedCTERelations` as it was, and when we call
`traverseAndSubstituteCTE` or `substituteCTE`, we pass `resolvedCTERelations ++
outerCTEDefs`
--
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]