apache / apache/jena

Select clause disappear from where clause

Open
#3,707 1 comment 0 reactions 0 assignees View on GitHub
bug Query Builder
Dominant language
Java
Stars
1.4k
Forks
712
Avg merge
15h 41m
Merged PRs (30d)
53

Description

### Version

5.6.0

### What happened?

Jena do not handle adding a clause values in a where clause nor in an optional clause.

Here is a minimal exemple (I also give a .zip minimal maven project with .jar in it maybe it's more convenient.).

[jena-test.zip](https://github.com/user-attachments/files/24738429/jena-test.zip)

To compile and execute project just go in the decompressed directory and run
```shell
mvn clean install
java -jar target/jena-test-1.0-SNAPSHOT.jar
```
#### minimal code
```java
public static void main( String[] args )
{
Var uriVar = Converters.makeVar("uri");
Var siteAdressVar = Converters.makeVar("siteAddress");
Var cityVar = Converters.makeVar("city");

SelectBuilder select = new SelectBuilder();
select.addVar(uriVar);

SelectBuilder selectOptional = new SelectBuilder();
selectOptional.addVar(uriVar)
.addVar(cityVar);

selectOptional.addWhere(uriVar, ORG.siteAddress, siteAdressVar);
selectOptional.addWhere(siteAdressVar, VCARD4.locality.asNode(), cityVar);

System.out.println("[ optional select clause ]");
System.out.println(selectOptional);
System.out.println("----------------------------------------");

select.addOptional(selectOptional);

System.out.println("[ final select query ]");
System.out.println(select.build());
System.out.println("----------------------------------------");
}
```
---

#### output :
```
[ optional select clause ]
SELECT ?uri ?city
WHERE
{ ?uri ?siteAddress .
?siteAddress ?city}

----------------------------------------
[ final select query ]
SELECT ?uri
WHERE
{ OPTIONAL
{ ?uri ?siteAddress .
?siteAddress ?city}}

----------------------------------------
```

#### expected output :
```
[ final select query ]
SELECT ?uri
WHERE
{ OPTIONAL
SELECT ?uri ?city
WHERE
{ ?uri ?siteAddress .
?siteAddress ?city
}
}
```
as you may noticed, the `SELECT ?uri ?city` has disappeared as well as the second where clause.

#### original exemple

The original purpose was to sort on the `?city` var which is not returned. For information here is the original request i want to create (tested and validated on an RDF4J workbench server).

full request here

PREFIX org: 

PREFIX dc:
PREFIX os-sec:
PREFIX vcard:
SELECT DISTINCT ?uri ?rdfType ?rdfTypeName ?lastUpdateDate ?name ?description ?publisher ?publicationDate ?address ?locationObservationCollection
WHERE
{ ?rdfType (rdfs:subClassOf)* org:Site
OPTIONAL
{ ?rdfType rdfs:label ?rdfTypeName
FILTER langMatches(lang(?rdfTypeName), "en")
}
OPTIONAL
{ ?rdfType rdfs:label ?rdfTypeName
FILTER langMatches(lang(?rdfTypeName), "")
}
GRAPH
{ ?uri rdf:type ?rdfType
OPTIONAL
{ ?uri dc:modified ?lastUpdateDate}
OPTIONAL
{ ?uri rdfs:label ?name}
OPTIONAL
{ ?uri rdfs:comment ?description}
OPTIONAL
{ ?uri dc:publisher ?publisher}
OPTIONAL
{ ?uri dc:issued ?publicationDate}
OPTIONAL
{ ?uri org:siteAddress ?address}
}
OPTIONAL
{ GRAPH
{ OPTIONAL
{ ?locationObservationCollection
?uri}}}
OPTIONAL
{ ?_organization
org:hasSite ?uri}
OPTIONAL
{ ?uri os-sec:hasGroup ?groups .
?groups os-sec:hasUserProfile ?userProfiles .
?userProfiles
os-sec:hasUser }
OPTIONAL {
{
SELECT ?uri ?city
WHERE {
?uri org:siteAddress ?address .
?address vcard:locality ?city .
}
}
}

FILTER ( ! isBlank(?uri) )
}
ORDER BY DESC(?city) ASC(?uri)
LIMIT 100

### Relevant output and stacktrace

```shell

```

### Are you interested in making a pull request?

None

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue with the attached minimal Maven project using `mvn clean install` and `java -jar target/jena-test-1.0-SNAPSHOT.jar`. Start from the `SelectBuilder` calls, especially `addOptional`, and trace how the nested builder is rendered; done means the optional block preserves its SELECT clause, projected variables, and both WHERE clauses as shown in the expected output.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
52/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.