/// <summary> /// Apply mapping translations to this expression /// </summary> /// <param name="expression"></param> /// <returns></returns> public virtual Expression Translate(Expression expression) { // convert references to LINQ operators into query specific nodes expression = QueryBinder.Bind(this, expression); // move aggregate computations so they occur in same select as group-by expression = AggregateRewriter.Rewrite(this.Translator.Linguist.Language, expression); // do reduction so duplicate association's are likely to be clumped together expression = UnusedColumnRemover.Remove(expression); expression = RedundantColumnRemover.Remove(expression); expression = RedundantSubqueryRemover.Remove(expression); expression = RedundantJoinRemover.Remove(expression); // convert references to association properties into correlated queries var bound = RelationshipBinder.Bind(this, expression); if (bound != expression) { expression = bound; // clean up after ourselves! (multiple references to same association property) expression = RedundantColumnRemover.Remove(expression); expression = RedundantJoinRemover.Remove(expression); } // rewrite comparison checks between entities and multi-valued constructs expression = ComparisonRewriter.Rewrite(this.Mapping, expression); return(expression); }
internal static Expression Optimize(Expression binded, QueryBinder binder, AliasGenerator aliasGenerator, HeavyProfiler.Tracer?log) { var isPostgres = Schema.Current.Settings.IsPostgres; log.Switch("Aggregate"); Expression rewriten = AggregateRewriter.Rewrite(binded); log.Switch("DupHistory"); Expression dupHistory = DuplicateHistory.Rewrite(rewriten, aliasGenerator); log.Switch("EntityCompleter"); Expression completed = EntityCompleter.Complete(dupHistory, binder); log.Switch("AliasReplacer"); Expression replaced = AliasProjectionReplacer.Replace(completed, aliasGenerator); log.Switch("OrderBy"); Expression orderRewrited = OrderByRewriter.Rewrite(replaced); log.Switch("OrderBy"); Expression lazyCastRemoved = SqlCastLazyRemover.Remove(orderRewrited); log.Switch("Rebinder"); Expression rebinded = QueryRebinder.Rebind(lazyCastRemoved); log.Switch("UnusedColumn"); Expression columnCleaned = UnusedColumnRemover.Remove(rebinded); log.Switch("Redundant"); Expression subqueryCleaned = RedundantSubqueryRemover.Remove(columnCleaned); log.Switch("Condition"); Expression rewriteConditions = isPostgres ? ConditionsRewriterPostgres.Rewrite(subqueryCleaned) : ConditionsRewriter.Rewrite(subqueryCleaned); log.Switch("Scalar"); Expression scalar = ScalarSubqueryRewriter.Rewrite(rewriteConditions); return(scalar); }
/// <summary> /// Apply mapping translations to this expression /// </summary> /// <param name="expression"></param> /// <returns></returns> public virtual Expression Translate(Expression expression) { // convert references to LINQ operators into query specific nodes expression = QueryBinder.Bind(this, expression); // move aggregate computations so they occur in same select as group-by expression = AggregateRewriter.Rewrite(expression); // do reduction so duplicate association's are likely to be clumped together expression = UnusedColumnRemover.Remove(expression); expression = RedundantColumnRemover.Remove(expression); expression = RedundantSubqueryRemover.Remove(expression); expression = RedundantJoinRemover.Remove(expression); // convert references to association properties into correlated queries expression = RelationshipBinder.Bind(this, expression); // clean up after ourselves! (multiple references to same association property) expression = RedundantColumnRemover.Remove(expression); expression = RedundantJoinRemover.Remove(expression); return(expression); }
internal Expression GetExecutionPlan(Expression expression) { LambdaExpression lambda = expression as LambdaExpression; if (lambda != null) { expression = lambda.Body; } var translation = expression; translation = PartialEvaluator.Eval(expression, ExpressionHelper.CanBeEvaluatedLocally); translation = FunctionBinder.Bind(this, translation); //translation = PartialEvaluator.Eval(translation, ExpressionHelper.CanBeEvaluatedLocally); translation = QueryBinder.Bind(ExpressionBuilder, this, translation); translation = AggregateRewriter.Rewrite(Dialect, translation); translation = UnusedColumnRemover.Remove(translation); translation = RedundantColumnRemover.Remove(translation); translation = RedundantSubqueryRemover.Remove(translation); translation = RedundantJoinRemover.Remove(translation); var bound = RelationshipBinder.Bind(ExpressionBuilder, translation); if (bound != translation) { translation = bound; translation = RedundantColumnRemover.Remove(translation); translation = RedundantJoinRemover.Remove(translation); } translation = ComparisonRewriter.Rewrite(ExpressionBuilder, translation); var rewritten = RelationshipIncluder.Include(ExpressionBuilder, this, translation); if (rewritten != translation) { translation = rewritten; translation = UnusedColumnRemover.Remove(translation); translation = RedundantColumnRemover.Remove(translation); translation = RedundantSubqueryRemover.Remove(translation); translation = RedundantJoinRemover.Remove(translation); } rewritten = SingletonProjectionRewriter.Rewrite(this.ExpressionBuilder, translation); if (rewritten != translation) { translation = rewritten; translation = UnusedColumnRemover.Remove(translation); translation = RedundantColumnRemover.Remove(translation); translation = RedundantSubqueryRemover.Remove(translation); translation = RedundantJoinRemover.Remove(translation); } rewritten = ClientJoinedProjectionRewriter.Rewrite(this, translation); if (rewritten != translation) { translation = rewritten; translation = UnusedColumnRemover.Remove(translation); translation = RedundantColumnRemover.Remove(translation); translation = RedundantSubqueryRemover.Remove(translation); translation = RedundantJoinRemover.Remove(translation); } // translation = this.ExpressionBuilder.Translate(translation); var parameters = lambda != null ? lambda.Parameters : null; Expression provider = Find(expression, parameters, typeof(InternalDbContext)); if (provider == null) { Expression rootQueryable = Find(expression, parameters, typeof(IQueryable)); provider = Expression.Property(rootQueryable, typeof(IQueryable).GetProperty("Provider")); } return(ExecutionBuilder.Build(this.Dialect, this, translation, provider)); }