You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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.
@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.
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)
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:
@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).
@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}.
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 ...
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.
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.:
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:
Activity
coffeemug commentedon Jan 14, 2014
Note that once this is implemented, we should drop support for
zip, which is currently the worst command in reql (since it hardcodes values).AtnNn commentedon Jan 14, 2014
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_barand03_quux).I suggest that
.collapse()should just merge theleftandrightfields.coffeemug commentedon Jan 14, 2014
I strongly disagree, particularly because hardcoding
leftandrighthas terrible taste to me, and any arbitrarily selected order (other than perhaps random) is better than hardcoding two field names. If we wantcollapse()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 forcollapsewith no arguments, but I think the pros of supporting that with alphabetical ordering outweigh the cons of not supporting it at all.mlucy commentedon Jan 14, 2014
@coffeemug -- I think alphabetical is what we want for
collapsewith 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.mlucy commentedon Jan 14, 2014
Also, I second the point that hardcoding
leftandrightis worse than almost any general deterministic rule for the default behavior.coffeemug commentedon Jan 14, 2014
I agree, apologies for ambiguity.
I weakly disagree (i.e. I won't stand in the way of taking it out if other people don't want it)
neumino commentedon Jan 14, 2014
I'm a little confused.
For nested fields, I would expect this
Can someone give a use case for
collapse(besides whatzipcurrently does)?AtnNn commentedon Jan 14, 2014
There is a special case for
leftandrightbecause those are the fields returned by join. We could also make joins return a two-element array and have.collapsealso 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
a2sorts beforea12. 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:
That kind of operation would make it easier to convert SQL into ReQL.
coffeemug commentedon Jan 14, 2014
@neumino -- for nested arguments,
collapsewould rip out the specified nested field and leave the rest as is (assuming we want to support that). So:@AtnNn -- one other option is to have an optional
conflictthat can take on a couple of meanings (e.g.prefix, andmerge). That would be really cool (though a bit complicated).mlucy commentedon Jan 14, 2014
@AtnNn -- you could make the same argument to say that
order_byshouldn'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}.coffeemug commentedon Jan 14, 2014
Ahh man, that's nasty.
AtnNn commentedon Jan 14, 2014
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 asselect a, a.b, c from t1 as a join t2 as b on ...coffeemug commentedon Jan 14, 2014
@AtnNn -- that works, but it would get extremely annoying for a large number of fields.
coffeemug commentedon Jan 14, 2014
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.
43 remaining items
coffeemug commentedon Aug 14, 2014
Moving to backlog. The demand for this command is way lower than expected.
jeffreywescott commentedon Apr 19, 2016
Am I the only one who doesn't want the results
collapsed? What I want (especially as a replacement forzip, when joining) is a way to replace something like an ID field with a nested object, e.g.:Input:
Desired Output:
muescha commentedon Apr 19, 2016
its more a join or something similar
muescha commentedon Apr 19, 2016
but then it should be also like:
and then
but i think it is a special use case
jeffreywescott commentedon Apr 19, 2016
I'm not sure it's a special use case, particularly for things like GraphQL.
rocketraman commentedon Apr 19, 2016
Yeah I'd say the current approach to
joinin 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:to produce:
or
to produce:
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.
danielmewes commentedon Apr 21, 2016
@jeffreywescott Your use case sounds related to the
groupoption suggested in #2720 .jeffreywescott commentedon Apr 22, 2016
@danielmewes: right you are! Can't wait for that functionality. I'm going weird things in the meantime: