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 @@ -89,6 +89,14 @@ object CodeFormatter {
}
new CodeAndComment(code.result().trim(), map)
}

def stripExtraNewLinesAndComments(input: String): String = {
val commentReg =
("""([ |\t]*?\/\*[\s|\S]*?\*\/[ |\t]*?)|""" + // strip /*comment*/
"""([ |\t]*?\/\/[\s\S]*?\n)""").r // strip //comment
val codeWithoutComment = commentReg.replaceAllIn(input, "")
codeWithoutComment.replaceAll("""\n\s*\n""", "\n") // strip ExtraNewLines
}
Copy link
Member

Choose a reason for hiding this comment

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

Could you also add back the test case of this function?

}

private class CodeFormatter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -772,16 +772,19 @@ class CodegenContext {
foldFunctions: Seq[String] => String = _.mkString("", ";\n", ";")): String = {
val blocks = new ArrayBuffer[String]()
val blockBuilder = new StringBuilder()
var length = 0
for (code <- expressions) {
// We can't know how many bytecode will be generated, so use the length of source code
// as metric. A method should not go beyond 8K, otherwise it will not be JITted, should
// also not be too small, or it will have many function calls (for wide table), see the
// results in BenchmarkWideTable.
if (blockBuilder.length > 1024) {
if (length > 1024) {
Copy link
Member

Choose a reason for hiding this comment

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

Could you add an internal SQLConf for it and make it adjustable?

Copy link
Member

Choose a reason for hiding this comment

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

+1

blocks += blockBuilder.toString()
blockBuilder.clear()
length = 0
}
blockBuilder.append(code)
length += CodeFormatter.stripExtraNewLinesAndComments(code).length
}
blocks += blockBuilder.toString()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,38 @@ class CodeFormatterSuite extends SparkFunSuite {
assert(reducedCode.body === "/*project_c4*/")
}

test("removing extra new lines and comments") {
val code =
"""
|/*
| * multi
| * line
| * comments
| */
|
|public function() {
|/*comment*/
| /*comment_with_space*/
|code_body
|//comment
|code_body
| //comment_with_space
|
|code_body
|}
""".stripMargin

val reducedCode = CodeFormatter.stripExtraNewLinesAndComments(code)
assert(reducedCode ===
"""
|public function() {
|code_body
|code_body
|code_body
|}
""".stripMargin)
}

testCase("basic example") {
"""
|class A {
Expand Down