Skip to content

feat(Query Builder): Respect Sorting When Exporting Query to CSV (duplicate of #8168)#8167

Closed
g1rly-c0d3r wants to merge 2 commits into
specify:mainfrom
g1rly-c0d3r:issue-5038
Closed

feat(Query Builder): Respect Sorting When Exporting Query to CSV (duplicate of #8168)#8167
g1rly-c0d3r wants to merge 2 commits into
specify:mainfrom
g1rly-c0d3r:issue-5038

Conversation

@g1rly-c0d3r

@g1rly-c0d3r g1rly-c0d3r commented Jun 8, 2026

Copy link
Copy Markdown
Collaborator

Fixes #5038

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 export now honors query sort order so exported rows match displayed ordering.
    • Export processing now always receives a valid query object, preventing failures when special post-query handling does not require listing results.

@coderabbitai

coderabbitai Bot commented Jun 8, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

📝 Walkthrough

Walkthrough

This PR applies ordering expressions to the SQLAlchemy query used for CSV export and makes apply_special_post_query_processing return the query when list mode is disabled, ensuring CSV export receives a valid, ordered query.

Changes

CSV Export Ordering

Layer / File(s) Summary
Query ordering in CSV export and post-query processing
specifyweb/backend/stored_queries/execution.py
query_to_csv captures order_by expressions from build_query and applies them via query.order_by(*order_by_expers) before iterating results. apply_special_post_query_processing now returns the original query when should_list_query=False.

Suggested reviewers

  • emenslin
  • grantfitzsimmons
  • bhumikaguptaa
🚥 Pre-merge checks | ✅ 4 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Automatic Tests ⚠️ Warning PR modifies CSV export to respect query sorting but includes no automated tests for the order_by functionality, though comprehensive test infrastructure exists in test_query_to_csv.py. Add tests verifying CSV exports respect single and multi-column sorting with both ascending/descending orders to test_query_to_csv.py.
Testing Instructions ⚠️ Warning Manual testing instructions lack specificity—don't address NULL handling, distinct queries, or test data. No automated tests added despite existing test suite. Add automated tests covering CSV export sorting and clarify manual instructions with edge cases (NULL values, distinct mode, multi-column sorts).
✅ Passed checks (4 passed)
Check name Status Explanation
Linked Issues check ✅ Passed The code changes directly address issue #5038 by capturing order_by expressions from build_query and applying them before CSV export, ensuring exported CSVs respect the query's sorting.
Out of Scope Changes check ✅ Passed All changes in execution.py are focused on the single objective of respecting sorting when exporting to CSV; no unrelated modifications were introduced.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and specifically describes the main change: ensuring CSV exports respect query sorting, which directly matches the code changes that apply order_by expressions before CSV export.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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 and usage tips.

@g1rly-c0d3r g1rly-c0d3r changed the title feat(Query Builder) feat(Query Builder): Respect Sorting When Exporting Query to CSV Jun 8, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@specifyweb/backend/stored_queries/execution.py`:
- Around line 333-335: In query_to_csv, move the ordering so query =
query.order_by(*order_by_expers) is called before
apply_special_post_query_processing to avoid calling .order_by on a materialized
list; specifically, swap the two calls so that
apply_special_post_query_processing(query, tableid, field_specs, collection,
user, should_list_query=False) receives an already-ordered query, and note that
parent_inheritance_post_query_processing and
cog_inheritance_post_query_processing may return list(query) which makes the
original order unsafe.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro Plus

Run ID: e13395b2-b1ff-491c-bbb6-217c0d3d5b7f

📥 Commits

Reviewing files that changed from the base of the PR and between 457a590 and 9c09604.

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

Comment thread specifyweb/backend/stored_queries/execution.py Outdated
@github-project-automation github-project-automation Bot moved this from 📋Back Log to Dev Attention Needed in General Tester Board Jun 8, 2026
@CarolineDenis CarolineDenis added this to the 7.12.1 milestone Jun 8, 2026
@g1rly-c0d3r g1rly-c0d3r changed the title feat(Query Builder): Respect Sorting When Exporting Query to CSV feat(Query Builder): Respect Sorting When Exporting Query to CSV (duplicate of #8168) Jun 8, 2026
@g1rly-c0d3r g1rly-c0d3r closed this Jun 8, 2026
@github-project-automation github-project-automation Bot moved this from Dev Attention Needed to ✅Done in General Tester Board Jun 8, 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

2 participants