Skip to content

CSHARP-5675: Where possible, return null for average over the empty set #1755

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ internal static class AverageMethodToExecutableQueryTranslator<TOutput>
// private static fields
private static readonly MethodInfo[] __averageMethods;
private static readonly MethodInfo[] __averageWithSelectorMethods;
private static readonly IExecutableQueryFinalizer<TOutput, TOutput> __finalizer = new SingleFinalizer<TOutput>();
private static readonly IExecutableQueryFinalizer<TOutput, TOutput> __singleFinalizer = new SingleFinalizer<TOutput>();
private static readonly IExecutableQueryFinalizer<TOutput, TOutput> __singleOrDefaultFinalizer = new SingleOrDefaultFinalizer<TOutput>();

// static constructor
static AverageMethodToExecutableQueryTranslator()
Expand Down Expand Up @@ -138,11 +139,11 @@ public static ExecutableQuery<TDocument, TOutput> Translate<TDocument>(MongoQuer

IBsonSerializer outputValueSerializer = expression.GetResultType() switch
{
Type t when t == typeof(int) => new Int32Serializer(),
Type t when t == typeof(long) => new Int64Serializer(),
Type t when t == typeof(float) => new SingleSerializer(),
Type t when t == typeof(double) => new DoubleSerializer(),
Type t when t == typeof(decimal) => new DecimalSerializer(),
Type t when t == typeof(int) => Int32Serializer.Instance,
Type t when t == typeof(long) => Int64Serializer.Instance,
Type t when t == typeof(float) => SingleSerializer.Instance,
Type t when t == typeof(double) => DoubleSerializer.Instance,
Type t when t == typeof(decimal) => DecimalSerializer.Instance,
Type { IsConstructedGenericType: true } t when t.GetGenericTypeDefinition() == typeof(Nullable<>) => (IBsonSerializer)Activator.CreateInstance(typeof(NullableSerializer<>).MakeGenericType(t.GenericTypeArguments[0])),
_ => throw new ExpressionNotSupportedException(expression)
};
Expand All @@ -155,10 +156,14 @@ public static ExecutableQuery<TDocument, TOutput> Translate<TDocument>(MongoQuer
AstStage.Project(AstProject.ExcludeId()),
outputWrappedValueSerializer);

var returnType = expression.Type;

return ExecutableQuery.Create(
provider,
pipeline,
__finalizer);
!returnType.IsValueType || returnType.IsNullable()
? __singleOrDefaultFinalizer
: __singleFinalizer);
}

throw new ExpressionNotSupportedException(expression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,11 @@ private void InsertSecond()
O = new List<long> { 100, 200, 300 },
P = 1.1,
U = -1.234565723762724332233489m,
Z = 10
Z = 10,
NullableW = 8,
NullableX = 9,
NullableY = 10,
NullableZ = 11
};
__collection.InsertOne(root);
}
Expand Down Expand Up @@ -333,6 +337,14 @@ public class Root : IRoot
public int Y { get; set; }

public decimal Z { get; set; }

public double? NullableW { get; set; }

public long? NullableX { get; set; }

public int? NullableY { get; set; }

public decimal? NullableZ { get; set; }
}

public class RootDescended : Root
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,106 @@ public async Task AverageAsync_with_selector()
result.Should().Be(61);
}

[Fact]
public void Average_on_empty_set()
{
Action action = () => CreateQuery().Where(x => x.A == "__dummy__").Select(x => x.W).Average();

action.ShouldThrow<InvalidOperationException>().WithMessage("Sequence contains no elements");
}

[Fact]
public void Average_on_empty_set_with_selector()
{
Action action = () => CreateQuery().Where(x => x.A == "__dummy__").Average(x => x.X);

action.ShouldThrow<InvalidOperationException>().WithMessage("Sequence contains no elements");
}

[Fact]
public void AverageAsync_on_empty_set()
{
var subject = CreateQuery().Where(x => x.A == "__dummy__").Select(x => x.Y).AverageAsync();

subject.Awaiting(async q => await q)
.ShouldThrow<InvalidOperationException>()
.WithMessage("Sequence contains no elements");
}

[Fact]
public void AverageAsync_on_empty_set_with_selector()
{
var subject = CreateQuery().Where(x => x.A == "__dummy__").AverageAsync(x => x.Z);

subject.Awaiting(async q => await q)
.ShouldThrow<InvalidOperationException>()
.WithMessage("Sequence contains no elements");
}

[Fact]
public void Average_on_nullable_empty_set()
{
var result = CreateQuery().Where(x => x.A == "__dummy__").Select(x => x.NullableW).Average();

result.Should().Be(null);
}

[Fact]
public void Average_on_nullable_empty_set_with_selector()
{
var result = CreateQuery().Where(x => x.A == "__dummy__").Average(x => x.NullableX);

result.Should().Be(null);
}

[Fact]
public async Task AverageAsync_on_nullable_empty_set()
{
var result = await CreateQuery().Where(x => x.A == "__dummy__").Select(x => x.NullableY).AverageAsync();

result.Should().Be(null);
}

[Fact]
public async Task AverageAsync_on_nullable_empty_set_with_selector()
{
var result = await CreateQuery().Where(x => x.A == "__dummy__").AverageAsync(x => x.NullableZ);

result.Should().Be(null);
}

[Fact]
public void Average_on_empty_set_cast_to_nullable()
{
var result = CreateQuery().Where(x => x.A == "__dummy__").Select(x => (double?)x.W).Average();

result.Should().Be(null);
}

[Fact]
public void Average_on_empty_set_cast_to_nullable_with_selector()
{
var result = CreateQuery().Where(x => x.A == "__dummy__").Average(x => (long?)x.X);

result.Should().Be(null);
}

[Fact]
public async Task AverageAsync_on_empty_set_cast_to_nullable()
{
var result = await CreateQuery().Where(x => x.A == "__dummy__").Select(x => (int?)x.Y).AverageAsync();

result.Should().Be(null);
}

[Fact]
public async Task AverageAsync_on_empty_set_cast_to_nullable_with_selector()
{
var result = await CreateQuery().Where(x => x.A == "__dummy__").AverageAsync(x => (decimal?)x.Z);

result.Should().Be(null);
}

[Fact]
public void GroupBy_combined_with_a_previous_embedded_pipeline()
{
Expand Down
Loading