Skip to content

Ported queries to be compatible with QLever - #661

Open
fenymufyd wants to merge 9 commits into
govdirectory:mainfrom
fenymufyd:qlever-compatible-query-port
Open

fenymufyd wants to merge 9 commits into
govdirectory:mainfrom
fenymufyd:qlever-compatible-query-port

Conversation

@fenymufyd

@fenymufyd fenymufyd commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Description

I've ported all .rq query files to be compatible with QLever. I used LLM (Copilot) to do most of the tedious work but I've tested some samples of the queries manually on WQDS and QLever and it works on both services. I think it also made organization-optional.rq faster on QLever, about 5-8 sec per item.

Issue Reference

this should fix #498

Checklist

Please ensure the following before submitting the PR:

  • I have tested the changes locally, and they work as expected.
  • The code follows the project's coding standards and conventions.
  • I have updated relevant documentation (if needed).
  • I have added unit tests or performed manual testing where necessary.

@Abbe98
Abbe98 self-requested a review July 15, 2026 13:50

@Abbe98 Abbe98 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for this work! Here are a few first notes.

Comment thread queries/generators/ukraine.rq
Comment thread queries/countries.rq Outdated
Comment thread queries/countries.rq
Comment thread queries/organization-optional.rq

@fenymufyd fenymufyd left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reviewed all except countries.rq

@Abbe98

Abbe98 commented Jul 15, 2026

Copy link
Copy Markdown
Member

Thank you @fenymufyd, I have some time scheduled for Govdirectory on Friday and will try to get a in depth review done!

@Abbe98

Abbe98 commented Jul 17, 2026

Copy link
Copy Markdown
Member

I'm rather happy now with this now! I want to merge #655 first and then rebase this. Not sure if I will be able to get it over the finish line before/during Wikimania but I hope so.

@Abbe98

Abbe98 commented Sep 2, 2026

Copy link
Copy Markdown
Member

EDIT: I refactored they query and it's now much faster. I haven't been able to get the following query through Qlever yet:

# expected_result_count: 429

PREFIX p: <http://www.wikidata.org/prop/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX schema: <http://schema.org/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdno: <http://www.wikidata.org/prop/novalue/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

SELECT DISTINCT
  ?qid
  ?orgLabel
  ?orgDescription
  ?type
  ?typeLabel
  ?country
WHERE {
  BIND(wd:Q55 AS ?country)

  {
  # agencies with country set to the Netherlands
  VALUES ?type {
    wd:Q3143387 # ministries (12)
    wd:Q4500821 # appeals court (4)
    wd:Q190752  # supreme court (2)
    wd:Q2125157 # district court (11)
    wd:Q134390  # provinces (12)
    wd:Q702081 # water boards (21)
    wd:Q2039348 # municipalities (342)
    wd:Q26934845 # integral overseas territory (3)
  }
  ?org wdt:P31 ?type .

  ?org wdt:P17 ?country .
  } UNION {
    # special case without a unique type
    VALUES ?org {
      wd:Q1857094 # cabinet of the King (1)
    }

    BIND("cabinet of the King" AS ?typeLabelCabinet)
  }
  UNION {
    ?org wdt:P31 wd:Q947233 . # high council of the state (6)
    BIND("High Council of State" AS ?typeLabelCouncil)
  }
  UNION {
    ?org wdt:P31 wd:Q138643360 . # inspectorates (15)
    BIND("Inspectorate" AS ?typeLabelInspectorate)
  }

  MINUS { ?org wdt:P576 [] . MINUS { ?item rdf:type wdno:P576 } }
  MINUS { ?org wdt:P1366 [] }
  MINUS { ?org wdt:P582 [] .
          MINUS { ?org wdt:P582 ?ddate ;
                       wdt:P580 ?idate .
                  FILTER ( ?idate > ?ddate )
               }
        }
  MINUS { 
    ?org p:P31 ?inStatement .
    ?inStatement pq:P582 [] .
    MINUS { ?org p:P31 ?p.
            ?p ps:P31 wd:Q2039348; pq:P580 ?start .
            FILTER NOT EXISTS { ?p pq:P582 []}
    }             
  }
  MINUS { ?org p:P31 ?p.
            ?p ps:P31 wd:Q2039348; pq:P580 ?start .
            FILTER ( ?start > NOW() )
  }

  BIND(REPLACE(STR(?org), "http://www.wikidata.org/entity/", "") AS ?qid)

  OPTIONAL { ?org rdfs:label ?orgLabelEn FILTER(LANG(?orgLabelEn) = "en") }
  OPTIONAL { ?org rdfs:label ?orgLabelNl FILTER(LANG(?orgLabelNl) = "nl") }
  OPTIONAL { ?org rdfs:label ?orgLabelMul FILTER(LANG(?orgLabelMul) = "mul") }
  BIND(COALESCE(?orgLabelEn, ?orgLabelNl, ?orgLabelMul, STR(?org)) AS ?orgLabel)

  OPTIONAL { ?org schema:description ?orgDescriptionEn FILTER(LANG(?orgDescriptionEn) = "en") }
  OPTIONAL { ?org schema:description ?orgDescriptionNl FILTER(LANG(?orgDescriptionNl) = "nl") }
  OPTIONAL { ?org schema:description ?orgDescriptionMul FILTER(LANG(?orgDescriptionMul) = "mul") }
  BIND(COALESCE(?orgDescriptionEn, ?orgDescriptionNl, ?orgDescriptionMul) AS ?orgDescription)

  OPTIONAL { ?type rdfs:label ?typeLabelEn FILTER(LANG(?typeLabelEn) = "en") }
  OPTIONAL { ?type rdfs:label ?typeLabelNl FILTER(LANG(?typeLabelNl) = "nl") }
  OPTIONAL { ?type rdfs:label ?typeLabelMul FILTER(LANG(?typeLabelMul) = "mul") }
  BIND(COALESCE(?typeLabelCabinet, ?typeLabelCouncil, ?typeLabelInspectorate, ?typeLabelEn, ?typeLabelNl, ?typeLabelMul, STR(?type)) AS ?typeLabel)
}
ORDER BY ?type ?typeLabel ?orgLabel

@fenymufyd

Copy link
Copy Markdown
Contributor Author

@Abbe98 this will fix the query so it can run on QLever. It seems we need to bind custom union type like Q1857094 to ?type first then bind that type label to custom ?typeLabel2 so the final bind coalesce works below it BIND(COALESCE(?typeLabel2, ?typeLabelEn, ?typeLabelNl, ?typeLabelMul, STR(?type)) AS ?typeLabel)

I also moved the wd:Q947233 # High Council of State (6) union to normal ?type above it because no value is being uncounted, which also saves a bit more time (around 200-500ms iirc)

# expected_result_count: 429

PREFIX p: <http://www.wikidata.org/prop/>
PREFIX pq: <http://www.wikidata.org/prop/qualifier/>
PREFIX ps: <http://www.wikidata.org/prop/statement/>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX schema: <http://schema.org/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX wdno: <http://www.wikidata.org/prop/novalue/>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>

SELECT DISTINCT
  ?qid
  ?orgLabel
  ?orgDescription
  ?type
  ?typeLabel
  ?country
WHERE {
  BIND(wd:Q55 AS ?country) # agencies with country set to the Netherlands

  {

    VALUES ?type { 
      wd:Q3143387 # ministries (12)
      wd:Q4500821 # appeals court (4)
      wd:Q190752  # supreme court (2)
      wd:Q2125157 # district court (11)
      wd:Q134390  # provinces (12)
      wd:Q702081 # water boards (21)
      wd:Q2039348 # municipalities (342)
      wd:Q26934845 # integral overseas territory (3)
      wd:Q947233 # High Council of State (6)
    }   
    
    ?org wdt:P31 ?type .
    
    ?org wdt:P17 ?country .
  } 
  UNION {
    VALUES ?org { wd:Q1857094 } # all instances of "Kabinet van de Koning" of netherlands (1)
    BIND(wd:Q1857094 AS ?type)  # bind custom type to ?type first
    BIND("cabinet of the King" AS ?typeLabel2)
  }
  UNION {
    ?org wdt:P31 wd:Q138643360 . # all instances of "Inspectorate" of netherlands, whether from mainland country (Q55) or the transcontinental kingdom (Q29999) (15)
    BIND(wd:Q138643360 AS ?type)
    BIND("Inspectorate" AS ?typeLabel2)
  }

  MINUS { ?org wdt:P576 [] }
  MINUS { ?org wdt:P1366 [] }
  MINUS { ?org wdt:P582 [] .
          MINUS { ?org wdt:P582 ?ddate ;
                       wdt:P580 ?idate .
                  FILTER ( ?idate > ?ddate )
               }
        }
  MINUS { 
    ?org p:P31 ?inStatement .
    ?inStatement pq:P582 [] .
    MINUS { ?org p:P31 ?p.
            ?p ps:P31 wd:Q2039348; pq:P580 ?start .
            FILTER NOT EXISTS { ?p pq:P582 []}
    }             
  }
  MINUS { ?org p:P31 ?p.
            ?p ps:P31 wd:Q2039348; pq:P580 ?start .
            FILTER ( ?start > NOW() )
  }

  BIND(REPLACE(STR(?org), "http://www.wikidata.org/entity/", "") AS ?qid)

  OPTIONAL { ?org rdfs:label ?orgLabelEn FILTER(LANG(?orgLabelEn) = "en") }
  OPTIONAL { ?org rdfs:label ?orgLabelNl FILTER(LANG(?orgLabelNl) = "nl") }
  OPTIONAL { ?org rdfs:label ?orgLabelMul FILTER(LANG(?orgLabelMul) = "mul") }
  BIND(COALESCE(?orgLabelEn, ?orgLabelNl, ?orgLabelMul, STR(?org)) AS ?orgLabel)

  OPTIONAL { ?org schema:description ?orgDescriptionEn FILTER(LANG(?orgDescriptionEn) = "en") }
  OPTIONAL { ?org schema:description ?orgDescriptionNl FILTER(LANG(?orgDescriptionNl) = "nl") }
  OPTIONAL { ?org schema:description ?orgDescriptionMul FILTER(LANG(?orgDescriptionMul) = "mul") }
  BIND(COALESCE(?orgDescriptionEn, ?orgDescriptionNl, ?orgDescriptionMul) AS ?orgDescription)

  OPTIONAL { ?type rdfs:label ?typeLabelEn FILTER(LANG(?typeLabelEn) = "en") }
  OPTIONAL { ?type rdfs:label ?typeLabelNl FILTER(LANG(?typeLabelNl) = "nl") }
  OPTIONAL { ?type rdfs:label ?typeLabelMul FILTER(LANG(?typeLabelMul) = "mul") }
  BIND(COALESCE(?typeLabel2, ?typeLabelEn, ?typeLabelNl, ?typeLabelMul, STR(?type)) AS ?typeLabel)
}
ORDER BY ?type ?typeLabel ?orgLabel

@RVA2869

RVA2869 commented Sep 14, 2026

Copy link
Copy Markdown
Collaborator

Could you also add these changes to ADD_A_COUNTRY.md?

@fenymufyd

Copy link
Copy Markdown
Contributor Author

Could you also add these changes to ADD_A_COUNTRY.md?

I've changed the markdown tutorials too at commit 552930f

@fenymufyd
fenymufyd requested a review from Abbe98 September 15, 2026 13:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make our SPARQL queries compatible with non-Blazegraph endpoints

3 participants