Skip to content

Fix completeness: a second key no longer turns a provable pair unprovable - #12

Open
zvonimir-dd wants to merge 2 commits into
qed-solver:mainfrom
zvonimir-dd:fix-second-key-completeness
Open

zvonimir-dd wants to merge 2 commits into
qed-solver:mainfrom
zvonimir-dd:fix-second-key-completeness

Conversation

@zvonimir-dd

Copy link
Copy Markdown

Fixes #11: a second key on a table can turn a provable pair unprovable. Adding a key only narrows the instances, so that should never happen. There are two causes, one per commit.

1. min_subst picks the summation variable by column order

With keys {id} and {grp} on items(id, owner, grp), the Scan encoding makes each key's columns functions of the other key's: id = rpn!0-0-1(grp) and grp = rpn!0-1-0(id). min_subst then eliminates whichever of the two it reaches first. On the issue's pair, the RUST_LOG=info trace of upstream main shows the result:

  • JOIN side: id := item_id, and the sum ranges over the item id (rpk!0-0($2)).
  • EXISTS side: id := rpn!0-0-1(grp), and the sum ranges over grp (rpk!0-1($2)).

Unify<Term> matches summation variables only up to a permutation. At the same v, one summand is "the row whose id is v" and the other "the row whose grp is v", so the check fails.

Fix: the functions of every key after the first get their own prefix, rpu!. min_subst tries expressions through them only in a second pass, and only for the variables the first pass kept. Secondary keys still eliminate what the first key cannot: SELECT grp vs SELECT DISTINCT grp still proves. But they no longer decide which key a sum ranges over.

2. Every key made every column a function of it

A second key on an n-column table added up to n more equations per scan. All of them go to the get_implied_equalities call in stablize. On wide tables that call hits QED_SMT_TIMEOUT, nothing gets substituted, and the pair comes back unprovable with equiv_class_timed_out set. This is how the timeouts the issue mentions arise.

Fix: a key after the first now only determines the first key's columns that it lacks, and its predicate is dropped.

Why neither change can create a false proof

  • Commit 1: whichever substitution min_subst picks is justified by an implied equality under the summand's own condition: Σ_v [L(v)]·f(v) = [L(e)]·f(e) when L ⟹ v = e. Changing which justified substitution is picked cannot make a non-equivalent pair provable.
  • Commit 2: the new encoding admits exactly the same relations.
    • Every relation it admits satisfies all keys. Two rows equal on a later key agree on the first key's remaining columns, through rpu!. So they agree on the whole first key, and are the same row.
    • Every relation satisfying all keys is admitted: suitable rpu! functions always exist.

Single-key inputs are unaffected. Pass 2 accepts nothing without rpu! terms. Any candidate pass 1 rejected had the variable itself among its root dependencies, and still does. Commit 2 changes only keys after the first.

Testing

  • Issue repro: two_keys.json goes from false to true; one_key.json stays true.

  • 42 hand-written controls. They run over three key settings: one key, two keys, and two keys in reverse order. They cover:

    • the issue's pair, also with the columns permuted and with a composite second key;
    • the same pair joined on grp;
    • SELECT DISTINCT over a column that is unique only through the second key;
    • a self-join on that column, which can be eliminated;
    • two pairs that must stay unprovable: DISTINCT owner and a self-join on owner.

    Upstream main misses 5 equivalent pairs, all second-key cases. With both commits, all 42 come out as expected, with no false proofs and no missed proofs.

  • tests/ (1731 cases), upstream main vs this branch:

    • The verdicts are identical: 1234 provable on both.
    • The logged min_subst decisions ([dep]/[key], as a sorted multiset per case) are byte-identical on all 1705 cases where no table has two keys.
    • 6 of the 26 multi-key cases decide differently, and all 26 keep their verdicts.
    • testExpandProjectIn sometimes segfaults on both builds, with Z3 interrupted during the equivalence-class step; run alone it reproduces on upstream main too.
  • Real-world pairs: on the pairs behind the issue's "about a dozen proofs lost", most proofs come back with both commits, on the solver versions pinned here. With commit 1 alone only the non-timeout cases come back. A few cases over very wide tables still run past a 60 s budget.

  • Builds: nix build warnings are unchanged. I also measured both commits on Z3 5.1.0 / cvc5 1.4.1. The direction is the same there, but more of the wide-table cases stay at the time budget, because that Z3's get_implied_equalities is slower.

🤖 Generated with Claude Code

zvonimir-dd and others added 2 commits October 9, 2026 16:02
With two keys on a table, each key's columns are functions of the other's,
and min_subst eliminated whichever variable came first. An equivalent pair
could then sum over the first key on one side and the second on the other
(e.g. a JOIN on the item id against an EXISTS), which unification, matching
summation variables only up to a permutation, cannot relate: adding a key
turned a provable pair unprovable.

Give the functions of every key after the first their own prefix (rpu!) and
make min_subst try expressions through them only in a second pass, for the
variables the first pass kept. Secondary keys still eliminate variables the
first key cannot (SELECT DISTINCT over a unique column), but no longer
decide which key a sum ranges over.

Any choice min_subst makes is justified by an implied equality, so this
cannot make a non-equivalent pair provable. Without rpu! expressions the
second pass eliminates nothing: a candidate the first pass rejected has the
variable itself among its root dependencies, and still does. Single-key
inputs are therefore decided exactly as before.

Fixes qed-solver#11.

Environment: Datadog workspace

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Every key made every other column a function of its columns, so a second
key on a table of n columns added up to n more equations per scan, all
handed to the implied-equalities call in stablize. On wide tables that call
hit its timeout, nothing was substituted, and pairs provable with one key
came back unprovable.

The first key already determines every column. A later key only has to
determine the first key's columns not in it: then two rows equal on the
later key agree on the first key, and so are the same row, and any instance
satisfying all keys has such functions. Its predicate is implied too, so it
is dropped. A superkey of the first key now adds nothing.

Environment: Datadog workspace

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Completeness: a second key on a table turns a provable pair unprovable

1 participant