-
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.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
11 changed files
with
316 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
use test; | ||
drop table if exists t; | ||
create table t( | ||
customer_id bigint, | ||
id bigint, | ||
expected_shard bigint unsigned, | ||
computed_shard bigint unsigned null, | ||
primary key (customer_id, id) | ||
); | ||
create index t_vitess_shard on t((vitess_hash(customer_id) >> 56)); | ||
explain format = 'brief' select customer_id from t where (vitess_hash(customer_id) >> 56) = x'd6' ORDER BY id; | ||
id estRows task access object operator info | ||
Projection 10.00 root test.t.customer_id | ||
└─Sort 10.00 root test.t.id | ||
└─IndexLookUp 10.00 root | ||
├─IndexRangeScan(Build) 10.00 cop[tikv] table:t, index:t_vitess_shard(vitess_hash(`customer_id`) >> 56) range:[214,214], keep order:false, stats:pseudo | ||
└─TableRowIDScan(Probe) 10.00 cop[tikv] table:t keep order:false, stats:pseudo | ||
explain format = 'brief' select id from t where (vitess_hash(customer_id) >> 56) IN (x'e0', x'e1') AND id BETWEEN 2 AND 5 ORDER BY id; | ||
id estRows task access object operator info | ||
Projection 0.50 root test.t.id | ||
└─Sort 0.50 root test.t.id | ||
└─IndexLookUp 0.50 root | ||
├─IndexRangeScan(Build) 20.00 cop[tikv] table:t, index:t_vitess_shard(vitess_hash(`customer_id`) >> 56) range:[224,224], [225,225], keep order:false, stats:pseudo | ||
└─Selection(Probe) 0.50 cop[tikv] ge(test.t.id, 2), le(test.t.id, 5) | ||
└─TableRowIDScan 20.00 cop[tikv] table:t keep order:false, stats:pseudo | ||
explain format = 'brief' select hex(vitess_hash(1123)) from t; | ||
id estRows task access object operator info | ||
Projection 10000.00 root 31B565D41BDF8CA->Column#7 | ||
└─IndexReader 10000.00 root index:IndexFullScan | ||
└─IndexFullScan 10000.00 cop[tikv] table:t, index:t_vitess_shard(vitess_hash(`customer_id`) >> 56) keep order:false, stats:pseudo |
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,13 @@ | ||
use test; | ||
drop table if exists t; | ||
create table t( | ||
customer_id bigint, | ||
id bigint, | ||
expected_shard bigint unsigned, | ||
computed_shard bigint unsigned null, | ||
primary key (customer_id, id) | ||
); | ||
create index t_vitess_shard on t((vitess_hash(customer_id) >> 56)); | ||
explain format = 'brief' select customer_id from t where (vitess_hash(customer_id) >> 56) = x'd6' ORDER BY id; | ||
explain format = 'brief' select id from t where (vitess_hash(customer_id) >> 56) IN (x'e0', x'e1') AND id BETWEEN 2 AND 5 ORDER BY id; | ||
explain format = 'brief' select hex(vitess_hash(1123)) from t; |
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
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,42 @@ | ||
// Copyright 2021 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" | ||
|
||
"github.com/pingcap/errors" | ||
) | ||
|
||
var nullKeyBlock cipher.Block | ||
|
||
func init() { | ||
var err error | ||
nullKeyBlock, err = des.NewCipher(make([]byte, 8)) | ||
if err != nil { | ||
panic(errors.Trace(err)) | ||
} | ||
} | ||
|
||
// HashUint64 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 HashUint64(shardKey uint64) (uint64, error) { | ||
var keybytes [8]byte | ||
binary.BigEndian.PutUint64(keybytes[:], shardKey) | ||
var hashed [8]byte | ||
nullKeyBlock.Encrypt(hashed[:], keybytes[:]) | ||
return binary.BigEndian.Uint64(hashed[:]), nil | ||
} |
Oops, something went wrong.