You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@jena.apache.org by "ting668 (via GitHub)" <gi...@apache.org> on 2026/09/20 02:15:26 UTC
[I] `DISTINCT` with `ORDER BY` on a non-injective expression is rewritten to `REDUCED` and leaks duplicate rows [jena]
ting668 opened a new issue, #4242:
URL: https://github.com/apache/jena/issues/4242
### Version
6.2.0
### What happened?
**Reporter:** ting668
## Environment
- Apache Jena Version: `6.2.0`
- Java Version: `21.0.12+8-LTS`
## Description
While testing Apache Jena using a method based on attribute-constraint analysis, I found that `SELECT DISTINCT` combined with `ORDER BY` on a non-injective expression leaks duplicate rows: with `ORDER BY STRLEN(?v) ?tag`, the value `"a"^^xsd:string` is returned twice.
`DISTINCT` must eliminate duplicates among the projected solutions, so each distinct value of `?v` must appear exactly once, independent of the ordering.
## How to Reproduce and Expected Behavior
**Note:** The queries below are a **minimized, simplified example** reproducing the bug.
Buggy query (with the default graph loaded with the N-Triples data below):
```sparql
SELECT DISTINCT ?v WHERE { ?s <urn:p> ?v . ?s <urn:q> ?tag . } ORDER BY STRLEN(?v) ?tag
```
Data (default graph, N-Triples):
```nt
<urn:s0> <urn:p> "a"^^<http://www.w3.org/2001/XMLSchema#string> .
<urn:s1> <urn:p> "b"^^<http://www.w3.org/2001/XMLSchema#string> .
<urn:s2> <urn:p> "a"^^<http://www.w3.org/2001/XMLSchema#string> .
<urn:s0> <urn:q> "1"^^<http://www.w3.org/2001/XMLSchema#integer> .
<urn:s1> <urn:q> "1"^^<http://www.w3.org/2001/XMLSchema#integer> .
<urn:s2> <urn:q> "2"^^<http://www.w3.org/2001/XMLSchema#integer> .
```
Expected behavior: the two `"a"^^xsd:string` solutions collapse under `DISTINCT`, leaving two rows:
```text
[{"v":"\"a\"^^xsd:string"},{"v":"\"b\"^^xsd:string"}]
```
Actual behavior: the duplicate `"a"^^xsd:string` row is returned:
```text
[{"v":"\"a\"^^xsd:string"},{"v":"\"a\"^^xsd:string"},{"v":"\"b\"^^xsd:string"}]
```
This suggests that the `DISTINCT` is compiled to `REDUCED` even though the `ORDER BY` expression `STRLEN(?v)` is not injective on the projected values: `REDUCED` does not guarantee duplicate elimination, so the duplicate `"a"` row leaks into the result.
### Relevant output and stacktrace
```shell
```
### Are you interested in making a pull request?
None
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@jena.apache.org.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@jena.apache.org
For additional commands, e-mail: issues-help@jena.apache.org
Re: [I] `DISTINCT` with `ORDER BY` on a non-injective expression is rewritten to `REDUCED` and leaks duplicate rows [jena]
Posted by "afs (via GitHub)" <gi...@apache.org>.
afs commented on issue #4242:
URL: https://github.com/apache/jena/issues/4242#issuecomment-5752138322
Confirmed
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@jena.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@jena.apache.org
For additional commands, e-mail: issues-help@jena.apache.org