Draft

The Answers We Might Have to Write

Conversation 4.2 — Output Size and the AGM Bound

The answers we might have to write

Ada · 01

At the end of 4.1, a projected query had many witnesses for one answer. Now request the full tuple. The same witnesses become answers we must actually produce. To judge wasted work, we need to distinguish those necessary outputs from candidates that will be rejected.

pair(x, y, z) :- R(x, y), S(y, z).
R = {(1,0),(2,0)}
S = {(0,3),(0,4)}

Can this input require more answers than either individual relation has rows?

Alice

Yes. The answers are (1,0,3), (1,0,4), (2,0,3), and (2,0,4). Each left tuple combines with both right tuples.

Ada · 02

Give each relation $N$ tuples, still sharing only the middle value 0. How many full answers can this require?

Alice

Take

$$\begin{aligned} R&=\{(i,0):1\leq i\leq N\},\\ S&=\{(0,j):1\leq j\leq N\}. \end{aligned} $$

Every pair combines. There are $N^2$ distinct tuples (i,0,j). Writing them already takes that much work.

Ada · 03

Close the triangle with $T(x,z)$, also of size $N$. Can all $N^2$ tuples of that particular join still survive?

Alice

No. They have $N^2$ different (x,z) pairs, and $T$ has room for only $N$ of them.

But that only rules out this construction. Perhaps different relations could give more than $N$ answers.

Ada · 04

Try all pairs over a two-element domain:

$$R(x,y)=S(y,z)=T(x,z)=\{0,1\}^2. $$

Count each input and the full output.

Alice

Four tuples per input, and all eight triples in $\{0,1\}^3$ survive. So an $N$-answer bound is already wrong at $N=4$.

Ada · 05

Replace the two-element domain by a domain of size $m$.

Alice

Each input has $m^2$ tuples and the result has $m^3$. Writing $N=m^2$, the output size is $N^{3/2}$.

We have constructed that many answers. Have we proved that no instance can have more?

Ada · 06

Not yet. For each fixed $y$, let $d_S(y)$ count its possible $z$ values in $S$. For each fixed $x$, let $d_T(x)$ count its possible $z$ values in $T$. How many $z$ values can complete one (x,y) from $R$?

Alice

The common values of those two sets. At most $\min(d_S(y),d_T(x))$. Summing those limits over $R$ bounds the answer count, but still depends on the individual groups.

Ada · 07

We want a bound using the three relation sizes. One useful step is to replace that minimum by something that separates the two degrees. For nonnegative $p\leq q$, why is $p\leq\sqrt{pq}$?

Alice

Because $p^2\leq pq$. Swapping the names handles $q\leq p$, so $\min(p,q)\leq\sqrt p\sqrt q$ in either case.

The bound can be loose: sizes 1 and 100 give at most one common value, but the square-root product is 10. It remains an upper bound.

Ada · 08

Now there is one term $\sqrt{d_S(y)d_T(x)}$ for each tuple of $R$. The arithmetic tool Cauchy–Schwarz says $(\sum_{i=1}^n a_i)^2\leq n\sum_{i=1}^n a_i^2$. Substitute those terms for $a_i$ and $|R|$ for $n$. What remains to bound?

Alice
$$\begin{aligned} |R\bowtie S\bowtie T| &\leq\sum_{(x,y)\in R}\sqrt{d_S(y)d_T(x)}\\ &\leq\sqrt{|R|\sum_{(x,y)\in R}d_S(y)d_T(x)}. \end{aligned} $$

We have reduced the question to that last sum.

Ada · 09

What happens if we enlarge the sum to all pairs of occurring $x,y$ values, including pairs absent from $R$?

Alice

Its terms are nonnegative, so it can only grow. Then the sum factors:

$$\begin{gathered} \sum_{(x,y)\in R}d_S(y)d_T(x)\\ \leq \left(\sum_y d_S(y)\right) \left(\sum_x d_T(x)\right)\\ =|S||T|. \end{gathered} $$

Therefore

$$|R\bowtie S\bowtie T|\leq\sqrt{|R||S||T|}. $$

With all three sizes at most $N$, that gives $N^{3/2}$. Our square-domain construction reaches it when $N=m^2$.

Ada · 10

We now have bounds $|R||S|$ and $|R|^{1/2}|S|^{1/2}|T|^{1/2}$. Can the query's variable sets help us recognize which products are valid? First try using $|R|$ alone. Test

R = {(0,0)}
S = {(0,j): 1 <= j <= m}
T = {(0,j): 1 <= j <= m}
Alice

There are $m$ answers (0,0,j) while $|R|=1$. That proposed bound misses the choices for $z$, the variable absent from $R$.

Ada · 11

Return to complete-pair inputs, but allow the $x,y,z$ domains to have different sizes $X,Y,Z$. What are the relation sizes and output size?

Alice

$|R|=XY$, $|S|=YZ$, $|T|=XZ$, and the output size is $XYZ$.

Ada · 12

Write a proposed bound as $|R|^{w_R}|S|^{w_S}|T|^{w_T}$ with nonnegative exponents. Substitute these complete-pair sizes. How often does each domain-size factor appear?

Alice

The product becomes

$$X^{w_R+w_T}Y^{w_R+w_S}Z^{w_S+w_T}. $$

To bound $XYZ$ for arbitrary positive integer domain sizes, each of those exponents must be at least 1. Otherwise I can enlarge the corresponding domain while keeping the other two sizes at 1.

Ada · 13

Express those requirements using the query's three edges.

Alice

The requirements are

$$\begin{aligned} w_R+w_T&\geq1,\\ w_R+w_S&\geq1,\\ w_S+w_T&\geq1. \end{aligned} $$

Taking every weight to be $1/2$ satisfies all three. Multiplying the relation sizes to those powers gives our square-root bound.

Ada · 14

Would taking every weight to be $1/3$ give a better valid cover?

Alice

It would give a smaller expression, but each variable would receive only $2/3$. On our two-element cube, that product is $4^{1/3+1/3+1/3}=4$, although we listed eight answers. It is not a valid bound.

Adding the three requirements gives $2(w_R+w_S+w_T)\geq3$. Thus the weight sum cannot be below $3/2$.

Ada · 15

Does the triangle also admit the cover $(w_R,w_S,w_T)=(1,1,0)$?

Alice

Yes. $x$ is covered by $R$, $z$ by $S$, and $y$ by both. It gives the bound $|R||S|$, which we already had by considering the first binary join.

Ada · 16

Let $|R|=|S|=2$ and $|T|=100$. Which of those two covers gives the smaller bound?

Alice

The all-halves cover gives $\sqrt{2\cdot2\cdot100}=20$. The cover $(1,1,0)$ gives $2\cdot2=4$.

With unequal sizes, minimizing the sum of weights is not the same as minimizing the product of sizes to those weights.

Ada · 17

Have the complete-pair examples proved that every cover gives a bound on every possible input?

Alice

No. They explained why those constraints are necessary for this family. Our degree argument proved the all-halves triangle bound for arbitrary triangle data. Neither argument proves the general hypergraph theorem.

Ada · 18

That general step is the AGM theorem below. We will use its stated guarantee, and continue testing how its cover conditions behave on other queries. Its general proof is in the reading.

Alice

So the examples motivate the conditions; the theorem supplies the extension beyond the cases we have proved.

Ada · 19

Transfer the cover test to the path $R(x,y),S(y,z)$. Can either edge receive weight $1/2$?

Alice

No. $x$ appears only in $R$, forcing $w_R\geq1$; $z$ forces $w_S\geq1$. The bound is $|R||S|$.

That agrees with our first construction. The extra triangle constraint was doing real work in the smaller bound.

Ada · 20

Now use the four-variable Loomis–Whitney shape from the CQ slides:

R(b,c,d), S(a,c,d), T(a,b,d), U(a,b,c)

Each relation has at most $N$ tuples. Find a cover, and show its total weight cannot be smaller.

Alice

Each variable occurs in three edges, so weight $1/3$ per edge works. Adding all four variable constraints counts each edge three times:

$$3(w_R+w_S+w_T+w_U)\geq4. $$

The minimum sum is $4/3$, giving $N^{4/3}$. With a domain of size $m$, the complete three-column inputs have $N=m^3$ tuples each and the full output has $m^4$.

Ada · 21

If a triangle input has no answers, is its AGM bound zero?

Alice

No. The bound uses relation sizes and query structure. Different data with those same sizes may have many answers.

The actual output and the largest possible output are different quantities.

Ada · 22

Bring back 2.5's road instance:

$$road=\{(0,i),(i,0):1\leq i\leq k\}, $$

and the full query

round_trip(a,b,c) :- road(a,b), road(b,c), road(c,a).

What did its first binary join produce?

Alice

$k^2+k$ candidates, followed by no complete answers. Each atom has $N=2k$ tuples, and the three-way output bound is $N^{3/2}=(2k)^{3/2}$.

The binary intermediate grows quadratically even though the full output could never grow quadratically under those size bounds.

Ada · 23

If we pipeline those candidates without putting them in an array, is the work problem gone?

Alice

The large stored array may be gone. But if we still construct and test all $k^2+k$ candidates, that many candidate steps remain.

Changing pull to push does not, by itself, let the third atom constrain how those candidates are constructed.

Ada · 24

Have we obtained an algorithm merely by finding weights?

Alice

No. We know a target and have an example that defeats the plain binary construction. We still need a way to build tuples while consulting the constraints that the binary plan postponed.

Recap

Output and work

The output-size question fixes a query and input-size bounds, then asks how many full answers any permitted instance can have. Fractional edge covers provide AGM upper bounds; the triangle's equal-size bound is $N^{3/2}$, attained by complete pairs over a common domain of size $\sqrt N$ when that size is integral.

A particular instance may have far fewer answers, including none. An execution can also produce partial candidates larger than the bound on complete answers. Neither an output bound nor pipeline fusion alone prevents that work. Worst-case-optimal evaluation requires an algorithm whose candidate construction can be charged to the appropriate bound.