Skip to content

Add ability to return the result of a RQL expression in an update #194

Closed

Description

@kareemk

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.

Activity

  1. coffeemug commented on Dec 27, 2012

    @coffeemug
    Contributor

    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.

  2. kareemk commented on Dec 27, 2012

    @kareemk
    Author

    @coffeemug point-updates would suffice for us to be able to replace mongo with rethink

  3. coffeemug commented on Dec 27, 2012

    @coffeemug
    Contributor

    @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.

  4. al3xandru commented on Dec 27, 2012

    @al3xandru
    Contributor

    @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.

  5. nviennot commented on Dec 27, 2012

    @nviennot
    Contributor

    @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_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

  6. kareemk commented on Dec 27, 2012

    @kareemk
    Author

    @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:

    @al3xandru https://github.com/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_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.

    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

  7. al3xandru commented on Dec 27, 2012

    @al3xandru
    Contributor

    @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).

  8. al3xandru commented on Dec 27, 2012

    @al3xandru
    Contributor

    @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?

  9. jdoliner commented on Dec 27, 2012

    @jdoliner
    Contributor

    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:

    table.get(1).update(lambda x: {x["foo"] : x["foo"] + table.get(2)["foo"])
    

    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.

  10. coffeemug commented on Dec 27, 2012

    @coffeemug
    Contributor

    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:

    r.table('foo').update(...).output(lambda old_row, new_row: ...).run()
    

    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.

  11. nviennot commented on Dec 27, 2012

    @nviennot
    Contributor

    I like your proposal @coffeemug

  12. jdoliner commented on Dec 27, 2012

    @jdoliner
    Contributor

    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:

    do(lambda x,y: ..., table.get(1).update(...), table.get(2).update(...))
    

    whereas with output you need to jump through some hoops and say:

    table.get(1).update(...).output(lambda new1 old1: table.get(2).update(...).output(lambda new2 old2: ...))
    

    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.

  13. coffeemug commented on Dec 28, 2012

    @coffeemug
    Contributor

    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.

  14. kareemk commented on Dec 28, 2012

    @kareemk
    Author

    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.

  15. neumino commented on May 26, 2013

    @neumino
    Member

    Someone on Google Groups just bumped into this.
    We should probably make it part of all the rql proposals.

  16. coffeemug commented on May 28, 2013

    @coffeemug
    Contributor

    I was planning to get this into 1.7. This is a pretty fundamental use case for many people using NoSQL products.

  17. srh commented on May 28, 2013

    @srh
    Contributor

    Not to mention in-database implementation of autoincrement.

  18. mlucy commented on Jun 10, 2013

    @mlucy
    Member

    Moving out of the milestone in deference to the specific proposal in #976.

  19. coffeemug commented on Jun 10, 2013

    @coffeemug
    Contributor

    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?

  20. mlucy commented on Jun 10, 2013

    @mlucy
    Member

    Nope.

  21. coffeemug commented on Jun 10, 2013

    @coffeemug
    Contributor

    Closing, please follow #976 for updates on this issue.

  22. added a commit that references this issue on Aug 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions