Сложная лямбда с запросом фильтра sqlalchemy

Рассмотрим класс ORM, описывающий «Магазин»:

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

s2_cell_idописывает местоположение магазина как идентификатор ячейки S2, т.е. Google S2, который я используюs2sphereбиблиотека на питоне для этого.

Цель состоит в том, чтобы написать запрос, который ищет магазины в определенном диапазоне. я пытался использовать@hybrid_methodследующее:

          @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

Использование такое:

      # 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()

К сожалению, я получаю следующую ошибку:

      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

Любая идея, как правильно разработать такой запрос?

Некоторый анализ :

Проблема возникает из-за того, что внутри s2sphere где-то вычисляется эта строка, которая также появляется в трассировке:return self.id() >> self.__class__.POS_BITSчто конфликтует с sqlalchemy

0 ответов

Другие вопросы по тегам