-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-13135][SQL] Don't print expressions recursively in generated code #13192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,7 +40,7 @@ object CodeFormatter { | |
| var lastLine: String = "dummy" | ||
| input.split('\n').foreach { l => | ||
| val line = l.trim() | ||
| val skip = line == "" && (lastLine == "" || lastLine.endsWith("{")) | ||
| val skip = line == "" && (lastLine == "" || lastLine.endsWith("{") || lastLine.endsWith("*/")) | ||
| if (!skip) { | ||
| code.append(line) | ||
| code.append("\n") | ||
|
|
@@ -49,6 +49,24 @@ object CodeFormatter { | |
| } | ||
| code.result() | ||
| } | ||
|
|
||
| def stripOverlappingComments(codeAndComment: CodeAndComment): CodeAndComment = { | ||
| val code = new StringBuilder | ||
| val map = codeAndComment.comment | ||
| var lastLine: String = "dummy" | ||
| codeAndComment.body.split('\n').foreach { l => | ||
| val line = l.trim() | ||
| val skip = lastLine.startsWith("/*") && lastLine.endsWith("*/") && | ||
| line.startsWith("/*") && line.endsWith("*/") && | ||
| map(lastLine).substring(3).contains(map(line).substring(3)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Have you check that this actually work? I think we have placeholders here so will not find any duplicated comments to skip.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, it should work, I missed the
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's okay for the performance.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, the skip condition is checking only consecutive comments lines.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SGTM |
||
| if (!skip) { | ||
| code.append(line) | ||
| code.append("\n") | ||
| } | ||
| lastLine = line | ||
| } | ||
| new CodeAndComment(code.result().trim(), map) | ||
| } | ||
| } | ||
|
|
||
| private class CodeFormatter { | ||
|
|
@@ -100,8 +118,11 @@ private class CodeFormatter { | |
| indentString | ||
| } | ||
| code.append(f"/* ${currentLine}%03d */ ") | ||
| code.append(thisLineIndent) | ||
| code.append(line) | ||
| if (line.trim().length > 0) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you explain a bit more about this logic?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure! This After As you see, the redundant spaces for indentation go away. |
||
| code.append(thisLineIndent) | ||
| if (inCommentBlock && line.startsWith("*") || line.startsWith("*/")) code.append(" ") | ||
| code.append(line) | ||
| } | ||
| code.append("\n") | ||
| indentLevel = newIndentLevel | ||
| indentString = " " * (indentSize * newIndentLevel) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -36,6 +36,22 @@ class CodeFormatterSuite extends SparkFunSuite { | |
| } | ||
| } | ||
|
|
||
| test("removing overlapping comments") { | ||
| val code = new CodeAndComment( | ||
| """/*project_c4*/ | ||
| |/*project_c3*/ | ||
| |/*project_c2*/ | ||
| """.stripMargin, | ||
| Map( | ||
| "/*project_c4*/" -> "// (((input[0, bigint, false] + 1) + 2) + 3))", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what if we have multi-line comment? Does it still work?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi, @cloud-fan .
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. seems no such case, then it's fine |
||
| "/*project_c3*/" -> "// ((input[0, bigint, false] + 1) + 2)", | ||
| "/*project_c2*/" -> "// (input[0, bigint, false] + 1)" | ||
| )) | ||
|
|
||
| val reducedCode = CodeFormatter.stripOverlappingComments(code) | ||
| assert(reducedCode.body === "/*project_c4*/") | ||
| } | ||
|
|
||
| testCase("basic example") { | ||
| """class A { | ||
| |blahblah; | ||
|
|
@@ -147,4 +163,31 @@ class CodeFormatterSuite extends SparkFunSuite { | |
| |/* 006 */ } | ||
| """.stripMargin | ||
| } | ||
|
|
||
| // scalastyle:off whitespace.end.of.line | ||
| testCase("reduce empty lines") { | ||
| CodeFormatter.stripExtraNewLines( | ||
| """class A { | ||
| | | ||
| | | ||
| | /*** comment1 */ | ||
| | | ||
| | class body; | ||
| | | ||
| | | ||
| | if (c) {duh;} | ||
| | else {boo;} | ||
| |}""".stripMargin) | ||
| }{ | ||
| """ | ||
| |/* 001 */ class A { | ||
| |/* 002 */ /*** comment1 */ | ||
| |/* 003 */ class body; | ||
| |/* 004 */ | ||
| |/* 005 */ if (c) {duh;} | ||
| |/* 006 */ else {boo;} | ||
| |/* 007 */ } | ||
| """.stripMargin | ||
| } | ||
| // scalastyle:on whitespace.end.of.line | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
are we assuming the comment holder will always take an entire line?