Skip to content

Page by subject whatever a path's declared maxCount; IRI tie-break on every page - #417

Merged
flyon merged 2 commits into
mainfrom
fix/page-by-subject
Nov 9, 2026
Merged

flyon merged 2 commits into
mainfrom
fix/page-by-subject

Conversation

@flyon

@flyon flyon commented Nov 9, 2026 •

Copy link
Copy Markdown
Contributor

What changed

limit/offset (and .one()) are documented to page over root subjects, with ties broken by the subject IRI. Two cases broke that contract:

  1. A maxCount: 1 path that holds several values in the data (e.g. rdfs:label in several languages). Paging trusted the declared maxCount, so it used a plain row LIMIT. Each extra value became an extra row, so a page held fewer subjects than asked and could repeat a subject across pages.
  2. Ties on the plain path. The plain LIMIT path ordered only by the select's keys, with no IRI tie-break. Subjects with equal or missing sort values had no stable order across pages.

The fix is in src/sparql/irToAlgebra.ts:

  • A paged ungrouped select now uses the existing sub-SELECT of subjects (pageBySubject) whenever a row carries any value besides its subject: a traversal, or a path in the projection or the order. This holds whatever the path's declared maxCount (new readsPathPerRow). A select of subjects only (Person.select().where(...)) keeps its plain LIMIT, because there a row is a subject.
  • The page always ends with an ASC(?subject) tie-break. An offset without an orderBy now pages in subject order, on both paths.
  • .for(id) still reads without a LIMIT.
  • A single-valued field whose data holds several values still returns one of them, as before. Because the outer rows follow the select's order, it is the first value in that order.

Example: Person.select((p) => p.name).minus(Employee).orderBy((p) => p.name).limit(7)

Before:

SELECT DISTINCT ?a0 ?a0_name WHERE {
  ?a0 rdf:type <Person> . OPTIONAL { ?a0 <name> ?a0_name . }
  MINUS { ?a0 rdf:type <Employee> . } }
ORDER BY DESC(BOUND(?a0_name)) ASC(?a0_name) LIMIT 7

After:

SELECT DISTINCT ?a0 ?a0_name WHERE {
  { SELECT ?a0 (MIN(?a0_name) AS ?page_order_0) WHERE {
      ?a0 rdf:type <Person> . OPTIONAL { ?a0 <name> ?a0_name . }
      MINUS { ?a0 rdf:type <Employee> . }
    } GROUP BY ?a0 ORDER BY DESC(BOUND(?page_order_0)) ASC(?page_order_0) ASC(?a0) LIMIT 7 }
  ?a0 rdf:type <Person> . OPTIONAL { ?a0 <name> ?a0_name . }
  MINUS { ?a0 rdf:type <Employee> . } }
ORDER BY DESC(BOUND(?page_order_0)) ASC(?page_order_0) ASC(?a0) DESC(BOUND(?a0_name)) ASC(?a0_name)

MINUS and where filters were already applied before the LIMIT on both paths; that part is unchanged.

Tests

  • New src/tests/select-paging-data-beyond-shape.test.ts. Unit SPARQL tests cover the sub-SELECT for single-valued projections, traversals and .one(), plus the IRI tie-break, MINUS inside the page, and offset without an order. Live Fuseki tests use a subject with two values of a maxCount: 1 path, three subjects tied on the sort key, two without it, and an excluded subject.
  • Written first. Before the fix: Tests: 8 failed, 3 passed, 11 total. The live failures were a subject counted twice (+ "a1") and a page missing a subject (- "a3").
  • Updated expectations that asserted the old plain LIMIT for single-valued selects (select-subject-paging, sparql-select-golden selectOne/outerWhereLimit, sparql-algebra outerWhereLimit).
  • Commit 2 (DISTINCT page) adds unit tests: no sort value → SELECT DISTINCT with no GROUP BY; an order on the subject itself → no GROUP BY.
  • Full suite with a live Fuseki (FUSEKI_BASE_URL=http://localhost:3961 FUSEKI_ADMIN_PASSWORD=admin npm test, after npx linked build):
[fuseki] live tests: 498 total — 496 passed, 0 failed, 2 skipped
Test Suites: 4 skipped, 173 passed, 173 of 177 total
Tests:       122 skipped, 3371 passed, 3493 total

The typecheck (tsc -p tsconfig-tests.json) passed as part of npm test.

Benchmark

Run on a local inferring Fuseki (the test image, TDB2 in memory) with two graphs:

  • items100k: 100,000 instances of one class (~500k triples). Each has 1–3 language-tagged rdfs:label values (only ~2,000 distinct base labels, so many ties) and two single-valued fields.
  • schema: the schema.org vocabulary (~1,000 rdfs:Classes with label and comment).

The select reads every field. Each time is the median of 2–3 runs after a warm-up, in seconds; the number of distinct subjects returned is in parentheses. "old" is the row LIMIT on main. "sub-SELECT (GROUP BY)" is this PR's first commit. "sub-SELECT (DISTINCT)" is what this PR ships for a page with no sort value to aggregate (commit 2: SELECT DISTINCT ?s … [ORDER BY ?s] LIMIT, which streams, where a GROUP BY reads every subject first).

set orderBy limit offset old row LIMIT sub-SELECT (GROUP BY) sub-SELECT (DISTINCT, shipped)
items100k none 20 0 0.027 (13) 0.382 (20) 0.028 (20)
items100k none 20 90000 12.842 (10) 1.836 (20) 1.131 (20)
items100k none 100 0 0.195 (51) 0.577 (100) 0.143 (100)
items100k none 100 90000 5.321 (51) 3.017 (100) 1.658 (100)
schema none 20 0 0.025 (20) 0.048 (20) 0.020 (20)
schema none 20 900 0.135 (20) 0.039 (20) 0.082 (20)
schema none 100 0 0.030 (100) 0.053 (100) 0.093 (100)
schema none 100 900 0.238 (100) 0.024 (100) 0.043 (100)
items100k label 20 0 5.727 (20) 1.752 (20) = (MIN key needs GROUP BY)
items100k label 20 90000 19.266 (20) 5.109 (20) =
items100k label 100 0 4.710 (100) 1.475 (100) =
items100k label 100 90000 10.029 (100) 3.975 (100) =
schema label 20 0 0.048 (20) 0.071 (20) =
schema label 20 900 0.094 (20) 0.023 (20) =
schema label 100 0 0.036 (100) 0.043 (100) =
schema label 100 900 0.033 (100) 0.030 (100) =

Reading the table:

  • With an orderBy on a path, the page is 3–4× faster than the old row LIMIT on the large set. The old query sorts and de-duplicates every joined row. The page sorts one MIN per subject over only the sort path.
  • Without an order, the first commit's GROUP BY page cost ~0.35 s on a first page that the row LIMIT answered in 0.03 s. The DISTINCT page brings that back to 0.03 s. The old row LIMIT also returned too few subjects (13 of 20, 51 of 100) on data with several labels.
  • The schema.org-sized graph is in the tens of milliseconds for every variant.

Follow-ups

  • None.
  • The root of a query has no isIri() / isBlank() (.where((c) => c.isIri()) throws "is accessed in a query, but it does not have a @linkedProperty decorator"). Excluding blank nodes (e.g. anonymous owl:unionOf class expressions) in the query itself needs that, or a $-prefixed node fact like $type(). Not part of this PR.

…I on every page

A row LIMIT counted a subject twice when the data held two values of a
maxCount-1 path (a label in two languages), so a page held fewer subjects
than asked. Paged selects now use the sub-SELECT of subjects whenever a row
carries any value besides its subject; a select of subjects only keeps its
plain LIMIT. An offset without an orderBy pages in subject order.
A GROUP BY reads every subject before the LIMIT; a DISTINCT streams. The
page keeps GROUP BY only for a MIN/MAX sort key or a HAVING.
@flyon
flyon merged commit cd74d17 into main Nov 9, 2026
2 checks passed
@flyon
flyon deleted the fix/page-by-subject branch November 9, 2026 21:19
@linked-fw-release linked-fw-release Bot mentioned this pull request Nov 9, 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