Repository navigation
Fix DWSQL MCP grouped aggregate ordering scope - #3884
Open
Souvik Ghosh (souvikghosh04) with Copilot wants to merge 2 commits into
Open
Souvik Ghosh (souvikghosh04) with Copilot wants to merge 2 commits into
Souvik Ghosh (souvikghosh04) with Copilot wants to merge 2 commits into
Conversation
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
1 task done
Co-authored-by: souvikghosh04 <210500244+souvikghosh04@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Fix aggregates in order by clause for dwsql
Fix DWSQL MCP grouped aggregate ordering scope
Oct 28, 2026
Souvik Ghosh (souvikghosh04)
marked this pull request as ready for review
October 28, 2026 10:26
Contributor
There was a problem hiding this comment.
🟡 Changes recommended
The unordered outer STRING_AGG can violate the requested sort order and cause pagination to skip or repeat groups, requiring matching WITHIN GROUP ordering.
1 open issue
What changed in this PR
This PR addresses #3695 by moving MCP grouped aggregate ordering into the correct DWSQL query scope, avoiding references to raw columns outside the grouped derived table.
Changes:
- Uses aggregate aliases for ordering inside DWSQL’s
STRING_AGGderived table. - Places pagination in the same scope while retaining the existing
FOR JSON PATHhandling. - Adds query-generation tests covering aggregate functions, sort directions, distinct values, pagination, and both DWSQL query paths.
| File | Description |
|---|---|
| src/Service.Tests/Mcp/AggregateRecordsToolTests.cs | Adds DWSQL grouped ordering and pagination regression tests. |
| src/Azure.DataApiBuilder.Mcp/BuiltInTools/AggregateRecordsTool.cs | Adjusts ordering and pagination placement for DWSQL grouped queries. |
Review effort: Balanced
Comment on lines
+982
to
+983
| insertionIndex = sql.LastIndexOf($") AS {queryBuilder.QuoteIdentifier(structure.SourceAlias)}", StringComparison.Ordinal); | ||
| orderByClause = $" ORDER BY {queryBuilder.QuoteIdentifier(ComputeAlias(args.Function, args.Field))} {direction}"; |
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.

Why make this change?
MCP
aggregate_recordsappends numeric aggregate ordering outside DWSQL’s grouped derived table, where raw measure columns are no longer available. Fabric Warehouse rejects these queries withInvalid column name.What is this change?
ORDER BYinside the grouped query for DWSQL’sSTRING_AGGpath.OFFSET/FETCHin the same scope.How was this tested?
Sample Request(s)
{ "method": "tools/call", "params": { "name": "aggregate_records", "arguments": { "entity": "my_view", "function": "sum", "field": "amount", "groupby": ["category"] } } }The grouped query uses
ORDER BY [sum_amount] DESCbefore the derived-table boundary.