From 5d9e710bda7458e2fd792a7a340f4d5300eafc2e Mon Sep 17 00:00:00 2001 From: Hannah Bast Date: Tue, 5 Mar 2024 23:34:01 +0100 Subject: [PATCH] Try it with generic COUNT queries So far, there was an explicit COUNT query for each group of backends. Now the COUNT query is simply a variant of the query that gets the geometries, namely with `SELECT (COUNT(?geometry) AS ?count) { ... }` around it. The old code is still there. Will clean this up in the next commit. --- src/qlever-petrimaps/GeomCache.cpp | 47 +++++++++++++++++++++--------- src/qlever-petrimaps/GeomCache.h | 2 +- src/util | 2 +- 3 files changed, 35 insertions(+), 16 deletions(-) diff --git a/src/qlever-petrimaps/GeomCache.cpp b/src/qlever-petrimaps/GeomCache.cpp index ff8e584..16985f0 100644 --- a/src/qlever-petrimaps/GeomCache.cpp +++ b/src/qlever-petrimaps/GeomCache.cpp @@ -28,37 +28,37 @@ using util::geo::latLngToWebMerc; const static std::string QUERY = "PREFIX geo: " "SELECT ?geometry WHERE {" - " ?osm_id geo:hasGeometry ?geometry " + " ?subject geo:hasGeometry ?geometry " "} INTERNAL SORT BY ?geometry"; const static std::string COUNT_QUERY = "PREFIX geo: " - "SELECT (COUNT(?geometry) as ?count) WHERE { " - " ?osm_id geo:hasGeometry ?geometry " + "SELECT (COUNT(?geometry) as ?count) WHERE {" + " ?subject geo:hasGeometry ?geometry " "}"; const static std::string QUERY_ASWKT = "PREFIX geo: " "SELECT ?geometry WHERE {" - " ?osm_id geo:hasGeometry ?m . ?m geo:asWKT ?geometry " + " ?subject geo:hasGeometry ?m . ?m geo:asWKT ?geometry " "} INTERNAL SORT BY ?geometry"; const static std::string COUNT_QUERY_ASWKT = "PREFIX geo: " "SELECT (COUNT(?geometry) AS ?count) WHERE {" - " ?osm_id geo:hasGeometry ?m . ?m geo:asWKT ?geometry " + " ?subject geo:hasGeometry ?m . ?m geo:asWKT ?geometry " "}"; const static std::string QUERY_WD = "PREFIX wdt: " - "SELECT ?coord WHERE {" - " ?ob wdt:P625 ?coord" - "} INTERNAL SORT BY ?coord"; + "SELECT ?geometry WHERE {" + " ?subject wdt:P625 ?geometry" + "} INTERNAL SORT BY ?geometry"; const static std::string COUNT_QUERY_WD = "PREFIX wdt: " - "SELECT (COUNT(?coord) as ?count) WHERE { " - " ?ob wdt:P625 ?coord" + "SELECT (COUNT(?geometry) as ?count) WHERE { " + " ?subject wdt:P625 ?geometry" "}"; // Helper function that returns one of the given three query strings based on @@ -75,7 +75,9 @@ static const std::string &selectQueryBasedOnUrl(const std::string &backendUrl, auto backendStartsWith = [&pos, &backendUrl](const std::string &prefix) { return backendUrl.find(prefix, pos) == pos; }; - if (backendStartsWith("osm")) { + if (backendStartsWith("osm-germany")) { + return query3; + } else if (backendStartsWith("osm") || backendStartsWith("ohm")) { return query1; } else if (backendStartsWith("wikidata") || backendStartsWith("dblp")) { return query2; @@ -90,10 +92,27 @@ const std::string &GeomCache::getQuery(const std::string &backendUrl) const { } // _____________________________________________________________________________ -const std::string &GeomCache::getCountQuery( +std::string GeomCache::getCountQuery( const std::string &backendUrl) const { - return selectQueryBasedOnUrl(backendUrl, COUNT_QUERY_ASWKT, COUNT_QUERY_WD, - COUNT_QUERY); + // Modify the query from `getQuery` to count the number of geometries. + std::string query = getQuery(backendUrl); + auto pos = query.find("SELECT"); + if (pos == std::string::npos) { + LOG(ERROR) << "Could not find SELECT in query: " << query; + return "SELECT ?count WHERE { VALUES ?count { 0 } }"; + } + query.insert(pos, "SELECT (COUNT(?geometry) AS ?count) WHERE { "); + // INTERNAL SORT BY ?subject instead of INTERNAL SORT BY ?geometry + // for the COUNT query (because the former query is cached). + // auto sortPos = query.find("INTERNAL SORT BY ?geometry"); + // if (sortPos == std::string::npos) { + // LOG(ERROR) << "Could not find INTERNAL SORT BY ?geometry in query: " + // << query; + // return "SELECT ?count WHERE { VALUES ?count { 0 } }"; + // } + // query.replace(sortPos, std::string::npos, "INTERNAL SORT BY ?subject }"); + query.append(" }"); + return query; } // _____________________________________________________________________________ diff --git a/src/qlever-petrimaps/GeomCache.h b/src/qlever-petrimaps/GeomCache.h index 78ae691..e54dc59 100644 --- a/src/qlever-petrimaps/GeomCache.h +++ b/src/qlever-petrimaps/GeomCache.h @@ -115,7 +115,7 @@ class GeomCache { // Get the right SPARQL query for the given backend. const std::string& getQuery(const std::string& backendUrl) const; - const std::string& getCountQuery(const std::string& backendUrl) const; + std::string getCountQuery(const std::string& backendUrl) const; std::string requestIndexHash(); diff --git a/src/util b/src/util index 4536392..1d434c4 160000 --- a/src/util +++ b/src/util @@ -1 +1 @@ -Subproject commit 45363920b8803b06b44dc9cae707f375c4495123 +Subproject commit 1d434c43989054f2a7fc77cbf3a5760b7412a310