-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathW2_time_travel.sql
97 lines (72 loc) · 2.65 KB
/
W2_time_travel.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
SHOW TABLES;
---> set the data retention time to 90 days
ALTER TABLE TASTY_BYTES.RAW_POS.TEST_MENU SET DATA_RETENTION_TIME_IN_DAYS = 90;
SHOW TABLES;
---> set the data retention time to 1 day
ALTER TABLE TASTY_BYTES.RAW_POS.TEST_MENU SET DATA_RETENTION_TIME_IN_DAYS = 1;
---> clone the truck table
CREATE OR REPLACE TABLE tasty_bytes.raw_pos.truck_dev
CLONE tasty_bytes.raw_pos.truck;
SELECT
t.truck_id,
t.year,
t.make,
t.model
FROM tasty_bytes.raw_pos.truck_dev t;
---> see how the age should have been calculated
SELECT
t.truck_id,
t.year,
t.make,
t.model,
(YEAR(CURRENT_DATE()) - t.year) AS truck_age
FROM tasty_bytes.raw_pos.truck_dev t;
---> record the most recent query_id, back when the data was still correct
SET good_data_query_id = LAST_QUERY_ID();
---> view the variable’s value
SELECT $good_data_query_id;
---> record the time, back when the data was still correct
SET good_data_timestamp = CURRENT_TIMESTAMP;
---> view the variable’s value
SELECT $good_data_timestamp;
---> confirm that that worked
SHOW VARIABLES;
---> make the first mistake: calculating the truck’s age incorrectly
SELECT
t.truck_id,
t.year,
t.make,
t.model,
(YEAR(CURRENT_DATE()) / t.year) AS truck_age
FROM tasty_bytes.raw_pos.truck_dev t;
---> make the second mistake: calculate age wrong, and overwrite the year!
UPDATE tasty_bytes.raw_pos.truck_dev t
SET t.year = (YEAR(CURRENT_DATE()) / t.year);
SELECT
t.truck_id,
t.year,
t.make,
t.model
FROM tasty_bytes.raw_pos.truck_dev t;
---> select the data as of a particular timestamp
SELECT * FROM tasty_bytes.raw_pos.truck_dev
AT(TIMESTAMP => $good_data_timestamp);
SELECT $good_data_timestamp;
---> example code, without a timestamp inserted:
-- SELECT * FROM tasty_bytes.raw_pos.truck_dev
-- AT(TIMESTAMP => '[insert timestamp]'::TIMESTAMP_LTZ);
--->example code, with a timestamp inserted
SELECT * FROM tasty_bytes.raw_pos.truck_dev
AT(TIMESTAMP => '2024-04-04 21:34:31.833 -0700'::TIMESTAMP_LTZ);
---> calculate the right offset
SELECT TIMESTAMPDIFF(second,CURRENT_TIMESTAMP,$good_data_timestamp);
---> Example code, without an offset inserted:
-- SELECT * FROM tasty_bytes.raw_pos.truck_dev
-- AT(OFFSET => -[WRITE OFFSET SECONDS PLUS A BIT]);
---> select the data as of a particular number of seconds back in time
SELECT * FROM tasty_bytes.raw_pos.truck_dev
AT(OFFSET => -45);
SELECT $good_data_query_id;
---> select the data as of its state before a previous query was run
SELECT * FROM tasty_bytes.raw_pos.truck_dev
BEFORE(STATEMENT => $good_data_query_id);