Q:

row level comparison of two tables

Hi I have a two tables like this.
source table 
orig1 orig2 orig3 xref1 xref2 xref3
1      1     1     2     2     2
1      1     1     3     3     3
23    23    23     12   12    12

target table:
orig1 orig2 orig3 xref1 xref2 xref3  version
1      1     1     1     1      1       0

I need output as follows
1) I need to match (source(orig1 orig2 orig3) == target(orig1 orig2 orig3)),
if its macthing we need to append from source to target table by increment the version by 1 
if its not matching, just append version as '0'
expected output is:
orig1 orig2 orig3 xref1 xref2 xref3  version
1      1     1     1     1      1       0
1      1     1     2     2      2       1
1      1     1     3     3      3       2
23    23    23     12   12     12       0

I tried with data frame level. But it is not working as expected. Any help would be appreciated.
I tried the following way.
val source = spark.sql("select xref1,xref2,xref3,orig1,orig2,orig3 from default.source")
val target = spark.sql("select xref1,xref2,xref3,orig1,orig2,orig3 from default.target")
val target10 = spark.sql("select xref1,xref2,xref3,orig1,orig2,orig3,version from default.target")
val diff=( source.select("xref1","xref2","xref3","orig1","orig2","orig3") == target.select("xref1","xref2","xref3","orig1","orig2","orig3"))
if  ( diff == false ){
  val diff1 = source.select("orig1","orig2","orig3").except(target.select("orig1","orig2","orig3"))
  if (  diff1.count > 0 ) {
   val ver = target10.groupBy("orig1","orig2","orig3").max("version")
   val common = source.select("orig1","orig2","orig3").intersect(target.select("orig1","orig2","orig3"))
   val result = common.join(ver, common("orig1") === ver("orig1") && common("orig2") === ver("orig2") && common("orig3") === ver("orig3"), "inner").select(ver("orig1"),ver("orig2"),ver("orig3"),(ver("max(version)") + 1
) as "version")
    val result1 = result.join(source, result("orig1") === source("orig1") && result("orig2") === source("orig2") && result("orig3") === source("orig3"), "inner").select(source("orig1"),source("orig2"),source("orig3"),result("version"),source("xref1"),source("xref2"),source("xref3"))
  val result2=source.select("orig1","orig2","orig3").except(target.select("orig1","orig2","orig3")).withColumn("version",lit(0))
  val execpettarget=result2.select($"orig1".alias("DIV"),$"orig2".alias("SEC"),$"orig3".alias("UN"),$"version".alias("VER"))
  val result23 = execpettarget.join(source, execpettarget("DIV") === source("orig1") && execpettarget("SEC") === source("orig2") && execpettarget("UN") === source("orig3"), "inner").select(source("orig1"),source("orig2"),source("orig3"),execpettarget("VER"),source("orig1"),source("orig2"),   source("orig3"))
   val final_result = result1.union(result23)
   final_result.show()
}else{
println("else")
val ver1 = target10.groupBy("orig1","orig2","orig3").max("version")
val common1 = source.select("orig1","orig2","orig3").intersect(target.select("orig1","orig2","orig3"))
val result11 = common1.join(ver1, common1("orig1") === ver1("orig1") && common1("orig2") === ver1("orig2") && common1("orig3") === ver1("orig3"), "inner").select(ver1("orig1"),ver1("orig2"),ver1("orig3"),(ver1("max(version)") + 1) as "version")
val result3 = result11.join(source, result11("orig1") === source("orig1") && result11("orig2") === source("orig2") && result11("orig3") === source("orig3"), "inner").select(source("orig1"),source("orig2"),source("orig3"),result11("version"),source("xref1"),source("xref2"),source("xref3"))
result3.show()
}}

But in the final join the source has 2 duplicate rows. So when joining source with target I am getting multiple rows.

A:

I didn't try to decipher your code, but based on source, target & expected result I would thing this could be a solution:
val w = Window.partitionBy('orig1,'orig2,'orig3).orderBy('version.desc)

val joined = source
  .withColumn("version", lit(null).cast(IntegerType))
  .union(target)
  .withColumn("version", row_number().over(w) + coalesce(max('version).over(w),lit(0)) - lit(1))

joined.show()

My idea was that a join doesn't make sense, because you want to end up with # records from source + # records from target: => union
After this union you want to process each group of similar keys (orig1, orig2, orig3) => Window
You care about the highest version number in the group, otherwise pick 0: => max & coalesce
You want to use this max as offet for a ranking of the rest of the window: => row_number
Based on the example this code will output:
+-----+-----+-----+-----+-----+-----+-------+
|orig1|orig2|orig3|xref1|xref2|xref3|version|
+-----+-----+-----+-----+-----+-----+-------+
|   23|   23|   23|   12|   12|   12|      0|
|    1|    1|    1|    1|    1|    1|      0|
|    1|    1|    1|    2|    2|    2|      1|
|    1|    1|    1|    3|    3|    3|      2|
+-----+-----+-----+-----+-----+-----+-------+

