Skip to content
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
2 changes: 1 addition & 1 deletion go/sqltypes/arithmetic.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func ToUint64(v Value) (uint64, error) {
panic("unreachable")
}

// ToInt64 converts Value to uint64.
// ToInt64 converts Value to int64.
func ToInt64(v Value) (int64, error) {
num, err := newIntegralNumeric(v)
if err != nil {
Expand Down
15 changes: 14 additions & 1 deletion go/vt/vtgate/vindexes/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"encoding/binary"
"encoding/hex"
"fmt"
"strconv"

"vitess.io/vitess/go/sqltypes"
"vitess.io/vitess/go/vt/key"
Expand Down Expand Up @@ -69,7 +70,19 @@ func (vind *Hash) IsFunctional() bool {
func (vind *Hash) Map(cursor VCursor, ids []sqltypes.Value) ([]key.Destination, error) {
out := make([]key.Destination, len(ids))
for i, id := range ids {
num, err := sqltypes.ToUint64(id)
var num uint64
var err error

if id.IsSigned() {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is better than what we have, but this will fail implicit conversions from string values. You could instead recreate (or reuse) this logic: https://github.com/vitessio/vitess/blob/master/go/sqltypes/arithmetic.go#L294, which will make this function more permissive.

I personally prefer more strict typing, but others have argued that we should more closely mimic MySQL. Let me know which way you want to go.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's how I originally did it, but apparently got confused along the way. See if it looks better now.

// This is ToUint64 with no check on negative values.
str := id.ToString()
var ival int64
ival, err = strconv.ParseInt(str, 10, 64)
num = uint64(ival)
} else {
num, err = sqltypes.ToUint64(id)
}

if err != nil {
out[i] = key.DestinationNone{}
continue
Expand Down
50 changes: 47 additions & 3 deletions go/vt/vtgate/vindexes/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ func TestHashMap(t *testing.T) {
sqltypes.NewInt64(4),
sqltypes.NewInt64(5),
sqltypes.NewInt64(6),
sqltypes.NewInt64(0),
sqltypes.NewInt64(-1),
sqltypes.NewUint64(18446744073709551615), // 2^64 - 1
sqltypes.NewInt64(9223372036854775807), // 2^63 - 1
sqltypes.NewUint64(9223372036854775807), // 2^63 - 1
sqltypes.NewInt64(-9223372036854775808), // - 2^63
})
if err != nil {
t.Error(err)
Expand All @@ -68,9 +74,19 @@ func TestHashMap(t *testing.T) {
key.DestinationKeyspaceID([]byte("\xd2\xfd\x88g\xd5\r-\xfe")),
key.DestinationKeyspaceID([]byte("p\xbb\x02<\x81\f\xa8z")),
key.DestinationKeyspaceID([]byte("\xf0\x98H\n\xc4ľq")),
key.DestinationKeyspaceID([]byte("\x8c\xa6M\xe9\xc1\xb1#\xa7")),
key.DestinationKeyspaceID([]byte("5UP\xb2\x15\x0e$Q")),
key.DestinationKeyspaceID([]byte("5UP\xb2\x15\x0e$Q")),
key.DestinationKeyspaceID([]byte("\xf7}H\xaaݡ\xf1\xbb")),
key.DestinationKeyspaceID([]byte("\xf7}H\xaaݡ\xf1\xbb")),
key.DestinationKeyspaceID([]byte("\x95\xf8\xa5\xe5\xdd1\xd9\x00")),
}
if !reflect.DeepEqual(got, want) {
t.Errorf("Map(): %#v, want %+v", got, want)
for i, v := range got {
if v.String() != want[i].String() {
t.Errorf("Map() %d: %#v, want %#v", i, v, want[i])
}
}
}
}

Expand All @@ -95,11 +111,39 @@ func TestHashVerify(t *testing.T) {
}

func TestHashReverseMap(t *testing.T) {
got, err := hash.(Reversible).ReverseMap(nil, [][]byte{[]byte("\x16k@\xb4J\xbaK\xd6")})
got, err := hash.(Reversible).ReverseMap(nil, [][]byte{
[]byte("\x16k@\xb4J\xbaK\xd6"),
[]byte("\x06\xe7\xea\"Βp\x8f"),
[]byte("N\xb1\x90ɢ\xfa\x16\x9c"),
[]byte("\xd2\xfd\x88g\xd5\r-\xfe"),
[]byte("p\xbb\x02<\x81\f\xa8z"),
[]byte("\xf0\x98H\n\xc4ľq"),
[]byte("\x8c\xa6M\xe9\xc1\xb1#\xa7"),
[]byte("5UP\xb2\x15\x0e$Q"),
[]byte("5UP\xb2\x15\x0e$Q"),
[]byte("\xf7}H\xaaݡ\xf1\xbb"),
[]byte("\xf7}H\xaaݡ\xf1\xbb"),
[]byte("\x95\xf8\xa5\xe5\xdd1\xd9\x00"),
})
if err != nil {
t.Error(err)
}
want := []sqltypes.Value{sqltypes.NewUint64(uint64(1))}
neg1 := int64(-1)
negmax := int64(-9223372036854775808)
want := []sqltypes.Value{
sqltypes.NewUint64(uint64(1)),
sqltypes.NewUint64(2),
sqltypes.NewUint64(3),
sqltypes.NewUint64(4),
sqltypes.NewUint64(5),
sqltypes.NewUint64(6),
sqltypes.NewUint64(0),
sqltypes.NewUint64(uint64(neg1)),
sqltypes.NewUint64(18446744073709551615), // 2^64 - 1
sqltypes.NewUint64(9223372036854775807), // 2^63 - 1
sqltypes.NewUint64(9223372036854775807), // 2^63 - 1
sqltypes.NewUint64(uint64(negmax)), // - 2^63
}
if !reflect.DeepEqual(got, want) {
t.Errorf("ReverseMap(): %v, want %v", got, want)
}
Expand Down