Skip to content

GMR: polymorphism for grouped_data/groups_to_array #2024

Closed

Description

@coffeemug

Suppose I run the following:

r.table('wiki_articles')('text').group(r.row.split(), {multi:true}).count()

I get a group and a reduction with frequency of word usage. Now I want to sort it:

r.table('wiki_articles')('text').group(r.row.split(), {multi:true}).count().orderBy(r.desc('reduction'))

No such luck -- my orderBy works on each reduction, not on whole grouped_data object. This is really bad because to do anything useful with grouped_data I have to first run groups_to_array.

I think it should work completely differently. Every time we call anything on grouped_data, it should be called on the object (as if it were an array or a stream), not on each reduction. So I should be able to call coerce_to('array') on grouped_data and actually get an array.

The current implementation allows me to do r.table('wiki_articles')('text').group(r.row.split(), {multi:true}).count().add(1), but that's not useful -- I almost never ever want to do that. Instead, I should be forced to map.

I also don't think groups_to_array should be equivalent to coerce_to('array') when called on grouped_data and should work on grouped_stream (which I thought was the whole point of groups_to_array).

Activity

  1. added this to the 1.12 milestone on Feb 26, 2014
  2. neumino commented on Feb 26, 2014

    @neumino
    Member

    What is a group<array>? Is it a grouped_stream or a grouped_data?

  3. mlucy commented on Feb 26, 2014

    @mlucy
    Member

    I think that's the exact opposite of the behavior we want. Let's say you do r(SOME_ARRAY).group('a') -- you get back GROUPED_DATA, where the data are arrays. We want r(SOME_ARRAY).group('a').count to call count on those arrays, not count the number of groups, because that's what tbl.group('a').count does.

    95% of the time what you want to do is operate on the content of the groups.

  4. mlucy commented on Feb 26, 2014

    @mlucy
    Member

    Also, automatically coercing GROUPED_DATA to its array form would be incredibly confusing. (If you're using the data explorer it might not feel too confusing because the array form of GROUPED_DATA happens to be the same as what the javascript driver displays, but if you're using Ruby it's crazy -- r.table('test').group('a').count returns an object, but r.table('test').group('a').map{...} return an array?)

  5. mlucy commented on Feb 26, 2014

    @mlucy
    Member

    (Actually, this is a pretty solid argument for not having the JS driver turn GROUPED_DATA into an array of objects that matches the output of groups_to_array -- it's likely to confuse users as well.)

  6. mlucy commented on Feb 26, 2014

    @mlucy
    Member

    Another example where you want to act on the values of the groups:

    # Get each user's highest-scoring game.
    > r.table('games').group('user').max('score')['game_id']
    {'mlucy' => 'game_328490',
     'slava' => 'game_339240',
     ...}
  7. coffeemug commented on Feb 26, 2014

    @coffeemug
    ContributorAuthor

    Let's say you do r(SOME_ARRAY).group('a') -- you get back GROUPED_DATA, where the data are arrays. We want r(SOME_ARRAY).group('a').count to call count on those arrays, not count the number of groups, because that's what tbl.group('a').count does.

    This is orthogonal to the issue. I agree with you that this is exactly what you want, but I don't think you should get that by having r(SOME_ARRAY).group('a') return GROUPED_DATA and defining things on that in a certain way. We should have GROUPING<STREAM> and GROUPING<ARRAY> instead.

    This way:

    • table.group returns GROUPING<STREAM>
    • array.group returns GROUPING<ARRAY>
    • a reduction on GROUPING<*> returns GROUPED_DATA

    That would get everything behaving properly.

    Also, automatically coercing GROUPED_DATA to its array form would be incredibly confusing. ([...] if you're using Ruby it's crazy -- r.table('test').group('a').count returns an object, but r.table('test').group('a').map{...} return an array?)

    I'm not sure what you mean. Assuming we automatically coerce grouped streams to arrays, both of the examples above return an object in Ruby.

    Another example where you want to act on the values of the groups

    That would do exactly what you want because grouped_data.map(...) -> grouped_data.

  8. mlucy commented on Feb 26, 2014

    @mlucy
    Member

    Sorry, let me try again.

    You were proposing that if you have GROUPED_DATA, and you called order_by on it, that it be converted from GROUPED_DATA to an ARRAY of the form [{group: ..., reduction: ...}, ...]. This is confusing. It doesn't seem confusing in JS because we turn GROUPED_DATA into that form in the clients, but in Ruby you would get an array instead of an object back.

    On the topic of GROUPING<ARRAY> vs. GROUPED_DATA, I really don't think we want two different ways to have grouped data. Why should stream.coerce_to('array').group(...) behave differently from stream.group(...).coerce_to('array')? Or if you want to special case the second, why should it be different from stream.group(...).map{|x| [x]}.reduce{|a,b| a+b}? An array is just a datum like any other.

  9. mlucy commented on Feb 26, 2014

    @mlucy
    Member

    Another example where you want to act on the values of the groups

    That would do exactly what you want because grouped_data.map(...) -> grouped_data.

    The ['game_id'] at the end is the part that you want to be distributed. You want it to call get_field on each of the groups, not call get_field on the {group: ..., reduction: ...} objects in some implicitly created array.

  10. mlucy commented on Feb 26, 2014

    @mlucy
    Member

    Er, why don't we just talk about this in person tomorrow. I get the feeling we aren't really making forward progress.

  11. coffeemug commented on Feb 26, 2014

    @coffeemug
    ContributorAuthor

    Ok.

    Also, sorry for opening these issues now. I realize it's annoying, but I think we spoke past each other in the original GMR spec in some cases without realizing it.

  12. neumino commented on Feb 26, 2014

    @neumino
    Member

    If you talk about it tomorrow in person, please let me in.

  13. mlucy commented on Feb 26, 2014

    @mlucy
    Member

    So, entirely separate from talking about what we should do, I'd like to sit down with Slava and hammer out:

    • Terminology.
    • What we currently do.
    • What we could plausibly do.

    Anyone's of course welcome to join for some or all of that, but I'm expecting a lot of typing, pointing, and saying "so what do you think the return type of that is?", and it might be pretty boring.

  14. coffeemug commented on Feb 26, 2014

    @coffeemug
    ContributorAuthor

    Yep, the two of us should first get together and agree on what we both think is happening. Then of course @neumino you're welcome to join!

  15. coffeemug commented on Feb 27, 2014

    @coffeemug
    ContributorAuthor

    Ok, everyone is in agreement to rename groupsToArray to ungroup. @mlucy will open an issue about it, we'll close this one then.

  16. mlucy commented on Feb 27, 2014

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions