How to resolve the algorithm Hash join step by step in the D programming language
How to resolve the algorithm Hash join step by step in the D 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 D programming language
Source code in the d programming language
import std.stdio, std.typecons;
auto hashJoin(size_t index1, size_t index2, T1, T2)
(in T1[] table1, in T2[] table2) pure /*nothrow*/ @safe
if (is(typeof(T1.init[index1]) == typeof(T2.init[index2]))) {
// Hash phase.
T1[][typeof(T1.init[index1])] h;
foreach (const s; table1)
h[s[index1]] ~= s;
// Join phase.
Tuple!(const T1, const T2)[] result;
foreach (const r; table2)
foreach (const s; h.get(r[index2], null)) // Not nothrow.
result ~= tuple(s, r);
return result;
}
void main() {
alias T = tuple;
immutable table1 = [T(27, "Jonah"),
T(18, "Alan"),
T(28, "Glory"),
T(18, "Popeye"),
T(28, "Alan")];
immutable table2 = [T("Jonah", "Whales"),
T("Jonah", "Spiders"),
T("Alan", "Ghosts"),
T("Alan", "Zombies"),
T("Glory", "Buffy")];
foreach (const row; hashJoin!(1, 0)(table1, table2))
writefln("(%s, %5s) (%5s, %7s)", row[0][], row[1][]);
}
You may also check:How to resolve the algorithm Unicode strings step by step in the Elixir programming language
You may also check:How to resolve the algorithm Random number generator (device) step by step in the Delphi programming language
You may also check:How to resolve the algorithm Burrows–Wheeler transform step by step in the Ksh programming language
You may also check:How to resolve the algorithm Sorting algorithms/Comb sort step by step in the Sidef programming language
You may also check:How to resolve the algorithm Euler's constant 0.5772... step by step in the Java programming language