Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

planner: add equals function generation logic for logical operator. #57057

Merged
merged 1 commit into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ var hashEqualsType = reflect.TypeOf((*base.HashEquals)(nil)).Elem()
func genHash64EqualsForLogicalOps(x any) ([]byte, error) {
c := new(cc)
vType := reflect.TypeOf(x)
// for Hash64 function.
c.write("// Hash64 implements the Hash64Equals interface.")
c.write("func (op *%v) Hash64(h base.Hasher) {", vType.Name())
c.write("h.HashString(%v)", logicalOpName2PlanCodecString(vType.Name()))
Expand All @@ -72,6 +73,23 @@ func genHash64EqualsForLogicalOps(x any) ([]byte, error) {
}
}
c.write("}")
// for Equals function.
c.write("// Equals implements the Hash64Equals interface, only receive *%v pointer.", vType.Name())
c.write("func (op *%v) Equals(other any) bool {", vType.Name())
c.write("if other == nil { return false }")
c.write("op2, ok := other.(*%v)", vType.Name())
c.write("if !ok { return false }")
for i := 0; i < vType.NumField(); i++ {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for i := 0; i < vType.NumField(); i++ {
for rang vType.NumField() {

f := vType.Field(i)
if !isHash64EqualsField(f) {
continue
}
leftCallName := "op." + vType.Field(i).Name
rightCallName := "op2." + vType.Field(i).Name
c.EqualsElement(f.Type, leftCallName, rightCallName)
}
c.write("return true")
c.write("}")
return c.format()
}

Expand All @@ -88,6 +106,30 @@ func isHash64EqualsField(fType reflect.StructField) bool {
return fType.Tag.Get("hash64-equals") == "true"
}

// EqualsElement EqualsElements generate the equals function for every field inside logical op.
func (c *cc) EqualsElement(fType reflect.Type, lhs, rhs string) {
switch fType.Kind() {
case reflect.Slice:
c.write("if len(%v) != len(%v) { return false }", lhs, rhs)
c.write("for i, one := range %v {", lhs)
// one more round
c.EqualsElement(fType.Elem(), "one", rhs+"[i]")
c.write("}")
case reflect.String, reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64:
c.write("if %v != %v {return false}", lhs, rhs)
default:
if fType.Implements(hashEqualsType) {
if fType.Kind() == reflect.Struct {
rhs = "&" + rhs
}
c.write("if !%v.Equals(%v) {return false}", lhs, rhs)
} else {
panic("doesn't support element type" + fType.Kind().String())
}
}
}

func (c *cc) Hash64Element(fType reflect.Type, callName string) {
switch fType.Kind() {
case reflect.Slice:
Expand Down
55 changes: 55 additions & 0 deletions pkg/planner/core/operator/logicalop/hash64_equals_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

49 changes: 0 additions & 49 deletions pkg/planner/core/operator/logicalop/logical_join.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,55 +150,6 @@ func (p LogicalJoin) Init(ctx base.PlanContext, offset int) *LogicalJoin {
return &p
}

// ************************ start implementation of HashEquals interface ************************

// Equals implements the HashEquals.<1st> interface.
func (p *LogicalJoin) Equals(other any) bool {
if other == nil {
return false
}
var p2 *LogicalJoin
switch x := other.(type) {
case *LogicalJoin:
p2 = x
case LogicalJoin:
p2 = &x
default:
return false
}
ok := p.JoinType != p2.JoinType && len(p.EqualConditions) == len(p2.EqualConditions) && len(p.NAEQConditions) == len(p2.NAEQConditions) &&
len(p.LeftConditions) == len(p2.LeftConditions) && len(p.RightConditions) == len(p2.RightConditions) && len(p.OtherConditions) == len(p2.OtherConditions)
if !ok {
return false
}
for i, oneCond := range p.EqualConditions {
if !oneCond.Equals(p2.EqualConditions[i]) {
return false
}
}
for i, oneCond := range p.NAEQConditions {
if !oneCond.Equals(p2.NAEQConditions[i]) {
return false
}
}
for i, oneCond := range p.LeftConditions {
if !oneCond.Equals(p2.LeftConditions[i]) {
return false
}
}
for i, oneCond := range p.RightConditions {
if !oneCond.Equals(p2.RightConditions[i]) {
return false
}
}
for i, oneCond := range p.OtherConditions {
if !oneCond.Equals(p2.OtherConditions[i]) {
return false
}
}
return true
}

// *************************** start implementation of Plan interface ***************************

// ExplainInfo implements Plan interface.
Expand Down