-
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 4 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 | ||||||
|
|
@@ -68,50 +69,91 @@ case class InlineCTE(alwaysInline: Boolean = false) extends Rule[LogicalPlan] { | |||||
| cteDef.child.exists(_.expressions.exists(_.isInstanceOf[OuterReference])) | ||||||
| } | ||||||
|
|
||||||
| /** | ||||||
| * Accumulates all the CTEs from a plan into a special map. | ||||||
| * | ||||||
| * @param plan The plan to collect the CTEs from | ||||||
| * @param cteMap A mutable map that accumulates the CTEs and their reference information by CTE | ||||||
| * ids. The value of the map is tuple whose elements are: | ||||||
| * - The CTE definition | ||||||
| * - The number of incoming references to the CTE. This includes references from | ||||||
| * outer CTEs and regular places. | ||||||
|
||||||
| * outer CTEs and regular places. | |
| * inner CTEs and regular places. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually wanted to write other CTEs and not inner/outer. E.g. in
WITH (
cte1 AS (SELECT 1),
cte2 AS (SELECT * FROM cte1)
)
SELECT * FROM cte1 JOIN cte2 ON ...
the reference count of cte1 is 2. 1 is from an "other" CTE (but not "inner"/"outer") (and 1 is from a "regular place").
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed in 806c2de
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cloud-fan, please let me know if anything else is needed here.
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.