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
ReQL functions cannot be passed to higher-order funtions #1312
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.
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.
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).
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.
@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.
@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).
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.
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.
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?
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.
@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.
Activity
jdoliner commentedon Aug 17, 2013
I feel like this is covered under #1190.
AtnNn commentedon Aug 17, 2013
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.
wmrowan commentedon Aug 19, 2013
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.
wmrowan commentedon Aug 19, 2013
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 ofr.addetc.As the signature of
r.addisdef 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 thatr.addtakes 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.
jdoliner commentedon Aug 19, 2013
Could we have a reql proposal written out for this?
coffeemug commentedon Aug 19, 2013
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).
wmrowan commentedon Aug 19, 2013
As far as I'm concerned, this issue is about a bug in the signature of
r.addand 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.AtnNn commentedon Aug 19, 2013
@wmrowan: instead of changing the signature of
add, I was going to changereduceto 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.
coffeemug commentedon Aug 19, 2013
@wmrowan @atnnn -- we still need to coordinate with @mlucy to make sure all drivers behave consistently with respect to this issue.
wmrowan commentedon Aug 19, 2013
@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).
wmrowan commentedon Aug 19, 2013
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.jdoliner commentedon Aug 19, 2013
Actually they're already inconsistent.
r.add(1)returns an error in javascript and1in python.wmrowan commentedon Aug 19, 2013
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.
mlucy commentedon Aug 19, 2013
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.
mlucy commentedon Aug 19, 2013
I think we should definitely fix this in Python if that would normally be how that syntax works.
mlucy commentedon Aug 19, 2013
Also, can I close this in favor of #1320, or are they discussing subtly different things?
wmrowan commentedon Aug 19, 2013
Yes, they are completely different at this point. This is now about the bug @atnnn found, not passing higher order functions.
wmrowan commentedon Aug 19, 2013
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 testedr.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?
mlucy commentedon Aug 19, 2013
Currently
r.sub(1)returns1, which is really screwy. If nobody objects, I'll change the server to require 2 arguments.wmrowan commentedon Aug 19, 2013
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:
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.
mlucy commentedon Aug 19, 2013
I think we should make them all require 2 arguments. Re-assigning to myself; I'll fix the server and add arity tests.
jdoliner commentedon Aug 19, 2013
I'm okay with making these all require 2 arguments.
wmrowan commentedon Aug 19, 2013
@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.
thelinuxlich commentedon Mar 12, 2018
@srh isn't this already implemented?
marshall007 commentedon Mar 13, 2018
@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.