internal static bool HasAggregates(SelectExpression expression)
        {
            AggregateChecker checker = new AggregateChecker();

            checker.Visit(expression);
            return(checker.hasAggregate);
        }
            private static bool CanMergeWithFrom(SelectExpression select, bool isTopLevel)
            {
                SelectExpression leftMostSelect = GetLeftMostSelect(select.From);

                if (leftMostSelect == null)
                {
                    return(false);
                }
                if (!IsColumnProjection(leftMostSelect))
                {
                    return(false);
                }
                bool flag  = RedundantSubqueryRemover.IsNameMapProjection(select);
                bool flag2 = (select.OrderBy != null) && (select.OrderBy.Count > 0);
                bool flag3 = (select.GroupBy != null) && (select.GroupBy.Count > 0);
                bool flag4 = AggregateChecker.HasAggregates(select);
                bool flag5 = select.From is JoinExpression;
                bool flag6 = (leftMostSelect.OrderBy != null) && (leftMostSelect.OrderBy.Count > 0);
                bool flag7 = (leftMostSelect.GroupBy != null) && (leftMostSelect.GroupBy.Count > 0);
                bool flag8 = AggregateChecker.HasAggregates(leftMostSelect);

                if (flag2 && flag6)
                {
                    return(false);
                }
                if (flag3 && flag7)
                {
                    return(false);
                }
                if (select.IsReverse || leftMostSelect.IsReverse)
                {
                    return(false);
                }
                if (flag6 && ((flag3 || flag4) || select.IsDistinct))
                {
                    return(false);
                }
                if (flag7)
                {
                    return(false);
                }
                if ((leftMostSelect.Take != null) && (((((select.Take != null) || (select.Skip != null)) || (select.IsDistinct || flag4)) || flag3) || flag5))
                {
                    return(false);
                }
                if ((leftMostSelect.Skip != null) && ((((select.Skip != null) || select.IsDistinct) || (flag4 || flag3)) || flag5))
                {
                    return(false);
                }
                if (leftMostSelect.IsDistinct && (((((select.Take != null) || (select.Skip != null)) || (!flag || flag3)) || (flag4 || (flag2 && !isTopLevel))) || flag5))
                {
                    return(false);
                }
                if (flag8 && (((((select.Take != null) || (select.Skip != null)) || (select.IsDistinct || flag4)) || flag3) || flag5))
                {
                    return(false);
                }
                return(true);
            }
        protected override Expression VisitSelect(SelectExpression select)
        {
            Expression expression;
            bool       isOuterMostSelect = this.isOuterMostSelect;

            try
            {
                this.isOuterMostSelect = false;
                select = (SelectExpression)base.VisitSelect(select);
                bool flag2 = (select.OrderBy != null) && (select.OrderBy.Count > 0);
                bool flag3 = (select.GroupBy != null) && (select.GroupBy.Count > 0);
                bool flag4 = (isOuterMostSelect || (select.Take != null)) || (select.Skip != null);
                bool flag5 = ((flag4 && !flag3) && !select.IsDistinct) && !AggregateChecker.HasAggregates(select);
                if (flag2)
                {
                    this.PrependOrderings(select.OrderBy);
                }
                if (select.IsReverse)
                {
                    this.ReverseOrderings();
                }
                IEnumerable <OrderExpression> orderBy = null;
                if (flag5)
                {
                    orderBy = this.gatheredOrderings;
                }
                else if (flag4)
                {
                    orderBy = select.OrderBy;
                }
                bool flag6 = (!isOuterMostSelect && !flag3) && !select.IsDistinct;
                ReadOnlyCollection <ColumnDeclaration> columns = select.Columns;
                if (this.gatheredOrderings != null)
                {
                    if (flag6)
                    {
                        HashSet <TableAlias> existingAliases = DeclaredAliasGatherer.Gather(select.From);
                        BindResult           result          = this.RebindOrderings(this.gatheredOrderings, select.Alias, existingAliases, select.Columns);
                        this.gatheredOrderings = null;
                        this.PrependOrderings(result.Orderings);
                        columns = result.Columns;
                    }
                    else
                    {
                        this.gatheredOrderings = null;
                    }
                }
                if (((orderBy != select.OrderBy) || (columns != select.Columns)) || select.IsReverse)
                {
                    select = new SelectExpression(select.Alias, columns, select.From, select.Where, orderBy, select.GroupBy, select.IsDistinct, select.Skip, select.Take, false);
                }
                expression = select;
            }
            finally
            {
                this.isOuterMostSelect = isOuterMostSelect;
            }
            return(expression);
        }
 protected override Expression VisitJoin(JoinExpression join)
 {
     join = (JoinExpression)base.VisitJoin(join);
     if ((join.Join == JoinType.CrossApply) || (join.Join == JoinType.OuterApply))
     {
         if (join.Right is TableExpression)
         {
             return(new JoinExpression(JoinType.CrossJoin, join.Left, join.Right, null));
         }
         SelectExpression right = join.Right as SelectExpression;
         if ((((right != null) && (right.Take == null)) && ((right.Skip == null) && !AggregateChecker.HasAggregates(right))) && ((right.GroupBy == null) || (right.GroupBy.Count == 0)))
         {
             SelectExpression     source = right.SetWhere(null);
             HashSet <TableAlias> set    = ReferencedAliasGatherer.Gather(source);
             HashSet <TableAlias> other  = DeclaredAliasGatherer.Gather(join.Left);
             set.IntersectWith(other);
             if (set.Count == 0)
             {
                 Expression where = right.Where;
                 right            = source;
                 ProjectedColumns columns = ColumnProjector.ProjectColumns(this.language, where, right.Columns, right.Alias, DeclaredAliasGatherer.Gather(right.From));
                 right = right.SetColumns(columns.Columns);
                 where = columns.Projector;
                 return(new JoinExpression((where == null) ? JoinType.CrossJoin : ((join.Join == JoinType.CrossApply) ? JoinType.InnerJoin : JoinType.LeftOuter), join.Left, right, where));
             }
         }
     }
     return(join);
 }
 private bool CanJoinOnClient(SelectExpression select)
 {
     return((((this.canJoinOnClient && (this.currentMember != null)) && (!this.policy.IsDeferLoaded(this.currentMember) && !select.IsDistinct)) && ((select.GroupBy == null) || (select.GroupBy.Count == 0))) && !AggregateChecker.HasAggregates(select));
 }
 private bool CanJoinOnServer(SelectExpression select)
 {
     return((!select.IsDistinct && ((select.GroupBy == null) || (select.GroupBy.Count == 0))) && !AggregateChecker.HasAggregates(select));
 }