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
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.
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.
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?
Likewise, how would I create a secondary index on author.id in this example? Would r.row('author')('id') work?
follow_refs should also be able to take a "selector" style argument like .pluck() and .withFields().
Are references checked for existence on insert/update or would it still be possible to create a reference that doesn't actually exist?
@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.
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:
@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?
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:
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.
@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).
@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.
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.
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.
@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!
@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.
Activity
coffeemug commentedon Sep 17, 2015
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.marshall007 commentedon Sep 17, 2015
I really like this proposal. Couple thoughts:
r.table('posts').get(post_id)('author')('id')? Does this load a document from theauthorstable or reference metadata from the ref field?author.idin this example? Wouldr.row('author')('id')work?follow_refsshould also be able to take a "selector" style argument like.pluck()and.withFields().coffeemug commentedon Sep 17, 2015
@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.coffeemug commentedon Sep 17, 2015
(Essentially, it's a shorthand syntax for joins, but it sounds about 1000x more convenient than joining explicitly)
deontologician commentedon Sep 17, 2015
This is less convenient than the implicit
follow_refsexpanding all references, but maybe there should be an explicit.derefmethod. So getting the author_id would be like:marshall007 commentedon Sep 18, 2015
@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
refpseudotype, something likeref.info()('value')('id')(currently thevalueproperty 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. Mayberef(db, table, uuid1, ..., uuidN)with an argument style similar togetAll?coffeemug commentedon Sep 18, 2015
Man, this is going to be really cool.
danielmewes commentedon Sep 22, 2015
This actually comes somewhat close to graph support. Pretty cool!
marshall007 commentedon Sep 26, 2015
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:
Combine this with referencing joined fields transparently, and things get insanely awesome!
marshall007 commentedon Sep 26, 2015
This would also provide a great migration path for people transitioning
r.binary <-> S3/remote files. You would just need to update yourr.binaryfields withrefsor vice-versa and add thefollow_refsoptarg to any relevant queries.danielmewes commentedon Sep 26, 2015
@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.reqlterm & type that allows us to put chunks of ReQL code into a document (#1863).marshall007 commentedon Sep 26, 2015
@danielmewes that's even better. If said ReQL expression gets evaluated with the current document set as context for
r.row, that actually improves ther.httpuse-case. I can now haveauthors_urlas it's own field that can be updated/queried against and my reference simply becomesr.http(r.row('authors_url'))for all rows. Likewise for traditional joins on ID fields in the original proposal.coffeemug commentedon Sep 26, 2015
Whoa, ya'll are blowing my mind. That is crazy cool.
thelinuxlich commentedon Sep 26, 2015
Awesome proposal!
mlucy commentedon Sep 30, 2015
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
deletewithfollow_refs: trueand 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 makedeletefollow 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.danielmewes commentedon Oct 1, 2015
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
filterquery 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_keytuple. References would fit into there very nicely from an API standpoint, though that would only work with the simple type of references.marshall007 commentedon Oct 1, 2015
@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, andr.httpare 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 areffunction that takes 2-n arguments. Plus, composability of expressions is one of the best parts of ReQL!danielmewes commentedon Oct 1, 2015
@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.refand then verifying that it falls into one of the categories of let's sayget,getAllandr.http.This is of course only if we actually decide that restricting the set of allowed functions is worthwhile.
danielmewes commentedon Mar 24, 2016
Also mentioning #4853 here, which has some older discussion on a similar proposal.
deontologician commentedon May 24, 2016
+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