public void Process(NonAggregatingGroupBy resultOperator, QueryModelVisitor queryModelVisitor, IntermediateHqlTree tree) { var selector = queryModelVisitor.Model.SelectClause.Selector; var keySelector = resultOperator.GroupBy.KeySelector; var elementSelector = resultOperator.GroupBy.ElementSelector; var sourceType = selector.Type; var keyType = keySelector.Type; var elementType = elementSelector.Type; // Stuff in the group by that doesn't map to HQL. Run it client-side var listParameter = Expression.Parameter(typeof(IEnumerable <object>), "list"); var keySelectorExpr = ReverseResolvingExpressionTreeVisitor.ReverseResolve(selector, keySelector); var elementSelectorExpr = ReverseResolvingExpressionTreeVisitor.ReverseResolve(selector, elementSelector); var groupByMethod = ReflectionCache.EnumerableMethods.GroupByWithElementSelectorDefinition .MakeGenericMethod(new[] { sourceType, keyType, elementType }); var castToItem = ReflectionCache.EnumerableMethods.CastDefinition.MakeGenericMethod(new[] { sourceType }); var toList = ReflectionCache.EnumerableMethods.ToListDefinition.MakeGenericMethod(new[] { resultOperator.GroupBy.ItemType }); Expression castToItemExpr = Expression.Call(castToItem, listParameter); var groupByExpr = Expression.Call(groupByMethod, castToItemExpr, keySelectorExpr, elementSelectorExpr); var toListExpr = Expression.Call(toList, groupByExpr); var lambdaExpr = Expression.Lambda(toListExpr, listParameter); tree.AddListTransformer(lambdaExpr); }
public override StreamedValue ExecuteInMemory <T>(StreamedSequence input) { var sequence = input.GetTypedSequence <T>(); var predicateLambda = ReverseResolvingExpressionTreeVisitor.ReverseResolve(input.DataInfo.ItemExpression, Predicate); var predicate = (Func <T, bool>)predicateLambda.Compile(); var result = sequence.All(predicate); return(new StreamedValue(result, (StreamedValueInfo)base.GetOutputDataInfo(input.DataInfo))); }
public void ReverseResolve_NonAccessibleReferenceExpression_Throws() { // itemExpression: new AnonymousType<Cook, Cook> ( a = [s1], b = [s2] ) // resolvedExpression: [s3] // expected result: exception var fromClause3 = ExpressionHelper.CreateMainFromClause_Int("s3", typeof(Cook), ExpressionHelper.CreateQueryable <Cook>()); var resolvedExpression = new QuerySourceReferenceExpression(fromClause3); ReverseResolvingExpressionTreeVisitor.ReverseResolve(_itemExpression, resolvedExpression); }
public void ReverseResolve_TopLevelReferenceExpression() { // itemExpression: new AnonymousType<Cook, Cook> ( a = [s1], b = [s2] ) // resolvedExpression: [s1] // expected result: input => input.a var resolvedExpression = _querySource1; LambdaExpression lambdaExpression = ReverseResolvingExpressionTreeVisitor.ReverseResolve(_itemExpression, resolvedExpression); var expectedExpression = ExpressionHelper.CreateLambdaExpression <AnonymousType <Cook, Cook>, Cook> (input => input.a); ExpressionTreeComparer.CheckAreEqualTrees(expectedExpression, lambdaExpression); }
public void ReverseResolve_NoReferenceExpression() { // itemExpression: new AnonymousType<Cook, Cook> ( a = [s1], b = [s2] ) // resolvedExpression: 0 // expected result: input => 0 var resolvedExpression = Expression.Constant(0); LambdaExpression lambdaExpression = ReverseResolvingExpressionTreeVisitor.ReverseResolve(_itemExpression, resolvedExpression); var expectedExpression = ExpressionHelper.CreateLambdaExpression <AnonymousType <Cook, Cook>, int> (input => 0); ExpressionTreeComparer.CheckAreEqualTrees(expectedExpression, lambdaExpression); }
public void ReverseResolve_MultipleNestedReferenceExpressions() { // itemExpression: new AnonymousType<Cook, Cook> ( a = [s1], b = [s2] ) // resolvedExpression: [s1].ID + [s2].ID // expected result: input => input.a.ID + input.b.ID var resolvedExpression = ExpressionHelper.Resolve <Cook, Cook, int> (_fromClause1, _fromClause2, (s1, s2) => s1.ID + s2.ID); LambdaExpression lambdaExpression = ReverseResolvingExpressionTreeVisitor.ReverseResolve(_itemExpression, resolvedExpression); var expectedExpression = ExpressionHelper.CreateLambdaExpression <AnonymousType <Cook, Cook>, int> (input => input.a.ID + input.b.ID); ExpressionTreeComparer.CheckAreEqualTrees(expectedExpression, lambdaExpression); }
public StreamedSequence ExecuteGroupingInMemory <TSource, TKey, TElement>(StreamedSequence input) { var inputSequence = input.GetTypedSequence <TSource>(); var keySelectorLambda = ReverseResolvingExpressionTreeVisitor.ReverseResolve(input.DataInfo.ItemExpression, KeySelector); var keySelector = (Func <TSource, TKey>)keySelectorLambda.Compile(); var elementSelectorLambda = ReverseResolvingExpressionTreeVisitor.ReverseResolve(input.DataInfo.ItemExpression, ElementSelector); var elementSelector = (Func <TSource, TElement>)elementSelectorLambda.Compile(); var resultSequence = inputSequence.GroupBy(keySelector, elementSelector); return(new StreamedSequence(resultSequence.AsQueryable(), (StreamedSequenceInfo)GetOutputDataInfo(input.DataInfo))); }