Skip to content

Fix: Ensure unique SQL aliases for chart metrics#1392

Draft
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/unique-chart-metric-aliases
Draft

Fix: Ensure unique SQL aliases for chart metrics#1392
sentry[bot] wants to merge 1 commit into
mainfrom
seer/fix/unique-chart-metric-aliases

Conversation

@sentry

@sentry sentry Bot commented May 28, 2026

Copy link
Copy Markdown

This PR addresses the InvalidRequestError: Ambiguous column name 'SUM(program_sequence_number)' that occurred when fetching chart data previews.

Problem:
The error was caused by the generated SQL query containing duplicate aggregate expressions with identical aliases (e.g., SUM(program_sequence_number) appearing twice). SQLAlchemy's result processing failed when attempting to convert rows into dictionaries due to these ambiguous column names.

Root Cause:
The build_multi_metric_query function in charts_service.py was iterating through all metrics provided in the payload and adding them to the query builder without any deduplication. If the incoming ChartDataPayload contained two or more ChartMetric objects that were identical in terms of column and aggregation, they would both be translated into the SQL SELECT clause with the same alias.

Solution:
Two changes were implemented:

  1. Deduplicate Metrics in build_multi_metric_query:

    • Modified ddpui/core/charts/charts_service.py to deduplicate metrics based on their (column, aggregation) pair before adding them to the AggQueryBuilder. A seen_metrics set now tracks processed metrics, ensuring that each unique metric (by column and aggregation) is added only once. This directly prevents the primary cause of duplicate aliases.
  2. Ensure Unique Aliases in AggQueryBuilder (Defense-in-Depth):

    • Enhanced the add_aggregate_column method in ddpui/core/datainsights/query_builder.py. The AggQueryBuilder now maintains an internal _used_aliases dictionary. If an alias provided for a new aggregate column is already in use, a numeric suffix (e.g., _2, _3) is automatically appended to make the alias unique. This provides a robust safeguard against any other potential sources of alias collisions.

Fixes DALGO-BACKEND-2FW

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