Skip to content

ReQL functions cannot be passed to higher-order funtions #1312

Open

Description

@AtnNn

You can do:

>>> r.expr([1,2]).reduce(lambda a, b: r.add(a,b)).run()
3

But not:

>>> r.expr([1,2]).reduce(r.add).run()
rethinkdb.errors.RqlCompileError

Activity

  1. ghost
    self-assigned this
    on Aug 17, 2013
  2. jdoliner commented on Aug 17, 2013

    @jdoliner
    Contributor

    I feel like this is covered under #1190.

  3. AtnNn commented on Aug 17, 2013

    @AtnNn
    MemberAuthor

    I think #1190 has a much bigger scope. We can achieve the behaviour described here with very minimal changes to the javascript and python driver. I'm not sure about ruby, because it does not have first class functions.

  4. wmrowan commented on Aug 19, 2013

    @wmrowan
    Contributor

    I'm going to take a look at this. This should only require changes to the drivers and could make affected queries a lot nicer.

  5. ghost
    self-assigned this
    on Aug 19, 2013
  6. wmrowan commented on Aug 19, 2013

    @wmrowan
    Contributor

    Looks like there is no reason in principle why r.expr([1,2]).reduce(r.add) shouldn't work out of the box in python. The error @atnnn get's when trying to do this relates to how we handle variatic arguments in the case of r.add etc.

    As the signature of r.add is def add(*args): python reports this function as taking 0 arguments when we inspect the code object. This prevents us from properly serializing the function.

    I was able to work around this by changing the signature to def add(one, two, *rest): which I also like better for aesthetic reasons since it correctly represents the fact that r.add takes two or more arguments.

    I'm going to implement this change regardless, but since this will enable sequence.reduce(r.add) I think we should highlight this better syntax by changing our documentation and examples to use it.

    I imagine that doing the same in the JS driver will involve a similar change plus a number unforeseeable additional difficulties as is always the case with the JS driver.

  7. jdoliner commented on Aug 19, 2013

    @jdoliner
    Contributor

    Could we have a reql proposal written out for this?

  8. coffeemug commented on Aug 19, 2013

    @coffeemug
    Contributor

    Seconding @jdoliner's request. I'd like for us to do this in the context of a ReQL proposal so all relevant parties can debate this as part of our regular process (because (a) there might be hidden unobvious gotchas, and (b) we need to make sure this is possible to do and is done in all drivers).

  9. wmrowan commented on Aug 19, 2013

    @wmrowan
    Contributor

    As far as I'm concerned, this issue is about a bug in the signature of r.add and friends. It just so happens that there is an unintended, undocumented feature that was blocked by this bug. I'm still going to fix this bug. Any subsequent proposal would then center on the question of documenting this latent functionality as an official feature.

  10. AtnNn commented on Aug 19, 2013

    @AtnNn
    MemberAuthor

    @wmrowan: instead of changing the signature of add, I was going to change reduce to always assume that the function it gets passed takes 2 arguments, and so on for other higher order functions.

    That way any variadic function that could be specialised to a two argument function will work.

  11. coffeemug commented on Aug 19, 2013

    @coffeemug
    Contributor

    @wmrowan @atnnn -- we still need to coordinate with @mlucy to make sure all drivers behave consistently with respect to this issue.

  12. wmrowan commented on Aug 19, 2013

    @wmrowan
    Contributor

    @atnnn I'd still like to change the function signature of r.add etc. We can discuss other modifications that may be necessary to support this feature in #1320 which I just opened for that discussion.

    @coffeemug This fix would actually change the behavior to be more consistent with the JS driver which has a minimum argument limit (though this is currently set to 1, we should probably change this to 2).

  13. wmrowan commented on Aug 19, 2013

    @wmrowan
    Contributor

    Actually, scratch that last statement. I was looking at the method version of r.add. The JS driver does enforce a 2 argument minimum for our binary functions. So yes, this change will make the python and JS drivers consistent.

  14. jdoliner commented on Aug 19, 2013

    @jdoliner
    Contributor

    Actually they're already inconsistent. r.add(1) returns an error in javascript and 1 in python.

  15. wmrowan commented on Aug 19, 2013

    @wmrowan
    Contributor

    Yes, they are currently inconsistent. This issue would make them consistent by changing the python behavior to match the JS behavior. I was just wrong about what the current JS behavior is.

  16. mlucy commented on Aug 19, 2013

    @mlucy
    Member

    We can mimic this syntax in Ruby with a small server change as long as we never have a variadic term that accepts 0 arguments.

  17. mlucy commented on Aug 19, 2013

    @mlucy
    Member

    I think we should definitely fix this in Python if that would normally be how that syntax works.

  18. mlucy commented on Aug 19, 2013

    @mlucy
    Member

    Also, can I close this in favor of #1320, or are they discussing subtly different things?

  19. wmrowan commented on Aug 19, 2013

    @wmrowan
    Contributor

    Yes, they are completely different at this point. This is now about the bug @atnnn found, not passing higher order functions.

  20. wmrowan commented on Aug 19, 2013

    @wmrowan
    Contributor

    Since there is confusion on this, I'm going to clarify what this issue is about and the potential conflict in driver vs. server behavior here.

    Currently the JS driver enforces a 2 argument minimum on each of our binary functions, i.e. r.add(1), r.eq(1), etc. produce an argument error ("expected 2 or more arguments, got 1") in the driver. The Python driver currently accepts any number of arguments (including 0 and 1). I don't know what the ruby driver does.

    The server enforces a 2 argument limit for some of our binary functions (I tested r.eq) but accepts 1 argument for others (I tested r.add). The argument checking behavior of the drivers and the server should all be consistent in these cases. At the very least, the python driver needs to change to enforce a minimum argument limit of either 1 or 2 in each of these cases (we should also check the ruby driver for this problem as well).

    The only open question in my mind is whether the drivers/server should enforce a 2 argument minimum for all these normally binary functions. Single argument add, mul, etc. are currently no-ops. These cases seem much more likely to be errors than intentionally useless behavior so I'd prefer to enforce 2 arguments. I believe we've argued this point before (I seem to recall #1184 being particularly nasty) so this might be old ground that I missed somehow. If we have already definitively settled on some binary functions accepting 1 argument (though a lack of polyglot tests for this seem to suggest an accidental provenance for this "feature") could I at least get a list so I can check that we are indeed consistent with whatever we decided before?

  21. mlucy commented on Aug 19, 2013

    @mlucy
    Member

    Currently r.sub(1) returns 1, which is really screwy. If nobody objects, I'll change the server to require 2 arguments.

  22. wmrowan commented on Aug 19, 2013

    @wmrowan
    Contributor

    That seems right to me. What about the others?

    A (semi)exhaustive search suggests that the following terms currently accept 1 argument and do nothing to it:

    • add
    • sub
    • mul
    • div

    Firstly, am I missing anything? Secondly, should we change any of the other three to require 2 arguments? I'd be in favor of doing it for all of them but I'm not familiar with the arguments in favor of unary add etc.

    Whatever we decide, the expected behavior should be encoded in polyglot tests.

  23. mlucy commented on Aug 19, 2013

    @mlucy
    Member

    I think we should make them all require 2 arguments. Re-assigning to myself; I'll fix the server and add arity tests.

  24. ghost
    self-assigned this
    on Aug 19, 2013
  25. jdoliner commented on Aug 19, 2013

    @jdoliner
    Contributor

    I'm okay with making these all require 2 arguments.

  26. wmrowan commented on Aug 19, 2013

    @wmrowan
    Contributor

    @mlucy I've pushed the required changes to the Python driver as branch 1312_binfunc. You can work off of that or merge it into your branch for this issue.

  27. thelinuxlich commented on Mar 12, 2018

    @thelinuxlich

    @srh isn't this already implemented?

  28. marshall007 commented on Mar 13, 2018

    @marshall007
    Contributor

    @thelinuxlich I don't believe so. In https://github.com/rethinkdb/rethinkdb/pull/5673 I had for the JS driver it was suggested that we implement it server-side instead, but I don't think anyone got around to it yet.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions