Skip to content

Commit 241c102

Browse files
authored
planner: generate the hash64 and equals for logical aggregation. (#57074)
ref #51664
1 parent 5da9d1a commit 241c102

File tree

3 files changed

+96
-66
lines changed

3 files changed

+96
-66
lines changed

pkg/planner/core/generator/hash64_equals/hash64_equals_generator.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ import (
3434
// If a field is tagged with `hash64-equals`, then it will be computed in hash64 and equals func.
3535
// If a field is not tagged, then it will be skipped.
3636
func GenHash64Equals4LogicalOps() ([]byte, error) {
37-
var structures = []any{logicalop.LogicalJoin{}}
37+
var structures = []any{logicalop.LogicalJoin{}, logicalop.LogicalAggregation{}}
3838
c := new(cc)
3939
c.write(codeGenHash64EqualsPrefix)
4040
for _, s := range structures {
@@ -86,7 +86,7 @@ func genHash64EqualsForLogicalOps(x any) ([]byte, error) {
8686
}
8787
leftCallName := "op." + vType.Field(i).Name
8888
rightCallName := "op2." + vType.Field(i).Name
89-
c.EqualsElement(f.Type, leftCallName, rightCallName)
89+
c.EqualsElement(f.Type, leftCallName, rightCallName, "i")
9090
}
9191
c.write("return true")
9292
c.write("}")
@@ -97,6 +97,8 @@ func logicalOpName2PlanCodecString(name string) string {
9797
switch name {
9898
case "LogicalJoin":
9999
return "plancodec.TypeJoin"
100+
case "LogicalAggregation":
101+
return "plancodec.TypeAgg"
100102
default:
101103
return ""
102104
}
@@ -107,13 +109,22 @@ func isHash64EqualsField(fType reflect.StructField) bool {
107109
}
108110

109111
// EqualsElement EqualsElements generate the equals function for every field inside logical op.
110-
func (c *cc) EqualsElement(fType reflect.Type, lhs, rhs string) {
112+
func (c *cc) EqualsElement(fType reflect.Type, lhs, rhs string, i string) {
111113
switch fType.Kind() {
112114
case reflect.Slice:
113115
c.write("if len(%v) != len(%v) { return false }", lhs, rhs)
114-
c.write("for i, one := range %v {", lhs)
116+
c.write("for %v, one := range %v {", i, lhs)
115117
// one more round
116-
c.EqualsElement(fType.Elem(), "one", rhs+"[i]")
118+
rhs = rhs + "[" + i + "]"
119+
// for ?, one := range [][][][]...
120+
// for use i for out-most ref, for each level deeper, appending another i for simple.
121+
// and you will see:
122+
// for i, one range := [][][]
123+
// for ii, one range := [][]
124+
// for iii, one := range []
125+
// and so on...
126+
newi := i + "i"
127+
c.EqualsElement(fType.Elem(), "one", rhs, newi)
117128
c.write("}")
118129
case reflect.String, reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
119130
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64:

pkg/planner/core/operator/logicalop/hash64_equals_generated.go

Lines changed: 76 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/planner/core/operator/logicalop/logical_aggregation.go

Lines changed: 4 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"github.com/pingcap/tidb/pkg/kv"
2424
"github.com/pingcap/tidb/pkg/parser/ast"
2525
"github.com/pingcap/tidb/pkg/planner/cardinality"
26-
base2 "github.com/pingcap/tidb/pkg/planner/cascades/base"
2726
"github.com/pingcap/tidb/pkg/planner/core/base"
2827
ruleutil "github.com/pingcap/tidb/pkg/planner/core/rule/util"
2928
fd "github.com/pingcap/tidb/pkg/planner/funcdep"
@@ -41,15 +40,15 @@ import (
4140
type LogicalAggregation struct {
4241
LogicalSchemaProducer
4342

44-
AggFuncs []*aggregation.AggFuncDesc
45-
GroupByItems []expression.Expression
43+
AggFuncs []*aggregation.AggFuncDesc `hash64-equals:"true"`
44+
GroupByItems []expression.Expression `hash64-equals:"true"`
4645

4746
// PreferAggType And PreferAggToCop stores aggregation hint information.
4847
PreferAggType uint
4948
PreferAggToCop bool
5049

51-
PossibleProperties [][]*expression.Column
52-
InputCount float64 // InputCount is the input count of this plan.
50+
PossibleProperties [][]*expression.Column `hash64-equals:"true"`
51+
InputCount float64 // InputCount is the input count of this plan.
5352

5453
// NoCopPushDown indicates if planner must not push this agg down to coprocessor.
5554
// It is true when the agg is in the outer child tree of apply.
@@ -62,62 +61,6 @@ func (la LogicalAggregation) Init(ctx base.PlanContext, offset int) *LogicalAggr
6261
return &la
6362
}
6463

65-
// *************************** start implementation of HashEquals interface ****************************
66-
67-
// Hash64 implements the base.Hash64.<0th> interface.
68-
func (la *LogicalAggregation) Hash64(h base2.Hasher) {
69-
h.HashInt(len(la.AggFuncs))
70-
for _, one := range la.AggFuncs {
71-
one.Hash64(h)
72-
}
73-
h.HashInt(len(la.GroupByItems))
74-
for _, one := range la.GroupByItems {
75-
one.Hash64(h)
76-
}
77-
h.HashInt(len(la.PossibleProperties))
78-
for _, one := range la.PossibleProperties {
79-
h.HashInt(len(one))
80-
for _, col := range one {
81-
col.Hash64(h)
82-
}
83-
}
84-
}
85-
86-
// Equals implements the base.HashEquals.<1st> interface.
87-
func (la *LogicalAggregation) Equals(other any) bool {
88-
if other == nil {
89-
return false
90-
}
91-
la2, ok := other.(*LogicalAggregation)
92-
if !ok {
93-
return false
94-
}
95-
if len(la.AggFuncs) != len(la2.AggFuncs) || len(la.GroupByItems) != len(la2.GroupByItems) || len(la.PossibleProperties) != len(la2.PossibleProperties) {
96-
return false
97-
}
98-
for i := range la.AggFuncs {
99-
if !la.AggFuncs[i].Equals(la2.AggFuncs[i]) {
100-
return false
101-
}
102-
}
103-
for i := range la.GroupByItems {
104-
if !la.GroupByItems[i].Equals(la2.GroupByItems[i]) {
105-
return false
106-
}
107-
}
108-
for i := range la.PossibleProperties {
109-
if len(la.PossibleProperties[i]) != len(la2.PossibleProperties[i]) {
110-
return false
111-
}
112-
for j := range la.PossibleProperties[i] {
113-
if !la.PossibleProperties[i][j].Equals(la2.PossibleProperties[i][j]) {
114-
return false
115-
}
116-
}
117-
}
118-
return true
119-
}
120-
12164
// *************************** start implementation of Plan interface ***************************
12265

12366
// ExplainInfo implements base.Plan.<4th> interface.

0 commit comments

Comments
 (0)