[ 
https://issues.apache.org/jira/browse/SPARK-22283?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16207780#comment-16207780
 ] 

Liang-Chi Hsieh commented on SPARK-22283:
-----------------------------------------

When joined result has duplicate column name, you can't select any of the 
ambiguous columns by just name. Doesn't {{withColumn}} current behavior simply 
follow it?


> withColumn should replace multiple instances with a single one
> --------------------------------------------------------------
>
>                 Key: SPARK-22283
>                 URL: https://issues.apache.org/jira/browse/SPARK-22283
>             Project: Spark
>          Issue Type: Bug
>          Components: Spark Core
>    Affects Versions: 2.2.0
>            Reporter: Albert Meltzer
>
> Currently, {{withColumn}} claims to do the following: _"adding a column or 
> replacing the existing column that has the same name."_
> Unfortunately, if multiple existing columns have the same name (which is a 
> normal occurrence after a join), this results in multiple replaced -- and 
> retained --
>  columns (with the same value), and messages about an ambiguous column.
> The current implementation of {{withColumn}} contains this:
> {noformat}
>   def withColumn(colName: String, col: Column): DataFrame = {
>     val resolver = sparkSession.sessionState.analyzer.resolver
>     val output = queryExecution.analyzed.output
>     val shouldReplace = output.exists(f => resolver(f.name, colName))
>     if (shouldReplace) {
>       val columns = output.map { field =>
>         if (resolver(field.name, colName)) {
>           col.as(colName)
>         } else {
>           Column(field)
>         }
>       }
>       select(columns : _*)
>     } else {
>       select(Column("*"), col.as(colName))
>     }
>   }
> {noformat}
> Instead, suggest something like this (which replaces all matching fields with 
> a single instance of the new one):
> {noformat}
>   def withColumn(colName: String, col: Column): DataFrame = {
>     val resolver = sparkSession.sessionState.analyzer.resolver
>     val output = queryExecution.analyzed.output
>     val existing = output.filterNot(f => resolver(f.name, colName)).map(new 
> Column(_))
>     select(existing :+ col.as(colName): _*)
>   }
> {noformat}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

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

Reply via email to