From f749ac7999ad455ca3ff53a55eb2d98da0aac148 Mon Sep 17 00:00:00 2001 From: Untone Date: Mon, 29 Jan 2024 00:42:03 +0300 Subject: [PATCH] scored-subquery-fix --- resolvers/reader.py | 72 +++++++++++++++------------------------------ 1 file changed, 24 insertions(+), 48 deletions(-) diff --git a/resolvers/reader.py b/resolvers/reader.py index a51ef6bf..b7816323 100644 --- a/resolvers/reader.py +++ b/resolvers/reader.py @@ -1,7 +1,7 @@ import logging from sqlalchemy import bindparam, distinct, literal_column, or_ -from sqlalchemy.orm import aliased, column_property, joinedload, selectinload +from sqlalchemy.orm import aliased, joinedload, selectinload from sqlalchemy.sql.expression import and_, asc, case, desc, func, nulls_last, select from starlette.exceptions import HTTPException @@ -82,9 +82,7 @@ async def get_shout(_, _info, slug=None, shout_id=None): 'rating': int(likes_stat or 0) - int(dislikes_stat or 0), } - for author_caption in ( - session.query(ShoutAuthor).join(Shout).where(Shout.slug == slug) - ): + for author_caption in session.query(ShoutAuthor).join(Shout).where(Shout.slug == slug): for author in shout.authors: if author.id == author_caption.author: author.caption = author_caption.caption @@ -105,9 +103,7 @@ async def get_shout(_, _info, slug=None, shout_id=None): shout.main_topic = main_topic[0] return shout except Exception: - raise HTTPException( - status_code=404, detail=f'shout {slug or shout_id} not found' - ) + raise HTTPException(status_code=404, detail=f'shout {slug or shout_id} not found') @query.field('load_shouts_by') @@ -153,9 +149,7 @@ async def load_shouts_by(_, _info, options): # order order_by = options.get('order_by', Shout.published_at) - query_order_by = ( - desc(order_by) if options.get('order_by_desc', True) else asc(order_by) - ) + query_order_by = desc(order_by) if options.get('order_by_desc', True) else asc(order_by) q = q.order_by(nulls_last(query_order_by)) # limit offset @@ -248,20 +242,15 @@ async def load_shouts_feed(_, info, options): with local_session() as session: reader = session.query(Author).filter(Author.user == user_id).first() if reader: - reader_followed_authors = select(AuthorFollower.author).where( - AuthorFollower.follower == reader.id - ) - reader_followed_topics = select(TopicFollower.topic).where( - TopicFollower.follower == reader.id - ) + reader_followed_authors = select(AuthorFollower.author).where(AuthorFollower.follower == reader.id) + reader_followed_topics = select(TopicFollower.topic).where(TopicFollower.follower == reader.id) subquery = ( select(Shout.id) .where(Shout.id == ShoutAuthor.shout) .where(Shout.id == ShoutTopic.shout) .where( - (ShoutAuthor.author.in_(reader_followed_authors)) - | (ShoutTopic.topic.in_(reader_followed_topics)) + (ShoutAuthor.author.in_(reader_followed_authors)) | (ShoutTopic.topic.in_(reader_followed_topics)) ) ) @@ -286,24 +275,15 @@ async def load_shouts_feed(_, info, options): order_by = options.get('order_by', Shout.published_at) - query_order_by = ( - desc(order_by) if options.get('order_by_desc', True) else asc(order_by) - ) + query_order_by = desc(order_by) if options.get('order_by_desc', True) else asc(order_by) offset = options.get('offset', 0) limit = options.get('limit', 10) - q = ( - q.group_by(Shout.id) - .order_by(nulls_last(query_order_by)) - .limit(limit) - .offset(offset) - ) + q = q.group_by(Shout.id).order_by(nulls_last(query_order_by)).limit(limit).offset(offset) # print(q.compile(compile_kwargs={"literal_binds": True})) - for [shout, reacted_stat, commented_stat, _last_comment] in session.execute( - q - ).unique(): + for [shout, reacted_stat, commented_stat, _last_comment] in session.execute(q).unique(): main_topic = ( session.query(Topic.slug) .join( @@ -336,13 +316,14 @@ async def load_shouts_search(_, _info, text, limit=50, offset=0): results_dict = {r['slug']: r for r in results} found_keys = list(results_dict.keys()) with local_session() as session: - # Define a virtual column 'score' using column_property - Shout.score = column_property( - literal_column(f"({results_dict.get(Shout.slug, {}).get('score', 0)})") - ) - - results = ( - session.query(Shout) + subquery = ( + select( + [ + Shout, + literal_column(f"({results_dict.get(Shout.slug, {}).get('score', 0)})").label('score'), + ] + ) + .select_from(Shout) .join(ShoutTopic, Shout.id == ShoutTopic.shout) .options( joinedload(Shout.authors), @@ -354,11 +335,10 @@ async def load_shouts_search(_, _info, text, limit=50, offset=0): Shout.slug.in_(found_keys), ) ) - .order_by(desc(Shout.score)) # Order by the virtual 'score' column - .limit(limit) - .offset(offset) - .all() - ) + ).alias('scored_shouts') + + # Use the subquery in the main query + results = session.query(subquery).order_by(desc(subquery.c.score)).limit(limit).offset(offset).all() logger.debug(f'search found {len(results)} results') @@ -380,9 +360,7 @@ async def load_shouts_unrated(_, info, limit: int = 50, offset: int = 0): and_( Reaction.shout == Shout.id, Reaction.replyTo.is_(None), - Reaction.kind.in_( - [ReactionKind.LIKE.value, ReactionKind.DISLIKE.value] - ), + Reaction.kind.in_([ReactionKind.LIKE.value, ReactionKind.DISLIKE.value]), ), ) .outerjoin(Author, Author.user == bindparam('user_id')) @@ -451,9 +429,7 @@ async def load_shouts_random_top(_, _info, options): aliased_reaction = aliased(Reaction) - subquery = ( - select(Shout.id).outerjoin(aliased_reaction).where(Shout.deleted_at.is_(None)) - ) + subquery = select(Shout.id).outerjoin(aliased_reaction).where(Shout.deleted_at.is_(None)) subquery = apply_filters(subquery, options.get('filters', {})) subquery = subquery.group_by(Shout.id).order_by(