Variables appearing in OPTIONAL clause cannot be searched if specified in SELECT

There are some triples that cannot be searched if a variable that appears in the OPTIONAL clause is specified in the SELECT. The following isql run shows zero results when the variable ?organismCommonName is specified in the SELECT, but one result when it is not specified in the SELECT.

SQL> SPARQL
Type the rest of statement, end with a semicolon (;)> DEFINE input:inference "trans"
Type the rest of statement, end with a semicolon (;)> PREFIX up:   <http://purl.uniprot.org/core/>
Type the rest of statement, end with a semicolon (;)> SELECT DISTINCT ?uniprot ?organism  ?organismCommonName
Type the rest of statement, end with a semicolon (;)> WHERE {
Type the rest of statement, end with a semicolon (;)>   VALUES ?uniprot { <http://purl.uniprot.org/uniprot/Q0P7R8> }
Type the rest of statement, end with a semicolon (;)>   ?uniprot up:organism ?organism .
Type the rest of statement, end with a semicolon (;)>   OPTIONAL {
Type the rest of statement, end with a semicolon (;)>     ?organism up:commonName ?organismCommonName .
Type the rest of statement, end with a semicolon (;)>   }
Type the rest of statement, end with a semicolon (;)> }
Type the rest of statement, end with a semicolon (;)> ;
uniprot                                 organism                                                                          organismCommonName
VARCHAR                                 LONG VARCHAR                                                                      LONG VARCHAR
_______________________________________________________________________________


0 Rows. -- 1 msec.
SQL>

SQL> SPARQL
Type the rest of statement, end with a semicolon (;)> DEFINE input:inference "trans"
Type the rest of statement, end with a semicolon (;)> PREFIX up:  <http://purl.uniprot.org/core/>
Type the rest of statement, end with a semicolon (;)> SELECT DISTINCT ?uniprot ?organism
Type the rest of statement, end with a semicolon (;)> WHERE {
Type the rest of statement, end with a semicolon (;)>   VALUES ?uniprot { <http://purl.uniprot.org/uniprot/Q0P7R8> }
Type the rest of statement, end with a semicolon (;)>   ?uniprot up:organism ?organism .
Type the rest of statement, end with a semicolon (;)>   OPTIONAL {
Type the rest of statement, end with a semicolon (;)>     ?organism up:commonName ?organismCommonName .
Type the rest of statement, end with a semicolon (;)>   }
Type the rest of statement, end with a semicolon (;)> }
Type the rest of statement, end with a semicolon (;)> ;
uniprot                                 organism
VARCHAR                                 LONG VARCHAR
_______________________________________________________________________________

http://purl.uniprot.org/uniprot/Q0P7R8  http://purl.uniprot.org/taxonomy/192222

1 Rows. -- 1 msec.
SQL>

What can I do to make sure these cases are included in the results?

The query with ?organismCommonName in the select list returns results when run against the /sparql endpoint, although nothing is returned for the ?organismCommonName column itself.

Thank you for your response. As for the first query, I ran it again and got the same result as your response.

However, the following query that uses OPTIONAL does not return a response when executed via HTTP (OpenLink Virtuoso SPARQL Query Editor).

DEFINE input:inference "trans"
PREFIX up:       <http://purl.uniprot.org/core/>
SELECT DISTINCT ?uniprot ?gene
WHERE {
  VALUES ?uniprot { <http://purl.uniprot.org/uniprot/Q0P7R8> }
  OPTIONAL {
    ?uniprot up:encodedBy [
      <http://www.w3.org/2004/02/skos/core#prefLabel> ?gene
    ] .
  }
}

→ Time-out (or more than 10 minutes).

However, without OPTIONAL, it responds in a few milliseconds.

DEFINE input:inference "trans"
PREFIX up:       <http://purl.uniprot.org/core/>
SELECT DISTINCT ?uniprot ?gene
WHERE {
  VALUES ?uniprot { <http://purl.uniprot.org/uniprot/Q0P7R8> }
    ?uniprot up:encodedBy [
      <http://www.w3.org/2004/02/skos/core#prefLabel> ?gene
    ] .
}

By the way, both COUNT(?gene) and COUNT(Blank Node) are 1.
And isql responds in a few to several tens of milliseconds for both queries.

I would like to have an immediate response on HTTP as I don’t have many triples, but I don’t know how to handle this. Can you please enlighten us?

Although this is a different query than the original query, we would appreciate your response as well.

What is the trans inference rule supposed to be doing ? We need this information as inference + optional will generate a totally different query solution production pipeline than one without either element.

The ontology enumerated next was registered in the graph <http://foo/inference> and rdfs_rule_set('trans', 'http://foo/inference'); was executed.

I sense that your objective here is for rdfs:subClassOf reasoning and inference to occur when inference rule is enabled i.e., what’s also referred to as subsumption.

If my assumption is correct, and your inference rules are working properly, an OPTIONAL clause will expand the range of data operated on as part of the query solution production pipeline –

Here is a simple subsumption example from our github repo which might add some clarity to this matter: