-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-15498][TESTS] fix slow tests #13273
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 |
|---|---|---|
|
|
@@ -17,7 +17,7 @@ | |
|
|
||
| package org.apache.spark.sql.catalyst.expressions.codegen | ||
|
|
||
| import org.apache.commons.lang3.StringUtils | ||
| import java.util.regex.Matcher | ||
|
|
||
| /** | ||
| * An utility class that indents a block of code based on the curly braces and parentheses. | ||
|
|
@@ -26,13 +26,17 @@ import org.apache.commons.lang3.StringUtils | |
| * Written by Matei Zaharia. | ||
| */ | ||
| object CodeFormatter { | ||
| val commentHolder = """\/\*(.+?)\*\/""".r | ||
|
|
||
| def format(code: CodeAndComment): String = { | ||
| new CodeFormatter().addLines( | ||
| StringUtils.replaceEach( | ||
| code.body, | ||
| code.comment.keys.toArray, | ||
| code.comment.values.toArray) | ||
| ).result | ||
| val formatter = new CodeFormatter | ||
| code.body.split("\n").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) | ||
| } | ||
| formatter.result() | ||
|
Member
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. Thanks, SGTM! |
||
| } | ||
|
|
||
| def stripExtraNewLines(input: String): String = { | ||
|
|
@@ -53,16 +57,28 @@ object CodeFormatter { | |
| def stripOverlappingComments(codeAndComment: CodeAndComment): CodeAndComment = { | ||
| val code = new StringBuilder | ||
| val map = codeAndComment.comment | ||
|
|
||
| def getComment(line: String): Option[String] = { | ||
|
Contributor
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. The previous one doesn't handle the case that the line is not in the comments map. |
||
| if (line.startsWith("/*") && line.endsWith("*/")) { | ||
| map.get(line.substring(2, line.length - 2)) | ||
| } else { | ||
| None | ||
| } | ||
| } | ||
|
|
||
| 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)) | ||
|
|
||
| val skip = getComment(lastLine).zip(getComment(line)).exists { | ||
| case (lastComment, currentComment) => | ||
| lastComment.substring(3).contains(currentComment.substring(3)) | ||
| } | ||
|
|
||
| if (!skip) { | ||
| code.append(line) | ||
| code.append("\n") | ||
| code.append(line).append("\n") | ||
| } | ||
|
|
||
| lastLine = line | ||
| } | ||
| new CodeAndComment(code.result().trim(), map) | ||
|
|
@@ -117,8 +133,9 @@ private class CodeFormatter { | |
| } else { | ||
| indentString | ||
| } | ||
| code.append(f"/* ${currentLine}%03d */ ") | ||
| code.append(f"/* ${currentLine}%03d */") | ||
| if (line.trim().length > 0) { | ||
| code.append(" ") // add a space after the line number comment. | ||
| code.append(thisLineIndent) | ||
| if (inCommentBlock && line.startsWith("*") || line.startsWith("*/")) code.append(" ") | ||
| code.append(line) | ||
|
|
@@ -129,10 +146,5 @@ private class CodeFormatter { | |
| currentLine += 1 | ||
| } | ||
|
|
||
| private def addLines(code: String): CodeFormatter = { | ||
| code.split('\n').foreach(s => addLine(s.trim())) | ||
| this | ||
| } | ||
|
|
||
| private def result(): String = code.result() | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,9 +23,10 @@ import org.apache.spark.sql.catalyst.util._ | |
|
|
||
| class CodeFormatterSuite extends SparkFunSuite { | ||
|
|
||
| def testCase(name: String)(input: String)(expected: String): Unit = { | ||
| def testCase(name: String)( | ||
| input: String, comment: Map[String, String] = Map.empty)(expected: String): Unit = { | ||
| test(name) { | ||
| val sourceCode = new CodeAndComment(input, Map.empty) | ||
| val sourceCode = new CodeAndComment(input.trim, comment) | ||
|
Contributor
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. always trim the input, so that we can write instead of |
||
| if (CodeFormatter.format(sourceCode).trim !== expected.trim) { | ||
| fail( | ||
| s""" | ||
|
|
@@ -43,19 +44,21 @@ class CodeFormatterSuite extends SparkFunSuite { | |
| |/*project_c2*/ | ||
| """.stripMargin, | ||
| Map( | ||
| "/*project_c4*/" -> "// (((input[0, bigint, false] + 1) + 2) + 3))", | ||
| "/*project_c3*/" -> "// ((input[0, bigint, false] + 1) + 2)", | ||
| "/*project_c2*/" -> "// (input[0, bigint, false] + 1)" | ||
| "project_c4" -> "// (((input[0, bigint, false] + 1) + 2) + 3))", | ||
| "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 { | ||
| """ | ||
| |class A { | ||
| |blahblah; | ||
| |}""".stripMargin | ||
| |} | ||
| """.stripMargin | ||
| }{ | ||
| """ | ||
| |/* 001 */ class A { | ||
|
|
@@ -65,11 +68,13 @@ class CodeFormatterSuite extends SparkFunSuite { | |
| } | ||
|
|
||
| testCase("nested example") { | ||
| """class A { | ||
| """ | ||
| |class A { | ||
| | if (c) { | ||
| |duh; | ||
| |} | ||
| |}""".stripMargin | ||
| |} | ||
| """.stripMargin | ||
| } { | ||
| """ | ||
| |/* 001 */ class A { | ||
|
|
@@ -81,9 +86,11 @@ class CodeFormatterSuite extends SparkFunSuite { | |
| } | ||
|
|
||
| testCase("single line") { | ||
| """class A { | ||
| """ | ||
| |class A { | ||
| | if (c) {duh;} | ||
| |}""".stripMargin | ||
| |} | ||
| """.stripMargin | ||
| }{ | ||
| """ | ||
| |/* 001 */ class A { | ||
|
|
@@ -93,9 +100,11 @@ class CodeFormatterSuite extends SparkFunSuite { | |
| } | ||
|
|
||
| testCase("if else on the same line") { | ||
| """class A { | ||
| """ | ||
| |class A { | ||
| | if (c) {duh;} else {boo;} | ||
| |}""".stripMargin | ||
| |} | ||
| """.stripMargin | ||
| }{ | ||
| """ | ||
| |/* 001 */ class A { | ||
|
|
@@ -105,10 +114,12 @@ class CodeFormatterSuite extends SparkFunSuite { | |
| } | ||
|
|
||
| testCase("function calls") { | ||
| """foo( | ||
| """ | ||
| |foo( | ||
| |a, | ||
| |b, | ||
| |c)""".stripMargin | ||
| |c) | ||
| """.stripMargin | ||
| }{ | ||
| """ | ||
| |/* 001 */ foo( | ||
|
|
@@ -119,10 +130,12 @@ class CodeFormatterSuite extends SparkFunSuite { | |
| } | ||
|
|
||
| testCase("single line comments") { | ||
| """// This is a comment about class A { { { ( ( | ||
| """ | ||
| |// This is a comment about class A { { { ( ( | ||
| |class A { | ||
| |class body; | ||
| |}""".stripMargin | ||
| |} | ||
| """.stripMargin | ||
| }{ | ||
| """ | ||
| |/* 001 */ // This is a comment about class A { { { ( ( | ||
|
|
@@ -133,10 +146,12 @@ class CodeFormatterSuite extends SparkFunSuite { | |
| } | ||
|
|
||
| testCase("single line comments /* */ ") { | ||
| """/** This is a comment about class A { { { ( ( */ | ||
| """ | ||
| |/** This is a comment about class A { { { ( ( */ | ||
| |class A { | ||
| |class body; | ||
| |}""".stripMargin | ||
| |} | ||
| """.stripMargin | ||
| }{ | ||
| """ | ||
| |/* 001 */ /** This is a comment about class A { { { ( ( */ | ||
|
|
@@ -147,12 +162,14 @@ class CodeFormatterSuite extends SparkFunSuite { | |
| } | ||
|
|
||
| testCase("multi-line comments") { | ||
| """ /* This is a comment about | ||
| """ | ||
| | /* This is a comment about | ||
| |class A { | ||
| |class body; ...*/ | ||
| |class A { | ||
| |class body; | ||
| |}""".stripMargin | ||
| |} | ||
| """.stripMargin | ||
| }{ | ||
| """ | ||
| |/* 001 */ /* This is a comment about | ||
|
|
@@ -164,30 +181,56 @@ class CodeFormatterSuite extends SparkFunSuite { | |
| """.stripMargin | ||
| } | ||
|
|
||
| // scalastyle:off whitespace.end.of.line | ||
| testCase("reduce empty lines") { | ||
| CodeFormatter.stripExtraNewLines( | ||
| """class A { | ||
| """ | ||
| |class A { | ||
| | | ||
| | | ||
| | /*** comment1 */ | ||
| | /* | ||
| | * multi | ||
| | * line | ||
| | * comment | ||
| | */ | ||
| | | ||
| | class body; | ||
| | | ||
| | | ||
| | if (c) {duh;} | ||
| | else {boo;} | ||
| |}""".stripMargin) | ||
| |} | ||
| """.stripMargin.trim) | ||
| }{ | ||
| """ | ||
| |/* 001 */ class A { | ||
| |/* 002 */ /*** comment1 */ | ||
| |/* 003 */ class body; | ||
| |/* 004 */ | ||
| |/* 005 */ if (c) {duh;} | ||
| |/* 006 */ else {boo;} | ||
| |/* 007 */ } | ||
| |/* 002 */ /* | ||
| |/* 003 */ * multi | ||
| |/* 004 */ * line | ||
| |/* 005 */ * comment | ||
| |/* 006 */ */ | ||
| |/* 007 */ class body; | ||
| |/* 008 */ | ||
| |/* 009 */ if (c) {duh;} | ||
| |/* 010 */ else {boo;} | ||
| |/* 011 */ } | ||
| """.stripMargin | ||
| } | ||
|
|
||
| testCase("comment place holder")( | ||
| """ | ||
| |/*c1*/ | ||
| |class A | ||
| |/*c2*/ | ||
| |class B | ||
| |/*c1*//*c2*/ | ||
| """.stripMargin, Map("c1" -> "/*abc*/", "c2" -> "/*xyz*/") | ||
| ) { | ||
| """ | ||
| |/* 001 */ /*abc*/ | ||
| |/* 002 */ class A | ||
| |/* 003 */ /*xyz*/ | ||
| |/* 004 */ class B | ||
| |/* 005 */ /*abc*//*xyz*/ | ||
| """.stripMargin | ||
| } | ||
| // scalastyle:on whitespace.end.of.line | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
cc @sarutak , here I assume the placeholder will always take an entire line, is it corrected?
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.
How slow is it if we use an regexp to match the placeholder here?