Code:
/ 4.0 / 4.0 / DEVDIV_TFS / Dev10 / Releases / RTMRel / ndp / fx / src / DataEntity / System / Data / Query / PlanCompiler / PlanCompilerUtil.cs / 1305376 / PlanCompilerUtil.cs
//----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// @owner [....]
// @backupOwner [....]
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data.Common.Utils;
using System.Data.Metadata.Edm;
using System.Data.Query.InternalTrees;
namespace System.Data.Query.PlanCompiler
{
///
/// Utility class for the methods shared among the classes comprising the plan compiler
///
internal static class PlanCompilerUtil
{
///
/// Utility method that determines whether a given CaseOp subtree can be optimized.
/// Called by both PreProcessor and NominalTypeEliminator.
///
/// If the case statement is of the shape:
/// case when X then NULL else Y, or
/// case when X then Y else NULL,
/// where Y is of row type, and the types of the input CaseOp, the NULL and Y are the same,
/// return true
///
///
///
///
internal static bool IsRowTypeCaseOpWithNullability(CaseOp op, Node n, out bool thenClauseIsNull)
{
thenClauseIsNull = false; //any default value will do
if (!TypeSemantics.IsRowType(op.Type))
{
return false;
}
if (n.Children.Count != 3)
{
return false;
}
//All three types must be equal
if (!n.Child1.Op.Type.EdmEquals(op.Type) || !n.Child2.Op.Type.EdmEquals(op.Type))
{
return false;
}
//At least one of Child1 and Child2 needs to be a null
if (n.Child1.Op.OpType == OpType.Null)
{
thenClauseIsNull = true;
return true;
}
if (n.Child2.Op.OpType == OpType.Null)
{
// thenClauseIsNull stays false
return true;
}
return false;
}
///
/// Is this function a collection aggregate function. It is, if
/// - it has exactly one child
/// - that child is a collection type
/// - and the function has been marked with the aggregate attribute
///
/// the function op
/// the current subtree
/// true, if this was a collection aggregate function
internal static bool IsCollectionAggregateFunction(FunctionOp op, Node n)
{
return ((n.Children.Count == 1) &&
TypeSemantics.IsCollectionType(n.Child0.Op.Type) &&
TypeSemantics.IsAggregateFunction(op.Function));
}
///
/// Is the given op one of the ConstantBaseOp-s
///
///
///
internal static bool IsConstantBaseOp(OpType opType)
{
return opType == OpType.Constant ||
opType == OpType.InternalConstant ||
opType == OpType.Null ||
opType == OpType.NullSentinel;
}
///
/// Combine two predicates by trying to avoid the predicate parts of the
/// second one that are already present in the first one.
///
/// In particular, given two nodes, predicate1 and predicate2,
/// it creates a combined predicate logically equivalent to
/// predicate1 AND predicate2,
/// but it does not include any AND parts of predicate2 that are present
/// in predicate1.
///
///
///
///
///
internal static Node CombinePredicates(Node predicate1, Node predicate2, Command command)
{
IEnumerable andParts1 = BreakIntoAndParts(predicate1);
IEnumerable andParts2 = BreakIntoAndParts(predicate2);
Node result = predicate1;
foreach (Node predicatePart2 in andParts2)
{
bool foundMatch = false;
foreach (Node predicatePart1 in andParts1)
{
if (predicatePart1.IsEquivalent(predicatePart2))
{
foundMatch = true;
break;
}
}
if (!foundMatch)
{
result = command.CreateNode(command.CreateConditionalOp(OpType.And), result, predicatePart2);
}
}
return result;
}
///
/// Create a list of AND parts for a given predicate.
/// For example, if the predicate is of the shape:
/// ((p1 and p2) and (p3 and p4)) the list is p1, p2, p3, p4
/// The predicates p1,p2, p3, p4 may be roots of subtrees that
/// have nodes with AND ops, but
/// would not be broken unless they are the AND nodes themselves.
///
///
///
private static IEnumerable BreakIntoAndParts(Node predicate)
{
return Helpers.GetLeafNodes(predicate,
node => (node.Op.OpType != OpType.And),
node => (new[] {node.Child0, node.Child1}));
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
//----------------------------------------------------------------------
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
//
// @owner [....]
// @backupOwner [....]
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data.Common.Utils;
using System.Data.Metadata.Edm;
using System.Data.Query.InternalTrees;
namespace System.Data.Query.PlanCompiler
{
///
/// Utility class for the methods shared among the classes comprising the plan compiler
///
internal static class PlanCompilerUtil
{
///
/// Utility method that determines whether a given CaseOp subtree can be optimized.
/// Called by both PreProcessor and NominalTypeEliminator.
///
/// If the case statement is of the shape:
/// case when X then NULL else Y, or
/// case when X then Y else NULL,
/// where Y is of row type, and the types of the input CaseOp, the NULL and Y are the same,
/// return true
///
///
///
///
internal static bool IsRowTypeCaseOpWithNullability(CaseOp op, Node n, out bool thenClauseIsNull)
{
thenClauseIsNull = false; //any default value will do
if (!TypeSemantics.IsRowType(op.Type))
{
return false;
}
if (n.Children.Count != 3)
{
return false;
}
//All three types must be equal
if (!n.Child1.Op.Type.EdmEquals(op.Type) || !n.Child2.Op.Type.EdmEquals(op.Type))
{
return false;
}
//At least one of Child1 and Child2 needs to be a null
if (n.Child1.Op.OpType == OpType.Null)
{
thenClauseIsNull = true;
return true;
}
if (n.Child2.Op.OpType == OpType.Null)
{
// thenClauseIsNull stays false
return true;
}
return false;
}
///
/// Is this function a collection aggregate function. It is, if
/// - it has exactly one child
/// - that child is a collection type
/// - and the function has been marked with the aggregate attribute
///
/// the function op
/// the current subtree
/// true, if this was a collection aggregate function
internal static bool IsCollectionAggregateFunction(FunctionOp op, Node n)
{
return ((n.Children.Count == 1) &&
TypeSemantics.IsCollectionType(n.Child0.Op.Type) &&
TypeSemantics.IsAggregateFunction(op.Function));
}
///
/// Is the given op one of the ConstantBaseOp-s
///
///
///
internal static bool IsConstantBaseOp(OpType opType)
{
return opType == OpType.Constant ||
opType == OpType.InternalConstant ||
opType == OpType.Null ||
opType == OpType.NullSentinel;
}
///
/// Combine two predicates by trying to avoid the predicate parts of the
/// second one that are already present in the first one.
///
/// In particular, given two nodes, predicate1 and predicate2,
/// it creates a combined predicate logically equivalent to
/// predicate1 AND predicate2,
/// but it does not include any AND parts of predicate2 that are present
/// in predicate1.
///
///
///
///
///
internal static Node CombinePredicates(Node predicate1, Node predicate2, Command command)
{
IEnumerable andParts1 = BreakIntoAndParts(predicate1);
IEnumerable andParts2 = BreakIntoAndParts(predicate2);
Node result = predicate1;
foreach (Node predicatePart2 in andParts2)
{
bool foundMatch = false;
foreach (Node predicatePart1 in andParts1)
{
if (predicatePart1.IsEquivalent(predicatePart2))
{
foundMatch = true;
break;
}
}
if (!foundMatch)
{
result = command.CreateNode(command.CreateConditionalOp(OpType.And), result, predicatePart2);
}
}
return result;
}
///
/// Create a list of AND parts for a given predicate.
/// For example, if the predicate is of the shape:
/// ((p1 and p2) and (p3 and p4)) the list is p1, p2, p3, p4
/// The predicates p1,p2, p3, p4 may be roots of subtrees that
/// have nodes with AND ops, but
/// would not be broken unless they are the AND nodes themselves.
///
///
///
private static IEnumerable BreakIntoAndParts(Node predicate)
{
return Helpers.GetLeafNodes(predicate,
node => (node.Op.OpType != OpType.And),
node => (new[] {node.Child0, node.Child1}));
}
}
}
// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
Link Menu

This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- HtmlInputButton.cs
- Label.cs
- ErrorItem.cs
- ResponseStream.cs
- SafeRightsManagementSessionHandle.cs
- QilFactory.cs
- ExclusiveCanonicalizationTransform.cs
- CommandHelpers.cs
- OletxEnlistment.cs
- XmlQuerySequence.cs
- RewritingPass.cs
- GridViewColumnCollectionChangedEventArgs.cs
- XmlSiteMapProvider.cs
- CaretElement.cs
- ScrollChangedEventArgs.cs
- ListViewUpdateEventArgs.cs
- DodSequenceMerge.cs
- ClientFormsAuthenticationCredentials.cs
- InputReport.cs
- NullReferenceException.cs
- HMAC.cs
- ObjectTypeMapping.cs
- DataShape.cs
- EdgeModeValidation.cs
- DriveNotFoundException.cs
- BooleanToSelectiveScrollingOrientationConverter.cs
- SerializationObjectManager.cs
- ComponentChangingEvent.cs
- VisualStyleRenderer.cs
- ResXResourceWriter.cs
- TableProviderWrapper.cs
- UpdateExpressionVisitor.cs
- DataGridViewColumnHeaderCell.cs
- ModuleElement.cs
- FullTrustAssemblyCollection.cs
- FrugalList.cs
- XLinq.cs
- CryptographicAttribute.cs
- Registry.cs
- ClientCultureInfo.cs
- SafeCryptContextHandle.cs
- PointLight.cs
- SelectionItemProviderWrapper.cs
- UIElementPropertyUndoUnit.cs
- EntityDataSourceValidationException.cs
- WmiEventSink.cs
- GradientStop.cs
- UserControlParser.cs
- DBSchemaRow.cs
- XPathDocumentIterator.cs
- GridViewAutoFormat.cs
- XmlChildNodes.cs
- HeaderCollection.cs
- BuildProviderCollection.cs
- WindowsFormsHost.cs
- HashCodeCombiner.cs
- MoveSizeWinEventHandler.cs
- HandoffBehavior.cs
- SystemTcpStatistics.cs
- _ConnectStream.cs
- TextSchema.cs
- RegexRunnerFactory.cs
- TraceHwndHost.cs
- MenuAdapter.cs
- ConstraintManager.cs
- UserPreferenceChangingEventArgs.cs
- ScriptComponentDescriptor.cs
- DataKeyArray.cs
- SamlAuthorityBinding.cs
- MetadataCollection.cs
- ToolStripItem.cs
- PeerObject.cs
- CompModSwitches.cs
- SelectingProviderEventArgs.cs
- ByteAnimationBase.cs
- ConnectionStringSettingsCollection.cs
- VersionedStream.cs
- ProtocolViolationException.cs
- OpenTypeLayoutCache.cs
- PersonalizationState.cs
- XmlDigitalSignatureProcessor.cs
- ArrayWithOffset.cs
- PreviewKeyDownEventArgs.cs
- LinkTarget.cs
- UIElementPropertyUndoUnit.cs
- PerformanceCounters.cs
- DiscoveryVersion.cs
- HyperLinkDataBindingHandler.cs
- documentsequencetextpointer.cs
- VisemeEventArgs.cs
- SignatureHelper.cs
- PresentationTraceSources.cs
- KeySplineConverter.cs
- Operand.cs
- SettingsPropertyCollection.cs
- SnapshotChangeTrackingStrategy.cs
- MatrixTransform.cs
- RtfControlWordInfo.cs
- XhtmlStyleClass.cs
- DataControlLinkButton.cs