-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnoisy.sql
More file actions
executable file
·121 lines (86 loc) · 4.7 KB
/
Copy pathnoisy.sql
File metadata and controls
executable file
·121 lines (86 loc) · 4.7 KB
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
LOAD 'pg_diffix';
SET pg_diffix.noise_layer_sd = 7;
SET pg_diffix.low_count_layer_sd = 2;
SET pg_diffix.low_count_min_threshold = 2;
SET ROLE diffix_test;
SET pg_diffix.session_access_level = 'anonymized_trusted';
----------------------------------------------------------------
-- Sanity checks
----------------------------------------------------------------
SELECT diffix.access_level();
----------------------------------------------------------------
-- Basic queries
----------------------------------------------------------------
SELECT COUNT(*) FROM test_customers;
SELECT COUNT(*) FROM test_purchases;
SELECT COUNT(city), COUNT(DISTINCT city) FROM test_customers;
SELECT COUNT(DISTINCT cid) FROM test_purchases;
SELECT city, COUNT(DISTINCT id) FROM test_customers GROUP BY 1;
SELECT COUNT(*) FROM test_customers WHERE planet = 'Earth';
----------------------------------------------------------------
-- Basic queries - sum
----------------------------------------------------------------
SELECT SUM(id), diffix.sum_noise(id) FROM test_customers;
SELECT SUM(discount), diffix.sum_noise(discount) FROM test_customers;
SELECT city, SUM(id), diffix.sum_noise(id) FROM test_customers GROUP BY 1;
SELECT city, SUM(discount), diffix.sum_noise(discount) FROM test_customers GROUP BY 1;
----------------------------------------------------------------
-- Basic queries - avg
----------------------------------------------------------------
SELECT city, AVG(discount), diffix.avg_noise(discount) FROM test_customers GROUP BY 1
EXCEPT
SELECT city, SUM(discount) / COUNT(discount), diffix.sum_noise(discount) / COUNT(discount) FROM test_customers GROUP BY 1;
SELECT city, AVG(id), diffix.avg_noise(id) FROM test_customers GROUP BY 1
EXCEPT
SELECT city, SUM(id)::float8 / COUNT(id), diffix.sum_noise(id) / COUNT(id) FROM test_customers GROUP BY 1;
----------------------------------------------------------------
-- Reporting noise
----------------------------------------------------------------
SELECT diffix.count_noise(*), diffix.count_noise(city), diffix.count_noise(DISTINCT city) FROM test_customers;
----------------------------------------------------------------
-- Basic queries - expanding constants in target expressions
----------------------------------------------------------------
SELECT 1 FROM test_patients;
SELECT cast(1 as real) FROM test_patients;
SELECT 1, COUNT(*) FROM test_patients;
SELECT 1, city FROM test_customers;
SELECT city, 'aaaa' FROM test_customers;
----------------------------------------------------------------
-- Multi-AID queries
----------------------------------------------------------------
SELECT city FROM test_patients GROUP BY 1;
SELECT COUNT(*), COUNT(city), COUNT(DISTINCT city) FROM test_patients;
----------------------------------------------------------------
-- LCF & Filtering
----------------------------------------------------------------
SELECT city FROM test_customers;
SELECT city FROM test_customers GROUP BY 1 HAVING length(city) <> 4;
SELECT COUNT(*), COUNT(city), COUNT(DISTINCT city) FROM test_customers WHERE city = 'London';
-- LCF doesn't depend on the bucket seed, both queries should have same noisy threshold.
SELECT diffix.floor_by(age, 30), COUNT(*) FROM test_patients GROUP BY 1;
SELECT diffix.floor_by(age, 30), diffix.floor_by(age, 106), COUNT(*) FROM test_patients GROUP BY 1, 2;
----------------------------------------------------------------
-- Empty tables
----------------------------------------------------------------
SELECT COUNT(*), COUNT(city), COUNT(DISTINCT city) FROM empty_test_customers;
----------------------------------------------------------------
-- WHERE clauses
----------------------------------------------------------------
-- Filtering and grouping produce identical results
(SELECT count(*) FROM test_customers GROUP BY city HAVING city = 'Berlin')
UNION
(SELECT count(*) FROM test_customers WHERE city = 'Berlin');
(SELECT count(*) FROM test_customers GROUP BY city, diffix.round_by(discount, 2)
HAVING city = 'Berlin' AND diffix.round_by(discount, 2) = 0)
UNION
(SELECT count(*) FROM test_customers WHERE city = 'Berlin' AND diffix.round_by(discount, 2) = 0);
(SELECT count(*) FROM test_customers WHERE diffix.round_by(discount, 2) = 0 GROUP BY city HAVING city = 'Berlin')
UNION
(SELECT count(*) FROM test_customers WHERE city = 'Berlin' AND diffix.round_by(discount, 2) = 0);
----------------------------------------------------------------
-- JOIN queries
----------------------------------------------------------------
-- JOIN order doesn't affect results
(SELECT COUNT(*) FROM test_customers AS c JOIN test_purchases ON c.id = cid WHERE city = 'Berlin')
UNION
(SELECT COUNT(*) FROM test_purchases JOIN test_customers AS c ON cid = c.id WHERE city = 'Berlin');