This repository has been archived by the owner on Jan 28, 2021. It is now read-only.
/
resolve_natural_joins.go
266 lines (219 loc) · 5.28 KB
/
resolve_natural_joins.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
package analyzer
import (
"reflect"
"gopkg.in/src-d/go-mysql-server.v0/sql"
"gopkg.in/src-d/go-mysql-server.v0/sql/expression"
"gopkg.in/src-d/go-mysql-server.v0/sql/plan"
)
type transformedJoin struct {
node sql.Node
condCols map[string]*transformedSource
}
type transformedSource struct {
correct string
wrong []string
}
func resolveNaturalJoins(ctx *sql.Context, a *Analyzer, n sql.Node) (sql.Node, error) {
span, _ := ctx.Span("resolve_natural_joins")
defer span.Finish()
if n.Resolved() {
return n, nil
}
var transformed []*transformedJoin
var aliasTables = map[string][]string{}
var colsToUnresolve = map[string]*transformedSource{}
a.Log("resolving natural joins, node of type %T", n)
node, err := n.TransformUp(func(n sql.Node) (sql.Node, error) {
a.Log("transforming node of type: %T", n)
if alias, ok := n.(*plan.TableAlias); ok {
table := alias.Child.(*plan.ResolvedTable).Name()
aliasTables[alias.Name()] = append(aliasTables[alias.Name()], table)
return n, nil
}
if n.Resolved() {
return n, nil
}
join, ok := n.(*plan.NaturalJoin)
if !ok {
return n, nil
}
// we need both leaves resolved before resolving this one
if !join.Left.Resolved() || !join.Right.Resolved() {
return n, nil
}
leftSchema, rightSchema := join.Left.Schema(), join.Right.Schema()
var conditions, common, left, right []sql.Expression
var seen = make(map[string]struct{})
for i, lcol := range leftSchema {
var found bool
leftCol := expression.NewGetFieldWithTable(
i,
lcol.Type,
lcol.Source,
lcol.Name,
lcol.Nullable,
)
for j, rcol := range rightSchema {
if lcol.Name == rcol.Name {
common = append(common, leftCol)
conditions = append(
conditions,
expression.NewEquals(
leftCol,
expression.NewGetFieldWithTable(
len(leftSchema)+j,
rcol.Type,
rcol.Source,
rcol.Name,
rcol.Nullable,
),
),
)
found = true
seen[lcol.Name] = struct{}{}
if source, ok := colsToUnresolve[lcol.Name]; ok {
source.correct = lcol.Source
source.wrong = append(source.wrong, rcol.Source)
} else {
colsToUnresolve[lcol.Name] = &transformedSource{
correct: lcol.Source,
wrong: []string{rcol.Source},
}
}
break
}
}
if !found {
left = append(left, leftCol)
}
}
if len(conditions) == 0 {
return plan.NewCrossJoin(join.Left, join.Right), nil
}
for i, col := range rightSchema {
if _, ok := seen[col.Name]; !ok {
right = append(
right,
expression.NewGetFieldWithTable(
len(leftSchema)+i,
col.Type,
col.Source,
col.Name,
col.Nullable,
),
)
}
}
projections := append(append(common, left...), right...)
tj := &transformedJoin{
node: plan.NewProject(
projections,
plan.NewInnerJoin(
join.Left,
join.Right,
expression.JoinAnd(conditions...),
),
),
condCols: colsToUnresolve,
}
transformed = append(transformed, tj)
return tj.node, nil
})
if err != nil || len(transformed) == 0 {
return node, err
}
var transformedSeen bool
return node.TransformUp(func(node sql.Node) (sql.Node, error) {
if ok, _ := isTransformedNode(node, transformed); ok {
transformedSeen = true
return node, nil
}
if !transformedSeen {
return node, nil
}
expressioner, ok := node.(sql.Expressioner)
if !ok {
return node, nil
}
return expressioner.TransformExpressions(func(e sql.Expression) (sql.Expression, error) {
var col, table string
switch e := e.(type) {
case *expression.GetField:
col, table = e.Name(), e.Table()
case *expression.UnresolvedColumn:
col, table = e.Name(), e.Table()
default:
return e, nil
}
sources, ok := colsToUnresolve[col]
if !ok {
return e, nil
}
if !mustUnresolve(aliasTables, table, sources.wrong) {
return e, nil
}
return expression.NewUnresolvedQualifiedColumn(
sources.correct,
col,
), nil
})
})
}
func isTransformedNode(node sql.Node, transformed []*transformedJoin) (is bool, colsToUnresolve map[string]*transformedSource) {
var project *plan.Project
var join *plan.InnerJoin
switch n := node.(type) {
case *plan.Project:
var ok bool
join, ok = n.Child.(*plan.InnerJoin)
if !ok {
return
}
project = n
case *plan.InnerJoin:
join = n
default:
return
}
for _, t := range transformed {
tproject, ok := t.node.(*plan.Project)
if !ok {
return
}
tjoin, ok := tproject.Child.(*plan.InnerJoin)
if !ok {
return
}
if project != nil && !reflect.DeepEqual(project.Projections, tproject.Projections) {
continue
}
if reflect.DeepEqual(join.Cond, tjoin.Cond) {
is = true
colsToUnresolve = t.condCols
}
}
return
}
func mustUnresolve(aliasTable map[string][]string, table string, wrongSources []string) bool {
return isIn(table, wrongSources) || isAliasFor(aliasTable, table, wrongSources)
}
func isIn(s string, l []string) bool {
for _, e := range l {
if s == e {
return true
}
}
return false
}
func isAliasFor(aliasTable map[string][]string, table string, wrongSources []string) bool {
tables, ok := aliasTable[table]
if !ok {
return false
}
for _, t := range tables {
if isIn(t, wrongSources) {
return true
}
}
return false
}