Uploaded image for project: 'Apache Jena'
  1. Apache Jena
  2. JENA-445

a problem in rewriting nested query by AlgebraGenerator

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Done
    • Jena 2.10.0
    • Jena 2.11.0
    • None
    • None
    • Windows

    Description

      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);

      }

      }

      Attachments

        Activity

          People

            rvesse Rob Vesse
            jwsliu John Liu
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: