Hash join: Difference between revisions
Content added Content deleted
(→{{header|Java}}: using map.getOrDefault is shorter) |
|||
Line 1,630: | Line 1,630: | ||
</pre> |
</pre> |
||
=={{header| |
=={{header|Ring}}== |
||
<lang ring>Table1 = [[27, "Jonah"], [18, "Alan"], [28, "Glory"], [18, "Popeye"], [28, "Alan"]] |
|||
<lang ruby>def hashJoin(table1, index1, table2, index2) |
|||
Table2 = [["Jonah", "Whales"], ["Jonah", "Spiders"], ["Alan", "Ghosts"], ["Alan", "Zombies"], ["Glory", "Buffy"]] |
|||
# hash phase |
|||
hTable = [] |
|||
h = table1.group_by {|s| s[index1]} |
|||
Qtable = [] |
|||
# join phase |
|||
table2.collect {|r| |
|||
h[r[index2]].collect {|s| [s, r]} |
|||
}.flatten(1) |
|||
end |
|||
for a in table1 |
|||
table1 = [[27, "Jonah"], |
|||
h = hashing(a[2]) |
|||
[18, "Alan"], |
|||
add(htable,[h , a]) |
|||
[28, "Glory"], |
|||
next |
|||
[18, "Popeye"], |
|||
[28, "Alan"]] |
|||
table2 = [["Jonah", "Whales"], |
|||
["Jonah", "Spiders"], |
|||
["Alan", "Ghosts"], |
|||
["Alan", "Zombies"], |
|||
⚫ | |||
for b in table2 |
|||
hashJoin(table1, 1, table2, 0).each { |row| p row }</lang> |
|||
h = hashing(b[1]) |
|||
for sh in htable |
|||
if sh[1] = h |
|||
add(qtable, sh[2] + b[2]) |
|||
ok |
|||
next |
|||
next |
|||
print(qtable) |
|||
#===============End of Execution========= |
|||
func print lst |
|||
see "--------------------------------------------------- |
|||
Age | Name || Name | Nemesis |
|||
--------------------------------------------------- |
|||
" |
|||
for l in lst |
|||
see string(l[1]) + char(9) + "| " + l[2] + copy(char(9),2) + "|| " + l[2] + " " + char(9) + "| " + l[3] + nl |
|||
next |
|||
func Hashing str |
|||
r = 0 |
|||
if len(str) > 4 |
|||
r = (ascii(str[1]) + ascii(str[len(str)]) + ascii(str[ceil(len(str) * 0.25)]) + ascii(str[ceil(len(str) * 0.75)])) |
|||
else |
|||
for s in str |
|||
r += ascii(s) |
|||
next |
|||
ok |
|||
return r</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
--------------------------------------------------- |
|||
[[27, "Jonah"], ["Jonah", "Whales"]] |
|||
Age | Name || Name | Nemesis |
|||
[[27, "Jonah"], ["Jonah", "Spiders"]] |
|||
--------------------------------------------------- |
|||
[[18, "Alan"], ["Alan", "Ghosts"]] |
|||
27 | Jonah || Jonah | Whales |
|||
[[28, "Alan"], ["Alan", "Ghosts"]] |
|||
27 | Jonah || Jonah | Whales |
|||
[[18, "Alan"], ["Alan", "Zombies"]] |
|||
18 | Alan || Alan | Ghosts |
|||
28 | Alan || Alan | Ghosts |
|||
[[28, "Glory"], ["Glory", "Buffy"]] |
|||
18 | Alan || Alan | Ghosts |
|||
28 | Alan || Alan | Ghosts |
|||
⚫ | |||
</pre> |
</pre> |
||