Skip to content

Fix column mapping in GroupBy and aggregation queries to ensure corre… - #3750

Merged
Arpit Gupta (ar-guptaar) merged 5 commits into
Azure:mainfrom
ar-guptaar:dev/guptaar/fix-remapping-field-aggregation
Aug 5, 2026
Merged

Fix column mapping in GroupBy and aggregation queries to ensure corre…#3750
Arpit Gupta (ar-guptaar) merged 5 commits into
Azure:mainfrom
ar-guptaar:dev/guptaar/fix-remapping-field-aggregation

Conversation

@ar-guptaar

@ar-guptaar Arpit Gupta (ar-guptaar) commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

Why make this change?

There is a bug when running aggregation queries, wherein if a mapping backed column is present, the aggregation fails since it references the surfaced column and not the backed column.

This pull request improves how group-by queries with mapped (aliased) columns are handled in SQL generation and adds a regression test to ensure correct behavior. The main focus is to ensure that SQL queries reference the backing (database) column names in the SELECT and GROUP BY clauses, while projecting the correct exposed (mapped) names in the results.

What is this change?

  • Updated ProcessGroupByField and ProcessGroupByFieldSelections in SqlQueryStructure.cs to ensure that the backing (database) column name is used in the SQL query, while the exposed (mapped) field name is used as the label in the results. This resolves previous issues where the SELECT clause referenced the exposed name instead of the actual database column. [1] [2]

How was this tested?

  • Integration Tests
  • Unit Tests
  • Added a new test method TestSupportForGroupByAggregationWithMappedColumns in MsSqlGraphQLQueryTests.cs to verify that group-by fields and aggregations both resolve to the correct backing columns, and that results are projected under the mapped names. This prevents regressions on mapped column handling in group-by queries.
  • Added missing import for Azure.DataApiBuilder.Service.GraphQLBuilder.Queries in MsSqlGraphQLQueryTests.cs to support the new test.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI 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.

Pull request overview

This PR fixes SQL generation for GraphQL groupBy + aggregation queries when entity fields are mapped/aliased in runtime config, ensuring the generated SQL references backing (database) column names while projecting results under exposed (mapped) names. It also adds a regression test to prevent reintroducing the mapped-column failure scenario.

Changes:

  • Update SqlQueryStructure group-by column handling to use backing column names in SQL while keeping exposed names as result labels.
  • Add a new MsSql GraphQL regression test validating group-by + aggregation behavior with mapped columns.
  • Add a missing test import needed for accessing GraphQL query builder constants.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Service.Tests/SqlTests/GraphQLQueryTests/MsSqlGraphQLQueryTests.cs Adds a regression test for group-by aggregations on mapped columns (and required import).
src/Core/Resolvers/Sql Query Structures/SqlQueryStructure.cs Fixes column/label handling for group-by fields and selections to generate correct SQL for mapped columns.

Comment thread src/Core/Resolvers/Sql Query Structures/SqlQueryStructure.cs Outdated

Copilot AI 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.

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

Suppressed comments (2)

src/Service.Tests/SqlTests/GraphQLQueryTests/MsSqlGraphQLQueryTests.cs:1097

  • This test compares the full JSON response against a hard-coded array ordering, but the query does not specify orderBy. Without an ORDER BY, SQL Server can return GROUP BY rows in an arbitrary order, making this assertion potentially flaky. Add an explicit orderBy (ASC) to make the group ordering deterministic.
            string graphQLQuery = @"
    {
        gQLmappings {

src/Service.Tests/SqlTests/GraphQLQueryTests/MsSqlGraphQLQueryTests.cs:1062

  • These tests assert a specific order of grouped results (1,3,4,5) but the GraphQL query does not specify any ordering. Since the generated SQL for groupBy does not add an ORDER BY by default, result order is not guaranteed and this can make the test flaky across environments. Add an explicit orderBy so the response order is deterministic.

This issue also appears on line 1095 of the same file.

    {
        gQLmappings {
            groupBy(fields: [column1]) {

@Aniruddh25

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 6 pipeline(s).

@ar-guptaar

Copy link
Copy Markdown
Contributor Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 6 pipeline(s).

@ar-guptaar
Arpit Gupta (ar-guptaar) enabled auto-merge (squash) August 4, 2026 16:09
@aaronburtle

Copy link
Copy Markdown
Contributor

Looks like we are missing a regression test for DWSQL.

@aaronburtle aaronburtle 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.

Just a couple comments about testing, but look good!

@ar-guptaar
Arpit Gupta (ar-guptaar) merged commit 032eff9 into Azure:main Aug 5, 2026
14 checks passed
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.

4 participants