Skip to content

ReQL proposal: document reference pseudotype #4853

Open

Description

@coffeemug

I'd like to propose adding a document reference pseudotype to the database to store foreign keys, and build some operations around it in the server and the drivers.

Suppose you have two tables authors and posts. Each post has a field author:

# insert a document into the `posts` table
> r.table('posts').insert({ 'text': 'foo', 'author': ref(db, table, uuid) })

I propose a follow_refs optarg to run:

> post = r.table('posts').get(post_uuid).run()
> post
{ 'text': 'foo', 'author': ref(uuid) }

> post['author']
ref(db, table, uuid)

# References are runnable on the driver
> post['author'].run()
{'name': 'foo', 'age': 'bar'}

# We can also follow references on the server
> r.table('posts').get(post_uuid).run(follow_refs=True)
{ 'text': 'foo', 'author': {'name': 'foo', 'age': 'bar'} }

# Similarly for deletes:
> r.table('posts').get(post_uuid).delete().run(follow_refs=True)
# deletes both the post and its author

The follow_refs optarg could also be an integer that specifies the depth the server will use to follow the refs. Presumably -1 will follow all refs (but what to do about cycles?)

This will make the user experience much better for a large class of use cases, and will also make Falcor integration much more compelling because "following" references is a fundamental assumption baked into Falcor.

Activity

  1. added this to the subsequent milestone on Sep 17, 2015
  2. coffeemug commented on Sep 17, 2015

    @coffeemug
    ContributorAuthor

    Also I just realized that there is an important question of what to do if the user says t.get(id).changes().run(follow_refs=True). We'd probably not support that at the beginning, but would eventually.

  3. marshall007 commented on Sep 17, 2015

    @marshall007
    Contributor

    I really like this proposal. Couple thoughts:

    1. What is the behavior of r.table('posts').get(post_id)('author')('id')? Does this load a document from the authors table or reference metadata from the ref field?
    2. Likewise, how would I create a secondary index on author.id in this example? Would r.row('author')('id') work?
    3. follow_refs should also be able to take a "selector" style argument like .pluck() and .withFields().
    4. Are references checked for existence on insert/update or would it still be possible to create a reference that doesn't actually exist?
  4. coffeemug commented on Sep 17, 2015

    @coffeemug
    ContributorAuthor

    @marshall007 -- all great questions; I haven't actually thought it through that far (someone suggested the idea and I wanted to write it down before I forget).

    Your suggestions are really cool though. Going into the reference straight on the server for instance (e.g. r.table('posts').get(post_id)('author')('id')) would be really, really, really frakkin' cool.

  5. coffeemug commented on Sep 17, 2015

    @coffeemug
    ContributorAuthor

    (Essentially, it's a shorthand syntax for joins, but it sounds about 1000x more convenient than joining explicitly)

  6. deontologician commented on Sep 17, 2015

    @deontologician
    Contributor

    This is less convenient than the implicit follow_refs expanding all references, but maybe there should be an explicit .deref method. So getting the author_id would be like:

    r.table('posts').get(post_id).deref('author')['id']
  7. marshall007 commented on Sep 18, 2015

    @marshall007
    Contributor

    @deontologician instead of introducing another term, I think that particular problem could be solved more generically for any pseudotype. Depending on the structure of the ref pseudotype, something like ref.info()('value')('id') (currently the value property of .info() always a string, but you get the idea).

    That said, I'd be more in favor of just magically optimizing the post('author')('id') case to lookup from the pseudotype value instead of doing the implicit join.

    Another thing to consider is how to represent one-to-many references. authors: [ ref(...), ref(...) ] would be a little verbose. Maybe ref(db, table, uuid1, ..., uuidN) with an argument style similar to getAll?

  8. coffeemug commented on Sep 18, 2015

    @coffeemug
    ContributorAuthor

    Man, this is going to be really cool.

  9. danielmewes commented on Sep 22, 2015

    @danielmewes
    Member

    This actually comes somewhat close to graph support. Pretty cool!

  10. marshall007 commented on Sep 26, 2015

    @marshall007
    Contributor

    Just thought of this: what if the reference pseudotype also supported remote resources? It might make sense as a completely different term, but I kinda like the idea of it being the same:

    { authors: ref(r.http('http://api.example.com/authors.json')) }

    Combine this with referencing joined fields transparently, and things get insanely awesome!

  11. marshall007 commented on Sep 26, 2015

    @marshall007
    Contributor

    This would also provide a great migration path for people transitioning r.binary <-> S3/remote files. You would just need to update your r.binary fields with refs or vice-versa and add the follow_refs optarg to any relevant queries.

  12. danielmewes commented on Sep 26, 2015

    @danielmewes
    Member

    @marshall007 Wow that would be cool! Should we just allow arbitrary ReQL functions in a reference maybe?
    I guess it might be a tricky in practice until we add an r.reql term & type that allows us to put chunks of ReQL code into a document (#1863).

  13. marshall007 commented on Sep 26, 2015

    @marshall007
    Contributor

    @danielmewes that's even better. If said ReQL expression gets evaluated with the current document set as context for r.row, that actually improves the r.http use-case. I can now have authors_url as it's own field that can be updated/queried against and my reference simply becomes r.http(r.row('authors_url')) for all rows. Likewise for traditional joins on ID fields in the original proposal.

  14. coffeemug commented on Sep 26, 2015

    @coffeemug
    ContributorAuthor

    Just thought of this:

    { authors: ref(r.http('http://api.example.com/authors.json')) }

    Should we just allow arbitrary ReQL functions in a reference maybe?

    Whoa, ya'll are blowing my mind. That is crazy cool.

  15. thelinuxlich commented on Sep 26, 2015

    @thelinuxlich

    Awesome proposal!

  16. mlucy commented on Sep 30, 2015

    @mlucy
    Member

    I like the idea of supporting functions instead of just table/ID pairs.

    One complication is that Slava's original proposal included the ability to call delete with follow_refs: true and have it delete the referenced documents. I'm not sure that this is a good idea because you might have multiple references to a document, some of which would be invalidated. But if we did go with this, then we'd have to restrict the set of functions that can be in references to the set of functions that return selections. (Alternatively, we could only make delete follow selections, but that sounds like it might be too subtle for people.)

    If references were functions that took no arguments, then people could store functions without getting the ability to recurse by building a combinator, which would limit the set of problems we have to worry about. (On the other hand, we could just bite the bullet and make functions first-class objects, and then have a recursion limit.)

    I also like the idea of references automatically resolving to thing they reference whenever you operate on them.


    On the technical side, enforcing existence of these foreign keys would be hard. We probably shouldn't do that for the first version, which maybe means we shouldn't call them foreign keys because people expect that behavior from foreign keys.

    Also, letting people use these foreign keys in sindex functions probably won't be possible since most of the time they'll be non-deterministic.

    We should probably make these references cache their value the first time they're dereferenced after being loading off disk, because otherwise a lot of our assumptions (like that you can compare two objects and always get the same result) would be violated. In general we'll have to think a bit about what it means that datum_ts can contain nondeterministic chunks of code that are lazily evaluated.

  17. danielmewes commented on Oct 1, 2015

    @danielmewes
    Member

    One thing to keep in mind is that if we ever want to add (even just limited/partial) support for referential integrity, having a less expressive reference format will actually make things easier.

    We're having a similar problem with secondary indexes. Because they can be any function, and we don't distinguish between "simple" secondary indexes and arbitrary ones, it is very difficult to automatically deduce anything about them. This makes things like automatically utilizing secondary indexes in e.g. a filter query in the context of an optimizer difficult.

    I generally really like the possibilities of arbitrary references, but I think we should think a bit more about what our eventual goals for references are.
    In some cases having a less expressive reference type might actually allow us to implement more features on top of it.

    Incidentally, some proposals discussed in #4598 (comment) are based on being able to represent a db, table, primary_key tuple. References would fit into there very nicely from an API standpoint, though that would only work with the simple type of references.

  18. marshall007 commented on Oct 1, 2015

    @marshall007
    Contributor

    @danielmewes I agree concerning the value in having a structured reference type and I think that's a pretty compelling reason to not allow arbitrary expressions in references. However, I don't see why that necessitates a less expressive syntax. .get, .getAll, and r.http are very easy terms to reason about and it seems like parsing their arguments into a structured pseudotype format would be relatively trivial (even client-side) if that's all that was supported.

    From an API standpoint, it's going to be much easier to remember how to use ref(table.get/getAll(...)) than a ref function that takes 2-n arguments. Plus, composability of expressions is one of the best parts of ReQL!

  19. danielmewes commented on Oct 1, 2015

    @danielmewes
    Member

    @marshall007 Yeah, I agree that the syntax you mention is a lot better than one that takes multiple fixed arguments.
    I like the idea of allowing an arbitrary ReQL query (or function?) to be passed into r.ref and then verifying that it falls into one of the categories of let's say get, getAll and r.http.
    This is of course only if we actually decide that restricting the set of allowed functions is worthwhile.

  20. danielmewes commented on Mar 24, 2016

    @danielmewes
    Member

    Also mentioning #4853 here, which has some older discussion on a similar proposal.

  21. deontologician commented on May 24, 2016

    @deontologician
    Contributor

    +1 again for this from the horizon side. Permissions rules would benefit from being able to refer to other documents https://discuss.horizon.io/t/how-to-handle-with-linked-related-associated-data/183

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