Skip to content

Commit

Permalink
Support type cast in group by (#2879)
Browse files Browse the repository at this point in the history
* Fix

* Tests

---------

Co-authored-by: Clément Bernard <cbernard@groupeisagri.com>
  • Loading branch information
clemvnt and Clément Bernard authored Jun 20, 2024
1 parent 98155ac commit 3a51fa0
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,21 @@ private static bool IsPropertyNode(SingleValueNode node)
{
return node.Kind == QueryNodeKind.SingleValuePropertyAccess ||
node.Kind == QueryNodeKind.SingleComplexNode ||
node.Kind == QueryNodeKind.SingleNavigationNode;
node.Kind == QueryNodeKind.SingleNavigationNode ||
node.Kind == QueryNodeKind.SingleResourceCast;
}

private static Stack<SingleValueNode> ReversePropertyPath(SingleValueNode node)
{
Stack<SingleValueNode> result = new Stack<SingleValueNode>();
do
{
if (node.Kind == QueryNodeKind.SingleResourceCast)
{
node = ((SingleResourceCastNode)node).Source;
continue;
}

result.Push(node);
if (node.Kind == QueryNodeKind.SingleValuePropertyAccess)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -786,6 +786,58 @@ public void BindVirtualPropertiesAfterCollapseReturnsApplyClause()
Assert.Equal(AggregationMethod.VirtualPropertyCount, aggExp.Method);
}

[Fact]
public void BindApplyWithGroupByAndTypeCastShouldReturnApplyClause()
{
IEnumerable<QueryToken> tokens = _parser.ParseApply("groupby((Fully.Qualified.Namespace.Employee/WorkEmail))");

MetadataBinder metadataBiner = new MetadataBinder(_bindingState);

ApplyBinder binder = new ApplyBinder(metadataBiner.Bind, _bindingState);
ApplyClause actual = binder.BindApply(tokens);

Assert.NotNull(actual);

var transformation = Assert.Single(actual.Transformations);
GroupByTransformationNode groupBy = Assert.IsType<GroupByTransformationNode>(transformation);

Assert.Equal(TransformationNodeKind.GroupBy, groupBy.Kind);
Assert.Null(groupBy.ChildTransformations);
Assert.NotNull(groupBy.GroupingProperties);

GroupByPropertyNode workEmailNode = Assert.Single(groupBy.GroupingProperties);
Assert.NotNull(workEmailNode.Expression);
Assert.Empty(workEmailNode.ChildTransformations);
}

[Fact]
public void BindApplyWithGroupByComplexAndTypeCastShouldReturnApplyClause()
{
IEnumerable<QueryToken> tokens = _parser.ParseApply("groupby((MyAddress/Fully.Qualified.Namespace.HomeAddress/HomeNO))");

MetadataBinder metadataBiner = new MetadataBinder(_bindingState);

ApplyBinder binder = new ApplyBinder(metadataBiner.Bind, _bindingState);
ApplyClause actual = binder.BindApply(tokens);

Assert.NotNull(actual);

var transformation = Assert.Single(actual.Transformations);
GroupByTransformationNode groupBy = Assert.IsType<GroupByTransformationNode>(transformation);

Assert.Equal(TransformationNodeKind.GroupBy, groupBy.Kind);
Assert.Null(groupBy.ChildTransformations);
Assert.NotNull(groupBy.GroupingProperties);

GroupByPropertyNode addressNode = Assert.Single(groupBy.GroupingProperties);
Assert.Equal("MyAddress", addressNode.Name);
Assert.Null(addressNode.Expression);

GroupByPropertyNode homeNode = Assert.Single(addressNode.ChildTransformations);
Assert.NotNull(homeNode.Expression);
Assert.Empty(homeNode.ChildTransformations);
}

private static ConstantNode _booleanPrimitiveNode = new ConstantNode(true);

private static SingleValueNode BindMethodReturnsBooleanPrimitive(QueryToken token)
Expand Down

0 comments on commit 3a51fa0

Please sign in to comment.