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

Liang-Chi Hsieh edited comment on SPARK-22283 at 10/17/17 11:43 PM:
--------------------------------------------------------------------

[~kitbellew] {{withColumn}} adds/replaces existing column that has the same 
name. In the ambiguous columns case, it sounds reasonable that it replaces the 
columns with the same name.

We should let a API does one thing. {{withColumn}} adds/replaces column with 
the same name. It sounds weird to implicitly drop one column when there are 
ambiguous columns.

For the use case {{a.join(b, ..., "left").withColumn("c", coalesce(a("c"), 
b("c")).select(..., "c")}}, if you just want to get one of the ambiguous 
columns, the simple workaround can be simply selecting the column like 
{{a.join(b, ..., "left").select(..., a("c"))}}.



was (Author: viirya):
[~kitbellew] I didn't mean you're doing select. I meant you can't select the 
ambiguous columns by name, so isn't it reasonable that you can't also 
withColumn the ambiguous columns by name? They are following the same behavior.

For the use case {{a.join(b, ..., "left").withColumn("c", coalesce(a("c"), 
b("c")).select(..., "c")}}, if you just want to get one of the ambiguous 
columns, the simple workaround can be simply selecting the column like 
{{a.join(b, ..., "left").select(..., a("c"))}}.


> 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: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to