Skip to content

Commit 52b20f3

Browse files
author
pj.fanning
committed
[SPARK-20871][SQL] log Janino code at info level (but include a max of 1000 lines)
1 parent 803166c commit 52b20f3

File tree

3 files changed

+33
-7
lines changed

3 files changed

+33
-7
lines changed

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,22 @@ import java.util.regex.Matcher
2828
object CodeFormatter {
2929
val commentHolder = """\/\*(.+?)\*\/""".r
3030

31-
def format(code: CodeAndComment): String = {
31+
def format(code: CodeAndComment): String = format(code, -1)
32+
33+
def format(code: CodeAndComment, maxLines: Int): String = {
3234
val formatter = new CodeFormatter
33-
code.body.split("\n").foreach { line =>
35+
val lines = code.body.split("\n")
36+
val needToTruncate = maxLines >= 0 && lines.length > maxLines
37+
val filteredLines = if (needToTruncate) lines.take(maxLines) else lines
38+
filteredLines.foreach { line =>
3439
val commentReplaced = commentHolder.replaceAllIn(
3540
line.trim,
3641
m => code.comment.get(m.group(1)).map(Matcher.quoteReplacement).getOrElse(m.group(0)))
3742
formatter.addLine(commentReplaced)
3843
}
44+
if (needToTruncate) {
45+
formatter.addLine(s"[truncated to $maxLines lines (total lines is ${lines.length})]")
46+
}
3947
formatter.result()
4048
}
4149

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1050,10 +1050,12 @@ object CodeGenerator extends Logging {
10501050
case e: JaninoRuntimeException =>
10511051
val msg = s"failed to compile: $e"
10521052
logError(msg, e)
1053+
logInfo(s"\n${CodeFormatter.format(code, 1000)}")
10531054
throw new JaninoRuntimeException(msg, e)
10541055
case e: CompileException =>
10551056
val msg = s"failed to compile: $e"
10561057
logError(msg, e)
1058+
logInfo(s"\n${CodeFormatter.format(code, 1000)}")
10571059
throw new CompileException(msg, e.getLocation)
10581060
}
10591061
evaluator.getClazz().newInstance().asInstanceOf[GeneratedClass]

sql/catalyst/src/test/scala/org/apache/spark/sql/catalyst/expressions/codegen/CodeFormatterSuite.scala

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,18 @@ package org.apache.spark.sql.catalyst.expressions.codegen
2020
import org.apache.spark.SparkFunSuite
2121
import org.apache.spark.sql.catalyst.util._
2222

23-
2423
class CodeFormatterSuite extends SparkFunSuite {
2524

26-
def testCase(name: String)(
27-
input: String, comment: Map[String, String] = Map.empty)(expected: String): Unit = {
25+
def testCase(name: String)(input: String,
26+
comment: Map[String, String] = Map.empty, maxLines: Int = -1)(expected: String): Unit = {
2827
test(name) {
2928
val sourceCode = new CodeAndComment(input.trim, comment)
30-
if (CodeFormatter.format(sourceCode).trim !== expected.trim) {
29+
if (CodeFormatter.format(sourceCode, maxLines).trim !== expected.trim) {
3130
fail(
3231
s"""
3332
|== FAIL: Formatted code doesn't match ===
34-
|${sideBySide(CodeFormatter.format(sourceCode).trim, expected.trim).mkString("\n")}
33+
|${sideBySide(CodeFormatter.format(sourceCode, maxLines).trim,
34+
expected.trim).mkString("\n")}
3535
""".stripMargin)
3636
}
3737
}
@@ -129,6 +129,22 @@ class CodeFormatterSuite extends SparkFunSuite {
129129
""".stripMargin
130130
}
131131

132+
testCase("function calls with maxLines=2") (
133+
"""
134+
|foo(
135+
|a,
136+
|b,
137+
|c)
138+
""".stripMargin,
139+
maxLines = 2
140+
) {
141+
"""
142+
|/* 001 */ foo(
143+
|/* 002 */ a,
144+
|/* 003 */ [truncated to 2 lines (total lines is 4)]
145+
""".stripMargin
146+
}
147+
132148
testCase("single line comments") {
133149
"""
134150
|// This is a comment about class A { { { ( (

0 commit comments

Comments
 (0)