Skip to content

feat: match Variables inside Quad pattern terms - #637

Draft
jeswr wants to merge 5 commits into
rdfjs:mainfrom
jeswr:feat/quad-pattern-matching
Draft

feat: match Variables inside Quad pattern terms#637
jeswr wants to merge 5 commits into
rdfjs:mainfrom
jeswr:feat/quad-pattern-matching

Conversation

@jeswr

@jeswr jeswr commented Jul 2, 2026

Copy link
Copy Markdown
Member

Adds structural matching for Quad pattern terms containing Variable components, on top of the component-id triple-term index of #635: match/getQuads/countQuads/has and friends can now answer ?r rdf:reifies <<( :s ?p ?o )>>-style queries from the index instead of a full scan, in any pattern slot that can hold a triple term (subject, object, graph) and at any nesting depth.

Semantics (aligned with rdf-stores, so existing Comunica consumers understand them): concrete components must be term-equal; a Variable matches anything in its position; nested Quad components recurse; the same variable twice acts as independent wildcards (no join — that stays with #281); variable-free Quad patterns keep the exact single-id path; top-level Variable arguments stay non-matching; a pattern built with DataFactory.quad(s, p, o) matches default-graph triple terms, and a Variable graph component also matches API-only graph-component terms.

Implementation: wildcard-bearing slots expand into candidate term ids enumerated from the _quadIds index and loop the existing _findInIndex/_countInIndex/indexMatch machinery with the numeric ids as fixed keys; variable-free patterns and non-Quad slots take the previous code paths unchanged. Advertising features.quotedTripleFiltering to Comunica is deferred to a follow-up (it belongs with an integration test).

Stacked on #635, rebased directly onto current main without #601's cross-entity-index set-operation changes. The branch now contains the focused triple-term component-id index plus #637's structural matching, documentation, and array-pattern refactor.

Performance (M1, Node 25, interleaved A/B fresh-process runs vs the #635 tip 72c8ffd; workload per the #633 analysis, 100 k / 1 M quads): the wildcard query <<( :s0 ?p ?o )>> with 100 hits answers in 183 µs / 184 µs — O(matches), size-independent — vs a 69 ms / 850 ms full-scan workaround (~375× / ~4 600×); the low-selectivity <<( ?s :p3 ?o )>> (N/16 hits) is materialization-bound at 15.6 ms / 168 ms (~5× vs scan, ~3.5–4× faster than the design memo's prototype). Ingest, heap (byte-identical) and the variable-free exact-match paths are unchanged within noise (the variable-free paths are unchanged code). Full npm test (6 573 tests, 100 % coverage) and the RDF 1.2 syntax suites pass.

Open questions (from the design memo, for sign-off):

  1. Top-level Variable arguments: kept non-matching (0 results, as today, pinned in a test). Aligning with rdf-stores (wildcards) would be more consistent but changes observable behavior of existing calls — separate decision.
  2. forSubjects/forPredicates/forObjects: scope-cut to exact-only for Quad patterns in this PR (documented); the candidate loop can be extended there too if wanted.

Closes #633
Refs rdfjs/types#68

cc @jeswr

Comment thread README.md Outdated
Comment thread test/N3Store-test.js
@jeswr

jeswr commented Jul 3, 2026

Copy link
Copy Markdown
Member Author

As a separate PR it would make sense to introduce something like matchPattern support where a set of quads are provided which can include variable terms - representing a BGP pattern; and the set of variable bindings matched over the store are returned.

@jeswr jeswr added the ai-generated Authored or prepared by an AI coding agent label Jul 9, 2026
@jeswr jeswr added the needs-author-review Awaiting author review; agent has done its part (author removes if follow-ups remain) label Aug 22, 2026
@jeswr

jeswr commented Aug 23, 2026

Copy link
Copy Markdown
Member Author

This PR appears to introduce cross graph logic - I assume because it builds upon a PR that has now been stood down. Can all of this logic please be removed.

Comment thread src/N3Store.js Fixed
Comment thread src/N3Store.js Fixed
jeswr and others added 4 commits August 23, 2026 12:13
@jeswr
jeswr force-pushed the feat/quad-pattern-matching branch from 4bf84e1 to 5f98fdf Compare August 23, 2026 11:16
@jeswr

jeswr commented Aug 23, 2026

Copy link
Copy Markdown
Member Author

Addressed. I rebuilt this branch directly on current main, retaining the focused triple-term component-id index and Quad-pattern matching while removing the inherited cross-entity-index intersection/difference helpers, remapping code, associated tests, and N3StoreSetOps benchmark. The full suite passes (6,637 tests, 100% coverage) and lint is clean. Current head: 5f98fdf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai-generated Authored or prepared by an AI coding agent needs-author-review Awaiting author review; agent has done its part (author removes if follow-ups remain)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Index RDF 1.2 triple terms and match patterns inside reified quads

2 participants