-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
expression: POC implementation of Vitess hashing algorithm. #20972
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7656,3 +7656,18 @@ func (s *testIntegrationSerialSuite) TestIssue20608(c *C) { | |
tk := testkit.NewTestKit(c, s.store) | ||
tk.MustQuery("select '䇇Հ' collate utf8mb4_bin like '___Հ';").Check(testkit.Rows("0")) | ||
} | ||
|
||
func (s *testIntegrationSuite) TestVitessHash(c *C) { | ||
defer s.cleanEnv(c) | ||
tk := testkit.NewTestKit(c, s.store) | ||
tk.MustExec("use test") | ||
tk.MustQuery("select vitess_hash(30375298039) from t").Check(testkit.Rows("\x03\x12\x65\x66\x1E\x5F\x11\x33")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we create a table There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I did not create There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's better to add some statements like
for safty. |
||
// Same as previous but passed as a binary string | ||
tk.MustQuery("select vitess_hash(x'00000007128243F7') from t").Check(testkit.Rows("\x03\x12\x65\x66\x1E\x5F\x11\x33")) | ||
// Less bits, should be prefixed with zeroes | ||
tk.MustQuery("select vitess_hash(x'07128243F7') from t").Check(testkit.Rows("\x03\x12\x65\x66\x1E\x5F\x11\x33")) | ||
tk.MustQuery("select vitess_hash(1123) from t").Check(testkit.Rows("\x03\x1B\x56\x5D\x41\xBD\xF8\xCA")) | ||
tk.MustQuery("select vitess_hash(30573721600) from t").Check(testkit.Rows("\x1E\xFD\x64\x39\xF2\x05\x0F\xFD")) | ||
tk.MustQuery("select vitess_hash(convert(116, decimal(8,4))) from t").Check(testkit.Rows("\x1E\x17\x88\xFF\x0F\xDE\x09\x3C")) | ||
tk.MustQuery(fmt.Sprintf("select vitess_hash(%d) from t", uint64(math.MaxUint64))).Check(testkit.Rows("\x35\x55\x50\xB2\x15\x0E\x24\x51")) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2020 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package vitess | ||
|
||
import ( | ||
"crypto/cipher" | ||
"crypto/des" | ||
"encoding/binary" | ||
"encoding/hex" | ||
"fmt" | ||
|
||
"github.com/pingcap/errors" | ||
) | ||
|
||
var nullKeyBlock cipher.Block | ||
|
||
func init() { | ||
block, err := des.NewCipher(make([]byte, 8)) | ||
if err != nil { | ||
panic(errors.Trace(err)) | ||
} | ||
nullKeyBlock = block | ||
} | ||
|
||
// VitessHash implements vitess' method of calculating a hash used for determining a shard key range. | ||
// Uses a DES encryption with 64 bit key, 64 bit block, null-key | ||
func VitessHash(shardKey []byte) ([]byte, error) { | ||
if len(shardKey) > 8 { | ||
return nil, fmt.Errorf("shard key is too long: %v", hex.EncodeToString(shardKey)) | ||
} else if len(shardKey) == 8 { | ||
var hashed [8]byte | ||
nullKeyBlock.Encrypt(hashed[:], shardKey[:]) | ||
return hashed[:], nil | ||
} else { | ||
var keybytes, hashed [8]byte | ||
copy(keybytes[len(keybytes)-len(shardKey):], shardKey) | ||
nullKeyBlock.Encrypt(hashed[:], keybytes[:]) | ||
return hashed[:], nil | ||
} | ||
} | ||
|
||
// VitessHashUint64 implements vitess' method of calculating a hash used for determining a shard key range. | ||
// Uses a DES encryption with 64 bit key, 64 bit block, null-key | ||
func VitessHashUint64(shardKey uint64) ([]byte, error) { | ||
var keybytes [8]byte | ||
binary.BigEndian.PutUint64(keybytes[:], shardKey) | ||
return VitessHash(keybytes[:]) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright 2020 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, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package vitess | ||
|
||
import ( | ||
"encoding/hex" | ||
bezmax marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"math" | ||
"strings" | ||
"testing" | ||
|
||
. "github.com/pingcap/check" | ||
"github.com/pingcap/tidb/util/testleak" | ||
) | ||
|
||
var _ = Suite(&testVitessSuite{}) | ||
|
||
func TestT(t *testing.T) { | ||
TestingT(t) | ||
} | ||
|
||
type testVitessSuite struct { | ||
} | ||
|
||
func toHex(buf []byte) string { | ||
return strings.ToUpper(hex.EncodeToString(buf)) | ||
} | ||
|
||
func fromHex(hexStr string) []byte { | ||
if buf, err := hex.DecodeString(hexStr); err == nil { | ||
return buf | ||
} else { | ||
panic(err) | ||
} | ||
} | ||
|
||
var _ = Suite(&testVitessSuite{}) | ||
|
||
func (s *testVitessSuite) TestVitessHash(c *C) { | ||
defer testleak.AfterTest(c)() | ||
|
||
hashed, err := VitessHashUint64(30375298039) | ||
c.Assert(err, IsNil) | ||
c.Assert(toHex(hashed), Equals, "031265661E5F1133") | ||
|
||
// Same as previous value but passed as a []byte instead | ||
hashed, err = VitessHash(fromHex("00000007128243F7")) | ||
c.Assert(err, IsNil) | ||
c.Assert(toHex(hashed), Equals, "031265661E5F1133") | ||
|
||
hashed, err = VitessHashUint64(1123) | ||
c.Assert(err, IsNil) | ||
c.Assert(toHex(hashed), Equals, "031B565D41BDF8CA") | ||
|
||
hashed, err = VitessHashUint64(30573721600) | ||
c.Assert(err, IsNil) | ||
c.Assert(toHex(hashed), Equals, "1EFD6439F2050FFD") | ||
|
||
hashed, err = VitessHashUint64(116) | ||
c.Assert(err, IsNil) | ||
c.Assert(toHex(hashed), Equals, "1E1788FF0FDE093C") | ||
|
||
hashed, err = VitessHashUint64(math.MaxUint64) | ||
c.Assert(err, IsNil) | ||
c.Assert(toHex(hashed), Equals, "355550B2150E2451") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we implement the vectorized expression function for
builtinVitessHashSig
inexpression/builtin_miscellaneous_vec.go
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Reminiscent Could you explain what are "vectorized expression functions" in this context?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can see this blog for more information. In short, the expression function
builtinVitessHashSig.evalString
here processes one row of data at a time. And if you implement the vectorized expression function forbuiltinVitessHashSig.vecEvalString
inexpression/builtin_miscellaneous_vec.go
, it can process a batch at a time. The blog introduces more information.