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
3 changes: 3 additions & 0 deletions data/test/vtgate/schema_test.json
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@
}
]
},
"pin_test": {
"pinned": "80"
},
"weird`name": {
"column_vindexes": [
{
Expand Down
20 changes: 20 additions & 0 deletions data/test/vtgate/select_cases.txt
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,26 @@
}
}

# select from pinned table
"select * from pin_test"
{
"Original": "select * from pin_test",
"Instructions": {
"Opcode": "SelectEqualUnique",
"Keyspace": {
"Name": "user",
"Sharded": true
},
"Query": "select * from pin_test",
"FieldQuery": "select * from pin_test where 1 != 1",
"Vindex": "binary",
"Values": [
"\ufffd"
]
}
}


# select from dual on sharded keyspace
"select @@session.auto_increment_increment from user.dual"
{
Expand Down
109 changes: 61 additions & 48 deletions go/vt/proto/vschema/vschema.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion go/vt/vtgate/vindexes/vschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package vindexes

import (
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
Expand Down Expand Up @@ -203,7 +204,13 @@ func buildTables(source *vschemapb.SrvVSchema, vschema *VSchema) error {
if table.Type == "sequence" {
t.IsSequence = true
}
if keyspace.Sharded && len(table.ColumnVindexes) == 0 {
if table.Pinned != "" {
decoded, err := hex.DecodeString(table.Pinned)
if err != nil {
return fmt.Errorf("could not decode the keyspace id for pin: %v", err)
}
t.Pinned = decoded
} else if keyspace.Sharded && len(table.ColumnVindexes) == 0 {
return fmt.Errorf("missing primary col vindex for table: %s", tname)
}

Expand Down
55 changes: 55 additions & 0 deletions go/vt/vtgate/vindexes/vschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,61 @@ func TestVSchemaColumnsFail(t *testing.T) {
}
}

func TestVSchemaPinned(t *testing.T) {
good := vschemapb.SrvVSchema{
Keyspaces: map[string]*vschemapb.Keyspace{
"sharded": {
Sharded: true,
Tables: map[string]*vschemapb.Table{
"t1": {
Pinned: "80",
},
},
},
},
}
got, err := BuildVSchema(&good)
if err != nil {
t.Error(err)
}
ks := &Keyspace{
Name: "sharded",
Sharded: true,
}
t1 := &Table{
Name: sqlparser.NewTableIdent("t1"),
Keyspace: ks,
Pinned: []byte{0x80},
}
dual := &Table{
Name: sqlparser.NewTableIdent("dual"),
Keyspace: ks,
Pinned: []byte{0},
}
want := &VSchema{
uniqueTables: map[string]*Table{
"t1": t1,
"dual": dual,
},
uniqueVindexes: map[string]Vindex{},
Keyspaces: map[string]*KeyspaceSchema{
"sharded": {
Keyspace: ks,
Tables: map[string]*Table{
"t1": t1,
"dual": dual,
},
Vindexes: map[string]Vindex{},
},
},
}
if !reflect.DeepEqual(got, want) {
gotjson, _ := json.Marshal(got)
wantjson, _ := json.Marshal(want)
t.Errorf("BuildVSchema:\n%s, want\n%s", gotjson, wantjson)
}
}

func TestShardedVSchemaOwned(t *testing.T) {
good := vschemapb.SrvVSchema{
Keyspaces: map[string]*vschemapb.Keyspace{
Expand Down
5 changes: 5 additions & 0 deletions proto/vschema.proto
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,11 @@ message Table {
AutoIncrement auto_increment = 3;
// columns lists the columns for the table.
repeated Column columns = 4;
// pinned pins an unsharded table to a specific
// shard, as dictated by the keyspace id.
// The keyspace id is represened in hex form
// like in keyranges.
string pinned =5;
}

// ColumnVindex is used to associate a column to a vindex.
Expand Down
31 changes: 19 additions & 12 deletions py/vtproto/vschema_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.