在Databricks中,array_union是否保持顺序

huangapple go评论65阅读模式
英文:

in databricks does array_union maintain order

问题

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->
MERGE INTO final_table a
USING 
(
select student_id,array_agg(distinct subject) new_subject
from changes_table b
group by 1,2
) b
on a.student_id =  b.student_id
WHEN MATCHED THEN
  UPDATE SET a.subject = array_union(array(new_subject),array(subject)),
WHEN NOT MATCHED
  THEN INSERT (student_id,subject) VALUES (b.student_id,new_subject)

<!-- end snippet 

<!-- begin snippet: js hide: false console: true babel: false -->
changed_table values = ["sql","python"] 
Final_table values = ["sql","scala"]

Result values ["sql", "python", "scala"]

The output that I am getting is correct. 

Question is that would it maintain the array_union maintain order in databricks ?
英文:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

MERGE INTO final_table a
USING 
(
select student_id,array_agg(distinct subject) new_subject
from changes_table b
group by 1,2
) b
on a.student_id =  b.student_id
WHEN MATCHED THEN
  UPDATE SET a.subject = array_union(array(new_subject),array(subject)),
WHEN NOT MATCHED
  THEN INSERT (student_id,subject) VALUES (b.student_id,new_subject)

<!-- end snippet

<!-- begin snippet: js hide: false console: true babel: false -->

changed_table values = [&quot;sql&quot;,&quot;python&quot;] 
Final_table values = [&quot;sql&quot;,&quot;scala&quot;]

Result values [&quot;sql&quot;, &quot;python&quot;, &quot;scala&quot;]

The output that I am getting is correct.

Question is that would it maintain the array_union maintain order in databricks ?

答案1

得分: 0

array_union是一个Spark函数,我们可以在GitHub上看到它的实现:这里

如果你仔细检查它,你会看到它是如何按顺序迭代两个数组并将唯一的条目添加到一个数组缓冲区中的,这个缓冲区最终成为最终结果。因此,它确实保留顺序。

英文:

array_union is a Spark function and we can see its implementation: here on GitHub.

If you examine it, you will see how it's iterating through both arrays in sequence and adding unique entries to an array buffer, which becomes the final result. Therefore yes, it does preserve order.

huangapple
  • 本文由 发表于 2023年2月24日 03:57:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/75549735.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定