-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy path03_etl.sql
557 lines (513 loc) · 15.5 KB
/
03_etl.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
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
-- noinspection SqlNoDataSourceInspectionForFile
/* ETL
Prerequisites:
- Transactions and blocks table have been populated with
data using Ethereum ETL
- Star schema has been set up using star_schema.sql
*/
/* EXTRACTION */
/* Extraction Table Transactions */
BEGIN;
CREATE TABLE IF NOT EXISTS e_d_transaction (
hash TEXT,
nonce BIGINT,
transaction_index BIGINT,
from_address TEXT,
to_address TEXT,
value NUMERIC(38),
gas BIGINT,
gas_price BIGINT,
input TEXT,
receipt_cumulative_gas_used BIGINT,
receipt_gas_used BIGINT,
receipt_contract_address TEXT,
receipt_root TEXT,
receipt_status BIGINT,
block_timestamp TIMESTAMP(0),
block_number BIGINT,
block_hash TEXT
);
TRUNCATE TABLE e_d_transaction;
INSERT INTO e_d_transaction (hash,
nonce,
transaction_index,
from_address,
to_address,
value,
gas,
gas_price,
input,
receipt_cumulative_gas_used,
receipt_gas_used,
receipt_contract_address,
receipt_root,
receipt_status,
block_timestamp,
block_number,
block_hash)
SELECT hash,
nonce,
transaction_index,
from_address,
to_address,
value,
gas,
gas_price,
input,
receipt_cumulative_gas_used,
receipt_gas_used,
receipt_contract_address,
receipt_root,
receipt_status,
block_timestamp,
block_number,
block_hash
FROM transactions;
/* Extraction Table Blocks */
CREATE TABLE IF NOT EXISTS e_d_block (
number BIGINT PRIMARY KEY,
hash TEXT,
parent_hash TEXT,
nonce TEXT,
sha3_uncles TEXT,
logs_bloom TEXT,
transactions_root TEXT,
state_root TEXT,
receipts_root TEXT,
miner TEXT,
difficulty NUMERIC(38),
total_difficulty NUMERIC(38),
size BIGINT,
extra_data TEXT,
gas_limit BIGINT,
gas_used BIGINT,
timestamp TIMESTAMP(0),
transaction_count BIGINT
);
TRUNCATE TABLE e_d_block;
INSERT INTO e_d_block (number,
hash,
parent_hash,
nonce,
sha3_uncles,
logs_bloom,
transactions_root,
state_root,
receipts_root,
miner,
difficulty,
total_difficulty,
size,
extra_data,
gas_limit,
gas_used,
timestamp,
transaction_count)
SELECT number,
hash,
parent_hash,
nonce,
sha3_uncles,
logs_bloom,
transactions_root,
state_root,
receipts_root,
miner,
difficulty,
total_difficulty,
size,
extra_data,
gas_limit,
gas_used,
timestamp,
transaction_count
FROM blocks;
/* TRANSFORMATION */
/* Transformation table for transactions */
CREATE TABLE IF NOT EXISTS t_d_transaction (
transaction_id BIGSERIAL NOT NULL
CONSTRAINT pk_t_d_transaction
PRIMARY KEY,
hash TEXT,
nonce BIGINT,
transaction_index BIGINT,
from_address TEXT,
to_address TEXT,
value NUMERIC(38),
gas BIGINT,
gas_price BIGINT,
input TEXT,
method_id TEXT,
method_parameters TEXT,
receipt_cumulative_gas_used BIGINT,
receipt_gas_used BIGINT,
receipt_contract_address TEXT,
receipt_root TEXT,
receipt_status BIGINT,
block_timestamp TIMESTAMP(0),
block_number BIGINT,
block_hash TEXT
);
TRUNCATE TABLE t_d_transaction;
INSERT INTO t_d_transaction (hash,
nonce,
transaction_index,
from_address,
to_address,
value,
gas,
gas_price,
input,
method_id,
method_parameters,
receipt_cumulative_gas_used,
receipt_gas_used,
receipt_contract_address,
receipt_root,
receipt_status,
block_timestamp,
block_number,
block_hash)
SELECT hash,
nonce,
transaction_index,
from_address,
CASE
WHEN to_address IS NULL
THEN '0x0000000000000000000000000000000000000000' --avoid problems in fact table
ELSE to_address
END,
value,
gas,
gas_price,
input,
substring(input, 1, 10),
CASE
WHEN input IS NULL
THEN NULL
ELSE substring(input, 11)
END,
receipt_cumulative_gas_used,
receipt_gas_used,
receipt_contract_address,
receipt_root,
receipt_status,
block_timestamp,
block_number,
block_hash
FROM e_d_transaction;
/* Transformation table Blocks */
CREATE TABLE IF NOT EXISTS t_d_block (
block_id BIGSERIAL NOT NULL
CONSTRAINT "pk_t_d_block"
PRIMARY KEY,
timestamp TIMESTAMP(0),
number BIGINT,
hash TEXT,
parent_hash TEXT,
nonce TEXT,
sha3_uncles TEXT,
logs_bloom TEXT,
transactions_root TEXT,
state_root TEXT,
receipts_root TEXT,
miner TEXT,
difficulty NUMERIC(38),
total_difficulty NUMERIC(38),
size BIGINT,
extra_data TEXT,
gas_limit BIGINT,
gas_used BIGINT,
transaction_count BIGINT
);
TRUNCATE TABLE t_d_block;
INSERT INTO t_d_block (timestamp,
number,
hash,
parent_hash,
nonce,
sha3_uncles,
logs_bloom,
transactions_root,
state_root,
receipts_root,
miner,
difficulty,
total_difficulty,
size,
extra_data,
gas_limit,
gas_used,
transaction_count)
SELECT timestamp,
number,
hash,
parent_hash,
nonce,
sha3_uncles,
logs_bloom,
transactions_root,
state_root,
receipts_root,
miner,
difficulty,
total_difficulty,
size,
extra_data,
gas_limit,
gas_used,
transaction_count
FROM e_d_block;
/* Transformation Table Date */
CREATE TABLE IF NOT EXISTS t_d_date (
date DATE NOT NULL
CONSTRAINT pk_t_d_date
PRIMARY KEY,
year INTEGER,
month INTEGER,
day INTEGER,
weekday INTEGER,
day_in_chars TEXT,
week INTEGER
);
TRUNCATE TABLE t_d_date;
INSERT INTO t_d_date (date,
year,
month,
day,
weekday,
day_in_chars,
week)
-- https://www.postgresql.org/docs/9.1/functions-datetime.html
SELECT distinct block_timestamp::date,
extract(isoyear from block_timestamp), -- use isoyear to avid problems with early jan dates
extract(month from block_timestamp),
extract(day from block_timestamp),
extract(isodow from block_timestamp),
to_char(block_timestamp, 'Day'),
extract(week from block_timestamp)
FROM t_d_transaction;
/* Transformation Table Time */
CREATE TABLE IF NOT EXISTS t_d_time (
time TIME NOT NULL
CONSTRAINT pk_t_d_time
PRIMARY KEY,
hours INTEGER,
minutes INTEGER,
seconds INTEGER
);
TRUNCATE TABLE t_d_time;
INSERT INTO t_d_time(time,
hours,
minutes,
seconds)
SELECT distinct block_timestamp::time,
extract(hour from block_timestamp),
extract(minute from block_timestamp),
extract(second from block_timestamp)
FROM t_d_transaction;
/* Account Transformation Tables */
CREATE TABLE IF NOT EXISTS t_d_account_from (
address TEXT NOT NULL,
eth_out NUMERIC(38)
);
TRUNCATE TABLE t_d_account_from;
INSERT INTO t_d_account_from (address,
eth_out)
SELECT from_address,
eth_out
FROM (SELECT from_address,
(value + receipt_gas_used::NUMERIC(38) * gas_price::NUMERIC(38)) *
-1 as eth_out -- Multiply by -1 so that later it gets subtracted from the eth received
FROM e_d_transaction AS edt) out;
CREATE TABLE IF NOT EXISTS t_d_account_to (
address TEXT NOT NULL,
eth_received NUMERIC(38)
);
TRUNCATE TABLE t_d_account_to;
INSERT INTO t_d_account_to (address,
eth_received)
SELECT to_address,
value
FROM e_d_transaction
WHERE to_address IS NOT NULL;
/*Put from and to together for balances*/
CREATE TABLE IF NOT EXISTS t_d_account (
account_id BIGSERIAL,
address TEXT
CONSTRAINT pk_t_d_account
PRIMARY KEY,
eth_sent NUMERIC(38),
eth_received NUMERIC(38),
account_balance NUMERIC(38)
);
TRUNCATE TABLE t_d_account;
INSERT INTO t_d_account (address,
eth_sent,
eth_received,
account_balance)
SELECT sums.address,
SUM(eth_out),
SUM(eth) + SUM(eth_out),
SUM(eth)
FROM (SELECT tdaf.address AS address,
tdaf.eth_out AS eth,
(tdaf.eth_out * -1) AS eth_out -- Reconvert to positive amount
FROM t_d_account_from AS tdaf
UNION ALL
SELECT tdat.address as address,
tdat.eth_received AS eth,
tdat.eth_received AS eth_in -- Just here to match rows for union all
FROM t_d_account_to AS tdat) sums
GROUP BY address;
/* LOADING */
/* Loading transaction dimension */
INSERT INTO d_transaction(transaction_id,
hash,
nonce,
transaction_index,
from_address,
to_address,
value,
gas,
gas_price,
input,
method_id,
method_parameters,
receipt_cumulative_gas_used,
receipt_gas_used,
receipt_contract_address,
receipt_root,
receipt_status,
block_timestamp,
block_number,
block_hash)
SELECT transaction_id,
hash,
nonce,
transaction_index,
from_address,
to_address,
value,
gas,
gas_price,
input,
method_id,
method_parameters,
receipt_cumulative_gas_used,
receipt_gas_used,
receipt_contract_address,
receipt_root,
receipt_status,
block_timestamp,
block_number,
block_hash
FROM t_d_transaction;
/* Loading block dimension */
INSERT INTO d_block (block_id,
timestamp,
number,
hash,
parent_hash,
nonce,
sha3_uncles,
logs_bloom,
transactions_root,
state_root,
receipts_root,
miner,
difficulty,
total_difficulty,
size,
extra_data,
gas_limit,
gas_used,
transaction_count)
SELECT block_id,
timestamp,
number,
hash,
parent_hash,
nonce,
sha3_uncles,
logs_bloom,
transactions_root,
state_root,
receipts_root,
miner,
difficulty,
total_difficulty,
size,
extra_data,
gas_limit,
gas_used,
transaction_count
FROM t_d_block;
/* Loading date dimension */
INSERT INTO d_date (date,
year,
month,
day,
weekday,
day_in_chars,
week)
SELECT date,
year,
month,
day,
weekday,
day_in_chars,
week
FROM t_d_date
ON CONFLICT DO NOTHING;
-- Avoid conflicts with duplicate dates
/* Loading time dimension */
INSERT INTO d_time (time,
hours,
minutes,
seconds)
SELECT time,
hours,
minutes,
seconds
FROM t_d_time
ON CONFLICT DO NOTHING;
-- Avoid conflicts with duplicate timestamps
/* Loading account dimension */
INSERT INTO d_account (address,
eth_sent,
eth_received,
account_balance)
SELECT address,
eth_sent,
eth_received,
account_balance
FROM t_d_account
-- On conflict add the changes to the account balances
ON CONFLICT (address) DO UPDATE
SET eth_sent = d_account.eth_sent + EXCLUDED.eth_sent,
eth_received = d_account.eth_received + EXCLUDED.eth_received,
account_balance = d_account.account_balance + EXCLUDED.account_balance;
/* Loading blockchain fact table */
INSERT INTO f_blockchain (block_id,
transaction_id,
account_from_address,
account_to_address,
date,
time)
SELECT tdb.block_id,
tdt.transaction_id,
tdt.from_address,
tdt.to_address,
tdd.date,
tdti.time
FROM t_d_transaction AS tdt,
t_d_block AS tdb,
t_d_account AS tda,
t_d_date AS tdd,
t_d_time AS tdti
WHERE tdt.block_timestamp::date = tdd.date
AND tdt.block_timestamp::time = tdti.time
AND tdt.block_hash = tdb.hash
AND tdt.from_address = tda.address;
COMMIT;