You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add ability to return the result of a RQL expression in an update #194
I'm looking to mimic the findandmodify feature in MongoDB.
A basic use case is a document that contains a count field that is being updated by concurrent processes where the application needs to trigger an event once and only once the count field crosses a boundary. Without the ability to return the new value of the RQL expression that was used to atomically increment the field this is not possible AFAIK in RethinkDB.
I think that for the specific use case you're describing, the proper way to support it is to implement triggers (which rethinkdb currently doesn't have).
Of course supporting find-modify-read is useful in other cases. We discussed it, but it didn't make it into the API. I think it would be easy to support it for point-updates, I'm not sure about range updates. Moving to post-protobuf-improvements, this will likely make it into 1.5.
@coffeemug just a quick update: mongodb's findAndModify actually returns the original/unmodified document, so basically it is not find-modify-read, but rather find-modify-return previous version.
Basically update/replace would take an extra optional parameter that would make the result contain the document(s) that have been processed.
In the following sentence, assume a deterministic query update which is most of the time true (no Math.rand() for example). By knowing the update query and the original document right before the update, the user can figure out the value of the updated document. So granted, "new" is really just a nice to have.
But findAndModify can reveal itself to be quite useful:
Suppose that you have an app that would give lottery tickets to people.
Have a document with a field lottery_tickets which contains an
array of tickets to be drawn, such as [4,3,1,7,5,8,10,2,6,9].
Each participant would take a ticket by doing a findAndModify("lottery_tickets.pop()").
It would return the chosen number, and would be race free since rethinkdb serializes writes to the same document.
How to approach this use case with the rethinkdb api?
the λ at the bottom right of the rethinkdb website
/me amused
@coffemug we don't have a timeline beyond ASAP. We have a large mongodb
deployment and we've lost confidence in the product for various reasons and
rethinkdb is top of the list as a replacement (hence our work on nobrainer
led by @nviennot).
On Thursday, December 27, 2012, Nicolas Viennot wrote:
In the following sentence, assume a deterministic query update which is
most of the time true (no Math.rand() for example). By knowing the update
query and the original document right before the update, the user can
figure out the value of the updated document. So granted, "new" is really
just a nice to have.
But findAndModify can reveal itself to be quite useful:
Suppose that you have an app that would give lottery tickets to people.
Have a document with a field lottery_tickets which contains an
array of tickets to be drawn, such as [4,3,1,7,5,8,10,2,6,9].
Each participant would take a ticket by doing a
findAndModify("lottery_tickets.pop()").
It would return the chosen number, and would be race free since rethinkdb
serializes writes to the same document.
How to approach this use case with the rethinkdb api?
the λ at the bottom right of the rethinkdb website
/me amused
—
Reply to this email directly or view it on GitHubhttps://github.com//issues/194#issuecomment-11705024.
@nviennot as @coffeemug said I don't think this is possible with the current API and that's the reason for me suggesting the optional arg enhancement that would basically allow update/replace to return the whole document as part of the result.
Also thanks for pointing out the new argument in findAndModify. Your example makes me think that returning the original version of the document is actually more useful than returning the new version. Even if applying deterministic operations to a doc, these are not necessarily reversible operations so you'd not be able to tell what the original version was (while having the original version and knowing the operation will always allow you to compute the new version).
@kareemk, @nviennot if you didn't do this already, would you mind sending us an email (slava and alex @ rethinkdb.com) so we could look together in the ASAP timeline and see how we can help?
This is actually pretty easy to do. The only reason we don't right now is because point-update and bulk-update use the same code path and returning a stream of modified documents in bulk update would be a bit expensive and kind of useless since they'd be in no particular order. These problems could be fixed but that's a bit of a question and having this for point update would be nice and easy.
I think that you actually want to return the old and new versions of the document. As @al3xandru points out it can be hard (or impossible) to invert the function and get the old version of the document but it's also pretty easy for there to be no way convert the old document into the new one if the function is non deterministic. Math.random() is one for this to happen but reading from another table and using that value is another that seems a lot more likely.
And you wanted to do something when it crossed a threshold you'd need both documents to be sure you only did it once.
Triggers are similar to this but I think we want both features long term. Triggers seem to give you a lot to think about (they can go into infinite loops for example) that's not to say we shouldn't do them it's just to say there's value in giving people the simpler solution for cases where it suffices. Plus triggers are hard and this is easy so we can do this now.
I think we should do this right, and implement something reasonably general-purpose, such as mssql's OUTPUT command. This would work on bulk as well as single operations, and on things other than update (e.g. we might want to get deleted documents as we delete them, or replaced documents, etc.)
We could implement it as additional parameters to mutation commands, or as a chainable .output(...) command. One benefit of doing a general purpose system like this is that the user can pass a ReQL lambda that receives an old and new document, and then they can do any operation they want. For example:
This way the user can return the old document, new document, any attributes they want, a sum of attributes, or really anything that strikes their fancy.
EDIT: ideally we'd design this system in a way where our current json summary document (the one that returns number of inserted/update/deleted documents, errors, generated ids, etc.) could be implemented on top of this system.
So actually in this case we get to reap the benefits of having done things right in the past. On the backside there are only 2 commands point-delete and point-mutate and actually there's no reason to have the prior so on the backend a solution is always going to be general purpose.
I'm not crazy about the output syntax because it seems a bit under powered. One problem is that if I wanted to use the results of 2 updates it would be a serious pain. What if update just evaluated to an object {new:..., old:...} then with the do syntax you could say:
This is also nice because with a bulk update you don't need to handle one row at a time. You could for example do a bulk update, then run a map reduce job on the results of the update and do something based on that.
I'm also not quite sure where the summary comes in to play. Maybe it should just be a separate field on the protocol buffers I don't think anyone ever wants to get that structure in to their REQL queries and insert it in to tables etc.
I can't exactly put my finger on why, but I'm not a particularly big fan of the { new: ..., old: ...} paradigm. It's sort of "outside" of the spirit of ReQL, and it hardcodes some names into a new JSON document -- there's something strange about it. I never loved how we did it with join, and this is similar.
I don't think the output proposal above is underpowered. Consider:
do(lambda x,y: ...,
table.get(1).update(...).output(lambda o, n: { left: o, right: n}),
table.get(2).update(...).output(lambda o, n: { left: o, right: n}))
Also:
table.update(...).output(lambda o, n: { left: o, right: n}).group_map_reduce(...)
Of course this begs the question of why have output take the lambda command in the first place instead of just returning the {new: ..., old: ...} document. One thing we can do is use the new/old paradigm here, and then replace it everywhere later if/when we have a better solution.
BTW, I don't think we should return this data by default, because 99% of the time the user won't want it. Presumably we could detect that by seeing if anything is chained after the modification command. If nothing is chained, perhaps the user would have to pass a flag to run.
I like output that returns {new:.. old:...} as it doesn't corrupt the update api and would suffice for all the usecases to support our
transition from mongo.
I feel like we should close the original issue when there is a new one with a concrete proposal. Having two issues for the same thing sitting around kind of irks me. Would anyone have an issue with me closing this?
Activity
coffeemug commentedon Dec 27, 2012
I think that for the specific use case you're describing, the proper way to support it is to implement triggers (which rethinkdb currently doesn't have).
Of course supporting find-modify-read is useful in other cases. We discussed it, but it didn't make it into the API. I think it would be easy to support it for point-updates, I'm not sure about range updates. Moving to post-protobuf-improvements, this will likely make it into 1.5.
kareemk commentedon Dec 27, 2012
@coffeemug point-updates would suffice for us to be able to replace mongo with rethink
coffeemug commentedon Dec 27, 2012
@kareemk -- what's your timeline for the evaluation? I can't promise we'll be able to get it in in time, but it will help us plan better on our side.
al3xandru commentedon Dec 27, 2012
@coffeemug just a quick update: mongodb's
findAndModifyactually returns the original/unmodified document, so basically it is not find-modify-read, but rather find-modify-return previous version.Basically
update/replacewould take an extra optional parameter that would make the result contain the document(s) that have been processed.nviennot commentedon Dec 27, 2012
@al3xandru findAndModify() actually accept a "new" argument. c.f. http://docs.mongodb.org/manual/reference/command/findAndModify/
In the following sentence, assume a deterministic query update which is most of the time true (no
Math.rand()for example). By knowing the update query and the original document right before the update, the user can figure out the value of the updated document. So granted, "new" is really just a nice to have.But findAndModify can reveal itself to be quite useful:
Suppose that you have an app that would give lottery tickets to people.
Have a document with a field
lottery_ticketswhich contains anarray of tickets to be drawn, such as
[4,3,1,7,5,8,10,2,6,9].Each participant would take a ticket by doing a
findAndModify("lottery_tickets.pop()").It would return the chosen number, and would be race free since rethinkdb serializes writes to the same document.
How to approach this use case with the rethinkdb api?
the λ at the bottom right of the rethinkdb website
/me amused
kareemk commentedon Dec 27, 2012
@coffemug we don't have a timeline beyond ASAP. We have a large mongodb
deployment and we've lost confidence in the product for various reasons and
rethinkdb is top of the list as a replacement (hence our work on nobrainer
led by @nviennot).
On Thursday, December 27, 2012, Nicolas Viennot wrote:
Kareem Kouddous
Head of Technology
crowdtap
225 Park Avenue S, 16th Fl, New York, NY 10003
*
*
p* * 917-582-9178
t* @kareemk
*
*
www.crowdtap.com | *@crowdtap
al3xandru commentedon Dec 27, 2012
@nviennot as @coffeemug said I don't think this is possible with the current API and that's the reason for me suggesting the optional arg enhancement that would basically allow
update/replaceto return the whole document as part of the result.Also thanks for pointing out the
newargument infindAndModify. Your example makes me think that returning the original version of the document is actually more useful than returning the new version. Even if applying deterministic operations to a doc, these are not necessarily reversible operations so you'd not be able to tell what the original version was (while having the original version and knowing the operation will always allow you to compute the new version).al3xandru commentedon Dec 27, 2012
@kareemk, @nviennot if you didn't do this already, would you mind sending us an email (slava and alex @ rethinkdb.com) so we could look together in the ASAP timeline and see how we can help?
jdoliner commentedon Dec 27, 2012
This is actually pretty easy to do. The only reason we don't right now is because point-update and bulk-update use the same code path and returning a stream of modified documents in bulk update would be a bit expensive and kind of useless since they'd be in no particular order. These problems could be fixed but that's a bit of a question and having this for point update would be nice and easy.
I think that you actually want to return the old and new versions of the document. As @al3xandru points out it can be hard (or impossible) to invert the function and get the old version of the document but it's also pretty easy for there to be no way convert the old document into the new one if the function is non deterministic.
Math.random()is one for this to happen but reading from another table and using that value is another that seems a lot more likely.Supposing you were doing something like:
And you wanted to do something when it crossed a threshold you'd need both documents to be sure you only did it once.
Triggers are similar to this but I think we want both features long term. Triggers seem to give you a lot to think about (they can go into infinite loops for example) that's not to say we shouldn't do them it's just to say there's value in giving people the simpler solution for cases where it suffices. Plus triggers are hard and this is easy so we can do this now.
coffeemug commentedon Dec 27, 2012
I think we should do this right, and implement something reasonably general-purpose, such as mssql's
OUTPUTcommand. This would work on bulk as well as single operations, and on things other than update (e.g. we might want to get deleted documents as we delete them, or replaced documents, etc.)We could implement it as additional parameters to mutation commands, or as a chainable
.output(...)command. One benefit of doing a general purpose system like this is that the user can pass a ReQL lambda that receives an old and new document, and then they can do any operation they want. For example:This way the user can return the old document, new document, any attributes they want, a sum of attributes, or really anything that strikes their fancy.
EDIT: ideally we'd design this system in a way where our current json summary document (the one that returns number of inserted/update/deleted documents, errors, generated ids, etc.) could be implemented on top of this system.
nviennot commentedon Dec 27, 2012
I like your proposal @coffeemug
jdoliner commentedon Dec 27, 2012
So actually in this case we get to reap the benefits of having done things right in the past. On the backside there are only 2 commands
point-deleteandpoint-mutateand actually there's no reason to have the prior so on the backend a solution is always going to be general purpose.I'm not crazy about the
outputsyntax because it seems a bit under powered. One problem is that if I wanted to use the results of 2 updates it would be a serious pain. What ifupdatejust evaluated to an object{new:..., old:...}then with the do syntax you could say:whereas with
outputyou need to jump through some hoops and say:This is also nice because with a bulk update you don't need to handle one row at a time. You could for example do a bulk update, then run a map reduce job on the results of the update and do something based on that.
I'm also not quite sure where the summary comes in to play. Maybe it should just be a separate field on the protocol buffers I don't think anyone ever wants to get that structure in to their REQL queries and insert it in to tables etc.
coffeemug commentedon Dec 28, 2012
I can't exactly put my finger on why, but I'm not a particularly big fan of the
{ new: ..., old: ...}paradigm. It's sort of "outside" of the spirit of ReQL, and it hardcodes some names into a new JSON document -- there's something strange about it. I never loved how we did it withjoin, and this is similar.I don't think the
outputproposal above is underpowered. Consider:Also:
Of course this begs the question of why have output take the lambda command in the first place instead of just returning the
{new: ..., old: ...}document. One thing we can do is use the new/old paradigm here, and then replace it everywhere later if/when we have a better solution.BTW, I don't think we should return this data by default, because 99% of the time the user won't want it. Presumably we could detect that by seeing if anything is chained after the modification command. If nothing is chained, perhaps the user would have to pass a flag to
run.kareemk commentedon Dec 28, 2012
I like
outputthat returns{new:.. old:...}as it doesn't corrupt theupdateapi and would suffice for all the usecases to support ourtransition from mongo.
neumino commentedon May 26, 2013
Someone on Google Groups just bumped into this.
We should probably make it part of all the rql proposals.
coffeemug commentedon May 28, 2013
I was planning to get this into 1.7. This is a pretty fundamental use case for many people using NoSQL products.
srh commentedon May 28, 2013
Not to mention in-database implementation of autoincrement.
mlucy commentedon Jun 10, 2013
Moving out of the milestone in deference to the specific proposal in #976.
coffeemug commentedon Jun 10, 2013
I feel like we should close the original issue when there is a new one with a concrete proposal. Having two issues for the same thing sitting around kind of irks me. Would anyone have an issue with me closing this?
mlucy commentedon Jun 10, 2013
Nope.
coffeemug commentedon Jun 10, 2013
Closing, please follow #976 for updates on this issue.
chore: board tick rethinkdb#194 — RT-GAP-035 closed (tier3 judge stag…