-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathqueries.sparql
executable file
·391 lines (368 loc) · 11.9 KB
/
queries.sparql
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
# Node types
select ?nodeType (count(*) as ?count) {
?node a ?nodeType
}
group by ?nodeType
:Address 2000
:BankAccount 1999
:Company 1000
:Person 1000
:Holding 163
:Transaction 23259
# Relationship types
select ?relationship (count(*) as ?count) {
?node ?relationship ?node2
}
group by ?relationship
order by ?count
:share 163
:company 163
:holds 163
wgs:lat 997
wgs:long 997
:name 1000
:lastName 1000
:description 1000
:email 1000
:firstName 1000
:zipCode 1144
:hasBankAccount 1213
:number 1999
:balance 1999
:city 2000
:street 2000
:hasAddress 2000
:country 3000
:amount 23259
:beneficiary 23259
:date 23259
:originated 23259
rdf:type 29421
# Find people without accounts
select ?p {
?p a :Person .
filter not exists {?p :hasBankAccount ?o}
}
# Same thing with virtual
select * {
service <virtual://aml> {
:BankAccount-4391-4466-9775-8814 :originated ?t .
?t :beneficiary :BankAccount-8201-8285-4081-3604 ;
:amount ?a ;
:date ?d
}
}
# Find company holdings (no company should have > 100% shares outstanding)
select ?c (sum(?s) as ?total) {
?t :holds ?h .
?h :share ?s ;
:company ?c .
}
group by ?c
order by desc (sum(?s))
# Find holdings for a specific set of companies
select * {
?t :holds ?h .
?h :share ?s ;
:company ?c .
filter (?c in (:Company-355,:Company-52,:Company-707))
}
# Count number of distict entity pairs that have transactions
select (count(*) as ?count) {
select distinct ?bankAccount1 ?bankAccount2 {
?bankAccount1 :originated ?tx .
?tx :beneficiary ?bankAccount2 .
}
}
# Group and count transactions by the types of the parties involved
select ?nodeType1 ?nodeType2 (count(*) as ?count) {
?entity1 a ?nodeType1 ;
:hasBankAccount ?bankAccount1.
?bankAccount1 :originated ?tx .
?tx :beneficiary ?bankAccount2 .
?entity2 a ?nodeType2 ;
:hasBankAccount ?bankAccount2.
}
group by ?nodeType1 ?nodeType2
# Group, count and sum transactions between pairs of people
select ?person1 ?person2 (sum(?amount) as ?total) (count(*) as ?count) {
?person1 a :Person ;
:hasBankAccount ?bankAccount1.
?bankAccount1 :originated ?tx .
?tx :beneficiary ?bankAccount2 ;
:amount ?amount .
?person2 a :Person ;
:hasBankAccount ?bankAccount2.
}
group by ?person1 ?person2
order by desc(?total)
limit 100
# Find money transfers through a (single-hop) affiliated relationship (on each side of transfer), grouped by party pairs
select ?org ?ben (sum(?m) as ?s) {
?org a :Person ;
:hasAffiliation ?t1 .
?t1 :hasBankAccount ?a1 .
?a1 :originated ?tx .
?tx :beneficiary ?a2 ;
:amount ?m .
?t2 :hasBankAccount ?a2 .
?ben a :Person ;
:hasAffiliation ?t2 .
}
group by ?org ?ben
order by desc(?s)
limit 50
# List all direct transfers between two specific people.
select * {
:Person-992 :hasAffiliation ?b .
?b :hasBankAccount ?c .
?c :originated ?d .
?d :beneficiary ?e ;
:amount ?m .
?f :hasBankAccount ?e .
:Person-321 :hasAffiliation ?f .
}
# Find paths between two people (turn reasoning off)
PATHS ALL
START ?s = :Person-451 END ?e = :Person-506
VIA { {?s ?p ?e} UNION {?e ?p ?s} FILTER (?p not in (rdf:type) && !isLiteral(?e)) }
MAX LENGTH 8
# Find all transfers through affiliated relationships (includes cases where ?org = ?ben, excludes direct to direct, includes direct to indirect and inverse)
select ?org ?ben (sum(?m) as ?s) {
?org a :Person ;
:hasAffiliation* ?t1 .
?t1 :hasBankAccount ?a1 .
?a1 :originated ?tx .
?tx :beneficiary ?a2 ;
:amount ?m .
?t2 :hasBankAccount ?a2 ;
:hasAffiliation* ?ben .
?ben a :Person .
# Nothing suspicious about direct transfers
filter (?org != ?t1 || ?ben != ?t2)
}
group by ?org ?ben
order by desc(?s)
limit 100
# Fraud score as Money * (Paths Count)^2
select * {
{
# Count distinct paths
select ?org ?name1 ?ben ?name2 ?s (count(*) as ?c) {
{
# Group by intermediaries
select ?org ?name1 ?ben ?name2 ?t1 ?t2 ?s {
?org :lastName ?lname1 ;
:firstName ?fname1 ;
:hasAffiliation* ?t1 .
?t1 :hasBankAccount ?a1 .
?a1 :originated ?tx .
?tx :beneficiary ?a2 .
?t2 :hasBankAccount ?a2 ;
:hasAffiliation* ?ben .
?ben :lastName ?lname2 ;
:firstName ?fname2 .
{
# Find highest sum of Tx for all paths
# between ?org and ?ben
select ?org ?ben (sum(?m) as ?s) {
?org a :Person ;
:hasAffiliation* ?t1 .
?t1 :hasBankAccount ?a1 .
?a1 :originated ?tx .
?tx :beneficiary ?a2 ;
:amount ?m .
?t2 :hasBankAccount ?a2 ;
:hasAffiliation* ?ben .
?ben a :Person .
}
group by ?org ?ben
order by desc(?s) ?org ?ben
limit 1000
}
bind(concat(?fname1, ' - ', ?lname1) as ?name1)
bind(concat(?fname2, ' - ', ?lname2) as ?name2)
}
group by ?org ?name1 ?ben ?name2 ?t1 ?t2 ?s
}
}
group by ?org ?ben ?name1 ?name2 ?s
order by desc(?s) ?org ?ben
}
bind(?s * ?c * ?c as ?z)
}
order by desc(?z) ?org ?ben
# List transactions through affiliates of a specific pair of actors
select * {
:Person-99 :hasAffiliation* ?t1 .
?t1 :hasBankAccount ?a1 .
?a1 :originated ?tx .
?tx :beneficiary ?a2 ;
:amount ?m .
?t2 :hasBankAccount ?a2 ;
:hasAffiliation* :Person-842 .
}
# Find money transfers through an affiliated relationship using virtual transaction data
select ?org ?ben (sum(?m) as ?s) {
?org a :Person ;
:hasAffiliation ?t1 .
?t1 :hasBankAccount ?a1 .
?t2 :hasBankAccount ?a2 .
?ben a :Person ;
:hasAffiliation ?t2 .
graph <virtual://aml>
{
?a1 :originated ?tx .
?tx :beneficiary ?a2 ;
:amount ?m .
}
}
group by ?org ?ben
order by desc(?s)
limit 50
# Portion of query plan prior query. Necessarily transfers all Tx data to Stardog
Slice(offset=0, limit=50) [#50]
`─ Distinct [#536K]
`─ Projection(?org, ?ben, ?s) [#536K]
`─ OrderBy(DESC(?s), offset=0, limit=50) [#536K]
`─ Group(by=[?org, ?ben] aggregates=[(SUM(?m) AS ?s)]) [#536K]
`─ Distinct [#2.1M]
`─ Projection(?tx, ?m, ?a2, ?a1, ?org, ?t1, ?ben, ?t2) [#2.1M]
`─ HashJoin(?a1) [#2.1M]
+─ ServiceJoin(?a2) [#945]
│ +─ VirtualGraphSql<virtual://aml> [#17084] {
│ │ +─ SELECT `tx_id` AS `F_1`, `account1` AS `F_0`, `account2` AS `F_6`, `amount` AS `F_5`
│ │ +─ FROM `transactions`
│ │ +─ WHERE `amount` IS NOT NULL AND `account2` IS NOT NULL AND `account1` IS NOT NULL
│ │ }
│ `─ Union [#472]
...
# Materialize (store in Stardog natively) rolled-up Tx data from date range
insert {
?a1 :originated [
:beneficiary ?a2 ;
:amount ?msum ;
:date ?date
]
}
where {
select * {
graph <virtual://aml>
{
select ?a1 ?a2 (sum(?m) as ?msum) ('2017-08-31'^^xsd:date as ?date) {
?a1 :originated ?tx .
?tx :beneficiary ?a2 ;
:amount ?m ;
:date ?d .
# Could save roll-up Tx for (say) each quarter, or could merge into single Tx per account pair
filter (?d < '2017-09-01'^^xsd:date)
}
group by ?a1 ?a2
}
}
}
# Find money transfers through an affiliated relationship using hybrid materialized/virtual transaction data
select ?org ?ben (sum(?m) as ?s) {
?org a :Person ;
:hasAffiliation ?t1 .
?t1 :hasBankAccount ?a1 .
?t2 :hasBankAccount ?a2 .
?ben a :Person ;
:hasAffiliation ?t2 .
{
?a1 :originated ?tx .
?tx :beneficiary ?a2 ;
:amount ?m ;
:date ?d
filter (?d < '2017-09-01'^^xsd:date)
}
union
{
graph <virtual://aml> {
?a1 :originated ?tx .
?tx :beneficiary ?a2 ;
:amount ?m ;
:date ?d
filter (?d >= '2017-09-01'^^xsd:date)
}
}
}
group by ?org ?ben
order by desc(?s)
limit 50
# Big Daddy - score fraud indicator as $ x (paths count)^2 - using hybrid materialized/virtual transaction data
select ?org ?name1 ?ben ?name2 ?score {
{
# Count distinct paths
select ?org ?name1 ?ben ?name2 ?s (count(*) as ?c) {
{
# Group by intermediaries
select ?org ?name1 ?ben ?name2 ?t1 ?t2 ?s {
?org :lastName ?lname1 ;
:firstName ?fname1 ;
:hasAffiliation* ?t1 .
?t1 :hasBankAccount ?a1 .
?t2 :hasBankAccount ?a2 ;
:hasAffiliation* ?ben .
?ben :lastName ?lname2 ;
:firstName ?fname2 .
{
?a1 :originated ?tx .
?tx :beneficiary ?a2 ;
:date ?d .
filter (?d < '2017-09-01'^^xsd:date)
}
union
{
graph <virtual://aml> {
?a1 :originated ?tx .
?tx :beneficiary ?a2 ;
:date ?d .
filter (?d >= '2017-09-01'^^xsd:date)
}
}
{
# Find highest sum of Tx for all paths between org and ben
select ?org ?ben (sum(?m) as ?s) {
?org a :Person ;
:hasAffiliation* ?t1 .
?t1 :hasBankAccount ?a1 .
?t2 :hasBankAccount ?a2 ;
:hasAffiliation* ?ben .
?ben a :Person .
{
?a1 :originated ?tx .
?tx :beneficiary ?a2 ;
:amount ?m ;
:date ?d
filter (?d < '2017-09-01'^^xsd:date)
}
union
{
graph <virtual://aml> {
?a1 :originated ?tx .
?tx :beneficiary ?a2 ;
:amount ?m ;
:date ?d
filter (?d >= '2017-09-01'^^xsd:date)
}
}
}
group by ?org ?ben
order by desc(?s) ?org ?ben
limit 1000
}
bind(concat(?fname1, ' - ', ?lname1) as ?name1)
bind(concat(?fname2, ' - ', ?lname2) as ?name2)
}
group by ?org ?name1 ?ben ?name2 ?t1 ?t2 ?s
}
}
group by ?org ?ben ?name1 ?name2 ?s
order by desc(?s) ?org ?ben
}
# Calculate score as total money transacted times
# the square of unique path count between actors
bind(?s * ?c * ?c as ?score)
}
order by desc(?score) ?org ?ben