Skip to content

Support incremental map/reduce #1118

Open

Description

@coffeemug

We had this on our radar for a while, but didn't have an issue to track it. Since some people have been asking for an official issue to track, I'm adding this to GitHub.

I'm going to write up a specific proposal a bit later. This is in backlog as it's obviously a medium-term priority feature.

Activity

  1. jdoliner commented on Jul 3, 2013

    @jdoliner
    Contributor

    How bad would it be if incremental map/reduce jobs could only be registered on a single table? If we limited ourselves to that this would actually become a much simpler problem to solve in the backend.

  2. coffeemug commented on Jul 3, 2013

    @coffeemug
    ContributorAuthor

    Hmm, I have to think about it. It might be sufficient for most real use cases, but at first glance this makes me feel really uneasy. One thing MongoDB does that people find extremely annoying is introduce features that don't work with other features of the database. For example, they have plenty of collection types that cannot be sharded, which makes the user experience really frustrating since it moves the burden from developers to users. People can't just use the features they want and have the confidence that they will work.

    (I don't think it necessarily means we should restrict functionality, just that this tradeoff comes with connotations frustrating for users, so we should think carefully before we choose to do it this way)

  3. apendleton commented on Jul 3, 2013

    @apendleton

    Like, a single incremental job could only operate on data from a single table? Or that each database could only have one table on which incremental jobs could be registered?

    For the use case I had in mind when I asked the HN question that I think prompted this ticket, the former would be acceptable, but the latter wouldn't. I have no idea what other uses people have in mind, though.

  4. jdoliner commented on Jul 3, 2013

    @jdoliner
    Contributor

    @apendleton the former was what I meant. To give people an idea of how much easier it is I think I could probably do the one table case in less than a month while the general case would probably as many as 4-5 months all told. I think it's a feature about on the same scale as secondary indexes which took about that long.

    I actually think we should ship the one table case sometime semi soon (I think post 2.0 probably), gauge people's response to it and then expand from there. Also if we had triggers then the one table limitation really wouldn't be that bad because you could write triggers to push data from where you wanted it in to your single table where it would get map reduced. We'd add some sugar on top of that it and could actually be really nice. On top of that a lot of the features for managing tables you actually want for this incremental map reduce stuff as well. Redundancy will make the computed value more available. Sharding can help it scale better.

  5. coffeemug commented on Jul 3, 2013

    @coffeemug
    ContributorAuthor

    @jdoliner -- when you get the chance could you explain the design for each of the options? (i.e. single-table option and multi-table option). I'd like to understand how you envision each version would work and where the factor of four-five difference in complexity comes from. (Obviously not urgent since we aren't doing this now)

  6. apendleton commented on Jul 3, 2013

    @apendleton

    @jdoliner yeah, that all sounds awesome. We have a currently-Postgres database that I think I want to eventually replace with something-not-Postgres TBD, and we build aggregates on a whole bunch of tables that are very expensive to compute, and currently recompute everything from scratch on updates (additions, deletions, and changes of records). There's occasionally inter-table stuff, but 90% or more is probably single-table, and if we could change records and get new aggregates without recomputing everything from scratch, that would be a huge boon. I think you're absolutely right, too, that that use case is probably much more common than a complicated multi-table MR situation, and that in the interest of 80%-20% solutions, getting the single-table case out the door early would be totally worthwhile.

  7. jdoliner commented on Jul 5, 2013

    @jdoliner
    Contributor

    @coffeemug actually having thought about this a bit more I think the multi-table version of this is less a question of being complicated from an engineering perspective and more a question of being algorithmically untenable. You can imagine even a fairly simple multi-table mapreduce such as: table.eq_join("foo", table2).map(...).reduce(...) is very complicated to keep track of in an incremental way, and in a lot of cases downright impossible. Even a single row change in table2 can conceivably change the value of every single piece of data going in to the map reduce so there's really just no efficient way to compute an incremental view without basically rerunning the map reduce for every change to table2. We could maybe make some optimizations that made it more efficient if you had an approximately one-to-one join (which is probably the most common case) but that's going to be a big undertaking that only works in very specific cases which will be hard to explain to people and will behave very badly when it's used outside of those cases. Furthermore if people start using arbitrary sub expressions like: table.map(lambda x: query_on_table2(x)).reduce(lambda x,y: query_on_table3(x,y)) then all bets are off.

    I definitely agree that it's annoying to have 2 features which aren't compatible but I think the reality is this is a situation where you can't sugarcoat the algorithmic limitations. Doing so is just going to lead to people bumping in to the limitations as exponential runtimes which is clearly a lot worse.

    My conclusion here is that the easier thing of having map reduce jobs rooted on a single table is actually the right thing to do because it's something I know we can make fast and make in to a very useful feature. Also it's really a very doable thing because almost all the annoying parts of it are already written and "working" for secondary indexes and the system was designed to be easily extended to support incremental map reduce. I'll write up a full proposal for this at some point in the near future.

  8. coffeemug commented on Jul 5, 2013

    @coffeemug
    ContributorAuthor

    @jdoliner -- this makes a lot of sense. I changed my mind -- I think it's ok to make this feature work on a single table and it's probably ok to never make it work on multiple tables. Actually, we already have precedent where we do best effort on non-deterministic queries, and generally handle them differently from deterministic ones. This would be no different.

  9. modified the milestones: subsequent, backlog on Apr 11, 2014
  10. modified the milestones: 1.14, subsequent on Jun 19, 2014
  11. coffeemug commented on Jun 19, 2014

    @coffeemug
    ContributorAuthor

    Moving to 1.14. We should debate the ReQL aspects of it, in case we decide to do it.

    Very roughly:

    r.table('users').avg('age').changes()
    r.table('users').group('city').avg('age').changes()
    r.table('users').group('city').reduce(reduction_fn, anireduction_fn).changes()

    #2542 has some discussion of what this should return. I think:

    • We shouldn't persist things on disk for v1. If the query dies, they rerun and recompute the first value.
    • We should come prepackaged with the inverse functions for common aggregators.
  12. srh commented on Jun 19, 2014

    @srh
    Contributor

    That doesn't seem like incremental map reduce to me. I would expect it to involve some kind of persisted thing that you can query on any connection, not something that requires a live changefeed to be open.

  13. apendleton commented on Jun 19, 2014

    @apendleton

    @srh yes, that was what I meant when I asked about it on HN last year; it's what Couch has and refers to by that name. You basically register a map/reduce job and its results are kept up to date automatically as the records it ran over are changed/deleted/added to.

  14. 80 remaining items

  15. danielmewes commented on Jun 14, 2016

    @danielmewes
    Member

    Also I would like to add that I'm extremely excited about this feature! It's going to be so amazing :-)

  16. v3ss0n commented on Jun 14, 2016

    @v3ss0n

    Can we have 2 and 3 in 2.5? :D

  17. danielmewes commented on Jun 14, 2016

    @danielmewes
    Member

    @v3ss0n I think so :)

  18. mlucy commented on Jun 14, 2016

    @mlucy
    Member

    @danielmewes -- leaving 2 and 3 for later sounds good to me. I don't think 2's representation would be a particularly involved discussion, though -- I was imagining we'd just emit the entire grouped data every time it changed (so {old_val: {grp: red, grp2: red2}, new_val: {grp: red}}). If we wanted to support plain old .group.changes that would require thinking a little about the format, though.

    On coerce_to, I think coerce_to('array') and coerce_to('object') are the only ones that can take a stream.

  19. danielmewes commented on Jun 22, 2016

    @danielmewes
    Member

    Marking settled as:

    • You can write any of these:
      • stream.avg(...).changes()
      • stream.sum(...).changes()
      • stream.count(...).changes()
      • stream.reduce(..., reverse: FUNC).changes()
      • stream.coerceTo('array').changes()
      • stream.coerceTo('object').changes()
    • In particular, it doesn't need to be on a selection;
      .map.reduce.changes etc. are legal. (We should probably support
      concat_map.reduce.changes even though we don't yet support
      .concat_map.changes, since it's easy.)

    For 2.4 we will implement the slower variant that performs the initial reduction on the parsing node rather than distributing it.

  20. marshall007 commented on Jun 22, 2016

    @marshall007
    Contributor

    This is gonna be great! Note that supporting .coerce_to('array').changes() solves the use-case I had for #3719, so I would say we probably don't need anything from that proposal anymore. I also prefer these semantics over the optarg from the other proposal.

  21. nighelles commented on Jul 29, 2016

    @nighelles

    This is in review 3714, except for coerce_to("array")

  22. self-assigned this
    on Jul 29, 2016
  23. modified the milestones: 2.4, 2.4-polish on Oct 14, 2016
  24. dvergeylen commented on Oct 17, 2016

    @dvergeylen

    As this has been transferred to milestone 2.4-polish (for obvious reasons), I just wanted to emphase what @danielmewes has written at this comment as a work-around for the time being. I suggest the following to support the other common aggregation operations:

    • sum(): Very similar to count() but we increment by the value of el instead of 1.
      • <f_BASE> = 0
      • <f_APPLY> = function(acc, el) { return acc.add(el); }
      • <f_UNAPPLY> = function(acc, el) { return acc.sub(el); }
      • <f_EMIT> = function(acc) { return acc; }
    • min():
      • <f_BASE> = Number.POSITIVE_INFINITY
      • <f_APPLY> = function(acc, el) { return (el < acc ? el : acc); }
      • <f_UNAPPLY> = function(acc, el) { return acc; }
      • <f_EMIT> = function(acc) { return acc; }
    • max():
      • <f_BASE> = Number.NEGATIVE_INFINITY
      • <f_APPLY> = function(acc, el) { return (el > acc ? el : acc); }
      • <f_UNAPPLY> = function(acc, el) { return acc; }
      • <f_EMIT> = function(acc) { return acc; }
    • avg(): Similar to what Daniel did, but I would handle empty sets with a one liner like this:
      • <f_EMIT> = function(acc) { return (acc('c').neq(0) ? acc('sum').div(acc('c')) : 0); }

    Any thoughts on this would be appreciated. 😃

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions