SPARQL query containing 'coalesce' returns no result on rdflib-endpoint.SparqlEndpoint() - sparql

I am running a SparqlEndpoint from this package: https://pypi.org/project/rdflib-endpoint/
I define a graph,
g = Graph(), then
g.parse() a number of .ttl files and create the endpoint with SparqlEndpoint(graph=g).
Then I use uvicorn.run(app, ...) to expose the endpoint on my local machine.
I can successfully run this simple sparql statement to query for keywords:
PREFIX dcat: <http://www.w3.org/ns/dcat#>
SELECT
?keyword
WHERE { ?subj dcat:keyword ?keyword }
However, as soon as I add a coalesce statement, the query does not return results any more:
PREFIX dcat: <http://www.w3.org/ns/dcat#>
SELECT
?keyword
(coalesce(?keyword, "xyz") as ?foo)
WHERE { ?subj dcat:keyword ?keyword }
I tried something similar on wikidata which has no problems:
SELECT ?item ?itemLabel
(coalesce(?itemLabel, 2) as ?foo)
WHERE
{
?item wdt:P31 wd:Q146. # Must be of a cat
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}
Is there maybe an issue with the rdflib-endpoint.SparqlEndpoint()?
The server debug output for the keywords query looks ok to me.
INFO: 127.0.0.1:43942 - "GET /sparql?format=json&query=%0APREFIX+dcat%3A+%3Chttp%3A%2F%2Fwww.w3.org%2Fns%2Fdcat%23%3E%0ASELECT+%0A%3Fkeyword%0A%28coalesce%28%3Fkeyword%2C+%22xyz%22%29+as+%3Ffoo%29+%0AWHERE+%7B+%3Fsubj+dcat%3Akeyword+%3Fkeyword+%7D%0ALIMIT+10%0A HTTP/1.1" 200 OK
Putting COALESCE in the WHERE clause does not solve the problem:
PREFIX dcat: <http://www.w3.org/ns/dcat#>
SELECT
?keyword
?foo
WHERE {
?subj dcat:keyword ?keyword
BIND(coalesce(?keyword, 2) as ?foo)
}
TIA

Related

Wikidata SPARQL queries returning different results after filtering for English labels

My understanding of Wikidata SPARQL queries is that you can filter results for English labels in two ways.
Adding SERVICE wikibase:label { bd:serviceParam wikibase:language "en" } to invoke a label service; or
Adding ?thing rdfs:label ?thingLabel FILTER (lang(?thingLabel) = "en") for every output label.
I am running a query where I'm trying to get all properties of an entity in English. I followed a Stackoverflow post and came up with two queries.
Query 1: Running this query takes returns 47 results.
SELECT ?itemLabel ?propLabel ?statement_property_objLabel
WHERE {
VALUES (?item) {(wd:Q24)}
?item ?property [?statement_property ?statement_property_obj] .
?prop wikibase:claim ?property.
?prop wikibase:statementProperty ?statement_property.
# Call label service.
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" }
} ORDER BY ?propLabel
Query 2: Running this query returns 35 results.
SELECT ?itemLabel ?propLabel ?statement_property_objLabel
WHERE {
VALUES (?item) {(wd:Q24)}
?item ?property [?statement_property ?statement_property_obj] .
?prop wikibase:claim ?property.
?prop wikibase:statementProperty ?statement_property.
# Call label service for each label.
?item rdfs:label ?itemLabel FILTER (lang(?itemLabel) = "en") .
?statement_property_obj rdfs:label ?statement_property_objLabel FILTER (lang(?statement_property_objLabel) = "en") .
?prop rdfs:label ?propLabel FILTER (lang(?propLabel) = "en") .
} ORDER BY ?propLabel
Why is the second query returning fewer rows? Thanks for any help.
I think the cause is that the wikibase:label service returns label results for any value of ?statement_property_obj, even if that value has no actual rdfs:label defined (it appears to just return the actual value of ?statement_property_obj itself).
As an example, see the very first result in query 1, where ?statement_property_objLabel is bound to topic/Jack_Bauer. This is not the value of an actual rdfs:label property in the data, just a 'fallback' value that the label service provides. So query 2, which explicitly queries for rdfs:label attributes, won't return this (and similar) results.

How to follow a path depend on qualifier SPARQL

I like to query out all people who is connected to each other but filter by qualifier value of the path
For example, the query below will get all human related to Putin. But his spouse, whose in statement get the qualifier "endtime" should not be followed
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?pep ?pepLabel ?relation ?relationLabel ?relatedPerson ?relatedPersonLabel ?endtimequalifier
WHERE
{
VALUES ?pep {wd:Q7747}
?relatedPerson wdt:P31 wd:Q5.
?pep ?relation ?relatedPerson.
#What should I put here for the query to ignore the spouse since the endtimequalifier is available
OPTIONAL{
?pep p:P26 [ps:P26 ?spouse; pq:P582 ?endtimequalifier ].
}
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
}

Filtering URL value

What I am trying to do is to select a certain entity for the certain condition. If the condition is url, then how I can make a proper query to get the only result that matches the v1 = "https://www.getmagicbullet.com/" .
The commented sentences are what I tried different ways, but only to fail. How could I adjust the query to get the right answer?
Thank you for your help.
SELECT DISTINCT ?iLabel ?p ?v1
WHERE {
?i wdt:P31 wd:Q212920.
?i wdt:P856 ?v1.
# FILTER (?v1Label = "https://www.getmagicbullet.com/")
# { ?v1 rdfs:label "https://www.getmagicbullet.com/"#en }
# UNION { ?v1 skos:altLabel "https://www.getmagicbullet.com/"#en }
SERVICE wikibase:label { bd:serviceParam wikibase:language "ko,en,[AUTO_LANGUAGE]". }
}
LIMIT 1000

SPARQL query for finding films originating from and released in the United States

I have the following SPARQL query that appears to correctly produce the films produced in the US (country of origin) and released in the US (place of publication) in 2018. The issue I'm having is that one row is produced for each release even though the other releases are outside of the US. I've added a limit to reduce the size of the response.
Here is the query:
SELECT ?item ?name ?publication_date ?placeLabel WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
?item rdfs:label ?name;
wdt:P31 wd:Q11424;
wdt:P495 wd:Q30; # -> country of origin US
wdt:P577 ?publication_date.
?item p:P577 ?publication_statement.
?publication_statement pq:P291 ?place.
FILTER(xsd:date(?publication_date) > "2018-01-01"^^xsd:date)
FILTER(
(LANG(?name)) = "en"
&& ?place=wd:Q30) # -> place of publication
}
ORDER BY ?name
LIMIT 10
I would like to change it so that it produces one row per movie IF it had a release in the US in 2018.
Thanks for your help. Comments on the use of FILTER or other non idiomatic SPARQL are also welcome.
You can use GROUP BY:
SELECT ?item (SAMPLE(?name) as ?Name) (SAMPLE(?publication_date) as ?Date) WHERE {
SERVICE wikibase:label { bd:serviceParam wikibase:language "[AUTO_LANGUAGE],en". }
?item rdfs:label ?name;
wdt:P31 wd:Q11424;
wdt:P495 wd:Q30; # -> country of origin US
wdt:P577 ?publication_date.
?item p:P577 ?publication_statement.
?publication_statement pq:P291 ?place.
FILTER(xsd:date(?publication_date) > "2018-01-01"^^xsd:date)
FILTER(
(LANG(?name)) = "en"
&& ?place=wd:Q30) # -> place of publication
}
GROUP BY ?item
ORDER BY ?Name
LIMIT 10
See this query on Wikidata.
And you need to fix the SELECT line as you can't pass out the indeterminate non-group keys without explicitly saying. See similar question.

How to query Wikidata for "also known as"

I would like to know how to query Wikidata by using the alias ("also known as").
Right now I am trying
SELECT ?item
WHERE
{
?item rdfs:aliases ?alias.
FILTER(CONTAINS(?alias, "Angela Kasner"#en))
}
LIMIT 5
This is simply a query that works if I replace rdfs:aliases by rdfs:labels.
I am trying this, because Help:Aliases says that aliases are searchable in the same way as labels, but I can't find any other resource on that nor can I find an example.
This query might be helpful for someone querying also known as for properties:
SELECT ?property ?propertyLabel ?propertyDescription (GROUP_CONCAT(DISTINCT(?altLabel); separator = ", ") AS ?altLabel_list) WHERE {
?property a wikibase:Property .
OPTIONAL { ?property skos:altLabel ?altLabel . FILTER (lang(?altLabel) = "en") }
SERVICE wikibase:label { bd:serviceParam wikibase:language "en" .}
}
GROUP BY ?property ?propertyLabel ?propertyDescription
LIMIT 5000