-
Notifications
You must be signed in to change notification settings - Fork 309
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to order by distance? #778
Comments
What is the function here that doesn't meet the syntax
|
In that case it's the operator |
For that, I think you can get away with a custom ArithmeticExpression from left = ... # custom functions # define query EDIT: ArithmeticExpression is used with enum which has a value attribute. A simple wrapper class can be used to have same attribute from pypika import CustomFunction, Field, Query
from pypika.terms import ArithmeticExpression
class NewOperation:
value: str = "<->"
ST_SetSRID = CustomFunction("ST_SetSRID", ["col", "value"])
ST_MakePoint = CustomFunction("ST_MakePoint", ["left", "right"])
left = ST_SetSRID(ST_MakePoint(Field("left"), Field("right")), 4326)
right = Field("point")
orderby = ArithmeticExpression(NewOperation, left, right)
query = Query.from_("house").select("id").orderby(orderby) |
That's exactly what's needed, thanks! It would be great if it was in the doc, so I opened a PR to add that: #779 |
Issue solved! |
I'm using distance operators in the ORDER BY, because those use geographic indexes better than their function equivalents:
Unfortunately, I don't seem to find a correct way to define that custom distance. It looks like defining a custom function assumes a prefix expression notation
function_name(param1, param2)
, when I'd like a postfix version.Has anyone covered a similar case? Thanks!
The text was updated successfully, but these errors were encountered: