11diff --git a/pom.xml b/pom.xml
2- index 22922143fc3..3c1f5d381ee 100644
2+ index 22922143fc3..477d4ec4194 100644
33--- a/pom.xml
44+++ b/pom.xml
55@@ -148,6 +148,8 @@
@@ -1701,20 +1701,22 @@ index 47d5ff67b84..8dc8f65d4b1 100644
17011701 withSQLConf(SQLConf.ADAPTIVE_EXECUTION_ENABLED.key -> "true",
17021702 SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1",
17031703diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/RemoveRedundantProjectsSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/RemoveRedundantProjectsSuite.scala
1704- index b5bac8079c4..a3731888e12 100644
1704+ index b5bac8079c4..9420dbdb936 100644
17051705--- a/sql/core/src/test/scala/org/apache/spark/sql/execution/RemoveRedundantProjectsSuite.scala
17061706+++ b/sql/core/src/test/scala/org/apache/spark/sql/execution/RemoveRedundantProjectsSuite.scala
1707- @@ -17,7 +17,8 @@
1707+ @@ -17,7 +17,10 @@
17081708
17091709 package org.apache.spark.sql.execution
17101710
17111711- import org.apache.spark.sql.{DataFrame, QueryTest, Row}
1712+ + import org.apache.comet.CometConf
1713+ +
17121714+ import org.apache.spark.sql.{DataFrame, IgnoreComet, QueryTest, Row}
17131715+ import org.apache.spark.sql.comet.CometProjectExec
17141716 import org.apache.spark.sql.connector.SimpleWritableDataSource
17151717 import org.apache.spark.sql.execution.adaptive.{AdaptiveSparkPlanHelper, DisableAdaptiveExecutionSuite, EnableAdaptiveExecutionSuite}
17161718 import org.apache.spark.sql.internal.SQLConf
1717- @@ -34,7 +35 ,10 @@ abstract class RemoveRedundantProjectsSuiteBase
1719+ @@ -34,7 +37 ,10 @@ abstract class RemoveRedundantProjectsSuiteBase
17181720 private def assertProjectExecCount(df: DataFrame, expected: Int): Unit = {
17191721 withClue(df.queryExecution) {
17201722 val plan = df.queryExecution.executedPlan
@@ -1726,7 +1728,7 @@ index b5bac8079c4..a3731888e12 100644
17261728 assert(actual == expected)
17271729 }
17281730 }
1729- @@ -112,7 +116 ,8 @@ abstract class RemoveRedundantProjectsSuiteBase
1731+ @@ -112,7 +118 ,8 @@ abstract class RemoveRedundantProjectsSuiteBase
17301732 assertProjectExec(query, 1, 3)
17311733 }
17321734
@@ -1736,6 +1738,42 @@ index b5bac8079c4..a3731888e12 100644
17361738 val query = "select * from (select key, a, c, b from testView) as t1 join " +
17371739 "(select key, a, b, c from testView) as t2 on t1.key = t2.key where t2.a > 50"
17381740 assertProjectExec(query, 2, 2)
1741+ @@ -134,12 +141,25 @@ abstract class RemoveRedundantProjectsSuiteBase
1742+ val df = data.selectExpr("a", "b", "key", "explode(array(key, a, b)) as d").filter("d > 0")
1743+ df.collect()
1744+ val plan = df.queryExecution.executedPlan
1745+ - val numProjects = collectWithSubqueries(plan) { case p: ProjectExec => p }.length
1746+ + val numProjects = collectWithSubqueries(plan) {
1747+ + case p: ProjectExec => p
1748+ + case p: CometProjectExec => p
1749+ + }.length
1750+
1751+ // Create a new plan that reverse the GenerateExec output and add a new ProjectExec between
1752+ // GenerateExec and its child. This is to test if the ProjectExec is removed, the output of
1753+ // the query will be incorrect.
1754+ - val newPlan = stripAQEPlan(plan) transform {
1755+ +
1756+ + // Comet-specific change to get original Spark plan before applying
1757+ + // a transformation to add a new ProjectExec
1758+ + var sparkPlan: SparkPlan = null
1759+ + withSQLConf(CometConf.COMET_EXEC_ENABLED.key -> "false") {
1760+ + val df = data.selectExpr("a", "b", "key", "explode(array(key, a, b)) as d").filter("d > 0")
1761+ + df.collect()
1762+ + sparkPlan = df.queryExecution.executedPlan
1763+ + }
1764+ +
1765+ + val newPlan = stripAQEPlan(sparkPlan) transform {
1766+ case g @ GenerateExec(_, requiredChildOutput, _, _, child) =>
1767+ g.copy(requiredChildOutput = requiredChildOutput.reverse,
1768+ child = ProjectExec(requiredChildOutput.reverse, child))
1769+ @@ -151,6 +171,7 @@ abstract class RemoveRedundantProjectsSuiteBase
1770+ // The manually added ProjectExec node shouldn't be removed.
1771+ assert(collectWithSubqueries(newExecutedPlan) {
1772+ case p: ProjectExec => p
1773+ + case p: CometProjectExec => p
1774+ }.size == numProjects + 1)
1775+
1776+ // Check the original plan's output and the new plan's output are the same.
17391777diff --git a/sql/core/src/test/scala/org/apache/spark/sql/execution/RemoveRedundantSortsSuite.scala b/sql/core/src/test/scala/org/apache/spark/sql/execution/RemoveRedundantSortsSuite.scala
17401778index 005e764cc30..92ec088efab 100644
17411779--- a/sql/core/src/test/scala/org/apache/spark/sql/execution/RemoveRedundantSortsSuite.scala
0 commit comments