// modifies usedViews - removes all redundant views from it
        internal static bool TrySimplifyJoinRewriting(ref T_Tile rewriting, T_Tile toAvoid, Dictionary <T_Tile, TileOpKind> usedViews, RewritingProcessor <T_Tile> qp)
        {
            RewritingSimplifier <T_Tile> simplifier = new RewritingSimplifier <T_Tile>(rewriting, toAvoid, usedViews, qp);
            T_Tile simplifiedRewriting;

            if (simplifier.SimplifyRewriting(out simplifiedRewriting))
            {
                rewriting = simplifiedRewriting;
                return(true);
            }
            return(false);
        }
Example #2
0
        public static bool RewriteQuery(T_Tile toFill, T_Tile toAvoid, out T_Tile rewriting, List <T_Tile> views, RewritingProcessor <T_Tile> qp)
        {
            RewritingPass <T_Tile> rewritingPass = new RewritingPass <T_Tile>(toFill, toAvoid, views, qp);

            if (rewritingPass.RewriteQuery(out rewriting))
            {
                RewritingSimplifier <T_Tile> .TrySimplifyUnionRewriting(ref rewriting, toFill, toAvoid, qp);

                return(true);
            }
            return(false);
        }
        // called for top query only
        internal static bool TrySimplifyUnionRewriting(ref T_Tile rewriting, T_Tile toFill, T_Tile toAvoid, RewritingProcessor <T_Tile> qp)
        {
            RewritingSimplifier <T_Tile> simplifier = new RewritingSimplifier <T_Tile>(rewriting, toFill, toAvoid, qp);
            // gather all unioned subqueries
            T_Tile simplifiedRewriting;

            if (simplifier.SimplifyRewriting(out simplifiedRewriting))
            {
                rewriting = simplifiedRewriting;
                return(true);
            }
            return(false);
        }
Example #4
0
        private bool RewriteQuery(out T_Tile rewriting)
        {
            rewriting = m_toFill;
            T_Tile rewritingSoFar;

            if (false == FindRewritingByIncludedAndDisjoint(out rewritingSoFar))
            {
                if (false == FindContributingView(out rewritingSoFar))
                {
                    return(false);
                }
            }

            bool hasExtraTuples = !m_qp.IsDisjointFrom(rewritingSoFar, m_toAvoid);

            // try to cut off extra tuples using joins
            if (hasExtraTuples)
            {
                foreach (T_Tile view in AvailableViews)
                {
                    if (TryJoin(view, ref rewritingSoFar))
                    {
                        hasExtraTuples = false;
                        break;
                    }
                }
            }

            // try to cut off extra tuples using anti-semijoins
            if (hasExtraTuples)
            {
                foreach (T_Tile view in AvailableViews)
                {
                    if (TryAntiSemiJoin(view, ref rewritingSoFar))
                    {
                        hasExtraTuples = false;
                        break;
                    }
                }
            }

            if (hasExtraTuples)
            {
                return(false); // won't be able to cut off extra tuples
            }

            // remove redundant joins and anti-semijoins
            RewritingSimplifier <T_Tile> .TrySimplifyJoinRewriting(ref rewritingSoFar, m_toAvoid, m_usedViews, m_qp);

            // find rewriting for missing tuples, if any
            T_Tile missingTuples = m_qp.AntiSemiJoin(m_toFill, rewritingSoFar);

            if (!m_qp.IsEmpty(missingTuples))
            {
                T_Tile rewritingForMissingTuples;
                if (false == RewritingPass <T_Tile> .RewriteQueryInternal(missingTuples, m_toAvoid, out rewritingForMissingTuples, m_views, new HashSet <T_Tile>(m_usedViews.Keys), m_qp))
                {
                    rewriting = rewritingForMissingTuples;
                    return(false); // failure
                }
                else
                {
                    // Although a more general optimization for UNIONs will handle this case,
                    // adding this check reduces the overall number of containment tests
                    if (m_qp.IsContainedIn(rewritingSoFar, rewritingForMissingTuples))
                    {
                        rewritingSoFar = rewritingForMissingTuples;
                    }
                    else
                    {
                        rewritingSoFar = m_qp.Union(rewritingSoFar, rewritingForMissingTuples);
                    }
                }
            }

            // if we reached this point, we have a successful rewriting
            rewriting = rewritingSoFar;
            return(true);
        }