@@ -19,21 +19,16 @@ import (
19
19
"encoding/binary"
20
20
"slices"
21
21
22
+ "github.com/pingcap/tidb/pkg/planner/util"
22
23
"github.com/pingcap/tidb/pkg/util/plancodec"
23
24
)
24
25
25
- func encodeIntAsUint32 (result []byte , value int ) []byte {
26
- var buf [4 ]byte
27
- binary .BigEndian .PutUint32 (buf [:], uint32 (value ))
28
- return append (result , buf [:]... )
29
- }
30
-
31
26
// HashCode implements LogicalPlan interface.
32
27
func (p * baseLogicalPlan ) HashCode () []byte {
33
28
// We use PlanID for the default hash, so if two plans do not have
34
29
// the same id, the hash value will never be the same.
35
30
result := make ([]byte , 0 , 4 )
36
- result = encodeIntAsUint32 (result , p .ID ())
31
+ result = util . EncodeIntAsUint32 (result , p .ID ())
37
32
return result
38
33
}
39
34
@@ -43,12 +38,12 @@ func (p *LogicalProjection) HashCode() []byte {
43
38
// Expressions are commonly `Column`s, whose hashcode has the length 9, so
44
39
// we pre-alloc 10 bytes for each expr's hashcode.
45
40
result := make ([]byte , 0 , 12 + len (p .Exprs )* 10 )
46
- result = encodeIntAsUint32 (result , plancodec .TypeStringToPhysicalID (p .TP ()))
47
- result = encodeIntAsUint32 (result , p .QueryBlockOffset ())
48
- result = encodeIntAsUint32 (result , len (p .Exprs ))
41
+ result = util . EncodeIntAsUint32 (result , plancodec .TypeStringToPhysicalID (p .TP ()))
42
+ result = util . EncodeIntAsUint32 (result , p .QueryBlockOffset ())
43
+ result = util . EncodeIntAsUint32 (result , len (p .Exprs ))
49
44
for _ , expr := range p .Exprs {
50
45
exprHashCode := expr .HashCode ()
51
- result = encodeIntAsUint32 (result , len (exprHashCode ))
46
+ result = util . EncodeIntAsUint32 (result , len (exprHashCode ))
52
47
result = append (result , exprHashCode ... )
53
48
}
54
49
return result
@@ -58,9 +53,9 @@ func (p *LogicalProjection) HashCode() []byte {
58
53
func (p * LogicalTableDual ) HashCode () []byte {
59
54
// PlanType + SelectOffset + RowCount
60
55
result := make ([]byte , 0 , 12 )
61
- result = encodeIntAsUint32 (result , plancodec .TypeStringToPhysicalID (p .TP ()))
62
- result = encodeIntAsUint32 (result , p .QueryBlockOffset ())
63
- result = encodeIntAsUint32 (result , p .RowCount )
56
+ result = util . EncodeIntAsUint32 (result , plancodec .TypeStringToPhysicalID (p .TP ()))
57
+ result = util . EncodeIntAsUint32 (result , p .QueryBlockOffset ())
58
+ result = util . EncodeIntAsUint32 (result , p .RowCount )
64
59
return result
65
60
}
66
61
@@ -70,9 +65,9 @@ func (p *LogicalSelection) HashCode() []byte {
70
65
// Conditions are commonly `ScalarFunction`s, whose hashcode usually has a
71
66
// length larger than 20, so we pre-alloc 25 bytes for each expr's hashcode.
72
67
result := make ([]byte , 0 , 12 + len (p .Conditions )* 25 )
73
- result = encodeIntAsUint32 (result , plancodec .TypeStringToPhysicalID (p .TP ()))
74
- result = encodeIntAsUint32 (result , p .QueryBlockOffset ())
75
- result = encodeIntAsUint32 (result , len (p .Conditions ))
68
+ result = util . EncodeIntAsUint32 (result , plancodec .TypeStringToPhysicalID (p .TP ()))
69
+ result = util . EncodeIntAsUint32 (result , p .QueryBlockOffset ())
70
+ result = util . EncodeIntAsUint32 (result , len (p .Conditions ))
76
71
77
72
condHashCodes := make ([][]byte , len (p .Conditions ))
78
73
for i , expr := range p .Conditions {
@@ -82,7 +77,7 @@ func (p *LogicalSelection) HashCode() []byte {
82
77
slices .SortFunc (condHashCodes , func (i , j []byte ) int { return bytes .Compare (i , j ) })
83
78
84
79
for _ , condHashCode := range condHashCodes {
85
- result = encodeIntAsUint32 (result , len (condHashCode ))
80
+ result = util . EncodeIntAsUint32 (result , len (condHashCode ))
86
81
result = append (result , condHashCode ... )
87
82
}
88
83
return result
0 commit comments