Skip to content

Commit 18bcbbd

Browse files
nonglirxin
authored andcommitted
[SPARK-13270][SQL] Remove extra new lines in whole stage codegen and include pipeline plan in comments.
Author: Nong Li <[email protected]> Closes apache#11155 from nongli/spark-13270.
1 parent e88bff1 commit 18bcbbd

File tree

2 files changed

+20
-2
lines changed

2 files changed

+20
-2
lines changed

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/expressions/codegen/CodeFormatter.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,20 @@ package org.apache.spark.sql.catalyst.expressions.codegen
2525
*/
2626
object CodeFormatter {
2727
def format(code: String): String = new CodeFormatter().addLines(code).result()
28+
def stripExtraNewLines(input: String): String = {
29+
val code = new StringBuilder
30+
var lastLine: String = "dummy"
31+
input.split('\n').foreach { l =>
32+
val line = l.trim()
33+
val skip = line == "" && (lastLine == "" || lastLine.endsWith("{"))
34+
if (!skip) {
35+
code.append(line)
36+
code.append("\n")
37+
}
38+
lastLine = line
39+
}
40+
code.result()
41+
}
2842
}
2943

3044
private class CodeFormatter {

sql/core/src/main/scala/org/apache/spark/sql/execution/WholeStageCodegen.scala

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,9 @@ case class WholeStageCodegen(plan: CodegenSupport, children: Seq[SparkPlan])
237237
return new GeneratedIterator(references);
238238
}
239239

240+
/** Codegened pipeline for:
241+
* ${plan.treeString.trim}
242+
*/
240243
class GeneratedIterator extends org.apache.spark.sql.execution.BufferedRowIterator {
241244

242245
private Object[] references;
@@ -256,8 +259,9 @@ case class WholeStageCodegen(plan: CodegenSupport, children: Seq[SparkPlan])
256259
"""
257260

258261
// try to compile, helpful for debug
259-
// println(s"${CodeFormatter.format(source)}")
260-
CodeGenerator.compile(source)
262+
val cleanedSource = CodeFormatter.stripExtraNewLines(source)
263+
// println(s"${CodeFormatter.format(cleanedSource)}")
264+
CodeGenerator.compile(cleanedSource)
261265

262266
plan.upstream().mapPartitions { iter =>
263267

0 commit comments

Comments
 (0)