Skip to content

Commit b295714

Browse files
marmbrusrxin
authored andcommitted
[SQL] Better logging when applying rules.
Author: Michael Armbrust <michael@databricks.com> Closes #616 from marmbrus/ruleLogging and squashes the following commits: 39c09fe [Michael Armbrust] Fix off by one error. 5af3537 [Michael Armbrust] Better logging when applying rules.
1 parent 4669a84 commit b295714

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/rules/RuleExecutor.scala

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,18 +50,18 @@ abstract class RuleExecutor[TreeType <: TreeNode[_]] extends Logging {
5050
var curPlan = plan
5151

5252
batches.foreach { batch =>
53+
val batchStartPlan = curPlan
5354
var iteration = 1
5455
var lastPlan = curPlan
55-
curPlan = batch.rules.foldLeft(curPlan) { case (plan, rule) => rule(plan) }
56+
var continue = true
5657

5758
// Run until fix point (or the max number of iterations as specified in the strategy.
58-
while (iteration < batch.strategy.maxIterations && !curPlan.fastEquals(lastPlan)) {
59-
lastPlan = curPlan
59+
while (continue) {
6060
curPlan = batch.rules.foldLeft(curPlan) {
6161
case (plan, rule) =>
6262
val result = rule(plan)
6363
if (!result.fastEquals(plan)) {
64-
logger.debug(
64+
logger.trace(
6565
s"""
6666
|=== Applying Rule ${rule.ruleName} ===
6767
|${sideBySide(plan.treeString, result.treeString).mkString("\n")}
@@ -71,6 +71,26 @@ abstract class RuleExecutor[TreeType <: TreeNode[_]] extends Logging {
7171
result
7272
}
7373
iteration += 1
74+
if (iteration > batch.strategy.maxIterations) {
75+
logger.info(s"Max iterations ($iteration) reached for batch ${batch.name}")
76+
continue = false
77+
}
78+
79+
if (curPlan.fastEquals(lastPlan)) {
80+
logger.trace(s"Fixed point reached for batch ${batch.name} after $iteration iterations.")
81+
continue = false
82+
}
83+
lastPlan = curPlan
84+
}
85+
86+
if (!batchStartPlan.fastEquals(curPlan)) {
87+
logger.debug(
88+
s"""
89+
|=== Result of Batch ${batch.name} ===
90+
|${sideBySide(plan.treeString, curPlan.treeString).mkString("\n")}
91+
""".stripMargin)
92+
} else {
93+
logger.trace(s"Batch ${batch.name} has no effect.")
7494
}
7595
}
7696

0 commit comments

Comments
 (0)