Skip to content

Commit 4450097

Browse files
authored
Merge pull request #442 from diffix/piotr/elm-to-master
Merge Elm into master
2 parents 30f082d + 93ad831 commit 4450097

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed

src/query/anonymization.c

+11
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@
88
#include "nodes/nodeFuncs.h"
99
#include "optimizer/optimizer.h"
1010
#include "optimizer/tlist.h"
11+
#include "parser/parse_coerce.h"
1112
#include "parser/parse_oper.h"
1213
#include "parser/parsetree.h"
14+
#include "utils/datetime.h"
1315
#include "utils/fmgroids.h"
1416
#include "utils/fmgrprotos.h"
17+
#include "utils/json.h"
1518
#include "utils/lsyscache.h"
1619

1720
#include "pg_diffix/aggregation/bucket_scan.h"
@@ -626,6 +629,14 @@ static hash_t hash_label(Oid type, Datum value, bool is_null)
626629
return hash_string(value_as_string);
627630
}
628631

632+
if (TypeCategory(type) == TYPCATEGORY_DATETIME)
633+
{
634+
char value_as_string[MAXDATELEN + 1];
635+
/* Leveraging `json.h` as a way to get style-stable encoding of various datetime types. */
636+
JsonEncodeDateTime(value_as_string, value, type, NULL);
637+
return hash_string(value_as_string);
638+
}
639+
629640
/* Handle all other types by casting to text. */
630641
Oid type_output_funcid = InvalidOid;
631642
bool is_varlena = false;

test/expected/datetime.out

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
LOAD 'pg_diffix';
2+
CREATE TABLE test_datetime (
3+
id INTEGER,
4+
d DATE,
5+
t TIME,
6+
ts TIMESTAMP WITHOUT TIME ZONE,
7+
tz TIMESTAMP WITH TIME ZONE,
8+
i INTERVAL
9+
);
10+
INSERT INTO test_datetime VALUES
11+
(1, '2012-05-14', '14:59', '2012-05-14', '2012-05-14', '1 years'),
12+
(2, '2012-05-14', '14:59', '2012-05-14', '2012-05-14', '1 years'),
13+
(3, '2012-05-14', '14:59', '2012-05-14', '2012-05-14', '1 years'),
14+
(4, '2012-05-14', '14:59', '2012-05-14', '2012-05-14', '1 years'),
15+
(5, '2012-05-14', '14:59', '2012-05-14', '2012-05-14', '1 years'),
16+
(6, '2012-05-14', '14:59', '2012-05-14', '2012-05-14', '1 years'),
17+
(7, '2012-05-14', '14:59', '2012-05-14', '2012-05-14', '1 years');
18+
CALL diffix.mark_personal('test_datetime', 'id');
19+
SET ROLE diffix_test;
20+
SET pg_diffix.session_access_level = 'anonymized_trusted';
21+
----------------------------------------------------------------
22+
-- Sanity checks
23+
----------------------------------------------------------------
24+
SELECT diffix.access_level();
25+
access_level
26+
--------------------
27+
anonymized_trusted
28+
(1 row)
29+
30+
----------------------------------------------------------------
31+
-- Seeding
32+
----------------------------------------------------------------
33+
-- Datetime values are seeded the same regardless of global `datestyle` setting
34+
SET datestyle = 'SQL';
35+
SELECT ts, count(*) FROM test_datetime GROUP BY 1;
36+
ts | count
37+
---------------------+-------
38+
05/14/2012 00:00:00 | 9
39+
(1 row)
40+
41+
SET datestyle = 'ISO';
42+
SELECT ts, count(*) FROM test_datetime GROUP BY 1;
43+
ts | count
44+
---------------------+-------
45+
2012-05-14 00:00:00 | 9
46+
(1 row)
47+

test/sql/datetime.sql

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
LOAD 'pg_diffix';
2+
3+
CREATE TABLE test_datetime (
4+
id INTEGER,
5+
d DATE,
6+
t TIME,
7+
ts TIMESTAMP WITHOUT TIME ZONE,
8+
tz TIMESTAMP WITH TIME ZONE,
9+
i INTERVAL
10+
);
11+
12+
INSERT INTO test_datetime VALUES
13+
(1, '2012-05-14', '14:59', '2012-05-14', '2012-05-14', '1 years'),
14+
(2, '2012-05-14', '14:59', '2012-05-14', '2012-05-14', '1 years'),
15+
(3, '2012-05-14', '14:59', '2012-05-14', '2012-05-14', '1 years'),
16+
(4, '2012-05-14', '14:59', '2012-05-14', '2012-05-14', '1 years'),
17+
(5, '2012-05-14', '14:59', '2012-05-14', '2012-05-14', '1 years'),
18+
(6, '2012-05-14', '14:59', '2012-05-14', '2012-05-14', '1 years'),
19+
(7, '2012-05-14', '14:59', '2012-05-14', '2012-05-14', '1 years');
20+
21+
CALL diffix.mark_personal('test_datetime', 'id');
22+
23+
SET ROLE diffix_test;
24+
SET pg_diffix.session_access_level = 'anonymized_trusted';
25+
26+
----------------------------------------------------------------
27+
-- Sanity checks
28+
----------------------------------------------------------------
29+
30+
SELECT diffix.access_level();
31+
32+
----------------------------------------------------------------
33+
-- Seeding
34+
----------------------------------------------------------------
35+
36+
-- Datetime values are seeded the same regardless of global `datestyle` setting
37+
SET datestyle = 'SQL';
38+
SELECT ts, count(*) FROM test_datetime GROUP BY 1;
39+
SET datestyle = 'ISO';
40+
SELECT ts, count(*) FROM test_datetime GROUP BY 1;

0 commit comments

Comments
 (0)