1 //---------------------------------------------------------------------
2 // <copyright file="VarRemapper.cs" company="Microsoft">
3 // Copyright (c) Microsoft Corporation. All rights reserved.
7 // @backupOwner Microsoft
8 //---------------------------------------------------------------------
11 using System.Collections.Generic;
12 //using System.Diagnostics; // Please use PlanCompiler.Assert instead of Debug.Assert in this class...
14 // It is fine to use Debug.Assert in cases where you assert an obvious thing that is supposed
15 // to prevent from simple mistakes during development (e.g. method argument validation
16 // in cases where it was you who created the variables or the variables had already been validated or
17 // in "else" clauses where due to code changes (e.g. adding a new value to an enum type) the default
18 // "else" block is chosen why the new condition should be treated separately). This kind of asserts are
19 // (can be) helpful when developing new code to avoid simple mistakes but have no or little value in
20 // the shipped product.
21 // PlanCompiler.Assert *MUST* be used to verify conditions in the trees. These would be assumptions
22 // about how the tree was built etc. - in these cases we probably want to throw an exception (this is
23 // what PlanCompiler.Assert does when the condition is not met) if either the assumption is not correct
24 // or the tree was built/rewritten not the way we thought it was.
25 // Use your judgment - if you rather remove an assert than ship it use Debug.Assert otherwise use
26 // PlanCompiler.Assert.
28 using System.Data.Query.InternalTrees;
30 namespace System.Data.Query.PlanCompiler
33 /// The VarRemapper is a utility class that can be used to "remap" Var references
34 /// in a node, or a subtree.
36 internal class VarRemapper : BasicOpVisitor
39 private readonly Dictionary<Var, Var> m_varMap;
40 protected readonly Command m_command;
45 /// Internal constructor
47 /// <param name="command">Current iqt command</param>
48 internal VarRemapper(Command command)
49 :this(command, new Dictionary<Var,Var>())
54 /// Internal constructor
56 /// <param name="command">Current iqt command</param>
57 /// <param name="varMap">Var map to be used</param>
58 internal VarRemapper(Command command, Dictionary<Var, Var> varMap)
65 #region Public surface
67 /// Add a mapping for "oldVar" - when the replace methods are invoked, they
68 /// will replace all references to "oldVar" by "newVar"
70 /// <param name="oldVar">var to replace</param>
71 /// <param name="newVar">the replacement var</param>
72 internal void AddMapping(Var oldVar, Var newVar)
74 m_varMap[oldVar] = newVar;
78 /// Update vars in just this node (and not the entire subtree)
79 /// Does *not* recompute the nodeinfo - there are at least some consumers of this
80 /// function that do not want the recomputation - transformation rules, for example
82 /// <param name="node">current node</param>
83 internal virtual void RemapNode(Node node)
85 if (m_varMap.Count == 0)
93 /// Update vars in this subtree. Recompute the nodeinfo along the way
95 /// <param name="subTree">subtree to "remap"</param>
96 internal virtual void RemapSubtree(Node subTree)
98 if (m_varMap.Count == 0)
103 foreach (Node chi in subTree.Children)
109 m_command.RecomputeNodeInfo(subTree);
113 /// Produce a a new remapped varList
115 /// <param name="varList"></param>
116 /// <returns>remapped varList</returns>
117 internal VarList RemapVarList(VarList varList)
119 return Command.CreateVarList(MapVars(varList));
123 /// Remap the given varList using the given varMap
125 /// <param name="command"></param>
126 /// <param name="varMap"></param>
127 /// <param name="varList"></param>
128 internal static VarList RemapVarList(Command command, Dictionary<Var, Var> varMap, VarList varList)
130 VarRemapper varRemapper = new VarRemapper(command, varMap);
131 return varRemapper.RemapVarList(varList);
135 #region Private methods
137 /// Get the mapping for a Var - returns the var itself, mapping was found
139 /// <param name="v"></param>
140 /// <returns></returns>
141 private Var Map(Var v)
146 if (!m_varMap.TryGetValue(v, out newVar))
154 private IEnumerable<Var> MapVars(IEnumerable<Var> vars)
156 foreach (Var v in vars)
162 private void Map(VarVec vec)
164 VarVec newVec = m_command.CreateVarVec(MapVars(vec));
165 vec.InitFrom(newVec);
168 private void Map(VarList varList)
170 VarList newList = Command.CreateVarList(MapVars(varList));
172 varList.AddRange(newList);
175 private void Map(VarMap varMap)
177 VarMap newVarMap = new VarMap();
178 foreach (KeyValuePair<Var, Var> kv in varMap)
180 Var newVar = Map(kv.Value);
181 newVarMap.Add(kv.Key, newVar);
184 foreach (KeyValuePair<Var, Var> kv in newVarMap)
186 varMap.Add(kv.Key, kv.Value);
189 private void Map(List<InternalTrees.SortKey> sortKeys)
191 VarVec sortVars = m_command.CreateVarVec();
192 bool hasDuplicates = false;
195 // Map each var in the sort list. Remapping may introduce duplicates, and
196 // we should get rid of duplicates, since sql doesn't like them
198 foreach (InternalTrees.SortKey sk in sortKeys)
200 sk.Var = Map(sk.Var);
201 if (sortVars.IsSet(sk.Var))
203 hasDuplicates = true;
205 sortVars.Set(sk.Var);
209 // Get rid of any duplicates
213 List<InternalTrees.SortKey> newSortKeys = new List<SortKey>(sortKeys);
216 foreach (InternalTrees.SortKey sk in newSortKeys)
218 if (!sortVars.IsSet(sk.Var))
222 sortVars.Set(sk.Var);
227 #region VisitorMethods
229 /// Default visitor for a node - does not visit the children
230 /// The reason we have this method is because the default VisitDefault
231 /// actually visits the children, and we don't want to do that
233 /// <param name="n"></param>
234 protected override void VisitDefault(Node n)
240 public override void Visit(VarRefOp op, Node n)
242 VisitScalarOpDefault(op, n);
243 Var newVar = Map(op.Var);
244 if (newVar != op.Var)
246 n.Op = m_command.CreateVarRefOp(newVar);
255 protected override void VisitNestOp(NestBaseOp op, Node n)
257 throw EntityUtil.NotSupported();
260 public override void Visit(PhysicalProjectOp op, Node n)
262 VisitPhysicalOpDefault(op, n);
265 SimpleCollectionColumnMap newColumnMap = (SimpleCollectionColumnMap)ColumnMapTranslator.Translate(op.ColumnMap, m_varMap);
266 n.Op = m_command.CreatePhysicalProjectOp(op.Outputs, newColumnMap);
271 protected override void VisitGroupByOp(GroupByBaseOp op, Node n)
273 VisitRelOpDefault(op, n);
277 public override void Visit(GroupByIntoOp op, Node n)
279 VisitGroupByOp(op, n);
282 public override void Visit(DistinctOp op, Node n)
284 VisitRelOpDefault(op, n);
287 public override void Visit(ProjectOp op, Node n)
289 VisitRelOpDefault(op, n);
292 public override void Visit(UnnestOp op, Node n)
294 VisitRelOpDefault(op, n);
295 Var newVar = Map(op.Var);
296 if (newVar != op.Var)
298 n.Op = m_command.CreateUnnestOp(newVar, op.Table);
301 protected override void VisitSetOp(SetOp op, Node n)
303 VisitRelOpDefault(op, n);
307 protected override void VisitSortOp(SortBaseOp op, Node n)
309 VisitRelOpDefault(op, n);