Reducing a product
In relational database theory, a join between tables can be thought of as a filtered product. A SQL SELECT
statement that joins 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 product without any filtering to pick the proper 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:
JT_ = TypeVar("JT_") def join( t1: Iterable[JT_], t2: Iterable[JT_], where: Callable[[Tuple[JT_, JT_]], bool] ) -> Iterable[Tuple[JT_, JT_]]: return filter(where, product(t1, t2))
All combinations of the two iterables, t1
and t2
, are computed. The filter()
function will apply the given where()
function to pass or reject two-tuples, hinted as Tuple[JT_, JT_...