Skip to content
Closed
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -140,25 +140,26 @@ case class InlineCTE(
cteMap: mutable.Map[Long, CTEReferenceInfo]): LogicalPlan = {
plan match {
case WithCTE(child, cteDefs) =>
val remainingDefs = cteDefs.filter { cteDef =>
val notInlined = mutable.ArrayBuffer.empty[CTERelationDef]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is also the behavior of the previous code.

cteDefs.foreach { cteDef =>
val refInfo = cteMap(cteDef.id)
if (refInfo.refCount > 0) {
val newDef = refInfo.cteDef.copy(child = inlineCTE(refInfo.cteDef.child, cteMap))
val inlineDecision = shouldInline(newDef, refInfo.refCount)
cteMap(cteDef.id) = cteMap(cteDef.id).copy(
cteDef = newDef, shouldInline = inlineDecision
)
// Retain the not-inlined CTE relations in place.
!inlineDecision
} else {
keepDanglingRelations
if (!inlineDecision) notInlined += newDef
} else if (keepDanglingRelations) {
notInlined += refInfo.cteDef
}
}
val inlined = inlineCTE(child, cteMap)
if (remainingDefs.isEmpty) {
if (notInlined.isEmpty) {
inlined
} else {
WithCTE(inlined, remainingDefs)
// Retain the not-inlined CTE relations in place.
WithCTE(inlined, notInlined.toSeq)
}

case ref: CTERelationRef =>
Expand Down
13 changes: 13 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/CTEInlineSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,19 @@ abstract class CTEInlineSuiteBase
checkErrorTableNotFound(e, "`tab_non_exists`", ExpectedContext("tab_non_exists", 83, 96))
}
}

test("SPARK-48307: not-inlined CTE references sibling") {
withTempView("t") {
Copy link
Member

Choose a reason for hiding this comment

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

I can't find where you create or use this view

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah I copied it from other test but didn't end up using it. Let me remove

val df = sql(
"""
|WITH
|v1 AS (SELECT 1 col),
|v2 AS (SELECT col, rand() FROM v1)
|SELECT l.col FROM v2 l JOIN v2 r ON l.col = r.col
|""".stripMargin)
checkAnswer(df, Row(1))
}
}
}

class CTEInlineSuiteAEOff extends CTEInlineSuiteBase with DisableAdaptiveExecutionSuite
Expand Down