forked from dolthub/go-mysql-server
/
apply_indexes_from_outer_scope.go
259 lines (221 loc) · 7.19 KB
/
apply_indexes_from_outer_scope.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
// 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 (
"strings"
"github.com/Sndav/go-mysql-server/sql"
"github.com/Sndav/go-mysql-server/sql/expression"
"github.com/Sndav/go-mysql-server/sql/plan"
)
// applyIndexesFromOuterScope attempts to apply an indexed lookup to a subquery using variables from the outer scope.
// It functions similarly to pushdownFilters, in that it applies an index to a table. But unlike that function, it must
// apply, effectively, an indexed join between two tables, one of which is defined in the outer scope. This is similar
// to the process in the join analyzer.
func applyIndexesFromOuterScope(ctx *sql.Context, a *Analyzer, n sql.Node, scope *Scope) (sql.Node, error) {
if scope == nil {
return n, nil
}
// this isn't good enough: we need to consider aliases defined in the outer scope as well for this analysis
tableAliases, err := getTableAliases(n, scope)
if err != nil {
return nil, err
}
indexLookups, err := getOuterScopeIndexes(ctx, a, n, scope, tableAliases)
if err != nil {
return nil, err
}
if len(indexLookups) == 0 {
return n, nil
}
childSelector := func(parent sql.Node, child sql.Node, childNum int) bool {
switch parent.(type) {
// We can't push any indexes down a branch that have already had an index pushed down it
case *plan.IndexedTableAccess:
return false
}
return true
}
// replace the tables with possible index lookups with indexed access
for _, idxLookup := range indexLookups {
n, err = plan.TransformUpWithSelector(n, childSelector, func(n sql.Node) (sql.Node, error) {
switch n := n.(type) {
case *plan.IndexedTableAccess:
return n, nil
case *plan.TableAlias:
if strings.ToLower(n.Name()) == idxLookup.table {
return pushdownIndexToTable(a, n, idxLookup.index, idxLookup.keyExpr)
}
return n, nil
case *plan.ResolvedTable:
if strings.ToLower(n.Name()) == idxLookup.table {
return pushdownIndexToTable(a, n, idxLookup.index, idxLookup.keyExpr)
}
return n, nil
default:
return n, nil
}
})
if err != nil {
return nil, err
}
}
return n, nil
}
// pushdownIndexToTable attempts to push the index given down to the table given, if it implements
// sql.IndexAddressableTable
func pushdownIndexToTable(a *Analyzer, tableNode NameableNode, index sql.Index, keyExpr []sql.Expression) (sql.Node, error) {
return plan.TransformUp(tableNode, func(n sql.Node) (sql.Node, error) {
switch n := n.(type) {
case *plan.ResolvedTable:
table := getTable(tableNode)
if table == nil {
return n, nil
}
if _, ok := table.(sql.IndexAddressableTable); ok {
a.Log("table %q transformed with pushdown of index", tableNode.Name())
return plan.NewIndexedTableAccess(n, index, keyExpr), nil
}
}
return n, nil
})
}
type subqueryIndexLookup struct {
table string
keyExpr []sql.Expression
index sql.Index
}
func getOuterScopeIndexes(
ctx *sql.Context,
a *Analyzer,
node sql.Node,
scope *Scope,
tableAliases TableAliases,
) ([]subqueryIndexLookup, error) {
indexSpan, _ := ctx.Span("getOuterScopeIndexes")
defer indexSpan.Finish()
var indexes map[string]sql.Index
var exprsByTable joinExpressionsByTable
var err error
plan.Inspect(node, func(node sql.Node) bool {
switch node := node.(type) {
case *plan.Filter:
var indexAnalyzer *indexAnalyzer
indexAnalyzer, err = getIndexesForNode(ctx, a, node)
if err != nil {
return false
}
defer indexAnalyzer.releaseUsedIndexes()
indexes, exprsByTable, err = getSubqueryIndexes(ctx, a, node.Expression, scope, indexAnalyzer, tableAliases)
if err != nil {
return false
}
}
return true
})
if len(indexes) == 0 {
return nil, nil
}
var lookups []subqueryIndexLookup
for table, idx := range indexes {
if exprsByTable[table] != nil {
// creating a key expression can fail in some cases, just skip this table
keyExpr := createIndexKeyExpr(ctx, idx, exprsByTable[table], tableAliases)
if keyExpr == nil {
continue
}
lookups = append(lookups, subqueryIndexLookup{
table: table,
keyExpr: keyExpr,
index: idx,
})
}
}
return lookups, nil
}
// createIndexKeyExpr returns a slice of expressions to be used when creating an index lookup key for the table given.
func createIndexKeyExpr(ctx *sql.Context, idx sql.Index, joinExprs []*joinColExpr, tableAliases TableAliases) []sql.Expression {
keyExprs := make([]sql.Expression, len(idx.Expressions()))
IndexExpressions:
for i, idxExpr := range idx.Expressions() {
for j := range joinExprs {
if idxExpr == normalizeExpression(ctx, tableAliases, joinExprs[j].colExpr).String() {
keyExprs[i] = joinExprs[j].comparand
continue IndexExpressions
}
}
// If we finished the loop, we didn't match this index expression
return nil
}
return keyExprs
}
func getSubqueryIndexes(
ctx *sql.Context,
a *Analyzer,
e sql.Expression,
scope *Scope,
ia *indexAnalyzer,
tableAliases TableAliases,
) (map[string]sql.Index, joinExpressionsByTable, error) {
scopeLen := len(scope.Schema())
// build a list of candidate predicate expressions, those that might be used for an index lookup
var candidatePredicates []sql.Expression
for _, e := range splitConjunction(e) {
// We are only interested in expressions that involve an outer scope variable (those whose index is less than the
// scope length)
isScopeExpr := false
sql.Inspect(e, func(e sql.Expression) bool {
if gf, ok := e.(*expression.GetField); ok {
if gf.Index() < scopeLen {
isScopeExpr = true
return false
}
}
return true
})
if isScopeExpr {
candidatePredicates = append(candidatePredicates, e)
}
}
tablesInScope := tablesInScope(scope)
// group them by the table they reference
// TODO: this only works for equality, make it work for other operands
exprsByTable := joinExprsByTable(candidatePredicates)
result := make(map[string]sql.Index)
// For every predicate involving a table in the outer scope, see if there's an index lookup possible on its comparands
// (the tables in this scope)
for _, table := range tablesInScope {
indexCols := exprsByTable[table]
if indexCols != nil {
idx := ia.IndexByExpression(ctx, ctx.GetCurrentDatabase(),
normalizeExpressions(ctx, tableAliases, extractComparands(indexCols)...)...)
if idx != nil {
result[indexCols[0].comparandCol.Table()] = idx
}
}
}
return result, exprsByTable, nil
}
func tablesInScope(scope *Scope) []string {
tables := make(map[string]bool)
for _, node := range scope.InnerToOuter() {
for _, col := range schemas(node.Children()) {
tables[col.Source] = true
}
}
var tableSlice []string
for table := range tables {
tableSlice = append(tableSlice, table)
}
return tableSlice
}