Draft

The Variables the Plan Still Needed

Conversation 2.4 — Matching, Forgetting, and Filtering

The variables the plan still needed

Ada · 01

Let us follow the columns through a plan. Return to our delivery query, without the day:

eligible(driver, job) :-
    based_at(driver, depot),
    stocks(depot, item),
    needs(job, item).

Call the body occurrences $D,S,N$ as before.

The eligible query hypergraph: D connects driver and depot, S connects depot and item, and N connects item and job.

Start with $D$, then join $S$. Which variable must match? Which is new?

Alice

depot must match. item is new.

The query graph above the intermediate columns driver and depot; depot is highlighted as the match required by S.

The result has driver,depot,item.

Ada · 02

Instead start with $D\times N$, then join $S$. Same query, same stock atom. What must $S$ check now?

Alice

Now both depot and item are already present. $S$ must match them together and adds no new column.

The same query graph above driver depot job item; depot and item are highlighted together as S's required match.
Ada · 03

Suppose a product candidate is

driverdepotjobitem
Linnorthjob3nut

and $S$ contains only

depotitem
northbolt
southnut

Does the candidate survive?

Alice

No. Neither stock tuple matches the whole pair (north,nut).

Both values must come from one tuple, as with the dated stock in 2.3.

Ada · 04

A join can extend a candidate or only filter it. Which it does depends on the columns already present.

Alice

So $S$ did not change; the partial result did. Could $S$ have to match three columns?

Ada · 05

Yes—even when the columns came from different atoms. Here, available(driver,day) records a driver's available days:

ready_on(driver, job, day) :-
    based_at(driver, depot),
    needs(job, item),
    available(driver, day),
    stocks_on(depot, item, day).

Use $A$ for available and $D,N,S$ for the other occurrences. Combine the first three. What must the final stock atom match? What can it add?

Alice

It matches the entire set {depot,item,day} and adds nothing.

The ready_on query hypergraph: the stock group overlaps based_at on depot, needs on item, and available on day.

depot arrived through based_at, item through needs, and day through available.

Ada · 06

Suppose those earlier atoms produced

driverdepotjobitemday
Linnorthjob1boltTue

Use the dated stock rows from 2.3:

depotitemday
northboltMon
northnutTue
southboltTue

Keep or discard this candidate?

Alice

Discard it. The required triple (north,bolt,Tue) is absent.

The pairwise witnesses still do not supply one matching stock tuple.

Ada · 07

Return to the query without the day or availability test:

eligible(driver, job) :-
    based_at(driver, depot),
    stocks(depot, item),
    needs(job, item).

We have computed $D\bowtie S$, with columns driver,depot,item. Only $N$ remains.

Who still needs item? Who still needs driver?

Alice

$N$ needs item to check the job's requirement. The head needs driver to report the answer.

Ada · 08

Who still needs depot?

Alice

Neither the remaining atom nor the head.

The join has already checked the depot connection. We can keep just driver,item.

The eligible query graph stays fixed above the intermediate schema changing from driver depot item to driver item.
Ada · 09

Would this work even if two depots connected the same driver and item?

driverdepotitem
Linnorthbolt
Linsouthbolt

What does projection onto driver,item return?

Alice

One tuple, (Lin,bolt), under set semantics.

$N$ can inspect only the item, and the head returns only driver and job. Two depot witnesses would not create two distinct answers.

Ada · 10

Then why not discard depot immediately after reading $D$? Use this small instance of the same query:

$D$: driverdepot
Linnorth
$S$: depotitem
southnut
$N$: jobitem
job3nut

First, what is the correct answer?

Alice

Empty. Lin's depot is north, but the only stock tuple is at south. The $D$–$S$ match fails.

Ada · 11

Now replace $D$ by $\pi_{driver}(D)$ before joining $S$. What happens?

Alice

The depot column that carried the required agreement is gone. driver and depot,item have no shared column, so the first combination admits (Lin,south,nut).

Joining $N$ then produces the false answer (Lin,job3).

We could discard depot after its last test, but not before that test.

Ada · 12

After the valid $D\bowtie S$ step, could we discard driver too? No remaining body atom uses it.

Alice

The head still does. If we discard it, we cannot report which driver can do the job.

So the head counts as a remaining use too.

Ada · 13

Yes. Suppose a completed part of the body is $C$. Who might still need one of its variables?

Alice

An unprocessed atom, the head, or a condition we have not tested yet.

Ada · 14

Check the other starting pair in eligible: complete $S\bowtie N$ first. It has depot,item,job. What can disappear?

Alice

item. The remaining $D$ needs depot, and the head needs job. Keep {depot,job}.

Ada · 15

Return once more to ready_on. Its final stock atom has just checked all three shared columns. What does the completed body retain for the head?

Alice

driver,job,day. Both depot and item can now disappear.

The stock test needed them; the head does not.

Ada · 16

Does discarding an intermediate column erase its variable from the original query graph?

Alice

No. Its constraints stay in the query. The intermediate carries fewer columns because some tests are finished.

How much does that save in a plan?

Ada · 17

Let us measure the effect. Use the first instance from 2.1 again:

$D$: driverdepot
Linnorth
Moenorth
Niasouth
Omarsouth
$S$: depotitem
northbolt
southnut
$N$: jobitem
job1bolt
job2bolt
job3nut
job4nut

Starting with $D\bowtie S$, how many rows and columns do we get before and after the safe projection?

Alice

Before: four rows and three columns. After: four rows and two columns.

The four driver–item pairs are distinct, so this projection reduces columns without reducing rows.

driveritem
Linbolt
Moebolt
Nianut
Omarnut
Ada · 18

Continue that plan through $N$, then project to the head. Annotate every step with both its row count and its schema.

Alice

Each driver–item pair matches two jobs.

A plan sequence: D join S gives 4 rows and three columns; projection keeps 4 rows and driver item; joining N gives 8 rows and three columns; head projection keeps 8 rows and driver job.
An evaluation sequence, not a query graph.
Ada · 19

Try product first. After $D\times N$, may we discard either depot or item before joining $S$?

Alice

No. $S$ still needs both, while the head needs driver,job. All four columns must stay.

Product-first evaluation: D times N gives 16 rows with four columns; joining S gives 8 rows with four columns; head projection gives 8 rows with two columns.
Ada · 20

Count columns immediately after each displayed join or projection. What is the largest intermediate arity in each plan?

Alice

Three in the driver-first plan, four in the product-first plan.

Without early projection, the complete body would have all four columns in both orders. The measurement depends on where the projections occur.

Ada · 21

Now keep only answers whose job is job1. Keep the same head columns driver,job.

Apply $N'=\sigma_{job=job1}(N)$. What changed: rows, columns, or both?

Alice

Rows. $N'$ contains just (job1,bolt), but still has the two columns job,item.

The selected relation has the same schema.

Ada · 22

The condition uses only job, which is already available in $N$. A rejected $N$ tuple could not contribute to a selected answer, so we can test it here.

What does $S\bowtie N'$ contain?

Alice

One tuple:

depotitemjob
northboltjob1
Ada · 23

Which column can disappear now? Then how many drivers match?

Alice

Discard item, keeping depot,job. $D$ matches Lin and Moe at north, giving the two answers (Lin,job1) and (Moe,job1).

Ada · 24

Could a semijoin communicate the same restriction to the stock input? Compute $S\ltimes N'$. What is its schema?

Alice

It keeps the stock tuples having a partner in $N'$:

depotitem
northbolt

The schema is still $S$'s depot,item. The semijoin filters the left relation; it does not bring job into it.

Ada · 25

Use unrestricted $N$ again. North's bolt matches two jobs. How many copies of (north,bolt) does $S\ltimes N$ contain?

Alice

One. Existence of a partner is enough. In this instance $S\ltimes N$ has two stock tuples, whereas $S\bowtie N$ has four depot–item–job tuples.

If we need the jobs, the semijoin alone does not provide them.

Ada · 26

We have reduced columns with projection and rows with filtering. What did your three-versus-four comparison measure?

Alice

Columns in the intermediate results. Selecting job1 reduced rows instead. We should name those quantities separately.

Ada · 27

Does selecting job1 change the variable groups in our query hypergraph?

Alice

No. It changes which tuples pass, while the variables and overlaps stay the same. What if we write a constant inside an atom instead?

Ada · 28

Then that occurrence is restricted by the constant. Try:

from_logan(to) :-
    road("Logan", via),
    road(via, to).

Use the SIP convention from 2.2: mark a vertex containing a constant with a small filled dot. Call the occurrences $E_1,E_2$.

Alice

The first vertex is marked. The shared variable is still via.

Two SIP vertices share via; E1 carries a filled constant mark for Logan.

The literal selects tuples of the first occurrence. It is not a query-variable vertex when we open the atoms into a hypergraph.

Ada · 29

We can now match a whole shared set, filter tuples, and retain only needed columns. Is every surviving tuple therefore part of a complete answer?

Alice

Not yet. We have checked the constraints processed so far. A remaining constraint can still reject it.

But what if it has a partner in every other atom? Would that be enough?