File: SqlDeflator.cs

package info (click to toggle)
mono 6.12.0.199%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 1,296,836 kB
  • sloc: cs: 11,181,803; xml: 2,850,076; ansic: 699,709; cpp: 123,344; perl: 59,361; javascript: 30,841; asm: 21,853; makefile: 20,405; sh: 15,009; python: 4,839; pascal: 925; sql: 859; sed: 16; php: 1
file content (503 lines) | stat: -rw-r--r-- 21,269 bytes parent folder | download | duplicates (6)
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace System.Data.Linq.SqlClient {

    internal class SqlDeflator {
        SqlValueDeflator vDeflator;
        SqlColumnDeflator cDeflator;
        SqlAliasDeflator aDeflator;
        SqlTopSelectDeflator tsDeflator;
        SqlDuplicateColumnDeflator dupColumnDeflator;

        internal SqlDeflator() {
            this.vDeflator = new SqlValueDeflator();
            this.cDeflator = new SqlColumnDeflator();
            this.aDeflator = new SqlAliasDeflator();
            this.tsDeflator = new SqlTopSelectDeflator();
            this.dupColumnDeflator = new SqlDuplicateColumnDeflator();
        }

        internal SqlNode Deflate(SqlNode node) {
            node = this.vDeflator.Visit(node);
            node = this.cDeflator.Visit(node);
            node = this.aDeflator.Visit(node);
            node = this.tsDeflator.Visit(node);
            node = this.dupColumnDeflator.Visit(node);
            return node;
        }

        // remove references to literal values
        class SqlValueDeflator : SqlVisitor {
            SelectionDeflator sDeflator;
            bool isTopLevel = true;

            internal SqlValueDeflator() {
                this.sDeflator = new SelectionDeflator();
            }

            internal override SqlSelect VisitSelect(SqlSelect select) {
                if (this.isTopLevel) {
                    select.Selection = sDeflator.VisitExpression(select.Selection);
                }
                return select;
            }

            internal override SqlExpression VisitSubSelect(SqlSubSelect ss) {
                bool saveIsTopLevel = this.isTopLevel;
                try {
                    return base.VisitSubSelect(ss);
                }
                finally {
                    this.isTopLevel = saveIsTopLevel;
                }
            }

            class SelectionDeflator : SqlVisitor {
                internal override SqlExpression VisitColumnRef(SqlColumnRef cref) {
                    SqlExpression literal = this.GetLiteralValue(cref);
                    if (literal != null) {
                        return literal;
                    }
                    return cref;
                }

                private SqlValue GetLiteralValue(SqlExpression expr) {
                    while (expr != null && expr.NodeType == SqlNodeType.ColumnRef) {
                        expr = ((SqlColumnRef)expr).Column.Expression;
                    }
                    return expr as SqlValue;
                }
            }
        }

        // remove unreferenced items in projection list
        class SqlColumnDeflator : SqlVisitor {
            Dictionary<SqlNode, SqlNode> referenceMap;
            bool isTopLevel;
            bool forceReferenceAll;
            SqlAggregateChecker aggregateChecker;

            internal SqlColumnDeflator() {
                this.referenceMap = new Dictionary<SqlNode, SqlNode>();
                this.aggregateChecker = new SqlAggregateChecker();
                this.isTopLevel = true;
            }

            internal override SqlExpression VisitColumnRef(SqlColumnRef cref) {
                this.referenceMap[cref.Column] = cref.Column;
                return cref;
            }

            internal override SqlExpression VisitScalarSubSelect(SqlSubSelect ss) {
                bool saveIsTopLevel = this.isTopLevel;
                this.isTopLevel = false;
                bool saveForceReferenceAll = this.forceReferenceAll;
                this.forceReferenceAll = true;
                try {
                    return base.VisitScalarSubSelect(ss);
                }
                finally {
                    this.isTopLevel = saveIsTopLevel;
                    this.forceReferenceAll = saveForceReferenceAll;
                }
            }

            internal override SqlExpression VisitExists(SqlSubSelect ss) {
                bool saveIsTopLevel = this.isTopLevel;
                this.isTopLevel = false;
                try {
                    return base.VisitExists(ss);
                }
                finally {
                    this.isTopLevel = saveIsTopLevel;
                }
            }
            
            internal override SqlNode VisitUnion(SqlUnion su) {
                bool saveForceReferenceAll = this.forceReferenceAll;
                this.forceReferenceAll = true;
                su.Left = this.Visit(su.Left);
                su.Right = this.Visit(su.Right);
                this.forceReferenceAll = saveForceReferenceAll;
                return su;
            }

            internal override SqlSelect VisitSelect(SqlSelect select) {
                bool saveForceReferenceAll = this.forceReferenceAll;
                this.forceReferenceAll = false;
                bool saveIsTopLevel = this.isTopLevel;

                try {
                    if (this.isTopLevel) {
                        // top-level projection references columns!
                        select.Selection = this.VisitExpression(select.Selection);
                    }
                    this.isTopLevel = false;

                    for (int i = select.Row.Columns.Count - 1; i >= 0; i--) {
                        SqlColumn c = select.Row.Columns[i];

                        bool safeToRemove =
                            !saveForceReferenceAll
                            && !this.referenceMap.ContainsKey(c)
                            // don't remove anything from a distinct select (except maybe a literal value) since it would change the meaning of the comparison
                            && !select.IsDistinct
                            // don't remove an aggregate expression that may be the only expression that forces the grouping (since it would change the cardinality of the results)
                            && !(select.GroupBy.Count == 0 && this.aggregateChecker.HasAggregates(c.Expression)); 

                        if (safeToRemove) {
                            select.Row.Columns.RemoveAt(i);
                        }
                        else {
                            this.VisitExpression(c.Expression);
                        }
                    }

                    select.Top = this.VisitExpression(select.Top);
                    for (int i = select.OrderBy.Count - 1; i >= 0; i--) {
                        select.OrderBy[i].Expression = this.VisitExpression(select.OrderBy[i].Expression);
                    }

                    select.Having = this.VisitExpression(select.Having);
                    for (int i = select.GroupBy.Count - 1; i >= 0; i--) {
                        select.GroupBy[i] = this.VisitExpression(select.GroupBy[i]);
                    }

                    select.Where = this.VisitExpression(select.Where);
                    select.From = this.VisitSource(select.From);
                }
                finally {
                    this.isTopLevel = saveIsTopLevel;
                    this.forceReferenceAll = saveForceReferenceAll;
                }

                return select;
            }

            internal override SqlSource VisitJoin(SqlJoin join) {
                join.Condition = this.VisitExpression(join.Condition);
                join.Right = this.VisitSource(join.Right);
                join.Left = this.VisitSource(join.Left);
                return join;
            }

            internal override SqlNode VisitLink(SqlLink link) {
                // don't visit expansion...
                for (int i = 0, n = link.KeyExpressions.Count; i < n; i++) {
                    link.KeyExpressions[i] = this.VisitExpression(link.KeyExpressions[i]);
                }
                return link;
            }
        }

        class SqlColumnEqualizer : SqlVisitor {
            Dictionary<SqlColumn, SqlColumn> map;

            internal SqlColumnEqualizer() {
            }

            internal void BuildEqivalenceMap(SqlSource scope) {
                this.map = new Dictionary<SqlColumn, SqlColumn>();
                this.Visit(scope);
            }

            internal bool AreEquivalent(SqlExpression e1, SqlExpression e2) {
                if (SqlComparer.AreEqual(e1, e2))
                    return true;

                SqlColumnRef cr1 = e1 as SqlColumnRef;
                SqlColumnRef cr2 = e2 as SqlColumnRef;
                 
                if (cr1 != null && cr2 != null) {
                    SqlColumn c1 = cr1.GetRootColumn();
                    SqlColumn c2 = cr2.GetRootColumn();
                    SqlColumn r;
                    return this.map.TryGetValue(c1, out r) && r == c2;
                }

                return false;
            }

            internal override SqlSource VisitJoin(SqlJoin join) {
                base.VisitJoin(join);
                if (join.Condition != null) {
                    this.CheckJoinCondition(join.Condition);
                }
                return join;
            }

            internal override SqlSelect VisitSelect(SqlSelect select) {
                base.VisitSelect(select);
                if (select.Where != null) {
                    this.CheckJoinCondition(select.Where);
                }
                return select;
            }

            [SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily", Justification="Microsoft: Cast is dependent on node type and casts do not happen unecessarily in a single code path.")]
            private void CheckJoinCondition(SqlExpression expr) {
                switch (expr.NodeType) {
                    case SqlNodeType.And: {
                        SqlBinary b = (SqlBinary)expr;
                        CheckJoinCondition(b.Left);
                        CheckJoinCondition(b.Right);
                        break;
                    }
                    case SqlNodeType.EQ:
                    case SqlNodeType.EQ2V: {
                        SqlBinary b = (SqlBinary)expr;
                        SqlColumnRef crLeft = b.Left as SqlColumnRef;
                        SqlColumnRef crRight = b.Right as SqlColumnRef;
                        if (crLeft != null && crRight != null) {
                            SqlColumn cLeft = crLeft.GetRootColumn();
                            SqlColumn cRight = crRight.GetRootColumn();
                            this.map[cLeft] = cRight;
                            this.map[cRight] = cLeft;
                        }
                        break;
                    }
                }
            }

            internal override SqlExpression VisitSubSelect(SqlSubSelect ss) {
                return ss;
            }
        }

        // remove redundant/trivial aliases
        class SqlAliasDeflator : SqlVisitor {
            Dictionary<SqlAlias, SqlAlias> removedMap;

            internal SqlAliasDeflator() {
                this.removedMap = new Dictionary<SqlAlias, SqlAlias>();
            }

            internal override SqlExpression VisitAliasRef(SqlAliasRef aref) {
                SqlAlias alias = aref.Alias;
                SqlAlias value;
                if (this.removedMap.TryGetValue(alias, out value)) {
                    throw Error.InvalidReferenceToRemovedAliasDuringDeflation();
                }
                return aref;
            }

            internal override SqlExpression VisitColumnRef(SqlColumnRef cref) {
                if (cref.Column.Alias != null && this.removedMap.ContainsKey(cref.Column.Alias)) {
                    SqlColumnRef c = cref.Column.Expression as SqlColumnRef;
                    if (c != null) {
                        //The following code checks for cases where there are differences between the type returned
                        //by a ColumnRef and the column that refers to it.  This situation can occur when conversions
                        //are optimized out of the SQL node tree.  As mentioned in the SetClrType comments this is not 
                        //an operation that can have adverse effects and should only be used in limited cases, such as
                        //this one.
                        if (c.ClrType != cref.ClrType) {
                            c.SetClrType(cref.ClrType);
                            return this.VisitColumnRef(c);
                        }
                    }
                    return c;
                }
                return cref;
            }

            internal override SqlSource VisitSource(SqlSource node) {
                node = (SqlSource)this.Visit(node);
                SqlAlias alias = node as SqlAlias;
                if (alias != null) {
                    SqlSelect sel = alias.Node as SqlSelect;
                    if (sel != null && this.IsTrivialSelect(sel)) {
                        this.removedMap[alias] = alias;
                        node = sel.From;
                    }
                }
                return node;
            }

            internal override SqlSource VisitJoin(SqlJoin join) {
                base.VisitJoin(join);
                switch (join.JoinType) {
                    case SqlJoinType.Cross:
                    case SqlJoinType.Inner:
                        // reducing either side would effect cardinality of results
                        break;
                    case SqlJoinType.LeftOuter:
                    case SqlJoinType.CrossApply:
                    case SqlJoinType.OuterApply:
                        // may reduce to left if no references to the right
                        if (this.HasEmptySource(join.Right)) {
                            SqlAlias a = (SqlAlias)join.Right;
                            this.removedMap[a] = a;
                            return join.Left;
                        }
                        break;
                }
                return join;
            }

            private bool IsTrivialSelect(SqlSelect select) {
                if (select.OrderBy.Count != 0 ||
                    select.GroupBy.Count != 0 ||
                    select.Having != null ||
                    select.Top != null ||
                    select.IsDistinct ||
                    select.Where != null)
                    return false;
                return this.HasTrivialSource(select.From) && this.HasTrivialProjection(select);
            }

            private bool HasTrivialSource(SqlSource node) {
                SqlJoin join = node as SqlJoin;
                if (join != null) {
                    return this.HasTrivialSource(join.Left) &&
                           this.HasTrivialSource(join.Right);
                }
                return node is SqlAlias;
            }

            [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification="Unknown reason.")]
            private bool HasTrivialProjection(SqlSelect select) {
                foreach (SqlColumn c in select.Row.Columns) {
                    if (c.Expression != null && c.Expression.NodeType != SqlNodeType.ColumnRef) {
                        return false;
                    }
                }
                return true;
            }

            [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification="Unknown reason.")]
            private bool HasEmptySource(SqlSource node) {
                SqlAlias alias = node as SqlAlias;
                if (alias == null) return false;
                SqlSelect sel = alias.Node as SqlSelect;
                if (sel == null) return false;
                return sel.Row.Columns.Count == 0 &&
                       sel.From == null &&
                       sel.Where == null &&
                       sel.GroupBy.Count == 0 &&
                       sel.Having == null &&
                       sel.OrderBy.Count == 0;
            }
        }

        // remove duplicate columns from order by and group by lists
        class SqlDuplicateColumnDeflator : SqlVisitor
        {
            SqlColumnEqualizer equalizer = new SqlColumnEqualizer();

            internal override SqlSelect VisitSelect(SqlSelect select) {
                select.From = this.VisitSource(select.From);
                select.Where = this.VisitExpression(select.Where);
                for (int i = 0, n = select.GroupBy.Count; i < n; i++)
                {
                    select.GroupBy[i] = this.VisitExpression(select.GroupBy[i]);
                }
                // remove duplicate group expressions
                for (int i = select.GroupBy.Count - 1; i >= 0; i--)
                {
                    for (int j = i - 1; j >= 0; j--)
                    {
                        if (SqlComparer.AreEqual(select.GroupBy[i], select.GroupBy[j]))
                        {
                            select.GroupBy.RemoveAt(i);
                            break;
                        }
                    }
                }
                select.Having = this.VisitExpression(select.Having);
                for (int i = 0, n = select.OrderBy.Count; i < n; i++)
                {
                    select.OrderBy[i].Expression = this.VisitExpression(select.OrderBy[i].Expression);
                }
                // remove duplicate order expressions
                if (select.OrderBy.Count > 0)
                {
                    this.equalizer.BuildEqivalenceMap(select.From);

                    for (int i = select.OrderBy.Count - 1; i >= 0; i--)
                    {
                        for (int j = i - 1; j >= 0; j--)
                        {
                            if (this.equalizer.AreEquivalent(select.OrderBy[i].Expression, select.OrderBy[j].Expression))
                            {
                                select.OrderBy.RemoveAt(i);
                                break;
                            }
                        }
                    }
                }
                select.Top = this.VisitExpression(select.Top);
                select.Row = (SqlRow)this.Visit(select.Row);
                select.Selection = this.VisitExpression(select.Selection);
                return select;
            }
        }

        // if the top level select is simply a reprojection of the subquery, then remove it,
        // pushing any distinct names down
        class SqlTopSelectDeflator : SqlVisitor {

            internal override SqlSelect VisitSelect(SqlSelect select) {
                if (IsTrivialSelect(select)) {
                    SqlSelect aselect = (SqlSelect)((SqlAlias)select.From).Node;
                    // build up a column map, so we can rewrite the top-level selection expression
                    Dictionary<SqlColumn, SqlColumnRef> map = new Dictionary<SqlColumn, SqlColumnRef>();
                    foreach (SqlColumn c in select.Row.Columns) {
                        SqlColumnRef cref = (SqlColumnRef)c.Expression;
                        map.Add(c, cref);
                        // push the interesting column names down (non null)
                        if (!string.IsNullOrEmpty(c.Name)) {
                            cref.Column.Name = c.Name;
                        }
                    }
                    aselect.Selection = new ColumnMapper(map).VisitExpression(select.Selection);
                    return aselect;
                }
                return select;
            }

            private bool IsTrivialSelect(SqlSelect select) {
                if (select.OrderBy.Count != 0 ||
                    select.GroupBy.Count != 0 ||
                    select.Having != null ||
                    select.Top != null ||
                    select.IsDistinct ||
                    select.Where != null)
                    return false;
                return this.HasTrivialSource(select.From) && this.HasTrivialProjection(select);
            }

            [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification="Unknown reason.")]
            private bool HasTrivialSource(SqlSource node) {
                SqlAlias alias = node as SqlAlias;
                if (alias == null) return false;
                return alias.Node is SqlSelect;
            }

            [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic", Justification="Unknown reason.")]
            private bool HasTrivialProjection(SqlSelect select) {
                foreach (SqlColumn c in select.Row.Columns) {
                    if (c.Expression != null && c.Expression.NodeType != SqlNodeType.ColumnRef) {
                        return false;
                    }
                }
                return true;
            }

            class ColumnMapper : SqlVisitor {
                Dictionary<SqlColumn, SqlColumnRef> map;
                internal ColumnMapper(Dictionary<SqlColumn, SqlColumnRef> map) {
                    this.map = map;
                }
                internal override SqlExpression VisitColumnRef(SqlColumnRef cref) {
                    SqlColumnRef mapped;
                    if (this.map.TryGetValue(cref.Column, out mapped)) {
                        return mapped;
                    }
                    return cref;
                }
            }
        }
    }
}