@@ -69,6 +69,19 @@ case class InlineCTE(alwaysInline: Boolean = false) extends Rule[LogicalPlan] {
6969 cteDef.child.exists(_.expressions.exists(_.isInstanceOf [OuterReference ]))
7070 }
7171
72+ /**
73+ * Accumulates all the CTEs from a plan into a special map.
74+ *
75+ * @param plan The plan to collect the CTEs from
76+ * @param cteMap A mutable map that accumulates the CTEs and their reference information by CTE
77+ * ids. The value of the map is tuple whose elements are:
78+ * - The CTE definition
79+ * - The number of incoming references to the CTE. This includes references from
80+ * outer CTEs and regular places.
81+ * - A mutable inner map that tracks outgoing references (counts) to other CTEs.
82+ * @param outerCTEId While collecting the map we use this optional CTE id to identify the
83+ * current outer CTE.
84+ */
7285 def buildCTEMap (
7386 plan : LogicalPlan ,
7487 cteMap : mutable.Map [Long , (CTERelationDef , Int , mutable.Map [Long , Int ])],
@@ -109,15 +122,22 @@ case class InlineCTE(alwaysInline: Boolean = false) extends Rule[LogicalPlan] {
109122 }
110123 }
111124
125+ /**
126+ * Cleans the CTE map by removing those CTEs that are not referenced at all and corrects those
127+ * CTE's reference counts where the removed CTE referred to.
128+ *
129+ * @param cteMap A mutable map that accumulates the CTEs and their reference information by CTE
130+ * ids. Needs to be sorted to speed up cleaning.
131+ */
112132 private def cleanCTEMap (
113- cteRefMap : mutable.SortedMap [Long , (CTERelationDef , Int , mutable.Map [Long , Int ])]
133+ cteMap : mutable.SortedMap [Long , (CTERelationDef , Int , mutable.Map [Long , Int ])]
114134 ) = {
115- cteRefMap .keys.toSeq.reverse.foreach { currentCTEId =>
116- val (_, currentRefCount, refMap) = cteRefMap (currentCTEId)
135+ cteMap .keys.toSeq.reverse.foreach { currentCTEId =>
136+ val (_, currentRefCount, refMap) = cteMap (currentCTEId)
117137 if (currentRefCount == 0 ) {
118138 refMap.foreach { case (referencedCTEId, uselessRefCount) =>
119- val (cteDef, refCount, refMap) = cteRefMap (referencedCTEId)
120- cteRefMap (referencedCTEId) = (cteDef, refCount - uselessRefCount, refMap)
139+ val (cteDef, refCount, refMap) = cteMap (referencedCTEId)
140+ cteMap (referencedCTEId) = (cteDef, refCount - uselessRefCount, refMap)
121141 }
122142 }
123143 }
0 commit comments