| | 1 | | // Copyright (c) Microsoft Corporation. All rights reserved. |
| | 2 | | // Licensed under the MIT License. |
| | 3 | |
|
| | 4 | | using System; |
| | 5 | | using System.Collections.Generic; |
| | 6 | | using System.Diagnostics; |
| | 7 | | using System.Linq.Expressions; |
| | 8 | | using System.Reflection; |
| | 9 | |
|
| | 10 | | namespace Azure.Data.Tables.Queryable |
| | 11 | | { |
| | 12 | | internal class ExpressionNormalizer : LinqExpressionVisitor |
| | 13 | | { |
| | 14 | | private const bool LiftToNull = false; |
| | 15 | |
|
| 460 | 16 | | private readonly Dictionary<Expression, Pattern> _patterns = new Dictionary<Expression, Pattern>(ReferenceEquali |
| | 17 | |
|
| 460 | 18 | | private ExpressionNormalizer(Dictionary<Expression, Expression> normalizerRewrites) |
| | 19 | | { |
| | 20 | | Debug.Assert(normalizerRewrites != null, "normalizerRewrites != null"); |
| 460 | 21 | | NormalizerRewrites = normalizerRewrites; |
| 460 | 22 | | } |
| | 23 | |
|
| 294 | 24 | | internal Dictionary<Expression, Expression> NormalizerRewrites { get; } |
| | 25 | |
|
| | 26 | | internal static Expression Normalize(Expression expression, Dictionary<Expression, Expression> rewrites) |
| | 27 | | { |
| | 28 | | Debug.Assert(expression != null, "expression != null"); |
| | 29 | | Debug.Assert(rewrites != null, "rewrites != null"); |
| | 30 | |
|
| 460 | 31 | | ExpressionNormalizer normalizer = new ExpressionNormalizer(rewrites); |
| 460 | 32 | | Expression result = normalizer.Visit(expression); |
| 460 | 33 | | return result; |
| | 34 | | } |
| | 35 | |
|
| | 36 | | internal override Expression VisitBinary(BinaryExpression b) |
| | 37 | | { |
| 1148 | 38 | | BinaryExpression visited = (BinaryExpression)base.VisitBinary(b); |
| | 39 | |
|
| 1148 | 40 | | switch (visited.NodeType) |
| | 41 | | { |
| | 42 | | case ExpressionType.Equal: |
| | 43 | | case ExpressionType.NotEqual: |
| | 44 | | case ExpressionType.LessThan: |
| | 45 | | case ExpressionType.LessThanOrEqual: |
| | 46 | | case ExpressionType.GreaterThan: |
| | 47 | | case ExpressionType.GreaterThanOrEqual: |
| | 48 | |
|
| 800 | 49 | | Expression normalizedLeft = UnwrapObjectConvert(visited.Left); |
| 800 | 50 | | Expression normalizedRight = UnwrapObjectConvert(visited.Right); |
| 800 | 51 | | if (normalizedLeft != visited.Left || normalizedRight != visited.Right) |
| | 52 | | { |
| 26 | 53 | | visited = CreateRelationalOperator(visited.NodeType, normalizedLeft, normalizedRight); |
| | 54 | | } |
| | 55 | | break; |
| | 56 | | } |
| | 57 | |
|
| 1148 | 58 | | if (_patterns.TryGetValue(visited.Left, out Pattern pattern) && pattern.Kind == PatternKind.Compare && IsCon |
| | 59 | | { |
| 64 | 60 | | ComparePattern comparePattern = (ComparePattern)pattern; |
| 64 | 61 | | if (TryCreateRelationalOperator(visited.NodeType, comparePattern.Left, comparePattern.Right, out BinaryE |
| | 62 | | { |
| 64 | 63 | | visited = relationalExpression; |
| | 64 | | } |
| | 65 | | } |
| | 66 | |
|
| 1148 | 67 | | RecordRewrite(b, visited); |
| | 68 | |
|
| 1148 | 69 | | return visited; |
| | 70 | | } |
| | 71 | |
|
| | 72 | | internal override Expression VisitUnary(UnaryExpression u) |
| | 73 | | { |
| 50 | 74 | | UnaryExpression visited = (UnaryExpression)base.VisitUnary(u); |
| 50 | 75 | | Expression result = visited; |
| | 76 | |
|
| 50 | 77 | | RecordRewrite(u, result); |
| | 78 | |
|
| 50 | 79 | | return result; |
| | 80 | | } |
| | 81 | |
|
| | 82 | | private static Expression UnwrapObjectConvert(Expression input) |
| | 83 | | { |
| 1600 | 84 | | if (input.NodeType == ExpressionType.Constant && input.Type == typeof(object)) |
| | 85 | | { |
| 0 | 86 | | ConstantExpression constant = (ConstantExpression)input; |
| | 87 | |
|
| 0 | 88 | | if (constant.Value != null && |
| 0 | 89 | | constant.Value.GetType() != typeof(object)) |
| | 90 | | { |
| 0 | 91 | | return Expression.Constant(constant.Value, constant.Value.GetType()); |
| | 92 | | } |
| | 93 | | } |
| | 94 | |
|
| 1626 | 95 | | while (ExpressionType.Convert == input.NodeType) |
| | 96 | | { |
| 26 | 97 | | input = ((UnaryExpression)input).Operand; |
| | 98 | | } |
| | 99 | |
|
| 1600 | 100 | | return input; |
| | 101 | | } |
| | 102 | |
|
| | 103 | | private static bool IsConstantZero(Expression expression) |
| | 104 | | { |
| 64 | 105 | | return expression.NodeType == ExpressionType.Constant && |
| 64 | 106 | | ((ConstantExpression)expression).Value.Equals(0); |
| | 107 | | } |
| | 108 | |
|
| | 109 | | internal override Expression VisitMethodCall(MethodCallExpression call) |
| | 110 | | { |
| 276 | 111 | | Expression visited = VisitMethodCallNoRewrite(call); |
| 276 | 112 | | RecordRewrite(call, visited); |
| 276 | 113 | | return visited; |
| | 114 | | } |
| | 115 | |
|
| | 116 | | internal Expression VisitMethodCallNoRewrite(MethodCallExpression call) |
| | 117 | | { |
| 276 | 118 | | MethodCallExpression visited = (MethodCallExpression)base.VisitMethodCall(call); |
| | 119 | |
|
| 276 | 120 | | if (visited.Method.IsStatic && visited.Method.Name == "Equals" && visited.Arguments.Count > 1) |
| | 121 | | { |
| 0 | 122 | | return Expression.Equal(visited.Arguments[0], visited.Arguments[1], false, visited.Method); |
| | 123 | | } |
| | 124 | |
|
| 276 | 125 | | if (!visited.Method.IsStatic && visited.Method.Name == "Equals" && visited.Arguments.Count > 0) |
| | 126 | | { |
| 0 | 127 | | return CreateRelationalOperator(ExpressionType.Equal, visited.Object, visited.Arguments[0]); |
| | 128 | | } |
| | 129 | |
|
| 276 | 130 | | if (visited.Method.IsStatic && visited.Method.Name == "CompareString" && visited.Method.DeclaringType.FullNa |
| | 131 | | { |
| 0 | 132 | | return CreateCompareExpression(visited.Arguments[0], visited.Arguments[1]); |
| | 133 | | } |
| | 134 | |
|
| 276 | 135 | | if (!visited.Method.IsStatic && visited.Method.Name == "CompareTo" && visited.Arguments.Count == 1 && visite |
| | 136 | | { |
| 64 | 137 | | return CreateCompareExpression(visited.Object, visited.Arguments[0]); |
| | 138 | | } |
| | 139 | |
|
| 212 | 140 | | if (visited.Method.IsStatic && visited.Method.Name == "Compare" && visited.Arguments.Count > 1 && visited.Me |
| | 141 | | { |
| 0 | 142 | | return CreateCompareExpression(visited.Arguments[0], visited.Arguments[1]); |
| | 143 | | } |
| | 144 | |
|
| 212 | 145 | | if (ReflectionUtil.s_dictionaryMethodInfosHash.Contains(visited.Method) && visited.Arguments.Count == 1 && v |
| | 146 | | { |
| 212 | 147 | | return visited; |
| | 148 | | } |
| | 149 | |
|
| 0 | 150 | | throw new NotSupportedException($"Method {visited.Method.Name} not supported."); |
| | 151 | | } |
| | 152 | |
|
| 2 | 153 | | private static readonly MethodInfo StaticRelationalOperatorPlaceholderMethod = typeof(ExpressionNormalizer).GetM |
| | 154 | |
|
| | 155 | | private static bool RelationalOperatorPlaceholder<TLeft, TRight>(TLeft left, TRight right) |
| | 156 | | { |
| | 157 | | Debug.Assert(false, "This method should never be called. It exists merely to support creation of relational |
| 0 | 158 | | return object.ReferenceEquals(left, right); |
| | 159 | | } |
| | 160 | |
|
| | 161 | | private static BinaryExpression CreateRelationalOperator(ExpressionType op, Expression left, Expression right) |
| | 162 | | { |
| 154 | 163 | | if (!TryCreateRelationalOperator(op, left, right, out BinaryExpression result)) |
| | 164 | | { |
| | 165 | | Debug.Assert(false, "CreateRelationalOperator has unknown op " + op); |
| | 166 | | } |
| | 167 | |
|
| 154 | 168 | | return result; |
| | 169 | | } |
| | 170 | |
|
| | 171 | | private static bool TryCreateRelationalOperator(ExpressionType op, Expression left, Expression right, out Binary |
| | 172 | | { |
| 218 | 173 | | MethodInfo relationalOperatorPlaceholderMethod = StaticRelationalOperatorPlaceholderMethod.MakeGenericMethod |
| | 174 | |
|
| | 175 | | switch (op) |
| | 176 | | { |
| | 177 | | case ExpressionType.Equal: |
| 64 | 178 | | result = Expression.Equal(left, right, LiftToNull, relationalOperatorPlaceholderMethod); |
| 64 | 179 | | return true; |
| | 180 | |
|
| | 181 | | case ExpressionType.NotEqual: |
| 0 | 182 | | result = Expression.NotEqual(left, right, LiftToNull, relationalOperatorPlaceholderMethod); |
| 0 | 183 | | return true; |
| | 184 | |
|
| | 185 | | case ExpressionType.LessThan: |
| 12 | 186 | | result = Expression.LessThan(left, right, LiftToNull, relationalOperatorPlaceholderMethod); |
| 12 | 187 | | return true; |
| | 188 | |
|
| | 189 | | case ExpressionType.LessThanOrEqual: |
| 4 | 190 | | result = Expression.LessThanOrEqual(left, right, LiftToNull, relationalOperatorPlaceholderMethod); |
| 4 | 191 | | return true; |
| | 192 | |
|
| | 193 | | case ExpressionType.GreaterThan: |
| 68 | 194 | | result = Expression.GreaterThan(left, right, LiftToNull, relationalOperatorPlaceholderMethod); |
| 68 | 195 | | return true; |
| | 196 | |
|
| | 197 | | case ExpressionType.GreaterThanOrEqual: |
| 70 | 198 | | result = Expression.GreaterThanOrEqual(left, right, LiftToNull, relationalOperatorPlaceholderMethod) |
| 70 | 199 | | return true; |
| | 200 | |
|
| | 201 | | default: |
| 0 | 202 | | result = null; |
| 0 | 203 | | return false; |
| | 204 | | } |
| | 205 | | } |
| | 206 | |
|
| | 207 | | private Expression CreateCompareExpression(Expression left, Expression right) |
| | 208 | | { |
| 64 | 209 | | Expression result = Expression.Condition( |
| 64 | 210 | | CreateRelationalOperator(ExpressionType.Equal, left, right), |
| 64 | 211 | | Expression.Constant(0), |
| 64 | 212 | | Expression.Condition( |
| 64 | 213 | | CreateRelationalOperator(ExpressionType.GreaterThan, left, right), |
| 64 | 214 | | Expression.Constant(1), |
| 64 | 215 | | Expression.Constant(-1))); |
| | 216 | |
|
| 64 | 217 | | _patterns[result] = new ComparePattern(left, right); |
| | 218 | |
|
| 64 | 219 | | return result; |
| | 220 | | } |
| | 221 | |
|
| | 222 | | private void RecordRewrite(Expression source, Expression rewritten) |
| | 223 | | { |
| | 224 | | Debug.Assert(source != null, "source != null"); |
| | 225 | | Debug.Assert(rewritten != null, "rewritten != null"); |
| | 226 | | Debug.Assert(NormalizerRewrites != null, "this.NormalizerRewrites != null"); |
| | 227 | |
|
| 1474 | 228 | | if (source != rewritten) |
| | 229 | | { |
| 294 | 230 | | NormalizerRewrites.Add(rewritten, source); |
| | 231 | | } |
| 1474 | 232 | | } |
| | 233 | |
|
| | 234 | | private abstract class Pattern |
| | 235 | | { |
| | 236 | | internal abstract PatternKind Kind { get; } |
| | 237 | | } |
| | 238 | |
|
| | 239 | | private enum PatternKind |
| | 240 | | { |
| | 241 | | Compare, |
| | 242 | | } |
| | 243 | |
|
| | 244 | | private sealed class ComparePattern : Pattern |
| | 245 | | { |
| 64 | 246 | | internal ComparePattern(Expression left, Expression right) |
| | 247 | | { |
| 64 | 248 | | Left = left; |
| 64 | 249 | | Right = right; |
| 64 | 250 | | } |
| | 251 | |
|
| | 252 | | internal readonly Expression Left; |
| | 253 | |
|
| | 254 | | internal readonly Expression Right; |
| | 255 | |
|
| | 256 | | internal override PatternKind Kind |
| | 257 | | { |
| 64 | 258 | | get { return PatternKind.Compare; } |
| | 259 | | } |
| | 260 | | } |
| | 261 | |
|
| | 262 | | } |
| | 263 | | } |