Skip to content

Commit 032eff9

Browse files
ar-guptaarARPIT GUPTA
andauthored
Fix column mapping in GroupBy and aggregation queries to ensure corre… (#3750)
## 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]](diffhunk://#diff-4b3df270873038f36ec2f627091120b7aafa271c0e2a2d257380f90602f5a298L907-R908) [[2]](diffhunk://#diff-4b3df270873038f36ec2f627091120b7aafa271c0e2a2d257380f90602f5a298L956-R956) ## How was this tested? - [x] Integration Tests - [X] 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. --------- Co-authored-by: ARPIT GUPTA <guptaar@microsoft.com>
1 parent 7341bc2 commit 032eff9

3 files changed

Lines changed: 339 additions & 11 deletions

File tree

src/Core/Resolvers/Sql Query Structures/SqlQueryStructure.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,7 @@ private void ProcessGroupByField(FieldNode groupByField, IMiddlewareContext ctx,
905905
}
906906

907907
GroupByMetadata.Fields[columnName] = new Column(DatabaseObject.SchemaName, DatabaseObject.Name, columnName, SourceAlias);
908-
AddColumn(fieldName, backingColumn ?? fieldName);
908+
AddColumn(columnName: columnName, labelName: fieldName);
909909
fieldsInArgument.Add(fieldName);
910910
}
911911
}
@@ -953,7 +953,7 @@ private void ProcessGroupByFieldSelections(FieldNode groupByFieldSelection, Hash
953953
}
954954

955955
string columnName = MetadataProvider.TryGetBackingColumn(EntityName, fieldName, out string? backingColumn) ? backingColumn : fieldName;
956-
AddColumn(fieldName, columnName);
956+
AddColumn(columnName: columnName, labelName: fieldName);
957957
}
958958

959959
}

src/Core/Resolvers/SqlPaginationUtil.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ public static JsonDocument CreatePaginationConnectionFromJsonDocument(JsonDocume
5454

5555
private static string GenerateGroupByObjectFromResult(GroupByMetadata groupByMetadata, IEnumerable<JsonElement> rootEnumerated)
5656
{
57+
HashSet<string> aggregationAliases = groupByMetadata.Aggregations
58+
.Select(aggregation => aggregation.Column.OperationAlias)
59+
.ToHashSet(StringComparer.Ordinal);
60+
5761
JsonArray groupByArray = new();
5862
foreach (JsonElement element in rootEnumerated)
5963
{
@@ -62,16 +66,13 @@ private static string GenerateGroupByObjectFromResult(GroupByMetadata groupByMet
6266
JsonObject combinedObject = new();
6367
foreach (JsonProperty property in element.EnumerateObject())
6468
{
65-
if (groupByMetadata.Fields.ContainsKey(property.Name))
69+
if (aggregationAliases.Contains(property.Name))
6670
{
67-
if (groupByMetadata.RequestedFields)
68-
{
69-
fieldObject.Add(property.Name, JsonNode.Parse(property.Value.GetRawText()));
70-
}
71+
aggregationObject.Add(property.Name, JsonNode.Parse(property.Value.GetRawText()));
7172
}
72-
else
73+
else if (groupByMetadata.RequestedFields)
7374
{
74-
aggregationObject.Add(property.Name, JsonNode.Parse(property.Value.GetRawText()));
75+
fieldObject.Add(property.Name, JsonNode.Parse(property.Value.GetRawText()));
7576
}
7677
}
7778

0 commit comments

Comments
 (0)