Skip to content
27 changes: 20 additions & 7 deletions src/sentry/replays/usecases/query/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,11 @@ def _query_using_scalar_strategy(

try:
where = handle_search_filters(scalar_search_config, search_filters)
orderby = handle_ordering(agg_sort_config, sort or "-" + DEFAULT_SORT_FIELD)
orderby = handle_ordering(
agg_sort_config,
sort or "-" + DEFAULT_SORT_FIELD,
tiebreaker="replay_id", # Ensure stable sort within the same score
)
except RetryAggregated:
return _query_using_aggregated_strategy(
search_filters,
Expand Down Expand Up @@ -378,7 +382,11 @@ def _query_using_aggregated_strategy(
period_start: datetime,
period_stop: datetime,
):
orderby = handle_ordering(agg_sort_config, sort or "-" + DEFAULT_SORT_FIELD)
orderby = handle_ordering(
agg_sort_config,
sort or "-" + DEFAULT_SORT_FIELD,
tiebreaker="replay_id", # Ensure stable sort within the same score
)

having: list[Condition] = handle_search_filters(agg_search_config, search_filters)
having.append(Condition(Function("min", parameters=[Column("segment_id")]), Op.EQ, 0))
Expand Down Expand Up @@ -459,11 +467,16 @@ def execute_query(query: Query, tenant_id: dict[str, int], referrer: str) -> Map
raise


def handle_ordering(config: dict[str, Expression], sort: str) -> list[OrderBy]:
if sort.startswith("-"):
return [OrderBy(_get_sort_column(config, sort[1:]), Direction.DESC)]
else:
return [OrderBy(_get_sort_column(config, sort), Direction.ASC)]
def handle_ordering(
config: dict[str, Expression], sort: str, tiebreaker: str | None = None
) -> list[OrderBy]:
direction = Direction.DESC if sort.startswith("-") else Direction.ASC
bare_sort = sort[1:] if sort.startswith("-") else sort
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could also use sort.lstrip("-")


orderby = [OrderBy(_get_sort_column(config, bare_sort), direction)]
if tiebreaker:
orderby.append(OrderBy(Column(tiebreaker), direction))
return orderby


def _get_sort_column(config: dict[str, Expression], column_name: str) -> Function:
Expand Down
Loading