zml1206 commented on code in PR #13714:
URL: https://github.com/apache/gluten/pull/13714#discussion_r4703359687


##########
gluten-substrait/src/main/scala/org/apache/gluten/extension/columnar/PushDownInputFileExpression.scala:
##########
@@ -228,14 +275,56 @@ object PushDownInputFileExpression {
             CollapseProjectShim.buildCleanedProjectList(p1.projectList, 
p2.projectList)))
     }
 
-    private def canCollapseProject(project: ProjectExec): Boolean = {
-      project.projectList.forall {
-        case Alias(_: InputFileName | _: InputFileBlockStart | _: 
InputFileBlockLength, _) => true
-        case _: Attribute => true
-        case _ => false
+    /**
+     * Folds `project` into the scan that `withOutput` makes with the given 
output. The scan reads
+     * each column once, and each input file function once even when 
PreOffload added it for calls
+     * at different levels, since the native scan maps a non-partition column 
to one output only. A
+     * project over the scan gives the repeats their values.
+     */
+    private def foldIntoScan(
+        project: ProjectExec,
+        withOutput: Seq[Attribute] => SparkPlan): SparkPlan = {
+      val firstByName = mutable.Map[String, Attribute]()
+      val read = mutable.Set[ExprId]()
+      // Left: read by the scan. Right: given by the project over it.
+      val columns: Seq[Either[Attribute, NamedExpression]] = 
project.projectList.map {
+        case a: Alias if firstByName.contains(a.name) =>
+          Right(Alias(firstByName(a.name), a.name)(exprId = a.exprId))
+        case a: Alias =>
+          firstByName(a.name) = a.toAttribute
+          read += a.exprId
+          Left(a.toAttribute)
+        case a if read.contains(a.exprId) => Right(a.toAttribute)
+        case a =>
+          read += a.exprId
+          Left(a.toAttribute)
+      }
+      val scan = withOutput(columns.collect { case Left(attr) => attr })
+      if (columns.forall(_.isLeft)) {
+        scan
+      } else {
+        val scanAttrs = scan.output.map(a => a.exprId -> a).toMap
+        
BackendsApiManager.getSparkPlanExecApiInstance.genProjectExecTransformer(

Review Comment:
   `foldIntoScan` creates a `ProjectExecTransformer` unconditionally when it 
needs to reproduce a repeated column. I verified this on Spark 4.1.1 with both 
Parquet V1 and V2. With `spark.gluten.sql.columnar.project=false`, the query 
below still has a native Project directly above the scan.
   
   ```sql
   SELECT input_file_name() AS f
   FROM t
   WHERE input_file_name() LIKE '%.parquet'
   ```
   
   Could we check `GlutenConfig.get.enableColumnarProject` here and use 
`ProjectExec(projectList, scan)` when it’s false? The scan can still read the 
file name once, and the Spark Project can expose it under both `exprId`s. I 
tested this change with project offload enabled and disabled on both V1 and V2. 
All four cases matched vanilla Spark, and the scan stayed native. The existing 
test for this query could also assert that the plan contains no 
`ProjectExecTransformer` when project offload is disabled.



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

Reply via email to