Maddy
Maddy

Reputation: 11

Apache Jena resultset not preserving order from the SPARQL query

I am executing a SPARQL query in my java application and I expect the resultset to preserve the order provided in the SPARQL query. The query returns ordered results when run in GraphDb editor, but when using Apache Jena, it does not return results in the expected order. Observed that this may be related to the union joins in the SPARQL query, but not sure if that's the case.

Has anyone else seen this happen and know how to solve this.

Thank you!

Upvotes: 1

Views: 130

Answers (1)

RobV
RobV

Reputation: 28655

Unless your query contains an explicit ORDER BY clause there is no guarantee of result ordering. The specification even calls this out in places e.g. Section 15.4 OFFSET says the following:

Using LIMIT and OFFSET to select different subsets of the query solutions will not be useful unless the order is made predictable by using ORDER BY.

Queries without the ORDER BY clause may consistently return results using a specific backend implementation but you should not rely on this behaviour. At best this will give you different results in different implementations. At worse results ordering could change when you upgrade your backend due to internal implementation/optimization changes in the underlying implementation.

If you want predictable result ordering you MUST provide an ORDER BY clause in your queries.

Upvotes: 1

Related Questions