Example #1
0
        // requires: children to be a list of nodes that are children of an
        // Inner Join node. slotNum does not correspond to the key slot
        // effects: Determines the child number from which the slot should be
        // picked up.
        private static int GetInnerJoinChildForSlot(List <CqlBlock> children, int slotNum)
        {
            // Picks the child with the non-constant slot first. If none, picks a non-null constant slot.
            // If not een that, picks any one
            int result = -1;

            for (int i = 0; i < children.Count; i++)
            {
                CqlBlock child = children[i];
                if (false == child.IsProjected(slotNum))
                {
                    continue;
                }
                ProjectedSlot         slot         = child.SlotValue(slotNum);
                ConstantProjectedSlot constantSlot = slot as ConstantProjectedSlot;
                MemberProjectedSlot   joinSlot     = slot as MemberProjectedSlot;
                if (joinSlot != null)
                { // Pick the non-constant slot
                    result = i;
                }
                else if (constantSlot != null && constantSlot.CellConstant.IsNull())
                {
                    if (result == -1)
                    { // In case, all are null
                        result = i;
                    }
                }
                else
                {
                    // Just pick anything
                    result = i;
                }
            }
            return(result);
        }
Example #2
0
        // requires: caseForOuterJoins corresponds the slot "slotNum"
        // effects: Adds a WhenThen corresponding to child to caseForOuterJoins.
        private void AddCaseForOuterJoins(CaseStatement caseForOuterJoins, CqlBlock child, int slotNum, CqlIdentifiers identifiers)
        {
            // Determine the cells that the slot comes from
            // and make an OR expression, e.g., WHEN _from0 or _from2 or ... THEN child[slotNum]

            ProjectedSlot         childSlot    = child.SlotValue(slotNum);
            ConstantProjectedSlot constantSlot = childSlot as ConstantProjectedSlot;

            if (constantSlot != null && constantSlot.CellConstant.IsNull())
            {
                // NULL being generated by a child - don't need to project
                return;
            }

            BoolExpression originBool = BoolExpression.False;

            for (int i = 0; i < NumBoolSlots; i++)
            {
                int boolSlotNum = BoolIndexToSlot(i);
                if (child.IsProjected(boolSlotNum))
                {
                    // OR it to the expression
                    QualifiedCellIdBoolean boolExpr = new QualifiedCellIdBoolean(child, identifiers, i);
                    originBool = BoolExpression.CreateOr(originBool, BoolExpression.CreateLiteral(boolExpr, RightDomainMap));
                }
            }
            // Qualify the slotNum with the child.CqlAlias for the THEN
            QualifiedSlot slot = child.QualifySlotWithBlockAlias(slotNum);

            caseForOuterJoins.AddWhenThen(originBool, slot);
        }
Example #3
0
        private SlotInfo[] CreateSlotInfosForCaseStatement(
            bool[] parentRequiredSlots,
            int foundSlot,
            CqlBlock childBlock,
            CaseStatement thisCaseStatement,
            IEnumerable <WithRelationship> withRelationships)
        {
            int num = childBlock.Slots.Count - this.TotalSlots;

            SlotInfo[] slotInfoArray = new SlotInfo[this.TotalSlots + num];
            for (int slotNum = 0; slotNum < this.TotalSlots; ++slotNum)
            {
                bool          isProjected        = childBlock.IsProjected(slotNum);
                bool          parentRequiredSlot = parentRequiredSlots[slotNum];
                ProjectedSlot slotValue          = childBlock.SlotValue(slotNum);
                MemberPath    outputMemberPath   = this.GetOutputMemberPath(slotNum);
                if (slotNum == foundSlot)
                {
                    slotValue   = (ProjectedSlot) new CaseStatementProjectedSlot(thisCaseStatement.DeepQualify(childBlock), withRelationships);
                    isProjected = true;
                }
                else if (isProjected && parentRequiredSlot)
                {
                    slotValue = (ProjectedSlot)childBlock.QualifySlotWithBlockAlias(slotNum);
                }
                SlotInfo slotInfo = new SlotInfo(parentRequiredSlot && isProjected, isProjected, slotValue, outputMemberPath);
                slotInfoArray[slotNum] = slotInfo;
            }
            for (int totalSlots = this.TotalSlots; totalSlots < this.TotalSlots + num; ++totalSlots)
            {
                QualifiedSlot qualifiedSlot = childBlock.QualifySlotWithBlockAlias(totalSlots);
                slotInfoArray[totalSlots] = new SlotInfo(true, true, (ProjectedSlot)qualifiedSlot, childBlock.MemberPath(totalSlots));
            }
            return(slotInfoArray);
        }
        private void AddCaseForOuterJoins(
            CaseStatement caseForOuterJoins,
            CqlBlock child,
            int slotNum,
            CqlIdentifiers identifiers)
        {
            ConstantProjectedSlot constantProjectedSlot = child.SlotValue(slotNum) as ConstantProjectedSlot;

            if (constantProjectedSlot != null && constantProjectedSlot.CellConstant.IsNull())
            {
                return;
            }
            BoolExpression or = BoolExpression.False;

            for (int index = 0; index < this.NumBoolSlots; ++index)
            {
                int slot = this.BoolIndexToSlot(index);
                if (child.IsProjected(slot))
                {
                    QualifiedCellIdBoolean qualifiedCellIdBoolean = new QualifiedCellIdBoolean(child, identifiers, index);
                    or = BoolExpression.CreateOr(or, BoolExpression.CreateLiteral((BoolLiteral)qualifiedCellIdBoolean, this.RightDomainMap));
                }
            }
            QualifiedSlot qualifiedSlot = child.QualifySlotWithBlockAlias(slotNum);

            caseForOuterJoins.AddWhenThen(or, (ProjectedSlot)qualifiedSlot);
        }
        private static int GetInnerJoinChildForSlot(List <CqlBlock> children, int slotNum)
        {
            int num = -1;

            for (int index = 0; index < children.Count; ++index)
            {
                CqlBlock child = children[index];
                if (child.IsProjected(slotNum))
                {
                    ProjectedSlot         projectedSlot         = child.SlotValue(slotNum);
                    ConstantProjectedSlot constantProjectedSlot = projectedSlot as ConstantProjectedSlot;
                    if (projectedSlot is MemberProjectedSlot)
                    {
                        num = index;
                    }
                    else if (constantProjectedSlot != null && constantProjectedSlot.CellConstant.IsNull())
                    {
                        if (num == -1)
                        {
                            num = index;
                        }
                    }
                    else
                    {
                        num = index;
                    }
                }
            }
            return(num);
        }
        private CqlBlock JoinToCqlBlock(
            bool[] requiredSlots,
            CqlIdentifiers identifiers,
            ref int blockAliasNum,
            ref List <WithRelationship> withRelationships)
        {
            int             length1  = requiredSlots.Length;
            List <CqlBlock> children = new List <CqlBlock>();
            List <Tuple <QualifiedSlot, MemberPath> > tupleList = new List <Tuple <QualifiedSlot, MemberPath> >();

            foreach (CellTreeNode child in this.Children)
            {
                bool[] projectedSlots = child.GetProjectedSlots();
                OpCellTreeNode.AndWith(projectedSlots, requiredSlots);
                CqlBlock cqlBlock = child.ToCqlBlock(projectedSlots, identifiers, ref blockAliasNum, ref withRelationships);
                children.Add(cqlBlock);
                for (int length2 = projectedSlots.Length; length2 < cqlBlock.Slots.Count; ++length2)
                {
                    tupleList.Add(Tuple.Create <QualifiedSlot, MemberPath>(cqlBlock.QualifySlotWithBlockAlias(length2), cqlBlock.MemberPath(length2)));
                }
            }
            SlotInfo[] slotInfos = new SlotInfo[length1 + tupleList.Count];
            for (int slotNum = 0; slotNum < length1; ++slotNum)
            {
                SlotInfo joinSlotInfo = this.GetJoinSlotInfo(this.OpType, requiredSlots[slotNum], children, slotNum, identifiers);
                slotInfos[slotNum] = joinSlotInfo;
            }
            int index1 = 0;
            int index2 = length1;

            while (index2 < length1 + tupleList.Count)
            {
                slotInfos[index2] = new SlotInfo(true, true, (ProjectedSlot)tupleList[index1].Item1, tupleList[index1].Item2);
                ++index2;
                ++index1;
            }
            List <JoinCqlBlock.OnClause> onClauses = new List <JoinCqlBlock.OnClause>();

            for (int index3 = 1; index3 < children.Count; ++index3)
            {
                CqlBlock cqlBlock = children[index3];
                JoinCqlBlock.OnClause onClause = new JoinCqlBlock.OnClause();
                foreach (int keySlot in this.KeySlots)
                {
                    if (!this.ViewgenContext.Config.IsValidationEnabled && (!cqlBlock.IsProjected(keySlot) || !children[0].IsProjected(keySlot)))
                    {
                        ErrorLog errorLog = new ErrorLog();
                        errorLog.AddEntry(new ErrorLog.Record(ViewGenErrorCode.NoJoinKeyOrFKProvidedInMapping, Strings.Viewgen_NoJoinKeyOrFK, (IEnumerable <LeftCellWrapper>) this.ViewgenContext.AllWrappersForExtent, string.Empty));
                        ExceptionHelpers.ThrowMappingException(errorLog, this.ViewgenContext.Config);
                    }
                    QualifiedSlot leftSlot     = children[0].QualifySlotWithBlockAlias(keySlot);
                    QualifiedSlot rightSlot    = cqlBlock.QualifySlotWithBlockAlias(keySlot);
                    MemberPath    outputMember = slotInfos[keySlot].OutputMember;
                    onClause.Add(leftSlot, outputMember, rightSlot, outputMember);
                }
                onClauses.Add(onClause);
            }
            return((CqlBlock) new JoinCqlBlock(this.OpType, slotInfos, children, onClauses, identifiers, ++blockAliasNum));
        }
        private SlotInfo GetJoinSlotInfo(
            CellTreeOpType opType,
            bool isRequiredSlot,
            List <CqlBlock> children,
            int slotNum,
            CqlIdentifiers identifiers)
        {
            if (!isRequiredSlot)
            {
                return(new SlotInfo(false, false, (ProjectedSlot)null, this.GetMemberPath(slotNum)));
            }
            int           index1        = -1;
            CaseStatement caseStatement = (CaseStatement)null;

            for (int index2 = 0; index2 < children.Count; ++index2)
            {
                CqlBlock child = children[index2];
                if (child.IsProjected(slotNum))
                {
                    if (this.IsKeySlot(slotNum))
                    {
                        index1 = index2;
                        break;
                    }
                    if (opType == CellTreeOpType.IJ)
                    {
                        index1 = OpCellTreeNode.GetInnerJoinChildForSlot(children, slotNum);
                        break;
                    }
                    if (index1 != -1)
                    {
                        if (caseStatement == null)
                        {
                            caseStatement = new CaseStatement(this.GetMemberPath(slotNum));
                            this.AddCaseForOuterJoins(caseStatement, children[index1], slotNum, identifiers);
                        }
                        this.AddCaseForOuterJoins(caseStatement, child, slotNum, identifiers);
                    }
                    index1 = index2;
                }
            }
            MemberPath    memberPath = this.GetMemberPath(slotNum);
            ProjectedSlot slotValue;

            if (caseStatement != null && (caseStatement.Clauses.Count > 0 || caseStatement.ElseValue != null))
            {
                caseStatement.Simplify();
                slotValue = (ProjectedSlot) new CaseStatementProjectedSlot(caseStatement, (IEnumerable <WithRelationship>)null);
            }
            else
            {
                slotValue = index1 < 0 ? (!this.IsBoolSlot(slotNum) ? (ProjectedSlot) new ConstantProjectedSlot(Domain.GetDefaultValueForMemberPath(memberPath, (IEnumerable <LeftCellWrapper>) this.GetLeaves(), this.ViewgenContext.Config)) : (ProjectedSlot) new BooleanProjectedSlot(BoolExpression.False, identifiers, this.SlotToBoolIndex(slotNum))) : (ProjectedSlot)children[index1].QualifySlotWithBlockAlias(slotNum);
            }
            bool enforceNotNull = this.IsBoolSlot(slotNum) && (opType == CellTreeOpType.LOJ && index1 > 0 || opType == CellTreeOpType.FOJ);

            return(new SlotInfo(true, true, slotValue, memberPath, enforceNotNull));
        }
Example #8
0
        // <summary>
        // Given the slot (<paramref name="foundSlot" />) and its corresponding case statement (
        // <paramref
        //     name="thisCaseStatement" />
        // ),
        // generates the slotinfos for the cql block producing the case statement.
        // </summary>
        private SlotInfo[] CreateSlotInfosForCaseStatement(
            bool[] parentRequiredSlots,
            int foundSlot,
            CqlBlock childBlock,
            CaseStatement thisCaseStatement,
            IEnumerable <WithRelationship> withRelationships)
        {
            var numSlotsAddedByChildBlock = childBlock.Slots.Count - TotalSlots;
            var slotInfos = new SlotInfo[TotalSlots + numSlotsAddedByChildBlock];

            for (var slotNum = 0; slotNum < TotalSlots; slotNum++)
            {
                var isProjected        = childBlock.IsProjected(slotNum);
                var isRequiredByParent = parentRequiredSlots[slotNum];
                var slot         = childBlock.SlotValue(slotNum);
                var outputMember = GetOutputMemberPath(slotNum);
                if (slotNum == foundSlot)
                {
                    // We need a case statement instead for this slot that we
                    // are handling right now
                    Debug.Assert(isRequiredByParent, "Case result not needed by parent");

                    // Get a case statement with all slots replaced by aliases slots
                    var newCaseStatement = thisCaseStatement.DeepQualify(childBlock);
                    slot        = new CaseStatementProjectedSlot(newCaseStatement, withRelationships);
                    isProjected = true; // We are projecting this slot now
                }
                else if (isProjected && isRequiredByParent)
                {
                    // We only alias something that is needed and is being projected by the child.
                    // It is a qualified slot into the child block.
                    slot = childBlock.QualifySlotWithBlockAlias(slotNum);
                }
                // For slots, if it is not required by the parent, we want to
                // set the isRequiredByParent for this slot to be
                // false. Furthermore, we do not want to introduce any "NULL
                // AS something" at this stage for slots not being
                // projected. So if the child does not project that slot, we
                // declare it as not being required by the parent (if such a
                // NULL was needed, it would have been pushed all the way
                // down to a non-case block.
                // Essentially, from a Case statement's parent perspective,
                // it is saying "If you can produce a slot either by yourself
                // or your children, please do. Otherwise, do not concoct anything"
                var slotInfo = new SlotInfo(isRequiredByParent && isProjected, isProjected, slot, outputMember);
                slotInfos[slotNum] = slotInfo;
            }
            for (var i = TotalSlots; i < TotalSlots + numSlotsAddedByChildBlock; i++)
            {
                var childAddedSlot = childBlock.QualifySlotWithBlockAlias(i);
                slotInfos[i] = new SlotInfo(true, true, childAddedSlot, childBlock.MemberPath(i));
            }
            return(slotInfos);
        }
Example #9
0
        // effects: Generates a SlotInfo object for a slot of a join node. It
        // uses the type of the join operation (opType), whether the slot is
        // required by the parent or not (isRequiredSlot), the children of
        // this node (children) and the number of the slotNum
        private SlotInfo GetJoinSlotInfo(CellTreeOpType opType, bool isRequiredSlot,
                                         List <CqlBlock> children, int slotNum, CqlIdentifiers identifiers)
        {
            if (false == isRequiredSlot)
            {
                // The slot will not be used. So we can set the projected slot to be null
                SlotInfo unrequiredSlotInfo = new SlotInfo(false, false, null, GetMemberPath(slotNum));
                return(unrequiredSlotInfo);
            }

            // For a required slot, determine the child who is contributing to this value
            int           childDefiningSlot = -1;
            CaseStatement caseForOuterJoins = null;

            for (int childNum = 0; childNum < children.Count; childNum++)
            {
                CqlBlock child = children[childNum];
                if (false == child.IsProjected(slotNum))
                {
                    continue;
                }
                // For keys, we can pick any child block. So the first
                // one that we find is fine as well
                if (IsKeySlot(slotNum))
                {
                    childDefiningSlot = childNum;
                    break;
                }
                else if (opType == CellTreeOpType.IJ)
                {
                    // For Inner Joins, most of the time, the entries will be
                    // the same in all the children. However, in some cases,
                    // we will end up with NULL in one child and an actual
                    // value in another -- we should pick up the actual value in that case
                    childDefiningSlot = GetInnerJoinChildForSlot(children, slotNum);
                    break;
                }
                else
                {
                    // For LOJs, we generate a case statement if more than
                    // one child generates the value - until then we do not
                    // create the caseForOuterJoins object
                    if (childDefiningSlot != -1)
                    {
                        // We really need a case statement now
                        // We have the value being generated by another child
                        // We need to fetch the variable from the appropriate child
                        Debug.Assert(false == IsBoolSlot(slotNum), "Boolean slots cannot come from two children");
                        if (caseForOuterJoins == null)
                        {
                            MemberPath outputMember = GetMemberPath(slotNum);
                            caseForOuterJoins = new CaseStatement(outputMember);
                            // Add the child that we had not added in the first shot
                            AddCaseForOuterJoins(caseForOuterJoins, children[childDefiningSlot], slotNum, identifiers);
                        }
                        AddCaseForOuterJoins(caseForOuterJoins, child, slotNum, identifiers);
                    }
                    childDefiningSlot = childNum;
                }
            }

            MemberPath    memberPath = GetMemberPath(slotNum);
            ProjectedSlot slot       = null;

            // Generate the slot value -- case statement slot, or a qualified slot or null or false.
            // If case statement slot has nothing, treat it as null/empty.
            if (caseForOuterJoins != null && (caseForOuterJoins.Clauses.Count > 0 || caseForOuterJoins.ElseValue != null))
            {
                caseForOuterJoins.Simplify();
                slot = new CaseStatementProjectedSlot(caseForOuterJoins, null);
            }
            else if (childDefiningSlot >= 0)
            {
                slot = children[childDefiningSlot].QualifySlotWithBlockAlias(slotNum);
            }
            else
            {
                // need to produce output slot, but don't have a value
                // output NULL for fields or False for bools
                if (IsBoolSlot(slotNum))
                {
                    slot = new BooleanProjectedSlot(BoolExpression.False, identifiers, SlotToBoolIndex(slotNum));
                }
                else
                {
                    slot = new ConstantProjectedSlot(Domain.GetDefaultValueForMemberPath(memberPath, GetLeaves(), ViewgenContext.Config), memberPath);
                }
            }

            // We need to ensure that _from variables are never null since
            // view generation uses 2-valued boolean logic.
            // They can become null in outer joins. We compensate for it by
            // adding AND NOT NULL condition on boolean slots coming from outer joins.
            bool enforceNotNull = IsBoolSlot(slotNum) &&
                                  ((opType == CellTreeOpType.LOJ && childDefiningSlot > 0) ||
                                   opType == CellTreeOpType.FOJ);
            // We set isProjected to be true since we have come up with some value for it
            SlotInfo slotInfo = new SlotInfo(true, true, slot, memberPath, enforceNotNull);

            return(slotInfo);
        }
Example #10
0
        // requires: node corresponds to an IJ, LOJ, FOJ node
        // effects: Given a union node and the slots required by the parent,
        // generates a CqlBlock for the subtree rooted at node
        private CqlBlock JoinToCqlBlock(bool[] requiredSlots, CqlIdentifiers identifiers, ref int blockAliasNum, ref List <WithRelationship> withRelationships)
        {
            int totalSlots = requiredSlots.Length;

            Debug.Assert(OpType == CellTreeOpType.IJ ||
                         OpType == CellTreeOpType.LOJ ||
                         OpType == CellTreeOpType.FOJ, "Only these join operations handled");

            List <CqlBlock> children = new List <CqlBlock>();
            List <Tuple <QualifiedSlot, MemberPath> > additionalChildSlots = new List <Tuple <QualifiedSlot, MemberPath> >();

            // First get the children nodes (FROM part)
            foreach (CellTreeNode child in Children)
            {
                // Determine the slots that are projected by this child.
                // These are the required slots as well - unlike Union, we do not need the child to project any extra nulls.
                bool[] childProjectedSlots = child.GetProjectedSlots();
                AndWith(childProjectedSlots, requiredSlots);
                CqlBlock childBlock = child.ToCqlBlock(childProjectedSlots, identifiers, ref blockAliasNum, ref withRelationships);
                children.Add(childBlock);
                for (int qualifiedSlotNumber = childProjectedSlots.Length; qualifiedSlotNumber < childBlock.Slots.Count; qualifiedSlotNumber++)
                {
                    additionalChildSlots.Add(Tuple.Create(childBlock.QualifySlotWithBlockAlias(qualifiedSlotNumber), childBlock.MemberPath(qualifiedSlotNumber)));
                }
                Debug.Assert(totalSlots == child.NumBoolSlots + child.NumProjectedSlots,
                             "Number of required slots is different from what each node in the tree has?");
            }

            // Now get the slots that are projected out by this node (SELECT part)
            SlotInfo[] slotInfos = new SlotInfo[totalSlots + additionalChildSlots.Count];
            for (int slotNum = 0; slotNum < totalSlots; slotNum++)
            {
                // Note: this call could create a CaseStatementSlot (i.e., slotInfo.SlotValue is CaseStatementSlot)
                // which uses "from" booleans that need to be projected by children
                SlotInfo slotInfo = GetJoinSlotInfo(OpType, requiredSlots[slotNum], children, slotNum, identifiers);
                slotInfos[slotNum] = slotInfo;
            }

            for (int i = 0, slotNum = totalSlots; slotNum < totalSlots + additionalChildSlots.Count; slotNum++, i++)
            {
                slotInfos[slotNum] = new SlotInfo(true, true, additionalChildSlots[i].Item1, additionalChildSlots[i].Item2);
            }

            // Generate the ON conditions: For each child, generate an ON
            // clause with the 0th child on the key fields
            List <JoinCqlBlock.OnClause> onClauses = new List <JoinCqlBlock.OnClause>();

            for (int i = 1; i < children.Count; i++)
            {
                CqlBlock child = children[i];
                JoinCqlBlock.OnClause onClause = new JoinCqlBlock.OnClause();
                foreach (int keySlotNum in this.KeySlots)
                {
                    if (ViewgenContext.Config.IsValidationEnabled)
                    {
                        Debug.Assert(children[0].IsProjected(keySlotNum), "Key is not in 0th child");
                        Debug.Assert(child.IsProjected(keySlotNum), "Key is not in child");
                    }
                    else
                    {
                        if (!child.IsProjected(keySlotNum) || !children[0].IsProjected(keySlotNum))
                        {
                            ErrorLog errorLog = new ErrorLog();
                            errorLog.AddEntry(new ErrorLog.Record(true, ViewGenErrorCode.NoJoinKeyOrFKProvidedInMapping,
                                                                  Strings.Viewgen_NoJoinKeyOrFK, ViewgenContext.AllWrappersForExtent, String.Empty));
                            ExceptionHelpers.ThrowMappingException(errorLog, ViewgenContext.Config);
                        }
                    }
                    var firstSlot    = children[0].QualifySlotWithBlockAlias(keySlotNum);
                    var secondSlot   = child.QualifySlotWithBlockAlias(keySlotNum);
                    var outputMember = slotInfos[keySlotNum].OutputMember;
                    onClause.Add(firstSlot, outputMember, secondSlot, outputMember);
                }
                onClauses.Add(onClause);
            }

            CqlBlock result = new JoinCqlBlock(OpType, slotInfos, children, onClauses, identifiers, ++blockAliasNum);

            return(result);
        }