Skip to content
Closed
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 @@ -28,14 +28,20 @@ import java.util.regex.Matcher
object CodeFormatter {
val commentHolder = """\/\*(.+?)\*\/""".r

def format(code: CodeAndComment): String = {
def format(code: CodeAndComment, maxLines: Int = -1): String = {
val formatter = new CodeFormatter
code.body.split("\n").foreach { line =>
val lines = code.body.split("\n")
val needToTruncate = maxLines >= 0 && lines.length > maxLines
val filteredLines = if (needToTruncate) lines.take(maxLines) else lines
filteredLines.foreach { line =>
val commentReplaced = commentHolder.replaceAllIn(
line.trim,
m => code.comment.get(m.group(1)).map(Matcher.quoteReplacement).getOrElse(m.group(0)))
formatter.addLine(commentReplaced)
}
if (needToTruncate) {
formatter.addLine(s"[truncated to $maxLines lines (total lines is ${lines.length})]")
}
formatter.result()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import org.apache.spark.metrics.source.CodegenMetrics
import org.apache.spark.sql.catalyst.InternalRow
import org.apache.spark.sql.catalyst.expressions._
import org.apache.spark.sql.catalyst.util.{ArrayData, MapData}
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types._
import org.apache.spark.unsafe.Platform
import org.apache.spark.unsafe.types._
Expand Down Expand Up @@ -1037,25 +1038,27 @@ object CodeGenerator extends Logging {
))
evaluator.setExtendedClass(classOf[GeneratedClass])

lazy val formatted = CodeFormatter.format(code)

logDebug({
// Only add extra debugging info to byte code when we are going to print the source code.
evaluator.setDebuggingInformation(true, true, false)
s"\n$formatted"
s"\n${CodeFormatter.format(code)}"
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd suggest dropping the string concatenation with \n here -- it requires an additional copy of the code to be held in-memory and for errors where the code is too long, this causes unnecessary additional pressure on the heap

Copy link
Member Author

Choose a reason for hiding this comment

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

@ash211 the logging will be less user friendly without the \n and this is debug logging - I am working on an enhancement to the CodeFormatter class for the info level logging discussed earlier with @marmbrus that would allow a max number of lines to be specified

})

try {
evaluator.cook("generated.java", code.body)
recordCompilationStats(evaluator)
} catch {
case e: JaninoRuntimeException =>
val msg = s"failed to compile: $e\n$formatted"
val msg = s"failed to compile: $e"
logError(msg, e)
val maxLines = SQLConf.get.loggingMaxLinesForCodegen
logInfo(s"\n${CodeFormatter.format(code, maxLines)}")
throw new JaninoRuntimeException(msg, e)
case e: CompileException =>
val msg = s"failed to compile: $e\n$formatted"
val msg = s"failed to compile: $e"
logError(msg, e)
val maxLines = SQLConf.get.loggingMaxLinesForCodegen
logInfo(s"\n${CodeFormatter.format(code, maxLines)}")
throw new CompileException(msg, e.getLocation)
}
evaluator.getClazz().newInstance().asInstanceOf[GeneratedClass]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,14 @@ object SQLConf {
.intConf
.createWithDefault(20)

val CODEGEN_LOGGING_MAX_LINES = buildConf("spark.sql.codegen.logging.maxLines")
.internal()
.doc("The maximum number of codegen lines to log when errors occur. Use -1 for unlimited.")
.intConf
.checkValue(maxLines => maxLines >= -1, "The maximum must be a positive integer, 0 to " +
"disable logging or -1 to apply no limit.")
.createWithDefault(1000)

val FILES_MAX_PARTITION_BYTES = buildConf("spark.sql.files.maxPartitionBytes")
.doc("The maximum number of bytes to pack into a single partition when reading files.")
.longConf
Expand Down Expand Up @@ -1002,6 +1010,8 @@ class SQLConf extends Serializable with Logging {

def maxCaseBranchesForCodegen: Int = getConf(MAX_CASES_BRANCHES)

def loggingMaxLinesForCodegen: Int = getConf(CODEGEN_LOGGING_MAX_LINES)

def tableRelationCacheSize: Int =
getConf(StaticSQLConf.FILESOURCE_TABLE_RELATION_CACHE_SIZE)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ package org.apache.spark.sql.catalyst.expressions.codegen
import org.apache.spark.SparkFunSuite
import org.apache.spark.sql.catalyst.util._


class CodeFormatterSuite extends SparkFunSuite {

def testCase(name: String)(
input: String, comment: Map[String, String] = Map.empty)(expected: String): Unit = {
def testCase(name: String)(input: String,
comment: Map[String, String] = Map.empty, maxLines: Int = -1)(expected: String): Unit = {
test(name) {
val sourceCode = new CodeAndComment(input.trim, comment)
if (CodeFormatter.format(sourceCode).trim !== expected.trim) {
if (CodeFormatter.format(sourceCode, maxLines).trim !== expected.trim) {
fail(
s"""
|== FAIL: Formatted code doesn't match ===
|${sideBySide(CodeFormatter.format(sourceCode).trim, expected.trim).mkString("\n")}
|${sideBySide(CodeFormatter.format(sourceCode, maxLines).trim,
expected.trim).mkString("\n")}
""".stripMargin)
}
}
Expand Down Expand Up @@ -129,6 +129,36 @@ class CodeFormatterSuite extends SparkFunSuite {
""".stripMargin
}

testCase("function calls with maxLines=0") (
"""
|foo(
|a,
|b,
|c)
""".stripMargin,
maxLines = 0
) {
"""
|/* 001 */ [truncated to 0 lines (total lines is 4)]
""".stripMargin
}

testCase("function calls with maxLines=2") (
"""
|foo(
|a,
|b,
|c)
""".stripMargin,
maxLines = 2
) {
"""
|/* 001 */ foo(
|/* 002 */ a,
|/* 003 */ [truncated to 2 lines (total lines is 4)]
""".stripMargin
}

testCase("single line comments") {
"""
|// This is a comment about class A { { { ( (
Expand Down