Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
systay committed Sep 20, 2024
1 parent e13a54c commit 1fc0054
Show file tree
Hide file tree
Showing 5 changed files with 2,759 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"encoding/json"
"fmt"
"maps"
"os"

"vitess.io/vitess/go/mysql"
Expand Down Expand Up @@ -110,11 +111,19 @@ func SetupCluster(
}

keyspaces := getKeyspaces(vschemaFile, vtexplainVschemaFile, defaultKeyspaceName, sharded)
if len(keyspaces) == 0 {
panic("no keyspaces found")
}
for _, keyspace := range keyspaces {
ksNames = append(ksNames, keyspace.Name)
vschemaKs, ok := vschema.Keyspaces[keyspace.Name]
if !ok {
panic(fmt.Sprintf("keyspace '%s' not found in vschema", keyspace.Name))
var ks []string
for v := range maps.Keys(vschema.Keyspaces) {
ks = append(ks, v)
}

panic(fmt.Sprintf("keyspace '%s' not found in vschema (%v)", keyspace.Name, ks))
}

if vschemaKs.Keyspace.Sharded {
Expand Down Expand Up @@ -271,16 +280,15 @@ func getSrvVschema(file string, wrap bool) ([]byte, *vschemapb.SrvVSchema, error
return vschemaStr, &srvVSchema, nil
}

func loadVschema(srvVschema *vschemapb.SrvVSchema, rawVschema []byte) (rkv RawKeyspaceVindex, err error) {
func loadVschema(srvVschema *vschemapb.SrvVSchema, rawVschema []byte) (RawKeyspaceVindex, error) {
vschema := *(vindexes.BuildVSchema(srvVschema, sqlparser.NewTestParser()))
if len(vschema.Keyspaces) == 0 {
err = fmt.Errorf("no keyspace defined in vschema")
return
return RawKeyspaceVindex{}, fmt.Errorf("no keyspace defined in vschema")
}

var rk RawKeyspaceVindex
err = json.Unmarshal(rawVschema, &rk)
return
err := json.Unmarshal(rawVschema, &rk)
return rk, err
}

type hashVindex struct {
Expand Down
120 changes: 120 additions & 0 deletions t/optimise.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
CREATE TABLE users
(
user_id INT NOT NULL,
username VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (user_id)
);

CREATE TABLE products
(
product_id INT NOT NULL,
name VARCHAR(100) NOT NULL,
price DECIMAL(10, 2) NOT NULL,
category VARCHAR(50) NOT NULL,
PRIMARY KEY (product_id)
);

CREATE TABLE orders
(
order_id INT NOT NULL,
user_id INT NOT NULL,
order_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
total_amount DECIMAL(10, 2) NOT NULL,
PRIMARY KEY (order_id)
);

CREATE TABLE order_items
(
order_id INT NOT NULL,
product_id INT NOT NULL,
quantity INT NOT NULL,
price DECIMAL(10, 2) NOT NULL,
PRIMARY KEY (order_id, product_id)
);

INSERT INTO users (user_id, username, email)
VALUES (101, 'alice_smith', '[email protected]'),
(102, 'bob_jones', '[email protected]'),
(103, 'charlie_brown', '[email protected]'),
(104, 'diana_prince', '[email protected]'),
(105, 'edward_stark', '[email protected]'),
(106, 'fiona_gale', '[email protected]'),
(107, 'george_white', '[email protected]'),
(108, 'hannah_green', '[email protected]'),
(109, 'ian_blue', '[email protected]'),
(110, 'julia_red', '[email protected]');

INSERT INTO products (product_id, name, price, category)
VALUES (201, 'Laptop', 999.99, 'Electronics'),
(202, 'Smartphone', 699.99, 'Electronics'),
(203, 'Headphones', 149.99, 'Electronics'),
(204, 'Running Shoes', 89.99, 'Sports'),
(205, 'Yoga Mat', 29.99, 'Sports'),
(206, 'Coffee Maker', 79.99, 'Home'),
(207, 'Blender', 59.99, 'Home'),
(208, 'Mystery Novel', 14.99, 'Books'),
(209, 'Cookbook', 24.99, 'Books'),
(210, 'Desk Chair', 199.99, 'Furniture');

INSERT INTO orders (order_id, user_id, order_date, total_amount)
VALUES (301, 101, '2024-09-01 10:00:00', 1149.98),
(302, 102, '2024-09-02 11:30:00', 699.99),
(303, 103, '2024-09-03 14:15:00', 239.97),
(304, 104, '2024-09-04 16:45:00', 224.98),
(305, 105, '2024-09-05 09:20:00', 1079.98),
(306, 101, '2024-09-06 13:10:00', 174.98),
(307, 106, '2024-09-07 17:30:00', 139.98),
(308, 107, '2024-09-08 12:00:00', 999.99),
(309, 108, '2024-09-09 10:45:00', 39.98),
(310, 109, '2024-09-10 15:20:00', 279.98),
(311, 110, '2024-09-11 11:00:00', 149.99),
(312, 101, '2024-09-12 14:30:00', 89.99);

INSERT INTO order_items (order_id, product_id, quantity, price)
VALUES (301, 201, 1, 999.99),
(301, 203, 1, 149.99),
(302, 202, 1, 699.99),
(303, 204, 2, 89.99),
(303, 205, 2, 29.99),
(304, 206, 1, 79.99),
(304, 208, 2, 14.99),
(304, 209, 1, 24.99),
(305, 201, 1, 999.99),
(305, 207, 1, 59.99),
(305, 205, 1, 29.99),
(306, 208, 3, 14.99),
(306, 209, 1, 24.99),
(307, 203, 1, 149.99),
(308, 201, 1, 999.99),
(309, 208, 2, 14.99),
(310, 210, 1, 199.99),
(310, 207, 1, 59.99),
(311, 203, 1, 149.99),
(312, 204, 1, 89.99);

# Query 1: Get all orders for a specific user
SELECT o.order_id, o.order_date, o.total_amount
FROM orders o
WHERE o.user_id = 123;

# Query 2: Get the total amount spent by each user
SELECT u.user_id, u.username, SUM(o.total_amount) as total_spent
FROM users u
JOIN orders o ON u.user_id = o.user_id
GROUP BY u.user_id, u.username;

# Query 3: Get the top 10 selling products
SELECT p.product_id, p.name, SUM(oi.quantity) as total_sold
FROM products p
JOIN order_items oi ON p.product_id = oi.product_id
GROUP BY p.product_id, p.name
ORDER BY total_sold DESC
LIMIT 10;

# Query 4: Get all order items for a specific order
SELECT oi.product_id, p.name, oi.quantity, oi.price
FROM order_items oi
JOIN products p ON oi.product_id = p.product_id
WHERE oi.order_id = 456;
46 changes: 46 additions & 0 deletions t/vschema.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"sharded": true,
"vindexes": {
"hash": {
"type": "hash"
}
},
"tables": {
"users": {
"column_vindexes": [
{
"column": "user_id",
"name": "hash"
}
]
},
"products": {
"column_vindexes": [
{
"column": "product_id",
"name": "hash"
}
]
},
"orders": {
"column_vindexes": [
{
"column": "order_id",
"name": "hash"
}
]
},
"order_items": {
"column_vindexes": [
{
"column": "order_id",
"name": "hash"
},
{
"column": "product_id",
"name": "hash"
}
]
}
}
}
Loading

0 comments on commit 1fc0054

Please sign in to comment.