Copilot commented on code in PR #12215:
URL: https://github.com/apache/gluten/pull/12215#discussion_r3426626344


##########
gluten-delta/src/main/scala/org/apache/gluten/extension/DeltaPostTransformRules.scala:
##########
@@ -171,6 +196,16 @@ object DeltaPostTransformRules {
     }
   }
 
+  private def containsDmlRowIndexFallbackScan(plan: SparkPlan): Boolean = {
+    plan.exists {
+      case scan: FileSourceScanExec =>
+        FallbackTags
+          .getOption(scan)
+          .exists(_.reason().contains("fallback Delta DV DML row-index scan"))
+      case _ => false
+    }
+  }

Review Comment:
   containsDmlRowIndexFallbackScan relies on matching a specific 
fallback-reason string. That is brittle (message text can change) and can leave 
the parent Project/Filter native, reintroducing the row↔columnar transitions 
this rule is trying to avoid. Prefer checking the DML row-index scan tag plus 
the presence of any FallbackTag instead of string matching.



##########
gluten-delta/src/main/scala/org/apache/gluten/extension/DeltaDeletionVectorDmlUtils.scala:
##########
@@ -0,0 +1,142 @@
+/*
+ * 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.gluten.extension
+
+import org.apache.spark.sql.catalyst.expressions.Expression
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreeNodeTag
+import org.apache.spark.sql.delta.DeltaParquetFileFormat
+import org.apache.spark.sql.delta.files.TahoeFileIndex
+import org.apache.spark.sql.delta.stats.PreparedDeltaFileIndex
+import org.apache.spark.sql.execution.{FileSourceScanExec, SparkPlan}
+
+object DeltaDeletionVectorDmlUtils {
+  private val DmlRowIndexScanTag: TreeNodeTag[Boolean] =
+    TreeNodeTag[Boolean]("org.apache.gluten.delta.dml.row.index.scan")
+
+  // Spark 3.5+ exposes this as 
ParquetFileFormat.ROW_INDEX_TEMPORARY_COLUMN_NAME.
+  private val parquetTemporaryRowIndexColumnName = "_tmp_metadata_row_index"
+  private val deletionVectorRowIndexColumnNames =
+    Set(
+      "__delta_internal_row_index",
+      DeltaParquetFileFormat.ROW_INDEX_COLUMN_NAME,
+      parquetTemporaryRowIndexColumnName,
+      "row_index",
+      "rowIndexCol")
+  private val filePathColumnNames = Set("file_path", "filePath")
+
+  val tagDmlRowIndexScans: Rule[SparkPlan] = (plan: SparkPlan) => {
+    def visit(
+        node: SparkPlan,
+        hasRowIndexReference: Boolean,
+        hasFilePathReference: Boolean,
+        hasBitmapAggregation: Boolean): Unit = {
+      val nextHasRowIndexReference =
+        hasRowIndexReference || 
node.expressions.exists(referencesRowIndexColumn)
+      val nextHasFilePathReference =
+        hasFilePathReference || 
node.expressions.exists(referencesFilePathColumn)
+      val nextHasBitmapAggregation =
+        hasBitmapAggregation || 
node.expressions.exists(referencesDeletionVectorBitmapAggregator)
+
+      node.children.foreach {
+        case scan: FileSourceScanExec
+            if nextHasBitmapAggregation &&
+              nextHasRowIndexReference &&
+              nextHasFilePathReference &&
+              isDeletionVectorDmlRowIndexScanCandidate(scan) =>
+          scan.setTagValue(DmlRowIndexScanTag, true)
+        case child =>
+          visit(
+            child,
+            nextHasRowIndexReference,
+            nextHasFilePathReference,
+            nextHasBitmapAggregation)
+      }
+    }
+
+    visit(
+      plan,
+      hasRowIndexReference = false,
+      hasFilePathReference = false,
+      hasBitmapAggregation = false)
+    plan
+  }
+
+  def copyDmlRowIndexScanTag(from: SparkPlan, to: SparkPlan): Unit = {
+    if (from.getTagValue(DmlRowIndexScanTag).contains(true)) {
+      to.setTagValue(DmlRowIndexScanTag, true)
+    }
+  }
+
+  def isDeltaScan(scan: FileSourceScanExec): Boolean = {
+    isDeltaFileIndex(scan) || isDeltaParquetScan(scan)
+  }
+
+  def isDeltaParquetScan(scan: FileSourceScanExec): Boolean = {
+    val fileFormatClass = scan.relation.fileFormat.getClass
+    fileFormatClass == classOf[DeltaParquetFileFormat] ||
+    fileFormatClass.getSimpleName == "GlutenDeltaParquetFileFormat"
+  }
+
+  def isDeltaFileIndex(scan: FileSourceScanExec): Boolean = {
+    scan.relation.location.isInstanceOf[TahoeFileIndex] ||
+    scan.relation.location.isInstanceOf[PreparedDeltaFileIndex]
+  }
+
+  def isDeletionVectorDmlRowIndexScan(scan: FileSourceScanExec): Boolean = {
+    scan.getTagValue(DmlRowIndexScanTag).contains(true) &&
+    isDeletionVectorDmlRowIndexScanCandidate(scan)
+  }
+
+  def isDeletionVectorDmlRowIndexScan(plan: SparkPlan): Boolean = {
+    plan.getTagValue(DmlRowIndexScanTag).contains(true)
+  }
+
+  private def isDeletionVectorDmlRowIndexScanCandidate(scan: 
FileSourceScanExec): Boolean = {
+    if (!isDeltaScan(scan)) {
+      return false
+    }
+
+    scanContainsColumnName(scan, deletionVectorRowIndexColumnNames) &&
+    scanContainsColumnName(scan, filePathColumnNames)
+  }
+
+  private def scanContainsColumnName(
+      scan: FileSourceScanExec,
+      columnNames: Set[String]): Boolean = {
+    val scanColumnNames = (scan.output.map(_.name) ++ 
scan.requiredSchema.fieldNames).toSet
+    scanColumnNames.exists(columnNames.contains) || 
columnNames.exists(scan.treeString.contains)
+  }
+
+  private def referencesRowIndexColumn(expr: Expression): Boolean = {
+    val expressionText = expr.toString()
+    expr.references.exists(attr => 
deletionVectorRowIndexColumnNames.contains(attr.name)) ||
+    deletionVectorRowIndexColumnNames.exists(expressionText.contains)
+  }

Review Comment:
   referencesRowIndexColumn falls back to expr.toString substring matching. 
This can produce false positives/negatives depending on expression string 
formatting, and it’s unnecessary when expr.references is available.



##########
gluten-delta/src/main/scala/org/apache/gluten/extension/DeltaDeletionVectorDmlUtils.scala:
##########
@@ -0,0 +1,142 @@
+/*
+ * 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.gluten.extension
+
+import org.apache.spark.sql.catalyst.expressions.Expression
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreeNodeTag
+import org.apache.spark.sql.delta.DeltaParquetFileFormat
+import org.apache.spark.sql.delta.files.TahoeFileIndex
+import org.apache.spark.sql.delta.stats.PreparedDeltaFileIndex
+import org.apache.spark.sql.execution.{FileSourceScanExec, SparkPlan}
+
+object DeltaDeletionVectorDmlUtils {
+  private val DmlRowIndexScanTag: TreeNodeTag[Boolean] =
+    TreeNodeTag[Boolean]("org.apache.gluten.delta.dml.row.index.scan")
+
+  // Spark 3.5+ exposes this as 
ParquetFileFormat.ROW_INDEX_TEMPORARY_COLUMN_NAME.
+  private val parquetTemporaryRowIndexColumnName = "_tmp_metadata_row_index"
+  private val deletionVectorRowIndexColumnNames =
+    Set(
+      "__delta_internal_row_index",
+      DeltaParquetFileFormat.ROW_INDEX_COLUMN_NAME,
+      parquetTemporaryRowIndexColumnName,
+      "row_index",
+      "rowIndexCol")
+  private val filePathColumnNames = Set("file_path", "filePath")
+
+  val tagDmlRowIndexScans: Rule[SparkPlan] = (plan: SparkPlan) => {
+    def visit(
+        node: SparkPlan,
+        hasRowIndexReference: Boolean,
+        hasFilePathReference: Boolean,
+        hasBitmapAggregation: Boolean): Unit = {
+      val nextHasRowIndexReference =
+        hasRowIndexReference || 
node.expressions.exists(referencesRowIndexColumn)
+      val nextHasFilePathReference =
+        hasFilePathReference || 
node.expressions.exists(referencesFilePathColumn)
+      val nextHasBitmapAggregation =
+        hasBitmapAggregation || 
node.expressions.exists(referencesDeletionVectorBitmapAggregator)
+
+      node.children.foreach {
+        case scan: FileSourceScanExec
+            if nextHasBitmapAggregation &&
+              nextHasRowIndexReference &&
+              nextHasFilePathReference &&
+              isDeletionVectorDmlRowIndexScanCandidate(scan) =>
+          scan.setTagValue(DmlRowIndexScanTag, true)
+        case child =>
+          visit(
+            child,
+            nextHasRowIndexReference,
+            nextHasFilePathReference,
+            nextHasBitmapAggregation)
+      }
+    }
+
+    visit(
+      plan,
+      hasRowIndexReference = false,
+      hasFilePathReference = false,
+      hasBitmapAggregation = false)
+    plan
+  }
+
+  def copyDmlRowIndexScanTag(from: SparkPlan, to: SparkPlan): Unit = {
+    if (from.getTagValue(DmlRowIndexScanTag).contains(true)) {
+      to.setTagValue(DmlRowIndexScanTag, true)
+    }
+  }
+
+  def isDeltaScan(scan: FileSourceScanExec): Boolean = {
+    isDeltaFileIndex(scan) || isDeltaParquetScan(scan)
+  }
+
+  def isDeltaParquetScan(scan: FileSourceScanExec): Boolean = {
+    val fileFormatClass = scan.relation.fileFormat.getClass
+    fileFormatClass == classOf[DeltaParquetFileFormat] ||
+    fileFormatClass.getSimpleName == "GlutenDeltaParquetFileFormat"
+  }
+
+  def isDeltaFileIndex(scan: FileSourceScanExec): Boolean = {
+    scan.relation.location.isInstanceOf[TahoeFileIndex] ||
+    scan.relation.location.isInstanceOf[PreparedDeltaFileIndex]
+  }
+
+  def isDeletionVectorDmlRowIndexScan(scan: FileSourceScanExec): Boolean = {
+    scan.getTagValue(DmlRowIndexScanTag).contains(true) &&
+    isDeletionVectorDmlRowIndexScanCandidate(scan)
+  }
+
+  def isDeletionVectorDmlRowIndexScan(plan: SparkPlan): Boolean = {
+    plan.getTagValue(DmlRowIndexScanTag).contains(true)
+  }
+
+  private def isDeletionVectorDmlRowIndexScanCandidate(scan: 
FileSourceScanExec): Boolean = {
+    if (!isDeltaScan(scan)) {
+      return false
+    }
+
+    scanContainsColumnName(scan, deletionVectorRowIndexColumnNames) &&
+    scanContainsColumnName(scan, filePathColumnNames)
+  }
+
+  private def scanContainsColumnName(
+      scan: FileSourceScanExec,
+      columnNames: Set[String]): Boolean = {
+    val scanColumnNames = (scan.output.map(_.name) ++ 
scan.requiredSchema.fieldNames).toSet
+    scanColumnNames.exists(columnNames.contains) || 
columnNames.exists(scan.treeString.contains)

Review Comment:
   scanContainsColumnName uses scan.treeString substring matching as a 
fallback. That’s expensive and can be unstable across Spark 
versions/formatting; since this is used to decide whether to tag/fallback a 
scan, it’s safer to rely on structured data (scan.output / requiredSchema) only.



##########
gluten-delta/src/main/scala/org/apache/gluten/extension/DeltaDeletionVectorDmlUtils.scala:
##########
@@ -0,0 +1,142 @@
+/*
+ * 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.gluten.extension
+
+import org.apache.spark.sql.catalyst.expressions.Expression
+import org.apache.spark.sql.catalyst.rules.Rule
+import org.apache.spark.sql.catalyst.trees.TreeNodeTag
+import org.apache.spark.sql.delta.DeltaParquetFileFormat
+import org.apache.spark.sql.delta.files.TahoeFileIndex
+import org.apache.spark.sql.delta.stats.PreparedDeltaFileIndex
+import org.apache.spark.sql.execution.{FileSourceScanExec, SparkPlan}
+
+object DeltaDeletionVectorDmlUtils {
+  private val DmlRowIndexScanTag: TreeNodeTag[Boolean] =
+    TreeNodeTag[Boolean]("org.apache.gluten.delta.dml.row.index.scan")
+
+  // Spark 3.5+ exposes this as 
ParquetFileFormat.ROW_INDEX_TEMPORARY_COLUMN_NAME.
+  private val parquetTemporaryRowIndexColumnName = "_tmp_metadata_row_index"
+  private val deletionVectorRowIndexColumnNames =
+    Set(
+      "__delta_internal_row_index",
+      DeltaParquetFileFormat.ROW_INDEX_COLUMN_NAME,
+      parquetTemporaryRowIndexColumnName,
+      "row_index",
+      "rowIndexCol")
+  private val filePathColumnNames = Set("file_path", "filePath")
+
+  val tagDmlRowIndexScans: Rule[SparkPlan] = (plan: SparkPlan) => {
+    def visit(
+        node: SparkPlan,
+        hasRowIndexReference: Boolean,
+        hasFilePathReference: Boolean,
+        hasBitmapAggregation: Boolean): Unit = {
+      val nextHasRowIndexReference =
+        hasRowIndexReference || 
node.expressions.exists(referencesRowIndexColumn)
+      val nextHasFilePathReference =
+        hasFilePathReference || 
node.expressions.exists(referencesFilePathColumn)
+      val nextHasBitmapAggregation =
+        hasBitmapAggregation || 
node.expressions.exists(referencesDeletionVectorBitmapAggregator)
+
+      node.children.foreach {
+        case scan: FileSourceScanExec
+            if nextHasBitmapAggregation &&
+              nextHasRowIndexReference &&
+              nextHasFilePathReference &&
+              isDeletionVectorDmlRowIndexScanCandidate(scan) =>
+          scan.setTagValue(DmlRowIndexScanTag, true)
+        case child =>
+          visit(
+            child,
+            nextHasRowIndexReference,
+            nextHasFilePathReference,
+            nextHasBitmapAggregation)
+      }
+    }
+
+    visit(
+      plan,
+      hasRowIndexReference = false,
+      hasFilePathReference = false,
+      hasBitmapAggregation = false)
+    plan
+  }
+
+  def copyDmlRowIndexScanTag(from: SparkPlan, to: SparkPlan): Unit = {
+    if (from.getTagValue(DmlRowIndexScanTag).contains(true)) {
+      to.setTagValue(DmlRowIndexScanTag, true)
+    }
+  }
+
+  def isDeltaScan(scan: FileSourceScanExec): Boolean = {
+    isDeltaFileIndex(scan) || isDeltaParquetScan(scan)
+  }
+
+  def isDeltaParquetScan(scan: FileSourceScanExec): Boolean = {
+    val fileFormatClass = scan.relation.fileFormat.getClass
+    fileFormatClass == classOf[DeltaParquetFileFormat] ||
+    fileFormatClass.getSimpleName == "GlutenDeltaParquetFileFormat"
+  }
+
+  def isDeltaFileIndex(scan: FileSourceScanExec): Boolean = {
+    scan.relation.location.isInstanceOf[TahoeFileIndex] ||
+    scan.relation.location.isInstanceOf[PreparedDeltaFileIndex]
+  }
+
+  def isDeletionVectorDmlRowIndexScan(scan: FileSourceScanExec): Boolean = {
+    scan.getTagValue(DmlRowIndexScanTag).contains(true) &&
+    isDeletionVectorDmlRowIndexScanCandidate(scan)
+  }
+
+  def isDeletionVectorDmlRowIndexScan(plan: SparkPlan): Boolean = {
+    plan.getTagValue(DmlRowIndexScanTag).contains(true)
+  }
+
+  private def isDeletionVectorDmlRowIndexScanCandidate(scan: 
FileSourceScanExec): Boolean = {
+    if (!isDeltaScan(scan)) {
+      return false
+    }
+
+    scanContainsColumnName(scan, deletionVectorRowIndexColumnNames) &&
+    scanContainsColumnName(scan, filePathColumnNames)
+  }
+
+  private def scanContainsColumnName(
+      scan: FileSourceScanExec,
+      columnNames: Set[String]): Boolean = {
+    val scanColumnNames = (scan.output.map(_.name) ++ 
scan.requiredSchema.fieldNames).toSet
+    scanColumnNames.exists(columnNames.contains) || 
columnNames.exists(scan.treeString.contains)
+  }
+
+  private def referencesRowIndexColumn(expr: Expression): Boolean = {
+    val expressionText = expr.toString()
+    expr.references.exists(attr => 
deletionVectorRowIndexColumnNames.contains(attr.name)) ||
+    deletionVectorRowIndexColumnNames.exists(expressionText.contains)
+  }
+
+  private def referencesFilePathColumn(expr: Expression): Boolean = {
+    val expressionText = expr.toString()
+    expr.references.exists(attr => filePathColumnNames.contains(attr.name)) ||
+    filePathColumnNames.exists(expressionText.contains)
+  }

Review Comment:
   referencesFilePathColumn falls back to expr.toString substring matching. 
This can be unstable across Spark versions and can accidentally match unrelated 
expressions; using expr.references only is more robust.



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