Skip to content

Commit

Permalink
opt: use RelExpr instead of ColumnID for reorderjoins relation map
Browse files Browse the repository at this point in the history
The reorderjoins opttester directive previously maintained a map from
the first column ID of each base relation to the relation's label in
the output. This could cause a panic for relations that didn't output
any columns. This patch changes the map to use the relations themselves
as keys, which prevents the panic.

Release note: None
  • Loading branch information
DrewKimball committed Nov 2, 2022
1 parent 42084db commit 3a84e12
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 100 deletions.
15 changes: 5 additions & 10 deletions pkg/sql/opt/testutils/opttester/reorder_joins.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,16 +113,15 @@ func (ot *OptTester) ReorderJoins() (string, error) {
type joinOrderFormatter struct {
o *xform.Optimizer

// relLabels is a map from the first ColumnID of each base relation to its
// assigned label.
relLabels map[opt.ColumnID]string
// relLabels is a map from each base relation to its assigned label.
relLabels map[memo.RelExpr]string
}

// newJoinOrderFormatter returns an initialized joinOrderFormatter.
func newJoinOrderFormatter(o *xform.Optimizer) *joinOrderFormatter {
return &joinOrderFormatter{
o: o,
relLabels: make(map[opt.ColumnID]string),
relLabels: make(map[memo.RelExpr]string),
}
}

Expand Down Expand Up @@ -195,11 +194,7 @@ func (jof *joinOrderFormatter) formatRules(rules []xform.OnReorderRuleParam) str
// relLabel returns the label for the given relation. Labels will follow the
// pattern A, B, ..., Z, A1, B1, etc.
func (jof *joinOrderFormatter) relLabel(e memo.RelExpr) string {
firstCol, ok := e.Relational().OutputCols.Next(0)
if !ok {
panic(errors.AssertionFailedf("failed to retrieve column from %v", e.Op()))
}
if label, ok := jof.relLabels[firstCol]; ok {
if label, ok := jof.relLabels[e]; ok {
return label
}
const lenAlphabet = 26
Expand All @@ -210,7 +205,7 @@ func (jof *joinOrderFormatter) relLabel(e memo.RelExpr) string {
// Names will follow the pattern: A, B, ..., Z, A1, B1, etc.
label += strconv.Itoa(number)
}
jof.relLabels[firstCol] = label
jof.relLabels[e] = label
return label
}

Expand Down
180 changes: 90 additions & 90 deletions pkg/sql/opt/xform/testdata/rules/join_order
Original file line number Diff line number Diff line change
Expand Up @@ -2002,14 +2002,14 @@ Join Tree #2
Vertexes
A:
scan abc [as=a2]
B:
C:
distinct-on
└── scan abc [as=a5]
Edges
a2.c = a5.c [inner, ses=AB, tes=AB, rules=()]
Joining AB
A B [inner, refs=AB]
B A [inner, refs=AB]
a2.c = a5.c [inner, ses=AC, tes=AC, rules=()]
Joining AC
A C [inner, refs=AC]
C A [inner, refs=AC]
Joins Considered: 2
--------------------------------------------------------------------------------
Join Tree #3
Expand All @@ -2024,24 +2024,24 @@ Join Tree #3
└── filters
└── a1.a = a2.a
Vertexes
C:
D:
scan abc [as=a1]
A:
scan abc [as=a2]
B:
scan abc [as=a5]
Edges
a2.c = a5.c [semi, ses=AB, tes=AB, rules=()]
a1.a = a2.a [inner, ses=CA, tes=CA, rules=()]
Joining CA
C A [inner, refs=CA]
A C [inner, refs=CA]
a1.a = a2.a [inner, ses=DA, tes=DA, rules=()]
Joining DA
D A [inner, refs=DA]
A D [inner, refs=DA]
Joining AB
A B [semi, refs=AB]
Joining CAB
C AB [inner, refs=CA]
AB C [inner, refs=CA]
CA B [semi, refs=AB]
Joining DAB
D AB [inner, refs=DA]
AB D [inner, refs=DA]
DA B [semi, refs=AB]
Joins Considered: 6
--------------------------------------------------------------------------------
Join Tree #4
Expand All @@ -2060,30 +2060,30 @@ Join Tree #4
└── filters
└── a2.b = a3.b
Vertexes
C:
D:
scan abc [as=a1]
A:
E:
semi-join (hash)
├── scan abc [as=a2]
├── scan abc [as=a5]
└── filters
└── a2.c = a5.c
D:
F:
scan abc [as=a3]
Edges
a1.a = a2.a [inner, ses=CA, tes=CA, rules=()]
a2.b = a3.b [inner, ses=AD, tes=AD, rules=()]
Joining CA
C A [inner, refs=CA]
A C [inner, refs=CA]
Joining AD
A D [inner, refs=AD]
D A [inner, refs=AD]
Joining CAD
C AD [inner, refs=CA]
AD C [inner, refs=CA]
CA D [inner, refs=AD]
D CA [inner, refs=AD]
a1.a = a2.a [inner, ses=DE, tes=DE, rules=()]
a2.b = a3.b [inner, ses=EF, tes=EF, rules=()]
Joining DE
D E [inner, refs=DE]
E D [inner, refs=DE]
Joining EF
E F [inner, refs=EF]
F E [inner, refs=EF]
Joining DEF
D EF [inner, refs=DE]
EF D [inner, refs=DE]
DE F [inner, refs=EF]
F DE [inner, refs=EF]
Joins Considered: 8
--------------------------------------------------------------------------------
Join Tree #5
Expand All @@ -2106,7 +2106,7 @@ Join Tree #5
└── filters
└── a3.a = a4.a
Vertexes
C:
G:
inner-join (hash)
├── scan abc [as=a1]
├── semi-join (hash)
Expand All @@ -2116,24 +2116,24 @@ Vertexes
│ └── a2.c = a5.c
└── filters
└── a1.a = a2.a
D:
F:
scan abc [as=a3]
E:
H:
scan abc [as=a4]
Edges
a2.b = a3.b [inner, ses=CD, tes=CD, rules=()]
a3.a = a4.a [inner, ses=DE, tes=DE, rules=()]
Joining CD
C D [inner, refs=CD]
D C [inner, refs=CD]
Joining DE
D E [inner, refs=DE]
E D [inner, refs=DE]
Joining CDE
C DE [inner, refs=CD]
DE C [inner, refs=CD]
CD E [inner, refs=DE]
E CD [inner, refs=DE]
a2.b = a3.b [inner, ses=GF, tes=GF, rules=()]
a3.a = a4.a [inner, ses=FH, tes=FH, rules=()]
Joining GF
G F [inner, refs=GF]
F G [inner, refs=GF]
Joining FH
F H [inner, refs=FH]
H F [inner, refs=FH]
Joining GFH
G FH [inner, refs=GF]
FH G [inner, refs=GF]
GF H [inner, refs=FH]
H GF [inner, refs=FH]
Joins Considered: 8
================================================================================
Final Plan
Expand Down Expand Up @@ -2253,29 +2253,29 @@ Vertexes
├── scan bx
└── filters
└── x IS NOT NULL
C:
D:
distinct-on
└── scan cy
Edges
a = x [inner, ses=AB, tes=AB, rules=()]
x = y [inner, ses=BC, tes=BC, rules=()]
a = y [inner, ses=AC, tes=AC, rules=()]
x = y [inner, ses=BD, tes=BD, rules=()]
a = y [inner, ses=AD, tes=AD, rules=()]
Joining AB
A B [inner, refs=AB]
B A [inner, refs=AB]
Joining AC
A C [inner, refs=AC]
C A [inner, refs=AC]
Joining BC
B C [inner, refs=BC]
C B [inner, refs=BC]
Joining ABC
A BC [inner, refs=AB]
BC A [inner, refs=AB]
B AC [inner, refs=AB]
AC B [inner, refs=AB]
AB C [inner, refs=BC]
C AB [inner, refs=BC]
Joining AD
A D [inner, refs=AD]
D A [inner, refs=AD]
Joining BD
B D [inner, refs=BD]
D B [inner, refs=BD]
Joining ABD
A BD [inner, refs=AB]
BD A [inner, refs=AB]
B AD [inner, refs=AB]
AD B [inner, refs=AB]
AB D [inner, refs=BD]
D AB [inner, refs=BD]
Joins Considered: 12
--------------------------------------------------------------------------------
Join Tree #4
Expand Down Expand Up @@ -2306,13 +2306,13 @@ Vertexes
└── x IS NOT NULL
C:
scan cy
D:
E:
scan dz
Edges
a = x [inner, ses=AB, tes=AB, rules=()]
x = y [semi, ses=BC, tes=BC, rules=()]
a = z [inner, ses=AD, tes=AD, rules=(C->B)]
x = z [inner, ses=BD, tes=BD, rules=()]
a = z [inner, ses=AE, tes=AE, rules=(C->B)]
x = z [inner, ses=BE, tes=BE, rules=()]
Joining AB
A B [inner, refs=AB]
B A [inner, refs=AB]
Expand All @@ -2322,31 +2322,31 @@ Joining ABC
A BC [inner, refs=AB]
BC A [inner, refs=AB]
AB C [semi, refs=BC]
Joining AD
A D [inner, refs=AD]
D A [inner, refs=AD]
Joining BD
B D [inner, refs=BD]
D B [inner, refs=BD]
Joining ABD
A BD [inner, refs=AB]
BD A [inner, refs=AB]
B AD [inner, refs=AB]
AD B [inner, refs=AB]
AB D [inner, refs=AD]
D AB [inner, refs=AD]
Joining BCD
BD C [semi, refs=BC]
BC D [inner, refs=BD]
D BC [inner, refs=BD]
Joining ABCD
A BCD [inner, refs=AB]
BCD A [inner, refs=AB]
ABD C [semi, refs=BC]
BC AD [inner, refs=AB]
AD BC [inner, refs=AB]
ABC D [inner, refs=AD]
D ABC [inner, refs=AD]
Joining AE
A E [inner, refs=AE]
E A [inner, refs=AE]
Joining BE
B E [inner, refs=BE]
E B [inner, refs=BE]
Joining ABE
A BE [inner, refs=AB]
BE A [inner, refs=AB]
B AE [inner, refs=AB]
AE B [inner, refs=AB]
AB E [inner, refs=AE]
E AB [inner, refs=AE]
Joining BCE
BE C [semi, refs=BC]
BC E [inner, refs=BE]
E BC [inner, refs=BE]
Joining ABCE
A BCE [inner, refs=AB]
BCE A [inner, refs=AB]
ABE C [semi, refs=BC]
BC AE [inner, refs=AB]
AE BC [inner, refs=AB]
ABC E [inner, refs=AE]
E ABC [inner, refs=AE]
Joins Considered: 26
================================================================================
Final Plan
Expand Down

0 comments on commit 3a84e12

Please sign in to comment.