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
We've learned a lot about how people use joins, and I think we can have a much cleaner, elegant, and useful API. I propose making the following changes:
Drop innerJoin and outerJoin commands. They're too slow for use in a realtime system, and everyone ends up creating an index and using eqJoin because these commands are effectively unusable for performance reasons.
Rename eqJoin to join.
Add more optargs to control behavior of join
include_missing (defaults to true). This optarg would essentially provide outer join behavior. If a row isn't found in the right table, it would be included into the resultset anyway, with the right value set to null.
group (defaults to true). If this optarg is set to false, the join command would operate as it does now. If it's set to true, it would group the right values into an array and inject them into the row via a 'joined' field. If it's a string, it would act in a similar way to when it's set to true but the 'joined' field would be renamed to the string. If the field name already exists in the document, it should be overwritten.
multi (defaults to false). If this optarg is set to false, the join command operates as it does now. If it's set to true, the left field argument is treated as an array of ids, and the join command joins in each field from the rightmost table that exists in this array.
Could we have include_missing default to true since group defaults to true?
That sounds like a good idea.
If there are more than one document, we can throw an error.
So I guess we should expand the spec. If group is true, then if the user doesn't specify an index to the join (and therefore use the primary key from the right table), the joined field should include a single document, not an array. If they specify an index (and therefore use get_all), the joined field should include an array.
We can add an optarg enforce_one or a more general one enforce_max or something like that. I don't really like that because it seems overly specific to me, but I'm not entirely opposed to it.
If only one document is expected, we return it as an object, not an array with one document
We can overwrite a whole object to handle a many to many relations in a nice way.
We can have nested joined. The compose option can look heavy, but it's from my point of view an order magnitude better than asking users to enter/exit scopes.
What it doesn't handle
For a many to many relation on a table with itself, people have to store the relation twice if they want a bi-directional link. They cannot store an array of two ids, and use a multi index.
I couldn't come up with a nice syntax, but if someone can, it could be nice.
@coffeemug -- we didn't seem to come to a real consensus on this inside the discussion period. Should we punt on it until the next discussion period, or should we just pick something? (I think both of the proposals on the table are better than what we have now.)
@neumino Your proposal looks really good, but I think the compose and group options could be combined (plus I think compose is a better term for describing what your group does).
If set to undefined join produces existing behavior. If set to a string it would group the right values into an array and assign the result to a property on the left document with the provided name. If provided a function, it will take two arguments: the left document and the joined documents.
Examples for both use cases:
// equivalent to @neumino's "group" exampler.table("posts").join("id",r.table("comments"),{index: "postId",compose: "comments"})// equivalent to @neumino's "group + compose" exampler.table("posts").join("id",r.table("comments"),{index: "postId",compose: function(post,comments){returnpost.merge({comments: comments.orderBy("date")});}})
So in addition to combining the options, this allows you to arbitrarily compose the join result (instead of being restricted to the two group options).
I like the idea of binding both the sequence and the post.
That would actually make bi-directional relation work with multi indexes.
I'm a little less fan of having to call merge inside the compose function though. It means that you must find about merge to do a join, and one point of the new join should be to make things simpler.
I would first provide the sequence, then the parent document too (so function(comments, post) in the example -- but that's probably because I would like to avoid the user to call post.merge(...).
Since we're past the reql discussion period, I'd like to put this back in subsequent. We'll settle this during the next discussion period.
This command is fairly important, so I don't think we should just go with what we have so far. I'd like to discuss @neumino's proposal and the use cases it attempts to address in more detail.
I originally wanted to try and schedule this for 2.1 (as it's reasonably easy to do, will clean up the join API a lot, add more functionality, and generally be less of a wart in ReQL).
Would like to see this discussion brought up again soon after the 2.1 release. See #4566 which would be solved by the include_missing optarg. Really been looking forward to group as well which would simplify a lot of code for us.
It's actually an interesting question whether an extended join command would be any faster than the options from #4566 with filter + get and/or loading one side into an object first. Internally, a join would be implemented pretty similarly to the filter.
We should probably revisit that part of #2156 in combination with this, to make sure that joins are parallelized sufficiently (though similar parallelization could also be applied to filter, so I think there's no deeper reason for why the join would be any faster than the filter).
I haven't thought this through much, but here's an alternative to the compose callback I suggested above which borrows concepts from the group term:
r.table("posts").join("id",r.table("comments"),{index: "postId"})// further operations are applied to joined documents.orderBy("date")// similar behavior to .ungroup().unjoin()
@marshall007 That's an interesting idea. Judging from our experience with group and ungroup, I think we should try to avoid this pattern though. The problem is that it's not immediately obvious what ungroup / unjoin do, since they don't change the result themselves. Instead they change how subsequent operations work. So this adds implicit state to the query, which can be hard to grasp for unexperienced users.
Activity
neumino commentedon Jul 18, 2014
That would be so good!
Could we have
include_missingdefault totruesincegroupdefaults totrue?Sometimes people want to do a 1-1 join. For example for a table
postsandauthors, you want to doIf there are more than one document, we can throw an error. That would avoid users (and me ^_^) to do things like that https://github.com/neumino/thinky/blob/master/lib/query.js#L152
If you set
multito true`, you still get back the left document just once right?neumino commentedon Jul 18, 2014
Err sorry I didn't see that it was not scheduled. Let's debate that once it is.
coffeemug commentedon Jul 18, 2014
That sounds like a good idea.
So I guess we should expand the spec. If
groupistrue, then if the user doesn't specify an index to the join (and therefore use the primary key from the right table), the joined field should include a single document, not an array. If they specify an index (and therefore useget_all), the joined field should include an array.We can add an optarg
enforce_oneor a more general oneenforce_maxor something like that. I don't really like that because it seems overly specific to me, but I'm not entirely opposed to it.coffeemug commentedon Jul 18, 2014
Err, sorry for getting suckered into commenting on this before it's time 😄
thelinuxlich commentedon Aug 7, 2014
+1
12 remaining items
neumino commentedon Sep 18, 2014
Here's my proposal:
https://gist.github.com/neumino/47b6fc616d9e884757a5
Here are the main reasons behind the proposal:
composeoption can look heavy, but it's from my point of view an order magnitude better than asking users to enter/exit scopes.What it doesn't handle
I couldn't come up with a nice syntax, but if someone can, it could be nice.
mlucy commentedon Sep 19, 2014
@coffeemug -- we didn't seem to come to a real consensus on this inside the discussion period. Should we punt on it until the next discussion period, or should we just pick something? (I think both of the proposals on the table are better than what we have now.)
marshall007 commentedon Sep 19, 2014
@neumino Your proposal looks really good, but I think the
composeandgroupoptions could be combined (plus I thinkcomposeis a better term for describing what yourgroupdoes).compose: undefined | string | function(doc, joined)If set to
undefinedjoin produces existing behavior. If set to a string it would group the right values into an array and assign the result to a property on the left document with the provided name. If provided a function, it will take two arguments: the left document and the joined documents.Examples for both use cases:
So in addition to combining the options, this allows you to arbitrarily compose the join result (instead of being restricted to the two
groupoptions).neumino commentedon Sep 19, 2014
I like the idea of binding both the sequence and the post.
That would actually make bi-directional relation work with multi indexes.
I'm a little less fan of having to call
mergeinside the compose function though. It means that you must find about merge to do a join, and one point of the newjoinshould be to make things simpler.I would first provide the sequence, then the parent document too (so
function(comments, post)in the example -- but that's probably because I would like to avoid the user to callpost.merge(...).coffeemug commentedon Sep 19, 2014
Since we're past the reql discussion period, I'd like to put this back in subsequent. We'll settle this during the next discussion period.
This command is fairly important, so I don't think we should just go with what we have so far. I'd like to discuss @neumino's proposal and the use cases it attempts to address in more detail.
geddski commentedon Feb 14, 2015
Any word on this?
danielmewes commentedon Feb 16, 2015
@geddski This got delayed because of some other more urgent features we're working on. Is there a particular aspect of this that you need?
coffeemug commentedon Feb 17, 2015
I originally wanted to try and schedule this for 2.1 (as it's reasonably easy to do, will clean up the join API a lot, add more functionality, and generally be less of a wart in ReQL).
marshall007 commentedon Jul 24, 2015
Would like to see this discussion brought up again soon after the 2.1 release. See #4566 which would be solved by the
include_missingoptarg. Really been looking forward togroupas well which would simplify a lot of code for us.danielmewes commentedon Jul 24, 2015
It's actually an interesting question whether an extended join command would be any faster than the options from #4566 with
filter+getand/or loading one side into an object first. Internally, a join would be implemented pretty similarly to thefilter.We should probably revisit that part of #2156 in combination with this, to make sure that joins are parallelized sufficiently (though similar parallelization could also be applied to
filter, so I think there's no deeper reason for why the join would be any faster than the filter).marshall007 commentedon Sep 22, 2015
I haven't thought this through much, but here's an alternative to the
composecallback I suggested above which borrows concepts from thegroupterm:danielmewes commentedon Sep 23, 2015
@marshall007 That's an interesting idea. Judging from our experience with
groupandungroup, I think we should try to avoid this pattern though. The problem is that it's not immediately obvious whatungroup/unjoindo, since they don't change the result themselves. Instead they change how subsequent operations work. So this adds implicit state to the query, which can be hard to grasp for unexperienced users.I think I'd prefer an explicit callback probably.