diff --git a/dev/diffs/3.5.9.diff b/dev/diffs/3.5.9.diff index c41291d25d5..398566bb00b 100644 --- a/dev/diffs/3.5.9.diff +++ b/dev/diffs/3.5.9.diff @@ -1703,13 +1703,58 @@ index 418ca3430bb..eb8267192f8 100644 withTempPath { path => val dir = path.getCanonicalPath diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/LogicalPlanTagInSparkPlanSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/LogicalPlanTagInSparkPlanSuite.scala -index 743ec41dbe7..9f30d6c8e04 100644 +index 743ec41dbe7..1ffb98e1f7f 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/LogicalPlanTagInSparkPlanSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/LogicalPlanTagInSparkPlanSuite.scala -@@ -53,6 +53,10 @@ class LogicalPlanTagInSparkPlanSuite extends TPCDSQuerySuite with DisableAdaptiv - case ColumnarToRowExec(i: InputAdapter) => isScanPlanTree(i.child) +@@ -22,6 +22,8 @@ import scala.reflect.ClassTag + import org.apache.spark.sql.TPCDSQuerySuite + import org.apache.spark.sql.catalyst.expressions.aggregate.{AggregateExpression, Complete, Final} + import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, Generate, Join, LocalRelation, LogicalPlan, Range, Sample, Union, Window, WithCTE} ++import org.apache.spark.sql.catalyst.util.resourceToString ++import org.apache.spark.sql.comet.{CometFilterExec, CometProjectExec} + import org.apache.spark.sql.execution.adaptive.DisableAdaptiveExecutionSuite + import org.apache.spark.sql.execution.aggregate.{HashAggregateExec, ObjectHashAggregateExec, SortAggregateExec} + import org.apache.spark.sql.execution.columnar.{InMemoryRelation, InMemoryTableScanExec} +@@ -30,10 +32,19 @@ import org.apache.spark.sql.execution.datasources.v2.{BatchScanExec, DataSourceV + import org.apache.spark.sql.execution.exchange.{BroadcastExchangeExec, ReusedExchangeExec, ShuffleExchangeExec} + import org.apache.spark.sql.execution.joins._ + import org.apache.spark.sql.execution.window.WindowExec ++import org.apache.spark.sql.internal.SQLConf + + // Disable AQE because AdaptiveSparkPlanExec does not have a logical plan link + class LogicalPlanTagInSparkPlanSuite extends TPCDSQuerySuite with DisableAdaptiveExecutionSuite { + ++ test("q9 logical plan tags without code generation") { ++ val queryString = resourceToString("tpcds/q9.sql", ++ classLoader = Thread.currentThread().getContextClassLoader) ++ withSQLConf(SQLConf.CODEGEN_FACTORY_MODE.key -> "NO_CODEGEN") { ++ checkLogicalPlanTag(sql(queryString).queryExecution.executedPlan) ++ } ++ } ++ + override protected def checkGeneratedCode( + plan: SparkPlan, checkMethodCodeSize: Boolean = true): Unit = { + super.checkGeneratedCode(plan, checkMethodCodeSize) +@@ -46,13 +57,25 @@ class LogicalPlanTagInSparkPlanSuite extends TPCDSQuerySuite with DisableAdaptiv + aggExprs.nonEmpty && aggExprs.forall(ae => ae.mode == Complete || ae.mode == Final) + } + ++ private def transitionInput(transition: ColumnarToRowTransition): SparkPlan = ++ transition.child match { ++ case InputAdapter(child) => child ++ case child => child ++ } ++ + // A scan plan tree is a plan tree that has a leaf node under zero or more Project/Filter nodes. + // We may add `ColumnarToRowExec` and `InputAdapter` above the scan node after planning. + @scala.annotation.tailrec + private def isScanPlanTree(plan: SparkPlan): Boolean = plan match { +- case ColumnarToRowExec(i: InputAdapter) => isScanPlanTree(i.child) ++ case t: ColumnarToRowTransition => isScanPlanTree(transitionInput(t)) case p: ProjectExec => isScanPlanTree(p.child) ++ case p: CometProjectExec => isScanPlanTree(p.child) case f: FilterExec => isScanPlanTree(f.child) ++ case f: CometFilterExec => isScanPlanTree(f.child) + // Comet produces scan plan tree like: + // ColumnarToRow + // +- ReusedExchange @@ -1717,6 +1762,15 @@ index 743ec41dbe7..9f30d6c8e04 100644 case _: LeafExecNode => true case _ => false } +@@ -97,7 +120,7 @@ class LogicalPlanTagInSparkPlanSuite extends TPCDSQuerySuite with DisableAdaptiv + // `ColumnarToRowExec` and `InputAdapter` are added outside of the planner, which doesn't + // have the logical plan tag. + val actualPlan = plan match { +- case ColumnarToRowExec(i: InputAdapter) => i.child ++ case t: ColumnarToRowTransition => transitionInput(t) + case _ => plan + } + diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/PlannerSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/PlannerSuite.scala index de24b8c82b0..1f835481290 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/PlannerSuite.scala diff --git a/dev/diffs/4.0.4.diff b/dev/diffs/4.0.4.diff index 772ffbcdadb..4782566903d 100644 --- a/dev/diffs/4.0.4.diff +++ b/dev/diffs/4.0.4.diff @@ -2126,13 +2126,58 @@ index d1b11a74cf3..75e4600863a 100644 }.isDefined } diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/LogicalPlanTagInSparkPlanSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/LogicalPlanTagInSparkPlanSuite.scala -index 743ec41dbe7..9f30d6c8e04 100644 +index 743ec41dbe7..1ffb98e1f7f 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/LogicalPlanTagInSparkPlanSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/LogicalPlanTagInSparkPlanSuite.scala -@@ -53,6 +53,10 @@ class LogicalPlanTagInSparkPlanSuite extends TPCDSQuerySuite with DisableAdaptiv - case ColumnarToRowExec(i: InputAdapter) => isScanPlanTree(i.child) +@@ -22,6 +22,8 @@ import scala.reflect.ClassTag + import org.apache.spark.sql.TPCDSQuerySuite + import org.apache.spark.sql.catalyst.expressions.aggregate.{AggregateExpression, Complete, Final} + import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, Generate, Join, LocalRelation, LogicalPlan, Range, Sample, Union, Window, WithCTE} ++import org.apache.spark.sql.catalyst.util.resourceToString ++import org.apache.spark.sql.comet.{CometFilterExec, CometProjectExec} + import org.apache.spark.sql.execution.adaptive.DisableAdaptiveExecutionSuite + import org.apache.spark.sql.execution.aggregate.{HashAggregateExec, ObjectHashAggregateExec, SortAggregateExec} + import org.apache.spark.sql.execution.columnar.{InMemoryRelation, InMemoryTableScanExec} +@@ -30,10 +32,19 @@ import org.apache.spark.sql.execution.datasources.v2.{BatchScanExec, DataSourceV + import org.apache.spark.sql.execution.exchange.{BroadcastExchangeExec, ReusedExchangeExec, ShuffleExchangeExec} + import org.apache.spark.sql.execution.joins._ + import org.apache.spark.sql.execution.window.WindowExec ++import org.apache.spark.sql.internal.SQLConf + + // Disable AQE because AdaptiveSparkPlanExec does not have a logical plan link + class LogicalPlanTagInSparkPlanSuite extends TPCDSQuerySuite with DisableAdaptiveExecutionSuite { + ++ test("q9 logical plan tags without code generation") { ++ val queryString = resourceToString("tpcds/q9.sql", ++ classLoader = Thread.currentThread().getContextClassLoader) ++ withSQLConf(SQLConf.CODEGEN_FACTORY_MODE.key -> "NO_CODEGEN") { ++ checkLogicalPlanTag(sql(queryString).queryExecution.executedPlan) ++ } ++ } ++ + override protected def checkGeneratedCode( + plan: SparkPlan, checkMethodCodeSize: Boolean = true): Unit = { + super.checkGeneratedCode(plan, checkMethodCodeSize) +@@ -46,13 +57,25 @@ class LogicalPlanTagInSparkPlanSuite extends TPCDSQuerySuite with DisableAdaptiv + aggExprs.nonEmpty && aggExprs.forall(ae => ae.mode == Complete || ae.mode == Final) + } + ++ private def transitionInput(transition: ColumnarToRowTransition): SparkPlan = ++ transition.child match { ++ case InputAdapter(child) => child ++ case child => child ++ } ++ + // A scan plan tree is a plan tree that has a leaf node under zero or more Project/Filter nodes. + // We may add `ColumnarToRowExec` and `InputAdapter` above the scan node after planning. + @scala.annotation.tailrec + private def isScanPlanTree(plan: SparkPlan): Boolean = plan match { +- case ColumnarToRowExec(i: InputAdapter) => isScanPlanTree(i.child) ++ case t: ColumnarToRowTransition => isScanPlanTree(transitionInput(t)) case p: ProjectExec => isScanPlanTree(p.child) ++ case p: CometProjectExec => isScanPlanTree(p.child) case f: FilterExec => isScanPlanTree(f.child) ++ case f: CometFilterExec => isScanPlanTree(f.child) + // Comet produces scan plan tree like: + // ColumnarToRow + // +- ReusedExchange @@ -2140,6 +2185,15 @@ index 743ec41dbe7..9f30d6c8e04 100644 case _: LeafExecNode => true case _ => false } +@@ -97,7 +120,7 @@ class LogicalPlanTagInSparkPlanSuite extends TPCDSQuerySuite with DisableAdaptiv + // `ColumnarToRowExec` and `InputAdapter` are added outside of the planner, which doesn't + // have the logical plan tag. + val actualPlan = plan match { +- case ColumnarToRowExec(i: InputAdapter) => i.child ++ case t: ColumnarToRowTransition => transitionInput(t) + case _ => plan + } + diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/PlannerSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/PlannerSuite.scala index 1400ee25f43..5b016c3f9c5 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/PlannerSuite.scala diff --git a/dev/diffs/4.1.3.diff b/dev/diffs/4.1.3.diff index daf53c6773c..cc0bbba9f59 100644 --- a/dev/diffs/4.1.3.diff +++ b/dev/diffs/4.1.3.diff @@ -2297,13 +2297,58 @@ index d1b11a74cf3..75e4600863a 100644 }.isDefined } diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/LogicalPlanTagInSparkPlanSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/LogicalPlanTagInSparkPlanSuite.scala -index 743ec41dbe7..9f30d6c8e04 100644 +index 743ec41dbe7..1ffb98e1f7f 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/LogicalPlanTagInSparkPlanSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/LogicalPlanTagInSparkPlanSuite.scala -@@ -53,6 +53,10 @@ class LogicalPlanTagInSparkPlanSuite extends TPCDSQuerySuite with DisableAdaptiv - case ColumnarToRowExec(i: InputAdapter) => isScanPlanTree(i.child) +@@ -22,6 +22,8 @@ import scala.reflect.ClassTag + import org.apache.spark.sql.TPCDSQuerySuite + import org.apache.spark.sql.catalyst.expressions.aggregate.{AggregateExpression, Complete, Final} + import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, Generate, Join, LocalRelation, LogicalPlan, Range, Sample, Union, Window, WithCTE} ++import org.apache.spark.sql.catalyst.util.resourceToString ++import org.apache.spark.sql.comet.{CometFilterExec, CometProjectExec} + import org.apache.spark.sql.execution.adaptive.DisableAdaptiveExecutionSuite + import org.apache.spark.sql.execution.aggregate.{HashAggregateExec, ObjectHashAggregateExec, SortAggregateExec} + import org.apache.spark.sql.execution.columnar.{InMemoryRelation, InMemoryTableScanExec} +@@ -30,10 +32,19 @@ import org.apache.spark.sql.execution.datasources.v2.{BatchScanExec, DataSourceV + import org.apache.spark.sql.execution.exchange.{BroadcastExchangeExec, ReusedExchangeExec, ShuffleExchangeExec} + import org.apache.spark.sql.execution.joins._ + import org.apache.spark.sql.execution.window.WindowExec ++import org.apache.spark.sql.internal.SQLConf + + // Disable AQE because AdaptiveSparkPlanExec does not have a logical plan link + class LogicalPlanTagInSparkPlanSuite extends TPCDSQuerySuite with DisableAdaptiveExecutionSuite { + ++ test("q9 logical plan tags without code generation") { ++ val queryString = resourceToString("tpcds/q9.sql", ++ classLoader = Thread.currentThread().getContextClassLoader) ++ withSQLConf(SQLConf.CODEGEN_FACTORY_MODE.key -> "NO_CODEGEN") { ++ checkLogicalPlanTag(sql(queryString).queryExecution.executedPlan) ++ } ++ } ++ + override protected def checkGeneratedCode( + plan: SparkPlan, checkMethodCodeSize: Boolean = true): Unit = { + super.checkGeneratedCode(plan, checkMethodCodeSize) +@@ -46,13 +57,25 @@ class LogicalPlanTagInSparkPlanSuite extends TPCDSQuerySuite with DisableAdaptiv + aggExprs.nonEmpty && aggExprs.forall(ae => ae.mode == Complete || ae.mode == Final) + } + ++ private def transitionInput(transition: ColumnarToRowTransition): SparkPlan = ++ transition.child match { ++ case InputAdapter(child) => child ++ case child => child ++ } ++ + // A scan plan tree is a plan tree that has a leaf node under zero or more Project/Filter nodes. + // We may add `ColumnarToRowExec` and `InputAdapter` above the scan node after planning. + @scala.annotation.tailrec + private def isScanPlanTree(plan: SparkPlan): Boolean = plan match { +- case ColumnarToRowExec(i: InputAdapter) => isScanPlanTree(i.child) ++ case t: ColumnarToRowTransition => isScanPlanTree(transitionInput(t)) case p: ProjectExec => isScanPlanTree(p.child) ++ case p: CometProjectExec => isScanPlanTree(p.child) case f: FilterExec => isScanPlanTree(f.child) ++ case f: CometFilterExec => isScanPlanTree(f.child) + // Comet produces scan plan tree like: + // ColumnarToRow + // +- ReusedExchange @@ -2311,6 +2356,15 @@ index 743ec41dbe7..9f30d6c8e04 100644 case _: LeafExecNode => true case _ => false } +@@ -97,7 +120,7 @@ class LogicalPlanTagInSparkPlanSuite extends TPCDSQuerySuite with DisableAdaptiv + // `ColumnarToRowExec` and `InputAdapter` are added outside of the planner, which doesn't + // have the logical plan tag. + val actualPlan = plan match { +- case ColumnarToRowExec(i: InputAdapter) => i.child ++ case t: ColumnarToRowTransition => transitionInput(t) + case _ => plan + } + diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/PlannerSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/PlannerSuite.scala index b926cc192bd..77510ccdb09 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/PlannerSuite.scala diff --git a/dev/diffs/4.2.0.diff b/dev/diffs/4.2.0.diff index 40aad4b1d59..ac3d17b51de 100644 --- a/dev/diffs/4.2.0.diff +++ b/dev/diffs/4.2.0.diff @@ -2353,13 +2353,58 @@ index faf0469ec20..dfed25d87fe 100644 }.isDefined } diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/LogicalPlanTagInSparkPlanSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/LogicalPlanTagInSparkPlanSuite.scala -index 743ec41dbe7..9f30d6c8e04 100644 +index 743ec41dbe7..1ffb98e1f7f 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/LogicalPlanTagInSparkPlanSuite.scala +++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/LogicalPlanTagInSparkPlanSuite.scala -@@ -53,6 +53,10 @@ class LogicalPlanTagInSparkPlanSuite extends TPCDSQuerySuite with DisableAdaptiv - case ColumnarToRowExec(i: InputAdapter) => isScanPlanTree(i.child) +@@ -22,6 +22,8 @@ import scala.reflect.ClassTag + import org.apache.spark.sql.TPCDSQuerySuite + import org.apache.spark.sql.catalyst.expressions.aggregate.{AggregateExpression, Complete, Final} + import org.apache.spark.sql.catalyst.plans.logical.{Aggregate, Generate, Join, LocalRelation, LogicalPlan, Range, Sample, Union, Window, WithCTE} ++import org.apache.spark.sql.catalyst.util.resourceToString ++import org.apache.spark.sql.comet.{CometFilterExec, CometProjectExec} + import org.apache.spark.sql.execution.adaptive.DisableAdaptiveExecutionSuite + import org.apache.spark.sql.execution.aggregate.{HashAggregateExec, ObjectHashAggregateExec, SortAggregateExec} + import org.apache.spark.sql.execution.columnar.{InMemoryRelation, InMemoryTableScanExec} +@@ -30,10 +32,19 @@ import org.apache.spark.sql.execution.datasources.v2.{BatchScanExec, DataSourceV + import org.apache.spark.sql.execution.exchange.{BroadcastExchangeExec, ReusedExchangeExec, ShuffleExchangeExec} + import org.apache.spark.sql.execution.joins._ + import org.apache.spark.sql.execution.window.WindowExec ++import org.apache.spark.sql.internal.SQLConf + + // Disable AQE because AdaptiveSparkPlanExec does not have a logical plan link + class LogicalPlanTagInSparkPlanSuite extends TPCDSQuerySuite with DisableAdaptiveExecutionSuite { + ++ test("q9 logical plan tags without code generation") { ++ val queryString = resourceToString("tpcds/q9.sql", ++ classLoader = Thread.currentThread().getContextClassLoader) ++ withSQLConf(SQLConf.CODEGEN_FACTORY_MODE.key -> "NO_CODEGEN") { ++ checkLogicalPlanTag(sql(queryString).queryExecution.executedPlan) ++ } ++ } ++ + override protected def checkGeneratedCode( + plan: SparkPlan, checkMethodCodeSize: Boolean = true): Unit = { + super.checkGeneratedCode(plan, checkMethodCodeSize) +@@ -46,13 +57,25 @@ class LogicalPlanTagInSparkPlanSuite extends TPCDSQuerySuite with DisableAdaptiv + aggExprs.nonEmpty && aggExprs.forall(ae => ae.mode == Complete || ae.mode == Final) + } + ++ private def transitionInput(transition: ColumnarToRowTransition): SparkPlan = ++ transition.child match { ++ case InputAdapter(child) => child ++ case child => child ++ } ++ + // A scan plan tree is a plan tree that has a leaf node under zero or more Project/Filter nodes. + // We may add `ColumnarToRowExec` and `InputAdapter` above the scan node after planning. + @scala.annotation.tailrec + private def isScanPlanTree(plan: SparkPlan): Boolean = plan match { +- case ColumnarToRowExec(i: InputAdapter) => isScanPlanTree(i.child) ++ case t: ColumnarToRowTransition => isScanPlanTree(transitionInput(t)) case p: ProjectExec => isScanPlanTree(p.child) ++ case p: CometProjectExec => isScanPlanTree(p.child) case f: FilterExec => isScanPlanTree(f.child) ++ case f: CometFilterExec => isScanPlanTree(f.child) + // Comet produces scan plan tree like: + // ColumnarToRow + // +- ReusedExchange @@ -2367,6 +2412,15 @@ index 743ec41dbe7..9f30d6c8e04 100644 case _: LeafExecNode => true case _ => false } +@@ -97,7 +120,7 @@ class LogicalPlanTagInSparkPlanSuite extends TPCDSQuerySuite with DisableAdaptiv + // `ColumnarToRowExec` and `InputAdapter` are added outside of the planner, which doesn't + // have the logical plan tag. + val actualPlan = plan match { +- case ColumnarToRowExec(i: InputAdapter) => i.child ++ case t: ColumnarToRowTransition => transitionInput(t) + case _ => plan + } + diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/PlannerSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/PlannerSuite.scala index 7c434ebee5f..ac18158100d 100644 --- a/sql/core/src/test/scala/org/apache/spark/sql/execution/PlannerSuite.scala diff --git a/docs/source/contributor-guide/adding_a_new_operator.md b/docs/source/contributor-guide/adding_a_new_operator.md index 5414bbe83db..c524aa28271 100644 --- a/docs/source/contributor-guide/adding_a_new_operator.md +++ b/docs/source/contributor-guide/adding_a_new_operator.md @@ -833,10 +833,16 @@ val protoExpr = exprToProto(sparkExpr, inputSchema) ### Restoring the Spark operator (`sparkFallback`) -`CometExec.originalPlan` is the Spark operator this node replaced. `CometExecRule` copies -`originalPlan.logicalLink` onto the Comet node, which is how AQE finds the node again when it -re-plans a stage. `RevertNativeForTransitionHeavyStages` calls `sparkFallback(newChildren)` to -rebuild that Spark operator with the children of the reverted stage. +`CometExec.originalPlan` is the Spark operator this node replaced. When adaptive query execution +(AQE) reuses a `CometExec`, `CometExecRule` preserves an existing direct link to a +`LogicalQueryStage`. It also preserves an inherited stage link if the stage wraps the exact +logical node referenced by `originalPlan.logicalLink`, even through nested stages. This lets AQE +find the current stage by identity during replanning. In all other cases, the rule restores the +direct and inherited tags separately on the Comet node itself, clearing each tag missing from +`originalPlan`. + +`RevertNativeForTransitionHeavyStages` calls `sparkFallback(newChildren)` to rebuild the original +Spark operator with the children of the reverted stage. The default implementation is `originalPlan.withNewChildren(newChildren)`. It refuses a null `originalPlan`, an `originalPlan` that is one of the node's own children, or a different number of diff --git a/spark/src/main/scala/org/apache/comet/rules/CometExecRule.scala b/spark/src/main/scala/org/apache/comet/rules/CometExecRule.scala index 870e7d66f63..bb96a1ad4e5 100644 --- a/spark/src/main/scala/org/apache/comet/rules/CometExecRule.scala +++ b/spark/src/main/scala/org/apache/comet/rules/CometExecRule.scala @@ -21,6 +21,7 @@ package org.apache.comet.rules import java.util.concurrent.ConcurrentHashMap +import scala.annotation.tailrec import scala.collection.mutable.ListBuffer import scala.jdk.CollectionConverters._ @@ -28,6 +29,7 @@ import org.apache.spark.sql.SparkSession import org.apache.spark.sql.catalyst.expressions.{Alias, Attribute, Expression, SortOrder} import org.apache.spark.sql.catalyst.expressions.aggregate.{AggregateMode, Final, Partial, PartialMerge} import org.apache.spark.sql.catalyst.optimizer.{BuildLeft, BuildRight, BuildSide} +import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan import org.apache.spark.sql.catalyst.rules.Rule import org.apache.spark.sql.catalyst.trees.TreeNodeTag import org.apache.spark.sql.catalyst.util.sideBySide @@ -60,7 +62,7 @@ import org.apache.spark.sql.types._ import org.apache.comet.{CometConf, CometExplainInfo, ConfigEntry, ExtendedExplainInfo} import org.apache.comet.CometConf.{COMET_SPARK_TO_ARROW_ENABLED, COMET_SPARK_TO_ARROW_SUPPORTED_OPERATOR_LIST} import org.apache.comet.CometSparkSessionExtensions._ -import org.apache.comet.rules.CometExecRule.allExecs +import org.apache.comet.rules.CometExecRule.{allExecs, copyLogicalLinks} import org.apache.comet.serde._ import org.apache.comet.serde.OperatorOuterClass.Operator import org.apache.comet.serde.operator._ @@ -68,6 +70,21 @@ import org.apache.comet.shims.{CometTypeShim, ShimCometMergeRows, ShimCometStrea object CometExecRule { + // setLogicalLink promotes an inherited link to a direct link and also rewrites descendants. + private[rules] def copyLogicalLinks( + from: SparkPlan, + to: SparkPlan, + clearMissing: Boolean): Unit = { + if (from ne to) { + def copyTag(tag: TreeNodeTag[LogicalPlan]): Unit = from.getTagValue(tag) match { + case Some(plan) => to.setTagValue(tag, plan) + case None => if (clearMissing) to.unsetTagValue(tag) + } + copyTag(SparkPlan.LOGICAL_PLAN_TAG) + copyTag(SparkPlan.LOGICAL_PLAN_INHERITED_TAG) + } + } + /** * Whether `scan` reads a relation stored in Comet's cache format. Comet's serializer stores * that format only for schemas it supports and delegates everything else to Spark's default @@ -768,9 +785,7 @@ case class CometExecRule(session: SparkSession, queryStagePrep: Boolean = false) } yield { // AQE can link the current projection to a materialized logical query stage. // Its originalPlan may have an older link; preserve the current direct/inherited tags. - Seq(SparkPlan.LOGICAL_PLAN_TAG, SparkPlan.LOGICAL_PLAN_INHERITED_TAG).foreach { tag => - project.getTagValue(tag).foreach(rebuilt.setTagValue(tag, _)) - } + copyLogicalLinks(project, rebuilt, clearMissing = false) rebuilt } } @@ -1018,6 +1033,14 @@ case class CometExecRule(session: SparkSession, queryStagePrep: Boolean = false) case _ => } + @tailrec + private def stageWraps(stage: LogicalQueryStage, original: LogicalPlan): Boolean = + stage.logicalPlan match { + case plan if plan eq original => true + case nested: LogicalQueryStage => stageWraps(nested, original) + case _ => false + } + override def apply(plan: SparkPlan): SparkPlan = { val newPlan = _apply(plan) if (showTransformations && !newPlan.fastEquals(plan)) { @@ -1168,52 +1191,33 @@ case class CometExecRule(session: SparkSession, queryStagePrep: Boolean = false) // Set up logical links newPlan = newPlan.transform { - case op: CometExec - if op - .getTagValue(SparkPlan.LOGICAL_PLAN_TAG) - .exists(_.isInstanceOf[LogicalQueryStage]) => - // AQE replanning reuses this physical root and links it to the current logical stage. - // originalPlan can still point to a subtree hidden inside that logical leaf, which - // AQE cannot replace in the current logical plan. Only preserve a direct stage link, - // not a link inherited from an ancestor. - // On the ordinary exchange path, the exchange itself is behind a QueryStageExec - // leaf and is not visited by this transform. - // Spark 4.1.3 returns the existing root in LogicalQueryStageStrategy and then calls - // setLogicalLink from SparkStrategies.plan: + case op: CometExec => + // Spark reuses the physical root and links it directly to the current AQE stage. + // A descendant can inherit that stage if it wraps the descendant's original node. + // AQE replaces the linked node by identity. A stale link leaves the plan unchanged. // scalastyle:off line.size.limit // https://github.com/apache/spark/blob/v4.1.3/sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/LogicalQueryStageStrategy.scala#L64-L65 // https://github.com/apache/spark/blob/v4.1.3/sql/core/src/main/scala/org/apache/spark/sql/execution/SparkStrategies.scala#L78-L87 + // https://github.com/apache/spark/blob/v4.1.3/sql/core/src/main/scala/org/apache/spark/sql/execution/adaptive/AdaptiveSparkPlanExec.scala#L778-L783 // scalastyle:on line.size.limit - op - case op: CometExec => - if (op.originalPlan.logicalLink.isEmpty) { - op.unsetTagValue(SparkPlan.LOGICAL_PLAN_TAG) - op.unsetTagValue(SparkPlan.LOGICAL_PLAN_INHERITED_TAG) - } else { - op.originalPlan.logicalLink.foreach(op.setLogicalLink) + val keepStage = op.logicalLink.exists { + case stage: LogicalQueryStage => + // logicalLink prefers the direct tag, so if present it points to this stage. + op.getTagValue(SparkPlan.LOGICAL_PLAN_TAG).isDefined || + op.originalPlan.logicalLink.exists(stageWraps(stage, _)) + case _ => false + } + if (!keepStage) { + copyLogicalLinks(op.originalPlan, op, clearMissing = true) } op case op: CometShuffleExchangeExec => - // Original Spark shuffle exchange operator might have empty logical link. - // But the `setLogicalLink` call above on downstream operator of - // `CometShuffleExchangeExec` will set its logical link to the downstream - // operators which cause AQE behavior to be incorrect. So we need to unset - // the logical link here. - if (op.originalPlan.logicalLink.isEmpty) { - op.unsetTagValue(SparkPlan.LOGICAL_PLAN_TAG) - op.unsetTagValue(SparkPlan.LOGICAL_PLAN_INHERITED_TAG) - } else { - op.originalPlan.logicalLink.foreach(op.setLogicalLink) - } + // An exchange without an original logical link can inherit an unrelated one from + // its parent. Clear it so AQE does not associate the exchange with that parent. + copyLogicalLinks(op.originalPlan, op, clearMissing = true) op - case op: CometBroadcastExchangeExec => - if (op.originalPlan.logicalLink.isEmpty) { - op.unsetTagValue(SparkPlan.LOGICAL_PLAN_TAG) - op.unsetTagValue(SparkPlan.LOGICAL_PLAN_INHERITED_TAG) - } else { - op.originalPlan.logicalLink.foreach(op.setLogicalLink) - } + copyLogicalLinks(op.originalPlan, op, clearMissing = true) op } diff --git a/spark/src/main/scala/org/apache/comet/rules/MakeDateEvaluation.scala b/spark/src/main/scala/org/apache/comet/rules/MakeDateEvaluation.scala index 7811b893b2f..3bda365a560 100644 --- a/spark/src/main/scala/org/apache/comet/rules/MakeDateEvaluation.scala +++ b/spark/src/main/scala/org/apache/comet/rules/MakeDateEvaluation.scala @@ -164,16 +164,6 @@ private[rules] object MakeDateEvaluation { AttributeSet(children.flatMap(eagerReferences)) } - private def copyLogicalLinks(from: SparkPlan, to: SparkPlan): Unit = { - if (from ne to) { - // setLogicalLink promotes inherited links to direct links and rewrites descendants. - // Keep each tag's role and retain the fallback plan's links when the source has none. - Seq(SparkPlan.LOGICAL_PLAN_TAG, SparkPlan.LOGICAL_PLAN_INHERITED_TAG).foreach { tag => - from.getTagValue(tag).foreach(to.setTagValue(tag, _)) - } - } - } - private def nativeBroadcastInput(plan: SparkPlan): Option[SparkPlan] = plan match { case _: CometBroadcastExchangeExec => Some(plan) case stage: BroadcastQueryStageExec => nativeBroadcastInput(stage.plan).map(_ => stage) @@ -191,7 +181,7 @@ private[rules] object MakeDateEvaluation { // mode, including through AQE/reuse wrappers, without re-executing the build input. val restored = CometColumnarToRowExec(input) restored.setTagValue(sparkEvaluation, reason) - copyLogicalLinks(plan, restored) + CometExecRule.copyLogicalLinks(plan, restored, clearMissing = false) restored } .getOrElse(plan) @@ -453,7 +443,7 @@ private[rules] object MakeDateEvaluation { original.withNewChildren(children) case _ => node.withNewChildren(children) } - copyLogicalLinks(node, rebuilt) + CometExecRule.copyLogicalLinks(node, rebuilt, clearMissing = false) rebuilt.unsetTagValue(collectProjectionLimit) if (original.isInstanceOf[ProjectExec] && rowReason.contains(collectLimitReason) && operatorReason.isEmpty && childReason.isEmpty && diff --git a/spark/src/test/scala/org/apache/comet/exec/CometExecSuite.scala b/spark/src/test/scala/org/apache/comet/exec/CometExecSuite.scala index f9261bc1e27..ae3fd7ded9b 100644 --- a/spark/src/test/scala/org/apache/comet/exec/CometExecSuite.scala +++ b/spark/src/test/scala/org/apache/comet/exec/CometExecSuite.scala @@ -35,6 +35,7 @@ import org.apache.spark.sql.catalyst.catalog.{BucketSpec, CatalogStatistics, Cat import org.apache.spark.sql.catalyst.expressions.{DynamicPruningExpression, Expression, ExpressionInfo, Hex, Literal} import org.apache.spark.sql.catalyst.expressions.aggregate.{AggregateMode, BloomFilterAggregate, Final} import org.apache.spark.sql.catalyst.plans.logical.Expand +import org.apache.spark.sql.catalyst.plans.physical.SinglePartition import org.apache.spark.sql.comet._ import org.apache.spark.sql.comet.execution.shuffle.{CometColumnarShuffle, CometNativeShuffle, CometShuffleExchangeExec} import org.apache.spark.sql.connector.catalog.InMemoryTableCatalog @@ -2393,6 +2394,35 @@ class CometExecSuite extends CometTestBase { } } + test("AQE broadcasts a mixed distinct aggregate after its second shuffle") { + withSQLConf( + SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true", + SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1", + SQLConf.ADAPTIVE_AUTO_BROADCASTJOIN_THRESHOLD.key -> "16384", + SQLConf.SHUFFLE_PARTITIONS.key -> "4", + CometConf.COMET_SHUFFLE_MODE.key -> "native", + CometConf.COMET_CONVERT_FROM_RANGE_ENABLED.key -> "true") { + // Each (group, distinct value) is shuffled before the 16 final groups fall below 16 KiB. + val df = sql(""" + |WITH counts AS ( + | SELECT id % 16 AS k, count(*) AS n, count(DISTINCT id % 127) AS u + | FROM range(0, 8192, 1, 4) GROUP BY id % 16 + |) + |SELECT p.id, d.n, d.u + |FROM range(0, 16, 1, 4) p LEFT JOIN counts d ON p.id = d.k + |""".stripMargin) + val adaptive = df.queryExecution.executedPlan.asInstanceOf[AdaptiveSparkPlanExec] + assert(collect(adaptive.executedPlan) { case b: CometBroadcastHashJoinExec => b }.isEmpty) + + checkAnswer(df, (0L until 16).map(k => Row(k, 512L, 127L))) + + val finalPlan = adaptive.executedPlan + assert( + collect(finalPlan) { case b: CometBroadcastHashJoinExec => b }.size == 1, + finalPlan.treeString) + } + } + test("CometShuffleExchangeExec logical link should be correct") { withTempView("v") { spark.sparkContext @@ -2417,6 +2447,24 @@ class CometExecSuite extends CometTestBase { } } + test("Comet shuffle inherits the logical sort below a nested ordered limit") { + assume(isSpark40Plus, "InsertSortForLimitAndOffset requires Spark 4.0+") + withSQLConf(SQLConf.TOP_K_SORT_FALLBACK_THRESHOLD.key -> "0") { + val df = + sql("SELECT SUM(id) FROM (SELECT id FROM range(0, 10, 1, 2) ORDER BY id DESC LIMIT 2)") + val plan = df.queryExecution.executedPlan + val shuffle = collectFirst(plan) { + case s: CometShuffleExchangeExec if s.outputPartitioning == SinglePartition => s + }.getOrElse(fail(s"Expected a single-partition Comet shuffle:\n${plan.treeString}")) + val inheritedTag = SparkPlan.LOGICAL_PLAN_INHERITED_TAG + val logicalSort = shuffle.originalPlan + .getTagValue(inheritedTag) + .getOrElse(fail(s"Expected the shuffle to inherit the local sort:\n${plan.treeString}")) + assert(shuffle.getTagValue(SparkPlan.LOGICAL_PLAN_TAG).isEmpty) + assert(shuffle.getTagValue(inheritedTag).exists(_ eq logicalSort)) + } + } + test("Ensure that the correct outputPartitioning of CometSort") { withTable("test_data") { val tableDF = spark.sparkContext diff --git a/spark/src/test/scala/org/apache/comet/rules/CometExecRuleSuite.scala b/spark/src/test/scala/org/apache/comet/rules/CometExecRuleSuite.scala index 0ad4c50a2c1..1309fc00773 100644 --- a/spark/src/test/scala/org/apache/comet/rules/CometExecRuleSuite.scala +++ b/spark/src/test/scala/org/apache/comet/rules/CometExecRuleSuite.scala @@ -37,6 +37,7 @@ import org.apache.spark.sql.catalyst.plans.Inner import org.apache.spark.sql.catalyst.plans.logical.{LocalRelation, LogicalPlan} import org.apache.spark.sql.catalyst.plans.physical.{HashPartitioning, IdentityBroadcastMode, RoundRobinPartitioning, SinglePartition} import org.apache.spark.sql.catalyst.rules.Rule +import org.apache.spark.sql.catalyst.trees.TreeNodeTag import org.apache.spark.sql.comet._ import org.apache.spark.sql.comet.CometExec.nativeLeaves import org.apache.spark.sql.comet.execution.shuffle.{CometNativeShuffle, CometShuffleExchangeExec} @@ -1308,33 +1309,36 @@ class CometExecRuleSuite extends CometTestBase { } } - test("CometExecRule preserves the current direct AQE logical link") { - withSQLConf( - SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "false", - SQLConf.WHOLESTAGE_CODEGEN_ENABLED.key -> "false", - CometConf.COMET_CONVERT_FROM_RANGE_ENABLED.key -> "true") { - val originalTags = - Seq(Some(SparkPlan.LOGICAL_PLAN_TAG), Some(SparkPlan.LOGICAL_PLAN_INHERITED_TAG), None) + private val logicalPlanTags = + Seq(SparkPlan.LOGICAL_PLAN_TAG, SparkPlan.LOGICAL_PLAN_INHERITED_TAG) + + private def forEachOriginalLogicalTag( + f: (CometHashAggregateExec, LogicalPlan, Option[TreeNodeTag[LogicalPlan]]) => Unit): Unit = + withAdaptiveAggregateConf { + val originalTags = logicalPlanTags.map(Some(_)) :+ None originalTags.foreach { originalTag => withClue(s"original logical tag: $originalTag") { val aggregate = createAdaptiveAggregate() val original = aggregate.originalPlan val originalLogicalPlan = original.logicalLink.get - original.unsetTagValue(SparkPlan.LOGICAL_PLAN_TAG) - original.unsetTagValue(SparkPlan.LOGICAL_PLAN_INHERITED_TAG) + logicalPlanTags.foreach(original.unsetTagValue(_)) originalTag.foreach(original.setTagValue(_, originalLogicalPlan)) + f(aggregate, originalLogicalPlan, originalTag) + } + } + } - var current: SparkPlan = aggregate - (1 to 2).foreach { _ => - val logicalStage = LogicalQueryStage(originalLogicalPlan, current) - val replanned = spark.sessionState.planner.plan(logicalStage).next() - assert(replanned eq current) - assert(replanned.getTagValue(SparkPlan.LOGICAL_PLAN_TAG).exists(_ eq logicalStage)) + test("CometExecRule preserves the current direct AQE logical link") { + forEachOriginalLogicalTag { (aggregate, originalLogicalPlan, _) => + var current: SparkPlan = aggregate + (1 to 2).foreach { _ => + val logicalStage = LogicalQueryStage(originalLogicalPlan, current) + val replanned = spark.sessionState.planner.plan(logicalStage).next() + assert(replanned eq current) + assert(replanned.getTagValue(SparkPlan.LOGICAL_PLAN_TAG).exists(_ eq logicalStage)) - current = applyCometExecRule(replanned) - assert(current.getTagValue(SparkPlan.LOGICAL_PLAN_TAG).exists(_ eq logicalStage)) - } - } + current = applyCometExecRule(replanned) + assert(current.getTagValue(SparkPlan.LOGICAL_PLAN_TAG).exists(_ eq logicalStage)) } } } @@ -1429,36 +1433,44 @@ class CometExecRuleSuite extends CometTestBase { } } - test("CometExecRule repairs ordinary and inherited logical links from the original plan") { - withSQLConf( - SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "false", - SQLConf.WHOLESTAGE_CODEGEN_ENABLED.key -> "false", - CometConf.COMET_CONVERT_FROM_RANGE_ENABLED.key -> "true") { - val originalTags = - Seq(Some(SparkPlan.LOGICAL_PLAN_TAG), Some(SparkPlan.LOGICAL_PLAN_INHERITED_TAG), None) - for (originalTag <- originalTags; hasDirectLink <- Seq(false, true)) { - withClue(s"original logical tag: $originalTag, ordinary direct link: $hasDirectLink") { - val aggregate = createAdaptiveAggregate() - val original = aggregate.originalPlan - val originalLogicalPlan = original.logicalLink.get - original.unsetTagValue(SparkPlan.LOGICAL_PLAN_TAG) - original.unsetTagValue(SparkPlan.LOGICAL_PLAN_INHERITED_TAG) - originalTag.foreach(original.setTagValue(_, originalLogicalPlan)) + test("CometExecRule preserves an inherited AQE stage for its original logical subtree") { + forEachOriginalLogicalTag { (aggregate, logicalPlan, originalTag) => + val stage = LogicalQueryStage(LogicalQueryStage(logicalPlan, aggregate), aggregate) + aggregate.unsetTagValue(SparkPlan.LOGICAL_PLAN_TAG) + aggregate.setTagValue(SparkPlan.LOGICAL_PLAN_INHERITED_TAG, stage) + val repaired = applyCometExecRule(aggregate) + assert(repaired.getTagValue(SparkPlan.LOGICAL_PLAN_TAG).isEmpty) + if (originalTag.isDefined) { + assert(repaired.logicalLink.exists(_ eq stage)) + } else { + assert(repaired.logicalLink.isEmpty) + } + } + } + + test("CometExecRule repairs stale logical links from the original plan") { + Seq(false, true).foreach { hasDirectLink => + forEachOriginalLogicalTag { (aggregate, originalLogicalPlan, originalTag) => + withClue(s"ordinary direct link: $hasDirectLink") { + // Without a direct link, use a structurally equal but unrelated logical node. + // With a direct link, even a matching inherited stage is shadowed. + val stagePlan = if (hasDirectLink) originalLogicalPlan else originalLogicalPlan.clone() aggregate.unsetTagValue(SparkPlan.LOGICAL_PLAN_TAG) aggregate.setTagValue( SparkPlan.LOGICAL_PLAN_INHERITED_TAG, - LogicalQueryStage(originalLogicalPlan, aggregate)) + LogicalQueryStage(stagePlan, aggregate)) if (hasDirectLink) { aggregate.setTagValue(SparkPlan.LOGICAL_PLAN_TAG, LocalRelation(aggregate.output)) } val transformed = applyCometExecRule(aggregate) - if (originalTag.isDefined) { - assert(transformed.logicalLink.exists(_ eq originalLogicalPlan)) - } else { - assert(transformed.getTagValue(SparkPlan.LOGICAL_PLAN_TAG).isEmpty) - assert(transformed.getTagValue(SparkPlan.LOGICAL_PLAN_INHERITED_TAG).isEmpty) + logicalPlanTags.foreach { tag => + if (originalTag.contains(tag)) { + assert(transformed.getTagValue(tag).exists(_ eq originalLogicalPlan)) + } else { + assert(transformed.getTagValue(tag).isEmpty) + } } } }