sarutak opened a new pull request #32606:
URL: https://github.com/apache/spark/pull/32606


   ### What changes were proposed in this pull request?
   <!--
   Please clarify what changes you are proposing. The purpose of this section 
is to outline the changes and how this PR fixes the issue. 
   If possible, please consider writing useful notes for better and faster 
reviews in your PR. See the examples below.
     1. If you refactor some codes with changing classes, showing the class 
hierarchy will help reviewers.
     2. If you fix some SQL features, you can provide some references of other 
DBMSes.
     3. If there is design documentation, please add the link.
     4. If there is a discussion in the mailing list, please add the link.
   -->
   This PR fixes an issue that `RemoveRedundantProjects` removes `ProjectExec` 
which is for generating `UnsafeRow`.
   In `DataSourceV2Strategy`, `ProjectExec` will be inserted to ensure internal 
rows are `UnsafeRow`.
   
   ```
     private def withProjectAndFilter(
         project: Seq[NamedExpression],
         filters: Seq[Expression],
         scan: LeafExecNode,
         needsUnsafeConversion: Boolean): SparkPlan = {
       val filterCondition = filters.reduceLeftOption(And)
       val withFilter = filterCondition.map(FilterExec(_, scan)).getOrElse(scan)
   
       if (withFilter.output != project || needsUnsafeConversion) {
         ProjectExec(project, withFilter)
       } else {
         withFilter
       }
     }
   ...
       case PhysicalOperation(project, filters, relation: 
DataSourceV2ScanRelation) =>
         // projection and filters were already pushed down in the optimizer.
         // this uses PhysicalOperation to get the projection and ensure that 
if the batch scan does
         // not support columnar, a projection is added to convert the rows to 
UnsafeRow.
         val batchExec = BatchScanExec(relation.output, relation.scan)
         withProjectAndFilter(project, filters, batchExec, 
!batchExec.supportsColumnar) :: Nil
   ```
   So, the hierarchy of the partial tree should be like 
`ProjectExec(FilterExec(BatchScan))`.
   But `RemoveRedundantProjects` doesn't consider this type of hierarchy, 
leading `ClassCastException`.
   
   A concreate example to reproduce this issue is reported:
   ```
   import scala.collection.JavaConverters._
   
   import org.apache.iceberg.{PartitionSpec, TableProperties}
   import org.apache.iceberg.hadoop.HadoopTables
   import org.apache.iceberg.spark.SparkSchemaUtil
   import org.apache.spark.sql.{DataFrame, QueryTest, SparkSession}
   import org.apache.spark.sql.internal.SQLConf
   
   class RemoveRedundantProjectsTest extends QueryTest {
     override val spark: SparkSession = SparkSession
       .builder()
       .master("local[4]")
       .config("spark.driver.bindAddress", "127.0.0.1")
       .appName(suiteName)
       .getOrCreate()
     test("RemoveRedundantProjects removes non-redundant projects") {
       withSQLConf(
         SQLConf.AUTO_BROADCASTJOIN_THRESHOLD.key -> "-1",
         SQLConf.WHOLESTAGE_CODEGEN_ENABLED.key -> "false",
         SQLConf.REMOVE_REDUNDANT_PROJECTS_ENABLED.key -> "true") {
         withTempDir { dir =>
           val path = dir.getCanonicalPath
           val data = spark.range(3).toDF
           val table = new HadoopTables().create(
             SparkSchemaUtil.convert(data.schema),
             PartitionSpec.unpartitioned(),
             Map(TableProperties.WRITE_NEW_DATA_LOCATION -> path).asJava,
             path)
           data.write.format("iceberg").mode("overwrite").save(path)
           table.refresh()
   
           val df = spark.read.format("iceberg").load(path)
           val dfX = df.as("x")
           val dfY = df.as("y")
           val join = dfX.filter(dfX("id") > 0).join(dfY, "id")
           join.explain("extended")
           assert(join.count() == 2)
         }
       }
     }
   }
   ```
   ```
   [info] - RemoveRedundantProjects removes non-redundant projects *** FAILED 
***
   [info]   org.apache.spark.SparkException: Job aborted due to stage failure: 
Task 0 in stage 1.0 failed 1 times, most recent failure: Lost task 0.0 in stage 
1.0 (TID 4) (xeroxms100.northamerica.corp.microsoft.com executor driver): 
java.lang.ClassCastException: 
org.apache.spark.sql.catalyst.expressions.GenericInternalRow cannot be cast to 
org.apache.spark.sql.catalyst.expressions.UnsafeRow
   [info]  at 
org.apache.spark.sql.execution.UnsafeExternalRowSorter.sort(UnsafeExternalRowSorter.java:226)
   [info]  at 
org.apache.spark.sql.execution.SortExec.$anonfun$doExecute$1(SortExec.scala:119)
   ```
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as 
the documentation fix.
   If yes, please clarify the previous behavior and the change this PR proposes 
- provide the console output, description and/or an example to show the 
behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to 
the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   No.
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some 
test cases that check the changes thoroughly including negative and positive 
cases if possible.
   If it was tested in a way different from regular unit tests, please clarify 
how you tested step by step, ideally copy and paste-able, so that other 
reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why 
it was difficult to add.
   -->
   New test.


-- 
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]

Reply via email to