Skip to content
This repository has been archived by the owner on Aug 26, 2024. It is now read-only.

Handle missing values when building a routing key #48

Merged
merged 1 commit into from
Mar 1, 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
1 change: 1 addition & 0 deletions policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -573,6 +573,7 @@ func (t *tokenAwareHostPolicy) Pick(qry ExecutableQuery) NextHost {

routingKey, err := qry.GetRoutingKey()
if err != nil {
t.logger.Printf("unable to get routing key for query: %v", err)
return t.fallback.Pick(qry)
} else if routingKey == nil {
return t.fallback.Pick(qry)
Expand Down
14 changes: 14 additions & 0 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,13 +665,16 @@ func (s *Session) routingKeyInfo(ctx context.Context, stmt string) (*routingKeyI
if len(info.request.pkeyColumns) > 0 {
// proto v4 dont need to calculate primary key columns
types := make([]TypeInfo, len(info.request.pkeyColumns))
names := make([]string, len(info.request.pkeyColumns))
for i, col := range info.request.pkeyColumns {
types[i] = info.request.columns[col].TypeInfo
names[i] = info.request.columns[col].Name
}

routingKeyInfo := &routingKeyInfo{
indexes: info.request.pkeyColumns,
types: types,
names: names,
lwt: info.request.lwt,
partitioner: partitioner,
keyspace: keyspace,
Expand Down Expand Up @@ -709,6 +712,7 @@ func (s *Session) routingKeyInfo(ctx context.Context, stmt string) (*routingKeyI
routingKeyInfo := &routingKeyInfo{
indexes: make([]int, size),
types: make([]TypeInfo, size),
names: make([]string, size),
lwt: info.request.lwt,
partitioner: partitioner,
keyspace: keyspace,
Expand All @@ -725,6 +729,7 @@ func (s *Session) routingKeyInfo(ctx context.Context, stmt string) (*routingKeyI
// there may be many such bound columns, pick the first
routingKeyInfo.indexes[keyIndex] = argIndex
routingKeyInfo.types[keyIndex] = boundColumn.TypeInfo
routingKeyInfo.names[keyIndex] = boundColumn.Name
break
}
}
Expand Down Expand Up @@ -2078,6 +2083,10 @@ func createRoutingKey(routingKeyInfo *routingKeyInfo, values []interface{}) ([]b
}

if len(routingKeyInfo.indexes) == 1 {
if len(values) <= routingKeyInfo.indexes[0] {
return nil, fmt.Errorf("gocql: missing routing key value at index %d for column %q",
routingKeyInfo.indexes[0], routingKeyInfo.names[0])
}
// single column routing key
routingKey, err := Marshal(
routingKeyInfo.types[0],
Expand All @@ -2092,6 +2101,10 @@ func createRoutingKey(routingKeyInfo *routingKeyInfo, values []interface{}) ([]b
// composite routing key
buf := bytes.NewBuffer(make([]byte, 0, 256))
for i := range routingKeyInfo.indexes {
if len(values) <= routingKeyInfo.indexes[i] {
return nil, fmt.Errorf("gocql: missing routing key value at index %d for column %q",
routingKeyInfo.indexes[i], routingKeyInfo.names[i])
}
encoded, err := Marshal(
routingKeyInfo.types[i],
values[routingKeyInfo.indexes[i]],
Expand Down Expand Up @@ -2154,6 +2167,7 @@ type routingKeyInfoLRU struct {
type routingKeyInfo struct {
indexes []int
types []TypeInfo
names []string
keyspace string
table string
lwt bool
Expand Down
Loading