Skip to content

Commit 1a1aa31

Browse files
authored
Add new tables to calculate the lowest resolution for flows that have a flows relationship (#1183)
* Add new merged table for flows relationships * Add tests for the merged tables in the data preparation * Simplify query to get flows relationship * Improve tests of the data preparation * Add comments from code review * Update SQL to create new merged table * Update tests according to code review
1 parent e00e555 commit 1a1aa31

File tree

3 files changed

+334
-52
lines changed

3 files changed

+334
-52
lines changed

src/data-preparation.jl

+6-52
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ end
374374
create_merged_tables!(connection)
375375
376376
Create the internal tables of merged flows and assets time partitions to be used in the computation of the lowest and highest resolution tables.
377-
The inputs tables are the flows table `flow_time_resolution_rep_period` and the assets table `asset_time_resolution_rep_period`.
377+
The inputs tables are the flows table `flow_time_resolution_rep_period`, the assets table `asset_time_resolution_rep_period` and the `flows_relationships`.
378378
All merged tables have the same columns: `asset`, `year`, `rep_period`, `time_block_start`, and `time_block_end`.
379379
Given a "group" `(asset, year, rep_period)`, the table will have the list of all partitions that should be used to compute the resolution tables.
380380
These are the output tables:
@@ -383,59 +383,13 @@ These are the output tables:
383383
- `merged_assets_and_out_flows`: Union of `merged_out_flows` and `asset_time_resolution_rep_period`.
384384
- `merged_all_flows`: Union (i.e., vertically concatenation) of the tables above.
385385
- `merged_all`: Union of `merged_all_flows` and `asset_time_resolution_rep_period`.
386+
- `merged_flows_relationship`: Set `asset` from `flow_time_resolution_rep_period` depending on `flows_relationships`
386387
This function is intended for internal use.
387388
"""
388389
function create_merged_tables!(connection)
389-
# Incoming flows
390-
DuckDB.execute(
391-
connection,
392-
"CREATE OR REPLACE TEMP TABLE merged_in_flows AS
393-
SELECT DISTINCT to_asset as asset, year, rep_period, time_block_start, time_block_end
394-
FROM flow_time_resolution_rep_period
395-
",
396-
)
397-
398-
# Outgoing flows
399-
DuckDB.execute(
400-
connection,
401-
"CREATE OR REPLACE TEMP TABLE merged_out_flows AS
402-
SELECT DISTINCT from_asset as asset, year, rep_period, time_block_start, time_block_end
403-
FROM flow_time_resolution_rep_period
404-
",
405-
)
406-
407-
# Union of all assets and outgoing flows
408-
DuckDB.execute(
409-
connection,
410-
"CREATE OR REPLACE TEMP TABLE merged_assets_and_out_flows AS
411-
SELECT DISTINCT asset, year, rep_period, time_block_start, time_block_end
412-
FROM asset_time_resolution_rep_period
413-
UNION
414-
FROM merged_out_flows
415-
",
416-
)
417-
418-
# Union of all incoming and outgoing flows
419-
DuckDB.execute(
420-
connection,
421-
"CREATE OR REPLACE TEMP TABLE merged_all_flows AS
422-
FROM merged_in_flows
423-
UNION
424-
FROM merged_out_flows
425-
",
426-
)
427-
428-
# Union of all assets, and incoming and outgoing flows
429-
DuckDB.execute(
430-
connection,
431-
"CREATE OR REPLACE TEMP TABLE merged_all AS
432-
SELECT DISTINCT asset, year, rep_period, time_block_start, time_block_end
433-
FROM asset_time_resolution_rep_period
434-
UNION
435-
FROM merged_all_flows
436-
",
437-
)
438-
return
390+
query_file = joinpath(SQL_FOLDER, "create-merged-tables.sql")
391+
DuckDB.query(connection, read(query_file, String))
392+
return nothing
439393
end
440394

441395
function create_lowest_resolution_table!(connection)
@@ -494,7 +448,7 @@ function create_lowest_resolution_table!(connection)
494448
# Start a new block with s = 12 + 1 = 13 and e = TBE = 12
495449
# - END OF GROUP: Is 1 ≤ s ≤ e? No, so this is not a valid block
496450

497-
for merged_table in ("merged_all_flows", "merged_all")
451+
for merged_table in ("merged_all_flows", "merged_all", "merged_flows_relationship")
498452
table_name = replace(merged_table, "merged" => "t_lowest")
499453
DuckDB.execute(
500454
connection,

src/sql/create-merged-tables.sql

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
-- incoming flows
2+
create or replace temp table merged_in_flows as
3+
select
4+
distinct to_asset as asset,
5+
year,
6+
rep_period,
7+
time_block_start,
8+
time_block_end
9+
from
10+
flow_time_resolution_rep_period
11+
;
12+
13+
-- outgoing flows
14+
create or replace temp table merged_out_flows as
15+
select
16+
distinct from_asset as asset,
17+
year,
18+
rep_period,
19+
time_block_start,
20+
time_block_end
21+
from
22+
flow_time_resolution_rep_period
23+
;
24+
25+
-- union of all assets and outgoing flows
26+
create or replace temp table merged_assets_and_out_flows as
27+
select
28+
distinct asset,
29+
year,
30+
rep_period,
31+
time_block_start,
32+
time_block_end
33+
from
34+
asset_time_resolution_rep_period
35+
union
36+
from
37+
merged_out_flows
38+
;
39+
40+
-- union of all incoming and outgoing flows
41+
create or replace temp table merged_all_flows as
42+
from
43+
merged_in_flows
44+
union
45+
from
46+
merged_out_flows
47+
;
48+
49+
-- union of all assets, and incoming and outgoing flows
50+
create or replace temp table merged_all as
51+
select
52+
distinct asset,
53+
year,
54+
rep_period,
55+
time_block_start,
56+
time_block_end
57+
from
58+
asset_time_resolution_rep_period
59+
union
60+
from
61+
merged_all_flows
62+
;
63+
64+
-- merged table for flows relationships:
65+
-- 1. Define the asset as the combination of the two flows (i.e., CONCAT)
66+
-- 2. Get the resolution of the flows that are in the relationship (i.e., JOIN)
67+
create or replace temp table merged_flows_relationship as
68+
select distinct
69+
CONCAT(fr.flow_1_from_asset, '_',
70+
fr.flow_1_to_asset, '_',
71+
fr.flow_2_from_asset, '_',
72+
fr.flow_2_to_asset) as asset,
73+
ftrrp.year,
74+
ftrrp.rep_period,
75+
ftrrp.time_block_start,
76+
ftrrp.time_block_end,
77+
from
78+
flow_time_resolution_rep_period as ftrrp
79+
join flows_relationships as fr on (
80+
(
81+
ftrrp.from_asset = fr.flow_1_from_asset
82+
and ftrrp.to_asset = fr.flow_1_to_asset
83+
)
84+
or (
85+
ftrrp.from_asset = fr.flow_2_from_asset
86+
and ftrrp.to_asset = fr.flow_2_to_asset
87+
)
88+
)
89+
and ftrrp.year = fr.milestone_year
90+
;

0 commit comments

Comments
 (0)