markj-db commented on code in PR #56216: URL: https://github.com/apache/spark/pull/56216#discussion_r3660617764
########## sql/core/src/test/scala/org/apache/spark/sql/execution/ExplainUtilsSuite.scala: ########## @@ -0,0 +1,84 @@ +/* + * 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.execution + +import org.apache.spark.sql.QueryTest +import org.apache.spark.sql.catalyst.plans.QueryPlan +import org.apache.spark.sql.catalyst.util.StringConcat +import org.apache.spark.sql.test.SharedSparkSession + +/** + * Tests for [[ExplainUtils.processPlan]]: operator ID assignment, WholeStageCodegen tag + * propagation, thread-local lifecycle, and subquery handling. + */ +class ExplainUtilsSuite extends QueryTest with SharedSparkSession { + + private def explainOutput(plan: SparkPlan): String = { + val concat = new StringConcat() + ExplainUtils.processPlan(plan, concat.append) + concat.toString + } + + test("processPlan assigns unique operator IDs to all visible plan nodes") { + val df = spark.range(100).filter("id > 10").select("id") + val output = explainOutput(df.queryExecution.executedPlan) + // Each operator ID appears both in the tree header ("Filter (2)") and as the header of its + // verbose section ("(2) Filter"). Anchor to the verbose-section headers, which are the only + // lines that begin with "(N)", so each operator contributes exactly one ID. + val ids = "(?m)^\\((\\d+)\\)".r.findAllMatchIn(output).map(_.group(1).toInt).toSeq + assert(ids.nonEmpty, "processPlan should assign at least one operator ID") + assert(ids == ids.distinct, s"processPlan operator IDs should be unique: $ids") + } + + test("processPlan sets WholeStageCodegen tags on plan nodes") { + withSQLConf("spark.sql.codegen.wholeStage" -> "true") { + val df = spark.range(10).filter("id > 3") + val plan = df.queryExecution.executedPlan + ExplainUtils.processPlan(plan, _ => ()) + // CODEGEN_ID_TAG is assigned only to nodes under a WholeStageCodegenExec (see Review Comment: Good catch on both. Reworked in a6730c1514a: the test now builds a `WholeStageCodegenExec(RangeExec(...))(codegenStageId = 7)` directly and asserts `processPlan` stamps the child with `CODEGEN_ID_TAG == 7`. This drops the planner dependency and the `nonEmpty` guard, so the assertion always runs and pins the concrete tagging behavior instead of the weaker structural claim. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
