Skip to content

contextdb: add joins, grouping, UNION, subqueries, graph patterns, ranked search and EXPLAIN to SELECT - #25

Merged
lucidprogrammer merged 2 commits into
devfrom
v2-query-language
Nov 15, 2026
Merged

lucidprogrammer merged 2 commits into
devfrom
v2-query-language

Conversation

@lucidprogrammer

Copy link
Copy Markdown
Contributor

The new engine under v2/ can now read across tables, walk a table of edges, rank a similarity search, and explain any statement without running it.

What this adds

  • SELECT joins tables (INNER, LEFT and CROSS JOIN). It groups with COUNT, SUM, MIN, MAX and AVG, GROUP BY and HAVING. It also takes DISTINCT, WITH, UNION and UNION ALL, IN and one-value subqueries that do not read the outer row, LIKE, BETWEEN, ORDER BY any expression, LIMIT and OFFSET, and ROW_NUMBER() OVER (...).
  • GRAPH_TABLE(edges MATCH (a)-[:TYPE]->{m,n}(b) COLUMNS (...)) is a pattern over a table of edges. With a range, it reports each node once at the shallowest depth it is reached, whether the filter names no start, one start or several. A walk follows at most 10 edges.
  • A JSON column is read with -> and ->>. JSON values compare by value and sort as Postgres jsonb sorts them. An index over a JSON field answers a condition on that field.
  • ORDER BY ... USE RANK name ranks a similarity search by a formula the column declares.
  • EXPLAIN of any SELECT, INSERT, UPDATE or DELETE says how the statement would be answered and changes nothing. It refuses a statement exactly as running it would refuse it for what is wrong in the statement itself.
  • Sixteen names of the vector clauses and their SHOW columns are now the words the industry uses. AUTO_INDEX_AT is INDEXING_THRESHOLD and CONSOLIDATION is COMPACTION. Their declared_* and effective_* columns follow, and so do three error names. pending_inserts is pending_tuples, first_grouping is unindexed and outside_grouping is unindexed_rows.
  • Names written without quotes are folded to lower case, as in Postgres. A whole number and a REAL are compared by exact value.

What a narrowed reader is told, and what it is never told

A narrowed reader is a handle narrowed to some Contexts, some scope labels or a principal.

  • A list read leaves hidden rows out and says nothing of them. A SELECT that names one hidden row by its key is refused by the rule that hid it, so the reader can tell "not yours" from "not there". The refusal never carries a value of that row or a count.
  • A GRAPH_TABLE that names one hidden start with = is refused the same way. A list of starts leaves the hidden one out, and NULL names no start.
  • An UPDATE or DELETE changes no row the reader cannot see, and says so. A write that would put a row outside what the reader may write is refused.
  • An index-only search (USE VECTOR INDEXED) by a narrowed reader is answered as an ordinary search (AUTO). It is never refused because of the state of the index.
  • Its EXPLAIN of a search prints only the declaration lines, filter_path and recovery. It prints nothing about routes, partitions, small_set or a refusal.
  • Both rules exist so that nothing the reader sees depends on data it may not see. The unnarrowed reader keeps the refusal and the full EXPLAIN.
  • Not covered by these rules: the time a statement takes, and the work counts on a result (rows_examined, distance_evaluations). Both depend on rows the reader may not see.

The report line

Shipped lines Tests Test lines The whole check on this tree
24,994 (limit 25,000) 391 (limit 400) 40,999 (limit 41,000) 3 minutes 57 seconds

The time is the six commands listed below, run one after another on the build machine described under "Speed", with the dependencies already compiled.

Checks run

  • On this tree, locally, the commands of the new engine's workflow (check.yml):
    • format, lint and documentation tests clean;
    • 343 of 343 tests passed; the one million-row load is skipped and left to the nightly run;
    • the same tests under cargo test: 351 passed, none failed;
    • the storage library's own tests: 199 passed, none failed.
  • Not run locally: the check on the oldest supported toolchain (Rust 1.89) and the macOS leg.
  • The new engine's workflow as written passed in 2.0 minutes in a container shaped like the hosted Linux runner, on this tree. The macOS leg does not run there.
  • The old engine's workflow (ci.yml) also runs on this pull request, because the change touches files outside v2/ (docs/performance/ and scripts/ci-local.py). It was run locally once: about 245 of its commands ran with no failure before its time limit stopped the run. It is proven by the hosted run.
  • The hosted run on this pull request is the proof of both workflows on the hosted runners.
  • The pages were checked against the code:
    • sentence by sentence: 44 changed sentences, 3 of them false, now corrected;
    • by a reader given the guide alone;
    • read cold by four kinds of reader.
      The false sentences, the two contradictions in the guide and the two findings that blocked a reader are fixed in this change.

Speed against the merged code

How the figures were taken:

  • Both sides ran on one build machine with 16 cores and 59 GiB of memory, each with the machine to itself. This is a build machine, not a user's machine.
  • Both are release builds of the measuring programs that v2/numbers.sh runs, on the same stores loaded the same way. A search runs on one thread, reading under a 1 GiB memory limit.
  • Each side was taken five times, and the median is shown.
  • The merged-code side was taken a few hours before the new side, on the same idle machine with the same programs and stores. Only the new side was taken again.
  • The new side was measured on a tree one line of engine code before this one; that line changes only the text EXPLAIN prints. The tests and pages changed after it do not enter the measuring programs.

What each store holds and how it was loaded:

  • 10,000 passages: the first 10,000 real search passages (vectors of 768 numbers) of one partition of a recorded dump, loaded and then sealed.
  • 11,588, 100,008 and 165,855 passages: three recorded dumps of real search passages, each loaded whole and then sealed.
  • 1,000,000 sightings: synthetic vectors of 768 numbers in tracks of ten, in one partition, sealed and grouped.
  • Context Graph's declared tables: the 100,008-passage dump written into its passages table with the rows they refer to, with its 13 indexes, 500 passages to a commit. The declared find and the kept count run on this store.
  • Context Graph's anchors: 62,447 recorded commands with the files and folders they name, 138,084 anchor rows, with 15 indexes, 500 events and their anchors to a commit. The keyed lookup runs on this store.

This change beside the merged code (the change before it), medians:

Merged code This change
Context Graph's declared find, one / several / all Contexts, 95th percentile 1.416 / 3.806 / 4.007 ms 1.462 / 3.913 / 4.136 ms
Ten nearest of 100,008 passages, one / all partitions, automatic, 95th percentile 1.397 / 3.979 ms 1.411 / 4.027 ms
Ten nearest of 1,000,000 sightings, indexed, 95th percentile 31.044 ms 30.577 ms
Keyed lookup of one anchor, median 0.029 ms 0.031 ms
Kept count, median 0.013 ms 0.014 ms
100,008 passages loaded 3.7 s 3.5 s
62,447 commands loaded 13.6 s 12.6 s
  • Of 105 figures, 88 are the same, 13 are slower and 4 are faster. "The same" means the medians are within 5%, or the new median lies inside the range of the merged code's five.
  • The slower ones, median before and after:
    • searches on stores of 10,000 to 11,588 passages, 5.1 to 6.3% slower (for example 0.383 to 0.405 ms);
    • the kept count: 0.013 to 0.014 ms;
    • the keyed lookup: 0.029 to 0.031 ms;
    • a search over 10,000 vectors not yet sealed: 24.363 to 26.333 ms.
  • Causes:
    • The keyed lookup and the kept count are slower because the larger language is parsed on each call.
    • The searches are slower mostly because the larger program runs the same instructions less efficiently, and in part because of the parse.
    • A small part of the slower search over unsealed vectors sits in storage code this change did not touch. It is not fully explained, and it is within the measuring error.
  • The declared find holds its bars at the 95th percentile: 1.462 ms against 1.50, 3.913 against 4.02 and 4.136 against 4.33.
  • Not taken again for this change, as the performance pages say:
    • the search over 1,000,000 synthetic vectors and over 5,000,000 vectors;
    • the loads of 1,000,000 small rows and of 1,000,000 synthetic vectors;
    • the search while vectors wait to be sealed;
    • the small store rewritten by a few large commits.
  • Every figure, with its machine, date and previous figure, is in the repository's performance pages (docs/performance/).

Old engine against this one, statement by statement

1,583 statements of the 47 SQL scripts of the new engine's tests, run through the old engine's shell and through this one, each script on a fresh store for each engine, as they stood one line of engine code before this tree: 1,105 answer the same, 132 are the new form (a form the old engine refuses), 211 differ on purpose, each recorded as an intended difference from the old engine, 71 follow from those, 41 differ only in wording, 23 are old-engine defects this change corrects, 0 are defects of this change, and 0 differ without such a record.

Known limits

  • Every statement is parsed on each call. This costs about 1.4 microseconds more than before on a keyed find and 2.0 on a kept count; prepared statements would remove it.
  • A filter of key ranges joined by OR reads every row, as it did before this change.
  • On a partition shared by several Contexts, an approximate search can return a different set of near neighbours as other data changes the index.
  • A subquery in FROM is not supported; the statement does not parse. Name the subquery in a WITH and read that name instead.
  • EXPLAIN of a statement whose WITH body is a similarity search, such as EXPLAIN WITH x AS (<search>) SELECT * FROM x, is refused with the PlanError "explaining this statement is not supported in this version of the library", because a search inside a WITH prints no line of its own.
  • The work counters returned with a result (rows_examined, distance_evaluations) count every row read, including rows a narrowed reader may not see, so a host serving such a reader should not pass them on.
  • A statement nests at most 64 levels of parentheses, NOT, unary minus and subqueries; one deeper is a ParseError.
  • Some behaviours change only the work done, never an answer, so no test pins them:
    • the memory of a subquery's list or a WITH name is handed back when it is no longer needed;
    • a join gathers its inner filter list once;
    • a NULL in a graph start list starts no walk.

@lucidprogrammer
lucidprogrammer merged commit 53a6356 into dev Nov 15, 2026
223 of 226 checks passed
@lucidprogrammer
lucidprogrammer deleted the v2-query-language branch November 15, 2026 08:01
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