Skip to content

Commit 1d53c85

Browse files
authored
planner: add some e2e test cases for outer join elimination (#58070)
ref #54057
1 parent 4e6468d commit 1d53c85

24 files changed

+220
-194
lines changed

pkg/expression/schema.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ func (ki KeyInfo) String() string {
4646
// Schema stands for the row schema and unique key information get from input.
4747
type Schema struct {
4848
Columns []*Column
49-
Keys []KeyInfo
50-
// UniqueKeys stores those unique indexes that allow null values, but Keys does not allow null values.
51-
// since equivalence conditions can filter out null values, in this case a unique index with null values can be a Key.
52-
UniqueKeys []KeyInfo
49+
PKOrUK []KeyInfo // this fields stores the primary key or unique key.
50+
// NullableUK stores those unique indexes that allow null values, but PKOrUK does not allow null values.
51+
// Since equivalence conditions can filter out null values, in this case a unique index with null values can be a Key.
52+
NullableUK []KeyInfo
5353
}
5454

5555
// String implements fmt.Stringer interface.
@@ -58,17 +58,17 @@ func (s *Schema) String() string {
5858
for _, col := range s.Columns {
5959
colStrs = append(colStrs, col.String())
6060
}
61-
strs := make([]string, 0, len(s.Keys))
62-
for _, key := range s.Keys {
61+
strs := make([]string, 0, len(s.PKOrUK))
62+
for _, key := range s.PKOrUK {
6363
strs = append(strs, key.String())
6464
}
65-
ukStrs := make([]string, 0, len(s.Keys))
66-
for _, key := range s.UniqueKeys {
65+
ukStrs := make([]string, 0, len(s.PKOrUK))
66+
for _, key := range s.NullableUK {
6767
ukStrs = append(ukStrs, key.String())
6868
}
6969
return "Column: [" + strings.Join(colStrs, ",") +
70-
"] Key: [" + strings.Join(strs, ",") +
71-
"] Unique key: [" + strings.Join(ukStrs, ",") + "]"
70+
"] PKOrUK: [" + strings.Join(strs, ",") +
71+
"] NullableUK: [" + strings.Join(ukStrs, ",") + "]"
7272
}
7373

7474
// Clone copies the total schema.
@@ -77,18 +77,18 @@ func (s *Schema) Clone() *Schema {
7777
return nil
7878
}
7979
cols := make([]*Column, 0, s.Len())
80-
keys := make([]KeyInfo, 0, len(s.Keys))
80+
keys := make([]KeyInfo, 0, len(s.PKOrUK))
8181
for _, col := range s.Columns {
8282
cols = append(cols, col.Clone().(*Column))
8383
}
84-
for _, key := range s.Keys {
84+
for _, key := range s.PKOrUK {
8585
keys = append(keys, key.Clone())
8686
}
8787
schema := NewSchema(cols...)
8888
schema.SetKeys(keys)
89-
if s.UniqueKeys != nil {
90-
uniqueKeys := make([]KeyInfo, 0, len(s.UniqueKeys))
91-
for _, key := range s.UniqueKeys {
89+
if s.NullableUK != nil {
90+
uniqueKeys := make([]KeyInfo, 0, len(s.NullableUK))
91+
for _, key := range s.NullableUK {
9292
uniqueKeys = append(uniqueKeys, key.Clone())
9393
}
9494
schema.SetUniqueKeys(uniqueKeys)
@@ -145,9 +145,9 @@ func (s *Schema) RetrieveColumn(col *Column) *Column {
145145
// Pass strong=true to check strong contraint: unique && notnull.
146146
// Pass strong=false to check weak contraint: unique && nullable.
147147
func (s *Schema) IsUnique(strong bool, cols ...*Column) bool {
148-
slicesToBeIterated := s.UniqueKeys
148+
slicesToBeIterated := s.NullableUK
149149
if strong {
150-
slicesToBeIterated = s.Keys
150+
slicesToBeIterated = s.PKOrUK
151151
}
152152
for _, key := range slicesToBeIterated {
153153
if len(key) > len(cols) {
@@ -210,12 +210,12 @@ func (s *Schema) Append(col ...*Column) {
210210

211211
// SetKeys will set the value of Schema.Keys.
212212
func (s *Schema) SetKeys(keys []KeyInfo) {
213-
s.Keys = keys
213+
s.PKOrUK = keys
214214
}
215215

216216
// SetUniqueKeys will set the value of Schema.UniqueKeys.
217217
func (s *Schema) SetUniqueKeys(keys []KeyInfo) {
218-
s.UniqueKeys = keys
218+
s.NullableUK = keys
219219
}
220220

221221
// ColumnsIndices will return a slice which contains the position of each column in schema.
@@ -269,18 +269,18 @@ func (s *Schema) MemoryUsage() (sum int64) {
269269
return
270270
}
271271

272-
sum = emptySchemaSize + int64(cap(s.Columns))*size.SizeOfPointer + int64(cap(s.Keys)+cap(s.UniqueKeys))*size.SizeOfSlice
272+
sum = emptySchemaSize + int64(cap(s.Columns))*size.SizeOfPointer + int64(cap(s.PKOrUK)+cap(s.NullableUK))*size.SizeOfSlice
273273

274274
for _, col := range s.Columns {
275275
sum += col.MemoryUsage()
276276
}
277-
for _, cols := range s.Keys {
277+
for _, cols := range s.PKOrUK {
278278
sum += int64(cap(cols)) * size.SizeOfPointer
279279
for _, col := range cols {
280280
sum += col.MemoryUsage()
281281
}
282282
}
283-
for _, cols := range s.UniqueKeys {
283+
for _, cols := range s.NullableUK {
284284
sum += int64(cap(cols)) * size.SizeOfPointer
285285
for _, col := range cols {
286286
sum += col.MemoryUsage()

pkg/expression/schema_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func generateKeys4Schema(schema *Schema) {
3434
for i := 0; i < keyCount; i++ {
3535
keys = append(keys, []*Column{schema.Columns[i]})
3636
}
37-
schema.Keys = keys
37+
schema.PKOrUK = keys
3838
}
3939

4040
// generateSchema will generate a schema for test. Used only in this file.
@@ -63,16 +63,16 @@ func TestSchemaClone(t *testing.T) {
6363
clonedSchema := schema.Clone()
6464
require.Equal(t, schema.String(), clonedSchema.String())
6565

66-
require.NotSame(t, unsafe.SliceData(schema.Keys), unsafe.SliceData(clonedSchema.Keys))
67-
require.NotSame(t, unsafe.SliceData(schema.UniqueKeys), unsafe.SliceData(clonedSchema.UniqueKeys))
66+
require.NotSame(t, unsafe.SliceData(schema.PKOrUK), unsafe.SliceData(clonedSchema.PKOrUK))
67+
require.NotSame(t, unsafe.SliceData(schema.NullableUK), unsafe.SliceData(clonedSchema.NullableUK))
6868
}
6969

7070
func TestSchemaString(t *testing.T) {
7171
s := &schemaGenerator{}
7272
schema := s.generateSchema(5)
73-
require.Equal(t, "Column: [Column#1,Column#2,Column#3,Column#4,Column#5] Key: [] Unique key: []", schema.String())
73+
require.Equal(t, "Column: [Column#1,Column#2,Column#3,Column#4,Column#5] PKOrUK: [] NullableUK: []", schema.String())
7474
generateKeys4Schema(schema)
75-
require.Equal(t, "Column: [Column#1,Column#2,Column#3,Column#4,Column#5] Key: [[Column#1],[Column#2],[Column#3],[Column#4]] Unique key: []", schema.String())
75+
require.Equal(t, "Column: [Column#1,Column#2,Column#3,Column#4,Column#5] PKOrUK: [[Column#1],[Column#2],[Column#3],[Column#4]] NullableUK: []", schema.String())
7676
}
7777

7878
func TestSchemaRetrieveColumn(t *testing.T) {

pkg/planner/cascades/old/stringer.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ func groupToString(ctx expression.EvalContext, g *memo.Group, idMap map[*memo.Gr
7474
groupLine := bytes.NewBufferString("")
7575
fmt.Fprintf(groupLine, "Group#%d Schema:[%s]", idMap[g], strings.Join(colStrs, ","))
7676

77-
if len(g.Prop.Schema.Keys) > 0 {
78-
ukStrs := make([]string, 0, len(schema.Keys))
79-
for _, key := range schema.Keys {
77+
if len(g.Prop.Schema.PKOrUK) > 0 {
78+
ukStrs := make([]string, 0, len(schema.PKOrUK))
79+
for _, key := range schema.PKOrUK {
8080
ukColStrs := make([]string, 0, len(key))
8181
for _, col := range key {
8282
ukColStrs = append(ukColStrs, col.StringWithCtx(ctx, errors.RedactLogDisable))

pkg/planner/cascades/old/transformation_rules.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,7 +1904,7 @@ func (*outerJoinEliminator) prepareForEliminateOuterJoin(joinExpr *memo.GroupExp
19041904
func (*outerJoinEliminator) isInnerJoinKeysContainUniqueKey(innerGroup *memo.Group, joinKeys *expression.Schema) (bool, error) {
19051905
// builds UniqueKey info of innerGroup.
19061906
innerGroup.BuildKeyInfo()
1907-
for _, keyInfo := range innerGroup.Prop.Schema.Keys {
1907+
for _, keyInfo := range innerGroup.Prop.Schema.PKOrUK {
19081908
joinKeysContainKeyInfo := true
19091909
for _, col := range keyInfo {
19101910
if !joinKeys.Contains(col) {
@@ -2191,7 +2191,7 @@ func (*TransformAggToProj) Match(expr *memo.ExprIter) bool {
21912191
childGroup := expr.GetExpr().Children[0]
21922192
childGroup.BuildKeyInfo()
21932193
schemaByGroupby := expression.NewSchema(agg.GetGroupByCols()...)
2194-
for _, key := range childGroup.Prop.Schema.Keys {
2194+
for _, key := range childGroup.Prop.Schema.PKOrUK {
21952195
if schemaByGroupby.ColumnsIndices(key) != nil {
21962196
return true
21972197
}

pkg/planner/core/casetest/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ go_test(
1212
],
1313
data = glob(["testdata/**"]),
1414
flaky = True,
15-
shard_count = 28,
15+
shard_count = 29,
1616
deps = [
1717
"//pkg/domain",
1818
"//pkg/errno",

0 commit comments

Comments
 (0)