Complex lambda with sqlalchemy filter query

218 views Asked by At

Consider an ORM class describing a "Store":

class Store(Base, AbstractSchema):
    __tablename__ = 'store'
    id = Column(Integer, primary_key=True)
    ...
    s2_cell_id       = Column(BigInteger,  unique=False, nullable=True)

s2_cell_id describes the location of a store as an id of S2 cell i.e Google S2 I am using s2sphere library in python for that.

Goal is to write a query that searches for stores in a certain range. I tried to use @hybrid_method as follows:

    @hybrid_method
    def lies_inside(self, cells : list[Cell]):
        for c in cells:
            is_inside = c.contains(Cell(CellId(self.s2_cell_id)))
            if is_inside : return True
        return False

    @lies_inside.expression
    def lies_inside(cls, cells: list[Cell]):
        for c in cells:
            is_inside = c.contains(Cell(CellId(cls.s2_cell_id)))
            if is_inside : return True
        return False

Usage is like this:

# Compute the set of cell that intersect the search area
candidates = router.location_manager.get_covering_cells(lat, lng, r, 13) 

# Query the database for stores whose cell IDs are in the set of intersecting cells
query = db.query(Store).filter(Store.lies_inside(candidates)).all()

I get the following error unfortunately:

Traceback (most recent call last):
  File "C:\Users\FARO-User\Anaconda3\envs\woher-backend\lib\site-packages\uvicorn\protocols\http\httptools_impl.py", line 419, in run_asgi
    result = await app(  # type: ignore[func-returns-value]
  File "C:\Users\FARO-User\Anaconda3\envs\woher-backend\lib\site-packages\uvicorn\middleware\proxy_headers.py", line 78, in __call__
    return await self.app(scope, receive, send)
  File "C:\Users\FARO-User\Anaconda3\envs\woher-backend\lib\site-packages\fastapi\applications.py", line 270, in __call__
    await super().__call__(scope, receive, send)
  File "C:\Users\FARO-User\Anaconda3\envs\woher-backend\lib\site-packages\starlette\applications.py", line 124, in __call__
    await self.middleware_stack(scope, receive, send)
  File "C:\Users\FARO-User\Anaconda3\envs\woher-backend\lib\site-packages\starlette\middleware\errors.py", line 184, in __call__
    raise exc
  File "C:\Users\FARO-User\Anaconda3\envs\woher-backend\lib\site-packages\starlette\middleware\errors.py", line 162, in __call__
    await self.app(scope, receive, _send)
  File "C:\Users\FARO-User\Anaconda3\envs\woher-backend\lib\site-packages\starlette\middleware\exceptions.py", line 79, in __call__
    raise exc
  File "C:\Users\FARO-User\Anaconda3\envs\woher-backend\lib\site-packages\starlette\middleware\exceptions.py", line 68, in __call__
    await self.app(scope, receive, sender)
  File "C:\Users\FARO-User\Anaconda3\envs\woher-backend\lib\site-packages\fastapi\middleware\asyncexitstack.py", line 21, in __call__
    raise e
  File "C:\Users\FARO-User\Anaconda3\envs\woher-backend\lib\site-packages\fastapi\middleware\asyncexitstack.py", line 18, in __call__
    await self.app(scope, receive, send)
  File "C:\Users\FARO-User\Anaconda3\envs\woher-backend\lib\site-packages\starlette\routing.py", line 706, in __call__
    await route.handle(scope, receive, send)
  File "C:\Users\FARO-User\Anaconda3\envs\woher-backend\lib\site-packages\starlette\routing.py", line 276, in handle
    await self.app(scope, receive, send)
  File "C:\Users\FARO-User\Anaconda3\envs\woher-backend\lib\site-packages\starlette\routing.py", line 66, in app
    response = await func(request)
  File "C:\Users\FARO-User\Anaconda3\envs\woher-backend\lib\site-packages\fastapi\routing.py", line 235, in app
    raw_response = await run_endpoint_function(
  File "C:\Users\FARO-User\Anaconda3\envs\woher-backend\lib\site-packages\fastapi\routing.py", line 163, in run_endpoint_function
    return await run_in_threadpool(dependant.call, **values)
  File "C:\Users\FARO-User\Anaconda3\envs\woher-backend\lib\site-packages\starlette\concurrency.py", line 41, in run_in_threadpool
    return await anyio.to_thread.run_sync(func, *args)
  File "C:\Users\FARO-User\Anaconda3\envs\woher-backend\lib\site-packages\anyio\to_thread.py", line 31, in run_sync
    return await get_asynclib().run_sync_in_worker_thread(
  File "C:\Users\FARO-User\Anaconda3\envs\woher-backend\lib\site-packages\anyio\_backends\_asyncio.py", line 937, in run_sync_in_worker_thread
    return await future
  File "C:\Users\FARO-User\Anaconda3\envs\woher-backend\lib\site-packages\anyio\_backends\_asyncio.py", line 867, in run
    result = context.run(func, *args)
  File "C:\Users\FARO-User\Desktop\personal\dev\repos\woher-backend\.\src\routers\search.py", line 33, in get_nearby_stores
    query = db.query(Store).filter(Store.lies_inside(candidates)).all()
  File "c:\users\faro-user\desktop\personal\dev\repos\woher-backend\src\sql\models\store.py", line 55, in lies_inside
    is_inside = c.contains(Cell(CellId(cls.s2_cell_id)))
  File "C:\Users\FARO-User\Anaconda3\envs\woher-backend\lib\site-packages\s2sphere\sphere.py", line 2354, in __init__
    face, i, j, orientation = cell_id.to_face_ij_orientation()
  File "C:\Users\FARO-User\Anaconda3\envs\woher-backend\lib\site-packages\s2sphere\sphere.py", line 1298, in to_face_ij_orientation
    face = self.face()
  File "C:\Users\FARO-User\Anaconda3\envs\woher-backend\lib\site-packages\s2sphere\sphere.py", line 1057, in face
    return self.id() >> self.__class__.POS_BITS
  File "C:\Users\FARO-User\Anaconda3\envs\woher-backend\lib\site-packages\sqlalchemy\sql\operators.py", line 458, in __rshift__
    return self.operate(rshift, other)
  File "C:\Users\FARO-User\Anaconda3\envs\woher-backend\lib\site-packages\sqlalchemy\sql\elements.py", line 868, in operate
    return op(self.comparator, *other, **kwargs)
  File "C:\Users\FARO-User\Anaconda3\envs\woher-backend\lib\site-packages\sqlalchemy\sql\operators.py", line 458, in __rshift__
    return self.operate(rshift, other)
  File "C:\Users\FARO-User\Anaconda3\envs\woher-backend\lib\site-packages\sqlalchemy\sql\type_api.py", line 77, in operate
    return o[0](self.expr, op, *(other + o[1:]), **kwargs)
  File "C:\Users\FARO-User\Anaconda3\envs\woher-backend\lib\site-packages\sqlalchemy\sql\default_comparator.py", line 181, in _unsupported_impl
    raise NotImplementedError(
NotImplementedError: Operator 'rshift' is not supported on this expression

Any idea how to design such query appropriately?

Some Analysis:

The issue occurs because internally in s2sphere, this line is computed somewhere which appears in the traceback as well: return self.id() >> self.__class__.POS_BITS which clashes with sqlalchemy

0

There are 0 answers