Skip to content

fix: Prevent invalid SQL when sorting by non-grouped columns#1370

Draft
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/chart-sorting-grouping-error
Draft

fix: Prevent invalid SQL when sorting by non-grouped columns#1370
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/chart-sorting-grouping-error

Conversation

@sentry
Copy link
Copy Markdown

@sentry sentry Bot commented May 22, 2026

This PR addresses a ProgrammingError that occurs when users attempt to sort chart data by a column that is neither a selected metric nor a dimension in the aggregated query.

Problem:
The apply_chart_sorting function in charts_service.py would add any non-metric sort column directly to the ORDER BY clause of the generated SQL. If this column was not also present in the GROUP BY clause or used in an aggregate function, PostgreSQL would raise a GroupingError. For example, sorting by financial_label when only start_date was grouped would lead to invalid SQL like GROUP BY start_date ORDER BY financial_label.

Solution:
The apply_chart_sorting function has been updated to validate non-metric sort columns. Before adding a column to the ORDER BY clause, it now checks if the column is either a metric alias or one of the chart's normalized dimension columns. If the sort column does not match any of these, it is skipped, and a warning is logged. This ensures that only valid columns are included in the ORDER BY clause, preventing the generation of invalid SQL and resolving the GroupingError.

Fixes DALGO-BACKEND-27J

@sentry
Copy link
Copy Markdown
Author

sentry Bot commented May 22, 2026

Codecov Report

❌ Patch coverage is 0% with 6 lines in your changes missing coverage. Please review.
✅ Project coverage is 58.64%. Comparing base (75cb6fd) to head (c5697b0).

Files with missing lines Patch % Lines
ddpui/core/charts/charts_service.py 0.00% 6 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1370      +/-   ##
==========================================
- Coverage   58.66%   58.64%   -0.03%     
==========================================
  Files         132      132              
  Lines       15690    15696       +6     
==========================================
  Hits         9205     9205              
- Misses       6485     6491       +6     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants