From cee3f89dd189ec38b5944410a871d758bf9e3aef Mon Sep 17 00:00:00 2001 From: Christopher Jolly Date: Thu, 31 Oct 2024 17:34:16 +0800 Subject: [PATCH 1/2] Use the proper type mapping for the result of AVG --- ...yableMethodTranslatingExpressionVisitor.cs | 2 +- ...thwindAggregateOperatorsQueryCosmosTest.cs | 38 ++++++++----------- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/src/EFCore.Cosmos/Query/Internal/CosmosQueryableMethodTranslatingExpressionVisitor.cs b/src/EFCore.Cosmos/Query/Internal/CosmosQueryableMethodTranslatingExpressionVisitor.cs index 9b446a78152..0213a9b8546 100644 --- a/src/EFCore.Cosmos/Query/Internal/CosmosQueryableMethodTranslatingExpressionVisitor.cs +++ b/src/EFCore.Cosmos/Query/Internal/CosmosQueryableMethodTranslatingExpressionVisitor.cs @@ -459,7 +459,7 @@ private ShapedQueryExpression CreateShapedQueryExpression(SelectExpression selec } var projection = (SqlExpression)selectExpression.GetMappedProjection(new ProjectionMember()); - projection = _sqlExpressionFactory.Function("AVG", new[] { projection }, projection.Type, projection.TypeMapping); + projection = _sqlExpressionFactory.Function("AVG", new[] { projection }, resultType, _typeMappingSource.FindMapping(resultType) ?? projection.TypeMapping); return AggregateResultShaper(source, projection, throwOnNullResult: true, resultType); } diff --git a/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindAggregateOperatorsQueryCosmosTest.cs b/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindAggregateOperatorsQueryCosmosTest.cs index 2d30fc87c90..c5a415fb1b9 100644 --- a/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindAggregateOperatorsQueryCosmosTest.cs +++ b/test/EFCore.Cosmos.FunctionalTests/Query/NorthwindAggregateOperatorsQueryCosmosTest.cs @@ -699,22 +699,19 @@ public override async Task Min_no_data_subquery(bool async) AssertSql(); } - public override async Task Average_with_no_arg(bool async) - { - // Always throws for sync. - if (async) - { - // Average truncates. Issue #26378. - await Assert.ThrowsAsync(async () => await base.Average_with_no_arg(async)); + public override Task Average_with_no_arg(bool async) + => Fixture.NoSyncTest( + async, async a => + { + await base.Average_with_no_arg(a); - AssertSql( - """ + AssertSql( + """ SELECT VALUE AVG(c["OrderID"]) FROM root c WHERE (c["$type"] = "Order") """); - } - } + }); public override Task Average_with_binary_expression(bool async) => Fixture.NoSyncTest( @@ -730,22 +727,19 @@ FROM root c """); }); - public override async Task Average_with_arg(bool async) - { - // Always throws for sync. - if (async) - { - // Average truncates. Issue #26378. - await Assert.ThrowsAsync(async () => await base.Average_with_arg(async)); + public override Task Average_with_arg(bool async) + => Fixture.NoSyncTest( + async, async a => + { + await base.Average_with_arg(a); - AssertSql( - """ + AssertSql( + """ SELECT VALUE AVG(c["OrderID"]) FROM root c WHERE (c["$type"] = "Order") """); - } - } + }); public override Task Average_with_arg_expression(bool async) => Fixture.NoSyncTest( From 93d2596ab4db4289e8d5248a265179a1d7b11556 Mon Sep 17 00:00:00 2001 From: Christopher Jolly Date: Thu, 31 Oct 2024 18:35:32 +0800 Subject: [PATCH 2/2] Remove unneeded fallback --- .../CosmosQueryableMethodTranslatingExpressionVisitor.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/EFCore.Cosmos/Query/Internal/CosmosQueryableMethodTranslatingExpressionVisitor.cs b/src/EFCore.Cosmos/Query/Internal/CosmosQueryableMethodTranslatingExpressionVisitor.cs index 0213a9b8546..80f90b8574a 100644 --- a/src/EFCore.Cosmos/Query/Internal/CosmosQueryableMethodTranslatingExpressionVisitor.cs +++ b/src/EFCore.Cosmos/Query/Internal/CosmosQueryableMethodTranslatingExpressionVisitor.cs @@ -459,7 +459,7 @@ private ShapedQueryExpression CreateShapedQueryExpression(SelectExpression selec } var projection = (SqlExpression)selectExpression.GetMappedProjection(new ProjectionMember()); - projection = _sqlExpressionFactory.Function("AVG", new[] { projection }, resultType, _typeMappingSource.FindMapping(resultType) ?? projection.TypeMapping); + projection = _sqlExpressionFactory.Function("AVG", new[] { projection }, resultType, _typeMappingSource.FindMapping(resultType)); return AggregateResultShaper(source, projection, throwOnNullResult: true, resultType); }