[ 
https://issues.apache.org/jira/browse/JENA-445?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13640585#comment-13640585
 ] 

Rob Vesse commented on JENA-445:
--------------------------------

Generally minimal test cases are preferred, for example I can reproduce this 
with the following simple query:

SELECT ?key ?agg WHERE { { SELECT ?key (COUNT(*) AS ?agg) { ?key ?p ?o } GROUP 
BY ?key } }

I have already checked in some fixes for this which should be available in the 
latest snapshots but I want to write some more test cases before I mark this as 
resolved.

I don't think we can handle every possible sub-query though, consider the 
following:

SELECT * { { SELECT ?s WHERE { ?s ?p ?o } }

After algebra translation and reconstructing as a query you would get the 
following:

SELECT ?s WHERE { ?s ?p ?o }

This is because there is insufficient information in the algebra to know that 
there is an outer query when SELECT * is used
                
> a problem in rewriting nested query by AlgebraGenerator
> -------------------------------------------------------
>
>                 Key: JENA-445
>                 URL: https://issues.apache.org/jira/browse/JENA-445
>             Project: Apache Jena
>          Issue Type: Bug
>    Affects Versions: Jena 2.10.0
>         Environment: Windows
>            Reporter: John Liu
>
> I found a problem when rewriting a nested query by AlgebraGenerator, the 
> generated query becomes syntax incorrect query. The version of JENA I tested 
> is 2.7.1 and 2.10.1
>  
> The code I rewrite the query is:
>     Query query = QueryFactory.create(queryString);
>      AlgebraGenerator ag =
>     new AlgebraGenerator();
>     Op op = ag.compile(query);
>     Query query2 = OpAsQuery.asQuery(op);
> The original query is:
> PREFIX dcterms: <http://purl.org/dc/terms/> 
> PREFIX dbpedia: <http://dbpedia.org/resource/> 
> SELECT ?num_of_holidays ?celebrate_Chinese_New_Year WHERE { 
> {SELECT ?country_cat (COUNT(?holiday) as ?num_of_holidays) 
> WHERE {?country_cat <http://www.w3.org/2004/02/skos/core#broader> 
> <http://dbpedia.org/resource/Category:Public_holidays_by_country>. 
> ?holiday dcterms:subject ?country_cat 
> }GROUP by ?country_cat 
> } 
> { 
> SELECT ?country_cat (COUNT(?holiday) as ?celebrate_Chinese_New_Year) 
> WHERE { 
> ?country_cat <http://www.w3.org/2004/02/skos/core#broader> 
> <http://dbpedia.org/resource/Category:Public_holidays_by_country>. 
> ?holiday dcterms:subject ?country_cat 
> FILTER(?holiday="http://dbpedia.org/resource/Lunar_New_Year's_Day") 
> }GROUP by ?country_cat 
> } 
> }
> The generated query is:
> SELECT ?country_cat ?celebrate_Chinese_New_Year
> WHERE
> { { ?country_cat <http://www.w3.org/2004/02/skos/core#broader> 
> <http://dbpedia.org/resource/Category:Public_holidays_by_country> .
> ?holiday <http://purl.org/dc/terms/subject> ?country_cat
> BIND(count(?holiday) AS ?num_of_holidays)
> }
> { ?country_cat <http://www.w3.org/2004/02/skos/core#broader> 
> <http://dbpedia.org/resource/Category:Public_holidays_by_country> .
> ?holiday <http://purl.org/dc/terms/subject> ?country_cat
> FILTER ( ?holiday = "http://dbpedia.org/resource/Lunar_New_Year's_Day" )
> BIND(count(?holiday) AS ?celebrate_Chinese_New_Year)
> }
> }
> GROUP BY ?country_cat
> The generated query has a syntax error: "Line 5, column 12: Aggregate 
> expression not legal at this point".
> The following  is a java class to demonstrate the problem:
> import com.hp.hpl.jena.query.Query;
> import com.hp.hpl.jena.query.QueryFactory;
> import com.hp.hpl.jena.sparql.algebra.AlgebraGenerator;
> import com.hp.hpl.jena.sparql.algebra.Op;
> import com.hp.hpl.jena.sparql.algebra.OpAsQuery;
> public class TestJena {
>       /**
>        * @param args
>        */
>       public static void main(String[] args) {
>               
>               String queryString = 
>                       "PREFIX dcterms: <http://purl.org/dc/terms/> \n" +
>                       "PREFIX dbpedia: <http://dbpedia.org/resource/> \n" +
>                               
>                               "SELECT ?num_of_holidays 
> ?celebrate_Chinese_New_Year  WHERE { \n" +
>                                               "{" +
>                                                       "SELECT ?country_cat 
> (COUNT(?holiday) as ?num_of_holidays) \n" +
>                                                       "WHERE {" +
>                                                               "?country_cat 
> <http://www.w3.org/2004/02/skos/core#broader> 
> <http://dbpedia.org/resource/Category:Public_holidays_by_country>. \n" +
>                                                               "?holiday 
> dcterms:subject ?country_cat \n" +
>                                                       "}GROUP by ?country_cat 
> \n" +
>                                               "} \n" +
>                                       "{ \n" +
>                                       "SELECT ?country_cat (COUNT(?holiday) 
> as ?celebrate_Chinese_New_Year) \n" +
>                                               "WHERE { \n" +
>                                                       "?country_cat 
> <http://www.w3.org/2004/02/skos/core#broader> 
> <http://dbpedia.org/resource/Category:Public_holidays_by_country>. \n" +
>                                                       "?holiday 
> dcterms:subject ?country_cat \n" +
>                                                       
> "FILTER(?holiday=\"http://dbpedia.org/resource/Lunar_New_Year\'s_Day\") \n" +
>                                               "}GROUP by ?country_cat \n" +
>                                       "} \n" + 
>                               "}\n";
>               
>               System.out.println("Original query: \n" + queryString);
>               
>               Query query = QueryFactory.create(queryString);
>               
>               AlgebraGenerator ag = new AlgebraGenerator();
>               Op op = ag.compile(query);
>               
>               Query query2 = OpAsQuery.asQuery(op);
>               
>               String queryString2 = query2.toString();
>               
>               System.out.println("Update query: \n" + queryString2);
>               
>               Query newQuery2 = QueryFactory.create(queryString2);
>               
>               
>               
>       }
> }
>  

--
This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators
For more information on JIRA, see: http://www.atlassian.com/software/jira

Reply via email to