Hash join: Difference between revisions

no edit summary
m (Dkf moved page Hash Join to Hash join: capitalisation policy)
No edit summary
Line 22:
 
Task: implement the Hash Join algorithm and show the result of joining two tables with it.
 
 
=={{header|Common Lisp}}==
<lang lisp>
;; Uses the same example as in Go
 
(defparameter *table-A* '((27 "Jonah") (18 "Alan") (28 "Glory") (18 "Popeye") (28 "Alan")))
 
(defparameter *table-B* '(("Jonah" "Whales") ("Jonah" "Spiders") ("Alan" "Ghosts") ("Alan" "Zombies") ("Glory" "Buffy")))
 
;; Hash phase
(defparameter *hash-table* (make-hash-table :test #'equal))
 
(loop for (i r) in *table-A*
for value = (gethash r *hash-table* (list nil)) do
(setf (gethash r *hash-table*) value)
(push (list i r) (first value)))
 
;; Join phase
(loop for (i r) in *table-B* do
(let ((val (car (gethash i *hash-table*))))
(loop for (a b) in val do
(format t "{~a ~a} {~a ~a}~%" a b i r))))
</lang>
 
{{out}}
<pre>
{27 Jonah} {Jonah Whales}
{27 Jonah} {Jonah Spiders}
{28 Alan} {Alan Ghosts}
{18 Alan} {Alan Ghosts}
{28 Alan} {Alan Zombies}
{18 Alan} {Alan Zombies}
{28 Glory} {Glory Buffy}
</pre>
 
=={{header|Go}}==