Skip to content

Commit

Permalink
Fix parsing of floats in $apply/aggregate/average expression (#1811)
Browse files Browse the repository at this point in the history
* Fix parsing of floats in apply-average expression

* Add review feedback

Co-authored-by: John Gathogo <jogathog@microsoft.com>
  • Loading branch information
gathogojr and gathogojr committed Jun 19, 2020
1 parent eae9e78 commit ad7f491
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Microsoft.OData.Core/UriParser/Aggregation/ApplyBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ private IEdmTypeReference CreateAggregateExpressionTypeReference(SingleValueNode
return EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Double, expressionType.IsNullable);
case EdmPrimitiveTypeKind.Decimal:
return EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Decimal, expressionType.IsNullable);
case EdmPrimitiveTypeKind.Single:
return EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Single, expressionType.IsNullable);
case EdmPrimitiveTypeKind.None:
return expressionType;
default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ internal class FakeBindMethods
public static readonly SingleValueNode FakeSingleIntPrimitive =
new ConstantNode(100);

public static readonly SingleValueNode FakeSingleFloatPrimitive =
new ConstantNode(100.50f);

public static readonly SingleValueNode FakeSingleOpenProperty =
new SingleValueOpenPropertyAccessNode(new ConstantNode(null), "A_OPENPROPERTY");

Expand Down Expand Up @@ -90,6 +93,11 @@ public static SingleValueNode BindMethodReturningASingleIntPrimitive(QueryToken
return FakeSingleIntPrimitive;
}

public static SingleValueNode BindMethodReturningASingleFloatPrimitive(QueryToken token)
{
return FakeSingleFloatPrimitive;
}

public static SingleValueNode BindMethodReturningASingleOpenProperty(QueryToken token)
{
return FakeSingleOpenProperty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,27 @@ public void BindApplyWithAggregateShouldReturnApplyClause()
Assert.Equal("TotalPrice", statement.Alias);
}

[Fact]
public void BindApplyWithAverageInAggregateShouldReturnApplyClause()
{
IEnumerable<QueryToken> tokens = _parser.ParseApply("aggregate(UnitPrice with average as AveragePrice)");

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("AveragePrice", statement.Alias);
}

[Fact]
public void BindApplyWithCountInAggregateShouldReturnApplyClause()
{
Expand Down

0 comments on commit ad7f491

Please sign in to comment.