Skip to content
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

Fix hint routing test #718

Merged
merged 2 commits into from
Jul 14, 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
52 changes: 52 additions & 0 deletions pkg/models/kr/keyrange.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package kr
import (
"encoding/binary"
"fmt"
"strconv"
"strings"

"github.com/pg-sharding/spqr/pkg/models/distributions"
Expand Down Expand Up @@ -93,6 +94,32 @@ func (kr *KeyRange) SendFunc(attribInd int) string {
}
}

func (kr *KeyRange) RecvFunc(attribInd int, val string) error {
var err error
switch kr.ColumnTypes[attribInd] {
case qdb.ColumnTypeVarcharDeprecated:
fallthrough
case qdb.ColumnTypeVarchar:
kr.LowerBound[attribInd] = val
case qdb.ColumnTypeVarcharHashed: /* is uint */
fallthrough
case qdb.ColumnTypeUinteger:
kr.LowerBound[attribInd], err = strconv.ParseUint(val, 10, 64)
if err != nil {
return err
}

case qdb.ColumnTypeInteger:
kr.LowerBound[attribInd], err = strconv.ParseInt(val, 10, 64)
if err != nil {
return err
}
default:
panic("unknown column type")
}
return nil
}

func (kr *KeyRange) Raw() [][]byte {
res := make([][]byte, len(kr.ColumnTypes))

Expand All @@ -113,6 +140,31 @@ func (kr *KeyRange) SendRaw() []string {
return res
}

func (kr *KeyRange) RecvRaw(vals []string) error {
kr.LowerBound = make([]interface{}, len(kr.ColumnTypes))

for i := 0; i < len(kr.ColumnTypes); i++ {
err := kr.RecvFunc(i, vals[i])
if err != nil {
return err
}
}

return nil
}

func KeyRangeBoundFromStrings(colTypes []string, vals []string) ([]interface{}, error) {
kr := &KeyRange{
ColumnTypes: colTypes,
}

if err := kr.RecvRaw(vals); err != nil {
return nil, err
}

return kr.LowerBound, nil
}

// TODO: use it
var MissTypedKeyRange = fmt.Errorf("key range bound is mistyped")

Expand Down
16 changes: 12 additions & 4 deletions router/relay/qstate.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/jackc/pgx/v5/pgproto3"
"github.com/pg-sharding/lyx/lyx"
"github.com/pg-sharding/spqr/pkg/config"
"github.com/pg-sharding/spqr/pkg/models/kr"
"github.com/pg-sharding/spqr/pkg/models/spqrerror"
"github.com/pg-sharding/spqr/pkg/session"
"github.com/pg-sharding/spqr/pkg/spqrlog"
Expand All @@ -32,7 +33,7 @@ func deparseRouteHint(rst RelayStateMgr, params map[string]string) (routehint.Ro

dsId := ""
if dsId, ok = params[session.SPQR_DISTRIBUTION]; !ok {
return nil, spqrerror.New(spqrerror.SPQR_NO_DISTRIBUTION, "got sharding key in comment without distribution")
return nil, spqrerror.New(spqrerror.SPQR_NO_DISTRIBUTION, "sharding key in comment without distribution")
}

ctx := context.TODO()
Expand All @@ -41,12 +42,19 @@ func deparseRouteHint(rst RelayStateMgr, params map[string]string) (routehint.Ro
return nil, err
}

distrib, err := rst.QueryRouter().Mgr().GetDistribution(ctx, dsId)
if err != nil {
return nil, err
}

// TODO: fix this
compositeKey := []interface{}{
val,
compositeKey, err := kr.KeyRangeBoundFromStrings(distrib.ColTypes, []string{val})

if err != nil {
return nil, err
}

ds, err := rst.QueryRouter().DeparseKeyWithRangesInternal(context.TODO(), compositeKey, krs)
ds, err := rst.QueryRouter().DeparseKeyWithRangesInternal(ctx, compositeKey, krs)
if err != nil {
return nil, err
}
Expand Down
12 changes: 12 additions & 0 deletions script/regress_local.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@

for name in `ls -1 test/regress/tests/router/sql/`;
do
echo $name start
cat test/regress/tests/router/sql/$name | psql "host=localhost port=6432 dbname=db1" --echo-all --quiet > test/regress/tests/router/expected/$(basename $name .sql).out 2>&1;
RESULT=$?
if [ ! $RESULT -eq 0 ]; then
exit 1
fi
echo $name done
done

for name in `ls -1 test/regress/tests/console/sql/`;
do
echo $name start
cat test/regress/tests/console/sql/$name | psql "host=localhost port=6432 dbname=spqr-console" --echo-all --quiet > test/regress/tests/console/expected/$(basename $name .sql).out 2>&1;
RESULT=$?
if [ ! $RESULT -eq 0 ]; then
exit 1
fi
echo $name done
done


Expand Down
Loading