You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@spark.apache.org by "Yichuan Wang (JIRA)" <ji...@apache.org> on 2017/06/27 01:44:00 UTC

[jira] [Comment Edited] (SPARK-6635) DataFrame.withColumn can create columns with identical names

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

Yichuan Wang edited comment on SPARK-6635 at 6/27/17 1:43 AM:
--------------------------------------------------------------

withColumn have this strange behavior with join, it replaces both columns from the left and right side of the join, and replace them with the new column (Spark 2.0.2)

{code:java}
val left = Seq((1, "one", null, "p1"), (2, "two", "b", null)).toDF("id", "name", "note", "note2")
val right = Seq((1, "c", 11, "n1"), (2,  null, 22, "n2")).toDF("id",  "note", "seq", "note2")
val j = left.join(right, "id")
j.show()
{code}

{code:java}
+---+----+----+-----+----+---+-----+
| id|name|note|note2|note|seq|note2|
+---+----+----+-----+----+---+-----+
|  1| one|null|   p1|   c| 11|   n1|
|  2| two|   b| null|null| 22|   n2|
+---+----+----+-----+----+---+-----+
{code}


{code:java}
val k = j.withColumn("note", coalesce(left("note"), right("note")))
k.show()
{code}


{code:java}
+---+----+----+-----+----+---+-----+
| id|name|note|note2|note|seq|note2|
+---+----+----+-----+----+---+-----+
|  1| one|   c|   p1|   c| 11|   n1|
|  2| two|   b| null|   b| 22|   n2|
+---+----+----+-----+----+---+-----+
{code}


{code:java}
val l = k.drop("note")
l.show()
{code}



{code:java}
+---+----+-----+---+-----+
| id|name|note2|seq|note2|
+---+----+-----+---+-----+
|  1| one|   p1| 11|   n1|
|  2| two| null| 22|   n2|
+---+----+-----+---+-----+
{code}







was (Author: yichuan):
withColumn have this strange behavior with join, it replace both columns from the left and right side of join, and replace them with the new column (Spark 2.0.2)

{code:java}
val left = Seq((1, "one", null, "p1"), (2, "two", "b", null)).toDF("id", "name", "note", "note2")
val right = Seq((1, "c", 11, "n1"), (2,  null, 22, "n2")).toDF("id",  "note", "seq", "note2")
val j = left.join(right, "id")
j.show()
{code}

{code:java}
+---+----+----+-----+----+---+-----+
| id|name|note|note2|note|seq|note2|
+---+----+----+-----+----+---+-----+
|  1| one|null|   p1|   c| 11|   n1|
|  2| two|   b| null|null| 22|   n2|
+---+----+----+-----+----+---+-----+
{code}


{code:java}
val k = j.withColumn("note", coalesce(left("note"), right("note")))
k.show()
{code}


{code:java}
+---+----+----+-----+----+---+-----+
| id|name|note|note2|note|seq|note2|
+---+----+----+-----+----+---+-----+
|  1| one|   c|   p1|   c| 11|   n1|
|  2| two|   b| null|   b| 22|   n2|
+---+----+----+-----+----+---+-----+
{code}


{code:java}
val l = k.drop("note")
l.show()
{code}



{code:java}
+---+----+-----+---+-----+
| id|name|note2|seq|note2|
+---+----+-----+---+-----+
|  1| one|   p1| 11|   n1|
|  2| two| null| 22|   n2|
+---+----+-----+---+-----+
{code}






> DataFrame.withColumn can create columns with identical names
> ------------------------------------------------------------
>
>                 Key: SPARK-6635
>                 URL: https://issues.apache.org/jira/browse/SPARK-6635
>             Project: Spark
>          Issue Type: Bug
>          Components: SQL
>    Affects Versions: 1.3.0
>            Reporter: Joseph K. Bradley
>            Assignee: Liang-Chi Hsieh
>             Fix For: 1.4.0
>
>
> DataFrame lets you create multiple columns with the same name, which causes problems when you try to refer to columns by name.
> Proposal: If a column is added to a DataFrame with a column of the same name, then the new column should replace the old column.
> {code}
> scala> val df = sc.parallelize(Array(1,2,3)).toDF("x")
> df: org.apache.spark.sql.DataFrame = [x: int]
> scala> val df3 = df.withColumn("x", df("x") + 1)
> df3: org.apache.spark.sql.DataFrame = [x: int, x: int]
> scala> df3.collect()
> res1: Array[org.apache.spark.sql.Row] = Array([1,2], [2,3], [3,4])
> scala> df3("x")
> org.apache.spark.sql.AnalysisException: Reference 'x' is ambiguous, could be: x, x.;
> 	at org.apache.spark.sql.catalyst.plans.logical.LogicalPlan.resolve(LogicalPlan.scala:216)
> 	at org.apache.spark.sql.catalyst.plans.logical.LogicalPlan.resolve(LogicalPlan.scala:121)
> 	at org.apache.spark.sql.DataFrame.resolve(DataFrame.scala:161)
> 	at org.apache.spark.sql.DataFrame.col(DataFrame.scala:436)
> 	at org.apache.spark.sql.DataFrame.apply(DataFrame.scala:426)
> 	at $iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC.<init>(<console>:26)
> 	at $iwC$$iwC$$iwC$$iwC$$iwC$$iwC$$iwC.<init>(<console>:31)
> 	at $iwC$$iwC$$iwC$$iwC$$iwC$$iwC.<init>(<console>:33)
> 	at $iwC$$iwC$$iwC$$iwC$$iwC.<init>(<console>:35)
> 	at $iwC$$iwC$$iwC$$iwC.<init>(<console>:37)
> 	at $iwC$$iwC$$iwC.<init>(<console>:39)
> 	at $iwC$$iwC.<init>(<console>:41)
> 	at $iwC.<init>(<console>:43)
> 	at <init>(<console>:45)
> 	at .<init>(<console>:49)
> 	at .<clinit>(<console>)
> 	at .<init>(<console>:7)
> 	at .<clinit>(<console>)
> 	at $print(<console>)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
> 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> 	at java.lang.reflect.Method.invoke(Method.java:606)
> 	at org.apache.spark.repl.SparkIMain$ReadEvalPrint.call(SparkIMain.scala:1065)
> 	at org.apache.spark.repl.SparkIMain$Request.loadAndRun(SparkIMain.scala:1338)
> 	at org.apache.spark.repl.SparkIMain.loadAndRunReq$1(SparkIMain.scala:840)
> 	at org.apache.spark.repl.SparkIMain.interpret(SparkIMain.scala:871)
> 	at org.apache.spark.repl.SparkIMain.interpret(SparkIMain.scala:819)
> 	at org.apache.spark.repl.SparkILoop.reallyInterpret$1(SparkILoop.scala:856)
> 	at org.apache.spark.repl.SparkILoop.interpretStartingWith(SparkILoop.scala:901)
> 	at org.apache.spark.repl.SparkILoop.command(SparkILoop.scala:813)
> 	at org.apache.spark.repl.SparkILoop.processLine$1(SparkILoop.scala:656)
> 	at org.apache.spark.repl.SparkILoop.innerLoop$1(SparkILoop.scala:664)
> 	at org.apache.spark.repl.SparkILoop.org$apache$spark$repl$SparkILoop$$loop(SparkILoop.scala:669)
> 	at org.apache.spark.repl.SparkILoop$$anonfun$org$apache$spark$repl$SparkILoop$$process$1.apply$mcZ$sp(SparkILoop.scala:996)
> 	at org.apache.spark.repl.SparkILoop$$anonfun$org$apache$spark$repl$SparkILoop$$process$1.apply(SparkILoop.scala:944)
> 	at org.apache.spark.repl.SparkILoop$$anonfun$org$apache$spark$repl$SparkILoop$$process$1.apply(SparkILoop.scala:944)
> 	at scala.tools.nsc.util.ScalaClassLoader$.savingContextLoader(ScalaClassLoader.scala:135)
> 	at org.apache.spark.repl.SparkILoop.org$apache$spark$repl$SparkILoop$$process(SparkILoop.scala:944)
> 	at org.apache.spark.repl.SparkILoop.process(SparkILoop.scala:1058)
> 	at org.apache.spark.repl.Main$.main(Main.scala:31)
> 	at org.apache.spark.repl.Main.main(Main.scala)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
> 	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
> 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
> 	at java.lang.reflect.Method.invoke(Method.java:606)
> 	at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:569)
> 	at org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:166)
> 	at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:189)
> 	at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:110)
> 	at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
> {code}



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

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