Skip to content

fix(queries): preserve sort in exported CSVs, again - #8394

Merged
grantfitzsimmons merged 1 commit into
mainfrom
issue-5038-reup
Aug 3, 2026
Merged

fix(queries): preserve sort in exported CSVs, again#8394
grantfitzsimmons merged 1 commit into
mainfrom
issue-5038-reup

Conversation

@grantfitzsimmons

@grantfitzsimmons grantfitzsimmons commented Aug 3, 2026

Copy link
Copy Markdown
Member

Fixes #5038

This PR adds back the CSV export tool preserving the sort order of a query. This was lost when #7994 was merged into main a few days ago.

Checklist

  • Self-review the PR after opening it to make sure the changes look good and
    self-explanatory (or properly documented)
  • Add relevant issue to release milestone
  • Add automated tests

Testing instructions

  • Create a new query
  • Sort by any field
  • Click "Create CSV" and download it when it is finished
  • Verify that the exported CSV is sorted by the same column as in the query.
    • Note that any other column may not be sorted if the column you sorted by has duplicates (i.e. two agents with the first name "A")
  • Sort by multiple columns and ensure the left-most column is sorted first
    • after that, it should sort by the next column to the right, and so on

Screenshots

(sorting by taxon in descending order)
2026-06-08-1042

2026-06-08-1040

Summary by CodeRabbit

  • Bug Fixes
    • CSV exports now preserve the requested query result ordering.
    • Exported data consistently matches the ordering shown by the query.

Co-Authored-By: eos Shapland <149845916+g1rly-c0d3r@users.noreply.github.com>
@github-actions

github-actions Bot commented Aug 3, 2026

Copy link
Copy Markdown

Warning

One or more dependencies are approaching or past End-of-Life.
Please plan upgrades accordingly.

STATUS=WARNING
NODE_VERSION=20
NODE_CYCLE=20
EOL_DATE=2026-04-30
DAYS_REMAINING=-95

--- Node.js ---
Version: 20
EOL: 2026-04-30
Status: WARNING

STATUS=OK
PYTHON_VERSION=3.12
PYTHON_CYCLE=3.12
EOL_DATE=2028-10-31
DAYS_REMAINING=820

--- Python ---
Version: 3.12
EOL: 2028-10-31
Status: OK

STATUS=WARNING
DJANGO_VERSION=4.2
DJANGO_CYCLE=4.2
EOL_DATE=2026-04-07
DAYS_REMAINING=-118

--- Django ---
Version: 4.2
EOL: 2026-04-07
Status: WARNING


@grantfitzsimmons
grantfitzsimmons requested a review from a team August 3, 2026 19:52
@coderabbitai

coderabbitai Bot commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: 7b23de7d-66a1-4086-a552-8fed162fdf56

📥 Commits

Reviewing files that changed from the base of the PR and between f5911c1 and 4f21c88.

📒 Files selected for processing (1)
  • specifyweb/backend/stored_queries/execution.py

📝 Walkthrough

Walkthrough

query_to_csv now applies the ordering expressions returned by build_query before retrieving rows for CSV generation.

Changes

CSV Export Ordering

Layer / File(s) Summary
Apply query ordering
specifyweb/backend/stored_queries/execution.py
query_to_csv preserves the ordering expressions from build_query and applies them with order_by before retrieving rows.

Possibly related PRs

Suggested reviewers: g1rly-c0d3r, carolinedenis, melton-jason

🚥 Pre-merge checks | ✅ 5 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Automatic Tests ⚠️ Warning The patch changes only execution.py; no test files were added or modified, and existing CSV tests do not assert exported sort order. Add automated CSV regression tests for ascending, descending, and multi-column ordering, then run the stored-query test suite.
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the fix that preserves query sorting in CSV exports.
Linked Issues check ✅ Passed The change applies the ordering expressions from build_query before CSV generation, satisfying issue #5038.
Out of Scope Changes check ✅ Passed The changes are limited to restoring query sort order during CSV export and match the linked issue scope.
Testing Instructions ✅ Passed The steps exercise QueryBuilder CSV export and verify single- and multi-column ordering, including duplicate-value caveats; this matches query_to_csv applying build_query order expressions.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch issue-5038-reup

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@g1rly-c0d3r g1rly-c0d3r left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Testing instructions

  • Verify that the exported CSV is sorted by the same column as in the query.
  • Sort by multiple columns and ensure the left-most column is sorted first

Hey I know this one.

@rijulpoudel
rijulpoudel self-requested a review August 3, 2026 20:35

@rijulpoudel rijulpoudel left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

  • Verify that the exported CSV is sorted by the same column as in the query.
- Note that any other column may not be sorted if the column you sorted by has duplicates (i.e. two agents with the first name "A")
  • Sort by multiple columns and ensure the left-most column is sorted first
    • after that, it should sort by the next column to the right, and so on

Test successful. The sortings are respected in the exported CSV.

@emenslin emenslin left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

  • Verify that the exported CSV is sorted by the same column as in the query.
  • Sort by multiple columns and ensure the left-most column is sorted first

Looks good, sorting is respected!

@grantfitzsimmons
grantfitzsimmons merged commit 2289e56 into main Aug 3, 2026
24 checks passed
@grantfitzsimmons
grantfitzsimmons deleted the issue-5038-reup branch August 3, 2026 20:47
@github-project-automation github-project-automation Bot moved this from 📋Back Log to ✅Done in General Tester Board Aug 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: ✅Done

Development

Successfully merging this pull request may close these issues.

CSV export should respect order by / sorting

4 participants