Ejemplo n.º 1
0
        internal static bool HasAggregates(DbSelectExpression expression)
        {
            AggregateChecker checker = new AggregateChecker();

            checker.Visit(expression);
            return(checker.hasAggregate);
        }
 private bool CanJoinOnClient(DbSelectExpression select)
 {
     // can add singleton (1:0,1) join if no grouping/aggregates or distinct
     return(!select.IsDistinct &&
            (select.GroupBy == null || select.GroupBy.Count == 0) &&
            !AggregateChecker.HasAggregates(select));
 }
Ejemplo n.º 3
0
            private static bool CanMergeWithFrom(DbSelectExpression select, bool isTopLevel)
            {
                DbSelectExpression 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 frmHasOrderBy           = fromSelect.OrderBy != null && fromSelect.OrderBy.Count > 0;
                bool frmHasGroupBy           = fromSelect.GroupBy != null && fromSelect.GroupBy.Count > 0;

                // 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 group-by if outer has where clause
                if (frmHasGroupBy /*&& (select.Where != null)*/) // need to assert projection is the same in order to move group-by forward
                {
                    return(false);
                }
                // cannot move forward a take if outer has take or skip or distinct
                if (fromSelect.Take != null && (select.Take != null || select.Skip != null || select.IsDistinct || selHasAggregates || selHasGroupBy))
                {
                    return(false);
                }
                // cannot move forward a skip if outer has skip or distinct
                if (fromSelect.Skip != null && (select.Skip != null || select.IsDistinct || selHasAggregates || selHasGroupBy))
                {
                    return(false);
                }
                // cannot move forward a distinct if outer has take, skip, groupby or a different projection
                if (fromSelect.IsDistinct && (select.Take != null || select.Skip != null || !selHasNameMapProjection ||
                                              selHasGroupBy || selHasAggregates || (selHasOrderBy && !isTopLevel)))
                {
                    return(false);
                }
                return(true);
            }
Ejemplo n.º 4
0
        protected override Expression VisitJoin(DbJoinExpression join)
        {
            join = (DbJoinExpression)base.VisitJoin(join);

            if (join.Join == DbJoinType.CrossApply || join.Join == DbJoinType.OuterApply)
            {
                if (join.Right is DbTableExpression)
                {
                    return(new DbJoinExpression(DbJoinType.CrossJoin, join.Left, join.Right, null));
                }
                else
                {
                    DbSelectExpression select = join.Right as DbSelectExpression;
                    // Only consider rewriting cross apply if
                    //   1) right side is a select
                    //   2) other than in the where clause in the right-side select, no left-side declared aliases are referenced
                    //   3) and has no behavior that would change semantics if the where clause is removed (like groups, aggregates, take, skip, etc).
                    // Note: it is best to attempt this after redundant subqueries have been removed.
                    if (select != null &&
                        select.Take == null &&
                        select.Skip == null &&
                        !AggregateChecker.HasAggregates(select) &&
                        (select.GroupBy == null || select.GroupBy.Count == 0))
                    {
                        DbSelectExpression     selectWithoutWhere = select.SetWhere(null);
                        HashSet <DbTableAlias> referencedAliases  = ReferencedAliasGatherer.Gather(selectWithoutWhere);
                        HashSet <DbTableAlias> declaredAliases    = DeclaredAliasGatherer.Gather(join.Left);
                        referencedAliases.IntersectWith(declaredAliases);
                        if (referencedAliases.Count == 0)
                        {
                            Expression where = select.Where;
                            select           = selectWithoutWhere;
                            var pc = ColumnProjector.ProjectColumns(this.CanBeColumn, where, select.Columns, select.Alias, DeclaredAliasGatherer.Gather(select.From));
                            select = select.SetColumns(pc.Columns);
                            where  = pc.Projector;
                            DbJoinType jt = (where == null) ? DbJoinType.CrossJoin :
                                            (join.Join == DbJoinType.CrossApply ? DbJoinType.InnerJoin : DbJoinType.LeftOuter);
                            return(new DbJoinExpression(jt, join.Left, select, where));
                        }
                    }
                }
            }

            return(join);
        }
 internal static bool HasAggregates(DbSelectExpression expression)
 {
     AggregateChecker checker = new AggregateChecker();
     checker.Visit(expression);
     return checker.hasAggregate;
 }