How to resolve the algorithm Hash join step by step in the Nim programming language
How to resolve the algorithm Hash join step by step in the Nim programming language
Table of Contents
Problem Statement
An inner join is an operation that combines two data tables into one table, based on matching column values. The simplest way of implementing this operation is the nested loop join algorithm, but a more scalable alternative is the hash join algorithm. Implement the "hash join" algorithm, and demonstrate that it passes the test-case listed below. You should represent the tables as data structures that feel natural in your programming language. The "hash join" algorithm consists of two steps:
In pseudo-code, the algorithm could be expressed as follows: The order of the rows in the output table is not significant. If you're using numerically indexed arrays to represent table rows (rather than referring to columns by name), you could represent the output rows in the form [[27, "Jonah"], ["Jonah", "Whales"]].
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Hash join step by step in the Nim programming language
Source code in the nim programming language
import strformat, tables
type
Data1 = tuple[value: int; key: string]
Data2 = tuple[key: string; value: string]
proc `$`(d: Data1 | Data2): string = &"({d[0]}, {d[1]})"
iterator hashJoin(table1: openArray[Data1]; table2: openArray[Data2]): tuple[a: Data1; b: Data2] =
# Hash phase.
var h: Table[string, seq[Data1]]
for s in table1:
h.mgetOrPut(s.key, @[]).add(s)
# Join phase.
for r in table2:
for s in h[r.key]:
yield (s, r)
let table1 = [(27, "Jonah"),
(18, "Alan"),
(28, "Glory"),
(18, "Popeye"),
(28, "Alan")]
let table2 = [("Jonah", "Whales"),
("Jonah", "Spiders"),
("Alan", "Ghosts"),
("Alan", "Zombies"),
("Glory", "Buffy")]
for row in hashJoin(table1, table2):
echo row.a, " ", row.b
You may also check:How to resolve the algorithm Identity matrix step by step in the Clojure programming language
You may also check:How to resolve the algorithm Unicode variable names step by step in the PHP programming language
You may also check:How to resolve the algorithm Minimum positive multiple in base 10 using only 0 and 1 step by step in the Pascal programming language
You may also check:How to resolve the algorithm Entropy/Narcissist step by step in the Tcl programming language
You may also check:How to resolve the algorithm Number reversal game step by step in the PowerShell programming language