  Recently, Gottlob, Lee, Valiant, and Valiant (GLVV) presented an output size
bound for join queries with functional dependencies (FD), based on a linear
program on polymatroids. GLVV bound strictly generalizes the bound of Atserias,
Grohe and Marx (AGM) for queries with no FD, in which case there are known
algorithms running within AGM bound and thus are worst-case optimal.
  A main result of this paper is an algorithm for computing join queries with
FDs, running within GLVV bound up to a poly-log factor. In particular, our
algorithm is worst-case optimal for any query where the GLVV bound is tight. As
an unexpected by-product, our algorithm manages to solve a harder problem,
where (some) input relations may have prescribed maximum degree bounds, of
which both functional dependencies and cardinality bounds are special cases.
  We extend Gottlob et al. framework by replacing all variable subsets with the
lattice of closed sets (under the given FDs). This gives us new insights into
the structure of the worst-case bound and worst-case instances. While it is
still open whether GLVV bound is tight in general, we show that it is tight on
distributive lattices and some other simple lattices. Distributive lattices
capture a strict superset of queries with no FD and with simple FDs. We also
present two simpler algorithms which are also worst-case optimal on
distributive lattices within a single-$\log$ factor, but they do not match GLVV
bound on a general lattice. Our algorithms are designed based on a novel
principle: we turn a proof of a polymatroid-based output size bound into an
algorithm.
