Skip to content

feat: add fluent SPARQL query builder#57

Merged
odysa merged 4 commits intomainfrom
feat/sparql-query-builder
Feb 28, 2026
Merged

feat: add fluent SPARQL query builder#57
odysa merged 4 commits intomainfrom
feat/sparql-query-builder

Conversation

@odysa
Copy link
Owner

@odysa odysa commented Feb 28, 2026

Summary

  • Add a fluent SPARQL query builder (rdf4j_python.query) supporting SELECT, ASK, CONSTRUCT, and DESCRIBE queries with method chaining
  • Accept both raw strings ("?var", "a", "ex:Person") and typed objects (IRI, Variable, Literal, Namespace-produced IRIs) as terms
  • Include GraphPattern for composable WHERE blocks with FILTER, OPTIONAL, UNION, BIND, VALUES, and sub-query support, plus SELECT modifiers (DISTINCT, ORDER BY, GROUP BY, HAVING, LIMIT, OFFSET)

Test plan

  • serialize_term() correctly handles all term types (IRI, Variable, Literal with lang/datatype, BlankNode, strings)
  • Each query type (SELECT, ASK, CONSTRUCT, DESCRIBE) produces valid SPARQL
  • All clause types tested: FILTER, OPTIONAL (triple + pattern), UNION, BIND, VALUES, sub-query
  • SELECT modifiers appear in correct order (GROUP BY → HAVING → ORDER BY → LIMIT → OFFSET)
  • Prefix handling works with string pairs and Namespace objects
  • __str__() matches build(), copy() produces independent builders
  • Validation errors raised for missing variables, empty WHERE, etc.
  • 62 unit tests all passing

🤖 Generated with Claude Code


Important

Adds a fluent SPARQL query builder to rdf4j_python with support for various query types, clauses, and method chaining, along with comprehensive unit tests.

  • Behavior:
    • Adds fluent SPARQL query builder in rdf4j_python.query for SELECT, ASK, CONSTRUCT, DESCRIBE queries with method chaining.
    • Supports raw strings and typed objects (IRI, Variable, Literal, Namespace) as terms.
    • Includes GraphPattern for composable WHERE blocks with FILTER, OPTIONAL, UNION, BIND, VALUES, sub-query support, and SELECT modifiers (DISTINCT, ORDER BY, GROUP BY, HAVING, LIMIT, OFFSET).
  • Functions:
    • select(), ask(), construct(), describe() in query/__init__.py create respective query builders.
    • serialize_term() in query/_term.py handles term serialization for SPARQL.
  • Classes:
    • SelectQuery, AskQuery, ConstructQuery, DescribeQuery in query/_builder.py for building respective queries.
    • GraphPattern in query/_pattern.py for WHERE clause composition.
  • Tests:
    • 62 unit tests in test_query_builder.py covering all query types, clauses, and term serialization.

This description was created by Ellipsis for 9ad0846. You can customize this summary. It will automatically update as commits are pushed.

Add a programmatic query builder as an alternative to writing raw SPARQL
strings. Supports SELECT, ASK, CONSTRUCT, and DESCRIBE queries with
fluent method chaining. Accepts both string terms and typed objects
(IRI, Variable, Literal, Namespace-produced IRIs).

Features:
- GraphPattern class for composable WHERE blocks
- WHERE, FILTER, OPTIONAL, UNION, BIND, VALUES, sub-query clauses
- SELECT modifiers: DISTINCT, ORDER BY, GROUP BY, HAVING, LIMIT, OFFSET
- Prefix handling via string pairs or Namespace objects
- Light validation in build() for structural correctness
- copy() for safe builder reuse
Copy link

@ellipsis-dev ellipsis-dev bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Important

Looks good to me! 👍

Reviewed everything up to 9ad0846 in 14 seconds. Click for details.
  • Reviewed 1005 lines of code in 6 files
  • Skipped 0 files when reviewing.
  • Skipped posting 0 draft comments. View those below.
  • Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.

Workflow ID: wflow_0Xh8HTTHB8oNNlLD

You can customize Ellipsis by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.

Replace raw SPARQL strings with fluent query builder calls in all
example files (query.py, query_and_print.py, complete_workflow.py).
Add a Query Builder section to the main README with usage examples.
Consolidate _WhereClauseMixin and _PrefixMixin into a single _QueryBase
that handles shared init, prefix rendering, WHERE delegation, copy(),
and __str__(). Remove unused re import and _PREFIXED_NAME_RE. Extract
_ensure_var helper in GraphPattern to deduplicate var normalization.
@odysa odysa merged commit 2921a98 into main Feb 28, 2026
5 checks passed
@odysa odysa deleted the feat/sparql-query-builder branch February 28, 2026 17:50
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