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 = RedundantSubqueryRemover.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); } // these are distinct operations if (select.IsReverse || fromSelect.IsReverse) { 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 || selHasJoin)) { return(false); } // cannot move forward a skip if outer has skip or distinct if (fromSelect.Skip != null && (select.Skip != null || select.IsDistinct || selHasAggregates || selHasGroupBy || selHasJoin)) { 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) || selHasJoin)) { return(false); } if (frmHasAggregates && (select.Take != null || select.Skip != null || select.IsDistinct || selHasAggregates || selHasGroupBy || selHasJoin)) { return(false); } return(true); }
private static bool IsRedudantSubquery(SelectExpression select) { return((RedundantSubqueryRemover.IsSimpleProjection(select) || RedundantSubqueryRemover.IsNameMapProjection(select)) && !select.IsDistinct && !select.IsReverse && select.Take == null && select.Skip == null && select.Where == null && (select.OrderBy == null || select.OrderBy.Count == 0) && (select.GroupBy == null || select.GroupBy.Count == 0)); }
public static Expression Remove(Expression expression) { expression = new RedundantSubqueryRemover().Visit(expression); expression = SubqueryMerger.Merge(expression); return(expression); }