-
Notifications
You must be signed in to change notification settings - Fork 18
/
community_core.sql
113 lines (103 loc) · 3.52 KB
/
community_core.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
create table if not exists fds_ds.seller_comm_detect (
vendorid string,
commid int,
degree int,
core boolean
) partitioned by (update_dt string)
row format delimited
fields terminated by ',';
set hive.mapred.mode = 'unstrict';
drop table if exists fds_ds_scratch.seller_comm_detect_inherited;
create table fds_ds_scratch.seller_comm_detect_inherited as
with dt_ranked as (
select
*, dense_rank() over (order by update_dt desc) as rn
from fds_ds.seller_comm_detect
where core = true
),
previous_core as (
select * from dt_ranked where rn = 1
)
select
a.id as vendorid
,a.component_id as component_id
,b.commid
from fds_ds.seller_comm_detect_tmp as a
join previous_core as b
on a.id = b.vendorid;
drop table if exists fds_ds_scratch.seller_comm_detect_merge;
create table fds_ds_scratch.seller_comm_detect_merge as
select
component_id
,min(commid) as commid
from fds_ds_scratch.seller_comm_detect_inherited
group by component_id;
drop table if exists fds_ds_scratch.seller_comm_detect_propagate;
create table fds_ds_scratch.seller_comm_detect_propagate as
select
a.id as vendorid
,a.component_id as component_id
,case when c.comp_size >= 3 then b.commid else null end as commid
from fds_ds.seller_comm_detect_tmp as a
left join fds_ds_scratch.seller_comm_detect_merge as b
on a.component_id = b.component_id
left join (
select component_id, count(*) as comp_size
from fds_ds.seller_comm_detect_tmp
group by component_id
) as c
on a.component_id = c.component_id;
set hive.mapred.mode = 'unstrict';
drop table if exists fds_ds_scratch.seller_comm_detect_max_commid;
create table fds_ds_scratch.seller_comm_detect_max_commid as
select coalesce(max(commid), 0) as max_commid from fds_ds.seller_comm_detect;
drop table if exists fds_ds_scratch.seller_comm_detect_new;
create table fds_ds_scratch.seller_comm_detect_new as
with unassigned as (
select
component_id
,count(*) as nrow
from fds_ds_scratch.seller_comm_detect_propagate
where commid is null
group by component_id
),
filtered as (
select *, row_number() over (order by nrow desc) as tmp
from unassigned
where nrow >= 3
)
select
a.component_id
,a.nrow
,a.tmp + b.max_commid as commid
from filtered as a, fds_ds_scratch.seller_comm_detect_max_commid as b;
set hive.exec.dynamic.partition.mode=nonstrict;
drop table if exists fds_ds_scratch.seller_comm_detect_complete_commid;
create table fds_ds_scratch.seller_comm_detect_complete_commid as
select
a.vendorid
,a.component_id
,coalesce(a.commid, b.commid) as commid
from fds_ds_scratch.seller_comm_detect_propagate as a
left join fds_ds_scratch.seller_comm_detect_new as b
on a.component_id = b.component_id;
with degree_ranked as (
select
a.vendorid, b.degree as degree, a.component_id, a.commid
,row_number() over (partition by a.component_id order by b.degree desc, a.vendorid asc) as rk
from fds_ds_scratch.seller_comm_detect_complete_commid as a
join fds_ds.seller_comm_detect_degree as b
on a.vendorid = b.id
)
insert into table fds_ds.seller_comm_detect partition (update_dt)
select
vendorid, commid, degree
,case when commid is null then null when rk = 1 then True else False end as core
,${update_dt} as update_dt
from degree_ranked;
drop table if exists fds_ds_scratch.seller_comm_detect_inherited;
drop table if exists fds_ds_scratch.seller_comm_detect_merge;
drop table if exists fds_ds_scratch.seller_comm_detect_max_commid;
drop table if exists fds_ds_scratch.seller_comm_detect_propagate;
drop table if exists fds_ds_scratch.seller_comm_detect_new;
drop table if exists fds_ds_scratch.seller_comm_detect_complete_commid;