You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by Zen 98052 <z9...@outlook.com> on 2015/10/08 03:33:53 UTC

DELETE with WHERE clause

If I pass this query:


DELETE
{
  ?contactInfo abc:hasEmail ?email .
  ?email ?emailPredicate ?emailObject .
}
WHERE
{
  ?person abc:name "foobar" .
  ?person abc:hasContactInfo ?contactInfo .
}


to this example code below, the it doesn't do anything.


   UpdateRequest updateRequest = UpdateFactory.create(sparqlUpdateQuery);
   for (Update update : updateRequest.getOperations()) {
    UpdateAction.parseExecute(update.toString(), graphDataset);
   }



The graphDataset above is subclass of Jena's DatasetGraphBase class, where I override the find, add, delete, deleteAny, and some other methods.

I guess I have to query first using the triples in WHERE clause, and then populate the query results into bindings, and pass the bindings to UpdateAction.parseExecute method?

I looked at Jena's code, and still have no idea where to add that code. Perhaps I was wrong, so any hint, or better if there is already existing sample code for the case I am looking for?



Thanks,

Z

Re: DELETE with WHERE clause

Posted by Andy Seaborne <an...@apache.org>.
On 08/10/15 20:52, Zen 98052 wrote:
> Thanks Rob for very nice explanation. Do you somehow know the answer for first question?
> Is putting them in separate query like example below the only workaround?

>> Is there a way to re-write the query so that that first pattern
>> onlyapply delete operation not as many as multiplying with result >> from 
second pattern? Is separating them the only option?

The code is UpdateEngineWorker -- you can look at that to see what is 
going on.  Open source.

The contract is that the WHERE clause generates bindings, the DELETE 
template removes triples formed by instantiating the template with the 
all the bindings.   How that happens is up to the implementation.

The default implementation, UpdateEngineWorker deletes each triple - in 
most storage systems, it is as easy to simply delete the triple 
regardless of existence. Indeed, that scales better than duplicate 
suppression which is inherently requiring temporary state.

If you want to suppress duplicate deletes, you can subclass 
UpdateEngineWorker and do that processing. Suppressing with a sliding 
window would address the scale issue at the possibility of occasional 
duplicates.  There is no "one size fits all" answer.

	Andy

>
> DELETE
> {
>    ?email ?emailPredicate ?emailObject .
> }
> WHERE
> {
>     ?person abc:name "foobar" .
>     ?person abc:hasContactInfo ?contactInfo .
>     ?contactInfo abc:hasEmail ?email .
>     ?email ?emailPredicate ?emailObject .
> };
> DELETE
> {
>    ?contactInfo abc:hasEmail ?email .
> }
> WHERE
> {
>     ?person abc:name "foobar" .
>     ?person abc:hasContactInfo ?contactInfo .
>     ?contactInfo abc:hasEmail ?email .
> };
>
>
> Thanks,
> Z
> ________________________________________
> From: Rob Vesse <rv...@dotnetrdf.org>
> Sent: Thursday, October 8, 2015 9:20 AM
> To: users@jena.apache.org
> Subject: Re: DELETE with WHERE clause
>
> On 08/10/2015 13:48, "Zen 98052" <z9...@outlook.com> wrote:
>
>> Second question, I notice that update operation is instance of
>> UpdateModify. Do you know why it is not UpdateDeleteWhere?
>
> UpdateDeleteWhere is a special case for the following:
>
> DELETE WHERE {
>    # Some patterns
> }
>
> I.e. a DELETE where the template is also treated as the WHERE clause
>
> http://www.w3.org/TR/sparql11-update/#deleteWhere
>
> This is more limited than a general DELETE/INSERT operation because you
> can only have quad patterns I.e. can't use FILTERs, BINDs, sub-queries etc
> that you could use in the WHERE clause of the longer form
>
> Your update is the full form (separate template and WHERE clause) so is an
> UpdateModify
>
> Rob
>
>
>
>


Re: DELETE with WHERE clause

Posted by Zen 98052 <z9...@outlook.com>.
Thanks Rob for very nice explanation. Do you somehow know the answer for first question?
Is putting them in separate query like example below the only workaround?

DELETE
{
  ?email ?emailPredicate ?emailObject .
}
WHERE
{
   ?person abc:name "foobar" .
   ?person abc:hasContactInfo ?contactInfo .
   ?contactInfo abc:hasEmail ?email .
   ?email ?emailPredicate ?emailObject .
};
DELETE
{
  ?contactInfo abc:hasEmail ?email .
}
WHERE
{
   ?person abc:name "foobar" .
   ?person abc:hasContactInfo ?contactInfo .
   ?contactInfo abc:hasEmail ?email .
};


Thanks,
Z
________________________________________
From: Rob Vesse <rv...@dotnetrdf.org>
Sent: Thursday, October 8, 2015 9:20 AM
To: users@jena.apache.org
Subject: Re: DELETE with WHERE clause

On 08/10/2015 13:48, "Zen 98052" <z9...@outlook.com> wrote:

>Second question, I notice that update operation is instance of
>UpdateModify. Do you know why it is not UpdateDeleteWhere?

UpdateDeleteWhere is a special case for the following:

DELETE WHERE {
  # Some patterns
}

I.e. a DELETE where the template is also treated as the WHERE clause

http://www.w3.org/TR/sparql11-update/#deleteWhere

This is more limited than a general DELETE/INSERT operation because you
can only have quad patterns I.e. can't use FILTERs, BINDs, sub-queries etc
that you could use in the WHERE clause of the longer form

Your update is the full form (separate template and WHERE clause) so is an
UpdateModify

Rob





Re: DELETE with WHERE clause

Posted by Rob Vesse <rv...@dotnetrdf.org>.
On 08/10/2015 13:48, "Zen 98052" <z9...@outlook.com> wrote:

>Second question, I notice that update operation is instance of
>UpdateModify. Do you know why it is not UpdateDeleteWhere?

UpdateDeleteWhere is a special case for the following:

DELETE WHERE {
  # Some patterns
}

I.e. a DELETE where the template is also treated as the WHERE clause

http://www.w3.org/TR/sparql11-update/#deleteWhere

This is more limited than a general DELETE/INSERT operation because you
can only have quad patterns I.e. can't use FILTERs, BINDs, sub-queries etc
that you could use in the WHERE clause of the longer form

Your update is the full form (separate template and WHERE clause) so is an
UpdateModify

Rob





Re: DELETE with WHERE clause

Posted by Zen 98052 <z9...@outlook.com>.
Very cool Andy!! It works after I moved those triples per your suggestion.
I thought '?email' in "?contactInfo abc:hasEmail ?email" is already bound, but the binding only happens in WHERE clause, now I got it.

Follow up questions:
There are two triples that match "?contactInfo abc:hasEmail ?email" (i.e. from 2 persons), and three triples that match "?email ?emailPredicate ?emailObject"
I see the delete operation was called 6 times for "?email ?emailPredicate ?emailObject", which is correct (2 * 3 = 6)
However, I see it is also called 6 times for "?contactInfo abc:hasEmail ?email", while I'd expect it should be just 2 times.
Delete operation is idempotent, so this may not be big deal, but I am trying to optimize it , this is not good when n * m becomes bigger.
Is there a way to re-write the query so that that first pattern only apply delete operation not as many as multiplying with result from second pattern? Is separating them the only option?

Second question, I notice that update operation is instance of UpdateModify. Do you know why it is not UpdateDeleteWhere?


Thanks,
Z
________________________________________
From: Andy Seaborne <an...@apache.org>
Sent: Thursday, October 8, 2015 3:29 AM
To: users@jena.apache.org
Subject: Re: DELETE with WHERE clause

On 08/10/15 02:33, Zen 98052 wrote:
> If I pass this query:
>
>
> DELETE
> {
>    ?contactInfo abc:hasEmail ?email .
>    ?email ?emailPredicate ?emailObject .
> }
> WHERE
> {
>    ?person abc:name "foobar" .
>    ?person abc:hasContactInfo ?contactInfo .
> }
>
>
> to this example code below, the it doesn't do anything.

?email and ?emailObject are not bound so
"?contactInfo abc:hasEmail ?email . isn't a triple to delete.

WHERE {
   ?person abc:name "foobar" .
   ?person abc:hasContactInfo ?contactInfo .
   ?contactInfo abc:hasEmail ?email .
   ?email ?emailPredicate ?emailObject .
}

        Andy

>
>
>     UpdateRequest updateRequest = UpdateFactory.create(sparqlUpdateQuery);
>     for (Update update : updateRequest.getOperations()) {
>      UpdateAction.parseExecute(update.toString(), graphDataset);
>     }
>
>
>
> The graphDataset above is subclass of Jena's DatasetGraphBase class, where I override the find, add, delete, deleteAny, and some other methods.
>
> I guess I have to query first using the triples in WHERE clause, and then populate the query results into bindings, and pass the bindings to UpdateAction.parseExecute method?
>
> I looked at Jena's code, and still have no idea where to add that code. Perhaps I was wrong, so any hint, or better if there is already existing sample code for the case I am looking for?
>
>
>
> Thanks,
>
> Z
>

Re: DELETE with WHERE clause

Posted by Andy Seaborne <an...@apache.org>.
On 08/10/15 02:33, Zen 98052 wrote:
> If I pass this query:
>
>
> DELETE
> {
>    ?contactInfo abc:hasEmail ?email .
>    ?email ?emailPredicate ?emailObject .
> }
> WHERE
> {
>    ?person abc:name "foobar" .
>    ?person abc:hasContactInfo ?contactInfo .
> }
>
>
> to this example code below, the it doesn't do anything.

?email and ?emailObject are not bound so
"?contactInfo abc:hasEmail ?email . isn't a triple to delete.

WHERE {
   ?person abc:name "foobar" .
   ?person abc:hasContactInfo ?contactInfo .
   ?contactInfo abc:hasEmail ?email .
   ?email ?emailPredicate ?emailObject .
}

	Andy

>
>
>     UpdateRequest updateRequest = UpdateFactory.create(sparqlUpdateQuery);
>     for (Update update : updateRequest.getOperations()) {
>      UpdateAction.parseExecute(update.toString(), graphDataset);
>     }
>
>
>
> The graphDataset above is subclass of Jena's DatasetGraphBase class, where I override the find, add, delete, deleteAny, and some other methods.
>
> I guess I have to query first using the triples in WHERE clause, and then populate the query results into bindings, and pass the bindings to UpdateAction.parseExecute method?
>
> I looked at Jena's code, and still have no idea where to add that code. Perhaps I was wrong, so any hint, or better if there is already existing sample code for the case I am looking for?
>
>
>
> Thanks,
>
> Z
>