Skip to content
New issue

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

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

Already on GitHub? # to your account

Fix hash routing for int in copy #859

Merged
merged 2 commits into from
Dec 12, 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
60 changes: 28 additions & 32 deletions pkg/models/hashfunction/hashfunction.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,40 +104,36 @@ func ApplyHashFunction(inp interface{}, ctype string, hf HashFunctionType) (inte
* Apply routing hash function on bytes receieved in their string representation (from COPY).
*/
func ApplyHashFunctionOnStringRepr(inp []byte, ctype string, hf HashFunctionType) (interface{}, error) {
switch hf {
case HashFunctionIdent:
/* ident is a special case here
* We need to convert raw bytes to approrpiate interface
* because caller expect data in form compatable with CompareKeyRange
*/
switch ctype {
case qdb.ColumnTypeInteger:
n, err := strconv.ParseInt(string(inp), 10, 64)
if err != nil {
return nil, err
}
return n, err
case qdb.ColumnTypeUinteger:
n, err := strconv.ParseUint(string(inp), 10, 64)
if err != nil {
return nil, err
}
return n, err
case qdb.ColumnTypeVarchar:
fallthrough
case qdb.ColumnTypeVarcharDeprecated:
return string(inp), nil

var parsedInp interface{}

/*
* We need to convert raw bytes to approrpiate interface
* because caller expect data in form compatable with CompareKeyRange
*/
switch ctype {
case qdb.ColumnTypeInteger:
n, err := strconv.ParseInt(string(inp), 10, 64)
if err != nil {
return nil, err
}
return inp, nil
case HashFunctionMurmur:
h := murmur3.Sum32(inp)
return uint64(h), nil
case HashFunctionCity:
h := city.Hash32(inp)
return uint64(h), nil
default:
return nil, errNoSuchHashFunction
parsedInp = n
case qdb.ColumnTypeUinteger:
n, err := strconv.ParseUint(string(inp), 10, 64)
if err != nil {
return nil, err
}
parsedInp = n

case qdb.ColumnTypeVarchar:
fallthrough
case qdb.ColumnTypeVarcharHashed:
fallthrough
case qdb.ColumnTypeVarcharDeprecated:
parsedInp = string(inp)
}

return ApplyHashFunction(parsedInp, ctype, hf)
}

// HashFunctionByName returns the corresponding HashFunctionType based on the given hash function name.
Expand Down
12 changes: 6 additions & 6 deletions test/regress/tests/router/expected/copy_multishard.out
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,24 @@ TABLE xx /* __spqr__execute_on: sh1 */;
NOTICE: send query to shard(s) : sh1
i | j
---+---
2 | 2
3 | 3
1 | 1
5 | 5
6 | 6
7 | 7
9 | 9
1 | 1
5 | 5
6 | 6
9 | 9
(10 rows)
(8 rows)

TABLE xx /* __spqr__execute_on: sh2 */;
NOTICE: send query to shard(s) : sh2
i | j
----+----
1 | 1
2 | 2
3 | 3
4 | 4
7 | 7
8 | 8
10 | 10
2 | 2
Expand All @@ -89,7 +89,7 @@ NOTICE: send query to shard(s) : sh2
7 | 7
8 | 8
10 | 10
(10 rows)
(12 rows)

DROP TABLE xx;
NOTICE: send query to shard(s) : sh1,sh2
Expand Down
Loading
Loading