Skip to content

fix: resolve aggregate column names for chart sorting#1395

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

fix: resolve aggregate column names for chart sorting#1395
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/chart-sorting-agg-column

Conversation

@sentry

@sentry sentry Bot commented May 29, 2026

Copy link
Copy Markdown

This PR addresses a ProgrammingError: (psycopg2.errors.UndefinedColumn) that occurred when sorting charts by aggregated metrics.

Problem:
When the frontend sent a sort column name as an aggregation expression (e.g., AVG(total_revenue)), the backend's apply_chart_sorting function failed to resolve it to the correct SQL alias (e.g., A) defined in the SELECT clause. This happened because apply_chart_sorting only checked against metric.alias and not the aggregation expression itself. Consequently, the raw AVG(total_revenue) string was passed to the query builder, which then generated an ORDER BY "AVG(total_revenue)" clause. PostgreSQL rejected this as an undefined column because "AVG(total_revenue)" was treated as a literal column name, not a reference to the aggregated value aliased as "A".

Solution:
Modified ddpui/core/charts/charts_service.py in the apply_chart_sorting function. A fallback mechanism has been added: if the sort column name does not match any metric.alias, the code now attempts a case-insensitive match against the aggregation expression pattern f"{metric.aggregation}({metric.column})". Once a matching metric is found through this fallback, the existing logic correctly generates the SQL alias (e.g., A) for the ORDER BY clause, resolving the UndefinedColumn error.

Fixes DALGO-BACKEND-26D

@sentry

sentry Bot commented May 29, 2026

Copy link
Copy Markdown
Author

Codecov Report

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

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

☔ 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