9.2 Reducing a product
In relational database theory, a join between tables can be thought of as a filtered product. For those who know SQL, the SELECT
statement joining tables without a WHERE
clause will produce a Cartesian product of rows in the tables. This can be thought of as the worst-case algorithm—a vast product without any useful filtering to pick the desired subset of results. We can implement this using the itertools.product()
function to enumerate all possible combinations and filter those to keep the few that match properly.
We can define a join()
function to join two iterable collections or generators, as shown in the following commands:
from collections.abc import Iterable, Iterator, Callable
from itertools import product
from typing import TypeVar
JTL = TypeVar("JTL")
JTR = TypeVar("JTR")
def join(
t1: Iterable[JTL],
t2: Iterable[JTR], ...