forked from dolthub/go-mysql-server
/
prune_columns.go
401 lines (347 loc) · 11.6 KB
/
prune_columns.go
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
// Copyright 2020-2021 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package analyzer
import (
"fmt"
"github.com/gabereiser/go-mysql-server/sql/transform"
"github.com/gabereiser/go-mysql-server/sql"
"github.com/gabereiser/go-mysql-server/sql/expression"
"github.com/gabereiser/go-mysql-server/sql/plan"
)
type usedColumns map[string]map[string]struct{}
func (uc usedColumns) add(table, col string) {
if _, ok := uc[table]; !ok {
uc[table] = make(map[string]struct{})
}
uc[table][col] = struct{}{}
}
func (uc usedColumns) has(table, col string) bool {
if _, ok := uc[table]; !ok {
return false
}
_, ok := uc[table][col]
return ok
}
// pruneColumns removes unneeded columns from Project and GroupBy nodes. It also rewrites field indexes as necessary,
// even if no columns were pruned. This is especially important for subqueries -- this function handles fixing field
// indexes when the outer scope schema changes as a result of other analyzer functions.
// TODO: as a result of reordering this rule to facilitate join planning, pruneColumns will not be called
// in a scope with a join node.
// TODO: this should be deprecated in favor of pruneTables (which will be renamed pruneColumns)
func pruneColumns(ctx *sql.Context, a *Analyzer, node sql.Node, scope *Scope, sel RuleSelector) (sql.Node, transform.TreeIdentity, error) {
if !node.Resolved() {
return node, transform.SameTree, nil
}
// Skip pruning columns for insert statements. For inserts involving a select (INSERT INTO table1 SELECT a,b FROM
// table2), all columns from the select are used for the insert, and error checking for schema compatibility
// happens at execution time. Otherwise the logic below will convert a Project to a ResolvedTable for the selected
// table, which can alter the column order of the select.
switch n := node.(type) {
case *plan.InsertInto, *plan.CreateTrigger:
return n, transform.SameTree, nil
}
if !pruneColumnsIsSafe(node) {
a.Log("not pruning columns because it is not safe.")
return node, transform.SameTree, nil
}
columns := columnsUsedByNode(node)
findUsedColumns(columns, node)
n, sameC, err := pruneUnusedColumns(a, node, columns)
if err != nil {
return nil, transform.SameTree, err
}
n, sameSq, err := pruneSubqueries(ctx, a, n, columns)
if err != nil {
return nil, transform.SameTree, err
}
n, sameFi, err := fixRemainingFieldsIndexes(ctx, a, n, scope)
return n, sameC && sameSq && sameFi, err
}
func pruneColumnsIsSafe(n sql.Node) bool {
isSafe := true
// We do not run pruneColumns if there is a Subquery
// expression, because the field rewrites and scope handling
// in here are not principled.
transform.InspectExpressions(n, func(e sql.Expression) bool {
if _, ok := e.(*plan.Subquery); ok {
isSafe = false
}
return isSafe
})
if !isSafe {
return false
}
// We cannot eliminate projections in RecursiveCte's,
// they are used implicitly in the recursive execution
// if not explicitly in the outer scope.
transform.Inspect(n, func(n sql.Node) bool {
switch n.(type) {
case *plan.RecursiveCte, *plan.Update, *plan.JoinNode:
isSafe = false
default:
}
return isSafe
})
return isSafe
}
func columnsUsedByNode(n sql.Node) usedColumns {
columns := make(usedColumns)
for _, col := range n.Schema() {
columns.add(col.Source, col.Name)
}
return columns
}
func canPruneChild(c transform.Context) bool {
j, ok := c.Parent.(*plan.JoinNode)
if !ok {
return true
}
return j.Op.IsPhysical()
}
func pruneSubqueryColumns(
ctx *sql.Context,
a *Analyzer,
n *plan.SubqueryAlias,
parentColumns usedColumns,
) (sql.Node, transform.TreeIdentity, error) {
a.Log("pruning columns of subquery with alias %q", n.Name())
columns := make(usedColumns)
// The columns coming from the parent have the subquery alias name as the source. We need to find the real table in
// order to prune the subquery correctly. The columns might also have been renamed.
tableByCol := make(map[string]tableCol)
for i, col := range n.Child.Schema() {
name := col.Name
if len(n.Columns) > 0 {
name = n.Columns[i]
}
tableByCol[name] = tableCol{table: col.Source, col: col.Name}
}
for col := range parentColumns[n.Name()] {
table, ok := tableByCol[col]
if !ok {
return nil, transform.SameTree, fmt.Errorf("this is likely a bug: missing projected column %q on subquery %q", col, n.Name())
}
columns.add(table.table, table.col)
}
findUsedColumns(columns, n.Child)
node, sameCols, err := pruneUnusedColumns(a, n.Child, columns)
if err != nil {
return nil, transform.SameTree, err
}
node, sameSq, err := pruneSubqueries(ctx, a, node, columns)
if err != nil {
return nil, transform.SameTree, err
}
same := sameCols && sameSq
if len(n.Columns) > 0 {
schemaLen := schemaLength(node)
if schemaLen != len(n.Columns) {
n = n.WithColumns(n.Columns[:schemaLen])
same = transform.NewTree
}
}
// There is no need to fix the field indexes after pruning here
// because the main query will take care of fixing the indexes of all the
// nodes in the tree.
if same {
return node, transform.SameTree, err
}
newn, err := n.WithChildren(node)
return newn, transform.NewTree, err
}
func findUsedColumns(columns usedColumns, n sql.Node) {
transform.Inspect(n, func(n sql.Node) bool {
switch n := n.(type) {
case *plan.Project:
addUsedProjectColumns(columns, n.Projections)
return true
case *plan.GroupBy:
addUsedProjectColumns(columns, n.SelectedExprs)
addUsedColumns(columns, n.GroupByExprs)
return true
case *plan.SubqueryAlias:
// TODO: inspect subquery for references to outer scope nodes
return false
}
exp, ok := n.(sql.Expressioner)
if ok {
addUsedColumns(columns, exp.Expressions())
}
return true
})
}
func addUsedProjectColumns(columns usedColumns, projection []sql.Expression) {
var candidates []sql.Expression
for _, e := range projection {
sql.Inspect(e, func(e sql.Expression) bool {
if e == nil {
return false
}
// TODO: not all of the columns mentioned in the subquery are relevant, just the ones that reference the outer scope
if sub, ok := e.(*plan.Subquery); ok {
findUsedColumns(columns, sub.Query)
return false
}
// Only check for expressions that are not directly a GetField. This
// is because in a projection we only care about those that were used
// to compute new columns, such as aliases and so on. The fields that
// are just passed up in the tree will already be in some other part
// if they are really used.
if _, ok := e.(*expression.GetField); !ok {
candidates = append(candidates, e)
}
return true
})
}
addUsedColumns(columns, candidates)
}
func addUsedColumns(columns usedColumns, exprs []sql.Expression) {
for _, e := range exprs {
sql.Inspect(e, func(e sql.Expression) bool {
if gf, ok := e.(*expression.GetField); ok {
columns.add(gf.Table(), gf.Name())
}
return true
})
}
}
func pruneSubqueries(
ctx *sql.Context,
a *Analyzer,
n sql.Node,
parentColumns usedColumns,
) (sql.Node, transform.TreeIdentity, error) {
return transform.NodeWithCtx(n, canPruneChild, func(c transform.Context) (sql.Node, transform.TreeIdentity, error) {
subq, ok := c.Node.(*plan.SubqueryAlias)
if !ok {
return c.Node, transform.SameTree, nil
}
return pruneSubqueryColumns(ctx, a, subq, parentColumns)
})
}
func pruneUnusedColumns(a *Analyzer, n sql.Node, columns usedColumns) (sql.Node, transform.TreeIdentity, error) {
return transform.NodeWithCtx(n, canPruneChild, func(c transform.Context) (sql.Node, transform.TreeIdentity, error) {
switch n := c.Node.(type) {
case *plan.Project:
return pruneProject(a, n, columns)
case *plan.GroupBy:
return pruneGroupBy(a, n, columns)
default:
return n, transform.SameTree, nil
}
})
}
func pruneProject(a *Analyzer, n *plan.Project, columns usedColumns) (sql.Node, transform.TreeIdentity, error) {
var remaining []sql.Expression
for _, e := range n.Projections {
if !shouldPruneExpr(e, columns) {
remaining = append(remaining, e)
} else {
a.Log("Pruned project expression %s", e)
}
}
if len(remaining) == 0 {
a.Log("Replacing empty project %s node with child %s", n, n.Child)
return n.Child, transform.NewTree, nil
}
if len(remaining) == len(n.Projections) {
return n, transform.SameTree, nil
}
return plan.NewProject(remaining, n.Child), transform.NewTree, nil
}
func pruneGroupBy(a *Analyzer, n *plan.GroupBy, columns usedColumns) (sql.Node, transform.TreeIdentity, error) {
var remaining []sql.Expression
for _, e := range n.SelectedExprs {
if !shouldPruneExpr(e, columns) {
remaining = append(remaining, e)
} else {
a.Log("Pruned groupby expression %s", e)
}
}
if len(remaining) == 0 {
a.Log("Replacing empty groupby %s node with child %s", n, n.Child)
// TODO: this seems wrong, even if all projections are now gone we still need to do a grouping
return n.Child, transform.NewTree, nil
}
if len(remaining) == len(n.SelectedExprs) {
return n, transform.SameTree, nil
}
return plan.NewGroupBy(remaining, n.GroupByExprs, n.Child), transform.NewTree, nil
}
func shouldPruneExpr(e sql.Expression, cols usedColumns) bool {
gf, ok := e.(*expression.GetField)
if !ok {
return false
}
if gf.Table() == "" {
return false
}
return !cols.has(gf.Table(), gf.Name())
}
// TODO: figure out why FixFieldIndexes cannot be used instead of this,
// otherwise SystemDiff tests break.
func fixRemainingFieldsIndexes(ctx *sql.Context, a *Analyzer, node sql.Node, scope *Scope) (sql.Node, transform.TreeIdentity, error) {
return transform.NodeWithCtx(node, canPruneChild, func(c transform.Context) (sql.Node, transform.TreeIdentity, error) {
switch n := c.Node.(type) {
case sql.SchemaTarget:
// do nothing, column defaults have already been resolved
return node, transform.SameTree, nil
case *plan.SubqueryAlias:
if !n.OuterScopeVisibility {
return n, transform.SameTree, nil
}
child, same, err := fixRemainingFieldsIndexes(ctx, a, n.Child, scope)
if err != nil {
return nil, transform.SameTree, err
}
if same {
return n, transform.SameTree, nil
}
node, err := n.WithChildren(child)
if err != nil {
return nil, transform.SameTree, err
}
return node, transform.NewTree, nil
default:
if _, ok := n.(sql.Expressioner); !ok {
return n, transform.SameTree, nil
}
indexedCols, err := indexColumns(ctx, a, n, scope)
if err != nil {
return nil, transform.SameTree, err
}
if len(indexedCols) == 0 {
return n, transform.SameTree, nil
}
// IndexedTableAccess contains expressions in its lookupBuilder that we don't need to fix up, so skip them
if _, ok := n.(*plan.IndexedTableAccess); ok {
return n, transform.SameTree, nil
}
return transform.OneNodeExprsWithNode(n, func(_ sql.Node, e sql.Expression) (sql.Expression, transform.TreeIdentity, error) {
gf, ok := e.(*expression.GetField)
if !ok {
return e, transform.SameTree, nil
}
idx, ok := indexedCols[newTableCol(gf.Table(), gf.Name())]
if !ok {
return nil, transform.SameTree, sql.ErrTableColumnNotFound.New(gf.Table(), gf.Name())
}
if idx.index == gf.Index() {
return e, transform.SameTree, nil
}
return gf.WithIndex(idx.index), transform.NewTree, nil
})
}
})
}