### Spark join产生错误的结果

``````scala> a.join(b, \$"filta" === \$"a" and \$"filtb" === \$"b" and a("c") === b("c"), "left_outer").show
16/03/21 12:50:00 WARN Column: Constructing trivially true equals predicate, 'c#18232 = c#18232'. Perhaps you need to use aliases.
+-----+-----+---+---+---+---+
|filta|filtb|  c|  a|  b|  c|
+-----+-----+---+---+---+---+
|    a|    b|  1|  a|  b|  1|
+-----+-----+---+---+---+---+
``````

``````scala> a.withColumn("newc", \$"c").join(b, \$"filta" === \$"a" and \$"filtb" === \$"b" and \$"newc" === b("c"), "left_outer").show
+-----+-----+---+----+---+---+---+
|filta|filtb|  c|newc|  a|  b|  c|
+-----+-----+---+----+---+---+---+
|    a|    b|  1|   1|  a|  b|  1|
|    a|    b|  1|   1|  a|  b|  2|
+-----+-----+---+----+---+---+---+
``````

`{{1}}`

#### 1 个答案:

``````val b = Seq(("a", "b", 1), ("a", "b", 2)).toDF("a", "b", "c")
val a = b.where("c = 1")

a.join(b, Seq("a", "b", "c")).show
// +---+---+---+
// |  a|  b|  c|
// +---+---+---+
// |  a|  b|  1|
// +---+---+---+
``````

``````val aa = a.alias("a")
val bb = b.alias("b")

aa.join(bb, \$"a.a" === \$"b.a" && \$"a.b" === \$"b.b" && \$"a.c" === \$"b.c")
``````

``````aa.join(bb, \$"a.a" <=> \$"b.a" && \$"a.b" <=> \$"b.b" && \$"a.c" <=> \$"b.c")
``````

``````val expr = \$"filta" === \$"a" and \$"filtb" === \$"b" and \$"newc" === \$"c"
a.withColumnRenamed("c", "newc").join(b, expr, "left_outer")
``````