-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspanner_schema.sql
More file actions
73 lines (66 loc) · 2.09 KB
/
Copy pathspanner_schema.sql
File metadata and controls
73 lines (66 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
-- Create Tables
CREATE TABLE products (
id INT64 NOT NULL,
cost FLOAT64,
category STRING(MAX),
name STRING(MAX),
brand STRING(MAX),
retail_price FLOAT64,
department STRING(MAX),
sku STRING(MAX),
distribution_center_id INT64,
embedding ARRAY<FLOAT64>(vector_length=>768),
product_description STRING(MAX),
product_image_uri STRING(MAX),
created_at TIMESTAMP,
name_tokens TOKENLIST AS (TOKENIZE_FULLTEXT(name)) HIDDEN,
category_tokens TOKENLIST AS (TOKENIZE_FULLTEXT(category)) HIDDEN,
brand_tokens TOKENLIST AS (TOKENIZE_FULLTEXT(brand)) HIDDEN,
description_tokens TOKENLIST AS (TOKENIZE_FULLTEXT(product_description)) HIDDEN,
) PRIMARY KEY (id);
CREATE TABLE users (
id INT64 NOT NULL,
first_name STRING(MAX),
last_name STRING(MAX),
email STRING(MAX),
age INT64,
gender STRING(MAX),
state STRING(MAX),
country STRING(MAX),
city STRING(MAX),
) PRIMARY KEY (id);
CREATE TABLE orders (
order_id INT64 NOT NULL,
user_id INT64,
status STRING(MAX),
created_at TIMESTAMP,
) PRIMARY KEY (order_id);
CREATE TABLE order_items (
id INT64 NOT NULL,
order_id INT64,
user_id INT64,
product_id INT64,
status STRING(MAX),
sale_price FLOAT64,
) PRIMARY KEY (id);
CREATE INDEX OrderItemsByProduct ON order_items(product_id) STORING (user_id);
CREATE INDEX OrderItemsByUser ON order_items(user_id) STORING (product_id);
-- Search Index for Full-Text Search
CREATE SEARCH INDEX ProductsFTS ON products(name_tokens, category_tokens, brand_tokens, description_tokens);
-- Vector Index (Approximate Nearest Neighbor)
CREATE VECTOR INDEX ProductsVectorIndex ON products(embedding)
STORING (name, product_description, retail_price, product_image_uri)
WHERE embedding IS NOT NULL
OPTIONS (distance_type='COSINE');
-- Graph Definition for Recommendations
CREATE PROPERTY GRAPH RetailGraph
NODE TABLES (
products,
users
)
EDGE TABLES (
order_items AS purchased
SOURCE KEY (user_id) REFERENCES users (id)
DESTINATION KEY (product_id) REFERENCES products (id)
LABEL purchased
);