RDFLib / sparqlwrapper

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

SPARQL malformed query #133

Closed novkor closed 4 years ago

novkor commented 5 years ago

Hey guys.

Im trying to build a query to get the type of a resource. My query looks like this :

query="""SELECT DISTINCT ?label WHERE { ?type dbp:type dbr:Dad's_Root_Beer_brands . OPTIONAL{ ?type rdfs:label ?label .} FILTER (lang(?label) = "en" ) }"""

The error i get : QueryBadFormed: QueryBadFormed: a bad request has been sent to the endpoint, probably the sparql query is bad formed. Response: b'Virtuoso 37000 Error SP030: SPARQL compiler, line 0: End-of-line in a short single-quoted string at \'\'s_Root_Beer_brands . \n\'\n\nSPARQL query:\n\n#output-format:application/sparql-results+json\nselect distinct ?label \nwhere { \n?type dbp:type dbr:Dad\'s_Root_Beer_brands . \nOPTIONAL{ ?type rdfs:label ?label .} \nFILTER (lang(?label) = "en" ) \n}'

My problem comes with the dbr:Dad's_Root_Beer_brands part. The apostrophe in Dad's seems like can't be escaped. When i try to escape it with \ so it would be Dad\'s it fails.

With the query above i get: "queryString" : 'select distinct ?label \nwhere { \n?type dbp:type dbr:Dad\'s_Root_Beer_brands . \nOPTIONAL{ ?type rdfs:label ?label .} \nFILTER (lang(?label) = "en" ) \n}' as the query string, if i try to escape it i get the same queryString.

To me it looks like that the apostrophe isn't or can't be escaped.

I'm pretty new to SPARQL, checked the documentation where they state that you can use as many ' and " as you like if you write your query between """ """ or ''' '''. I can escape any other special character in my query with \, but apostrophe.

I spent hours looking for a solution but could not find any. Did any of you ever encountered with this issue, or see some error in my query that can cause this error?

Thanks in advance.

dayures commented 5 years ago

Hi @novkor ! As a quick comment, did you see this issue https://github.com/RDFLib/sparqlwrapper/issues/94 ?? Maybe it is related.

BTW, what endpoint engine are you querying (Virtuoso, Fuseki2, Allegrograph...) ?

novkor commented 5 years ago

Hi @dayures ! Haven't seen it yet, took a quick look. I had to escape commas as well, but i could manage to do that. As i mentioned i can escape every special character, but apostrophe. :( Maybe it is indeed related, but i'm not sure yet. When i take a look on the querystring i am constructing, i can only see that like it tries to escape apostrophe by default, and if i try to escape it with a backslash (as i escape other special characters), the querystring does not change. If i try with 2 backslashes, then the querystring contains 3 backslashes before the apostrophe. As i said i am a newbie, so not sure if this is how it should work, but i doubt that.

I'm using Virtuoso.

Thanks

dayures commented 5 years ago

Hi @novkor

I had a look on this case and I was able to run the query with the apostrophe. Please, take into account that the query is different, because I couldn't find in dbpedia the dbp:type relation.

query = """ SELECT DISTINCT ?label WHERE { ?type dct:subject <http://dbpedia.org/resource/Category:Dad's_Root_Beer_brands> . OPTIONAL{ ?type rdfs:label ?label .} FILTER (lang(?label) = "en" ) }"""

query = """ SELECT DISTINCT ?label WHERE { ?type dct:subject dbr:Category:Dad\\'s_Root_Beer_brands . OPTIONAL{ ?type rdfs:label ?label .} FILTER (lang(?label) = "en" ) }"""

Could you try and give us your feedback? Thanks!

dayures commented 4 years ago

Thanks for contributing to this issue. As it has been more than 90 days since the last activity, we are automatically closing the issue. This is often because the request was already solved in some way and it just wasn't updated or it's no longer applicable. If that's not the case, please do feel free to either reopen this issue or open a new one. We'll gladly take a look again!