Skip to content

Proposal: r.collapse #1855

Open
Open

Description

@coffeemug

This is a proposal in response to #325.

r.expr({left: {a: 1, b: 2}, right: {b: 3, c: 4}}).collapse()
{a: 1, b: 3, c: 4}

r.expr({a: {a: 1, b: 2}, b: {b: 3, c: 4}}).collapse()
{a: 1, b: 3, c: 4}

r.expr({a: {a: 1, b: 2}, b: {b: 3, c: 4}}).collapse('a', 'b')
{a: 1, b: 3, c: 4}

r.expr({a: {a: 1, b: 2}, b: {b: 3, c: 4}}).collapse('b')
{a: {a: 1, b: 2}, b: 3, c: 4}

r.expr({a: 1, b: 2}).collapse()
// Error: values must be objects to be collapsed

r.expr({left: {a: 1, b: 2}, right: {b: {e:5}, c: 4}}).collapse('left', { 'right': 'b' })
{a: 1, b: 2, {e: 5}, {b: {c: 4}}}

The command collapse takes an arbitrarily long list of field names and injects those values into the top level of the object in alphabetical order. If collapse is given no arguments, it expands all the top-level fields.

collapse can also accept nested object syntax (e.g. {right: 'b'} or {right: {b: true}}). In this case it collapses the specified part of the nested object into the top level object.

Activity

  1. ghost assigned on Jan 14, 2014
  2. coffeemug commented on Jan 14, 2014

    @coffeemug
    ContributorAuthor

    Note that once this is implemented, we should drop support for zip, which is currently the worst command in reql (since it hardcodes values).

  3. AtnNn commented on Jan 14, 2014

    @AtnNn
    Member

    I think that he default should not be to traverse the fields in alphabetical order because naming should not affect the order of operations. It would cause programs to behave differently if the names of the fields change and we would be encouraging programmers to prefix their fields so that they sort correctly (such as 01_foo, 02_bar and 03_quux).

    I suggest that .collapse() should just merge the left and right fields.

  4. coffeemug commented on Jan 14, 2014

    @coffeemug
    ContributorAuthor

    I strongly disagree, particularly because hardcoding left and right has terrible taste to me, and any arbitrarily selected order (other than perhaps random) is better than hardcoding two field names. If we want collapse() to work by default, we'd have to pick some order, and alphabetical order is probably the best we can hope for. Alternatively we can drop support for collapse with no arguments, but I think the pros of supporting that with alphabetical ordering outweigh the cons of not supporting it at all.

  5. mlucy commented on Jan 14, 2014

    @mlucy
    Member

    @coffeemug -- I think alphabetical is what we want for collapse with no arguments, but when people specify fields I think that we should proceed from left to right through the fields. This gives people a really easy way to get the merge behavior they want (stream.collapse('b', 'a') if they want a b-preferential merge). I also don't think we should support nested object syntax; it seems too complicated to me, and you can get most of what you want from it by chaining collapse commands.

  6. mlucy commented on Jan 14, 2014

    @mlucy
    Member

    Also, I second the point that hardcoding left and right is worse than almost any general deterministic rule for the default behavior.

  7. coffeemug commented on Jan 14, 2014

    @coffeemug
    ContributorAuthor

    I think alphabetical is what we want for collapse with no arguments, but when people specify fields I think that we should proceed from left to right through the fields.

    I agree, apologies for ambiguity.

    I also don't think we should support nested object syntax

    I weakly disagree (i.e. I won't stand in the way of taking it out if other people don't want it)

  8. neumino commented on Jan 14, 2014

    @neumino
    Member

    I'm a little confused.

    For nested fields, I would expect this

    r.expr({left: {a: 1, b: 2}, right: {b: {e:5}, c: 4}}).collapse('left', { 'right': 'b' })
    {a: 1, b: 2, {e: 5}}
    // and not this {a: 1, b: 2, {e: 5}, {b: {c: 4}}}

    Can someone give a use case for collapse (besides what zip currently does)?

  9. AtnNn commented on Jan 14, 2014

    @AtnNn
    Member

    There is a special case for left and right because those are the fields returned by join. We could also make joins return a two-element array and have .collapse also work on arrays (just like like .reduce(merge)).

    I think we should not sort alphabetically because it has no inherent advantage. We could use numerical order so that a2 sorts before a12. Or, since "tfel" also sorts before "tghir", we could also use the alphabetical order of the reverse of the fields.

    As an unrelated suggestion, how about renaming fields that conflict:

    r.expr({a: {a: 1, b: 2}, b: {b: 3, c: 4}}).collapse('a', 'b', {prefixConflict: True})
    {a: 1, 'a.b': 2, 'b.b': 3, c: 4}
    

    That kind of operation would make it easier to convert SQL into ReQL.

  10. coffeemug commented on Jan 14, 2014

    @coffeemug
    ContributorAuthor

    @neumino -- for nested arguments, collapse would rip out the specified nested field and leave the rest as is (assuming we want to support that). So:

    r.expr({left: {a: 1, b: 2}, right: {b: {e:5}, c: 4}}).collapse('left', { 'right': 'b' })
    {a:1, b:2, right: {c: 4}, e: 5}

    @AtnNn -- one other option is to have an optional conflict that can take on a couple of meanings (e.g. prefix, and merge). That would be really cool (though a bit complicated).

  11. mlucy commented on Jan 14, 2014

    @mlucy
    Member

    @AtnNn -- you could make the same argument to say that order_by shouldn't sort lexicographically because it has no inherent advantage over sorting by e.g. the reverse of the md5 hash or whatever. When you need to pick an ordering (which you do when merging several things together), lexicographic is a very sane default. We also already order the keys in an object lexicographically when comparing two objects (as I would hope everyone does).

    Since there's an easy way to override it, I think we should default to merging with lexicographic preference.

    Doing string munging on field names would turn into a nightmare very quickly. How do you resolve: {a: {b.c: 1}, a.b: {c: 1}} for example? I wouldn't necessarily be opposed to leaving conflicting fields in their original documents, so {a: {b: 1, c: 1, d: 1}, b: {c: 1, e: 1}} would collapse to {a: {b: 1, c: 1}, d: 1, b: {c: 1}, e: 1}.

  12. coffeemug commented on Jan 14, 2014

    @coffeemug
    ContributorAuthor

    {a: {b.c: 1}, a.b: {c: 1}}

    Ahh man, that's nasty.

  13. AtnNn commented on Jan 14, 2014

    @AtnNn
    Member

    I had forgotten that RethinkDB used alphabetical ordering of fields. I would argue that comparing objects should be an error, but not here.

    I much prefer doing something like this .do(lambda x: { 'a': x['a']['a'], 'b': x['a']['b'], 'c': x['b']['c'] }) than using .collapse. The main application I see for collapse is to port SQL code such as select a, a.b, c from t1 as a join t2 as b on ...

  14. coffeemug commented on Jan 14, 2014

    @coffeemug
    ContributorAuthor

    @AtnNn -- that works, but it would get extremely annoying for a large number of fields.

  15. coffeemug commented on Jan 14, 2014

    @coffeemug
    ContributorAuthor

    I have a nagging suspicion that this is somewhat related to #881 in some way, but I'm not sure how exactly. I feel like there are some really elegant abstractions here. I'd like to sleep on it for another day or so.

  16. 43 remaining items

  17. modified the milestones: , backlog on Aug 13, 2014
  18. coffeemug commented on Aug 14, 2014

    @coffeemug
    ContributorAuthor

    Moving to backlog. The demand for this command is way lower than expected.

  19. modified the milestones: , backlog on Sep 10, 2014
  20. jeffreywescott commented on Apr 19, 2016

    @jeffreywescott

    Am I the only one who doesn't want the results collapsed? What I want (especially as a replacement for zip, when joining) is a way to replace something like an ID field with a nested object, e.g.:

    Input:

    left: { id: 1, name: 'John Doe', groupId: 12 }
    right: { id: 12, name: 'Some Group Name' }
    

    Desired Output:

    { id: 1, name: 'John Doe', group: { id: 12, name: 'Some Group Name' } }
    
  21. muescha commented on Apr 19, 2016

    @muescha

    its more a join or something similar

  22. muescha commented on Apr 19, 2016

    @muescha

    but then it should be also like:

    data = {
      users: [ 
        { id: 1, name: 'John Doe', groupId: 12 }, 
        { id: 2, name: 'Jean Doe', groupId: 5 }
      ],
      groups: [ 
        { id: 5, name: 'Some Group Name' }, 
        { id: 12, name: 'Some Group Name' }
      ]
    }

    and then

    r.expr(data).replace(['user','groupId'],['groups','id'],'group'])
    r.expr(data).replaceField(
      {
        field: [ 'user' , 'groupId' ],
        with:  [ 'groups' , 'id' ],
        rename:  'group'
      })
    r.expr(data).replaceField(
      {
        field: [ 'user' , 'groupId' ],
        with:    'groups',
        on:    [ 'id' ],
        rename:  'group'
      })

    but i think it is a special use case

  23. jeffreywescott commented on Apr 19, 2016

    @jeffreywescott

    I'm not sure it's a special use case, particularly for things like GraphQL.

  24. rocketraman commented on Apr 19, 2016

    @rocketraman
    Contributor

    Yeah I'd say the current approach to join in RethinkDB is very SQL-like: by default, return a flat structure with the content from both sides of the join mashed together (collapsed?). Since I'm getting back JSON, I'd much rather have an easy way to make my result look like Jeffrey noted above. It should also be easy to operate on lists or maps of foreign keys, like:

    left: { id: 1, name: 'John Doe', groupIds: [12, 13] }
    right: [{ id: 12, name: 'Some Group Name' }, { id: 13, name: 'Another Group Name' }]
    

    to produce:

    { id: 1, name: 'John Doe', groups: [ { id: 12, name: 'Some Group Name' }, { id: 13, name: 'Another Group Name' } ] }
    

    or

    left: { id: 1, name: 'John Doe', { homeGroup: 12, workGroup: 13 } }
    right: [{ id: 12, name: 'Some Group Name' }, { id: 13, name: 'Another Group Name' }]
    

    to produce:

    { id: 1, name: 'John Doe', groups: { homeGroup: { id: 12, name: 'Some Group Name' }, workGroup: { id: 13, name: 'Another Group Name' } } }
    

    UPDATE: According to https://www.rethinkdb.com/docs/table-joins/#using-subqueries, the "right" way to do the above is via a subquery rather than a join. Not sure if there is a case for an easy mechanism to accomplish this with join results as well.

  25. danielmewes commented on Apr 21, 2016

    @danielmewes
    Member

    @jeffreywescott Your use case sounds related to the group option suggested in #2720 .

  26. jeffreywescott commented on Apr 22, 2016

    @jeffreywescott

    @danielmewes: right you are! Can't wait for that functionality. I'm going weird things in the meantime:

            const result = await r.table('votes')
              .eqJoin('playerId', r.table('players'))
              .without({left: 'playerId'})
              .map(doc => doc('left').merge({player: doc('right')}))
              .eqJoin('cycleId', r.table('cycles'))
              .without({left: 'cycleId'})
              .map(doc => doc('left').merge({cycle: doc('right')}))
              .run()
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions