You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@jena.apache.org by Rob Vesse <rv...@yarcdata.com> on 2013/08/02 18:52:48 UTC
Initial Bindings in Query Evaluation
Hi All
Holger's question (http://mail-archives.apache.org/mod_mbox/jena-users/201308.mbox/%3c51FB8B52.7070805@knublauch.com%3e) about a regression in ARQs treatment of initial bindings raises an interesting disconnect between the interpretation of SPARQL and the Initial Bindings API.
Initial bindings in their current form allows for users to essentially change the semantics of a query in a non-intuitive way. Take his example query:
ASK { FILTER(?a = ?b) }
Intuitively that query MUST always return false yet with initial bindings in the mix the query can be made to return true, at least prior to 2.10.2 which introduces a new optimizer which includes special case recognition for this.
The problem is that using initial bindings can fundamentally change the semantics of queries in non-intuitive ways when I believe the intention of the API was merely to allow for improved performance by guiding the engine.
To me this suggests that initial bindings as currently implemented is fundamentally flawed and I would suggest that we think about re-architecting this feature in a future release (not the next release). I believe there are probably several ways of doing this:
1 – Remove support for initial bindings on queries entirely (as we already did for updates) in favor of using ParameterizedSparqlString
2 – Change initial bindings to be a pre-optimization algebra transformation of the query
As we've discussed previously in the context of ParameterizedSparqlString there is potential to do the substitution at the algebra tree level rather than at the textual level. This allows for stronger syntax checking and actually changes the query appropriately. The problem with this is that it doesn't work if we want to inject multiple values for a variable, hence Option 3
3 – Change initial bindings to be done by injection of VALUES clauses
This approach is again by algebra transform and would involve inserting VALUES clauses at each leaf of the algebra tree. So Holger's query with initial bindings applied would be rewritten like so:
ASK
{
VALUES ( ?a ?b ) { ( true true ) }
FILTER (?a = b)
}
However this approach might get rather complex for larger queries and also runs into issues of scope, what if we insert the VALUES clause inside of a sub-query which doesn't propagate those initial bindings outside of it etc.
4 – Skip optimization when initial bindings are involved
This is the easiest approach but we can't enforce this on other query engine implementations and it could seriously harm performance for those that use initial bindings extensively.
There may also be other approaches I haven't thought so please suggest anything that makes sense. Bottom line is that initial bindings in its current form seems fundamentally broken to me and we should be thinking of how to fix this in the future.
Rob
Re: Initial Bindings in Query Evaluation
Posted by Holger Knublauch <ho...@knublauch.com>.
On 8/8/2013 9:27, Rob Vesse wrote:
> The way that past versions of ARQ implemented initial bindings for updates
> is that the initial bindings were only fed into the WHERE portion of
> queries. Also this was not done via substitution., any restoration of
> initial bindings of updates would likely need to not use substitution.
> Although this may be moot because I don't know whether ARQ applies query
> optimization to WHERE portions of updates.
>
> This meant that the variable having a value injected still existed in both
> the WHERE and template portions so if a blank node value is bound it is as
> if the value is simply copied from the WHERE portion to the template
> portion. Whereas with the ParameterizedSparqlString the behavior is
> substitution for a blank node so the template portion has a blank node in
> place of a variable hence generation of a new blank node rather than
> copying an existing one.
Good - thanks for clarifying.
Holger
Re: Initial Bindings in Query Evaluation
Posted by Rob Vesse <rv...@yarcdata.com>.
The way that past versions of ARQ implemented initial bindings for updates
is that the initial bindings were only fed into the WHERE portion of
queries. Also this was not done via substitution., any restoration of
initial bindings of updates would likely need to not use substitution.
Although this may be moot because I don't know whether ARQ applies query
optimization to WHERE portions of updates.
This meant that the variable having a value injected still existed in both
the WHERE and template portions so if a blank node value is bound it is as
if the value is simply copied from the WHERE portion to the template
portion. Whereas with the ParameterizedSparqlString the behavior is
substitution for a blank node so the template portion has a blank node in
place of a variable hence generation of a new blank node rather than
copying an existing one.
Rob
On 8/7/13 4:16 PM, "Holger Knublauch" <ho...@knublauch.com> wrote:
>On 8/8/2013 9:02, Rob Vesse wrote:
>> Sorry I am snowed under with work atm.
>
>Sorry me too - working towards a release.
>
>> I have not tested but I would be
>> tempted to enable and change the tests so that they reflect Holger's
>> original regression test case. This would realign the behavior with
>> previous versions of ARQ and fix the regression
>>
>>
>> On a related topic I looked at Holger's question around injecting BNodes
>> into SPARQL updates via ParameterizedSparqlString and it doesn't work in
>> the scenario he describes (an INSERT WHERE) if the variable is used both
>> in the INSERT template and WHERE since the template mention is treated
>>as
>> a minting a fresh BNode. Either he needs to use the BIND workaround
>> discussed by yourself in another thread
>> (http://markmail.org/message/3lsnjq7yca4es2wb) which I suspect is not
>> workable for TQ OR we need to look at restoring initial bindings for
>> updates.
>>
>> I think restoring the feature is going to be the best option, the
>> documentation just needs to be really clear that initial bindings only
>> apply to the WHERE portion of updates and not more generally since that
>>is
>> the only way they were used prior to the feature being removed (I went
>> back and looked at the ARW 2.9.4 code). We can always look at expanding
>> their scope later as we've discussed in the past.
>
>I would appreciate restoring this. I don't understand the last section
>though - the scenario with pre-bound variables in the INSERT *did* work
>in 2.9.2. It will be hard to explain to users that INSERT behaves
>differently from CONSTRUCT. Furthermore, most examples of updates in our
>code base either take URIs or bnodes as parameters, and if I understand
>the limitation correctly then I would need to rewrite them all to copy
>any variable with another BIND.
>
>Thanks,
>Holger
>
Re: Initial Bindings in Query Evaluation
Posted by Holger Knublauch <ho...@knublauch.com>.
On 8/8/2013 9:02, Rob Vesse wrote:
> Sorry I am snowed under with work atm.
Sorry me too - working towards a release.
> I have not tested but I would be
> tempted to enable and change the tests so that they reflect Holger's
> original regression test case. This would realign the behavior with
> previous versions of ARQ and fix the regression
>
>
> On a related topic I looked at Holger's question around injecting BNodes
> into SPARQL updates via ParameterizedSparqlString and it doesn't work in
> the scenario he describes (an INSERT WHERE) if the variable is used both
> in the INSERT template and WHERE since the template mention is treated as
> a minting a fresh BNode. Either he needs to use the BIND workaround
> discussed by yourself in another thread
> (http://markmail.org/message/3lsnjq7yca4es2wb) which I suspect is not
> workable for TQ OR we need to look at restoring initial bindings for
> updates.
>
> I think restoring the feature is going to be the best option, the
> documentation just needs to be really clear that initial bindings only
> apply to the WHERE portion of updates and not more generally since that is
> the only way they were used prior to the feature being removed (I went
> back and looked at the ARW 2.9.4 code). We can always look at expanding
> their scope later as we've discussed in the past.
I would appreciate restoring this. I don't understand the last section
though - the scenario with pre-bound variables in the INSERT *did* work
in 2.9.2. It will be hard to explain to users that INSERT behaves
differently from CONSTRUCT. Furthermore, most examples of updates in our
code base either take URIs or bnodes as parameters, and if I understand
the limitation correctly then I would need to rewrite them all to copy
any variable with another BIND.
Thanks,
Holger
Re: Initial Bindings in Query Evaluation
Posted by Andy Seaborne <an...@apache.org>.
On 08/08/13 00:02, Rob Vesse wrote:
> Sorry I am snowed under with work atm. I have not tested but I would be
> tempted to enable and change the tests so that they reflect Holger's
> original regression test case. This would realign the behavior with
> previous versions of ARQ and fix the regression
Change made.
Andy
Re: Initial Bindings in Query Evaluation
Posted by Rob Vesse <rv...@yarcdata.com>.
Sorry I am snowed under with work atm. I have not tested but I would be
tempted to enable and change the tests so that they reflect Holger's
original regression test case. This would realign the behavior with
previous versions of ARQ and fix the regression
On a related topic I looked at Holger's question around injecting BNodes
into SPARQL updates via ParameterizedSparqlString and it doesn't work in
the scenario he describes (an INSERT WHERE) if the variable is used both
in the INSERT template and WHERE since the template mention is treated as
a minting a fresh BNode. Either he needs to use the BIND workaround
discussed by yourself in another thread
(http://markmail.org/message/3lsnjq7yca4es2wb) which I suspect is not
workable for TQ OR we need to look at restoring initial bindings for
updates.
I think restoring the feature is going to be the best option, the
documentation just needs to be really clear that initial bindings only
apply to the WHERE portion of updates and not more generally since that is
the only way they were used prior to the feature being removed (I went
back and looked at the ARW 2.9.4 code). We can always look at expanding
their scope later as we've discussed in the past.
Rob
On 8/7/13 3:53 PM, "Andy Seaborne" <an...@apache.org> wrote:
>Holger, everyone - ping.
>
>Anyone tried this (it's currently "if (false &&"-ed out).
>Shall I enabled in trunk (and change the two affected tests)?
>
> Andy
>
>On 05/08/13 20:52, Andy Seaborne wrote:
>> I've checked into QueryEngineBase (line 96) code to substitute variables
>> before algebra optimization, rather than the current approach which
>> optimizes then expects the storage query engine to deal with it.
>>
>> This does not apply to QueryEngineHTTP (this has never supported initial
>> bindings).
>>
>> This does not apply to updates.
>>
>> Please take a look and say what you think.
>> if you enable the code, two test will fail -
>>
>> com.hp.hpl.jena.sparql.api.TestAPI
>>
>> testInitialBindings5
>>
>> ASK WHERE { FILTER (?a = <http://constant>) }
>> substituting <http://constant> for ?a
>>
>> testInitialBindings6
>>
>> ASK WHERE { FILTER (?a = ?b) }
>> substituting value true for ?a and ?b
>>
>> this is correct for the substitution-before-optimization design.
>>
>> Andy
>>
>>
>
Re: Initial Bindings in Query Evaluation
Posted by Andy Seaborne <an...@apache.org>.
Holger, everyone - ping.
Anyone tried this (it's currently "if (false &&"-ed out).
Shall I enabled in trunk (and change the two affected tests)?
Andy
On 05/08/13 20:52, Andy Seaborne wrote:
> I've checked into QueryEngineBase (line 96) code to substitute variables
> before algebra optimization, rather than the current approach which
> optimizes then expects the storage query engine to deal with it.
>
> This does not apply to QueryEngineHTTP (this has never supported initial
> bindings).
>
> This does not apply to updates.
>
> Please take a look and say what you think.
> if you enable the code, two test will fail -
>
> com.hp.hpl.jena.sparql.api.TestAPI
>
> testInitialBindings5
>
> ASK WHERE { FILTER (?a = <http://constant>) }
> substituting <http://constant> for ?a
>
> testInitialBindings6
>
> ASK WHERE { FILTER (?a = ?b) }
> substituting value true for ?a and ?b
>
> this is correct for the substitution-before-optimization design.
>
> Andy
>
>
Re: Initial Bindings in Query Evaluation
Posted by Andy Seaborne <an...@apache.org>.
I've checked into QueryEngineBase (line 96) code to substitute variables
before algebra optimization, rather than the current approach which
optimizes then expects the storage query engine to deal with it.
This does not apply to QueryEngineHTTP (this has never supported initial
bindings).
This does not apply to updates.
Please take a look and say what you think.
if you enable the code, two test will fail -
com.hp.hpl.jena.sparql.api.TestAPI
testInitialBindings5
ASK WHERE { FILTER (?a = <http://constant>) }
substituting <http://constant> for ?a
testInitialBindings6
ASK WHERE { FILTER (?a = ?b) }
substituting value true for ?a and ?b
this is correct for the substitution-before-optimization design.
Andy
Re: Initial Bindings in Query Evaluation
Posted by Andy Seaborne <an...@apache.org>.
The problem is that the subsitution is done after optimization but it
still is substitution semantics.
Currently, execution proceeds as:
QueryEngineBase.createPlan:
Op op = modifyOp(queryOp) ;
eval(op, dsg, binding, context) ;
and modifyOp does the highlevel optimizations as selected by the engine.
At evaluation time, something like this happens in eval for each query
engine:
if ( ! input.isEmpty() )
op = Substitute.substitute(op, input) ;
We can:
A/ Do early algebra substitution:
Advantage: gives the optimizer more chance to do a good jon, especially
on FILTER (?x = ?param)
Disadvantage:
If Service.exec changes to use the original input syntax, it breaks. See
note in that method.
The change is:
QueryEngineBase
protected Plan createPlan()
{
// Decide the algebra to actually execute.
Op op = queryOp ;
*** New code ***
if ( ! startBinding.isEmpty() ) {
op = Substitute.substitute(op, startBinding) ;
context.put(ARQConstants.sysCurrentAlgebra, op) ;
// Don't reset the startBinding because it also is needed
// in the output.
}
*** New code ***
op = modifyOp(op) ;
doing it in setOp() may be better.
This works - testInitialBindings5 and testInitialBindings6 then fail as
expected.
Query engines then don't need to do this step - they still need to
create bindings including the initial conditions (else CONSTRUCT does
not work).
B/ Do abstract query syntax substitution.
Do this at inside QueryExecutionBase.setInitialBinding.
To this end I have now committed code that does abstract query syntax
substitution that I wrote as an experiment during the last discussions:
https://svn.apache.org/repos/asf/jena/Scratch/AFS/Jena-Dev/trunk/src/element/
This is not advocacy of this approach - it's giving us choices. It also
sort-of works for updates. (Biggest hole is in testing - updates don't
support structure .equals).
Substitution and Update:
We need something. The most natural case IMO is
INSERT DATA { ?param1 foaf:name ?param2 }
so rewriting the abstract syntax tree will not currently work - that's
illegal syntax for an INSERT DATA. The parser could be modified and the
"no vars" check done later.
The current parametrized SPARQL Strings can do this. A touch odd we
have two separate mechanisms.
Multiple Values:
Whatever mechanism we end up with, the semantics of multiple values
should be loop-substitute.
Rob's list:
> 1 – Remove support for initial bindings on queries entirely
AFS: -1
> 2 – Change initial bindings to be a pre-optimization algebra
> transformation of the query
2a – Algebra -> Algebra done before optimization (A)
2b – AST -> AST done before creating the query engine (B)
AFS: +1 (A is tested, and seems to work).
Note the checked-in code for B does the case of
SELECT ?x {}
==>
SELECT (1 AS ?x) {}
which parametrized SPARQL strings and algebra->algebra does not.
> 3 – Change initial bindings to be done by injection of VALUES clauses
AFS: -0.5 - seems complicated and I'm not clear it will work (I'd need
more time to think about it).
> However this approach might get rather complex
Yes - complicated - certainly it isn't a simple add VALUES to the top
level pattern but I'd have to think harder to know whether it interacts
with scoping and name substitution.
Need to be very careful that it does not assume ARQ's current execution
strategy which is scope sensitive.
4 – Skip optimization when initial bindings are involved
AFS: -1
This isn't per-se an optimization issue - the feature is there for
taking information obtained elsewhere - it does work for a graph-wide
query becoming quite local and a big advantage.
Andy
RE: Initial Bindings in Query Evaluation
Posted by Rob Vesse <rv...@yarcdata.com>.
Thought I should add that I am in favour of keeping the API
My preference is for changing the under the hood implementation to use some combination of Options 2 and 3 as a pre-optimization algebra transformation giving some level of cached execution plans
Rob
________________________________________
From: Rob Vesse [rvesse@cray.com]
Sent: 02 August 2013 16:50
To: dev@jena.apache.org
Subject: Re: Initial Bindings in Query Evaluation
Holger
Comments inline:
On 8/2/13 3:50 PM, "Holger Knublauch" <ho...@knublauch.com> wrote:
>Folks,
>
>all I did was report a bug, and your response is to delete the whole
>feature! This would completely throw out the baby with the bathwater and
>may mean that we at TopQuadrant have to branch off to our own Jena
>version when this happens.
I started the discussion because what your bug highlights is that initial
bindings as they are currently implemented are broken, we appear to
disagree on how they are broken but clearly they are not entirely correct
in their behavior one way or the other.
I'm aiming to solicit opinions from all interested parties and try to get
a consensus about how this feature should be handled in the future.
Whether that means merely fixing the current implementation, replacing it
with another solution or something else entirely. Note that I explicitly
said that this is about re-architecting this feature in some future
release, no-one is going into the code base and deleting this feature
immediately. If the eventual decision is to remove this feature in its
current form then there would be a deprecation cycle.
Forcing TQ to fork Jena is not my intention, I'm trying to find an
appropriate means to address something which I consider to be broken in a
way that benefits the whole community. Note that there are other places
where initial bindings are completely unimplemented/broken depending on
your POV e.g. remote queries.
>
>I would be in favor of this option
>
>5) Keep the API as it is and restore initial bindings for UPDATE as well.
Side Note - Initial bindings for updates was removed because it was a
barrier to streaming updates
(http://markmail.org/message/bazwh2exmcc5vmoh). Also as others noted in
the discussion there initial bindings is a little murkier for updates
since does it apply only to WHERE clauses, to all portions of requests,
etc?
Keeping the API as-is is always an option, if this ends up being the
preference of the community then we definitely need to improve the
documentation to note that there can be unintended interactions with other
parts of the query engine such as the optimizer when initial bindings are
used.
>
>It was IMHO a mistake to switch to parameterized queries. We make very
>heavy use of initial bindings throughout our software stack, SPIN, SWP,
>SPARQLMotion. They are a great feature, allowing users to treat SPARQL
>like a programming language (think about a function call, where the
>parameters are different each time, yet the developer only needs to
>write the logic once).
What you are talking about sounds much more like cached execution plans in
SQL. I understand the analogy of a SPARQL query to a function but SPARQL
variables were not intended to be function arguments, that you choose to
treat them as such and that initial bindings lets you treat them as such
is a perhaps unintended consequence of ARQ's API.
>
> From what I understand so far, parameterized SPARQL has performance
>issues - we would need to re-parse the string while with initial
>bindings we can reuse the same (compiled) Query object each time.
>Parameterized queries also don't support the bound(?var) operator, and
>probably others.
Yes there is a performance hit associated with parameterized queries and
yes it is possible to turn a syntactically valid query into an invalid one
by injecting certain values because of the way the parameterization is
done on a purely textual basis.
This is why I suggested Options 2 and 3 which are algebraic manipulations
which would allow the initial bindings to be injected by transformation of
the unoptimized algebra tree. This allows you to only take the parsing
hit once, use initial bindings as you do now without the optimizer making
incorrect decisions because of information that initial bindings gives
that it doesn't have access to. These options require substantially more
work to get all the kinks worked out but are undoubtably doable, if people
agree this is the way forward then likely this will happen eventually but
contributions to make it happen sooner are always welcome. This would
give users something much closer to cached execution plans which from what
you described is essentially how TQ are using initial bindings.
Also note that as a user you always have the option of using the BIND
and/or VALUES clauses in your queries and updates which achieves the same
ends as the initial bindings API and has the benefit of being semantically
much clearer from the POV of both the query writer and query engine.
>
>We have used initial bindings successfully for many years, and although
>there have been occasional bugs (I probably reported one such bug per
>year) it is a very essential feature from our point of view. If the
>overhead of fixing the optimizer is too big, I would be OK with
>switching off this optimizer if initial bindings are present. It worked
>fine without this optimizer for many years.
I understand that TQ considers the feature essential and I hope we as a
community can come to some agreement on this topic that satisfies the
whole community.
The optimizer is not broken, what the optimizer is doing is entirely
correct wrt to the semantics of the SPARQL query it is given and the
information it has. As I pointed out in my original reply to you there
are other optimizations that have had the same behavior for years. That
none of your unit tests previously covered a case which would be affected
in this way whereas the new optimization does happen to cover such a case
is a lucky coincidence as otherwise neither you/ourselves would have been
any the wiser.
The example query semantically should always return false, that the
initial bindings API allows a user to make it do otherwise is the bug IMO
though I think it is clear we disagree on this point.
Individual optimizations can always be turned off, for this specific one:
ARQ.getContext().set(ARQ.optFilterImplicitJoin, false);
If FILTER(?x = ?y) and FILTER(?x = <uri>) are common patterns that TQ and
SPIN users make use of in conjunction with initial bindings then you
should perhaps turn both this and ARQ.optFilterEquality off. Though if
the problem is only in strange corner cases like the example you gave then
you may be perfectly fine since the special case causing you problems only
occurs when the variables are not used within the inner operator that the
FILTER applies over.
Switching the whole optimizer off is overkill and will almost certainly
harm performance because that will include disabling critical
optimizations such as ARQs index join strategy.
Rob
>
>HTH
>Holger
>
>
>On 8/3/2013 2:52, Rob Vesse wrote:
>> Hi All
>>
>> Holger's question
>>(http://mail-archives.apache.org/mod_mbox/jena-users/201308.mbox/%3c51FB8
>>B52.7070805@knublauch.com%3e) about a regression in ARQs treatment of
>>initial bindings raises an interesting disconnect between the
>>interpretation of SPARQL and the Initial Bindings API.
>>
>> Initial bindings in their current form allows for users to essentially
>>change the semantics of a query in a non-intuitive way. Take his
>>example query:
>>
>> ASK { FILTER(?a = ?b) }
>>
>> Intuitively that query MUST always return false yet with initial
>>bindings in the mix the query can be made to return true, at least prior
>>to 2.10.2 which introduces a new optimizer which includes special case
>>recognition for this.
>>
>> The problem is that using initial bindings can fundamentally change the
>>semantics of queries in non-intuitive ways when I believe the intention
>>of the API was merely to allow for improved performance by guiding the
>>engine.
>>
>> To me this suggests that initial bindings as currently implemented is
>>fundamentally flawed and I would suggest that we think about
>>re-architecting this feature in a future release (not the next release).
>> I believe there are probably several ways of doing this:
>>
>> 1 Remove support for initial bindings on queries entirely (as we
>>already did for updates) in favor of using ParameterizedSparqlString
>>
>> 2 Change initial bindings to be a pre-optimization algebra
>>transformation of the query
>>
>> As we've discussed previously in the context of
>>ParameterizedSparqlString there is potential to do the substitution at
>>the algebra tree level rather than at the textual level. This allows
>>for stronger syntax checking and actually changes the query
>>appropriately. The problem with this is that it doesn't work if we want
>>to inject multiple values for a variable, hence Option 3
>>
>> 3 Change initial bindings to be done by injection of VALUES clauses
>>
>> This approach is again by algebra transform and would involve inserting
>>VALUES clauses at each leaf of the algebra tree. So Holger's query with
>>initial bindings applied would be rewritten like so:
>>
>> ASK
>> {
>> VALUES ( ?a ?b ) { ( true true ) }
>> FILTER (?a = b)
>> }
>>
>> However this approach might get rather complex for larger queries and
>>also runs into issues of scope, what if we insert the VALUES clause
>>inside of a sub-query which doesn't propagate those initial bindings
>>outside of it etc.
>>
>> 4 Skip optimization when initial bindings are involved
>>
>> This is the easiest approach but we can't enforce this on other query
>>engine implementations and it could seriously harm performance for those
>>that use initial bindings extensively.
>>
>> There may also be other approaches I haven't thought so please suggest
>>anything that makes sense. Bottom line is that initial bindings in its
>>current form seems fundamentally broken to me and we should be thinking
>>of how to fix this in the future.
>>
>> Rob
>>
>
Re: Initial Bindings in Query Evaluation
Posted by Holger Knublauch <ho...@knublauch.com>.
On 8/5/2013 18:47, Andy Seaborne wrote:
>
> How does TopQuadrant use initial bindings with update? Please give some
> concrete examples.
A reasonably representative random choice from our SPARQL Web Pages library:
---
Input: ?this
INSERT {
?facet a search:PropertyFacet .
?facet search:property ?property .
?facet search:facetIndex
?facetIndex .
?this search:facet ?facet .
}
WHERE {
BIND
((COALESCE(spl:objectCount(?this, search:facet), 0) + 1) AS ?facetIndex) .
BIND (BNODE() AS ?facet) .
}
---
Input: ?resource
INSERT {
GRAPH ui:tempGraph {
swa:DestructorMetadata swa:delete ?child .
} .
}
WHERE {
?child (rdfs:subClassOf)* ?resource .
}
---
Input: ?resource
DELETE {
GRAPH ui:tempGraph {
swa:DestructorMetadata swa:delete ?child .
} .
}
WHERE {
?child (rdfs:subClassOf)+ ?resource .
?child (rdfs:subClassOf)+ ?other .
FILTER NOT EXISTS {
GRAPH ui:tempGraph {
swa:DestructorMetadata ?either ?other .
} .
} .
}
---
Input: ?label, ?labelLang, ?uri
INSERT {
?class a ?resourceType .
?class rdfs:label ?prefLabel .
?class rdfs:subClassOf ?contextResource .
}
WHERE {
BIND (IRI(str(?uri)) AS ?class) .
BIND (IF(bound(?labelLang), STRLANG(?label,
?labelLang), ?label) AS ?prefLabel) .
}
---
Input: ?search, ?searchGraph
INSERT {
GRAPH ?searchGraph {
?search a search:Search .
?search search:rootType ?rootType .
?search search:queryGraph ?queryGraph .
?search swa:loadId ?id .
} .
}
WHERE {
?search search:rootType ?rootType .
}
---
Input: ?searchGraph
DELETE WHERE {
GRAPH ?searchGraph {
?s ?p ?o .
} .
}
---
Input: ?searchGraph, ?first
INSERT {
GRAPH ?sessionGraph {
?resource ?p ?o .
} .
}
WHERE {
?first ?p ?o .
FILTER NOT EXISTS {
( ?rs 0 ) spr:colCells ?other .
FILTER ((?other != ?first) &&
NOT EXISTS {
?other ?p ?o .
}) .
} .
}
---
Input: ?this, ?targetGraph -------- This is the one that I am
currently stuck with
INSERT {
GRAPH ?targetGraph {
?this ?p ?o .
} .
}
WHERE {
?this ?p ?o .
}
Not sure if this is what you are looking for.
Holger
Re: Initial Bindings in Query Evaluation
Posted by Andy Seaborne <an...@apache.org>.
On 05/08/13 02:21, Holger Knublauch wrote:
> On 8/3/2013 9:50, Rob Vesse wrote:
>> Side Note - Initial bindings for updates was removed because it
>> was a barrier to streaming updates
>> (http://markmail.org/message/bazwh2exmcc5vmoh). Also as others
>> noted in the discussion there initial bindings is a little murkier
>> for updates since does it apply only to WHERE clauses, to all
>> portions of requests, etc? Keeping the API as-is is always an
>> option, if this ends up being the preference of the community then
>> we definitely need to improve the documentation to note that there
>> can be unintended interactions with other parts of the query
>> engine such as the optimizer when initial bindings are used.
>
> Yet I believe the handling of this removal for UPDATEs was a bit
> rushed. I don't have the whole background, so apologies if I miss
> something obvious, but it sounds like there was no real deprecation
> cycle because it would have complicated some *modes* of using
> UPDATEs (streaming, remote). However, these are just some modes among
> others, and it might have been possible to document it away for those
> modes, and throw an exception if someone uses a streaming update
> with initial bindings. Right now the API is inconsistent because
> bindings are still present for Queries, and all our usages of initial
> bindings with UPDATEs would have continued to work. We were
> unfortunately unable to upgrade to newer Jena versions earlier,
> because there have been some other show stopper bugs in the code. I
> am using the SNAPSHOT now to try to detect those before a release,
> and may have more reports in the next couple of days as I run through
> manual and automated test scenarios.
The most important and obvious use case (IMO) is insert-data:
INSERT DATA { ?s rds:label "foo" }
but that is illegal syntax in strict SPARQL 1.1 so this use case does
not work in the initial bindings paradigm. (Yes - we would can change
the parser and do the "DATA is all data" check later. It would fail WG
tests of syntax).
How does TopQuadrant use initial bindings with update? Please give some
concrete examples.
Andy
> Thanks Holger
>
Re: Initial Bindings in Query Evaluation
Posted by Holger Knublauch <ho...@knublauch.com>.
On 8/3/2013 9:50, Rob Vesse wrote:
> Side Note - Initial bindings for updates was removed because it was a
> barrier to streaming updates
> (http://markmail.org/message/bazwh2exmcc5vmoh). Also as others noted
> in the discussion there initial bindings is a little murkier for
> updates since does it apply only to WHERE clauses, to all portions of
> requests, etc? Keeping the API as-is is always an option, if this ends
> up being the preference of the community then we definitely need to
> improve the documentation to note that there can be unintended
> interactions with other parts of the query engine such as the
> optimizer when initial bindings are used.
Yet I believe the handling of this removal for UPDATEs was a bit rushed.
I don't have the whole background, so apologies if I miss something
obvious, but it sounds like there was no real deprecation cycle because
it would have complicated some *modes* of using UPDATEs (streaming,
remote). However, these are just some modes among others, and it might
have been possible to document it away for those modes, and throw an
exception if someone uses a streaming update with initial bindings.
Right now the API is inconsistent because bindings are still present for
Queries, and all our usages of initial bindings with UPDATEs would have
continued to work. We were unfortunately unable to upgrade to newer Jena
versions earlier, because there have been some other show stopper bugs
in the code. I am using the SNAPSHOT now to try to detect those before a
release, and may have more reports in the next couple of days as I run
through manual and automated test scenarios.
> What you are talking about sounds much more like cached execution
> plans in SQL. I understand the analogy of a SPARQL query to a function
> but SPARQL variables were not intended to be function arguments, that
> you choose to treat them as such and that initial bindings lets you
> treat them as such is a perhaps unintended consequence of ARQ's API.
Yep, unintended things are often the best, because they open new doors.
You may not be aware of how central SPARQL (and this feature) has become
for our software stack and commercial product portfolio. There are
literally thousands of SPARQL queries with varying complexity in our
products, and they all execute within contexts of pre-bound variables.
Even things like user interfaces are constructed with the help of
running hundreds of queries per page request, so speed is crucial and
has always been good enough.
So, whatever change is made, I would strongly favor a solution that
preserves the semantics and doesn't negatively affect performance (of
running many small queries).
Thanks
Holger
Re: Initial Bindings in Query Evaluation
Posted by Rob Vesse <rv...@yarcdata.com>.
Holger
Comments inline:
On 8/2/13 3:50 PM, "Holger Knublauch" <ho...@knublauch.com> wrote:
>Folks,
>
>all I did was report a bug, and your response is to delete the whole
>feature! This would completely throw out the baby with the bathwater and
>may mean that we at TopQuadrant have to branch off to our own Jena
>version when this happens.
I started the discussion because what your bug highlights is that initial
bindings as they are currently implemented are broken, we appear to
disagree on how they are broken but clearly they are not entirely correct
in their behavior one way or the other.
I'm aiming to solicit opinions from all interested parties and try to get
a consensus about how this feature should be handled in the future.
Whether that means merely fixing the current implementation, replacing it
with another solution or something else entirely. Note that I explicitly
said that this is about re-architecting this feature in some future
release, no-one is going into the code base and deleting this feature
immediately. If the eventual decision is to remove this feature in its
current form then there would be a deprecation cycle.
Forcing TQ to fork Jena is not my intention, I'm trying to find an
appropriate means to address something which I consider to be broken in a
way that benefits the whole community. Note that there are other places
where initial bindings are completely unimplemented/broken depending on
your POV e.g. remote queries.
>
>I would be in favor of this option
>
>5) Keep the API as it is and restore initial bindings for UPDATE as well.
Side Note - Initial bindings for updates was removed because it was a
barrier to streaming updates
(http://markmail.org/message/bazwh2exmcc5vmoh). Also as others noted in
the discussion there initial bindings is a little murkier for updates
since does it apply only to WHERE clauses, to all portions of requests,
etc?
Keeping the API as-is is always an option, if this ends up being the
preference of the community then we definitely need to improve the
documentation to note that there can be unintended interactions with other
parts of the query engine such as the optimizer when initial bindings are
used.
>
>It was IMHO a mistake to switch to parameterized queries. We make very
>heavy use of initial bindings throughout our software stack, SPIN, SWP,
>SPARQLMotion. They are a great feature, allowing users to treat SPARQL
>like a programming language (think about a function call, where the
>parameters are different each time, yet the developer only needs to
>write the logic once).
What you are talking about sounds much more like cached execution plans in
SQL. I understand the analogy of a SPARQL query to a function but SPARQL
variables were not intended to be function arguments, that you choose to
treat them as such and that initial bindings lets you treat them as such
is a perhaps unintended consequence of ARQ's API.
>
> From what I understand so far, parameterized SPARQL has performance
>issues - we would need to re-parse the string while with initial
>bindings we can reuse the same (compiled) Query object each time.
>Parameterized queries also don't support the bound(?var) operator, and
>probably others.
Yes there is a performance hit associated with parameterized queries and
yes it is possible to turn a syntactically valid query into an invalid one
by injecting certain values because of the way the parameterization is
done on a purely textual basis.
This is why I suggested Options 2 and 3 which are algebraic manipulations
which would allow the initial bindings to be injected by transformation of
the unoptimized algebra tree. This allows you to only take the parsing
hit once, use initial bindings as you do now without the optimizer making
incorrect decisions because of information that initial bindings gives
that it doesn't have access to. These options require substantially more
work to get all the kinks worked out but are undoubtably doable, if people
agree this is the way forward then likely this will happen eventually but
contributions to make it happen sooner are always welcome. This would
give users something much closer to cached execution plans which from what
you described is essentially how TQ are using initial bindings.
Also note that as a user you always have the option of using the BIND
and/or VALUES clauses in your queries and updates which achieves the same
ends as the initial bindings API and has the benefit of being semantically
much clearer from the POV of both the query writer and query engine.
>
>We have used initial bindings successfully for many years, and although
>there have been occasional bugs (I probably reported one such bug per
>year) it is a very essential feature from our point of view. If the
>overhead of fixing the optimizer is too big, I would be OK with
>switching off this optimizer if initial bindings are present. It worked
>fine without this optimizer for many years.
I understand that TQ considers the feature essential and I hope we as a
community can come to some agreement on this topic that satisfies the
whole community.
The optimizer is not broken, what the optimizer is doing is entirely
correct wrt to the semantics of the SPARQL query it is given and the
information it has. As I pointed out in my original reply to you there
are other optimizations that have had the same behavior for years. That
none of your unit tests previously covered a case which would be affected
in this way whereas the new optimization does happen to cover such a case
is a lucky coincidence as otherwise neither you/ourselves would have been
any the wiser.
The example query semantically should always return false, that the
initial bindings API allows a user to make it do otherwise is the bug IMO
though I think it is clear we disagree on this point.
Individual optimizations can always be turned off, for this specific one:
ARQ.getContext().set(ARQ.optFilterImplicitJoin, false);
If FILTER(?x = ?y) and FILTER(?x = <uri>) are common patterns that TQ and
SPIN users make use of in conjunction with initial bindings then you
should perhaps turn both this and ARQ.optFilterEquality off. Though if
the problem is only in strange corner cases like the example you gave then
you may be perfectly fine since the special case causing you problems only
occurs when the variables are not used within the inner operator that the
FILTER applies over.
Switching the whole optimizer off is overkill and will almost certainly
harm performance because that will include disabling critical
optimizations such as ARQs index join strategy.
Rob
>
>HTH
>Holger
>
>
>On 8/3/2013 2:52, Rob Vesse wrote:
>> Hi All
>>
>> Holger's question
>>(http://mail-archives.apache.org/mod_mbox/jena-users/201308.mbox/%3c51FB8
>>B52.7070805@knublauch.com%3e) about a regression in ARQs treatment of
>>initial bindings raises an interesting disconnect between the
>>interpretation of SPARQL and the Initial Bindings API.
>>
>> Initial bindings in their current form allows for users to essentially
>>change the semantics of a query in a non-intuitive way. Take his
>>example query:
>>
>> ASK { FILTER(?a = ?b) }
>>
>> Intuitively that query MUST always return false yet with initial
>>bindings in the mix the query can be made to return true, at least prior
>>to 2.10.2 which introduces a new optimizer which includes special case
>>recognition for this.
>>
>> The problem is that using initial bindings can fundamentally change the
>>semantics of queries in non-intuitive ways when I believe the intention
>>of the API was merely to allow for improved performance by guiding the
>>engine.
>>
>> To me this suggests that initial bindings as currently implemented is
>>fundamentally flawed and I would suggest that we think about
>>re-architecting this feature in a future release (not the next release).
>> I believe there are probably several ways of doing this:
>>
>> 1 Remove support for initial bindings on queries entirely (as we
>>already did for updates) in favor of using ParameterizedSparqlString
>>
>> 2 Change initial bindings to be a pre-optimization algebra
>>transformation of the query
>>
>> As we've discussed previously in the context of
>>ParameterizedSparqlString there is potential to do the substitution at
>>the algebra tree level rather than at the textual level. This allows
>>for stronger syntax checking and actually changes the query
>>appropriately. The problem with this is that it doesn't work if we want
>>to inject multiple values for a variable, hence Option 3
>>
>> 3 Change initial bindings to be done by injection of VALUES clauses
>>
>> This approach is again by algebra transform and would involve inserting
>>VALUES clauses at each leaf of the algebra tree. So Holger's query with
>>initial bindings applied would be rewritten like so:
>>
>> ASK
>> {
>> VALUES ( ?a ?b ) { ( true true ) }
>> FILTER (?a = b)
>> }
>>
>> However this approach might get rather complex for larger queries and
>>also runs into issues of scope, what if we insert the VALUES clause
>>inside of a sub-query which doesn't propagate those initial bindings
>>outside of it etc.
>>
>> 4 Skip optimization when initial bindings are involved
>>
>> This is the easiest approach but we can't enforce this on other query
>>engine implementations and it could seriously harm performance for those
>>that use initial bindings extensively.
>>
>> There may also be other approaches I haven't thought so please suggest
>>anything that makes sense. Bottom line is that initial bindings in its
>>current form seems fundamentally broken to me and we should be thinking
>>of how to fix this in the future.
>>
>> Rob
>>
>
Re: Initial Bindings in Query Evaluation
Posted by Holger Knublauch <ho...@knublauch.com>.
Folks,
all I did was report a bug, and your response is to delete the whole
feature! This would completely throw out the baby with the bathwater and
may mean that we at TopQuadrant have to branch off to our own Jena
version when this happens.
I would be in favor of this option
5) Keep the API as it is and restore initial bindings for UPDATE as well.
It was IMHO a mistake to switch to parameterized queries. We make very
heavy use of initial bindings throughout our software stack, SPIN, SWP,
SPARQLMotion. They are a great feature, allowing users to treat SPARQL
like a programming language (think about a function call, where the
parameters are different each time, yet the developer only needs to
write the logic once).
From what I understand so far, parameterized SPARQL has performance
issues - we would need to re-parse the string while with initial
bindings we can reuse the same (compiled) Query object each time.
Parameterized queries also don't support the bound(?var) operator, and
probably others.
We have used initial bindings successfully for many years, and although
there have been occasional bugs (I probably reported one such bug per
year) it is a very essential feature from our point of view. If the
overhead of fixing the optimizer is too big, I would be OK with
switching off this optimizer if initial bindings are present. It worked
fine without this optimizer for many years.
HTH
Holger
On 8/3/2013 2:52, Rob Vesse wrote:
> Hi All
>
> Holger's question (http://mail-archives.apache.org/mod_mbox/jena-users/201308.mbox/%3c51FB8B52.7070805@knublauch.com%3e) about a regression in ARQs treatment of initial bindings raises an interesting disconnect between the interpretation of SPARQL and the Initial Bindings API.
>
> Initial bindings in their current form allows for users to essentially change the semantics of a query in a non-intuitive way. Take his example query:
>
> ASK { FILTER(?a = ?b) }
>
> Intuitively that query MUST always return false yet with initial bindings in the mix the query can be made to return true, at least prior to 2.10.2 which introduces a new optimizer which includes special case recognition for this.
>
> The problem is that using initial bindings can fundamentally change the semantics of queries in non-intuitive ways when I believe the intention of the API was merely to allow for improved performance by guiding the engine.
>
> To me this suggests that initial bindings as currently implemented is fundamentally flawed and I would suggest that we think about re-architecting this feature in a future release (not the next release). I believe there are probably several ways of doing this:
>
> 1 – Remove support for initial bindings on queries entirely (as we already did for updates) in favor of using ParameterizedSparqlString
>
> 2 – Change initial bindings to be a pre-optimization algebra transformation of the query
>
> As we've discussed previously in the context of ParameterizedSparqlString there is potential to do the substitution at the algebra tree level rather than at the textual level. This allows for stronger syntax checking and actually changes the query appropriately. The problem with this is that it doesn't work if we want to inject multiple values for a variable, hence Option 3
>
> 3 – Change initial bindings to be done by injection of VALUES clauses
>
> This approach is again by algebra transform and would involve inserting VALUES clauses at each leaf of the algebra tree. So Holger's query with initial bindings applied would be rewritten like so:
>
> ASK
> {
> VALUES ( ?a ?b ) { ( true true ) }
> FILTER (?a = b)
> }
>
> However this approach might get rather complex for larger queries and also runs into issues of scope, what if we insert the VALUES clause inside of a sub-query which doesn't propagate those initial bindings outside of it etc.
>
> 4 – Skip optimization when initial bindings are involved
>
> This is the easiest approach but we can't enforce this on other query engine implementations and it could seriously harm performance for those that use initial bindings extensively.
>
> There may also be other approaches I haven't thought so please suggest anything that makes sense. Bottom line is that initial bindings in its current form seems fundamentally broken to me and we should be thinking of how to fix this in the future.
>
> Rob
>
Re: Initial Bindings in Query Evaluation
Posted by Stephen Allen <sa...@apache.org>.
I would be in favor of option 1, remove initial binding support all
together. It is already fairly broken, and doesn't work for remote
queries, while VALUES provides a standards-compliant mechanism to do what
initial bindings used to do.
-Stephen
On Fri, Aug 2, 2013 at 12:52 PM, Rob Vesse <rv...@yarcdata.com> wrote:
> Hi All
>
> Holger's question (
> http://mail-archives.apache.org/mod_mbox/jena-users/201308.mbox/%3c51FB8B52.7070805@knublauch.com%3e)
> about a regression in ARQs treatment of initial bindings raises an
> interesting disconnect between the interpretation of SPARQL and the Initial
> Bindings API.
>
> Initial bindings in their current form allows for users to essentially
> change the semantics of a query in a non-intuitive way. Take his example
> query:
>
> ASK { FILTER(?a = ?b) }
>
> Intuitively that query MUST always return false yet with initial bindings
> in the mix the query can be made to return true, at least prior to 2.10.2
> which introduces a new optimizer which includes special case recognition
> for this.
>
> The problem is that using initial bindings can fundamentally change the
> semantics of queries in non-intuitive ways when I believe the intention of
> the API was merely to allow for improved performance by guiding the engine.
>
> To me this suggests that initial bindings as currently implemented is
> fundamentally flawed and I would suggest that we think about
> re-architecting this feature in a future release (not the next release). I
> believe there are probably several ways of doing this:
>
> 1 – Remove support for initial bindings on queries entirely (as we already
> did for updates) in favor of using ParameterizedSparqlString
>
> 2 – Change initial bindings to be a pre-optimization algebra
> transformation of the query
>
> As we've discussed previously in the context of ParameterizedSparqlString
> there is potential to do the substitution at the algebra tree level rather
> than at the textual level. This allows for stronger syntax checking and
> actually changes the query appropriately. The problem with this is that it
> doesn't work if we want to inject multiple values for a variable, hence
> Option 3
>
> 3 – Change initial bindings to be done by injection of VALUES clauses
>
> This approach is again by algebra transform and would involve inserting
> VALUES clauses at each leaf of the algebra tree. So Holger's query with
> initial bindings applied would be rewritten like so:
>
> ASK
> {
> VALUES ( ?a ?b ) { ( true true ) }
> FILTER (?a = b)
> }
>
> However this approach might get rather complex for larger queries and also
> runs into issues of scope, what if we insert the VALUES clause inside of a
> sub-query which doesn't propagate those initial bindings outside of it etc.
>
> 4 – Skip optimization when initial bindings are involved
>
> This is the easiest approach but we can't enforce this on other query
> engine implementations and it could seriously harm performance for those
> that use initial bindings extensively.
>
> There may also be other approaches I haven't thought so please suggest
> anything that makes sense. Bottom line is that initial bindings in its
> current form seems fundamentally broken to me and we should be thinking of
> how to fix this in the future.
>
> Rob
>