Skip to content

fix: support solution modifiers in short form CONSTRUCT WHERE queries - #3660

Open
Thanatos9404 wants to merge 1 commit into
RDFLib:mainfrom
Thanatos9404:fix-construct-where-modifiers
Open

Thanatos9404 wants to merge 1 commit into
RDFLib:mainfrom
Thanatos9404:fix-construct-where-modifiers

Conversation

@Thanatos9404

Copy link
Copy Markdown

Summary of changes

Fixes #1785.

A short form CONSTRUCT WHERE query with a solution modifier failed with TypeError: 'NoneType' object is not iterable:

g.query("CONSTRUCT WHERE { ?s ?p ?o } LIMIT 10")

translateQuery stored no template for the short form. evalConstructQuery then read it from query.p.p.triples, which is the basic graph pattern only when the Project sits directly on it:

  • LIMIT and OFFSET add a Slice above the Project;
  • ORDER BY adds an OrderBy below it;
  • a VALUES clause adds a Join.

As suggested in the issue, the fix is now upstream of evaluation: translateQuery builds the template from the triples of the short form's WHERE clause, the FakeGroupGraphPatten the parser creates for this step. evalConstructQuery uses query.template as it is, so the shape of the algebra no longer matters.

Behaviour change: CONSTRUCT { } WHERE { ... } now returns an empty graph, as an empty template builds no triples. Before, an empty template was treated as the short form. With a plain basic graph pattern and no modifiers, it returned the matched triples. Otherwise it raised the same TypeError, unless the query had no solutions.

Checklist

  • Checked that there aren't other open pull requests for
    the same change.
  • Checked that all tests and type checking passes.
  • If the change has a potential impact on users of this project:
    • Added or updated tests that fail without the change.
  • Considered granting push permissions to the PR branch,
    so maintainers can fix minor issues and keep your PR up to date.

The new test/test_sparql/test_construct_where.py covers:

  • LIMIT, OFFSET, ORDER BY, VALUES and combinations of them;
  • a pattern with two triples;
  • an empty template with a basic graph pattern, with LIMIT, and with OPTIONAL.

All but the unmodified short form fail on main.

These all pass:

  • test/test_sparql;
  • the three W3C SPARQL suites;
  • ruff check, ruff format --check and mypy;
  • uv lock --check.

The template of a CONSTRUCT WHERE query was read at evaluation time
from query.p.p, which is its basic graph pattern only when no LIMIT,
OFFSET, ORDER BY or VALUES wraps the pattern. Build the template from
the WHERE clause's triples in translateQuery instead, and give an
empty CONSTRUCT template an empty template rather than treating it as
the short form.

Fixes RDFLib#1785
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.

Shortform CONSTRUCT query with a LIMIT triggers exception

1 participant