Skip to content
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

Fixes #3165: UriParser - Aggregation method Average doesn't support Int16 #3168

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions src/Microsoft.OData.Core/UriParser/Aggregation/ApplyBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,9 @@ private IEdmTypeReference CreateAggregateExpressionTypeReference(SingleValueNode
EdmPrimitiveTypeKind expressionPrimitiveKind = expressionType.PrimitiveKind();
switch (expressionPrimitiveKind)
{
case EdmPrimitiveTypeKind.SByte:
case EdmPrimitiveTypeKind.Byte:
case EdmPrimitiveTypeKind.Int16:
Comment on lines +179 to +181
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just realized that the types we support here correspond with to the types supported by the generic Average method supported by the framework https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.average?view=net-9.0. There's no Average method that accepts a collection of Int16.

Furthermore, we'd need to ensure parity with the ASP.NET Core OData library otherwise we'd end up with a bug in the dependent library due to the mappings here:
https://github.com/OData/AspNetCoreOData/blob/0ceeb086e4fd7a6396b4c6e0fd4f123283d55736/src/Microsoft.AspNetCore.OData/Query/ExpressionHelperMethods.cs#L86

We could cast Int16 to Int32 in order to support your scenario, but we need to discuss this more broadly before moving forward with this change.

Finally, I don't think we should add SByte and Byte for now. I think such scenarios are much less likely. Either way, this pull request doesn't add tests for those two additions.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, I'll wait with the changes to the PR till your response with the result of your discussion. Is that ok?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And just to add my two cents to this conversation. I do know that .NET's Average method does not contain sufficient override for Int16, but in my opinion thatis just another bug and UriParser should be agnostic to this. Why should a parser determine that having average over Int16 is forbidden? Especially when Sum is supported for Int16 by the parser.

Also, even if the types of sbyte and byte are less common, it still makes sense to average over them.

case EdmPrimitiveTypeKind.Int32:
case EdmPrimitiveTypeKind.Int64:
case EdmPrimitiveTypeKind.Double:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,27 @@ public void BindApplyWithGroupByComplexAndTypeCastShouldReturnApplyClause()
Assert.Empty(homeNode.ChildTransformations);
}

[Fact]
public void BindApplyWithAverageWithInt16PropertyShouldReturnApplyClause()
{
IEnumerable<QueryToken> tokens = _parser.ParseApply("aggregate(FavoriteNumber with average as AverageFavoriteNumber)");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FavoriteNumber is defined as UInt16 in the referenced Edm model. I'd advise that we also add a test for Int16 to cover all our bases


ApplyBinder binder = new ApplyBinder(FakeBindMethods.BindMethodReturningASingleFloatPrimitive, _bindingState);
ApplyClause actual = binder.BindApply(tokens);

Assert.NotNull(actual);
AggregateTransformationNode aggregate = Assert.IsType<AggregateTransformationNode>(Assert.Single(actual.Transformations));

Assert.Equal(TransformationNodeKind.Aggregate, aggregate.Kind);
Assert.NotNull(aggregate.AggregateExpressions);

AggregateExpression statement = Assert.IsType<AggregateExpression>(Assert.Single(aggregate.AggregateExpressions));
Assert.NotNull(statement.Expression);
Assert.Same(FakeBindMethods.FakeSingleFloatPrimitive, statement.Expression);
Assert.Equal(AggregationMethod.Average, statement.Method);
Assert.Equal("AverageFavoriteNumber", statement.Alias);
}

private static ConstantNode _booleanPrimitiveNode = new ConstantNode(true);

private static SingleValueNode BindMethodReturnsBooleanPrimitive(QueryToken token)
Expand Down