1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
//---------------------------------------------------------------------
// <copyright file="VarInfo.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//
// @owner Microsoft
// @backupOwner Microsoft
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
//using System.Diagnostics; // Please use PlanCompiler.Assert instead of Debug.Assert in this class...
// It is fine to use Debug.Assert in cases where you assert an obvious thing that is supposed
// to prevent from simple mistakes during development (e.g. method argument validation
// in cases where it was you who created the variables or the variables had already been validated or
// in "else" clauses where due to code changes (e.g. adding a new value to an enum type) the default
// "else" block is chosen why the new condition should be treated separately). This kind of asserts are
// (can be) helpful when developing new code to avoid simple mistakes but have no or little value in
// the shipped product.
// PlanCompiler.Assert *MUST* be used to verify conditions in the trees. These would be assumptions
// about how the tree was built etc. - in these cases we probably want to throw an exception (this is
// what PlanCompiler.Assert does when the condition is not met) if either the assumption is not correct
// or the tree was built/rewritten not the way we thought it was.
// Use your judgment - if you rather remove an assert than ship it use Debug.Assert otherwise use
// PlanCompiler.Assert.
using System.Globalization;
using System.Data.Common;
using md = System.Data.Metadata.Edm;
using System.Data.Query.InternalTrees;
using System.Data.Query.PlanCompiler;
namespace System.Data.Query.PlanCompiler {
/// <summary>
/// Kind of VarInfo
/// </summary>
internal enum VarInfoKind
{
/// <summary>
/// The VarInfo is of <see cref="PrimitiveTypeVarInfo"/> type.
/// </summary>
PrimitiveTypeVarInfo,
/// <summary>
/// The VarInfo is of <see cref="StructuredVarInfo"/> type.
/// </summary>
StructuredTypeVarInfo,
/// <summary>
/// The VarInfo is of <see cref="CollectionVarInfo"/> type.
/// </summary>
CollectionVarInfo
}
/// <summary>
/// Information about a Var and its replacement
/// </summary>
internal abstract class VarInfo {
/// <summary>
/// Gets <see cref="VarInfoKind"/> for this <see cref="VarInfo"/>.
/// </summary>
internal abstract VarInfoKind Kind { get; }
/// <summary>
/// Get the list of new Vars introduced by this VarInfo
/// </summary>
internal virtual List<Var> NewVars { get { return null; } }
}
/// <summary>
/// Represents information about a collection typed Var.
/// Each such Var is replaced by a Var with a new "mapped" type - the "mapped" type
/// is simply a collection type where the element type has been "mapped"
/// </summary>
internal class CollectionVarInfo : VarInfo {
private List<Var> m_newVars; // always a singleton list
/// <summary>
/// Create a CollectionVarInfo
/// </summary>
/// <param name="newVar"></param>
internal CollectionVarInfo(Var newVar) {
m_newVars = new List<Var>();
m_newVars.Add(newVar);
}
/// <summary>
/// Get the newVar
/// </summary>
internal Var NewVar { get { return m_newVars[0]; } }
/// <summary>
/// Gets <see cref="VarInfoKind"/> for this <see cref="VarInfo"/>. Always <see cref="VarInfoKind.CollectionVarInfo"/>.
/// </summary>
internal override VarInfoKind Kind { get { return VarInfoKind.CollectionVarInfo; } }
/// <summary>
/// Get the list of all NewVars - just one really
/// </summary>
internal override List<Var> NewVars { get { return m_newVars; } }
}
/// <summary>
/// The StructuredVarInfo class contains information about a structured type Var
/// and how it can be replaced. This is targeted towards Vars of complex/record/
/// entity/ref types, and the goal is to replace all such Vars in this module.
/// </summary>
internal class StructuredVarInfo : VarInfo {
private Dictionary<md.EdmProperty, Var> m_propertyToVarMap;
List<Var> m_newVars;
bool m_newVarsIncludeNullSentinelVar;
List<md.EdmProperty> m_newProperties;
md.RowType m_newType;
md.TypeUsage m_newTypeUsage;
/// <summary>
/// Constructor
/// </summary>
/// <param name="newType">new "flat" record type corresponding to the Var's datatype</param>
/// <param name="newVars">List of vars to replace current Var</param>
/// <param name="newTypeProperties">List of properties in the "flat" record type</param>
/// <param name="newVarsIncludeNullSentinelVar">Do the new vars include a var that represents a null sentinel either for this type or for any nested type</param>
internal StructuredVarInfo(md.RowType newType, List<Var> newVars, List<md.EdmProperty> newTypeProperties, bool newVarsIncludeNullSentinelVar)
{
PlanCompiler.Assert(newVars.Count == newTypeProperties.Count, "count mismatch");
// I see a few places where this is legal
// PlanCompiler.Assert(newVars.Count > 0, "0 vars?");
m_newVars = newVars;
m_newProperties = newTypeProperties;
m_newType = newType;
m_newVarsIncludeNullSentinelVar = newVarsIncludeNullSentinelVar;
m_newTypeUsage = md.TypeUsage.Create(newType);
}
/// <summary>
/// Gets <see cref="VarInfoKind"/> for this <see cref="VarInfo"/>. Always <see cref="VarInfoKind.StructuredTypeVarInfo"/>.
/// </summary>
internal override VarInfoKind Kind
{
get { return VarInfoKind.StructuredTypeVarInfo; }
}
/// <summary>
/// The NewVars property of the VarInfo is a list of the corresponding
/// "scalar" Vars that can be used to replace the current Var. This is
/// mainly intended for use by other RelOps that maintain lists of Vars
/// - for example, the "Vars" property of ProjectOp and other similar
/// locations.
/// </summary>
internal override List<Var> NewVars { get { return m_newVars; } }
/// <summary>
/// The Fields property is matched 1-1 with the NewVars property, and
/// specifies the properties of the record type corresponding to the
/// original VarType
/// </summary>
internal List<md.EdmProperty> Fields { get { return m_newProperties; } }
/// <summary>
/// Indicates whether any of the vars in NewVars 'derives'
/// from a null sentinel. For example, for a type that is a Record with two
/// nested records, if any has a null sentinel, it would be set to true.
/// It is used when expanding sort keys, to be able to indicate that there is a
/// sorting operation that includes null sentinels. This indication is later
/// used by transformation rules.
/// </summary>
internal bool NewVarsIncludeNullSentinelVar { get { return m_newVarsIncludeNullSentinelVar; } }
/// <summary>
/// Get the Var corresponding to a specific property
/// </summary>
/// <param name="p">the requested property</param>
/// <param name="v">the corresponding Var</param>
/// <returns>true, if the Var was found</returns>
internal bool TryGetVar(md.EdmProperty p, out Var v) {
if (m_propertyToVarMap == null) {
InitPropertyToVarMap();
}
return m_propertyToVarMap.TryGetValue(p, out v);
}
/// <summary>
/// The NewType property describes the new "flattened" record type
/// that is a replacement for the original type of the Var
/// </summary>
internal md.RowType NewType { get { return m_newType; } }
/// <summary>
/// Returns the NewType wrapped in a TypeUsage
/// </summary>
internal md.TypeUsage NewTypeUsage { get { return m_newTypeUsage; } }
/// <summary>
/// Initialize mapping from properties to the corresponding Var
/// </summary>
private void InitPropertyToVarMap() {
if (m_propertyToVarMap == null) {
m_propertyToVarMap = new Dictionary<md.EdmProperty, Var>();
IEnumerator<Var> newVarEnumerator = m_newVars.GetEnumerator();
foreach (md.EdmProperty prop in m_newProperties) {
newVarEnumerator.MoveNext();
m_propertyToVarMap.Add(prop, newVarEnumerator.Current);
}
newVarEnumerator.Dispose();
}
}
}
/// <summary>
/// Represents information about a primitive typed Var and how it can be replaced.
/// </summary>
internal class PrimitiveTypeVarInfo : VarInfo
{
private List<Var> m_newVars; // always a singleton list
/// <summary>
/// Initializes a new instance of <see cref="PrimitiveTypeVarInfo"/> class.
/// </summary>
/// <param name="newVar">
/// New <see cref="Var"/> that replaces current <see cref="Var"/>.
/// </param>
internal PrimitiveTypeVarInfo(Var newVar)
{
System.Diagnostics.Debug.Assert(newVar != null, "newVar != null");
m_newVars = new List<Var>() { newVar };
}
/// <summary>
/// Gets the newVar.
/// </summary>
internal Var NewVar { get { return m_newVars[0]; } }
/// <summary>
/// Gets <see cref="VarInfoKind"/> for this <see cref="VarInfo"/>. Always <see cref="VarInfoKind.CollectionVarInfo"/>.
/// </summary>
internal override VarInfoKind Kind
{
get { return VarInfoKind.PrimitiveTypeVarInfo; }
}
/// <summary>
/// Gets the list of all NewVars. The list contains always just one element.
/// </summary>
internal override List<Var> NewVars { get { return m_newVars; } }
}
/// <summary>
/// The VarInfo map maintains a mapping from Vars to their corresponding VarInfo
/// It is logically a Dictionary
/// </summary>
internal class VarInfoMap {
private Dictionary<Var, VarInfo> m_map;
/// <summary>
/// Default constructor
/// </summary>
internal VarInfoMap() {
m_map = new Dictionary<Var, VarInfo>();
}
/// <summary>
/// Create a new VarInfo for a structured type Var
/// </summary>
/// <param name="v">The structured type Var</param>
/// <param name="newType">"Mapped" type for v</param>
/// <param name="newVars">List of vars corresponding to v</param>
/// <param name="newProperties">Flattened Properties </param>
/// <param name="newVarsIncludeNullSentinelVar">Do the new vars include a var that represents a null sentinel either for this type or for any nested type</param>
/// <returns>the VarInfo</returns>
internal VarInfo CreateStructuredVarInfo(Var v, md.RowType newType, List<Var> newVars, List<md.EdmProperty> newProperties, bool newVarsIncludeNullSentinelVar)
{
VarInfo varInfo = new StructuredVarInfo(newType, newVars, newProperties, newVarsIncludeNullSentinelVar);
m_map.Add(v, varInfo);
return varInfo;
}
/// <summary>
/// Create a new VarInfo for a structured type Var where the newVars cannot include a null sentinel
/// </summary>
/// <param name="v">The structured type Var</param>
/// <param name="newType">"Mapped" type for v</param>
/// <param name="newVars">List of vars corresponding to v</param>
/// <param name="newProperties">Flattened Properties </param>
internal VarInfo CreateStructuredVarInfo(Var v, md.RowType newType, List<Var> newVars, List<md.EdmProperty> newProperties)
{
return CreateStructuredVarInfo(v, newType, newVars, newProperties, false);
}
/// <summary>
/// Create a VarInfo for a collection typed Var
/// </summary>
/// <param name="v">The collection-typed Var</param>
/// <param name="newVar">the new Var</param>
/// <returns>the VarInfo</returns>
internal VarInfo CreateCollectionVarInfo(Var v, Var newVar) {
VarInfo varInfo = new CollectionVarInfo(newVar);
m_map.Add(v, varInfo);
return varInfo;
}
/// <summary>
/// Creates a var info for var variables of primitive or enum type.
/// </summary>
/// <param name="v">Current variable of primitive or enum type.</param>
/// <param name="newVar">The new variable replacing <paramref name="v"/>.</param>
/// <returns><see cref="PrimitiveTypeVarInfo"/> for <paramref name="v"/>.</returns>
internal VarInfo CreatePrimitiveTypeVarInfo(Var v, Var newVar)
{
System.Diagnostics.Debug.Assert(v != null, "v != null");
System.Diagnostics.Debug.Assert(newVar != null, "newVar != null");
PlanCompiler.Assert(md.TypeSemantics.IsScalarType(v.Type), "The current variable should be of primitive or enum type.");
PlanCompiler.Assert(md.TypeSemantics.IsScalarType(newVar.Type), "The new variable should be of primitive or enum type.");
VarInfo varInfo = new PrimitiveTypeVarInfo(newVar);
m_map.Add(v, varInfo);
return varInfo;
}
/// <summary>
/// Return the VarInfo for the specified var (if one exists, of course)
/// </summary>
/// <param name="v">The Var</param>
/// <param name="varInfo">the corresponding VarInfo</param>
/// <returns></returns>
internal bool TryGetVarInfo(Var v, out VarInfo varInfo) {
return m_map.TryGetValue(v, out varInfo);
}
}
}
|