RDFLib / sparqlwrapper

A wrapper for a remote SPARQL endpoint
https://sparqlwrapper.readthedocs.io/
Other
513 stars 121 forks source link

using "describe" in a comment leads to error #211

Open WolfgangFahl opened 2 years ago

WolfgangFahl commented 2 years ago
# Count all academic conference(Q2020153) https://www.wikidata.org/wiki/Q2020153 items
# with the given described at URL(P973) https://www.wikidata.org/wiki/Property:P973 
SELECT ?item (COUNT (?value) AS ?count)
WHERE
{
  # instance of academic conference
  ?item wdt:P31 wd:Q2020153.
  # described at URL
  ?item wdt:P973 ?value.
} GROUP by ?item
HAVING (COUNT (?value) > 1)
ORDER BY DESC(?count)

will fail.

.. lib/python/site-packages/SPARQLWrapper/Wrapper.py:979: RuntimeWarning: Sending Accept header '/' because unexpected returned format 'json' in a 'DESCRIBE' SPARQL query form warnings.warn("Sending Accept header '/' because unexpected returned format '%s' in a '%s' SPARQL query form" % (self.returnFormat, self.queryType), RuntimeWarning)

WolfgangFahl commented 2 years ago

query


# Count all academic conference(Q2020153) https://www.wikidata.org/wiki/Q2020153 items
# with the given describ'd at URL(P973) https://www.wikidata.org/wiki/Property:P973 
SELECT ?item (COUNT (?value) AS ?count)
WHERE
{
  # instance of academic conference
  ?item wdt:P31 wd:Q2020153.
  # describ'd at URL
  ?item wdt:P973 ?value.
} GROUP by ?item
HAVING (COUNT (?value) > 1)
ORDER BY DESC(?count)

result

item count
http://www.wikidata.org/entity/Q106540635 3
http://www.wikidata.org/entity/Q106331820 2
eggplants commented 2 years ago
$ rqw --version
rqw 2.0.0
$ cat query.rq
# Count all academic conference(Q2020153) https://www.wikidata.org/wiki/Q2020153 items
# with the given described at URL(P973) https://www.wikidata.org/wiki/Property:P973 
SELECT ?item (COUNT (?value) AS ?count)
WHERE
{
  # instance of academic conference
  ?item wdt:P31 wd:Q2020153.
  # described at URL
  ?item wdt:P973 ?value.
} GROUP by ?item
HAVING (COUNT (?value) > 1)
ORDER BY DESC(?count)

$ rqw -e 'https://query.wikidata.org/sparql' -f query.rq
/Users/eggplants/.pyenv/versions/3.9.10/lib/python3.9/site-packages/SPARQLWrapper/Wrapper.py:794: RuntimeWarning: Sending Accept header '*/*' because unexpected returned format 'json' in a 'DESCRIBE' SPARQL query form
  warnings.warn(
{
    "head": {
        "vars": [
            "item",
            "count"
        ]
    },
    "results": {
        "bindings": [
            {
                "item": {
                    "type": "uri",
                    "value": "http://www.wikidata.org/entity/Q106540635"
                },
                "count": {
                    "datatype": "http://www.w3.org/2001/XMLSchema#integer",
                    "type": "literal",
                    "value": "3"
                }
            },
            {
                "item": {
                    "type": "uri",
                    "value": "http://www.wikidata.org/entity/Q106331820"
                },
                "count": {
                    "datatype": "http://www.w3.org/2001/XMLSchema#integer",
                    "type": "literal",
                    "value": "2"
                }
            }
        ]
    }
}

@WolfgangFahl What version are you using?

WolfgangFahl commented 2 years ago
pip list | grep -i sparqlwrapper
SPARQLWrapper                 1.8.5

since this seems not to be the most recent i did

pip install sparqlwrapper -U
rqw --version
rqw 2.0.0
python --version
Python 3.9.10
rqw -e 'https://query.wikidata.org/sparql' -f query.rq
/Users/wf/Library/Python/3.9/lib/python/site-packages/SPARQLWrapper/Wrapper.py:794: RuntimeWarning: Sending Accept header '*/*' because unexpected returned format 'json' in a 'DESCRIBE' SPARQL query form
  warnings.warn(
{
    "head": {
        "vars": [
            "item",
            "count"
        ]
    },
    "results": {
        "bindings": [
            {
                "item": {
                    "type": "uri",
                    "value": "http://www.wikidata.org/entity/Q106540635"
                },
                "count": {
                    "datatype": "http://www.w3.org/2001/XMLSchema#integer",
                    "type": "literal",
                    "value": "3"
                }
            },
            {
                "item": {
                    "type": "uri",
                    "value": "http://www.wikidata.org/entity/Q106331820"
                },
                "count": {
                    "datatype": "http://www.w3.org/2001/XMLSchema#integer",
                    "type": "literal",
                    "value": "2"
                }
            }
        ]
    }
}
WolfgangFahl commented 2 years ago

Why does the runtime warning lead to a failure in the programmatic access?

eggplants commented 2 years ago

What do you mean a failure in the programmatic access? the query seems to be successful.

WolfgangFahl commented 2 years ago

In the debugger: grafik grafik The query type is detected as "DESCRIBE" so there is no binding resolution.

WolfgangFahl commented 2 years ago

Oh 2.0.0 was just release today - now i get it. The pyLodStorage unit tests seem to fail with this see https://github.com/WolfgangFahl/pyLoDStorage/actions.

eggplants commented 2 years ago

The query type is detected as "DESCRIBE" so there is no binding resolution.

This apparently mismatch of query type maybe causes:

https://github.com/RDFLib/sparqlwrapper/blob/db695a579289f5b4e8b0d6ae9665c1e8894e1234/SPARQLWrapper/Wrapper.py#L616-L621

The pyLodStorage unit tests seem to fail with this see

2.0.0 has huge breaking changes. If you find any errors it would be great if you could send a fixture PR to sparqlwrapper as well as your project.

eggplants commented 2 years ago

@WolfgangFahl Rdflib members seems to be under-resourced. So I think it would be better if you create a PR that resolves this issue (and also your other Issues) rather than waiting for others.

WolfgangFahl commented 2 years ago

Just did a 0.1.19 release for pyLodStorage pinning sparqlwrapper to 1.8.5 i am now working on the 0.2.0 release intending to use the newer version of SPARQLWrapper. I'll have to work around the "described" issue for the time being in my downstream projects until things are fixed here. No i don't really have time to help with PRs - pyLodStorage was created as a separate project after #160 was not incorporated directly here. We could merge things again but that would be some major refactoring effort. I would love to have the named query and query result documentation support being integrated here for instance see https://github.com/WolfgangFahl/pyLoDStorage/discussions/78.

eggplants commented 2 years ago

Sparqlwrapper uses regular expressions for uncommenting queries and extracting types, which is a bad point and problematic. It may be better to use rdflib.plugins.sparql.parser.parseQuery(q) at SPARQLWrapper._parseQueryType(q). I'll try to do it later, and it will be deal with this issue.

WolfgangFahl commented 2 years ago

@eggplants - thx for your current engagement. When i wrote " No i don't really have time to help with PRs" - this means that currently i am busy. I have already closed one of the issues you mentioned and intend to do more in the future. I suggest to open the discussions here in github for being able to have some communication about ideas and other aspects that are not directly issue related.