Skip to content

Add groupby/aggregation support for PostgreSQL in GraphQL - #3741

Merged
Arjun Narendra (ArjunNarendra) merged 21 commits into
Azure:mainfrom
ArjunNarendra:user/an/add-aggregation-support-to-postgresql
Aug 6, 2026
Merged

Add groupby/aggregation support for PostgreSQL in GraphQL#3741
Arjun Narendra (ArjunNarendra) merged 21 commits into
Azure:mainfrom
ArjunNarendra:user/an/add-aggregation-support-to-postgresql

Conversation

@ArjunNarendra

Copy link
Copy Markdown
Contributor

Why make this change?

Closes #2850. This PR adds support for group by and aggregation in PostgreSQL in GraphQL queries.

What is this change?

Groupby and aggregation types/enums were added to the GraphQL schema in PostgreSQL, but they essentially were orphaned because the user couldn't utilize them in their GraphQL query. This PR hooks up the groupby/aggregation schema types/enums into a GraphQL input query.

How was this tested?

  • Integration Tests

Sample Request(s)

query {
  stocks_prices {
    groupBy(fields: [categoryid, pieceid]) {
      fields {
        categoryid
        pieceid
      }
      aggregations {
        total: sum(field: price)
        count: count(field: pieceid, having: { gt: 5 })
      }
    }
  }
}

Previously, a query such as the one above would fail because the groupBy field would not have been attached to the stocks_prices object type. This PR adds the groupBy field to the relevant list query entity types. The groupBy field serves as a window to the rest of the groupBy/aggregation query.

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 enables end-to-end GraphQL groupBy + aggregation support for PostgreSQL by wiring Postgres into the GraphQL schema’s aggregation feature gate and updating the Postgres SQL query builder to emit aggregation columns and GROUP BY/HAVING clauses. It also re-enables the previously ignored PostgreSQL aggregation integration tests and updates the PostgreSQL test schema.

Changes:

  • Enable GraphQL aggregation support for DatabaseType.PostgreSQL in the schema builder.
  • Extend PostgresQueryBuilder SQL generation to include aggregation columns plus GROUP BY/HAVING/conditional ORDER BY.
  • Un-ignore and update PostgreSQL GraphQL aggregation tests; extend PostgreSQL test schema with date_only_table.

Reviewed changes

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

Show a summary per file
File Description
src/Service.GraphQLBuilder/Queries/QueryBuilder.cs Enables aggregation/groupBy schema features for PostgreSQL.
src/Core/Resolvers/PostgresQueryBuilder.cs Adds aggregation + group-by/having clause generation to Postgres SELECT queries.
src/Core/Resolvers/BaseTSqlQueryBuilder.cs Moves shared group-by/having/order-by/aggregation helpers out of the T-SQL-only base.
src/Core/Resolvers/BaseSqlQueryBuilder.cs Centralizes shared group-by/having/order-by/aggregation helper methods for all SQL builders.
src/Service.Tests/SqlTests/GraphQLQueryTests/PostgreSqlGraphQLQueryTests.cs Re-enables and updates PostgreSQL aggregation integration tests and expected SQL baselines.
src/Service.Tests/DatabaseSchema-PostgreSql.sql Adds/drop support for date_only_table test data.
Comments suppressed due to low confidence (2)

src/Service.Tests/SqlTests/GraphQLQueryTests/PostgreSqlGraphQLQueryTests.cs:633

  • This GROUP BY test’s expected SQL omits ORDER BY, but the assertion compares arrays by index. Postgres row order for GROUP BY without ORDER BY is not guaranteed, so the test can be flaky. Add ORDER BY on the group-by fields to make the expected result deterministic.
                    FROM stocks_price
                    GROUP BY categoryid, pieceid
                    HAVING SUM(price) > 50 AND COUNT(pieceid) <= 100
                ) AS table0";

src/Service.Tests/SqlTests/GraphQLQueryTests/PostgreSqlGraphQLQueryTests.cs:653

  • This GROUP BY expected SQL has no ORDER BY, but the test compares expected vs actual by position. Since Postgres doesn’t guarantee GROUP BY output order, add an ORDER BY on the group-by keys to avoid nondeterministic failures.
                    FROM stocks_price
                    GROUP BY categoryid, pieceid
                ) AS table0";

Comment thread src/Core/Resolvers/BaseSqlQueryBuilder.cs
Comment thread src/Core/Resolvers/PostgresQueryBuilder.cs
@ArjunNarendra Arjun Narendra (ArjunNarendra) changed the title User/an/add aggregation support to postgresql Add groupby/aggregation support for PostgreSQL in GraphQL Jul 31, 2026

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.

Approving, assuming the review comments would be addressed along with test coverages.

@ar-guptaar

Copy link
Copy Markdown
Contributor

There is a bug in group by feature wherein if there are mappings present on the columns, the GroupBy workflow does not honor the backed column name.
This impacts both MSSQL & PG workflows.
I have fixed it via the following PR : #3750. This has some regression tests added as well.
My suggestion would be to add some regression tests for PG code path as well (refer PR), that way we can be sure both the paths are working as expected.

CC: Souvik Ghosh (@souvikghosh04) Arjun Narendra (@ArjunNarendra)

@ArjunNarendra

Arjun Narendra (ArjunNarendra) commented Aug 5, 2026

Copy link
Copy Markdown
Contributor Author

There is a bug in group by feature wherein if there are mappings present on the columns, the GroupBy workflow does not honor the backed column name. This impacts both MSSQL & PG workflows. I have fixed it via the following PR : #3750. This has some regression tests added as well. My suggestion would be to add some regression tests for PG code path as well (refer PR), that way we can be sure both the paths are working as expected.

CC: Souvik Ghosh (@souvikghosh04) Arjun Narendra (@ArjunNarendra)

Thanks for catching this, Arpit Gupta (@ar-guptaar). I tested the bug and was able to reproduce it. The fix you propose makes sense. I will add some regression tests here.

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.

Please check the lastest comments

Comment thread src/Core/Resolvers/Sql Query Structures/SqlQueryStructure.cs
Comment thread src/Service.Tests/DatabaseSchema-MsSql.sql

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.

LGTM!

@ArjunNarendra
Arjun Narendra (ArjunNarendra) merged commit 7ad4f49 into Azure:main Aug 6, 2026
14 checks passed
@ArjunNarendra
Arjun Narendra (ArjunNarendra) deleted the user/an/add-aggregation-support-to-postgresql branch August 6, 2026 21:38
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.

[Enh]: Support Aggregations in read (PostgreSQL)

6 participants