-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
planner: integrate hashEqual interface into LogicalPlan and expressio…
- Loading branch information
Showing
11 changed files
with
219 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test") | ||
|
||
go_library( | ||
name = "base", | ||
srcs = [ | ||
"base.go", | ||
"hash_equaler.go", | ||
], | ||
importpath = "github.com/pingcap/tidb/pkg/planner/cascades/base", | ||
visibility = ["//visibility:public"], | ||
) | ||
|
||
go_test( | ||
name = "base_test", | ||
timeout = "short", | ||
srcs = [ | ||
"base_test.go", | ||
"hash_equaler_test.go", | ||
], | ||
embed = [":base"], | ||
flaky = True, | ||
shard_count = 3, | ||
deps = ["@com_github_stretchr_testify//require"], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright 2024 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package base | ||
|
||
// Hash64 is the interface for hashcode. | ||
// It is used to calculate the lossy digest of an object to return uint64 | ||
// rather than compacted bytes from cascaded operators bottom-up. | ||
type Hash64 interface { | ||
// Hash64 returns the uint64 digest of an object. | ||
Hash64(h Hasher) | ||
} | ||
|
||
// Equals is the interface for equality check. | ||
// When we need to compare two objects when countering hash conflicts, we can | ||
// use this interface to check whether they are equal. | ||
type Equals interface { | ||
// Equals checks whether two base objects are equal. | ||
Equals(any) bool | ||
} | ||
|
||
// HashEquals is the interface for hash64 and equality check. | ||
type HashEquals interface { | ||
Hash64 | ||
Equals | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// Copyright 2024 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package base | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
type testcase struct { | ||
a int | ||
b int | ||
c string | ||
} | ||
|
||
type TestEquals[T any] interface { | ||
EqualsT(other T) bool | ||
} | ||
|
||
type TestEqualAny interface { | ||
EqualsAny(other any) bool | ||
} | ||
|
||
type TestSuperEquals interface { | ||
TestEquals[TestSuperEquals] | ||
} | ||
|
||
func (tc *testcase) EqualsT(other *testcase) bool { | ||
return tc.a == other.a && tc.b == other.b && tc.c == other.c | ||
} | ||
|
||
func (tc *testcase) EqualsAny(other any) bool { | ||
tc1, ok := other.(*testcase) | ||
if !ok { | ||
return false | ||
} | ||
return tc.a == tc1.a && tc.b == tc1.b && tc.c == tc1.c | ||
} | ||
|
||
// goos: linux | ||
// goarch: amd64 | ||
// pkg: github.com/pingcap/tidb/pkg/planner/cascades/memo | ||
// cpu: 12th Gen Intel(R) Core(TM) i7-12700KF | ||
// BenchmarkEqualsT | ||
// BenchmarkEqualsT-20 1000000000 0.8981 ns/op | ||
func BenchmarkEqualsT(b *testing.B) { | ||
tc1 := &testcase{a: 1, b: 2, c: "3"} | ||
var tc2 TestEquals[*testcase] = &testcase{a: 1, b: 2, c: "3"} | ||
for i := 0; i < b.N; i++ { | ||
if tc3, ok := tc2.(*testcase); ok { | ||
tc1.EqualsT(tc3) | ||
} | ||
} | ||
} | ||
|
||
// goos: linux | ||
// goarch: amd64 | ||
// pkg: github.com/pingcap/tidb/pkg/planner/cascades/memo | ||
// cpu: 12th Gen Intel(R) Core(TM) i7-12700KF | ||
// BenchmarkEqualsAny | ||
// BenchmarkEqualsAny-20 1000000000 0.4837 ns/op | ||
func BenchmarkEqualsAny(b *testing.B) { | ||
tc1 := &testcase{a: 1, b: 2, c: "3"} | ||
var tc2 TestEqualAny = &testcase{a: 1, b: 2, c: "3"} | ||
for i := 0; i < b.N; i++ { | ||
tc1.EqualsAny(tc2) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters