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
42 changes: 24 additions & 18 deletions examples/demo/demo.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package main
import (
"bufio"
"context"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
Expand Down Expand Up @@ -60,21 +61,23 @@ func runCluster() {
Config: vttest.Config{
Topology: &vttestpb.VTTestTopology{
Keyspaces: []*vttestpb.Keyspace{{
Name: "user",
Name: "customer",
Shards: []*vttestpb.Shard{{
Name: "-80",
}, {
Name: "80-",
}},
}, {
Name: "lookup",
Name: "product",
Shards: []*vttestpb.Shard{{
Name: "0",
}},
}},
},
SchemaDir: path.Join(os.Getenv("VTROOT"), "examples/demo/schema"),
MySQLBindHost: "0.0.0.0",
// VSchemaDDLAuthorizedUsers allows you to experiment with vschema DDLs.
VSchemaDDLAuthorizedUsers: "%",
},
}
env, err := vttest.NewLocalTestEnv("", 12345)
Expand Down Expand Up @@ -140,21 +143,17 @@ func exec(w http.ResponseWriter, req *http.Request) {
}
response["queries"] = queries

execQuery(conn, "user0", "select * from user", "user", "-80", response)
execQuery(conn, "user1", "select * from user", "user", "80-", response)
execQuery(conn, "user_extra0", "select * from user_extra", "user", "-80", response)
execQuery(conn, "user_extra1", "select * from user_extra", "user", "80-", response)
execQuery(conn, "music0", "select * from music", "user", "-80", response)
execQuery(conn, "music1", "select * from music", "user", "80-", response)
execQuery(conn, "music_extra0", "select * from music_extra", "user", "-80", response)
execQuery(conn, "music_extra1", "select * from music_extra", "user", "80-", response)
execQuery(conn, "name_info0", "select * from name_info", "user", "-80", response)
execQuery(conn, "name_info1", "select * from name_info", "user", "80-", response)
execQuery(conn, "music_keyspace_idx0", "select music_id, hex(keyspace_id) from music_keyspace_idx", "user", "-80", response)
execQuery(conn, "music_keyspace_idx1", "select music_id, hex(keyspace_id) from music_keyspace_idx", "user", "80-", response)
execQuery(conn, "user_seq", "select * from user_seq", "lookup", "0", response)
execQuery(conn, "music_seq", "select * from music_seq", "lookup", "0", response)
execQuery(conn, "name_keyspace_idx", "select name, hex(keyspace_id) from name_keyspace_idx", "lookup", "0", response)
execQuery(conn, "customer0", "select * from customer", "customer", "-80", response)
execQuery(conn, "customer1", "select * from customer", "customer", "80-", response)
execQuery(conn, "corder0", "select * from corder", "customer", "-80", response)
execQuery(conn, "corder1", "select * from corder", "customer", "80-", response)
execQuery(conn, "corder_event0", "select * from corder_event", "customer", "-80", response)
execQuery(conn, "corder_event1", "select * from corder_event", "customer", "80-", response)
execQuery(conn, "oname_keyspace_idx0", "select * from oname_keyspace_idx", "customer", "-80", response)
execQuery(conn, "oname_keyspace_idx1", "select * from oname_keyspace_idx", "customer", "80-", response)
execQuery(conn, "product", "select * from product", "product", "0", response)
execQuery(conn, "customer_seq", "select * from customer_seq", "product", "0", response)
execQuery(conn, "corder_keyspace_idx", "select * from corder_keyspace_idx", "product", "0", response)
enc.Encode(response)
}

Expand All @@ -181,6 +180,9 @@ func execQuery(conn *mysql.Conn, key, query, keyspace, shard string, response ma
}
qr, err := conn.ExecuteFetch(query, 10000, true)
if err != nil {
if strings.Contains(err.Error(), "doesn't exist") {
return
}
response[key] = map[string]interface{}{
"title": title,
"error": err.Error(),
Expand All @@ -200,7 +202,11 @@ func resultToMap(title string, qr *sqltypes.Result) map[string]interface{} {
for _, row := range qr.Rows {
srow := make([]string, 0, len(row))
for _, value := range row {
srow = append(srow, value.ToString())
if value.Type() == sqltypes.VarBinary {
srow = append(srow, hex.EncodeToString(value.ToBytes()))
} else {
srow = append(srow, value.ToString())
}
}
rows = append(rows, srow)
}
Expand Down
62 changes: 34 additions & 28 deletions examples/demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,67 +5,73 @@
href="https://stackpath.bootstrapcdn.com/bootswatch/4.3.1/minty/bootstrap.min.css">
<head>
<meta charset="US-ASCII">
<title>Vitess V3 demo</title>
<title>Vitess VSchema demo</title>
</head>
<body data-ng-controller="DemoController">
<div class="container-fluid text-dark">
<div class="row">
<h4 class="col-md-9">Vitess V3 demo</h4>
<h4 class="col-md-9">Vitess VSchema demo</h4>
</div>
<div class="row">
<div class="col-md-10">
<div class="col-md-8">
<div class="card mt-3 mb-3">
<div class="card-heading">
<h4 class="text-center">customer</h4>
</div>
<div class="row">
<div class="col-md-3">
<div data-ng-repeat="curResult in [result.user0]"
data-ng-include="'result.html'"></div>
<div data-ng-repeat="curResult in [result.user_extra0]"
data-ng-include="'result.html'"></div>
<div data-ng-repeat="curResult in [result.music0]"
<div class="col-md-6">
<div data-ng-show="result.customer0" data-ng-repeat="curResult in [result.customer0]"
data-ng-include="'result.html'"></div>
</div>
<div class="col-md-3">
<div data-ng-repeat="curResult in [result.music_extra0]"
data-ng-include="'result.html'"></div>
<div data-ng-repeat="curResult in [result.name_info0]"
data-ng-include="'result.html'"></div>
<div data-ng-repeat="curResult in [result.music_keyspace_idx0]"
<div class="col-md-6">
<div data-ng-show="result.customer1" data-ng-repeat="curResult in [result.customer1]"
data-ng-include="'result.html'"></div>
</div>
<div class="col-md-3">
<div data-ng-repeat="curResult in [result.user1]"
</div>
<div class="row">
<div class="col-md-6">
<div data-ng-show="result.corder0" data-ng-repeat="curResult in [result.corder0]"
data-ng-include="'result.html'"></div>
<div data-ng-repeat="curResult in [result.user_extra1]"
</div>
<div class="col-md-6">
<div data-ng-show="result.corder1" data-ng-repeat="curResult in [result.corder1]"
data-ng-include="'result.html'"></div>
<div data-ng-repeat="curResult in [result.music1]"
</div>
</div>
<div class="row">
<div class="col-md-6">
<div data-ng-show="result.corder_event0" data-ng-repeat="curResult in [result.corder_event0]"
data-ng-include="'result.html'"></div>
</div>
<div class="col-md-3">
<div data-ng-repeat="curResult in [result.music_extra1]"
<div class="col-md-6">
<div data-ng-show="result.corder_event1" data-ng-repeat="curResult in [result.corder_event1]"
data-ng-include="'result.html'"></div>
<div data-ng-repeat="curResult in [result.name_info1]"
</div>
</div>
<div class="row">
<div class="col-md-6">
<div data-ng-show="result.oname_keyspace_idx0" data-ng-repeat="curResult in [result.oname_keyspace_idx0]"
data-ng-include="'result.html'"></div>
<div data-ng-repeat="curResult in [result.music_keyspace_idx1]"
</div>
<div class="col-md-6">
<div data-ng-show="result.oname_keyspace_idx1" data-ng-repeat="curResult in [result.oname_keyspace_idx1]"
data-ng-include="'result.html'"></div>
</div>
</div>
</div>
</div>
<div class="col-md-2">
<div class="col-md-4">
<div class="card mt-3 mb-3">
<div class="card-heading">
<h4 class="text-center">lookup</h4>
<h4 class="text-center">product</h4>
</div>
<div class="row">
<div class="col-md-12">
<div data-ng-repeat="curResult in [result.user_seq]"
<div data-ng-show="result.product" data-ng-repeat="curResult in [result.product]"
data-ng-include="'result.html'"></div>
<div data-ng-repeat="curResult in [result.music_seq]"
<div data-ng-show="result.customer_seq" data-ng-repeat="curResult in [result.customer_seq]"
data-ng-include="'result.html'"></div>
<div data-ng-repeat="curResult in [result.name_keyspace_idx]"
<div data-ng-show="result.corder_keyspace_idx" data-ng-repeat="curResult in [result.corder_keyspace_idx]"
data-ng-include="'result.html'"></div>
</div>
</div>
Expand Down
62 changes: 19 additions & 43 deletions examples/demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,49 +19,25 @@ function DemoController($scope, $http) {

function init() {
$scope.samples = [
"insert into user(name) values('test1') /* run this at least 6 times with different values of name */",
"insert into user(name) values(null) /* error: name must be supplied */",
"select user_id, name from user where user_id=6 /* unique select */",
"select user_id, name from user where name='test1' /* non-unique select */",
"select user_id, name from user where user_id in (1, 6) /* unique multi-select */",
"select user_id, name from user where name in ('test1', 'test2') /* non-unique multi-select */",
"select user_id, name from user /* scatter */",
"select count(*) from user where user_id=1 /* aggregation on unique vindex */",
"select count(*) from user where name='foo' /* error: aggregation on non-unique vindex */",
"select user_id, name from user where user_id=1 limit 1 /* limit on unique vindex */",
"update user set user_id=1 where user_id=2 /* error: cannot change vindex columns */",
"delete from user where user_id=1 /* other 'test1' in name_user_idx unaffected */",
"delete from user where name='test1' /* error: cannot delete by non-unique vindex */",
"",
"insert into user_extra(user_id, extra) values(1, 'extra1')",
"insert into user_extra(user_id, extra) values(2, 'test1')",
"insert into user_extra(user_id, extra) values(6, 'test2')",
"insert into user_extra(extra) values('extra1') /* error: must supply value for user_id */",
"select user_id, extra from user_extra where extra='extra1' /* scatter */",
"update user_extra set extra='extra2' where user_id=1 /* allowed */",
"delete from user_extra where user_id=1 /* vindexes are unchanged */",
"",
"insert into music(user_id) values(1) /* auto-inc on music_id */",
"select user_id, music_id from music where user_id=1",
"delete from music where music_id=6 /* one row deleted */",
"delete from music where user_id=1 /* multiple rows deleted */",
"",
"insert into name_info(name, info) values('test1', 'test info')",
"insert into name_info(name, info) values('test4', 'test info 4')",
"select u.user_id, n.info from user u join name_info n on u.name=n.name where u.user_id=1",
"",
"select u.user_id, u.name, e.user_id, e.extra from user u join user_extra e on u.user_id = e.user_id where u.user_id = 2 /* simple, single row join */",
"select u.user_id, u.name, e.user_id, e.extra from user u join user_extra e on u.user_id = e.user_id /* simple, scatter join */",
"select u.user_id, u.name, e.user_id, e.extra from user u join user_extra e on u.name != e.extra where u.user_id = 2 /* simple, cross-shard complex join */",
"select u1.user_id, u1.name, u2.user_id, u2.name from user u1 join user u2 on u1.name = u2.name where u1.user_id = 2 /* self-join */",
"select u.user_id, u.name, e.user_id, e.extra from user u left join user_extra e on u.user_id = e.user_id /* left join */",
"select u.user_id, u.name, e.user_id, e.extra from user u left join user_extra e on u.name != e.extra where u.user_id = 2 /* cross-shard left join */",
"select user_id, name from user where name in (select extra from user_extra where user_id = user.user_id) /* correlated subquery */",
"select count(*), u.user_id, u.name, e.extra from user u join user_extra e on u.user_id = e.user_id /* aggregates */",
"select u.user_id, u.name, m.music_id, m.user_id from user u join music m on u.user_id = m.music_id where u.user_id = 1 order by u.user_id, u.name, m.user_id /* order by, in spite of odd join */",
"",
"insert into music_extra(music_id) values(1) /* keyspace_id back-computed */",
"insert into music_extra(music_id, keyspace_id) values(1, 1) /* invalid keyspace id */",
"insert into product(pname) values ('monitor'), ('keyboard')",
"select * from product /* pass-through to unsharded keyspace */",
"--",
"insert into customer(uname) values('alice'),('bob'),('charlie'),('dan'),('eve') /* multi-shard insert */",
"select * from customer where customer_id=1 /* use hash vindex */",
"select * from customer where uname='bob' /* scatter */",
"update customer set customer_id=10 where customer_id=1 /* error: cannot change primary vindex column */",
"delete from customer where uname='eve' /* scatter DML */",
"--",
"insert into corder(customer_id, product_id, oname) values (1,1,'gift'),(1,2,'gift'),(2,1,'work'),(3,2,'personal'),(4,1,'personal') /* orders are grouped with their customer, observe lookup table changes */",
"select * from corder where customer_id=1 /* single-shard select */",
"select * from corder where corder_id=1 /* use unique lookup */",
"select * from corder where oname='gift' /* use non-unique lookup, also try with 'work' and 'personal' */",
"select c.uname, o.oname, o.product_id from customer c join corder o on c.customer_id = o.customer_id where c.customer_id=1 /* local join */",
"select c.uname, o.oname, o.product_id from customer c join corder o on c.customer_id = o.customer_id /* scatter local join */",
"select c.uname, o.oname, p.pname from customer c join corder o on c.customer_id = o.customer_id join product p on o.product_id = p.product_id /* cross-shard join */",
"delete from corder where corder_id=3 /* delete also deletes lookup entries */",
"--",
"insert into corder_event(corder_id, ename) values(1, 'paid'), (5, 'delivered') /* automatic population of keyspace_id */",
];
$scope.submitQuery()
}
Expand Down
4 changes: 4 additions & 0 deletions examples/demo/schema/customer/customer_schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
create table customer(customer_id bigint, uname varchar(128), primary key(customer_id));
create table corder(corder_id bigint, customer_id bigint, product_id bigint, oname varchar(128), primary key(corder_id));
create table corder_event(corder_event_id bigint, corder_id bigint, ename varchar(128), keyspace_id varbinary(10), primary key(corder_id, corder_event_id));
create table oname_keyspace_idx(oname varchar(128), corder_id bigint, keyspace_id varbinary(10), primary key(oname, corder_id));
79 changes: 79 additions & 0 deletions examples/demo/schema/customer/vschema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
{
"sharded": true,
"vindexes": {
"hash": {
"type": "hash"
},
"corder_keyspace_idx": {
"type": "consistent_lookup_unique",
"params": {
"table": "product.corder_keyspace_idx",
"from": "corder_id",
"to": "keyspace_id"
},
"owner": "corder"
},
"oname_keyspace_idx": {
"type": "consistent_lookup",
"params": {
"table": "customer.oname_keyspace_idx",
"from": "oname,corder_id",
"to": "keyspace_id"
},
"owner": "corder"
},
"unicode_loose_md5": {
"type": "unicode_loose_md5"
},
"binary": {
"type": "binary"
}
},
"tables": {
"customer": {
"column_vindexes": [{
"column": "customer_id",
"name": "hash"
}],
"auto_increment": {
"column": "customer_id",
"sequence": "product.customer_seq"
}
},
"corder": {
"column_vindexes": [{
"column": "customer_id",
"name": "hash"
}, {
"column": "corder_id",
"name": "corder_keyspace_idx"
}, {
"columns": ["oname", "corder_id"],
"name": "oname_keyspace_idx"
}],
"auto_increment": {
"column": "corder_id",
"sequence": "product.corder_seq"
}
},
"corder_event": {
"column_vindexes": [{
"column": "corder_id",
"name": "corder_keyspace_idx"
}, {
"column": "keyspace_id",
"name": "binary"
}],
"auto_increment": {
"column": "corder_event_id",
"sequence": "product.corder_event_seq"
}
},
"oname_keyspace_idx": {
"column_vindexes": [{
"column": "oname",
"name": "unicode_loose_md5"
}]
}
}
}
5 changes: 0 additions & 5 deletions examples/demo/schema/lookup/lookup_schema.sql

This file was deleted.

12 changes: 0 additions & 12 deletions examples/demo/schema/lookup/vschema.json

This file was deleted.

8 changes: 8 additions & 0 deletions examples/demo/schema/product/product_schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
create table product(product_id bigint auto_increment, pname varchar(128), primary key(product_id));
create table customer_seq(id bigint, next_id bigint, cache bigint, primary key(id)) comment 'vitess_sequence';
insert into customer_seq(id, next_id, cache) values(0, 1, 3);
create table corder_seq(id bigint, next_id bigint, cache bigint, primary key(id)) comment 'vitess_sequence';
insert into corder_seq(id, next_id, cache) values(0, 1, 3);
create table corder_event_seq(id bigint, next_id bigint, cache bigint, primary key(id)) comment 'vitess_sequence';
insert into corder_event_seq(id, next_id, cache) values(0, 1, 3);
create table corder_keyspace_idx(corder_id bigint not null auto_increment, keyspace_id varbinary(10), primary key(corder_id));
10 changes: 10 additions & 0 deletions examples/demo/schema/product/vschema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"sharded": false,
"tables": {
"product": {},
"customer_seq": { "type": "sequence" },
"corder_seq": { "type": "sequence" },
"corder_event_seq": { "type": "sequence" },
"corder_keyspace_idx": {}
}
}
6 changes: 0 additions & 6 deletions examples/demo/schema/user/user_schema.sql

This file was deleted.

Loading