Skip to content

feat(retrieval): SIMILAR TO, similarity search by hypergraph edit distance - #122

Merged
venkat1701 merged 2 commits into
mainfrom
feature/similarity-search
Nov 23, 2026
Merged

venkat1701 merged 2 commits into
mainfrom
feature/similarity-search

Conversation

@venkat1701

Copy link
Copy Markdown
Collaborator

Stacked on #121, which it shares parser changes with. Merge #121 first, then retarget this to main.

SIMILAR TO finds groups of hyperedges that look like a query. A match can have the same shape with any participants, or, with IDENTITY, the same participants. The query is a pattern written like a HYPERMATCH pattern, a set of stored hyperedges, or the child hyperedges of a container.

similar = "SIMILAR" "TO" ( "(" hyperedge { "," hyperedge } ")" | "EDGES" "(" ref { "," ref } ")" | "CHILDREN" "OF" ref ) [ "IDENTITY" ]
          "IN" ( "CHILDREN" "OF" identifier
               | "AROUND" ref [ "SIZE" integer ] [ "THROUGH" identifier { "," identifier } ]
               | "SETS" "(" "(" ref { "," ref } ")" { "," "(" ref { "," ref } ")" } ")" )
          ( "WITHIN" number | "TOP" integer ) ;

The syntax is open for renaming before merge. Details are in docs/database/similarity.md.

Distance. Hypergraph edit distance with incidence edits. Each node or hyperedge inserted, deleted or retyped costs 1, and each membership added or removed costs 1, so a meeting missing one attendee is one or two edits away.

  • For each mapping of hyperedges, the node mapping is one assignment problem, solved exactly with a new Hungarian solver (retrieve/Assignment.java). Enumerating the hyperedge mappings with branch and bound makes the distance exact up to 5 hyperedges per side.
  • Larger candidates get an interval. Assignments over 512 nodes or hyperedges are never solved.
  • A lower bound from type counts, sizes and degrees rejects candidates before any of this.
  • IDENTITY fixes the atoms and is exact for any size.

Candidates

  • the children of every container of a type
  • connected sets of up to 6 hyperedges around an anchor
  • listed sets

Answers

  • WITHIN d returns everything within distance d.
  • TOP k visits candidates in order of their lower bound and stops once the bound passes the k-th distance.
  • Every row shows lower and upper; they are equal when the distance is exact.
  • The message says whether the whole answer is exact.

Testing

  • Full build: engine 269, database 278, server 41, all passing.
  • EditDistanceTest against a brute force over every node map and every hyperedge map, on 300 random pairs with type changes:
    • the exact distance, the cutoff behaviour, the lower and upper bounds, and the trivial upper bound
    • the identity distance against its own brute force
    • the Hungarian solver against all permutations
    • the design's worked example (distances 2, 2 and 7)
  • SimilarityTest: threshold and top-k over children and around-anchor spaces against the brute force; tie order; finding an exact copy at distance 0; a 3,000-member candidate; budget stops keeping partial answers; never claiming exactness wrongly.
  • Mutation checks: each of these breaks a test:
    • kept incidences counted once
    • a lower bound that is too high
    • over-pruning the hyperedge search
    • a wrong identity node cost
    • top-k dropping ties
    • pruning around an anchor too early
    • the threshold filter dropping the boundary
    • removing the assignment cap
  • SimilarStatementTest: the worked example end to end, and every invalid form.
  • Security: SecurityTest#similaritySearchStaysInsideTheTenant plants cross-tenant children and members. Without the member filter the test fails.
  • Live, over the wire, two tenants: 50/50 SIMILAR TO checks per tenant agree with a Python brute force, including identity mode. Results are identical after a SIGKILL and recovery, and all 3 cross-tenant SIMILAR TO probes are refused. The earlier statements, rechecked in the same run, are also unchanged.

Independent review. One critical issue: an unbounded assignment that could exhaust memory on a huge candidate. Also four important ones:

  • claiming exactness after dropping an interval
  • returning nothing when the budget ran out
  • unmetered repeated member reads
  • a wasted upper-bound solve

All are fixed, and each has a test that failed first except the exactness claim, which random data never triggered and which now has a guard test.

Not done here

  • Attribute, role and embedding costs.
  • Text-seeded candidates.
  • The motif and star lower bounds.
  • Parallel verification.

…tance

SIMILAR TO finds groups of hyperedges close to a query: a pattern written
as in HYPERMATCH, a set of stored hyperedges, or the children of a
container. Candidates are the children of every container of a type,
connected sets of up to six hyperedges around an anchor, or listed sets.
The distance is hypergraph edit distance with incidence edits. For each
mapping of hyperedges, the node mapping is solved exactly as an
assignment problem, which makes the distance exact up to five hyperedges
per side; larger candidates get an interval. A lower bound from type
counts, sizes and degrees rejects candidates first. WITHIN returns every
candidate within a distance and TOP the closest, stopping once the lower
bounds pass the k-th distance. IDENTITY keeps atoms fixed and is exact
for any size. Only the reader's hyperedges and visible members count.

No issue tracks this work.
An assignment over more than 512 nodes or hyperedges is no longer solved;
such a candidate gets the cost of deleting the query and inserting it as
its upper bound, and every solve is charged by its size. Finding and
bounding candidates and verifying them each get half of the budget, and a
budget stop keeps what was done. TOP no longer reports an exact answer
after dropping a candidate whose interval could have ranked higher.
Member reads are cached for the search and charged, ties include the
candidate, repeated listed sets are compared once, empty sets and empty
example containers are refused, and THROUGH must name hyperedge types.

No issue tracks this work.
@venkat1701
venkat1701 changed the base branch from feature/motif-census to main November 23, 2026 19:08
@venkat1701
venkat1701 merged commit 10b67b3 into main Nov 23, 2026
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.

1 participant