-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_data_warehouses_tasty_bytes.sql
414 lines (353 loc) · 12.6 KB
/
create_data_warehouses_tasty_bytes.sql
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
/***************************************************************************************************
_______ _ ____ _
|__ __| | | | _ \ | |
| | __ _ ___ | |_ _ _ | |_) | _ _ | |_ ___ ___
| | / _` |/ __|| __|| | | | | _ < | | | || __|/ _ \/ __|
| || (_| |\__ \| |_ | |_| | | |_) || |_| || |_| __/\__ \
|_| \__,_||___/ \__| \__, | |____/ \__, | \__|\___||___/
__/ | __/ |
|___/ |___/
Quickstart: Tasty Bytes - Zero to Snowflake - Introduction
Version: v2
Author: Jacob Kranzler
Copyright(c): 2024 Snowflake Inc. All rights reserved.
****************************************************************************************************
SUMMARY OF CHANGES
Date(yyyy-mm-dd) Author Comments
------------------- ------------------- ------------------------------------------------------------
2024-05-23 Jacob Kranzler Initial Release
***************************************************************************************************/
USE ROLE sysadmin;
-- assign Query Tag to Session
ALTER SESSION SET query_tag = '{"origin":"sf_sit-is","name":"tb_zts","version":{"major":1, "minor":1},"attributes":{"is_quickstart":1, "source":"sql", "vignette": "intro"}}';
/*--
• database, schema and warehouse creation
--*/
-- create tb_101 database
CREATE OR REPLACE DATABASE tb_101;
-- create raw_pos schema
CREATE OR REPLACE SCHEMA tb_101.raw_pos;
-- create raw_customer schema
CREATE OR REPLACE SCHEMA tb_101.raw_customer;
-- create harmonized schema
CREATE OR REPLACE SCHEMA tb_101.harmonized;
-- create analytics schema
CREATE OR REPLACE SCHEMA tb_101.analytics;
-- create warehouses
CREATE OR REPLACE WAREHOUSE tb_de_wh
WAREHOUSE_SIZE = 'large' -- Large for initial data load - scaled down to XSmall at end of this scripts
WAREHOUSE_TYPE = 'standard'
AUTO_SUSPEND = 60
AUTO_RESUME = TRUE
INITIALLY_SUSPENDED = TRUE
COMMENT = 'data engineering warehouse for tasty bytes';
CREATE OR REPLACE WAREHOUSE tb_dev_wh
WAREHOUSE_SIZE = 'xsmall'
WAREHOUSE_TYPE = 'standard'
AUTO_SUSPEND = 60
AUTO_RESUME = TRUE
INITIALLY_SUSPENDED = TRUE
COMMENT = 'developer warehouse for tasty bytes';
-- create roles
USE ROLE securityadmin;
-- functional roles
CREATE ROLE IF NOT EXISTS tb_admin
COMMENT = 'admin for tasty bytes';
CREATE ROLE IF NOT EXISTS tb_data_engineer
COMMENT = 'data engineer for tasty bytes';
CREATE ROLE IF NOT EXISTS tb_dev
COMMENT = 'developer for tasty bytes';
-- role hierarchy
GRANT ROLE tb_admin TO ROLE sysadmin;
GRANT ROLE tb_data_engineer TO ROLE tb_admin;
GRANT ROLE tb_dev TO ROLE tb_data_engineer;
-- privilege grants
USE ROLE accountadmin;
GRANT IMPORTED PRIVILEGES ON DATABASE snowflake TO ROLE tb_data_engineer;
GRANT CREATE WAREHOUSE ON ACCOUNT TO ROLE tb_admin;
USE ROLE securityadmin;
GRANT USAGE ON DATABASE tb_101 TO ROLE tb_admin;
GRANT USAGE ON DATABASE tb_101 TO ROLE tb_data_engineer;
GRANT USAGE ON DATABASE tb_101 TO ROLE tb_dev;
GRANT USAGE ON ALL SCHEMAS IN DATABASE tb_101 TO ROLE tb_admin;
GRANT USAGE ON ALL SCHEMAS IN DATABASE tb_101 TO ROLE tb_data_engineer;
GRANT USAGE ON ALL SCHEMAS IN DATABASE tb_101 TO ROLE tb_dev;
GRANT ALL ON SCHEMA tb_101.raw_pos TO ROLE tb_admin;
GRANT ALL ON SCHEMA tb_101.raw_pos TO ROLE tb_data_engineer;
GRANT ALL ON SCHEMA tb_101.raw_pos TO ROLE tb_dev;
GRANT ALL ON SCHEMA tb_101.harmonized TO ROLE tb_admin;
GRANT ALL ON SCHEMA tb_101.harmonized TO ROLE tb_data_engineer;
GRANT ALL ON SCHEMA tb_101.harmonized TO ROLE tb_dev;
GRANT ALL ON SCHEMA tb_101.analytics TO ROLE tb_admin;
GRANT ALL ON SCHEMA tb_101.analytics TO ROLE tb_data_engineer;
GRANT ALL ON SCHEMA tb_101.analytics TO ROLE tb_dev;
-- warehouse grants
GRANT OWNERSHIP ON WAREHOUSE tb_de_wh TO ROLE tb_admin COPY CURRENT GRANTS;
GRANT ALL ON WAREHOUSE tb_de_wh TO ROLE tb_admin;
GRANT ALL ON WAREHOUSE tb_de_wh TO ROLE tb_data_engineer;
GRANT ALL ON WAREHOUSE tb_dev_wh TO ROLE tb_admin;
GRANT ALL ON WAREHOUSE tb_dev_wh TO ROLE tb_data_engineer;
GRANT ALL ON WAREHOUSE tb_dev_wh TO ROLE tb_dev;
-- future grants
GRANT ALL ON FUTURE TABLES IN SCHEMA tb_101.raw_pos TO ROLE tb_admin;
GRANT ALL ON FUTURE TABLES IN SCHEMA tb_101.raw_pos TO ROLE tb_data_engineer;
GRANT ALL ON FUTURE TABLES IN SCHEMA tb_101.raw_pos TO ROLE tb_dev;
GRANT ALL ON FUTURE TABLES IN SCHEMA tb_101.raw_customer TO ROLE tb_admin;
GRANT ALL ON FUTURE TABLES IN SCHEMA tb_101.raw_customer TO ROLE tb_data_engineer;
GRANT ALL ON FUTURE TABLES IN SCHEMA tb_101.raw_customer TO ROLE tb_dev;
GRANT ALL ON FUTURE VIEWS IN SCHEMA tb_101.harmonized TO ROLE tb_admin;
GRANT ALL ON FUTURE VIEWS IN SCHEMA tb_101.harmonized TO ROLE tb_data_engineer;
GRANT ALL ON FUTURE VIEWS IN SCHEMA tb_101.harmonized TO ROLE tb_dev;
GRANT ALL ON FUTURE VIEWS IN SCHEMA tb_101.analytics TO ROLE tb_admin;
GRANT ALL ON FUTURE VIEWS IN SCHEMA tb_101.analytics TO ROLE tb_data_engineer;
GRANT ALL ON FUTURE VIEWS IN SCHEMA tb_101.analytics TO ROLE tb_dev;
-- Apply Masking Policy Grants
USE ROLE accountadmin;
GRANT APPLY MASKING POLICY ON ACCOUNT TO ROLE tb_admin;
GRANT APPLY MASKING POLICY ON ACCOUNT TO ROLE tb_data_engineer;
-- raw_pos table build
USE ROLE sysadmin;
USE WAREHOUSE tb_de_wh;
/*--
• file format and stage creation
--*/
CREATE OR REPLACE FILE FORMAT tb_101.public.csv_ff
type = 'csv';
CREATE OR REPLACE STAGE tb_101.public.s3load
COMMENT = 'Quickstarts S3 Stage Connection'
url = 's3://sfquickstarts/frostbyte_tastybytes/'
file_format = tb_101.public.csv_ff;
/*--
raw zone table build
--*/
-- country table build
CREATE OR REPLACE TABLE tb_101.raw_pos.country
(
country_id NUMBER(18,0),
country VARCHAR(16777216),
iso_currency VARCHAR(3),
iso_country VARCHAR(2),
city_id NUMBER(19,0),
city VARCHAR(16777216),
city_population VARCHAR(16777216)
);
-- franchise table build
CREATE OR REPLACE TABLE tb_101.raw_pos.franchise
(
franchise_id NUMBER(38,0),
first_name VARCHAR(16777216),
last_name VARCHAR(16777216),
city VARCHAR(16777216),
country VARCHAR(16777216),
e_mail VARCHAR(16777216),
phone_number VARCHAR(16777216)
);
-- location table build
CREATE OR REPLACE TABLE tb_101.raw_pos.location
(
location_id NUMBER(19,0),
placekey VARCHAR(16777216),
location VARCHAR(16777216),
city VARCHAR(16777216),
region VARCHAR(16777216),
iso_country_code VARCHAR(16777216),
country VARCHAR(16777216)
);
-- menu table build
CREATE OR REPLACE TABLE tb_101.raw_pos.menu
(
menu_id NUMBER(19,0),
menu_type_id NUMBER(38,0),
menu_type VARCHAR(16777216),
truck_brand_name VARCHAR(16777216),
menu_item_id NUMBER(38,0),
menu_item_name VARCHAR(16777216),
item_category VARCHAR(16777216),
item_subcategory VARCHAR(16777216),
cost_of_goods_usd NUMBER(38,4),
sale_price_usd NUMBER(38,4),
menu_item_health_metrics_obj VARIANT
);
-- truck table build
CREATE OR REPLACE TABLE tb_101.raw_pos.truck
(
truck_id NUMBER(38,0),
menu_type_id NUMBER(38,0),
primary_city VARCHAR(16777216),
region VARCHAR(16777216),
iso_region VARCHAR(16777216),
country VARCHAR(16777216),
iso_country_code VARCHAR(16777216),
franchise_flag NUMBER(38,0),
year NUMBER(38,0),
make VARCHAR(16777216),
model VARCHAR(16777216),
ev_flag NUMBER(38,0),
franchise_id NUMBER(38,0),
truck_opening_date DATE
);
-- order_header table build
CREATE OR REPLACE TABLE tb_101.raw_pos.order_header
(
order_id NUMBER(38,0),
truck_id NUMBER(38,0),
location_id FLOAT,
customer_id NUMBER(38,0),
discount_id VARCHAR(16777216),
shift_id NUMBER(38,0),
shift_start_time TIME(9),
shift_end_time TIME(9),
order_channel VARCHAR(16777216),
order_ts TIMESTAMP_NTZ(9),
served_ts VARCHAR(16777216),
order_currency VARCHAR(3),
order_amount NUMBER(38,4),
order_tax_amount VARCHAR(16777216),
order_discount_amount VARCHAR(16777216),
order_total NUMBER(38,4)
);
-- order_detail table build
CREATE OR REPLACE TABLE tb_101.raw_pos.order_detail
(
order_detail_id NUMBER(38,0),
order_id NUMBER(38,0),
menu_item_id NUMBER(38,0),
discount_id VARCHAR(16777216),
line_number NUMBER(38,0),
quantity NUMBER(5,0),
unit_price NUMBER(38,4),
price NUMBER(38,4),
order_item_discount_amount VARCHAR(16777216)
);
-- customer loyalty table build
CREATE OR REPLACE TABLE tb_101.raw_customer.customer_loyalty
(
customer_id NUMBER(38,0),
first_name VARCHAR(16777216),
last_name VARCHAR(16777216),
city VARCHAR(16777216),
country VARCHAR(16777216),
postal_code VARCHAR(16777216),
preferred_language VARCHAR(16777216),
gender VARCHAR(16777216),
favourite_brand VARCHAR(16777216),
marital_status VARCHAR(16777216),
children_count VARCHAR(16777216),
sign_up_date DATE,
birthday_date DATE,
e_mail VARCHAR(16777216),
phone_number VARCHAR(16777216)
);
/*--
• harmonized view creation
--*/
-- orders_v view
CREATE OR REPLACE VIEW tb_101.harmonized.orders_v
AS
SELECT
oh.order_id,
oh.truck_id,
oh.order_ts,
od.order_detail_id,
od.line_number,
m.truck_brand_name,
m.menu_type,
t.primary_city,
t.region,
t.country,
t.franchise_flag,
t.franchise_id,
f.first_name AS franchisee_first_name,
f.last_name AS franchisee_last_name,
l.location_id,
cl.customer_id,
cl.first_name,
cl.last_name,
cl.e_mail,
cl.phone_number,
cl.children_count,
cl.gender,
cl.marital_status,
od.menu_item_id,
m.menu_item_name,
od.quantity,
od.unit_price,
od.price,
oh.order_amount,
oh.order_tax_amount,
oh.order_discount_amount,
oh.order_total
FROM tb_101.raw_pos.order_detail od
JOIN tb_101.raw_pos.order_header oh
ON od.order_id = oh.order_id
JOIN tb_101.raw_pos.truck t
ON oh.truck_id = t.truck_id
JOIN tb_101.raw_pos.menu m
ON od.menu_item_id = m.menu_item_id
JOIN tb_101.raw_pos.franchise f
ON t.franchise_id = f.franchise_id
JOIN tb_101.raw_pos.location l
ON oh.location_id = l.location_id
LEFT JOIN tb_101.raw_customer.customer_loyalty cl
ON oh.customer_id = cl.customer_id;
-- loyalty_metrics_v view
CREATE OR REPLACE VIEW tb_101.harmonized.customer_loyalty_metrics_v
AS
SELECT
cl.customer_id,
cl.city,
cl.country,
cl.first_name,
cl.last_name,
cl.phone_number,
cl.e_mail,
SUM(oh.order_total) AS total_sales,
ARRAY_AGG(DISTINCT oh.location_id) AS visited_location_ids_array
FROM tb_101.raw_customer.customer_loyalty cl
JOIN tb_101.raw_pos.order_header oh
ON cl.customer_id = oh.customer_id
GROUP BY cl.customer_id, cl.city, cl.country, cl.first_name,
cl.last_name, cl.phone_number, cl.e_mail;
/*--
• analytics view creation
--*/
-- orders_v view
CREATE OR REPLACE VIEW tb_101.analytics.orders_v
COMMENT = 'Tasty Bytes Order Detail View'
AS
SELECT DATE(o.order_ts) AS date, * FROM tb_101.harmonized.orders_v o;
-- customer_loyalty_metrics_v view
CREATE OR REPLACE VIEW tb_101.analytics.customer_loyalty_metrics_v
COMMENT = 'Tasty Bytes Customer Loyalty Member Metrics View'
AS
SELECT * FROM tb_101.harmonized.customer_loyalty_metrics_v;
/*--
raw zone table load
--*/
-- country table load
COPY INTO tb_101.raw_pos.country
FROM @tb_101.public.s3load/raw_pos/country/;
-- franchise table load
COPY INTO tb_101.raw_pos.franchise
FROM @tb_101.public.s3load/raw_pos/franchise/;
-- location table load
COPY INTO tb_101.raw_pos.location
FROM @tb_101.public.s3load/raw_pos/location/;
-- menu table load
COPY INTO tb_101.raw_pos.menu
FROM @tb_101.public.s3load/raw_pos/menu/;
-- truck table load
COPY INTO tb_101.raw_pos.truck
FROM @tb_101.public.s3load/raw_pos/truck/;
-- customer_loyalty table load
COPY INTO tb_101.raw_customer.customer_loyalty
FROM @tb_101.public.s3load/raw_customer/customer_loyalty/;
-- order_header table load
COPY INTO tb_101.raw_pos.order_header
FROM @tb_101.public.s3load/raw_pos/order_header/;
-- order_detail table load
COPY INTO tb_101.raw_pos.order_detail
FROM @tb_101.public.s3load/raw_pos/order_detail/;
ALTER WAREHOUSE tb_de_wh SET WAREHOUSE_SIZE = 'XSmall';
-- setup completion note
SELECT 'tb_101 setup is now complete' AS note;