dengziming commented on code in PR #51686:
URL: https://github.com/apache/spark/pull/51686#discussion_r2239551508


##########
sql/core/src/main/scala/org/apache/spark/sql/execution/datasources/v2/V2ScanRelationPushDown.scala:
##########
@@ -226,10 +195,50 @@ object V2ScanRelationPushDown extends Rule[LogicalPlan] 
with PredicateHelper {
       }
   }
 
-  def generateJoinOutputAlias(name: String): String =
-    s"${name}_${java.util.UUID.randomUUID().toString.replace("-", "_")}"
+  private def generateColumnAliasesForDuplicatedName(
+    leftSideRequiredColumnNames: Array[String],
+    rightSideRequiredColumnNames: Array[String]
+  ): (Array[SupportsPushDownJoin.ColumnWithAlias],
+    Array[SupportsPushDownJoin.ColumnWithAlias]) = {
+    //  Count occurrences of each column name across both sides to identify 
duplicates.
+    val allRequiredColumnNames = leftSideRequiredColumnNames ++ 
rightSideRequiredColumnNames
+    val allNameCounts: Map[String, Int] =
+      allRequiredColumnNames.groupBy(identity).view.mapValues(_.size).toMap
+    // Use Set for O(1) lookups when checking existing column names, claim all 
names
+    // that appears only once to ensure they have highest priority.
+    val allClaimedAliases = mutable.HashSet.empty ++ allNameCounts.filter(_._2 
== 1).keySet
+
+    // Track the next suffix index for each column name (starts at 0) to avoid 
extreme worst
+    // case of O(n^2) alias generation.
+    val aliasSuffixIndex = mutable.HashMap[String, Int]().withDefaultValue(0)
+
+    def processColumn(name: String): SupportsPushDownJoin.ColumnWithAlias = {
+      // Ensure a name that appears only once does not require an alias.
+      if (allNameCounts(name) == 1) {
+        new SupportsPushDownJoin.ColumnWithAlias(name, null)
+      } else {
+        var attempt = aliasSuffixIndex(name)
+
+        // Generate candidate alias: use original name for the first attempt, 
then append
+        // suffix for more attempts.
+        var candidate = if (attempt == 0) name else s"${name}_$attempt"

Review Comment:
   I think I misunderstood your idea, but I tried your idea locally and the 
result was unexpected in the below picture , we can also make some adjustments 
based on it, but the LOC gain is not great.
   
   <img width="2344" height="960" alt="image" 
src="https://github.com/user-attachments/assets/4d2c6ee4-65b1-4dfd-ac16-64cbab6ab558";
 />
   



-- 
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: reviews-unsubscr...@spark.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org
For additional commands, e-mail: reviews-h...@spark.apache.org

Reply via email to