Skip to content

Commit 2545589

Browse files
committed
opt: fix LookupJoinDef interning, add tests
Fixing an omission I noticed in internLookupJoinDef and adding missing tests for interning defs. Release note: None
1 parent 403ca04 commit 2545589

File tree

2 files changed

+366
-0
lines changed

2 files changed

+366
-0
lines changed

pkg/sql/opt/memo/private_storage.go

+2
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,9 @@ func (ps *privateStorage) internLookupJoinDef(def *LookupJoinDef) PrivateID {
315315
// The below code is carefully constructed to not allocate in the case where
316316
// the value is already in the map. Be careful when modifying.
317317
ps.keyBuf.Reset()
318+
ps.keyBuf.writeUvarint(uint64(def.JoinType))
318319
ps.keyBuf.writeUvarint(uint64(def.Table))
320+
ps.keyBuf.writeUvarint(uint64(def.Index))
319321
ps.keyBuf.writeColList(def.KeyCols)
320322
// Add a separator between the list and the set. Note that the column IDs
321323
// cannot be 0.

pkg/sql/opt/memo/private_storage_test.go

+364
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,370 @@ func TestInternScanOpDef(t *testing.T) {
153153
test(scanDef9, scanDef10, false)
154154
}
155155

156+
func TestVirtualScanOpDef(t *testing.T) {
157+
var ps privateStorage
158+
ps.init()
159+
160+
test := func(left, right *VirtualScanOpDef, expected bool) {
161+
t.Helper()
162+
leftID := ps.internVirtualScanOpDef(left)
163+
rightID := ps.internVirtualScanOpDef(right)
164+
if (leftID == rightID) != expected {
165+
t.Errorf("%v == %v, expected %v, got %v", left, right, expected, !expected)
166+
}
167+
}
168+
testEQ := func(left, right *VirtualScanOpDef) {
169+
t.Helper()
170+
test(left, right, true)
171+
}
172+
testNE := func(left, right *VirtualScanOpDef) {
173+
t.Helper()
174+
test(left, right, false)
175+
}
176+
177+
def1 := &VirtualScanOpDef{Table: 0, Cols: util.MakeFastIntSet(1, 2)}
178+
def2 := &VirtualScanOpDef{Table: 0, Cols: util.MakeFastIntSet(1, 2, 3)}
179+
def3 := &VirtualScanOpDef{Table: 1, Cols: util.MakeFastIntSet(1, 2)}
180+
testEQ(def1, def1)
181+
testNE(def1, def2)
182+
testNE(def1, def3)
183+
testNE(def2, def3)
184+
}
185+
186+
func TestIndexJoinDef(t *testing.T) {
187+
var ps privateStorage
188+
ps.init()
189+
190+
test := func(left, right *IndexJoinDef, expected bool) {
191+
t.Helper()
192+
leftID := ps.internIndexJoinDef(left)
193+
rightID := ps.internIndexJoinDef(right)
194+
if (leftID == rightID) != expected {
195+
t.Errorf("%v == %v, expected %v, got %v", left, right, expected, !expected)
196+
}
197+
}
198+
testEQ := func(left, right *IndexJoinDef) {
199+
t.Helper()
200+
test(left, right, true)
201+
}
202+
testNE := func(left, right *IndexJoinDef) {
203+
t.Helper()
204+
test(left, right, false)
205+
}
206+
207+
def1 := &IndexJoinDef{Table: 0, Cols: util.MakeFastIntSet(1, 2)}
208+
def2 := &IndexJoinDef{Table: 0, Cols: util.MakeFastIntSet(1, 2, 3)}
209+
def3 := &IndexJoinDef{Table: 1, Cols: util.MakeFastIntSet(1, 2)}
210+
testEQ(def1, def1)
211+
testNE(def1, def2)
212+
testNE(def1, def3)
213+
testNE(def2, def3)
214+
}
215+
216+
func TestLookupJoinDef(t *testing.T) {
217+
var ps privateStorage
218+
ps.init()
219+
test := func(left, right *LookupJoinDef, expected bool) {
220+
t.Helper()
221+
leftID := ps.internLookupJoinDef(left)
222+
rightID := ps.internLookupJoinDef(right)
223+
if (leftID == rightID) != expected {
224+
t.Errorf("%v == %v, expected %v, got %v", left, right, expected, !expected)
225+
}
226+
}
227+
testEQ := func(left, right *LookupJoinDef) {
228+
t.Helper()
229+
test(left, right, true)
230+
}
231+
testNE := func(left, right *LookupJoinDef) {
232+
t.Helper()
233+
test(left, right, false)
234+
}
235+
236+
def1 := &LookupJoinDef{
237+
JoinType: opt.InnerJoinOp,
238+
Table: 0,
239+
Index: 0,
240+
KeyCols: opt.ColList{10, 11},
241+
LookupCols: util.MakeFastIntSet(1, 2),
242+
}
243+
def2 := &LookupJoinDef{
244+
JoinType: opt.LeftJoinOp,
245+
Table: 0,
246+
Index: 0,
247+
KeyCols: opt.ColList{10, 11},
248+
LookupCols: util.MakeFastIntSet(1, 2),
249+
}
250+
def3 := &LookupJoinDef{
251+
JoinType: opt.InnerJoinOp,
252+
Table: 1,
253+
Index: 0,
254+
KeyCols: opt.ColList{10, 11},
255+
LookupCols: util.MakeFastIntSet(1, 2),
256+
}
257+
def4 := &LookupJoinDef{
258+
JoinType: opt.InnerJoinOp,
259+
Table: 0,
260+
Index: 1,
261+
KeyCols: opt.ColList{10, 11},
262+
LookupCols: util.MakeFastIntSet(1, 2),
263+
}
264+
def5 := &LookupJoinDef{
265+
JoinType: opt.InnerJoinOp,
266+
Table: 0,
267+
Index: 0,
268+
KeyCols: opt.ColList{10},
269+
LookupCols: util.MakeFastIntSet(1, 2),
270+
}
271+
def6 := &LookupJoinDef{
272+
JoinType: opt.InnerJoinOp,
273+
Table: 0,
274+
Index: 0,
275+
KeyCols: opt.ColList{10, 11},
276+
LookupCols: util.MakeFastIntSet(1, 2, 3),
277+
}
278+
testEQ(def1, def1)
279+
testNE(def1, def2)
280+
testNE(def1, def3)
281+
testNE(def1, def4)
282+
testNE(def1, def5)
283+
testNE(def1, def6)
284+
}
285+
286+
func TestExplainOpDef(t *testing.T) {
287+
var ps privateStorage
288+
ps.init()
289+
290+
test := func(left, right *ExplainOpDef, expected bool) {
291+
t.Helper()
292+
leftID := ps.internExplainOpDef(left)
293+
rightID := ps.internExplainOpDef(right)
294+
if (leftID == rightID) != expected {
295+
t.Errorf("%v == %v, expected %v, got %v", left, right, expected, !expected)
296+
}
297+
}
298+
testEQ := func(left, right *ExplainOpDef) {
299+
t.Helper()
300+
test(left, right, true)
301+
}
302+
testNE := func(left, right *ExplainOpDef) {
303+
t.Helper()
304+
test(left, right, false)
305+
}
306+
307+
def1 := &ExplainOpDef{
308+
Options: tree.ExplainOptions{
309+
Mode: tree.ExplainPlan,
310+
Flags: util.MakeFastIntSet(tree.ExplainFlagVerbose),
311+
},
312+
ColList: opt.ColList{1, 2},
313+
Props: props.Physical{
314+
Presentation: props.Presentation{opt.LabeledColumn{Label: "c", ID: 1}},
315+
Ordering: props.ParseOrderingChoice("+(1|2),+3 opt(4,5)"),
316+
},
317+
}
318+
def2 := &ExplainOpDef{
319+
Options: tree.ExplainOptions{
320+
Mode: tree.ExplainPlan,
321+
},
322+
ColList: opt.ColList{1, 2},
323+
Props: props.Physical{
324+
Presentation: props.Presentation{opt.LabeledColumn{Label: "c", ID: 1}},
325+
Ordering: props.ParseOrderingChoice("+(1|2),+3 opt(4,5)"),
326+
},
327+
}
328+
def3 := &ExplainOpDef{
329+
Options: tree.ExplainOptions{
330+
Mode: tree.ExplainOpt,
331+
Flags: util.MakeFastIntSet(tree.ExplainFlagVerbose),
332+
},
333+
ColList: opt.ColList{1, 2},
334+
Props: props.Physical{
335+
Presentation: props.Presentation{opt.LabeledColumn{Label: "c", ID: 1}},
336+
Ordering: props.ParseOrderingChoice("+(1|2),+3 opt(4,5)"),
337+
},
338+
}
339+
def4 := &ExplainOpDef{
340+
Options: tree.ExplainOptions{
341+
Mode: tree.ExplainPlan,
342+
Flags: util.MakeFastIntSet(tree.ExplainFlagVerbose),
343+
},
344+
ColList: opt.ColList{1, 2, 3},
345+
Props: props.Physical{
346+
Presentation: props.Presentation{opt.LabeledColumn{Label: "c", ID: 1}},
347+
Ordering: props.ParseOrderingChoice("+(1|2),+3 opt(4,5)"),
348+
},
349+
}
350+
def5 := &ExplainOpDef{
351+
Options: tree.ExplainOptions{
352+
Mode: tree.ExplainPlan,
353+
Flags: util.MakeFastIntSet(tree.ExplainFlagVerbose),
354+
},
355+
ColList: opt.ColList{1, 2},
356+
Props: props.Physical{
357+
Presentation: props.Presentation{opt.LabeledColumn{Label: "x", ID: 1}},
358+
Ordering: props.ParseOrderingChoice("+(1|2),+3 opt(4,5)"),
359+
},
360+
}
361+
def6 := &ExplainOpDef{
362+
Options: tree.ExplainOptions{
363+
Mode: tree.ExplainPlan,
364+
Flags: util.MakeFastIntSet(tree.ExplainFlagVerbose),
365+
},
366+
ColList: opt.ColList{1, 2},
367+
Props: props.Physical{
368+
Presentation: props.Presentation{opt.LabeledColumn{Label: "c", ID: 1}},
369+
Ordering: props.ParseOrderingChoice("+1,+3 opt(4,5)"),
370+
},
371+
}
372+
testEQ(def1, def1)
373+
testNE(def1, def2)
374+
testNE(def1, def3)
375+
testNE(def1, def4)
376+
testNE(def1, def5)
377+
testNE(def1, def6)
378+
}
379+
380+
func TestShowTraceOpDef(t *testing.T) {
381+
var ps privateStorage
382+
ps.init()
383+
384+
test := func(left, right *ShowTraceOpDef, expected bool) {
385+
t.Helper()
386+
leftID := ps.internShowTraceOpDef(left)
387+
rightID := ps.internShowTraceOpDef(right)
388+
if (leftID == rightID) != expected {
389+
t.Errorf("%v == %v, expected %v, got %v", left, right, expected, !expected)
390+
}
391+
}
392+
testEQ := func(left, right *ShowTraceOpDef) {
393+
t.Helper()
394+
test(left, right, true)
395+
}
396+
testNE := func(left, right *ShowTraceOpDef) {
397+
t.Helper()
398+
test(left, right, false)
399+
}
400+
401+
def1 := &ShowTraceOpDef{Type: tree.ShowTraceRaw, Compact: false, ColList: opt.ColList{1, 2}}
402+
def2 := &ShowTraceOpDef{Type: tree.ShowTraceReplica, Compact: false, ColList: opt.ColList{1, 2}}
403+
def3 := &ShowTraceOpDef{Type: tree.ShowTraceRaw, Compact: true, ColList: opt.ColList{1, 2}}
404+
def4 := &ShowTraceOpDef{Type: tree.ShowTraceRaw, Compact: false, ColList: opt.ColList{1}}
405+
testEQ(def1, def1)
406+
testNE(def1, def2)
407+
testNE(def1, def3)
408+
testNE(def1, def4)
409+
}
410+
411+
func TestMergeOnDef(t *testing.T) {
412+
var ps privateStorage
413+
ps.init()
414+
415+
test := func(left, right *MergeOnDef, expected bool) {
416+
t.Helper()
417+
leftID := ps.internMergeOnDef(left)
418+
rightID := ps.internMergeOnDef(right)
419+
if (leftID == rightID) != expected {
420+
t.Errorf("%v == %v, expected %v, got %v", left, right, expected, !expected)
421+
}
422+
}
423+
testEQ := func(left, right *MergeOnDef) {
424+
t.Helper()
425+
test(left, right, true)
426+
}
427+
testNE := func(left, right *MergeOnDef) {
428+
t.Helper()
429+
test(left, right, false)
430+
}
431+
432+
def1 := &MergeOnDef{
433+
JoinType: opt.InnerJoinOp,
434+
LeftEq: opt.Ordering{1, -2},
435+
RightEq: opt.Ordering{4, -5},
436+
LeftOrdering: props.ParseOrderingChoice("+1,-(2|9)"),
437+
RightOrdering: props.ParseOrderingChoice("-5 opt(4)"),
438+
}
439+
def2 := &MergeOnDef{
440+
JoinType: opt.LeftJoinOp,
441+
LeftEq: opt.Ordering{1, -2},
442+
RightEq: opt.Ordering{4, -5},
443+
LeftOrdering: props.ParseOrderingChoice("+1,-(2|9)"),
444+
RightOrdering: props.ParseOrderingChoice("-5 opt(4)"),
445+
}
446+
def3 := &MergeOnDef{
447+
JoinType: opt.InnerJoinOp,
448+
LeftEq: opt.Ordering{1, +2},
449+
RightEq: opt.Ordering{4, -5},
450+
LeftOrdering: props.ParseOrderingChoice("+1,-(2|9)"),
451+
RightOrdering: props.ParseOrderingChoice("-5 opt(4)"),
452+
}
453+
def4 := &MergeOnDef{
454+
JoinType: opt.InnerJoinOp,
455+
LeftEq: opt.Ordering{1, -2},
456+
RightEq: opt.Ordering{4},
457+
LeftOrdering: props.ParseOrderingChoice("+1,-(2|9)"),
458+
RightOrdering: props.ParseOrderingChoice("-5 opt(4)"),
459+
}
460+
def5 := &MergeOnDef{
461+
JoinType: opt.InnerJoinOp,
462+
LeftEq: opt.Ordering{1, -2},
463+
RightEq: opt.Ordering{4, -5},
464+
LeftOrdering: props.ParseOrderingChoice("+1,-2"),
465+
RightOrdering: props.ParseOrderingChoice("-5 opt(4)"),
466+
}
467+
def6 := &MergeOnDef{
468+
JoinType: opt.InnerJoinOp,
469+
LeftEq: opt.Ordering{1, -2},
470+
RightEq: opt.Ordering{4, -5},
471+
LeftOrdering: props.ParseOrderingChoice("+1,-(2|9)"),
472+
RightOrdering: props.ParseOrderingChoice("+4,-5"),
473+
}
474+
testEQ(def1, def1)
475+
testNE(def1, def2)
476+
testNE(def1, def3)
477+
testNE(def1, def4)
478+
testNE(def1, def5)
479+
testNE(def1, def6)
480+
}
481+
482+
func TestRowNumberDef(t *testing.T) {
483+
var ps privateStorage
484+
ps.init()
485+
486+
test := func(left, right *RowNumberDef, expected bool) {
487+
t.Helper()
488+
leftID := ps.internRowNumberDef(left)
489+
rightID := ps.internRowNumberDef(right)
490+
if (leftID == rightID) != expected {
491+
t.Errorf("%v == %v, expected %v, got %v", left, right, expected, !expected)
492+
}
493+
}
494+
testEQ := func(left, right *RowNumberDef) {
495+
t.Helper()
496+
test(left, right, true)
497+
}
498+
testNE := func(left, right *RowNumberDef) {
499+
t.Helper()
500+
test(left, right, false)
501+
}
502+
503+
def1 := &RowNumberDef{
504+
Ordering: props.ParseOrderingChoice("+1,+2"),
505+
ColID: 1,
506+
}
507+
def2 := &RowNumberDef{
508+
Ordering: props.ParseOrderingChoice("+1,+(2|3)"),
509+
ColID: 1,
510+
}
511+
def3 := &RowNumberDef{
512+
Ordering: props.ParseOrderingChoice("+1,+2"),
513+
ColID: 2,
514+
}
515+
testEQ(def1, def1)
516+
testNE(def1, def2)
517+
testNE(def1, def3)
518+
}
519+
156520
func TestInternGroupByDef(t *testing.T) {
157521
var ps privateStorage
158522
ps.init()

0 commit comments

Comments
 (0)