gengliangwang commented on a change in pull request #29270: URL: https://github.com/apache/spark/pull/29270#discussion_r461668408
########## File path: sql/core/src/test/scala/org/apache/spark/sql/PlanStabilitySuite.scala ########## @@ -0,0 +1,302 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.sql + +import java.io.File + +import scala.collection.mutable + +import org.apache.commons.io.FileUtils + +import org.apache.spark.sql.catalyst.expressions.AttributeSet +import org.apache.spark.sql.catalyst.util._ +import org.apache.spark.sql.execution._ +import org.apache.spark.sql.execution.adaptive.DisableAdaptiveExecutionSuite +import org.apache.spark.sql.execution.exchange.{Exchange, ReusedExchangeExec} +import org.apache.spark.sql.internal.SQLConf + +/** + * Check that TPCDS SparkPlans don't change. + * If there is a regression, the error message looks like this: + * Plans did not match: + * last approved plan: /path/to/tpcds-plan-stability/approved-plans-xxx/q1/0.simplified.txt + * last explain: /path/to/tpcds-plan-stability/approved-plans-xxx/q1/0.explain.txt + * actual plan: /path/to/tmp/q1.actual.simplified.txt + * actual explain: /path/to/tmp/q1.actual.explain.txt + * [side by side plan diff] + * The explain files are saved to help debug later, they are not checked. Only the simplified + * plans are checked (by string comparison). + * + * Approving new plans: + * IF the plan change is intended then re-running the test + * with environ var SPARK_GENERATE_GOLDEN_FILES=1 will make the new plan canon. + * This should be done only for the queries that need it, to avoid unnecessary diffs in the + * other approved plans. + * This can be done by running sbt test-only *PlanStabilitySuite* -- -z "(q31)" + * The new plan files should be part of the PR and reviewed. + * + * Multiple approved plans: + * It's possible that a query has multiple correct plans. This should be decided as part of the + * review. In this case, change the call to approvePlans and set variant=true. + */ +trait PlanStabilitySuite extends TPCDSBase with DisableAdaptiveExecutionSuite { + + private val regenerateGoldenFiles: Boolean = System.getenv("SPARK_GENERATE_GOLDEN_FILES") == "1" + + protected val baseResourcePath = { + // If regenerateGoldenFiles is true, we must be running this in SBT and we use hard-coded + // relative path. Otherwise, we use classloader's getResource to find the location. + if (regenerateGoldenFiles) { + java.nio.file.Paths.get("src", "test", "resources", "tpcds-plan-stability").toFile + } else { + val res = getClass.getClassLoader.getResource("tpcds-plan-stability") + new File(res.getFile) + } + } + + def goldenFilePath: String + + private def getExistingVariants(name: String): Array[Int] = { + val dir = new File(goldenFilePath, name) + // file paths are the form ../q3/2.simplified.txt + val rgx = """(\d+).simplified.txt""".r + + dir.listFiles().filter(_.getName.contains("simplified")).map { file => + val rgx(numStr) = file.getName + numStr.toInt + } + } + + private def getNextVariantNumber(name: String): Int = { + val existingVariants = getExistingVariants(name) + if (existingVariants.isEmpty) { + 0 + } else { + existingVariants.max + 1 + } + } + + private def getDirForTest(name: String): File = { + new File(goldenFilePath, name) + } + + private def isApproved(name: String, plan: SparkPlan): Boolean = { + val dir = getDirForTest(name) + if (!dir.exists()) { + false + } else { + val existingVariants = getExistingVariants(name) + val actual = getSimplifiedPlan(plan) + existingVariants.exists { variant => + val file = new File(dir, s"$variant.simplified.txt") + val approved = FileUtils.readFileToString(file) Review comment: Nit: This is deprecated. Please use `readFileToString(final File file, final Charset encoding)` ########## File path: sql/core/src/test/scala/org/apache/spark/sql/PlanStabilitySuite.scala ########## @@ -0,0 +1,302 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.sql + +import java.io.File + +import scala.collection.mutable + +import org.apache.commons.io.FileUtils + +import org.apache.spark.sql.catalyst.expressions.AttributeSet +import org.apache.spark.sql.catalyst.util._ +import org.apache.spark.sql.execution._ +import org.apache.spark.sql.execution.adaptive.DisableAdaptiveExecutionSuite +import org.apache.spark.sql.execution.exchange.{Exchange, ReusedExchangeExec} +import org.apache.spark.sql.internal.SQLConf + +/** + * Check that TPCDS SparkPlans don't change. + * If there is a regression, the error message looks like this: + * Plans did not match: + * last approved plan: /path/to/tpcds-plan-stability/approved-plans-xxx/q1/0.simplified.txt + * last explain: /path/to/tpcds-plan-stability/approved-plans-xxx/q1/0.explain.txt + * actual plan: /path/to/tmp/q1.actual.simplified.txt + * actual explain: /path/to/tmp/q1.actual.explain.txt + * [side by side plan diff] + * The explain files are saved to help debug later, they are not checked. Only the simplified + * plans are checked (by string comparison). + * + * Approving new plans: + * IF the plan change is intended then re-running the test + * with environ var SPARK_GENERATE_GOLDEN_FILES=1 will make the new plan canon. + * This should be done only for the queries that need it, to avoid unnecessary diffs in the + * other approved plans. + * This can be done by running sbt test-only *PlanStabilitySuite* -- -z "(q31)" + * The new plan files should be part of the PR and reviewed. + * + * Multiple approved plans: + * It's possible that a query has multiple correct plans. This should be decided as part of the + * review. In this case, change the call to approvePlans and set variant=true. + */ +trait PlanStabilitySuite extends TPCDSBase with DisableAdaptiveExecutionSuite { + + private val regenerateGoldenFiles: Boolean = System.getenv("SPARK_GENERATE_GOLDEN_FILES") == "1" + + protected val baseResourcePath = { + // If regenerateGoldenFiles is true, we must be running this in SBT and we use hard-coded + // relative path. Otherwise, we use classloader's getResource to find the location. + if (regenerateGoldenFiles) { + java.nio.file.Paths.get("src", "test", "resources", "tpcds-plan-stability").toFile + } else { + val res = getClass.getClassLoader.getResource("tpcds-plan-stability") + new File(res.getFile) + } + } + + def goldenFilePath: String + + private def getExistingVariants(name: String): Array[Int] = { + val dir = new File(goldenFilePath, name) + // file paths are the form ../q3/2.simplified.txt + val rgx = """(\d+).simplified.txt""".r + + dir.listFiles().filter(_.getName.contains("simplified")).map { file => + val rgx(numStr) = file.getName + numStr.toInt + } + } + + private def getNextVariantNumber(name: String): Int = { + val existingVariants = getExistingVariants(name) + if (existingVariants.isEmpty) { + 0 + } else { + existingVariants.max + 1 + } + } + + private def getDirForTest(name: String): File = { + new File(goldenFilePath, name) + } + + private def isApproved(name: String, plan: SparkPlan): Boolean = { + val dir = getDirForTest(name) + if (!dir.exists()) { + false + } else { + val existingVariants = getExistingVariants(name) + val actual = getSimplifiedPlan(plan) + existingVariants.exists { variant => + val file = new File(dir, s"$variant.simplified.txt") + val approved = FileUtils.readFileToString(file) + approved == actual + } + } + } + + /** + * Serialize and save this SparkPlan. + * The resulting file is used by [[checkWithApproved]] to check stability. + * + * @param plan the [[SparkPlan]] + * @param name the name of the query + * @param variant if false, this plan will become the only approved plan, otherwise + * it will be added as an additional approved plan. + * @param explain the full explain output; this is saved to help debug later as the simplified + * plan is not too useful for debugging + */ + private def approvePlan( + plan: SparkPlan, + name: String, + variant: Boolean, + explain: String): Unit = { + val foundMatch = isApproved(name, plan) + val dir = getDirForTest(name) + + if (!foundMatch) { + if (!variant) { + FileUtils.deleteDirectory(dir) + assert(dir.mkdirs()) + } + val simplified = getSimplifiedPlan(plan) + val nextVariant = getNextVariantNumber(name) + val file = new File(dir, s"$nextVariant.simplified.txt") + FileUtils.writeStringToFile(file, simplified) Review comment: Nit: This is deprecated as well ########## File path: sql/core/src/test/scala/org/apache/spark/sql/PlanStabilitySuite.scala ########## @@ -0,0 +1,302 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.spark.sql + +import java.io.File + +import scala.collection.mutable + +import org.apache.commons.io.FileUtils + +import org.apache.spark.sql.catalyst.expressions.AttributeSet +import org.apache.spark.sql.catalyst.util._ +import org.apache.spark.sql.execution._ +import org.apache.spark.sql.execution.adaptive.DisableAdaptiveExecutionSuite +import org.apache.spark.sql.execution.exchange.{Exchange, ReusedExchangeExec} +import org.apache.spark.sql.internal.SQLConf + +/** + * Check that TPCDS SparkPlans don't change. + * If there is a regression, the error message looks like this: + * Plans did not match: + * last approved plan: /path/to/tpcds-plan-stability/approved-plans-xxx/q1/0.simplified.txt + * last explain: /path/to/tpcds-plan-stability/approved-plans-xxx/q1/0.explain.txt + * actual plan: /path/to/tmp/q1.actual.simplified.txt + * actual explain: /path/to/tmp/q1.actual.explain.txt + * [side by side plan diff] + * The explain files are saved to help debug later, they are not checked. Only the simplified + * plans are checked (by string comparison). + * + * Approving new plans: + * IF the plan change is intended then re-running the test + * with environ var SPARK_GENERATE_GOLDEN_FILES=1 will make the new plan canon. + * This should be done only for the queries that need it, to avoid unnecessary diffs in the + * other approved plans. + * This can be done by running sbt test-only *PlanStabilitySuite* -- -z "(q31)" + * The new plan files should be part of the PR and reviewed. + * + * Multiple approved plans: + * It's possible that a query has multiple correct plans. This should be decided as part of the + * review. In this case, change the call to approvePlans and set variant=true. + */ +trait PlanStabilitySuite extends TPCDSBase with DisableAdaptiveExecutionSuite { + + private val regenerateGoldenFiles: Boolean = System.getenv("SPARK_GENERATE_GOLDEN_FILES") == "1" + + protected val baseResourcePath = { + // If regenerateGoldenFiles is true, we must be running this in SBT and we use hard-coded + // relative path. Otherwise, we use classloader's getResource to find the location. + if (regenerateGoldenFiles) { + java.nio.file.Paths.get("src", "test", "resources", "tpcds-plan-stability").toFile + } else { + val res = getClass.getClassLoader.getResource("tpcds-plan-stability") + new File(res.getFile) + } + } + + def goldenFilePath: String + + private def getExistingVariants(name: String): Array[Int] = { + val dir = new File(goldenFilePath, name) + // file paths are the form ../q3/2.simplified.txt + val rgx = """(\d+).simplified.txt""".r + + dir.listFiles().filter(_.getName.contains("simplified")).map { file => + val rgx(numStr) = file.getName + numStr.toInt + } + } + + private def getNextVariantNumber(name: String): Int = { + val existingVariants = getExistingVariants(name) + if (existingVariants.isEmpty) { + 0 + } else { + existingVariants.max + 1 + } + } + + private def getDirForTest(name: String): File = { + new File(goldenFilePath, name) + } + + private def isApproved(name: String, plan: SparkPlan): Boolean = { + val dir = getDirForTest(name) + if (!dir.exists()) { + false + } else { + val existingVariants = getExistingVariants(name) + val actual = getSimplifiedPlan(plan) + existingVariants.exists { variant => + val file = new File(dir, s"$variant.simplified.txt") + val approved = FileUtils.readFileToString(file) + approved == actual + } + } + } + + /** + * Serialize and save this SparkPlan. + * The resulting file is used by [[checkWithApproved]] to check stability. + * + * @param plan the [[SparkPlan]] + * @param name the name of the query + * @param variant if false, this plan will become the only approved plan, otherwise + * it will be added as an additional approved plan. + * @param explain the full explain output; this is saved to help debug later as the simplified + * plan is not too useful for debugging + */ + private def approvePlan( + plan: SparkPlan, + name: String, + variant: Boolean, + explain: String): Unit = { + val foundMatch = isApproved(name, plan) + val dir = getDirForTest(name) + + if (!foundMatch) { + if (!variant) { + FileUtils.deleteDirectory(dir) + assert(dir.mkdirs()) + } + val simplified = getSimplifiedPlan(plan) + val nextVariant = getNextVariantNumber(name) + val file = new File(dir, s"$nextVariant.simplified.txt") + FileUtils.writeStringToFile(file, simplified) + val fileOriginalPlan = new File(dir, s"$nextVariant.explain.txt") + FileUtils.writeStringToFile(fileOriginalPlan, explain) + logWarning(s"APPROVED: ${file} ${fileOriginalPlan}") Review comment: Nit: use logInfo? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
