Ejemplo n.º 1
0
        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 fromSelect = GetLeftMostSelect(select.From);

                if (fromSelect == null)
                {
                    return(false);
                }
                //if (!IsColumnProjection(fromSelect)) return false;

                bool selHasNameMapProjection = IsNameMapProjection(select);
                bool selHasOrderBy           = select.OrderBy != null && select.OrderBy.Count > 0;
                bool selHasGroupBy           = select.GroupBy != null && select.GroupBy.Count > 0;
                bool selHasAggregates        = AggregateChecker.HasAggregates(select);
                bool selHasJoin       = select.From is JoinExpression;
                bool frmHasOrderBy    = fromSelect.OrderBy != null && fromSelect.OrderBy.Count > 0;
                bool frmHasGroupBy    = fromSelect.GroupBy != null && fromSelect.GroupBy.Count > 0;
                bool frmHasAggregates = AggregateChecker.HasAggregates(fromSelect);

                // both cannot have orderby
                if (selHasOrderBy && frmHasOrderBy)
                {
                    return(false);
                }
                // both cannot have groupby
                if (selHasGroupBy && frmHasGroupBy)
                {
                    return(false);
                }
                // cannot move forward order-by if outer has group-by
                if (frmHasOrderBy && (selHasGroupBy || selHasAggregates || select.IsDistinct))
                {
                    return(false);
                }
                // cannot move forward a limit or an offset if outer has distinct
                if (fromSelect.Limit != null && fromSelect.Offset != null && (select.IsDistinct || selHasAggregates || selHasGroupBy || selHasJoin))
                {
                    return(false);
                }
                // cannot move forward a distinct if outer has groupby or a different projection
                if (fromSelect.IsDistinct && (!selHasNameMapProjection || selHasGroupBy || selHasAggregates || (selHasOrderBy && !isTopLevel) || selHasJoin))
                {
                    return(false);
                }
                if (frmHasAggregates && (select.IsDistinct || selHasAggregates || selHasGroupBy || selHasJoin))
                {
                    return(false);
                }

                return(true);
            }