-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-43199][SQL] Make InlineCTE idempotent #40856
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
7dce656
8765bf7
fccda08
ecbc4b8
806c2de
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,8 +42,9 @@ case class InlineCTE(alwaysInline: Boolean = false) extends Rule[LogicalPlan] { | |
|
|
||
| override def apply(plan: LogicalPlan): LogicalPlan = { | ||
| if (!plan.isInstanceOf[Subquery] && plan.containsPattern(CTE)) { | ||
| val cteMap = mutable.HashMap.empty[Long, (CTERelationDef, Int)] | ||
| val cteMap = mutable.SortedMap.empty[Long, (CTERelationDef, Int, mutable.Map[Long, Int])] | ||
| buildCTEMap(plan, cteMap) | ||
| cleanCTEMap(cteMap) | ||
| val notInlined = mutable.ArrayBuffer.empty[CTERelationDef] | ||
| val inlined = inlineCTE(plan, cteMap, notInlined) | ||
| // CTEs in SQL Commands have been inlined by `CTESubstitution` already, so it is safe to add | ||
|
|
@@ -70,48 +71,69 @@ case class InlineCTE(alwaysInline: Boolean = false) extends Rule[LogicalPlan] { | |
|
|
||
| def buildCTEMap( | ||
| plan: LogicalPlan, | ||
| cteMap: mutable.HashMap[Long, (CTERelationDef, Int)]): Unit = { | ||
| cteMap: mutable.Map[Long, (CTERelationDef, Int, mutable.Map[Long, Int])], | ||
| outerCTEId: Option[Long] = None): Unit = { | ||
| plan match { | ||
| case WithCTE(_, cteDefs) => | ||
| case WithCTE(child, cteDefs) => | ||
| cteDefs.foreach { cteDef => | ||
| cteMap(cteDef.id) = (cteDef, 0, mutable.Map.empty.withDefaultValue(0)) | ||
| } | ||
| cteDefs.foreach { cteDef => | ||
| cteMap.put(cteDef.id, (cteDef, 0)) | ||
| buildCTEMap(cteDef, cteMap, Some(cteDef.id)) | ||
| } | ||
| buildCTEMap(child, cteMap, outerCTEId) | ||
|
|
||
| case ref: CTERelationRef => | ||
| val (cteDef, refCount) = cteMap(ref.cteId) | ||
| cteMap.update(ref.cteId, (cteDef, refCount + 1)) | ||
| val (cteDef, refCount, refMap) = cteMap(ref.cteId) | ||
| cteMap(ref.cteId) = (cteDef, refCount + 1, refMap) | ||
| outerCTEId.foreach { cteId => | ||
| val (_, _, outerRefMap) = cteMap(cteId) | ||
| outerRefMap(ref.cteId) += 1 | ||
| } | ||
|
|
||
| case _ => | ||
| } | ||
|
|
||
| if (plan.containsPattern(CTE)) { | ||
| plan.children.foreach { child => | ||
| buildCTEMap(child, cteMap) | ||
| } | ||
| if (plan.containsPattern(CTE)) { | ||
| plan.children.foreach { child => | ||
| buildCTEMap(child, cteMap, outerCTEId) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this duplicated? If plan is
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nvm, this is the |
||
| } | ||
|
|
||
| plan.expressions.foreach { expr => | ||
| if (expr.containsAllPatterns(PLAN_EXPRESSION, CTE)) { | ||
| expr.foreach { | ||
| case e: SubqueryExpression => | ||
| buildCTEMap(e.plan, cteMap) | ||
| case _ => | ||
| plan.expressions.foreach { expr => | ||
| if (expr.containsAllPatterns(PLAN_EXPRESSION, CTE)) { | ||
| expr.foreach { | ||
| case e: SubqueryExpression => buildCTEMap(e.plan, cteMap, outerCTEId) | ||
| case _ => | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private def cleanCTEMap( | ||
| cteRefMap: mutable.SortedMap[Long, (CTERelationDef, Int, mutable.Map[Long, Int])] | ||
| ) = { | ||
| cteRefMap.keys.toSeq.reverse.foreach { currentCTEId => | ||
| val (_, currentRefCount, refMap) = cteRefMap(currentCTEId) | ||
|
||
| if (currentRefCount == 0) { | ||
| refMap.foreach { case (referencedCTEId, uselessRefCount) => | ||
| val (cteDef, refCount, refMap) = cteRefMap(referencedCTEId) | ||
| cteRefMap(referencedCTEId) = (cteDef, refCount - uselessRefCount, refMap) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private def inlineCTE( | ||
| plan: LogicalPlan, | ||
| cteMap: mutable.HashMap[Long, (CTERelationDef, Int)], | ||
| cteMap: mutable.Map[Long, (CTERelationDef, Int, mutable.Map[Long, Int])], | ||
| notInlined: mutable.ArrayBuffer[CTERelationDef]): LogicalPlan = { | ||
| plan match { | ||
| case WithCTE(child, cteDefs) => | ||
| cteDefs.foreach { cteDef => | ||
| val (cte, refCount) = cteMap(cteDef.id) | ||
| val (cte, refCount, refMap) = cteMap(cteDef.id) | ||
| if (refCount > 0) { | ||
| val inlined = cte.copy(child = inlineCTE(cte.child, cteMap, notInlined)) | ||
| cteMap.update(cteDef.id, (inlined, refCount)) | ||
| cteMap(cteDef.id) = (inlined, refCount, refMap) | ||
| if (!shouldInline(inlined, refCount)) { | ||
| notInlined.append(inlined) | ||
| } | ||
|
|
@@ -120,7 +142,7 @@ case class InlineCTE(alwaysInline: Boolean = false) extends Rule[LogicalPlan] { | |
| inlineCTE(child, cteMap, notInlined) | ||
|
|
||
| case ref: CTERelationRef => | ||
| val (cteDef, refCount) = cteMap(ref.cteId) | ||
| val (cteDef, refCount, _) = cteMap(ref.cteId) | ||
| if (shouldInline(cteDef, refCount)) { | ||
| if (ref.outputSet == cteDef.outputSet) { | ||
| cteDef.child | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.