-
lookup
+ product
diff --git a/examples/demo/index.js b/examples/demo/index.js
index 846d02a67ec..549f15f3c4b 100644
--- a/examples/demo/index.js
+++ b/examples/demo/index.js
@@ -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()
}
diff --git a/examples/demo/schema/customer/customer_schema.sql b/examples/demo/schema/customer/customer_schema.sql
new file mode 100644
index 00000000000..aeabb800b10
--- /dev/null
+++ b/examples/demo/schema/customer/customer_schema.sql
@@ -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));
diff --git a/examples/demo/schema/customer/vschema.json b/examples/demo/schema/customer/vschema.json
new file mode 100644
index 00000000000..9c361475227
--- /dev/null
+++ b/examples/demo/schema/customer/vschema.json
@@ -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"
+ }]
+ }
+ }
+}
diff --git a/examples/demo/schema/lookup/lookup_schema.sql b/examples/demo/schema/lookup/lookup_schema.sql
deleted file mode 100644
index 10ef4d3d16a..00000000000
--- a/examples/demo/schema/lookup/lookup_schema.sql
+++ /dev/null
@@ -1,5 +0,0 @@
-create table user_seq(id int, next_id bigint, cache bigint, primary key(id)) comment 'vitess_sequence';
-insert into user_seq(id, next_id, cache) values(0, 1, 3);
-create table music_seq(id int, next_id bigint, cache bigint, primary key(id)) comment 'vitess_sequence';
-insert into music_seq(id, next_id, cache) values(0, 1, 2);
-create table name_keyspace_idx(name varchar(128), keyspace_id binary(8), primary key(name, keyspace_id));
diff --git a/examples/demo/schema/lookup/vschema.json b/examples/demo/schema/lookup/vschema.json
deleted file mode 100644
index b65f1f337ca..00000000000
--- a/examples/demo/schema/lookup/vschema.json
+++ /dev/null
@@ -1,12 +0,0 @@
-{
- "sharded": false,
- "tables": {
- "user_seq": {
- "type": "sequence"
- },
- "music_seq": {
- "type": "sequence"
- },
- "name_keyspace_idx": {}
- }
-}
diff --git a/examples/demo/schema/product/product_schema.sql b/examples/demo/schema/product/product_schema.sql
new file mode 100644
index 00000000000..766da1d5838
--- /dev/null
+++ b/examples/demo/schema/product/product_schema.sql
@@ -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));
diff --git a/examples/demo/schema/product/vschema.json b/examples/demo/schema/product/vschema.json
new file mode 100644
index 00000000000..712e8c1a277
--- /dev/null
+++ b/examples/demo/schema/product/vschema.json
@@ -0,0 +1,10 @@
+{
+ "sharded": false,
+ "tables": {
+ "product": {},
+ "customer_seq": { "type": "sequence" },
+ "corder_seq": { "type": "sequence" },
+ "corder_event_seq": { "type": "sequence" },
+ "corder_keyspace_idx": {}
+ }
+}
diff --git a/examples/demo/schema/user/user_schema.sql b/examples/demo/schema/user/user_schema.sql
deleted file mode 100644
index 08c79a9f732..00000000000
--- a/examples/demo/schema/user/user_schema.sql
+++ /dev/null
@@ -1,6 +0,0 @@
-create table user(user_id bigint, name varchar(128), primary key(user_id));
-create table user_extra(user_id bigint, extra varchar(128), primary key(user_id));
-create table music(user_id bigint, music_id bigint, primary key(user_id, music_id));
-create table music_extra(music_id bigint, keyspace_id bigint unsigned, primary key(music_id));
-create table name_info(name varchar(128), info varchar(128), primary key(name));
-create table music_keyspace_idx(music_id bigint not null auto_increment, keyspace_id binary(8), primary key(music_id));
diff --git a/examples/demo/schema/user/vschema.json b/examples/demo/schema/user/vschema.json
deleted file mode 100644
index 0cf0f42e6d1..00000000000
--- a/examples/demo/schema/user/vschema.json
+++ /dev/null
@@ -1,102 +0,0 @@
-{
- "sharded": true,
- "vindexes": {
- "hash": {
- "type": "hash"
- },
- "unicode_loose_md5": {
- "type": "unicode_loose_md5"
- },
- "name_keyspace_idx": {
- "type": "lookup",
- "params": {
- "table": "name_keyspace_idx",
- "from": "name",
- "to": "keyspace_id"
- },
- "owner": "user"
- },
- "music_keyspace_idx": {
- "type": "lookup_unique",
- "params": {
- "table": "music_keyspace_idx",
- "from": "music_id",
- "to": "keyspace_id"
- },
- "owner": "music"
- },
- "keyspace_idx": {
- "type": "numeric"
- }
- },
- "tables": {
- "user": {
- "column_vindexes": [
- {
- "column": "user_id",
- "name": "hash"
- },
- {
- "column": "name",
- "name": "name_keyspace_idx"
- }
- ],
- "auto_increment": {
- "column": "user_id",
- "sequence": "user_seq"
- }
- },
- "user_extra": {
- "column_vindexes": [
- {
- "column": "user_id",
- "name": "hash"
- }
- ]
- },
- "music": {
- "column_vindexes": [
- {
- "column": "user_id",
- "name": "hash"
- },
- {
- "column": "music_id",
- "name": "music_keyspace_idx"
- }
- ],
- "auto_increment": {
- "column": "music_id",
- "sequence": "music_seq"
- }
- },
- "music_extra": {
- "column_vindexes": [
- {
- "column": "music_id",
- "name": "music_keyspace_idx"
- },
- {
- "column": "keyspace_id",
- "name": "keyspace_idx"
- }
- ]
- },
- "name_info": {
- "column_vindexes": [
- {
- "column": "name",
- "name": "unicode_loose_md5"
- }
- ]
- },
- "music_keyspace_idx": {
- "column_vindexes": [
- {
- "column": "music_id",
- "name": "hash"
- }
- ]
- }
- }
-}
diff --git a/examples/demo/vschema_ddls.sql b/examples/demo/vschema_ddls.sql
new file mode 100644
index 00000000000..3347fbe2b86
--- /dev/null
+++ b/examples/demo/vschema_ddls.sql
@@ -0,0 +1,29 @@
+-- Unsharded Keyspace
+alter vschema add table product.product;
+
+-- Sharded Keyspace
+alter vschema on customer.customer add vindex hash(customer_id) using hash;
+
+-- Sequences
+alter vschema add sequence product.customer_seq;
+alter vschema on customer.customer add auto_increment customer_id using product.customer_seq;
+
+-- Shared Vindexes and Foreign Keys
+alter vschema on customer.corder add vindex hash(customer_id);
+alter vschema add sequence product.corder_seq;
+alter vschema on customer.corder add auto_increment corder_id using product.corder_seq;
+
+-- Unique Lookup Vindexes
+alter vschema add table product.corder_keyspace_idx;
+alter vschema on customer.corder add vindex corder_keyspace_idx(corder_id) using consistent_lookup_unique with owner=`corder`, table=`product.corder_keyspace_idx`, from=`corder_id`, to=`keyspace_id`;
+
+-- Non-Unique Lookup Vindexes
+alter vschema on customer.oname_keyspace_idx add vindex unicode_loose_md5(oname) using unicode_loose_md5;
+alter vschema on customer.corder add vindex oname_keyspace_idx(oname,corder_id) using consistent_lookup with owner=`corder`, table=`customer.oname_keyspace_idx`, from=`oname,corder_id`, to=`keyspace_id`;
+
+-- Lookup as Primary Vindex
+alter vschema add sequence product.corder_event_seq;
+alter vschema on customer.corder_event add vindex corder_keyspace_idx(corder_id);
+alter vschema on customer.corder_event add auto_increment corder_event_id using product.corder_event_seq;
+-- Reversible Vindexes
+alter vschema on customer.corder_event add vindex `binary`(keyspace_id) using `binary`;