forked from ldbc/ldbc_snb_interactive_v1_impls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
query22.sql
137 lines (137 loc) · 3.69 KB
/
query22.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
/* Q22. International dialog
\set country1 '\'Indonesia\''
\set country2 '\'Brazil\''
*/
WITH person1_list AS (
SELECT p.p_personid AS personid
, ci.pl_placeid AS cityid
FROM place co -- country
, place ci -- city
, person p
WHERE 1=1
-- join
AND co.pl_placeid = ci.pl_containerplaceid
AND ci.pl_placeid = p.p_placeid
-- filter
AND co.pl_name = :country1
)
, person2_list AS (
SELECT p.p_personid AS personid
FROM place co -- country
, place ci -- city
, person p
WHERE 1=1
-- join
AND co.pl_placeid = ci.pl_containerplaceid
AND ci.pl_placeid = p.p_placeid
-- filter
AND co.pl_name = :country2
)
, case1 AS (
SELECT DISTINCT
p1.personid AS person1id
, p2.personid AS person2id
, 4 AS score
FROM person1_list p1
, person2_list p2
, post m -- message by p2
, post r -- reply by p1
WHERE 1=1
-- join
AND m.ps_postid = r.ps_replyof
AND p1.personid = r.ps_creatorid
AND p2.personid = m.ps_creatorid
)
, case2 AS (
SELECT DISTINCT
p1.personid AS person1id
, p2.personid AS person2id
, 1 AS score
FROM person1_list p1
, person2_list p2
, post m -- message by p1
, post r -- reply by p2
WHERE 1=1
-- join
AND m.ps_postid = r.ps_replyof
AND p2.personid = r.ps_creatorid
AND p1.personid = m.ps_creatorid
)
, case3 AS (
SELECT -- no need for distinct
p1.personid AS person1id
, p2.personid AS person2id
, 15 AS score
FROM person1_list p1
, person2_list p2
, knows k
WHERE 1=1
-- join
AND p1.personid = k.k_person1id
AND p2.personid = k.k_person2id
)
, case4 AS (
SELECT DISTINCT
p1.personid AS person1id
, p2.personid AS person2id
, 10 AS score
FROM person1_list p1
, person2_list p2
, post m -- message by p2
, likes l
WHERE 1=1
-- join
AND p2.personid = m.ps_creatorid
AND m.ps_postid = l.l_postid
AND l.l_personid = p1.personid
)
, case5 AS (
SELECT DISTINCT
p1.personid AS person1id
, p2.personid AS person2id
, 1 AS score
FROM person1_list p1
, person2_list p2
, post m -- message by p1
, likes l
WHERE 1=1
-- join
AND p1.personid = m.ps_creatorid
AND m.ps_postid = l.l_postid
AND l.l_personid = p2.personid
)
, pair_scores AS (
SELECT person1id, person2id, sum(score) AS score
FROM (SELECT * FROM case1
UNION ALL SELECT * FROM case2
UNION ALL SELECT * FROM case3
UNION ALL SELECT * FROM case4
UNION ALL SELECT * FROM case5
) t
GROUP BY person1id, person2id
)
, score_ranks AS (
SELECT s.person1id
, s.person2id
, ci.pl_name AS cityName
, s.score
, row_number() OVER (PARTITION BY ci.pl_placeid ORDER BY s.score DESC NULLS LAST, s.person1id, s.person2id) AS rn
FROM place co -- country
INNER JOIN place ci ON (co.pl_placeid = ci.pl_containerplaceid) -- city
LEFT JOIN person1_list p1l ON (ci.pl_placeid = p1l.cityid)
LEFT JOIN pair_scores s ON (p1l.personid = s.person1id)
WHERE 1=1
-- filter
AND co.pl_name = :country1
)
SELECT s.person1id AS "person1.id"
, s.person2id AS "person2.id"
, s.cityName AS "city1.name"
, s.score
FROM score_ranks s
WHERE 1=1
-- filter
AND s.rn = 1
ORDER BY s.score DESC, s.person1id, s.person2id
LIMIT 100
;