Skip to content

Commit 33b752b

Browse files
committed
refactor: operator class
1 parent ab69b3f commit 33b752b

File tree

8 files changed

+338
-356
lines changed

8 files changed

+338
-356
lines changed

src/operators/operator_class.sql

Lines changed: 6 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -1,148 +1,19 @@
11
-- REQUIRE: src/schema.sql
22
-- REQUIRE: src/encrypted/types.sql
33
-- REQUIRE: src/encrypted/functions.sql
4-
-- REQUIRE: src/ore_block_u64_8_256/types.sql
5-
-- REQUIRE: src/ore_block_u64_8_256/functions.sql
4+
-- REQUIRE: src/encrypted/compare.sql
65
-- REQUIRE: src/operators/<.sql
76
-- REQUIRE: src/operators/<=.sql
87
-- REQUIRE: src/operators/=.sql
98
-- REQUIRE: src/operators/>=.sql
109
-- REQUIRE: src/operators/>.sql
1110

1211

13-
--
14-
-- Compare two eql_v2_encrypted values
15-
-- Uses `ore_block_u64_8_256` or `has_hmac_256` index terms for comparison if defined on ONE of the compared value
16-
--
17-
-- Important note: -- Index order of operations is reversed from equality operator.
18-
-- In equality operations, `has_hmac_256` is preferred as it reduces to a text comparison and is more efficient
19-
-- As compare is used for ordering, `ore_block_u64_8_256` provides more complete ordering and is checked first.
20-
-- THe assumption is that if you add ore you are adding it because you want to use it specifically for comparison.
21-
22-
-- Thusly, the logic for determining which index term to use:
23-
-- Use ORE if BOTH parameters have ore index
24-
-- Fallback to hmac if BOTH parameters have hmac index
25-
-- Fallback to ORE if ONE of the parameters has ore index (will compare against a NULL term for the other parameter)
26-
-- Fallback to hmac if ONE of the parameters has hmac index (will compare against a NULL term term for the other parameter)
27-
--
28-
-- As a general rule, columns should have the same index terms as they are encrypted with the same configuration.
29-
-- Index terms should only be different during an encryption config migration.
30-
-- eg, when adding an ore index to a column any existing values will NOT have the ore index until encryptindexed/migrated
31-
--
32-
CREATE FUNCTION eql_v2.compare(a eql_v2_encrypted, b eql_v2_encrypted)
33-
RETURNS integer
34-
IMMUTABLE STRICT PARALLEL SAFE
35-
AS $$
36-
BEGIN
37-
38-
-- PERFORM eql_v2.log('eql_v2.has_hmac_256(a)', eql_v2.has_hmac_256(a)::text);
39-
-- PERFORM eql_v2.log('eql_v2.has_hmac_256(b)', eql_v2.has_hmac_256(b)::text);
40-
-- PERFORM eql_v2.log('eql_v2.has_ore_block_u64_8_256(b)', eql_v2.has_ore_block_u64_8_256(b)::text);
41-
-- PERFORM eql_v2.log('eql_v2.has_ore_block_u64_8_256(b)', eql_v2.has_ore_block_u64_8_256(b)::text);
42-
43-
44-
-- Use ORE if BOTH parameters have ore index
45-
IF eql_v2.has_ore_block_u64_8_256(a) AND eql_v2.has_ore_block_u64_8_256(b) THEN
46-
RETURN eql_v2.compare_ore_block_u64_8_256(a, b);
47-
END IF;
48-
49-
-- Fallback to hmac if BOTH parameters have hmac index
50-
IF eql_v2.has_hmac_256(a) AND eql_v2.has_hmac_256(b) THEN
51-
RETURN eql_v2.compare_hmac(a, b);
52-
END IF;
53-
54-
-- Fallback to ORE if one of the parameters has ore index
55-
IF eql_v2.has_ore_block_u64_8_256(a) OR eql_v2.has_ore_block_u64_8_256(b) THEN
56-
RETURN eql_v2.compare_ore_block_u64_8_256(a, b);
57-
END IF;
58-
59-
-- Fallback to hmac if ONE of the parameters has hmac index
60-
IF eql_v2.has_hmac_256(a) OR eql_v2.has_hmac_256(b) THEN
61-
RETURN eql_v2.compare_hmac(a, b);
62-
END IF;
63-
64-
RAISE 'Expected an hmac_256 (hm) or ore_block_u64_8_256 (ob) value in json: %', val;
65-
END;
66-
$$ LANGUAGE plpgsql;
67-
6812
--------------------
6913

70-
CREATE FUNCTION eql_v2.compare_ore_block_u64_8_256(a eql_v2_encrypted, b eql_v2_encrypted)
71-
RETURNS integer
72-
IMMUTABLE STRICT PARALLEL SAFE
73-
AS $$
74-
DECLARE
75-
a_ore eql_v2.ore_block_u64_8_256;
76-
b_ore eql_v2.ore_block_u64_8_256;
77-
BEGIN
78-
79-
a_ore := eql_v2.ore_block_u64_8_256(a);
80-
b_ore := eql_v2.ore_block_u64_8_256(b);
81-
82-
IF a_ore IS NULL AND b_ore IS NULL THEN
83-
RETURN 0;
84-
END IF;
85-
86-
IF a_ore IS NULL THEN
87-
RETURN -1;
88-
END IF;
14+
CREATE OPERATOR FAMILY eql_v2.encrypted_operator_family USING btree;
8915

90-
IF b_ore IS NULL THEN
91-
RETURN 1;
92-
END IF;
93-
94-
RETURN eql_v2.compare_ore_array(a_ore.terms, b_ore.terms);
95-
END;
96-
$$ LANGUAGE plpgsql;
97-
98-
99-
--------------------
100-
101-
CREATE FUNCTION eql_v2.compare_hmac(a eql_v2_encrypted, b eql_v2_encrypted)
102-
RETURNS integer
103-
IMMUTABLE STRICT PARALLEL SAFE
104-
AS $$
105-
DECLARE
106-
a_hmac eql_v2.hmac_256;
107-
b_hmac eql_v2.hmac_256;
108-
BEGIN
109-
110-
a_hmac = eql_v2.hmac_256(a);
111-
b_hmac = eql_v2.hmac_256(b);
112-
113-
IF a_hmac IS NULL AND b_hmac IS NULL THEN
114-
RETURN 0;
115-
END IF;
116-
117-
IF a_hmac IS NULL THEN
118-
RETURN -1;
119-
END IF;
120-
121-
IF b_hmac IS NULL THEN
122-
RETURN 1;
123-
END IF;
124-
125-
IF a_hmac = b_hmac THEN
126-
RETURN 0;
127-
END IF;
128-
129-
IF a_hmac < b_hmac THEN
130-
RETURN -1;
131-
END IF;
132-
133-
IF a_hmac > b_hmac THEN
134-
RETURN 1;
135-
END IF;
136-
137-
END;
138-
$$ LANGUAGE plpgsql;
139-
140-
141-
--------------------
142-
143-
CREATE OPERATOR FAMILY eql_v2.encrypted_operator USING btree;
144-
145-
CREATE OPERATOR CLASS eql_v2.encrypted_operator DEFAULT FOR TYPE eql_v2_encrypted USING btree FAMILY eql_v2.encrypted_operator AS
16+
CREATE OPERATOR CLASS eql_v2.encrypted_operator_class DEFAULT FOR TYPE eql_v2_encrypted USING btree FAMILY eql_v2.encrypted_operator_family AS
14617
OPERATOR 1 <,
14718
OPERATOR 2 <=,
14819
OPERATOR 3 =,
@@ -153,17 +24,17 @@ CREATE OPERATOR CLASS eql_v2.encrypted_operator DEFAULT FOR TYPE eql_v2_encrypte
15324

15425
--------------------
15526

156-
-- CREATE OPERATOR FAMILY eql_v2.encrypted_operator_ore_block_u64_8_256 USING btree;
27+
-- CREATE OPERATOR FAMILY eql_v2.encrypted_operator_ordered USING btree;
15728

158-
-- CREATE OPERATOR CLASS eql_v2.encrypted_operator_ore_block_u64_8_256 FOR TYPE eql_v2_encrypted USING btree FAMILY eql_v2.encrypted_operator_ore_block_u64_8_256 AS
29+
-- CREATE OPERATOR CLASS eql_v2.encrypted_operator_ordered FOR TYPE eql_v2_encrypted USING btree FAMILY eql_v2.encrypted_operator_ordered AS
15930
-- OPERATOR 1 <,
16031
-- OPERATOR 2 <=,
16132
-- OPERATOR 3 =,
16233
-- OPERATOR 4 >=,
16334
-- OPERATOR 5 >,
16435
-- FUNCTION 1 eql_v2.compare_ore_block_u64_8_256(a eql_v2_encrypted, b eql_v2_encrypted);
16536

166-
-- --------------------
37+
--------------------
16738

16839
-- CREATE OPERATOR FAMILY eql_v2.encrypted_hmac_256_operator USING btree;
16940

0 commit comments

Comments
 (0)