fix: Handle aggregate expressions in chart sorting#1389
Draft
sentry[bot] wants to merge 1 commit into
Draft
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR addresses the
ProgrammingError: (psycopg2.errors.UndefinedColumn) column "SUM(silt_achieved)" does not existthat occurred when attempting to sort charts by aggregate expressions (e.g.,SUM(silt_achieved)).Root Cause:
The
apply_chart_sorting()function inddpui/core/charts/charts_service.pywas incorrectly treating aggregate expressions provided in the sort configuration as raw column names. When SQLAlchemy processed these, it would quote them as literal identifiers (e.g.,"SUM(silt_achieved)"), which do not exist as actual columns in the database, leading to theUndefinedColumnerror.Solution:
apply_chart_sorting()function now includes logic to detect and parse aggregate-expression-style sort column strings (e.g.,SUM(column_name)) using a regular expression.payload.metricsto find a corresponding metric. If a match is found, the metric's alias is used for sorting.funcexpression (e.g.,func.sum(column('column_name'))) for theORDER BYclause. These expressions are directly appended toquery_builder.order_by_clauses, bypassing theorder_cols_bymethod's default behavior of quoting string column names.This ensures that aggregate expressions in sort configurations are correctly translated into valid SQL, resolving the
UndefinedColumnerror.Fixes DALGO-BACKEND-29P