Skip to content

r.without returns strange results on recursion #1036

Closed

Description

@coffeemug

This query:

r.expr([{a:1}, [{b:2}, {a:3}]]).hasFields('a')

Returns:

[{"a":1},[{"b":2},{"a":3}]]

I think it should return

[{"a":1},[{"a":3}]]

Is the server wrong or is it me?

Activity

  1. ghost assigned on Jun 18, 2013
  2. mlucy commented on Jun 19, 2013

    @mlucy
    Member

    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:

    seq.has_fields('a')
    seq.filter{|x| x.has_fields('a')}

    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.

  3. mlucy commented on Jun 19, 2013

    @mlucy
    Member

    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.

  4. jdoliner commented on Jun 19, 2013

    @jdoliner
    Contributor

    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:

    seq.reduce(lambda x,y: {"foo": x["foo"] + y["foo"]})

    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:

    seq.has_fields("foo").reduce(lambda x, y: {"foo" : x["foo"] + y["foo"]})

    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.

  5. mlucy commented on Jun 19, 2013

    @mlucy
    Member

    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.

  6. jdoliner commented on Jun 19, 2013

    @jdoliner
    Contributor

    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
    .

  7. mlucy commented on Jun 19, 2013

    @mlucy
    Member

    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.

  8. coffeemug commented on Jun 19, 2013

    @coffeemug
    ContributorAuthor

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

  9. mlucy commented on Jun 19, 2013

    @mlucy
    Member

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

  10. coffeemug commented on Jun 19, 2013

    @coffeemug
    ContributorAuthor

    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?

  11. jdoliner commented on Jun 19, 2013

    @jdoliner
    Contributor

    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.

  12. mlucy commented on Jun 19, 2013

    @mlucy
    Member

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

  13. coffeemug commented on Jun 19, 2013

    @coffeemug
    ContributorAuthor

    Ah, I see, thanks.

  14. mlucy commented on Jun 19, 2013

    @mlucy
    Member

    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?

  15. coffeemug commented on Jun 19, 2013

    @coffeemug
    ContributorAuthor

    No objections on my part.

  16. jdoliner commented on Jun 19, 2013

    @jdoliner
    Contributor

    No objection, just make sure the note about better documentation is ported over.

  17. coffeemug commented on Jun 19, 2013

    @coffeemug
    ContributorAuthor

    I'm moving this to 1.7-optinal, as we'd probably change this behavior along with pluck if we were to do it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions