Skip to content

Instantly share code, notes, and snippets.

@kasei
Last active January 15, 2021 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kasei/2543d93652016e0bb860 to your computer and use it in GitHub Desktop.
Save kasei/2543d93652016e0bb860 to your computer and use it in GitHub Desktop.
SPARQL FILTER variable substitution

This test tries to show what happens during variable substitution when an EXISTS filter pattern with a sub-query is evaluated. The results from running it under both ARQ and RDF::Query contradict my understanding of how SPARQL filter and EXISTS evaluation are meant to occur.

test.ttl

<x> <seq> 2 ;
	<even> 2, 4 .

<y> <seq> 5 ;
	<even> 2, 4, 6 .

test.rq

# Find all ?s that have a <seq> value that is equal to the
# number of <even> values belonging to that ?s
SELECT ?s WHERE {
	?s <seq> ?value .
	FILTER EXISTS {
		?s <seq> ?count
		{
			SELECT (COUNT(*) AS ?count) WHERE {
				?s <even> ?other
			}
		}
	}
}

Based on variable substitution for EXISTS, I expect both appearances of ?s in the EXISTS pattern to be replaced during filtering. However, the results from existing implementations seems to confict with this interpretation, instead indicating that they are evaluating the aggregation without variable substitution (getting a ?count value of 5 instead of 2 and 3 (for ?s=<x> and ?s=<y>, respectively).

Expected results (Sesame/2.7.11):

-------
| s   |
=======
| <x> |
-------

Jena ARQ/2.11.1, dotNetRDF/1.0.5, and RDF::Query/2.910 results:

-------
| s   |
=======
| <y> |
-------
@kasei
Copy link
Author

kasei commented Apr 30, 2014

I'm fairly certain that RDF::Query is producing the wrong results because of a bug that isn't doing the proper variable substitution in aggregates (as a result of how variable substitution is implemented).

@afs
Copy link

afs commented May 19, 2014

(summary response) It should not matter if the inner SELECT were:

 SELECT (COUNT(*) AS ?count) WHERE {
                ?x <even> ?other
            }

because that ?s never comes in contact with the outer ?s and hence not the top of the EXISTS.

@kasei
Copy link
Author

kasei commented May 28, 2014

Following up based on twitter conversation, it seems the spec text may disagree with the common implementation approach.

@abrokenjester
Copy link

Sesame (2.7.11) produces what you call the expected result (?s = x), so different from Jena and RDF::Query. I have not yet fully wrapped my head around the case to figure out who is right or wrong though.

@rvesse
Copy link

rvesse commented May 29, 2014

dotNetRDF (1.0.5) agrees with ARQ and RDF::Query

@kasei
Copy link
Author

kasei commented Jun 3, 2014

Jeen -- I think Andy and I agree that you're following the spec, but that what ended up in the spec might not have been the original intention (and it seems many implementations diverge from the spec text in this case).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment