From 729e3220c31b0a1a74a2d501eb1798a7e0906a4e Mon Sep 17 00:00:00 2001 From: Athos Couto Date: Wed, 8 Feb 2017 16:50:57 -0800 Subject: [PATCH] Removing compiler warnings, adding negative test case, removing redundant code and adding comments referencing issue #758. --- .../UriParser/Aggregation/ApplyBinder.cs | 1 + .../Parsers/UriQueryExpressionParser.cs | 3 ++- .../SemanticAst/CountVirtualPropertyNode.cs | 25 ++++++++++++++----- .../Parsers/UriQueryExpressionParserTests.cs | 9 +++++++ 4 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/Microsoft.OData.Core/UriParser/Aggregation/ApplyBinder.cs b/src/Microsoft.OData.Core/UriParser/Aggregation/ApplyBinder.cs index bda1330b5b..11723c53ac 100644 --- a/src/Microsoft.OData.Core/UriParser/Aggregation/ApplyBinder.cs +++ b/src/Microsoft.OData.Core/UriParser/Aggregation/ApplyBinder.cs @@ -118,6 +118,7 @@ private IEdmTypeReference CreateAggregateExpressionTypeReference(SingleValueNode case AggregationMethod.VirtualPropertyCount: case AggregationMethod.CountDistinct: + // Issue #758: CountDistinct and $Count should return type Edm.Decimal with Scale="0" and sufficient Precision. return EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int64, false); case AggregationMethod.Max: case AggregationMethod.Min: diff --git a/src/Microsoft.OData.Core/UriParser/Parsers/UriQueryExpressionParser.cs b/src/Microsoft.OData.Core/UriParser/Parsers/UriQueryExpressionParser.cs index 3a96d46cf9..d8e493c157 100644 --- a/src/Microsoft.OData.Core/UriParser/Parsers/UriQueryExpressionParser.cs +++ b/src/Microsoft.OData.Core/UriParser/Parsers/UriQueryExpressionParser.cs @@ -287,11 +287,12 @@ internal AggregateExpressionToken ParseAggregateExpression() var endPathExpression = expression as EndPathToken; AggregationMethod verb; + // e.g. aggregate($count as Count) if (endPathExpression != null && endPathExpression.Identifier == ExpressionConstants.QueryOptionCount) { - expression = new EndPathToken("$count", endPathExpression.NextToken); verb = AggregationMethod.VirtualPropertyCount; } + // e.g. aggregate(UnitPrice with sum as Total) else { // "with" verb diff --git a/src/Microsoft.OData.Core/UriParser/SemanticAst/CountVirtualPropertyNode.cs b/src/Microsoft.OData.Core/UriParser/SemanticAst/CountVirtualPropertyNode.cs index a9784f98f1..4ffa75747f 100644 --- a/src/Microsoft.OData.Core/UriParser/SemanticAst/CountVirtualPropertyNode.cs +++ b/src/Microsoft.OData.Core/UriParser/SemanticAst/CountVirtualPropertyNode.cs @@ -1,25 +1,38 @@ -using Microsoft.OData.Edm; +//--------------------------------------------------------------------- +// +// Copyright (C) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information. +// +//--------------------------------------------------------------------- +using Microsoft.OData.Edm; namespace Microsoft.OData.UriParser { /// - /// Dummy class that allows virtual property $count + /// Dummy class that allows virtual property $count /// to work like any other aggregation method. /// public sealed class CountVirtualPropertyNode : SingleValueNode { - public CountVirtualPropertyNode() { } + /// Constructor. + public CountVirtualPropertyNode() + { + } + /// Kind of the single value node. public override QueryNodeKind Kind { get { - return QueryNodeKind.None; + return QueryNodeKind.Count; } } - public override IEdmTypeReference TypeReference { - get { + /// Type returned by the $count virtual property. + public override IEdmTypeReference TypeReference + { + get + { + // Issue #758: CountDistinct and $Count should return type Edm.Decimal with Scale="0" and sufficient Precision. return EdmCoreModel.Instance.GetPrimitive(EdmPrimitiveTypeKind.Int64, false); } } diff --git a/test/FunctionalTests/Microsoft.OData.Core.Tests/UriParser/Parsers/UriQueryExpressionParserTests.cs b/test/FunctionalTests/Microsoft.OData.Core.Tests/UriParser/Parsers/UriQueryExpressionParserTests.cs index da726025e3..6fd822a6a4 100644 --- a/test/FunctionalTests/Microsoft.OData.Core.Tests/UriParser/Parsers/UriQueryExpressionParserTests.cs +++ b/test/FunctionalTests/Microsoft.OData.Core.Tests/UriParser/Parsers/UriQueryExpressionParserTests.cs @@ -180,6 +180,15 @@ public void ParseApplyWithSingleCountExpressionShouldReturnAggregateToken() VerifyAggregateExpressionToken("$count", AggregationMethod.VirtualPropertyCount, "Count", aggregate.Expressions.First()); } + [Fact] + public void ParseApplyWithSingleCountExpressionCannotHaveWithKeyWord() + { + var apply = "aggregate($count with sum as Count)"; + + Action parse = () => this.testSubject.ParseApply(apply); + parse.ShouldThrow().Where(e => e.Message == ErrorStrings.UriQueryExpressionParser_AsExpected(17, apply)); + } + [Fact] public void ParseApplyWithCountAndOtherAggregationExpressionShouldReturnAggregateToken() {