You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Bjørnar Tessem <Bj...@infomedia.uib.no> on 2012/10/24 11:21:08 UTC

SELECT expression in results part of query

I want to do a nested query towards dbpedia with a select expression in the results part.
This works well when I use the dbpedia online query service, but will not work with jena.
But jena's parser called in QueryFactory.create() does not seem to like SELECT in the results part.

A simplified version of the query I build and try to send is:

PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX : <http://dbpedia.org/resource/>
PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX dbpediaont: <http://dbpedia.org/ontology/> 
SELECT  distinct ?y 
        ((SELECT count(*) WHERE {?y ?r1 ?z}) as ?c1)
        ?c3 ?label WHERE 
	{{SELECT ?y 
			 (count(?r3) as ?c3) 
			 ?label WHERE {
		 <http://dbpedia.org/resource/Simpson,_Milton_Keynes> ?r3 ?y . 
		 ?y rdfs:label ?label.
		 FILTER (lang(?label) = "en")}}} 

Adds Java code (this has nothing to do with it I believe, as I have tried all variants of syntaxes in the parameter list of the QueryFactory.create()):
                ...
		String dbpediaEndpointUri = "http://dbpedia.org/sparql";
		// Builds query.
		Query query = QueryFactory.create(buildQuery(lt.getSubject()),Syntax.syntaxSPARQL);
		QueryExecution qe = QueryExecutionFactory.sparqlService(
				dbpediaEndpointUri, query, "http://dbpedia.org");
		ResultSet resSet = qe.execSelect();

According to what I have read, this should be allowed in SPARQL 1.1  and in ARQ, so what is the problem.
Doesn't jena support the full language?

The error message I get  is (which is exactly the same as for the larger query asking for some more data):

Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered " "select" "SELECT "" at line 7, column 11.
Was expecting one of:
    <IRIref> ...
    <PNAME_NS> ...
    <PNAME_LN> ...
    <VAR1> ...
    <VAR2> ...
    "exists" ...
    "not" ...
   etc....

Bjørnar Tessem

SV: SELECT expression in results part of query

Posted by Bjørnar Tessem <Bj...@infomedia.uib.no>.
Thank you. 

I took use of the query structure you described and it worked perfectly.

Apparantly I was mistaken about the sub-select in the select clause. I was a bit quick when I read the sparql 1.1 spec.
However, as I mentioned, it works fine in dbpedia's end point.

The query was really to find the number of links from a resource in dbpedia to another resource, and then how many links
there is from that resource to and from the rest of the world.

Bjørnar 

________________________________________
Fra: Andy Seaborne [andy.seaborne.apache@gmail.com] p&#229; vegne av Andy Seaborne [andy@apache.org]
Sendt: 25. oktober 2012 12:18
Til: users@jena.apache.org
Emne: Re: SELECT expression in results part of query

On 24/10/12 10:21, Bjørnar Tessem wrote:
> I want to do a nested query towards dbpedia with a select expression in the results part.
> This works well when I use the dbpedia online query service, but will not work with jena.
> But jena's parser called in QueryFactory.create() does not seem to like SELECT in the results part.

Indeed - the syntax is illegal for SPARQL.  You also have expressions
with non-group keys which SPARQL is quite strict about (some SQL
databases are, some are not)

SPARQL does not have sub-SELECT in the SELECT clause.

>
> According to what I have read, this should be allowed in SPARQL 1.1  and in ARQ, so what is the problem.

Where did you read that?

> Doesn't jena support the full language?

Normally, Jena is just one implementation of SPARQL and subject to all
the bugs and misunderstandings of the spec that can arise.

But the grammar is different - the JavaCC grammar that makes the Jena
parser is also the source of the HTML that goes into the spec.

> A simplified version of the query I build and try to send is:
>
> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
> PREFIX foaf: <http://xmlns.com/foaf/0.1/>
> PREFIX : <http://dbpedia.org/resource/>
> PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
> PREFIX dbpediaont: <http://dbpedia.org/ontology/>
> SELECT  distinct ?y
>          ((SELECT count(*) WHERE {?y ?r1 ?z}) as ?c1)

You will need to add another subselect into the query (which is
effectively what your query would do in SQL)

I'm not quite sure what the query is doing but something like:

{ SELECT (count(*) AS ?c1) WHERE {?y ?r1 ?z} GROUP BY ?y }

so would this query be close to what you are looking for:

PREFIX  :     <http://dbpedia.org/resource/>
PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  geo:  <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX  dbpediaont: <http://dbpedia.org/ontology/>
PREFIX  foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?y ?c3 ?c1 ?slabel
WHERE
   { { SELECT  ?y (count(?r3) AS ?c3) (sample(?label) AS ?slabel)
       WHERE
         { <http://dbpedia.org/resource/Simpson,_Milton_Keynes> ?r3 ?y .
           ?y rdfs:label ?label
           FILTER ( lang(?label) = "en" )
         }
       GROUP BY ?y
     }
     { SELECT ?y ( count(*) AS ?c1) WHERE {?y ?r1 ?z} GROUP BY ?y }
   }


        Andy

>          ?c3 ?label WHERE
>       {{SELECT ?y
>                        (count(?r3) as ?c3)
>                        ?label WHERE {
>                <http://dbpedia.org/resource/Simpson,_Milton_Keynes> ?r3 ?y .
>                ?y rdfs:label ?label.
>                FILTER (lang(?label) = "en")}}}
>
> Adds Java code (this has nothing to do with it I believe, as I have tried all variants of syntaxes in the parameter list of the QueryFactory.create()):
>                  ...
>               String dbpediaEndpointUri = "http://dbpedia.org/sparql";
>               // Builds query.
>               Query query = QueryFactory.create(buildQuery(lt.getSubject()),Syntax.syntaxSPARQL);
>               QueryExecution qe = QueryExecutionFactory.sparqlService(
>                               dbpediaEndpointUri, query, "http://dbpedia.org");
>               ResultSet resSet = qe.execSelect();
>
> According to what I have read, this should be allowed in SPARQL 1.1  and in ARQ, so what is the problem.
> Doesn't jena support the full language?
>
> The error message I get  is (which is exactly the same as for the larger query asking for some more data):
>
> Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered " "select" "SELECT "" at line 7, column 11.
> Was expecting one of:
>      <IRIref> ...
>      <PNAME_NS> ...
>      <PNAME_LN> ...
>      <VAR1> ...
>      <VAR2> ...
>      "exists" ...
>      "not" ...
>     etc....
>
> Bjørnar Tessem
>


Re: SELECT expression in results part of query

Posted by Andy Seaborne <an...@apache.org>.
On 24/10/12 10:21, Bjørnar Tessem wrote:
> I want to do a nested query towards dbpedia with a select expression in the results part.
> This works well when I use the dbpedia online query service, but will not work with jena.
> But jena's parser called in QueryFactory.create() does not seem to like SELECT in the results part.

Indeed - the syntax is illegal for SPARQL.  You also have expressions 
with non-group keys which SPARQL is quite strict about (some SQL 
databases are, some are not)

SPARQL does not have sub-SELECT in the SELECT clause.

>
> According to what I have read, this should be allowed in SPARQL 1.1  and in ARQ, so what is the problem.

Where did you read that?

> Doesn't jena support the full language?

Normally, Jena is just one implementation of SPARQL and subject to all 
the bugs and misunderstandings of the spec that can arise.

But the grammar is different - the JavaCC grammar that makes the Jena 
parser is also the source of the HTML that goes into the spec.

> A simplified version of the query I build and try to send is:
>
> PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
> PREFIX foaf: <http://xmlns.com/foaf/0.1/>
> PREFIX : <http://dbpedia.org/resource/>
> PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#>
> PREFIX dbpediaont: <http://dbpedia.org/ontology/>
> SELECT  distinct ?y
>          ((SELECT count(*) WHERE {?y ?r1 ?z}) as ?c1)

You will need to add another subselect into the query (which is 
effectively what your query would do in SQL)

I'm not quite sure what the query is doing but something like:

{ SELECT (count(*) AS ?c1) WHERE {?y ?r1 ?z} GROUP BY ?y }

so would this query be close to what you are looking for:

PREFIX  :     <http://dbpedia.org/resource/>
PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX  geo:  <http://www.w3.org/2003/01/geo/wgs84_pos#>
PREFIX  dbpediaont: <http://dbpedia.org/ontology/>
PREFIX  foaf: <http://xmlns.com/foaf/0.1/>

SELECT ?y ?c3 ?c1 ?slabel
WHERE
   { { SELECT  ?y (count(?r3) AS ?c3) (sample(?label) AS ?slabel)
       WHERE
         { <http://dbpedia.org/resource/Simpson,_Milton_Keynes> ?r3 ?y .
           ?y rdfs:label ?label
           FILTER ( lang(?label) = "en" )
         }
       GROUP BY ?y
     }
     { SELECT ?y ( count(*) AS ?c1) WHERE {?y ?r1 ?z} GROUP BY ?y }
   }


	Andy

>          ?c3 ?label WHERE
> 	{{SELECT ?y
> 			 (count(?r3) as ?c3)
> 			 ?label WHERE {
> 		 <http://dbpedia.org/resource/Simpson,_Milton_Keynes> ?r3 ?y .
> 		 ?y rdfs:label ?label.
> 		 FILTER (lang(?label) = "en")}}}
>
> Adds Java code (this has nothing to do with it I believe, as I have tried all variants of syntaxes in the parameter list of the QueryFactory.create()):
>                  ...
> 		String dbpediaEndpointUri = "http://dbpedia.org/sparql";
> 		// Builds query.
> 		Query query = QueryFactory.create(buildQuery(lt.getSubject()),Syntax.syntaxSPARQL);
> 		QueryExecution qe = QueryExecutionFactory.sparqlService(
> 				dbpediaEndpointUri, query, "http://dbpedia.org");
> 		ResultSet resSet = qe.execSelect();
>
> According to what I have read, this should be allowed in SPARQL 1.1  and in ARQ, so what is the problem.
> Doesn't jena support the full language?
>
> The error message I get  is (which is exactly the same as for the larger query asking for some more data):
>
> Exception in thread "main" com.hp.hpl.jena.query.QueryParseException: Encountered " "select" "SELECT "" at line 7, column 11.
> Was expecting one of:
>      <IRIref> ...
>      <PNAME_NS> ...
>      <PNAME_LN> ...
>      <VAR1> ...
>      <VAR2> ...
>      "exists" ...
>      "not" ...
>     etc....
>
> Bjørnar Tessem
>