Skip to content
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

Support save WithCTE for insertRepartitionBeforeWrite #6783

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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 @@ -128,6 +128,7 @@ trait RepartitionBeforeWriteHelper extends Rule[LogicalPlan] {
def canInsertRepartitionByExpression(plan: LogicalPlan): Boolean = {
def canInsert(p: LogicalPlan): Boolean = p match {
case Project(_, child) => canInsert(child)
case WithCTE(plan, _) => canInsert(plan)
case SubqueryAlias(_, child) => canInsert(child)
case Limit(_, _) => false
case _: Sort => false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ trait RepartitionBeforeWriteHelper extends Rule[LogicalPlan] {
def canInsertRepartitionByExpression(plan: LogicalPlan): Boolean = {
def canInsert(p: LogicalPlan): Boolean = p match {
case Project(_, child) => canInsert(child)
case WithCTE(plan, _) => canInsert(plan)
case SubqueryAlias(_, child) => canInsert(child)
case Limit(_, _) => false
case _: Sort => false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ trait RepartitionBeforeWriteHelper extends Rule[LogicalPlan] {
def canInsertRepartitionByExpression(plan: LogicalPlan): Boolean = {
def canInsert(p: LogicalPlan): Boolean = p match {
case Project(_, child) => canInsert(child)
case WithCTE(plan, _) => canInsert(plan)
case SubqueryAlias(_, child) => canInsert(child)
case Limit(_, _) => false
case _: Sort => false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ trait KyuubiSparkSQLExtensionTest extends QueryTest
}

def withListener(df: => DataFrame)(callback: DataWritingCommand => Unit): Unit = {
runWithListener(df.collect())(callback)
}

def runWithListener(func: => Unit)(callback: DataWritingCommand => Unit): Unit = {
val listener = new QueryExecutionListener {
override def onFailure(f: String, qe: QueryExecution, e: Exception): Unit = {}

Expand All @@ -112,7 +116,7 @@ trait KyuubiSparkSQLExtensionTest extends QueryTest
}
spark.listenerManager.register(listener)
try {
df.collect()
func
sparkContext.listenerBus.waitUntilEmpty()
} finally {
spark.listenerManager.unregister(listener)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,4 +292,36 @@ class RebalanceBeforeWritingSuite extends KyuubiSparkSQLExtensionTest {
}
}
}

test("check rebalance exists for WithCTE") {
withSQLConf(
KyuubiSQLConf.INSERT_REPARTITION_BEFORE_WRITE.key -> "true",
KyuubiSQLConf.INSERT_REPARTITION_BEFORE_WRITE_IF_NO_SHUFFLE.key -> "true") {
withTempDir { tempDir =>
withTable("tmp1") {
spark.sql("CREATE TABLE tmp1 (id BIGINT,name BIGINT)")

val result = sql(
"""
|WITH tmp_table AS (
| SELECT id, name FROM tmp1 GROUP BY id, name
|)
|SELECT id, name FROM tmp_table ORDER BY id""".stripMargin)

val colName = (0 until result.schema.size).map(x => "col" + x)
val renamedDf = result.toDF(colName: _*)

runWithListener {
renamedDf.write
.mode("overwrite")
.save(tempDir.getCanonicalPath)
} { write =>
assert(write.collect {
case r: RebalancePartitions => r
}.size == 1)
}
}
}
}
}
}
Loading