Fix SPARQL queries failing on Virtuoso endpoints - #171
Open
kvistgaard wants to merge 1 commit into
Open
Conversation
- Use FILTER(!isBlank(...)) instead of FILTER(isIri(...)) on incoming-link patterns, where a literal subject is not possible; isIri() there makes Virtuoso choose a catastrophic query plan scanning the whole graph. - Rewrite the OwlRdfsSettings link type statistics query as a UNION with an outer sum() instead of joining two aggregate sub-queries, which the Virtuoso cost estimator rejects; drop the LIMIT that applied to a single-row aggregate result and so never limited anything. - Treat unbound link counts as 0: some endpoints return the aggregate variable unbound when it counts an empty solution group. - Add a connectedLinkStats() unit test covering the statistics query. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix SPARQL queries failing on Virtuoso endpoints
Summary
Three queries generated by
SparqlDataProviderfail against Virtuoso (tested onversion 08.03.3332): two hang until the server's timeout, one is rejected outright by the
cost estimator. Each fix keeps the query semantics identical for standard SPARQL
1.1 endpoints, so behaviour elsewhere does not change. Verified against a
Virtuoso endpoint holding an 11M-triple graph and against Comunica via a new
unit test.
Symptoms
On a Virtuoso endpoint, with
OwlStatsSettings:on an 11M-triple graph the affected queries take >20s instead of ~0.1s.
Causes and fixes
1.
FILTER(isIri(...))on incoming-link patterns (sparqlDataProvider.ts)createRefQueryPartemittedFILTER(isIri(?inst))on both branches of theconnected-elements union, and
connectedLinkStatsemittedFILTER IsIri(?inObject)for the statistics query's incoming branch. On anincoming pattern (
?inst ?link <element>) that filter makes Virtuoso abandonthe object-anchored index and scan the graph:
{ ?inst ?link <e> FILTER(isIri(?inst)) }in union{ ?inst ?link <e> FILTER(!isBlank(?inst)) }in unionIn the subject position a standard SPARQL 1.1 term cannot be a literal, so
!isBlankaccepts the same solutions asisIrithere. Outgoing patterns keepisIri(their object can be a literal, and the subject-anchored plan is notaffected). Known trade-off: on an RDF-star endpoint a quoted-triple subject now
passes the filter where
isIriexcluded it, andmapSparqlResponseIntoRdfJsthrows on
"type": "triple"bindings; there is no SPARQL 1.1-portable filterthat excludes quoted triples without also breaking on non-star endpoints
(
isTRIPLEis SPARQL-star syntax). If RDF-star tolerance is wanted, skippingunmappable term types in the response mapper would be a follow-up.
2. Joined aggregate subqueries in
linkTypesStatisticsQuery(sparqlDataProviderSettings.ts)The per-link-type count query in
OwlRdfsSettingsjoined two aggregatesubqueries. Virtuoso's cost estimator rejects that shape with HTTP 400:
Rewritten as a
UNIONof two row subqueries with an outersum(), which theestimator accepts (an aggregation over ~47k rows answers in 0.12s). Counts
stay exact; the previous
LIMIT 101is dropped because it applied to asingle-row aggregate result and so never limited anything. Constants in the
projection of an aggregate query are permitted by SPARQL 1.1 (§18.2.4.1). The
helper variables are named
?__out/?__into avoid capturing variables fromuser-authored
LinkConfiguration.pathpatterns, which are substituted verbatiminto the subqueries.
3. Unbound counts crash
getLinkStatistics(responseHandler.ts,sparqlModels.ts)When a counted pattern matches nothing, Virtuoso returns the aggregate variable
unbound rather than
0, andparseCountcrashed reading.valueofundefined.parseCountnow treats a missing binding as 0, the default querywraps the sums in
COALESCE(..., 0), andLinkCountBindingdeclaresinCount/outCountoptional so consumers cannot type-check past the case.Testing
connectedLinkStats()with exact counts over the org ontologyfixture, which runs the rewritten statistics query through Comunica.
npm run lint,npm run typecheck, and the library +examples build pass.
graph: the connections dialog lists all link types with counts,
connected-element search returns, links render between placed elements.
🤖 Generated with Claude Code