Skip to content

Commit

Permalink
Fix submissions_status RPC when UNION ALL doesn't keep the order (#1201)
Browse files Browse the repository at this point in the history
UNION ALL is not guaranteed to keep the order of the results. In fact
with (at least) Postgres 13 the order is not well defined and can change
between executions.

This patch adds an extra column to the query, tagging each statistics
with its key, so that we can reconstruct the correct order of the
results.
  • Loading branch information
edomora97 authored Nov 6, 2024
1 parent d6d10fc commit 98ad9a2
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions cms/server/admin/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

import logging

from sqlalchemy import func, not_
from sqlalchemy import func, not_, literal_column

from cms import config, ServiceCoord, get_service_shards
from cms.db import SessionGen, Dataset, Submission, SubmissionResult, Task
Expand Down Expand Up @@ -176,13 +176,17 @@ def submissions_status(contest_id):
.filter(Task.contest_id == contest_id)
queries['total'] = total_query

stats = {}
# Add a "key" column for keeping track of each stats, in case they
# get shuffled.
for key, query in queries.items():
key_column = literal_column(f"'{key}'").label("key")
queries[key] = query.add_columns(key_column)

keys = list(queries.keys())
results = queries[keys[0]].union_all(
*(queries[key] for key in keys[1:])).all()

for i, k in enumerate(keys):
stats[k] = results[i][0]
stats = {key: value for value, key in results}
stats['compiling'] += 2 * stats['total'] - sum(stats.values())

return stats

0 comments on commit 98ad9a2

Please sign in to comment.