-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
lookup_join_test.go
394 lines (376 loc) · 11.1 KB
/
lookup_join_test.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
// Copyright 2018 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package ordering
import (
"fmt"
"testing"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/sql/opt"
"github.com/cockroachdb/cockroach/pkg/sql/opt/cat"
"github.com/cockroachdb/cockroach/pkg/sql/opt/memo"
"github.com/cockroachdb/cockroach/pkg/sql/opt/norm"
"github.com/cockroachdb/cockroach/pkg/sql/opt/props"
"github.com/cockroachdb/cockroach/pkg/sql/opt/props/physical"
"github.com/cockroachdb/cockroach/pkg/sql/opt/testutils/testcat"
"github.com/cockroachdb/cockroach/pkg/sql/opt/testutils/testexpr"
"github.com/cockroachdb/cockroach/pkg/sql/sem/eval"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
)
func TestLookupJoinProvided(t *testing.T) {
tc := testcat.New()
if _, err := tc.ExecuteDDL(
"CREATE TABLE t (c1 INT, c2 INT, c3 INT, c4 INT, PRIMARY KEY(c1, c2))",
); err != nil {
t.Fatal(err)
}
st := cluster.MakeTestingClusterSettings()
evalCtx := eval.NewTestingEvalContext(st)
var f norm.Factory
f.Init(evalCtx, tc)
md := f.Metadata()
tn := tree.NewUnqualifiedTableName("t")
tab := md.AddTable(tc.Table(tn), tn)
if c1 := tab.ColumnID(0); c1 != 1 {
t.Fatalf("unexpected ID for column c1: %d\n", c1)
}
c := func(cols ...opt.ColumnID) opt.ColSet {
return opt.MakeColSet(cols...)
}
testCases := []struct {
keyCols opt.ColList
inputKey opt.ColSet
outCols opt.ColSet
required string
input string
provided string
}{
// In these tests, the input (left side of the join) has columns 5,6 and the
// table (right side) has columns 1,2,3,4.
//
{ // case 1: the lookup join adds columns 3,4 from the table and retains the
// input columns. Joining on (c1, c2) = (c5, c6).
keyCols: opt.ColList{5, 6},
inputKey: c(5, 6),
outCols: c(3, 4, 5, 6, 7, 8),
required: "+5,+6",
input: "+5,+6",
provided: "+5,+6",
},
{ // case 2: the lookup join produces all columns. The provided ordering
// on 5,6 is equivalent to an ordering on 1,2. Joining on (c1, c2) =
// (c5, c6).
keyCols: opt.ColList{5, 6},
inputKey: c(5, 6),
outCols: c(1, 2, 3, 4, 5, 6, 7, 8),
required: "-(1|5),+(2|6)",
input: "-5,+6",
provided: "-5,+6",
},
{ // case 3: the lookup join does not produce input columns 5,6; we must
// remap the input ordering to refer to output columns 1,2 instead.
// Joining on (c1, c2) = (c5, c6).
keyCols: opt.ColList{5, 6},
inputKey: c(5, 6),
outCols: c(1, 2, 3, 4),
required: "+(1|5),-(2|6)",
input: "+5,-6",
provided: "+1,-2",
},
{ // case 4: a hybrid of the two cases above (we need to remap column 6).
// Joining on (c1, c2) = (c5, c6).
keyCols: opt.ColList{5, 6},
inputKey: c(5, 6),
outCols: c(1, 2, 3, 4, 5),
required: "-(1|5),-(2|6)",
input: "-5,-6",
provided: "-5,-2",
},
{ // case 5: the lookup join adds column c2 as an ordering column. Joining
// on c1 = c5.
keyCols: opt.ColList{5},
inputKey: c(5, 6),
outCols: c(2, 3, 4, 5, 6),
required: "+(1|5),+6,+2",
input: "+5,+6",
provided: "+5,+6,+2",
},
{ // case 6: the lookup join outputs all columns and adds column c2 as an
// ordering column. Joining on c1 = c6.
keyCols: opt.ColList{6},
inputKey: c(6),
outCols: c(1, 2, 3, 4, 5, 6),
required: "-5,+(1|6),+2",
input: "-5,+6",
provided: "-5,+6,+2",
},
{ // case 7: the lookup join does not produce input columns 5,6; we must
// remap the input ordering to refer to output column 1 instead.
keyCols: opt.ColList{5},
inputKey: c(5),
outCols: c(1, 2, 3, 4),
required: "+(1|5),+2",
input: "+5",
provided: "+1,+2",
},
}
for tcIdx, tc := range testCases {
t.Run(fmt.Sprintf("case%d", tcIdx+1), func(t *testing.T) {
inputFDs := props.FuncDepSet{}
inputFDs.AddStrictKey(tc.inputKey, c(5, 6))
input := &testexpr.Instance{
Rel: &props.Relational{
OutputCols: c(5, 6),
FuncDeps: inputFDs,
},
Provided: &physical.Provided{
Ordering: props.ParseOrdering(tc.input),
},
}
lookupJoin := f.Memo().MemoizeLookupJoin(
input,
nil, /* FiltersExpr */
&memo.LookupJoinPrivate{
JoinType: opt.InnerJoinOp,
Table: tab,
Index: cat.PrimaryIndex,
KeyCols: tc.keyCols,
Cols: tc.outCols,
},
)
req := props.ParseOrderingChoice(tc.required)
res := lookupJoinBuildProvided(lookupJoin, &req).String()
if res != tc.provided {
t.Errorf("expected '%s', got '%s'", tc.provided, res)
}
})
}
}
func TestLookupJoinCanProvide(t *testing.T) {
tc := testcat.New()
if _, err := tc.ExecuteDDL(
"CREATE TABLE t (c1 INT, c2 INT, c3 INT, c4 INT, PRIMARY KEY(c1, c2), INDEX sec_idx(c3, c4, c1, c2))",
); err != nil {
t.Fatal(err)
}
st := cluster.MakeTestingClusterSettings()
evalCtx := eval.NewTestingEvalContext(st)
var f norm.Factory
f.Init(evalCtx, tc)
md := f.Metadata()
tn := tree.NewUnqualifiedTableName("t")
tab := md.AddTable(tc.Table(tn), tn)
if c1 := tab.ColumnID(0); c1 != 1 {
t.Fatalf("unexpected ID for column c1: %d\n", c1)
}
const secondaryIndex = 1
idxName := md.Table(tab).Index(secondaryIndex).Name()
if idxName != "sec_idx" {
t.Fatalf("unexpected secondary index: %s", idxName)
}
c := opt.MakeColSet
errorString := func(canProvide bool) string {
if canProvide {
return "expected to be able to provide %s"
}
return "expected not to be able to provide %s"
}
testCases := []struct {
idx cat.IndexOrdinal
keyCols opt.ColList
outCols opt.ColSet
inputKey opt.ColSet
required string
canProvide bool
}{
// In these tests, the input (left side of the join) has columns 5,6 and the
// table (right side) has columns 1,2,3,4.
//
{ // Case 1: the ordering can project input columns.
keyCols: opt.ColList{5},
outCols: c(1, 5, 6),
inputKey: c(5, 6),
required: "+(1|5),+6",
canProvide: true,
},
{ // Case 2: the ordering cannot project only input columns, but the lookup
// can maintain the ordering on both input and lookup columns.
keyCols: opt.ColList{5},
outCols: c(1, 2, 5, 6),
inputKey: c(5, 6),
required: "+(1|5),+6 opt(2)",
canProvide: true,
},
{ // Case 3: the ordering cannot project only input columns, but the lookup
// can maintain the ordering on both input and lookup columns.
keyCols: opt.ColList{5},
outCols: c(1, 2, 5, 6),
inputKey: c(5, 6),
required: "+(1|5),+6,+2",
canProvide: true,
},
{ // Case 4: the ordering cannot project only input columns, but the lookup
// can maintain the ordering on both input and lookup columns.
keyCols: opt.ColList{5, 6},
outCols: c(1, 2, 5, 6),
inputKey: c(5, 6),
required: "+5,+6,+1,+2",
canProvide: true,
},
{ // Case 5: the ordering cannot be satisfied because the input and lookup
// ordering columns are interleaved.
keyCols: opt.ColList{5},
outCols: c(1, 2, 5, 6),
inputKey: c(5, 6),
required: "+(1|5),+2,+6",
canProvide: false,
},
{ // Case 6: the ordering cannot be satisfied because the input ordering
// columns do not form a key over the input.
keyCols: opt.ColList{5},
outCols: c(1, 2, 5, 6),
inputKey: c(6),
required: "+(1|5),+2",
canProvide: false,
},
{ // Case 7: the ordering cannot be satisfied because the lookup ordering
// involves columns that are not part of the index.
keyCols: opt.ColList{5, 6},
outCols: c(1, 3, 5, 6),
inputKey: c(5, 6),
required: "+(1|5),+6,+3",
canProvide: false,
},
{ // Case 8: the ordering cannot be satisfied because the lookup ordering
// columns are not in index order.
idx: secondaryIndex,
keyCols: opt.ColList{5},
outCols: c(1, 2, 3, 4, 5, 6),
inputKey: c(5),
required: "+(3|5),+1,+4",
canProvide: false,
},
{ // Case 9: the ordering cannot be satisfied because one of the lookup
// ordering columns is sorted in the wrong direction.
keyCols: opt.ColList{5},
outCols: c(1, 2, 5, 6),
inputKey: c(5, 6),
required: "+(1|5),+6,-2",
canProvide: false,
},
}
for tcIdx, tc := range testCases {
t.Run(fmt.Sprintf("case%d", tcIdx+1), func(t *testing.T) {
fds := props.FuncDepSet{}
fds.AddStrictKey(tc.inputKey, c(5, 6))
input := &testexpr.Instance{
Rel: &props.Relational{
OutputCols: c(5, 6),
FuncDeps: fds,
},
}
lookupJoin := f.Memo().MemoizeLookupJoin(
input,
nil, /* FiltersExpr */
&memo.LookupJoinPrivate{
JoinType: opt.InnerJoinOp,
Table: tab,
Index: tc.idx,
KeyCols: tc.keyCols,
Cols: tc.outCols,
},
)
req := props.ParseOrderingChoice(tc.required)
canProvide := lookupOrIndexJoinCanProvideOrdering(lookupJoin, &req)
if canProvide != tc.canProvide {
t.Errorf(errorString(tc.canProvide), req)
}
})
}
}
func TestTrySatisfyRequired(t *testing.T) {
testCases := []struct {
required string
provided string
prefix string
toExtend string
}{
{ // Case 1: required ordering is prefix of provided.
required: "+1,+2,+3",
provided: "+1,+2,+3,+4",
prefix: "+1,+2,+3",
toExtend: "",
},
{ // Case 2: required ordering is empty.
required: "",
provided: "+1,-2",
prefix: "",
toExtend: "",
},
{ // Case 3: provided ordering includes optional columns.
required: "+1,+2,+3 opt(4,5)",
provided: "+1,-4,+2,+5,+3",
prefix: "+1,-4,+2,+5,+3",
toExtend: "",
},
{ // Case 4: required ordering includes equivalent columns.
required: "+(1|4),-(2|5),+3",
provided: "+1,-2,+3",
prefix: "+1,-2,+3",
toExtend: "",
},
{ // Case 5: provided ordering is prefix of required.
required: "+1,+2,+3",
provided: "+1,+2",
prefix: "+1,+2",
toExtend: "+3",
},
{ // Case 6: provided ordering has non-optional columns between required
// columns.
required: "+1,+2,+3",
provided: "+1,+2,+4,+3",
prefix: "+1,+2",
toExtend: "+3",
},
{ // Case 7: provided ordering column is in the wrong direction.
required: "+1,+2,+3",
provided: "+1,-2,+3",
prefix: "+1",
toExtend: "+2,+3",
},
{ // Case 8: provided ordering is empty and required is non-empty.
required: "+1",
provided: "",
prefix: "",
toExtend: "+1",
},
}
expect := func(exp, got string) {
t.Helper()
if got != exp {
t.Errorf("expected %s; got %s", exp, got)
}
}
for i, tc := range testCases {
t.Run(fmt.Sprintf("case%d", i+1), func(t *testing.T) {
required := props.ParseOrderingChoice(tc.required)
provided := props.ParseOrdering(tc.provided)
prefix, toExtend := trySatisfyRequired(&required, provided)
prefixString, toExtendString := "", ""
if prefix != nil {
prefixString = prefix.String()
}
if toExtend != nil {
toExtendString = toExtend.String()
}
expect(tc.prefix, prefixString)
expect(tc.toExtend, toExtendString)
})
}
}