Skip to content

Commit 07393c1

Browse files
authored
chore: Improve test assertions in plan stability suite (apache#2505)
1 parent def45fa commit 07393c1

File tree

1 file changed

+48
-60
lines changed

1 file changed

+48
-60
lines changed

spark/src/test/scala/org/apache/spark/sql/comet/CometPlanStabilitySuite.scala

Lines changed: 48 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -89,79 +89,64 @@ trait CometPlanStabilitySuite extends DisableAdaptiveExecutionSuite with TPCDSBa
8989
new File(goldenFilePath, goldenFileName)
9090
}
9191

92-
private def isApproved(
93-
dir: File,
94-
actualSimplifiedPlan: String,
95-
actualExplain: String): Boolean = {
96-
val simplifiedFile = new File(dir, "simplified.txt")
97-
val expectedSimplified = FileUtils.readFileToString(simplifiedFile, StandardCharsets.UTF_8)
98-
val explainFile = new File(dir, "explain.txt")
99-
val expectedExplain = FileUtils.readFileToString(explainFile, StandardCharsets.UTF_8)
100-
expectedSimplified == actualSimplifiedPlan && expectedExplain == actualExplain
101-
}
102-
10392
/**
10493
* Serialize and save this SparkPlan. The resulting file is used by [[checkWithApproved]] to
10594
* check stability.
10695
*
107-
* @param plan
108-
* the SparkPlan
109-
* @param name
110-
* the name of the query
96+
* @param dir
97+
* the directory to write to
98+
* @param simplified
99+
* the simplified plan
111100
* @param explain
112101
* the full explain output; this is saved to help debug later as the simplified plan is not
113102
* too useful for debugging
114103
*/
115-
private def generateGoldenFile(plan: SparkPlan, name: String, explain: String): Unit = {
116-
val dir = getDirForTest(name)
117-
val simplified = getSimplifiedPlan(plan)
118-
val foundMatch = dir.exists() && isApproved(dir, simplified, explain)
119-
120-
if (!foundMatch) {
121-
FileUtils.deleteDirectory(dir)
122-
if (!dir.mkdirs()) {
123-
fail(s"Could not create dir: $dir")
124-
}
125-
126-
val file = new File(dir, "simplified.txt")
127-
FileUtils.writeStringToFile(file, simplified, StandardCharsets.UTF_8)
128-
val fileOriginalPlan = new File(dir, "explain.txt")
129-
FileUtils.writeStringToFile(fileOriginalPlan, explain, StandardCharsets.UTF_8)
130-
logDebug(s"APPROVED: $file $fileOriginalPlan")
104+
private def generateGoldenFile(dir: File, simplified: String, explain: String): Unit = {
105+
FileUtils.deleteDirectory(dir)
106+
if (!dir.mkdirs()) {
107+
fail(s"Could not create dir: $dir")
131108
}
109+
110+
val file = new File(dir, "simplified.txt")
111+
FileUtils.writeStringToFile(file, simplified, StandardCharsets.UTF_8)
112+
val fileOriginalPlan = new File(dir, "explain.txt")
113+
FileUtils.writeStringToFile(fileOriginalPlan, explain, StandardCharsets.UTF_8)
114+
logDebug(s"APPROVED: $file $fileOriginalPlan")
132115
}
133116

134-
private def checkWithApproved(plan: SparkPlan, name: String, explain: String): Unit = {
135-
val dir = getDirForTest(name)
136-
val tempDir = FileUtils.getTempDirectory
137-
val actualSimplified = getSimplifiedPlan(plan)
138-
val foundMatch = isApproved(dir, actualSimplified, explain)
117+
private def checkWithApproved(
118+
dir: File,
119+
name: String,
120+
simplified: String,
121+
explain: String): Unit = {
139122

140-
if (!foundMatch) {
141-
// show diff with last approved
142-
val approvedSimplifiedFile = new File(dir, "simplified.txt")
143-
val approvedExplainFile = new File(dir, "explain.txt")
123+
val approvedSimplifiedFile = new File(dir, "simplified.txt")
124+
val approvedExplainFile = new File(dir, "explain.txt")
144125

145-
val actualSimplifiedFile = new File(tempDir, s"$name.actual.simplified.txt")
146-
val actualExplainFile = new File(tempDir, s"$name.actual.explain.txt")
126+
// write actual files out for debugging
127+
val tempDir = FileUtils.getTempDirectory
128+
val actualSimplifiedFile = new File(tempDir, s"$name.actual.simplified.txt")
129+
val actualExplainFile = new File(tempDir, s"$name.actual.explain.txt")
130+
FileUtils.writeStringToFile(actualSimplifiedFile, simplified, StandardCharsets.UTF_8)
131+
FileUtils.writeStringToFile(actualExplainFile, explain, StandardCharsets.UTF_8)
147132

148-
val approvedSimplified =
149-
FileUtils.readFileToString(approvedSimplifiedFile, StandardCharsets.UTF_8)
150-
// write out for debugging
151-
FileUtils.writeStringToFile(actualSimplifiedFile, actualSimplified, StandardCharsets.UTF_8)
152-
FileUtils.writeStringToFile(actualExplainFile, explain, StandardCharsets.UTF_8)
133+
comparePlans("simplified", approvedSimplifiedFile, actualSimplifiedFile)
134+
comparePlans("explain", approvedExplainFile, actualExplainFile)
135+
}
153136

137+
private def comparePlans(planType: String, expectedFile: File, actualFile: File): Unit = {
138+
val expected = FileUtils.readFileToString(expectedFile, StandardCharsets.UTF_8)
139+
val actual = FileUtils.readFileToString(actualFile, StandardCharsets.UTF_8)
140+
if (expected != actual) {
154141
fail(s"""
155-
|Plans did not match:
156-
|last approved simplified plan: ${approvedSimplifiedFile.getAbsolutePath}
157-
|last approved explain plan: ${approvedExplainFile.getAbsolutePath}
158-
|
159-
|$approvedSimplified
160-
|
161-
|actual simplified plan: ${actualSimplifiedFile.getAbsolutePath}
162-
|actual explain plan: ${actualExplainFile.getAbsolutePath}
163-
|
164-
|$actualSimplified
142+
|Plans did not match:
143+
|last approved $planType plan: ${expectedFile.getAbsolutePath}
144+
|
145+
|$expected
146+
|
147+
|actual $planType plan: ${actualFile.getAbsolutePath}
148+
|
149+
|$actual
165150
""".stripMargin)
166151
}
167152
}
@@ -274,13 +259,16 @@ trait CometPlanStabilitySuite extends DisableAdaptiveExecutionSuite with TPCDSBa
274259
val qe = sql(queryString).queryExecution
275260
val plan = qe.executedPlan
276261
val explain = normalizeLocation(normalizeIds(qe.explainString(FormattedMode)))
277-
262+
val simplified = getSimplifiedPlan(plan)
278263
assert(ValidateRequirements.validate(plan))
279264

265+
val name = query + suffix
266+
val dir = getDirForTest(name)
267+
280268
if (regenerateGoldenFiles) {
281-
generateGoldenFile(plan, query + suffix, explain)
269+
generateGoldenFile(dir, simplified, explain)
282270
} else {
283-
checkWithApproved(plan, query + suffix, explain)
271+
checkWithApproved(dir, name, simplified, explain)
284272
}
285273
}
286274
}

0 commit comments

Comments
 (0)