-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathnoiseless.sql
More file actions
executable file
·120 lines (86 loc) · 4.54 KB
/
Copy pathnoiseless.sql
File metadata and controls
executable file
·120 lines (86 loc) · 4.54 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
LOAD 'pg_diffix';
SET pg_diffix.strict = false;
SET pg_diffix.noise_layer_sd = 0;
SET pg_diffix.low_count_layer_sd = 0;
SET pg_diffix.outlier_count_min = 1;
SET pg_diffix.outlier_count_max = 1;
SET pg_diffix.top_count_min = 3;
SET pg_diffix.top_count_max = 3;
-- Additional tables for SUM testing
CREATE TABLE test_customers_negative AS SELECT id, city, -discount as discount, planet FROM test_customers;
CREATE TABLE test_customers_mixed AS SELECT id, city, discount - 1.0 as discount, planet FROM test_customers;
CALL diffix.mark_personal('public.test_customers_negative', 'id');
CALL diffix.mark_personal('public.test_customers_mixed', 'id');
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;
SELECT SUM(discount), diffix.sum_noise(discount) FROM test_customers_negative;
SELECT SUM(discount), diffix.sum_noise(discount) FROM test_customers_mixed;
-- sum supports numeric type
SELECT city, SUM(discount::numeric), pg_typeof(SUM(discount::numeric)), diffix.sum_noise(discount::numeric)
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;
----------------------------------------------------------------
-- 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';
----------------------------------------------------------------
-- Empty tables
----------------------------------------------------------------
SELECT COUNT(*), COUNT(city), COUNT(DISTINCT city) FROM empty_test_customers;
----------------------------------------------------------------
-- Minimal count
----------------------------------------------------------------
-- Zero for global aggregation
SELECT COUNT(DISTINCT planet) FROM test_customers;
-- `low_count_min_threshold` for queries with GROUP BY
SELECT city, COUNT(DISTINCT city) FROM test_customers GROUP BY 1;
SELECT discount, COUNT(DISTINCT id) FROM test_customers GROUP BY 1;
----------------------------------------------------------------
-- Prepared statements
----------------------------------------------------------------
PREPARE prepared_floor_by(numeric) AS SELECT diffix.floor_by(discount, $1), count(*) FROM test_customers GROUP BY 1;
EXECUTE prepared_floor_by(2.0);