-
Notifications
You must be signed in to change notification settings - Fork 324
[MCP] Bug-fix- aggregate_records always requires groupby #3294
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -171,8 +171,6 @@ public async Task AggregateRecords_InvalidFieldFunctionCombination_ReturnsInvali | |
| #region Input Validation Tests - GroupBy Dependencies | ||
|
|
||
| [DataTestMethod] | ||
| [DataRow("{\"entity\": \"Book\", \"function\": \"count\", \"field\": \"*\", \"orderby\": \"desc\"}", "groupby", | ||
| DisplayName = "Orderby without groupby")] | ||
| [DataRow("{\"entity\": \"Book\", \"function\": \"count\", \"field\": \"*\", \"having\": {\"gt\": 5}}", "groupby", | ||
| DisplayName = "Having without groupby")] | ||
| [DataRow("{\"entity\": \"Book\", \"function\": \"count\", \"field\": \"*\", \"groupby\": [\"title\"], \"orderby\": \"ascending\"}", "'asc' or 'desc'", | ||
|
|
@@ -190,6 +188,126 @@ public async Task AggregateRecords_GroupByDependencyViolation_ReturnsInvalidArgu | |
|
|
||
| #endregion | ||
|
|
||
| #region Input Validation Tests - Orderby Without Groupby (Issue #3279) | ||
|
|
||
| /// <summary> | ||
| /// Verifies that orderby without groupby is silently ignored rather than rejected. | ||
| /// This is the core fix for https://github.com/Azure/data-api-builder/issues/3279. | ||
| /// The tool should pass input validation and only fail at metadata resolution (no real DB). | ||
| /// </summary> | ||
| [DataTestMethod] | ||
| [DataRow("{\"entity\": \"Book\", \"function\": \"count\", \"field\": \"*\", \"orderby\": \"desc\"}", | ||
| DisplayName = "count(*) with orderby desc, no groupby")] | ||
| [DataRow("{\"entity\": \"Book\", \"function\": \"count\", \"field\": \"*\", \"orderby\": \"asc\"}", | ||
| DisplayName = "count(*) with orderby asc, no groupby")] | ||
| [DataRow("{\"entity\": \"Book\", \"function\": \"avg\", \"field\": \"price\", \"orderby\": \"desc\"}", | ||
| DisplayName = "avg(price) with orderby desc, no groupby")] | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need to change the aggregation function - avg, sum to test the same input validation as done for count Essentially, avg and sum tests are redundant, can be removed |
||
| [DataRow("{\"entity\": \"Book\", \"function\": \"sum\", \"field\": \"price\", \"orderby\": \"asc\"}", | ||
| DisplayName = "sum(price) with orderby asc, no groupby")] | ||
| [DataRow("{\"entity\": \"Book\", \"function\": \"count\", \"field\": \"*\", \"orderby\": \"ascending\"}", | ||
| DisplayName = "Invalid orderby value without groupby is silently ignored")] | ||
| [DataRow("{\"entity\": \"Book\", \"function\": \"count\", \"field\": \"*\", \"orderby\": \"garbage\"}", | ||
| DisplayName = "Nonsense orderby value without groupby is silently ignored")] | ||
| public async Task AggregateRecords_OrderbyWithoutGroupby_PassesValidation(string json) | ||
| { | ||
| IServiceProvider sp = CreateDefaultServiceProvider(); | ||
|
|
||
| CallToolResult result = await ExecuteToolAsync(sp, json); | ||
|
|
||
| // The tool may fail at metadata resolution (no real DB), but must NOT fail with InvalidArguments. | ||
| // If the tool succeeds, that's also acceptable — the test is focused on input validation. | ||
| if (result.IsError != true) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| JsonElement content = ParseContent(result); | ||
| string errorType = content.GetProperty("error").GetProperty("type").GetString()!; | ||
| Assert.AreNotEqual("InvalidArguments", errorType, | ||
| $"orderby without groupby must not be rejected as InvalidArguments. Got error type: {errorType}"); | ||
souvikghosh04 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /// <summary> | ||
| /// Verifies that simple count(*) without orderby or groupby passes validation. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public async Task AggregateRecords_SimpleCountStar_PassesValidation() | ||
| { | ||
| IServiceProvider sp = CreateDefaultServiceProvider(); | ||
|
|
||
| CallToolResult result = await ExecuteToolAsync(sp, | ||
| "{\"entity\": \"Book\", \"function\": \"count\", \"field\": \"*\"}"); | ||
|
|
||
| // If the tool succeeds, that's acceptable — the test is focused on input validation. | ||
| if (result.IsError != true) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| JsonElement content = ParseContent(result); | ||
| string errorType = content.GetProperty("error").GetProperty("type").GetString()!; | ||
| Assert.AreNotEqual("InvalidArguments", errorType, | ||
| "Simple count(*) must pass input validation."); | ||
souvikghosh04 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| /// <summary> | ||
| /// Verifies ValidateGroupByDependencies directly: orderby is silently cleared | ||
| /// when groupby count is 0. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public void ValidateGroupByDependencies_OrderbyWithoutGroupby_ClearsOrderbyFlag() | ||
| { | ||
| bool userProvidedOrderby = true; | ||
| CallToolResult? result = AggregateRecordsTool.ValidateGroupByDependencies( | ||
| groupbyCount: 0, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The tests above regarding validation already cover this. |
||
| userProvidedOrderby: ref userProvidedOrderby, | ||
| first: null, | ||
| after: null, | ||
| toolName: "aggregate_records", | ||
| logger: null); | ||
|
|
||
| Assert.IsNull(result, "No error should be returned when orderby is provided without groupby."); | ||
| Assert.IsFalse(userProvidedOrderby, "userProvidedOrderby must be set to false when groupby is absent."); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Verifies ValidateGroupByDependencies preserves orderby when groupby is present. | ||
| /// </summary> | ||
| [TestMethod] | ||
| public void ValidateGroupByDependencies_OrderbyWithGroupby_PreservesOrderbyFlag() | ||
| { | ||
| bool userProvidedOrderby = true; | ||
| CallToolResult? result = AggregateRecordsTool.ValidateGroupByDependencies( | ||
| groupbyCount: 2, | ||
| userProvidedOrderby: ref userProvidedOrderby, | ||
| first: null, | ||
| after: null, | ||
| toolName: "aggregate_records", | ||
| logger: null); | ||
|
|
||
| Assert.IsNull(result, "No error should be returned when both orderby and groupby are provided."); | ||
| Assert.IsTrue(userProvidedOrderby, "userProvidedOrderby must remain true when groupby is present."); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Verifies that the orderby schema property has no default value (fix for #3279). | ||
| /// </summary> | ||
| [TestMethod] | ||
| public void GetToolMetadata_OrderbySchemaHasNoDefault() | ||
| { | ||
| AggregateRecordsTool tool = new(); | ||
| Tool metadata = tool.GetToolMetadata(); | ||
|
|
||
| JsonElement properties = metadata.InputSchema.GetProperty("properties"); | ||
| JsonElement orderbyProp = properties.GetProperty("orderby"); | ||
|
|
||
| Assert.IsFalse(orderbyProp.TryGetProperty("default", out _), | ||
| "The 'orderby' schema property must not have a default value. " + | ||
| "A default causes LLMs to always send orderby, which previously forced groupby to be required."); | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
| #region Input Validation Tests - Having Clause | ||
|
|
||
| [DataTestMethod] | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.