Skip to content

Implement support for multiple aggregations after r.group #1725

Open

Description

@coffeemug

Once r.group is done (#1096), we should implement support for multiple aggregations (i.e. doing both sum and average in one shot).

@mlucy proposed doing this by implementing a way to split streams (from what I understand, it's basically tee):

r.table('test').map{|x| x[:id]}.split{|ids| [ids.count, ids.reduce{...}]}

Activity

  1. jdoliner commented on Nov 30, 2013

    @jdoliner
    Contributor

    If we going to allow streams to be bound to variables we should just allow
    it in "do" no reason to add a new keyword for it.

    On Friday, November 29, 2013, coffeemug wrote:

    Once r.group is done (#1096#1096),
    we should implement support for multiple aggregations (i.e. doing both sum
    and average in one shot).

    @mlucy https://github.com/mlucy proposed doing this by implementing a
    way to split streams (from what I understand, it's basically tee):

    r.table('test').map{|x| x[:id]}.split{|ids| [ids.count, ids.reduce{...}]}

    —
    Reply to this email directly or view it on GitHubhttps://github.com//issues/1725
    .

  2. mlucy commented on Nov 30, 2013

    @mlucy
    Member

    I was thinking of split having semantics where the only legal operations on the value in the variable are reductions (sum etc. count as reductions for this). This constraint will let us have an efficient implementation because we won't have to deal with cases like table.do{|t| t.map{|row| row.merge({count: t.count})}. Handling that case correctly would require loading the entire table into memory, or else using different values for t both times it's referenced. By contrast, if we have a term split and only allow reductions on the bound "stream", we only have to load a single batch into memory at once to feed all the reductions.

    This is in subsequent, though, so let's avoid getting too deep into it right now.

  3. coffeemug commented on Nov 30, 2013

    @coffeemug
    ContributorAuthor

    I was thinking of split having semantics where the only legal operations on the value in the variable are reductions (sum etc. count as reductions for this).

    I think it would be much more intuitive to implement a command that combines reductions instead:

    r.table('users').group('city').combine(r.count, r.avg('age'), r.max('height'))

    (The syntax above is might be weird, but a step in the right direction IMO; also, sorry for commenting on this out of discussion period).

  4. jdoliner commented on Nov 30, 2013

    @jdoliner
    Contributor

    @mlucy it seems like you're still going to have to do some fairly heavy duty analysis to make sure that your arguments to split don't reference themselves. And I think that if we're going to do that it's not a huge step farther to just let people bind streams to variables and statically check that they're not evaluating things recursively. It also gets a lot better. For example with this avg becomes almost unnecessary. You can simply write:

    table["pts"].do({|s| s.sum() / s.count()})

    I'm not advocating we get rid of avg but having it be this flexible seems really powerful. I actually talked to @AtnNn about a scheme like this he had come up with and I believe actually wrote some code for in the haskell driver. I think we should definitely take a look at how nice we can get this.

  5. mlucy commented on Dec 2, 2013

    @mlucy
    Member

    @jdoliner -- that example with avg would work with the split semantics I'm proposing as well.

    The analysis is actually pretty straightforward; the variable x bound by split:

    • Can only have terminals called on it (we already have a list of these for when we push work to the shards).
    • Can't be referenced from inside a nested function.

    If we enforce those two constraints, we can do something efficient without too much work.

    I think that supporting full-blown streams in variables in a way that we know will be efficient would be a lot more work, but I'm not necessarily opposed to it if there's an easy way to do it.

  6. AtnNn commented on Dec 2, 2013

    @AtnNn
    Member

    Here is what I put in the Haskell driver:

    http://hackage.haskell.org/package/rethinkdb-1.8.0.5/docs/Database-RethinkDB.html#v:groupBy

    It takes any function and splits it into map, reduce and finalizer functions. When the function contains multiple aggregations, say (\row -> f row / g row), it turns it into a map of (\row -> [f_map row, g_map row]), a reduce of (\a b -> [f_reduce a[0] b[0], g_reduce a[1] b[1]]) and a finalizer of (\row -> row[0] / row[1])

  7. jdoliner commented on Dec 2, 2013

    @jdoliner
    Contributor

    It sounds to me like we're pretty close to agreeing on the functionality here. @mlucy what you're describing sounds close enough to me to binding the stream to a variable that I think it's going to be easier to explain to users as do than split. I think concept of "if I want to use the same value multiple times without recomputing it I bind it to a variable" is universal enough that this is the easiest way to explain the functionality. And when people do hit the limitations of binding streams I don't think it's going to be harder to understand than it would be if it was a differently named function. Also we will eventually be overcoming a lot of those limitations so long term it's going to be very similar to do.

    Also if we don't just roll the functionality in to do I really hate the name split. We're not splitting the stream what we're doing is better described as tee or maybe clone.

  8. coffeemug commented on Dec 2, 2013

    @coffeemug
    ContributorAuthor

    I think that if we do this, we should just use do. If people use the streams incorrectly, they'll get an informative error message about the limitations, and will know what went wrong and how to use the feature. If eventually we remove the limitation, we'll just let people know and they'll be able to essentially use the same API.

    However, I still think this will require a porcelain syntax like #1725 (comment) to make it easier for users to understand/type/etc. I think it's worthwhile to try to figure out what that syntax is first. It might give us bigger bang for the buck to just implement the porcelain syntax directly and drop the more general purpose implementation.

  9. mlucy commented on Dec 2, 2013

    @mlucy
    Member

    What about multi_reduce instead of split? If the only legal operations on the variable are reductions, I think using plain-old do will be more confusing than helpful.

  10. jdoliner commented on Dec 3, 2013

    @jdoliner
    Contributor

    There isn't a good name here because no matter how you name it you're still introducing an unneeded and confusing concept. Users shouldn't have to think of it as multi_reduce for one thing users don't think of count and sum as falling under the heading of reduce. Passing a stream through do is something that I've already seen tons of users do expecting mostly the semantics we've described. From what I've seen the types of operations rarely run afoul of the limitations we're talking about but most importantly when they do we can give them a very clear error message so this isn't dangerous. We're just going to be giving them that same error message when they run afoul of the rules in another function so why not just put this functionality where people expect it?


    As for the porcelain command. One of the things I really like about this proposal is that it removes reductions as nouns in ReQL. Having objects like r.avg("foo") floating around in group_by worked well at the time but IMO it's a lot easier to understand if you think of avg as a verb and you only see it in the context of foo.avg() which reads as "take the average of foo." ReQL's really built around chaining and the idea that foo.bar can be thought of as "apply the action bar to the noun foo." Bringing the syntax for groups in line with this seems both very powerful and like it lets us present a much tighter coherent API.

    I'm failing to see what the combine proposal is adding. I don't think it really makes queries much shorter (if any shorter). And it gives us an entirely new way of calling every single reducing function that we need to implement (actually a decent amount of work) and document.

  11. coffeemug commented on Dec 3, 2013

    @coffeemug
    ContributorAuthor

    I'm failing to see what the combine proposal is adding.

    Here is what I think it would get us, and what I was trying to convey. Suppose you're doing demographics analysis and want to determine an average salary in a city. Here is how you'd do it in SQL:

    SELECT city, AVG(salary)
    FROM residents
    GROUP BY city
    

    And here is what this query would look like under the current proposal:

    r.table('residents').group('city').avg('salary')

    Now, suppose you're thinking "hmm, I wonder what this will look like if I see the average age too". In SQL, all you have to do is add AVG(age):

    SELECT city, AVG(salary), AVG(age)
    FROM residents
    GROUP BY city
    

    The typing distance is very short, but most importantly the conceptual distance is very short too. You don't have to learn any new concepts at all. Now, here's what this query would look like in ReQL under proposed syntax (I'm assuming the do syntax, but the same thing applies under split):

    r.table('residents').group('city').do(lambda x: [x.avg('salary'), x.avg('age')])

    Ignoring the question of labeling the fields and what this actually returns, we're asking users to make quite a large conceptual jump. They have to introduce the do command, lambdas, and arrays to get to their data.

    Given how ridiculously common it is to want to do such a thing, I think we should think about simpler porcelain syntax. That being said, I agree combine is bad syntax, and that removing the notion of aggregators as nouns is a good idea. I think we should hunt around for a better solution than combine.

  12. modified the milestones: 1.14, on Jun 12, 2014
  13. coffeemug commented on Jun 24, 2014

    @coffeemug
    ContributorAuthor

    Moving to 1.14-polish. This isn't as important as major features, and I'd like to agree on those first. We can tackle this later.

  14. 11 remaining items

  15. modified the milestones: , 2.1 on Mar 26, 2015
  16. modified the milestones: , subsequent on Apr 22, 2015
  17. coffeemug commented on Apr 22, 2015

    @coffeemug
    ContributorAuthor

    @danielmewes -- I'd like to propose taking this out of the reql discussion for 2.1/polish. There are already a lot of issues to discuss, and I think this one is unlikely to get a good discussion that it deserves. How do you feel about getting back to it after 2.1 is out (or possibly after 2.2)?

  18. danielmewes commented on Apr 22, 2015

    @danielmewes
    Member

    Agreed on second thought, I already did.

  19. dmitry commented on Aug 26, 2016

    @dmitry

    As I understood, this feature will allow to aggregate results into a multiple separate groups as it's done in Eslasticsearch?

  20. danielmewes commented on Aug 26, 2016

    @danielmewes
    Member

    @dmitry Not entirely sure what you mean. Do you have a link to documentation of the Elasticsearch feature you're referring to?

  21. dmitry commented on Aug 27, 2016

    @dmitry

    @dalanmiller bucket aggregations: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket.html

    And specifically I would be glad if the following will work (filter aggregations):
    https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-filter-aggregation.html - specifically filter a values inside the aggregation bucket, not affecting on the other aggregations and the main filter (something like SQL subselect, that is totally independent on the other queries... but it should be much faster, than subselect).

    Use case: faceted search

  22. danielmewes commented on Aug 29, 2016

    @danielmewes
    Member

    @dmitry I'm not quite sure if this is the same as what this issue is proposing, but it looks like it allows for similar functionality.

    The split syntax that was proposed originally would allow for filtering for only some of the aggregations.

  23. dmitry commented on Aug 29, 2016

    @dmitry

    @danielmewes is it possible at the moment to construct a query that allows to generate faceted search? I'm especially interested in disjunctive faceting, like:

    disjunctive faceting by algolia

  24. danielmewes commented on Aug 30, 2016

    @danielmewes
    Member

    @dmitry Can you explain a bit more what exactly you mean by faceted search?

    If the categories are disjunct, can't you just do a .group("category").count() for this?

    If they are not disjunct, you could use a group("categories", {multi: true}).count(), where the categories field would need to be an array of categories in each document.

  25. dmitry commented on Aug 31, 2016

    @dmitry

    @danielmewes correct example could be made only in case of the several filters.

    Lets say we have "categories" with ID numbers and "prices" with ranges (0-10, 10-20). When I'm selecting "0-10" in prices and 1 in categories, other "prices" and "categories" should also have count numbers, which tells how many "items" will filtered-in after I select those categories or price ranges. The problem here is when I'm selecting category 1, the other "categories" should be also grouped. But group aggregates values only after filter makes it work, so the group could not return count of category "2" when category "2" was selected in a filter.

    Simply: "categories" group should somehow skip filter by "categories", but "prices" group should include filtering by the "categories".

    Goal of the disjunctive faceted search is to show only possible variants of filters, and by including more selections in one filter (categories or prices) it extends search.

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