// 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); }
// 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); }
protected override bool IsEqualTo(ProjectedSlot right) { ConstantProjectedSlot rightSlot = right as ConstantProjectedSlot; if (rightSlot == null) { return(false); } return(Constant.EqualityComparer.Equals(m_constant, rightSlot.m_constant)); }
/// <summary> /// Simplifies the <see cref="CaseStatement"/> so that unnecessary WHEN/THENs for nulls/undefined values are eliminated. /// Also, adds an ELSE clause if possible. /// </summary> internal void Simplify() { if (m_simplified) { return; } List <CaseStatement.WhenThen> clauses = new List <CaseStatement.WhenThen>(); // remove all WHEN clauses where the value gets set to "undefined" // We eliminate the last clause for now - we could determine the // "most complicated" WHEN clause and eliminate it bool eliminatedNullClauses = false; foreach (WhenThen clause in m_clauses) { ConstantProjectedSlot constantSlot = clause.Value as ConstantProjectedSlot; // If null or undefined, remove it if (constantSlot != null && (constantSlot.CellConstant.IsNull() || constantSlot.CellConstant.IsUndefined())) { eliminatedNullClauses = true; } else { clauses.Add(clause); if (clause.Condition.IsTrue) { // none of subsequent case statements will be evaluated - ignore them break; } } } if (eliminatedNullClauses && clauses.Count == 0) { // There is nothing left -- we should add a null as the value m_elseValue = new ConstantProjectedSlot(Constant.Null, m_memberPath); } // If we eliminated some undefined or null clauses, we do not want an else clause if (clauses.Count > 0 && false == eliminatedNullClauses) { // turn the last WHEN clause into an ELSE int lastIndex = clauses.Count - 1; m_elseValue = clauses[lastIndex].Value; clauses.RemoveAt(lastIndex); } m_clauses = clauses; m_simplified = true; }
private bool TryGetInstantiatedType(ProjectedSlot slot, out EdmType type) { type = null; ConstantProjectedSlot constantSlot = slot as ConstantProjectedSlot; if (constantSlot != null) { TypeConstant typeConstant = constantSlot.CellConstant as TypeConstant; if (typeConstant != null) { type = typeConstant.EdmType; return(true); } } return(false); }
private static void WithRelationshipsClauseAsCql(Action <WithRelationship> emitWithRelationship, IEnumerable <WithRelationship> withRelationships, ProjectedSlot slot) { if (withRelationships != null && withRelationships.Count() > 0) { ConstantProjectedSlot constantSlot = slot as ConstantProjectedSlot; Debug.Assert(constantSlot != null, "WITH RELATIONSHIP clauses should be specified for type constant slots only."); TypeConstant typeConstant = constantSlot.CellConstant as TypeConstant; Debug.Assert(typeConstant != null, "WITH RELATIONSHIP clauses should be there for type constants only."); EdmType fromType = typeConstant.EdmType; foreach (WithRelationship withRelationship in withRelationships) { // Add With statement for the types that participate in the association. if (withRelationship.FromEndEntityType.IsAssignableFrom(fromType)) { emitWithRelationship(withRelationship); } } } }
internal override CqlBlock ToCqlBlock(bool[] requiredSlots, CqlIdentifiers identifiers, ref int blockAliasNum, ref List <WithRelationship> withRelationships) { // Get the projected slots and the boolean expressions int totalSlots = requiredSlots.Length; CellQuery cellQuery = LeftCellWrapper.RightCellQuery; SlotInfo[] projectedSlots = new SlotInfo[totalSlots]; Debug.Assert(cellQuery.NumProjectedSlots + cellQuery.NumBoolVars == totalSlots, "Wrong number of projected slots in node"); Debug.Assert(cellQuery.NumProjectedSlots == ProjectedSlotMap.Count, "Different number of slots in cell query and what we have mappings for"); // Add the regular fields for (int i = 0; i < cellQuery.NumProjectedSlots; i++) { ProjectedSlot slot = cellQuery.ProjectedSlotAt(i); // If the slot is not null, we will project it // For extents, we say that all requiredlots are the only the // ones that are CLR non-null. Recall that "real" nulls are // handled by having a CellConstant.Null in ConstantSlot if (requiredSlots[i] && slot == null) { MemberPath memberPath = ProjectedSlotMap[i]; ConstantProjectedSlot defaultValue = new ConstantProjectedSlot(Domain.GetDefaultValueForMemberPath(memberPath, GetLeaves(), ViewgenContext.Config), memberPath); cellQuery.FixMissingSlotAsDefaultConstant(i, defaultValue); slot = defaultValue; } SlotInfo slotInfo = new SlotInfo(requiredSlots[i], slot != null, slot, ProjectedSlotMap[i]); projectedSlots[i] = slotInfo; } // Add the boolean fields for (int boolNum = 0; boolNum < cellQuery.NumBoolVars; boolNum++) { BoolExpression expr = cellQuery.GetBoolVar(boolNum); BooleanProjectedSlot boolSlot; if (expr != null) { boolSlot = new BooleanProjectedSlot(expr, identifiers, boolNum); } else { boolSlot = new BooleanProjectedSlot(BoolExpression.False, identifiers, boolNum); } int slotIndex = BoolIndexToSlot(boolNum); SlotInfo slotInfo = new SlotInfo(requiredSlots[slotIndex], expr != null, boolSlot, null); projectedSlots[slotIndex] = slotInfo; } // See if we are generating a query view and whether there are any colocated foreign keys for which // we have to add With statements. IEnumerable <SlotInfo> totalProjectedSlots = projectedSlots; if ((cellQuery.Extent.EntityContainer.DataSpace == DataSpace.SSpace) && (this.m_cellWrapper.LeftExtent.BuiltInTypeKind == BuiltInTypeKind.EntitySet)) { IEnumerable <StorageAssociationSetMapping> associationSetMaps = this.ViewgenContext.EntityContainerMapping.GetRelationshipSetMappingsFor(this.m_cellWrapper.LeftExtent, cellQuery.Extent); List <SlotInfo> foreignKeySlots = new List <SlotInfo>(); foreach (StorageAssociationSetMapping colocatedAssociationSetMap in associationSetMaps) { WithRelationship withRelationship; if (TryGetWithRelationship(colocatedAssociationSetMap, this.m_cellWrapper.LeftExtent, cellQuery.SourceExtentMemberPath, ref foreignKeySlots, out withRelationship)) { withRelationships.Add(withRelationship); totalProjectedSlots = projectedSlots.Concat(foreignKeySlots); } } } ExtentCqlBlock result = new ExtentCqlBlock(cellQuery.Extent, cellQuery.SelectDistinctFlag, totalProjectedSlots.ToArray(), cellQuery.WhereClause, identifiers, ++blockAliasNum); return(result); }
// 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); }
// returns true when the case statement is completed private bool AddRewritingToCaseStatement( Tile<FragmentQuery> rewriting, CaseStatement caseStatement, MemberPath currentPath, Constant domainValue) { var whenCondition = BoolExpression.True; // check whether the rewriting is always true or always false // if it's always true, we don't need any other WHEN clauses in the case statement // if it's always false, we don't need to add this WHEN clause to the case statement // given: domainQuery is satisfied. Check (domainQuery -> rewriting) var isAlwaysTrue = _qp.IsContainedIn(CreateTile(_domainQuery), rewriting); var isAlwaysFalse = _qp.IsDisjointFrom(CreateTile(_domainQuery), rewriting); Debug.Assert(!(isAlwaysTrue && isAlwaysFalse)); if (isAlwaysFalse) { return false; // don't need an unsatisfiable WHEN clause } if (isAlwaysTrue) { Debug.Assert(caseStatement.Clauses.Count == 0); } ProjectedSlot projectedSlot; if (domainValue.HasNotNull()) { projectedSlot = new MemberProjectedSlot(currentPath); } else { projectedSlot = new ConstantProjectedSlot(domainValue); } if (!isAlwaysTrue) { whenCondition = TileToBoolExpr(rewriting); } else { whenCondition = BoolExpression.True; } caseStatement.AddWhenThen(whenCondition, projectedSlot); return isAlwaysTrue; }
internal void FixMissingSlotAsDefaultConstant(int slotNumber, ConstantProjectedSlot slot) { Debug.Assert(m_projectedSlots[slotNumber] == null, "Another attempt to plug in a default value"); m_projectedSlots[slotNumber] = slot; }