Skip to content

ReQL proposal: modernizing joins #2720

Open

Description

@coffeemug

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.

Supersedes #1223, #1168, #1152, #2008.

Activity

  1. added this to the subsequent milestone on Jul 18, 2014
  2. modified the milestones: outdated, subsequent on Jul 18, 2014
  3. neumino commented on Jul 18, 2014

    @neumino
    Member

    That would be so good!

    Could we have include_missing default to true since group defaults to true?

    Sometimes people want to do a 1-1 join. For example for a table posts and authors, you want to do

    r.table("posts").join("authorId", r.table("authors").run(...)
    // will retrrieve
    {
       id: ... // (post's id)
      title: ...
      content: ...
      authorId: ...
      author: {
        // document from the table authors
      }
    }
    

    If 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 multi to true`, you still get back the left document just once right?

  4. neumino commented on Jul 18, 2014

    @neumino
    Member

    Err sorry I didn't see that it was not scheduled. Let's debate that once it is.

  5. coffeemug commented on Jul 18, 2014

    @coffeemug
    ContributorAuthor

    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.

  6. coffeemug commented on Jul 18, 2014

    @coffeemug
    ContributorAuthor

    Err, sorry for getting suckered into commenting on this before it's time 😄

  7. thelinuxlich commented on Aug 7, 2014

    @thelinuxlich

    +1

  8. modified the milestones: subsequent, on Aug 20, 2014
  9. 12 remaining items

  10. neumino commented on Sep 18, 2014

    @neumino
    Member

    Here's my proposal:
    https://gist.github.com/neumino/47b6fc616d9e884757a5

    Here are the main reasons behind the proposal:

    • Joined documents can be stored in a field
    • 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.
  11. mlucy commented on Sep 19, 2014

    @mlucy
    Member

    @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.)

  12. marshall007 commented on Sep 19, 2014

    @marshall007
    Contributor

    @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).

    compose: undefined | string | function(doc, joined)

    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" example
    r.table("posts").join("id", r.table("comments"), {
      index: "postId",
      compose: "comments"
    })
    
    // equivalent to @neumino's "group + compose" example
    r.table("posts").join("id", r.table("comments"), {
      index: "postId",
      compose: function (post, comments) {
        return post.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).

  13. neumino commented on Sep 19, 2014

    @neumino
    Member

    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(...).

  14. coffeemug commented on Sep 19, 2014

    @coffeemug
    ContributorAuthor

    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.

  15. modified the milestones: subsequent, on Sep 19, 2014
  16. geddski commented on Feb 14, 2015

    @geddski

    Any word on this?

  17. danielmewes commented on Feb 16, 2015

    @danielmewes
    Member

    @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?

  18. coffeemug commented on Feb 17, 2015

    @coffeemug
    ContributorAuthor

    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).

  19. marshall007 commented on Jul 24, 2015

    @marshall007
    Contributor

    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.

  20. danielmewes commented on Jul 24, 2015

    @danielmewes
    Member

    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).

  21. marshall007 commented on Sep 22, 2015

    @marshall007
    Contributor

    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()
  22. danielmewes commented on Sep 23, 2015

    @danielmewes
    Member

    @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.

    I think I'd prefer an explicit callback probably.

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