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
|
//---------------------------------------------------------------------
// <copyright file="Normalizer.cs" company="Microsoft">
// Copyright (c) Microsoft Corporation. All rights reserved.
// </copyright>
//
// @owner Microsoft
// @backupOwner Microsoft
//---------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Metadata.Edm;
using System.Data.Query.InternalTrees;
//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.
//
// The normalizer performs transformations of the tree to bring it to a 'normalized' format
// In particular it does the following:
// (a) Transforms collection aggregate functions into a GroupBy.
// (b) Translates Exists(X) into Exists(select 1 from X)
//
namespace System.Data.Query.PlanCompiler
{
/// <summary>
/// The normalizer performs transformations of the tree to bring it to a 'normalized' format
/// </summary>
internal class Normalizer : SubqueryTrackingVisitor
{
#region constructors
private Normalizer(PlanCompiler planCompilerState)
:base(planCompilerState)
{
}
#endregion
#region public methods
/// <summary>
/// The driver routine.
/// </summary>
/// <param name="planCompilerState">plan compiler state</param>
internal static void Process(PlanCompiler planCompilerState)
{
Normalizer normalizer = new Normalizer(planCompilerState);
normalizer.Process();
}
#endregion
#region private methods
#region driver
private void Process()
{
m_command.Root = VisitNode(m_command.Root);
}
#endregion
#region visitor methods
#region ScalarOps
/// <summary>
/// Translate Exists(X) into Exists(select 1 from X)
/// </summary>
/// <param name="op"></param>
/// <param name="n"></param>
/// <returns></returns>
public override Node Visit(ExistsOp op, Node n)
{
VisitChildren(n);
// Build up a dummy project node over the input
n.Child0 = BuildDummyProjectForExists(n.Child0);
return n;
}
/// <summary>
/// Build Project(select 1 from child).
/// </summary>
/// <param name="child"></param>
/// <returns></returns>
private Node BuildDummyProjectForExists(Node child)
{
Var newVar;
Node projectNode = m_command.BuildProject(
child,
m_command.CreateNode(m_command.CreateInternalConstantOp(m_command.IntegerType, 1)),
out newVar);
return projectNode;
}
/// <summary>
/// Build up an unnest above a scalar op node
/// X => unnest(X)
/// </summary>
/// <param name="collectionNode">the scalarop collection node</param>
/// <returns>the unnest node</returns>
private Node BuildUnnest(Node collectionNode)
{
PlanCompiler.Assert(collectionNode.Op.IsScalarOp, "non-scalar usage of Unnest?");
PlanCompiler.Assert(TypeSemantics.IsCollectionType(collectionNode.Op.Type), "non-collection usage for Unnest?");
Var newVar;
Node varDefNode = m_command.CreateVarDefNode(collectionNode, out newVar);
UnnestOp unnestOp = m_command.CreateUnnestOp(newVar);
Node unnestNode = m_command.CreateNode(unnestOp, varDefNode);
return unnestNode;
}
/// <summary>
/// Converts the reference to a TVF as following: Collect(PhysicalProject(Unnest(Func)))
/// </summary>
/// <param name="op">current function op</param>
/// <param name="n">current function subtree</param>
/// <returns>the new expression that corresponds to the TVF</returns>
private Node VisitCollectionFunction(FunctionOp op, Node n)
{
PlanCompiler.Assert(TypeSemantics.IsCollectionType(op.Type), "non-TVF function?");
Node unnestNode = BuildUnnest(n);
UnnestOp unnestOp = unnestNode.Op as UnnestOp;
PhysicalProjectOp projectOp = m_command.CreatePhysicalProjectOp(unnestOp.Table.Columns[0]);
Node projectNode = m_command.CreateNode(projectOp, unnestNode);
CollectOp collectOp = m_command.CreateCollectOp(n.Op.Type);
Node collectNode = m_command.CreateNode(collectOp, projectNode);
return collectNode;
}
/// <summary>
/// Converts a collection aggregate function count(X), where X is a collection into
/// two parts. Part A is a groupby subquery that looks like
/// GroupBy(Unnest(X), empty, count(y))
/// where "empty" describes the fact that the groupby has no keys, and y is an
/// element var of the Unnest
///
/// Part 2 is a VarRef that refers to the aggregate var for count(y) described above.
///
/// Logically, we would replace the entire functionOp by element(GroupBy...). However,
/// since we also want to translate element() into single-row-subqueries, we do this
/// here as well.
///
/// The function itself is replaced by the VarRef, and the GroupBy is added to the list
/// of scalar subqueries for the current relOp node on the stack
///
/// </summary>
/// <param name="op">the functionOp for the collection agg</param>
/// <param name="n">current subtree</param>
/// <returns>the VarRef node that should replace the function</returns>
private Node VisitCollectionAggregateFunction(FunctionOp op, Node n)
{
TypeUsage softCastType = null;
Node argNode = n.Child0;
if (OpType.SoftCast == argNode.Op.OpType)
{
softCastType = TypeHelpers.GetEdmType<CollectionType>(argNode.Op.Type).TypeUsage;
argNode = argNode.Child0;
while (OpType.SoftCast == argNode.Op.OpType)
{
argNode = argNode.Child0;
}
}
Node unnestNode = BuildUnnest(argNode);
UnnestOp unnestOp = unnestNode.Op as UnnestOp;
Var unnestOutputVar = unnestOp.Table.Columns[0];
AggregateOp aggregateOp = m_command.CreateAggregateOp(op.Function, false);
VarRefOp unnestVarRefOp = m_command.CreateVarRefOp(unnestOutputVar);
Node unnestVarRefNode = m_command.CreateNode(unnestVarRefOp);
if (softCastType != null)
{
unnestVarRefNode = m_command.CreateNode(m_command.CreateSoftCastOp(softCastType), unnestVarRefNode);
}
Node aggExprNode = m_command.CreateNode(aggregateOp, unnestVarRefNode);
VarVec keyVars = m_command.CreateVarVec(); // empty keys
Node keyVarDefListNode = m_command.CreateNode(m_command.CreateVarDefListOp());
VarVec gbyOutputVars = m_command.CreateVarVec();
Var aggVar;
Node aggVarDefListNode = m_command.CreateVarDefListNode(aggExprNode, out aggVar);
gbyOutputVars.Set(aggVar);
GroupByOp gbyOp = m_command.CreateGroupByOp(keyVars, gbyOutputVars);
Node gbySubqueryNode = m_command.CreateNode(gbyOp, unnestNode, keyVarDefListNode, aggVarDefListNode);
// "Move" this subquery to my parent relop
Node ret = AddSubqueryToParentRelOp(aggVar, gbySubqueryNode);
return ret;
}
/// <summary>
/// Pre-processing for a function. Does the default scalar op processing.
/// If the function returns a collection (TVF), the method converts this expression into
/// Collect(PhysicalProject(Unnest(Func))).
/// If the function is a collection aggregate, converts it into the corresponding group aggregate.
/// </summary>
/// <param name="op"></param>
/// <param name="n"></param>
/// <returns></returns>
public override Node Visit(FunctionOp op, Node n)
{
VisitScalarOpDefault(op, n);
Node newNode = null;
// Is this a TVF?
if (TypeSemantics.IsCollectionType(op.Type))
{
newNode = VisitCollectionFunction(op, n);
}
// Is this a collection-aggregate function?
else if (PlanCompilerUtil.IsCollectionAggregateFunction(op, n))
{
newNode = VisitCollectionAggregateFunction(op, n);
}
else
{
newNode = n;
}
PlanCompiler.Assert(newNode != null, "failure to construct a functionOp?");
return newNode;
}
#endregion
#region RelOps
/// <summary>
/// Processing for all JoinOps
/// </summary>
/// <param name="op">JoinOp</param>
/// <param name="n">Current subtree</param>
/// <returns></returns>
protected override Node VisitJoinOp(JoinBaseOp op, Node n)
{
if (base.ProcessJoinOp(op, n))
{
// update the join condition
// #479372: Build up a dummy project node over the input, as we always wrap the child of exists
n.Child2.Child0 = BuildDummyProjectForExists(n.Child2.Child0);
}
return n;
}
#endregion
#endregion
#endregion
}
}
|