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
{{ message }}
Repository navigation
r.without returns strange results on recursion #1036
This is actually a combination of has_fields being polymorphic with respect to filter and the addition of promiscuous truthiness. The server is behaving as expected, but arguably the behavior is confusing and should change.
To clarify, we currently say that the following are equivalent if seq is a sequence:
So when evaluating [{:a => 1}, [{:b => 2}, {:a => 3}]], we end up doing a filter. r({:a => 1}).has_fields('a') returns true, so {:a => 1} is included in the output stream. r([{:b => 2}, {:a => 3}]).has_fields('a') returns [{:a => 3}], which evaluates to true due to promiscuous truthiness, so [{:b => 2}, {:a => 3}] is included in the output stream.
I think we should change this to have the behavior that slava expects, which is similar to what the other polymorphic functions (e.g. pluck) already do on nested arrays.
I kind of dislike both of these behaviors. I thought to main use case for has_fields was for cases where you wanted to run some operation over a sequence of objects that requires them to have a certain field. For example:
but this recursive behavior means that this no longer always solves your problems. I really like having has_fields("attr") give you the invariant that anything that remains in the sequence is guaranteed to return a non null value if you call ["attr"] on it.
I think it would be really confusing to have this behave differently from pluck and friends. It would also make with_fields not do what you want, since with_fields is just has_fields followed by pluck.
Note that has_fields doesn't strip out non-object non-array elements from the sequence, so it wouldn't solve your problems in any case. Its role is to take a sequence of objects and return a sequence of objects, not to take a sequence of arbitrary types and return a sequence of objects with all other types excluded. Making it recurse into subsequences makes more sense in that context than making it error, at least to me.
It just seems really confusing to me to call has_fields on a sequence and
then try to do something with the result and get an error that says "the
object doesn't have the field you just called has fields with".
On Jun 18, 2013 7:39 PM, "Michael Lucy" [email protected] wrote:
I think it would be really confusing to have this behave differently from
pluck and friends. It would also make with_fields not do what you want,
since with_fields is just has_fields followed by pluck.
Note that has_fields doesn't strip out non-object non-array elements from
the sequence, so it wouldn't solve your problems in any case. Its role is
to take a sequence of objects and return a sequence of objects, not to take
a sequence of arbitrary types and return a sequence of objects with all
other types excluded. Making it recurse into subsequences makes more sense
in that context than making it error, at least to me.
—
Reply to this email directly or view it on GitHubhttps://github.com//issues/1036#issuecomment-19659329
.
Well, if you try to e.g. map getatttr over the resulting sequence the actual error you'll get is RethinkDB::RqlRuntimeError: Expected type OBJECT but found ARRAY., which I think is the same error you'd get if we made has_fields error here.
So, I agree with @jdoliner, and as I mentioned in #889 (comment) with @neumino. I think it would be better to take the recursive polymorphic behavior out in all commands. It's a neat trick, but after thinking about it a bit it seems less useful, and doesn't seem very relevant once we implement static value sugar for dealing with nested elements (including arrays).
@coffeemug -- so we would make has_fields, pluck, with_fields, etc. only go one level deep? I'm not necessarily opposed to that, as long as we make a consistent choice for all of them.
The main problem I have with not recursing is that it sort of complicates the evaluation model (both on the server and in people's heads). You can no longer say that seq.pluck(...) <=> seq.map{|x| x.pluck(...)}. That might be OK, though.
so we would make has_fields, pluck, with_fields, etc. only go one level deep?
This is what I'm proposing, yes. (I'm not unequivocally saying that we should, just that it seems like a good idea to me and that we should discuss it and see if my intuition holds up).
You can no longer say that seq.pluck(...) <=> seq.map{|x| x.pluck(...)}
I'm not sure I understand -- why is it that you can no longer say that?
Also as a side note here our docs on has_fields are kind of lacking. They don't even mention that it can be used on sequences which I think is a really nice feature of it that's worth showcasing. Whoever fixes this issue should fix the docs as well.
@coffeemug -- you couldn't say that because if we did this and you have x = [[{:a => 1}]] then x.pluck('a') will throw an error while x.map{|y| y.pluck('a')} won't.
@jdoliner -- I concur; that was probably my fault.
Alright, I think I buy that recursing more than one level deep is a bad idea. Would anyone object to closing this issue and opening a new RQL proposal to limit the recursion depth for all the object/sequence polymorphic terms?
Activity
mlucy commentedon Jun 19, 2013
This is actually a combination of
has_fieldsbeing polymorphic with respect to filter and the addition of promiscuous truthiness. The server is behaving as expected, but arguably the behavior is confusing and should change.To clarify, we currently say that the following are equivalent if
seqis a sequence:So when evaluating
[{:a => 1}, [{:b => 2}, {:a => 3}]], we end up doing a filter.r({:a => 1}).has_fields('a')returnstrue, so{:a => 1}is included in the output stream.r([{:b => 2}, {:a => 3}]).has_fields('a')returns[{:a => 3}], which evaluates totruedue to promiscuous truthiness, so[{:b => 2}, {:a => 3}]is included in the output stream.mlucy commentedon Jun 19, 2013
I think we should change this to have the behavior that slava expects, which is similar to what the other polymorphic functions (e.g.
pluck) already do on nested arrays.jdoliner commentedon Jun 19, 2013
I kind of dislike both of these behaviors. I thought to main use case for
has_fieldswas for cases where you wanted to run some operation over a sequence of objects that requires them to have a certain field. For example:might be failing because some elements of the sequence don't have a field with key "foo" in them so you would replace it with:
but this recursive behavior means that this no longer always solves your problems. I really like having
has_fields("attr")give you the invariant that anything that remains in the sequence is guaranteed to return a nonnullvalue if you call["attr"]on it.mlucy commentedon Jun 19, 2013
I think it would be really confusing to have this behave differently from
pluckand friends. It would also makewith_fieldsnot do what you want, sincewith_fieldsis justhas_fieldsfollowed bypluck.Note that
has_fieldsdoesn't strip out non-object non-array elements from the sequence, so it wouldn't solve your problems in any case. Its role is to take a sequence of objects and return a sequence of objects, not to take a sequence of arbitrary types and return a sequence of objects with all other types excluded. Making it recurse into subsequences makes more sense in that context than making it error, at least to me.jdoliner commentedon Jun 19, 2013
It just seems really confusing to me to call has_fields on a sequence and
then try to do something with the result and get an error that says "the
object doesn't have the field you just called has fields with".
On Jun 18, 2013 7:39 PM, "Michael Lucy" [email protected] wrote:
mlucy commentedon Jun 19, 2013
Well, if you try to e.g. map
getatttrover the resulting sequence the actual error you'll get isRethinkDB::RqlRuntimeError: Expected type OBJECT but found ARRAY., which I think is the same error you'd get if we madehas_fieldserror here.coffeemug commentedon Jun 19, 2013
So, I agree with @jdoliner, and as I mentioned in #889 (comment) with @neumino. I think it would be better to take the recursive polymorphic behavior out in all commands. It's a neat trick, but after thinking about it a bit it seems less useful, and doesn't seem very relevant once we implement static value sugar for dealing with nested elements (including arrays).
mlucy commentedon Jun 19, 2013
@coffeemug -- so we would make
has_fields,pluck,with_fields, etc. only go one level deep? I'm not necessarily opposed to that, as long as we make a consistent choice for all of them.The main problem I have with not recursing is that it sort of complicates the evaluation model (both on the server and in people's heads). You can no longer say that
seq.pluck(...) <=> seq.map{|x| x.pluck(...)}. That might be OK, though.coffeemug commentedon Jun 19, 2013
This is what I'm proposing, yes. (I'm not unequivocally saying that we should, just that it seems like a good idea to me and that we should discuss it and see if my intuition holds up).
I'm not sure I understand -- why is it that you can no longer say that?
jdoliner commentedon Jun 19, 2013
Also as a side note here our docs on
has_fieldsare kind of lacking. They don't even mention that it can be used on sequences which I think is a really nice feature of it that's worth showcasing. Whoever fixes this issue should fix the docs as well.mlucy commentedon Jun 19, 2013
@coffeemug -- you couldn't say that because if we did this and you have
x = [[{:a => 1}]]thenx.pluck('a')will throw an error whilex.map{|y| y.pluck('a')}won't.@jdoliner -- I concur; that was probably my fault.
coffeemug commentedon Jun 19, 2013
Ah, I see, thanks.
mlucy commentedon Jun 19, 2013
Alright, I think I buy that recursing more than one level deep is a bad idea. Would anyone object to closing this issue and opening a new RQL proposal to limit the recursion depth for all the object/sequence polymorphic terms?
coffeemug commentedon Jun 19, 2013
No objections on my part.
jdoliner commentedon Jun 19, 2013
No objection, just make sure the note about better documentation is ported over.
coffeemug commentedon Jun 19, 2013
I'm moving this to 1.7-optinal, as we'd probably change this behavior along with
pluckif we were to do it.