Skip to content

Add a multi optarg to group commands. #1533

Closed

Description

@jdoliner

Similar to how we have a multi optarg for indexes it would be cool if you could return an array from a group function and have it count the document in multiple groups. For example suppose I have a table, "blog_posts" and I want to count comments based on tags. I could write:

table("blog_posts").group_by(row["tags"], count("comment_count"), multi=True)

this looks nicer if group is a separate function but it's still fairly usable otherwise.

Activity

  1. jdoliner commented on Oct 10, 2013

    @jdoliner
    ContributorAuthor

    Putting this in backlog for now, we'll get to it when we get to it.

  2. coffeemug commented on Oct 10, 2013

    @coffeemug
    Contributor

    FYI, a few people asked me for this.

  3. mlucy commented on Oct 10, 2013

    @mlucy
    Member

    I also think this is a great idea. Tagging as a proposal and moving to subsequent, since people have asked for it.

  4. jdoliner commented on Oct 10, 2013

    @jdoliner
    ContributorAuthor

    Alright I guess we're fast tracking this. As far as I can see this is a dead simple implementation. Anyone have any misgivings about the API? It seems pretty obvious to me that this should mimic the multi index API. Also just to be specific This is going to affect gmr in the same was as group_by.

  5. mlucy commented on Oct 10, 2013

    @mlucy
    Member

    No objections to the api.

  6. coffeemug commented on Oct 10, 2013

    @coffeemug
    Contributor

    No objections. But just so you know, I'm going to bide my time until multi slowly makes its way into various functions in the API, and then gently reintroduce #334 (comment).

  7. jdoliner commented on Oct 10, 2013

    @jdoliner
    ContributorAuthor

    @coffeemug you mean the emit syntax? How would that work in group_by?

  8. coffeemug commented on Oct 10, 2013

    @coffeemug
    Contributor

    No, I meant removing concatMap and replacing it with the following:

    r.table('users').map(r.row['posts'], multi = True)
  9. jdoliner commented on Oct 10, 2013

    @jdoliner
    ContributorAuthor

    @coffeemug the real lulz begin when grouped_map_reduce now takes a multi arg for group and a multi arg for map. Obviously this isn't actual a problem we could solve it by splitting the 2 components up.

  10. jdoliner commented on Oct 11, 2013

    @jdoliner
    ContributorAuthor

    Also while we're adding it any objections to adding this to eq_join? i.e.

    r.table("foo").eq_join("field_of_foreign_keys", r.table("bar"), multi=True).run()
    

    I just ran in to a situation where I need this.

  11. mlucy commented on Oct 11, 2013

    @mlucy
    Member

    No objections to adding it to eq_join.

  12. coffeemug commented on Oct 11, 2013

    @coffeemug
    Contributor

    I think it's a little weird in eq_join and the same thing can easily be accomplished by prepending a concatMap. So I'm not sure we should do that here, but if other people are cool with it, I don't have a strong objection.

  13. jdoliner commented on Oct 11, 2013

    @jdoliner
    ContributorAuthor

    @coffeemug it's awkward to do with a concat_map. So in my example the document representing a blog post might look like:

    {
        "comment_ids" : [0,1,2,3]
    }

    to join this to the comment table I'd have to do:

    r.table("blog_posts")
    .concat_map(lambda x: x["comment_ids"].map(lambda y: x.merge("comment_id" : y)))
    .eq_join("comment_id", r.table("comments"))

    which I think is tricky enough that it's worth having a shorthand for. Why does it seem weird to you? I think it's pretty analogous to our other multis.

  14. coffeemug commented on Oct 11, 2013

    @coffeemug
    Contributor

    My immediate reaction was that it's not obvious at first glance that it applies to the leftmost argument in eq_join. You sort of have to think about it for a bit, and at first glance you might think "is it affecting how it treats the second argument?"

    But the bigger reason is that in other cases multi always means "treat the output of this command differently", whereas in case of eq_join it means "treat the input to this command differently". For index creation, grouping, and mapping (if we choose to get rid of concatMap) , multi affects what the function spits out, and there is really no other way to do it in the language. For eq_join it affects the input, and there is another way to do it in the language.

    That's not to say that we shouldn't fix cumbersome cases, just that this particular one seems different enough that I think it's worth thinking about.

  15. coffeemug commented on Oct 11, 2013

    @coffeemug
    Contributor

    Also note that the reason you have to go through the annoying map.merge step is that merge doesn't accept a lambda. After #1167 this problem will go away, so you'll be able to just say merge (though admittedly it doesn't make your example all that much nicer).

    Basically adding multi to eq_join (i.e. making it a modifier for the input argument) seems like special casing something that might be better represented as a more general purpose primitive. Consider an alternative (made up) query that would do what you want:

    r.table("blog_posts")
    .merge({ 'comment_id': r.row['comment_id'] }, multi=True)
    .eq_join(comment_id, r.table("comments"))

    Or may be we could add a porcelain primitive to make the merge part above less cumbersome:

    r.table("blog_posts")
    .unwind('comment_id')    # same as   merge({ 'comment_id: r.row['comment_id'] }, multi=True)
    .eq_join(comment_id, r.table("comments"))

    That seems far more general than adding multi to eq_join.

  16. coffeemug commented on Oct 11, 2013

    @coffeemug
    Contributor

    Note, that I'm not saying the proposals above are good ideas that we should implement (they may or may not be, I'm not sure). It's more of a way to hint at why adding multi to eq_join smells funny.

  17. jdoliner commented on Oct 11, 2013

    @jdoliner
    ContributorAuthor

    I'm a bit confused about what you mean about the multi argument to eq_join affecting the input while the others affect the output. I think they all affect the output and they all affect it in exactly the same way. Consider the following:

    For any f the following holds:

    stream.map(f, multi=True).count() ==
    stream.eq_join(f, table, multi=True).count() == 
    stream.group(f, multi=True).count() == # this doesn't exist but it could
    table.order_by(index="multi_index_on_f").count() # this is kind of a hack but you get the idea 

    Assuming that you don't hit the error cases such as the f erroring or the corresponding row not existing.

    I think trying to come up with a primitive for this is not going to yield good results (there's some obvious problems with both the ones you proposed) but also I think you'll wind up with a primitive that's impossible to describe to users unless you show them how it interacts with these functions.

  18. coffeemug commented on Oct 11, 2013

    @coffeemug
    Contributor

    Ok, my not-so-strong objection just got even weaker.

  19. coffeemug commented on Feb 25, 2014

    @coffeemug
    Contributor

    Moving to 1.12 and assigning to @mlucy. I've opened a separate issue for adding multi to joins (see #2008).

  20. modified the milestones: 1.12, on Feb 25, 2014
  21. mlucy commented on Feb 25, 2014

    @mlucy
    Member

    Marking as API_settled.

  22. mlucy commented on Feb 25, 2014

    @mlucy
    Member

    This is in review 1262 by @Tryneus .

  23. mlucy commented on Feb 26, 2014

    @mlucy
    Member

    This is in next.

  24. modified the milestones: moved, 1.12 on Jul 18, 2014
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

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions