Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
use outerCTEId instead of outerRefMap
  • Loading branch information
peter-toth committed Apr 20, 2023
commit fccda08a3f605169282f42e1f147aad91f4adf0c
Original file line number Diff line number Diff line change
Expand Up @@ -72,33 +72,35 @@ case class InlineCTE(alwaysInline: Boolean = false) extends Rule[LogicalPlan] {
def buildCTEMap(
plan: LogicalPlan,
cteMap: mutable.Map[Long, (CTERelationDef, Int, mutable.Map[Long, Int])],
outerRefMap: Option[mutable.Map[Long, Int]] = None): Unit = {
outerCTEId: Option[Long] = None): Unit = {
plan match {
case WithCTE(child, cteDefs) =>
cteDefs.foreach { cteDef =>
cteMap(cteDef.id) = (cteDef, 0, mutable.Map.empty.withDefaultValue(0))
}
cteDefs.foreach { cteDef =>
val (_, _, refMap) = cteMap(cteDef.id)
buildCTEMap(cteDef, cteMap, Some(refMap))
buildCTEMap(cteDef, cteMap, Some(cteDef.id))
}
buildCTEMap(child, cteMap, outerRefMap)
buildCTEMap(child, cteMap, outerCTEId)

case ref: CTERelationRef =>
val (cteDef, refCount, refMap) = cteMap(ref.cteId)
cteMap(ref.cteId) = (cteDef, refCount + 1, refMap)
outerRefMap.foreach(_(ref.cteId) += 1)
outerCTEId.foreach { cteId =>
val (_, _, outerRefMap) = cteMap(cteId)
outerRefMap(ref.cteId) += 1
}

case _ =>
if (plan.containsPattern(CTE)) {
plan.children.foreach { child =>
buildCTEMap(child, cteMap, outerRefMap)
buildCTEMap(child, cteMap, outerCTEId)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this duplicated? If plan is WithCTE, we should have already invoked buildCTEMap for CTE relations in https://github.com/apache/spark/pull/40856/files#diff-1c15413e5d63f78fff1db3dec9df4a671e78b76d086104d81f4a967eb2800805R82

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nvm, this is the case _ branch.

}

plan.expressions.foreach { expr =>
if (expr.containsAllPatterns(PLAN_EXPRESSION, CTE)) {
expr.foreach {
case e: SubqueryExpression => buildCTEMap(e.plan, cteMap, outerRefMap)
case e: SubqueryExpression => buildCTEMap(e.plan, cteMap, outerCTEId)
case _ =>
}
}
Expand Down