Skip to content

Commit c6392b6

Browse files
committed
refactor: operators use compare function
1 parent 55007b6 commit c6392b6

File tree

10 files changed

+135
-324
lines changed

10 files changed

+135
-324
lines changed

src/operators/<.sql

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,18 @@
11
-- REQUIRE: src/schema.sql
2-
-- REQUIRE: src/ore_block_u64_8_256/types.sql
3-
-- REQUIRE: src/ore_block_u64_8_256/functions.sql
4-
-- REQUIRE: src/ore_block_u64_8_256/operators.sql
2+
-- REQUIRE: src/encrypted/types.sql
3+
-- REQUIRE: src/operators/compare.sql
54

65

76
-- Operators for < less than comparisons of eql_v2_encrypted types
87
--
9-
-- Support for the following comparisons:
8+
-- Uses `eql_v2.compare` for the actual comparison logic.
109
--
11-
-- eql_v2_encrypted = eql_v2_encrypted
12-
-- eql_v2_encrypted = jsonb
13-
-- jsonb = eql_v2_encrypted
1410
--
15-
-- There are multiple index terms that provide equality comparisons
16-
-- - ore_block_u64_8_256
17-
-- - ore_cllw_8_v2
18-
--
19-
-- We check these index terms in this order and use the first one that exists for both parameters
20-
--
21-
--
22-
23-
2411
CREATE FUNCTION eql_v2.lt(a eql_v2_encrypted, b eql_v2_encrypted)
2512
RETURNS boolean
2613
AS $$
2714
BEGIN
28-
29-
BEGIN
30-
RETURN eql_v2.ore_cllw_u64_8(a) < eql_v2.ore_cllw_u64_8(b);
31-
EXCEPTION WHEN OTHERS THEN
32-
-- PERFORM eql_v2.log('eql_v2.lt no ore_cllw_u64_8 index');
33-
END;
34-
35-
BEGIN
36-
RETURN eql_v2.ore_cllw_var_8(a) < eql_v2.ore_cllw_var_8(b);
37-
EXCEPTION WHEN OTHERS THEN
38-
-- PERFORM eql_v2.log('eql_v2.lt no ore_cllw_var_8 index');
39-
END;
40-
41-
BEGIN
42-
RETURN eql_v2.ore_block_u64_8_256(a) < eql_v2.ore_block_u64_8_256(b);
43-
EXCEPTION WHEN OTHERS THEN
44-
-- PERFORM eql_v2.log('eql_v2.lt no ore_block_u64_8_256 index');
45-
END;
46-
47-
RETURN false;
15+
RETURN eql_v2.compare(a, b) = -1;
4816
END;
4917
$$ LANGUAGE plpgsql;
5018

src/operators/<=.sql

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,18 @@
1+
-- REQUIRE: src/schema.sql
12
-- REQUIRE: src/encrypted/types.sql
2-
-- REQUIRE: src/ore_block_u64_8_256/types.sql
3-
-- REQUIRE: src/ore_block_u64_8_256/functions.sql
4-
-- REQUIRE: src/ore_block_u64_8_256/operators.sql
3+
-- REQUIRE: src/operators/compare.sql
54

65

7-
-- Operators for < less than comparisons of eql_v2_encrypted types
6+
-- Operators for <= less than or equal to comparisons of eql_v2_encrypted types
87
--
9-
-- Support for the following comparisons:
8+
-- Uses `eql_v2.compare` for the actual comparison logic.
109
--
11-
-- eql_v2_encrypted = eql_v2_encrypted
12-
-- eql_v2_encrypted = jsonb
13-
-- jsonb = eql_v2_encrypted
1410
--
15-
-- There are multiple index terms that provide equality comparisons
16-
-- - ore_block_u64_8_256
17-
-- - ore_cllw_8_v2
18-
--
19-
-- We check these index terms in this order and use the first one that exists for both parameters
20-
--
21-
--
22-
23-
2411
CREATE FUNCTION eql_v2.lte(a eql_v2_encrypted, b eql_v2_encrypted)
2512
RETURNS boolean
2613
AS $$
2714
BEGIN
28-
29-
BEGIN
30-
RETURN eql_v2.ore_cllw_u64_8(a) <= eql_v2.ore_cllw_u64_8(b);
31-
EXCEPTION WHEN OTHERS THEN
32-
-- PERFORM eql_v2.log('eql_v2.lte no ore_cllw_u64_8 index');
33-
END;
34-
35-
BEGIN
36-
RETURN eql_v2.ore_cllw_var_8(a) <= eql_v2.ore_cllw_var_8(b);
37-
EXCEPTION WHEN OTHERS THEN
38-
-- PERFORM eql_v2.log('eql_v2.lte no ore_cllw_var_8 index');
39-
END;
40-
41-
BEGIN
42-
RETURN eql_v2.ore_block_u64_8_256(a) <= eql_v2.ore_block_u64_8_256(b);
43-
EXCEPTION WHEN OTHERS THEN
44-
-- PERFORM eql_v2.log('eql_v2.lte no ore_block_u64_8_256 index');
45-
END;
46-
47-
RETURN false;
15+
RETURN eql_v2.compare(a, b) <= 0;
4816
END;
4917
$$ LANGUAGE plpgsql;
5018

src/operators/<=_test.sql

Lines changed: 89 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,84 @@
33
SELECT create_table_with_encrypted();
44
SELECT seed_encrypted_json();
55

6-
SELECT e FROM encrypted WHERE e->'a7cea93975ed8c01f861ccb6bd082784'::text <= '("{""c"": ""mBbM0#UZON2jQ3@LiWcvns2Yf6y3L;hykEh`}*fX#aF;n*=>+*o5Uarod39C7TF-SiCD-NgkG)l%Vw=l!tX>H*P<PfE$+0Szy"", ""s"": ""2517068c0d1f9d4d41d2c666211f785e"", ""ocf"": ""b0c13d4a4a9ffcb2ef853959fb2d26236337244ed86d66470d08963ed703356a1cee600a9a75a70aaefc1b4ca03b7918a7df25b7cd4ca774fd5b8616e6b9adb8""}")'::eql_v2_encrypted;
6+
7+
8+
-- --
9+
-- -- ORE - eql_v2_encrypted <= eql_v2_encrypted
10+
-- --
11+
-- DO $$
12+
-- DECLARE
13+
-- e eql_v2_encrypted;
14+
-- ore_term jsonb;
15+
-- BEGIN
16+
17+
-- -- Record with a Numeric ORE term of 42
18+
-- e := create_encrypted_ore_json(42);
19+
-- PERFORM seed_encrypted(e);
20+
21+
-- PERFORM assert_result(
22+
-- 'eql_v2_encrypted <= eql_v2_encrypted',
23+
-- format('SELECT e FROM encrypted WHERE e <= %L::eql_v2_encrypted', e));
24+
25+
-- PERFORM assert_count(
26+
-- format('eql_v2_encrypted <= eql_v2_encrypted'),
27+
-- format('SELECT e FROM encrypted WHERE e <= %L;', e),
28+
-- 4);
29+
30+
-- e := create_encrypted_ore_json(20);
31+
32+
-- PERFORM assert_result(
33+
-- 'eql_v2_encrypted <= eql_v2_encrypted',
34+
-- format('SELECT e FROM encrypted WHERE e <= %L::eql_v2_encrypted', e));
35+
36+
-- PERFORM assert_count(
37+
-- format('eql_v2_encrypted <= eql_v2_encrypted'),
38+
-- format('SELECT e FROM encrypted WHERE e <= %L;', e),
39+
-- 2);
40+
-- END;
41+
-- $$ LANGUAGE plpgsql;
42+
43+
44+
-- --
45+
-- -- ORE - eql_v2.lte(a eql_v2_encrypted, b eql_v2_encrypted)
46+
-- --
47+
-- DO $$
48+
-- DECLARE
49+
-- e eql_v2_encrypted;
50+
-- ore_term jsonb;
51+
-- BEGIN
52+
-- -- Reset data
53+
-- PERFORM seed_encrypted_json();
54+
55+
-- -- Record with a Numeric ORE term of 42
56+
-- e := create_encrypted_ore_json(42);
57+
-- PERFORM seed_encrypted(e);
58+
59+
-- PERFORM assert_result(
60+
-- 'eql_v2.lte(a eql_v2_encrypted, b eql_v2_encrypted)',
61+
-- format('SELECT e FROM encrypted WHERE eql_v2.lte(e, %L)', e));
62+
63+
-- -- include
64+
-- PERFORM assert_count(
65+
-- 'eql_v2.lte(a eql_v2_encrypted, b eql_v2_encrypted)',
66+
-- format('SELECT e FROM encrypted WHERE eql_v2.lte(e, %L)', e),
67+
-- 4);
68+
69+
-- -- Record with a Numeric ORE term of 30
70+
-- e := create_encrypted_ore_json(30);
71+
72+
-- PERFORM assert_result(
73+
-- 'eql_v2.get(a eql_v2_encrypted, b eql_v2_encrypted)',
74+
-- format('SELECT e FROM encrypted WHERE eql_v2.lte(e, %L)', e));
75+
76+
-- PERFORM assert_count(
77+
-- 'eql_v2.get(a eql_v2_encrypted, b eql_v2_encrypted)',
78+
-- format('SELECT e FROM encrypted WHERE eql_v2.lte(e, %L)', e),
79+
-- 3);
80+
-- END;
81+
-- $$ LANGUAGE plpgsql;
82+
83+
784

885

986
-- ------------------------------------------------------------------------
@@ -31,25 +108,30 @@ DECLARE
31108
-- extract the term at $.n returned as eql_v2_encrypted
32109
term := sv->'2517068c0d1f9d4d41d2c666211f785e'::text;
33110

111+
RAISE NOTICE 'term: %', term;
112+
34113
-- -- -- -- $.n
35114
PERFORM assert_result(
36115
format('eql_v2_encrypted <= eql_v2_encrypted with ore_cllw_u64_8 index term'),
37116
format('SELECT e FROM encrypted WHERE e->''2517068c0d1f9d4d41d2c666211f785e''::text <= %L::eql_v2_encrypted', term));
38117

39118
PERFORM assert_count(
40-
format('eql_v2_encrypted <= eql_v2_encrypted with ore index term'),
119+
format('eql_v2_encrypted <= eql_v2_encrypted with ore_cllw_u64_8 index term'),
41120
format('SELECT e FROM encrypted WHERE e->''2517068c0d1f9d4d41d2c666211f785e''::text <= %L::eql_v2_encrypted', term),
42121
3);
43122

44-
-- -- Check the $.hello path
45-
-- -- Returned encrypted does not have ore_cllw_u64_8
46-
PERFORM assert_no_result(
123+
-- Check the $.hello path
124+
-- Returned encrypted does not have ore_cllw_u64_8
125+
-- Falls back to jsonb literal comparison
126+
PERFORM assert_result(
47127
format('eql_v2_encrypted <= eql_v2_encrypted with ore_cllw_u64_8 index term'),
48128
format('SELECT e FROM encrypted WHERE e->''a7cea93975ed8c01f861ccb6bd082784''::text <= %L::eql_v2_encrypted', term));
49129

50130
END;
51131
$$ LANGUAGE plpgsql;
52132

133+
134+
53135
-- ------------------------------------------------------------------------
54136
-- ------------------------------------------------------------------------
55137
--
@@ -87,7 +169,8 @@ DECLARE
87169

88170
-- -- Check the $.n path
89171
-- -- Returned encrypted does not have ore_cllw_u64_8
90-
PERFORM assert_no_result(
172+
-- Falls back to jsonb literal comparison
173+
PERFORM assert_result(
91174
format('eql_v2_encrypted <= eql_v2_encrypted with ore_cllw_var_8 index term'),
92175
format('SELECT e FROM encrypted WHERE e->''2517068c0d1f9d4d41d2c666211f785e''::text <= %L::eql_v2_encrypted', term));
93176

@@ -96,80 +179,4 @@ $$ LANGUAGE plpgsql;
96179

97180

98181

99-
--
100-
-- ORE - eql_v2_encrypted <= eql_v2_encrypted
101-
--
102-
DO $$
103-
DECLARE
104-
e eql_v2_encrypted;
105-
ore_term jsonb;
106-
BEGIN
107-
108-
-- Record with a Numeric ORE term of 42
109-
e := create_encrypted_ore_json(42);
110-
PERFORM seed_encrypted(e);
111-
112-
PERFORM assert_result(
113-
'eql_v2_encrypted <= eql_v2_encrypted',
114-
format('SELECT e FROM encrypted WHERE e <= %L::eql_v2_encrypted', e));
115-
116-
PERFORM assert_count(
117-
format('eql_v2_encrypted <= eql_v2_encrypted'),
118-
format('SELECT e FROM encrypted WHERE e <= %L;', e),
119-
4);
120-
121-
e := create_encrypted_ore_json(20);
122-
123-
PERFORM assert_result(
124-
'eql_v2_encrypted <= eql_v2_encrypted',
125-
format('SELECT e FROM encrypted WHERE e <= %L::eql_v2_encrypted', e));
126-
127-
PERFORM assert_count(
128-
format('eql_v2_encrypted <= eql_v2_encrypted'),
129-
format('SELECT e FROM encrypted WHERE e <= %L;', e),
130-
2);
131-
END;
132-
$$ LANGUAGE plpgsql;
133-
134-
135-
--
136-
-- ORE - eql_v2.gte(a eql_v2_encrypted, b eql_v2_encrypted)
137-
--
138-
DO $$
139-
DECLARE
140-
e eql_v2_encrypted;
141-
ore_term jsonb;
142-
BEGIN
143-
-- Reset data
144-
PERFORM seed_encrypted_json();
145-
146-
-- Record with a Numeric ORE term of 42
147-
e := create_encrypted_ore_json(42);
148-
PERFORM seed_encrypted(e);
149-
150-
PERFORM assert_result(
151-
'eql_v2.lte(a eql_v2_encrypted, b eql_v2_encrypted)',
152-
format('SELECT e FROM encrypted WHERE eql_v2.lte(e, %L)', e));
153-
154-
-- include
155-
PERFORM assert_count(
156-
'eql_v2.lte(a eql_v2_encrypted, b eql_v2_encrypted)',
157-
format('SELECT e FROM encrypted WHERE eql_v2.lte(e, %L)', e),
158-
4);
159-
160-
-- Record with a Numeric ORE term of 30
161-
e := create_encrypted_ore_json(30);
162-
163-
PERFORM assert_result(
164-
'eql_v2.get(a eql_v2_encrypted, b eql_v2_encrypted)',
165-
format('SELECT e FROM encrypted WHERE eql_v2.lte(e, %L)', e));
166-
167-
PERFORM assert_count(
168-
'eql_v2.get(a eql_v2_encrypted, b eql_v2_encrypted)',
169-
format('SELECT e FROM encrypted WHERE eql_v2.lte(e, %L)', e),
170-
3);
171-
END;
172-
$$ LANGUAGE plpgsql;
173-
174-
175182
SELECT drop_table_with_encrypted();

src/operators/<>.sql

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,19 @@
1+
-- REQUIRE: src/schema.sql
12
-- REQUIRE: src/encrypted/types.sql
2-
-- REQUIRE: src/hmac_256/types.sql
3-
-- REQUIRE: src/hmac_256/functions.sql
4-
-- REQUIRE: src/ore_block_u64_8_256/types.sql
5-
-- REQUIRE: src/ore_block_u64_8_256/functions.sql
6-
-- REQUIRE: src/operators/=.sql
3+
-- REQUIRE: src/operators/compare.sql
74

8-
-- Operators for equality comparisons of eql_v2_encrypted types
9-
--
10-
-- Support for the following comparisons:
11-
--
12-
-- eql_v2_encrypted <> eql_v2_encrypted
13-
-- eql_v2_encrypted <> jsonb
14-
-- jsonb <> eql_v2_encrypted
15-
--
16-
-- There are multiple index terms that provide equality comparisons
17-
-- - hmac_256
18-
-- - ore_block_u64_8_256
19-
-- - ore_cllw_8_v2
5+
6+
-- Operators for <> not equal comparisons of eql_v2_encrypted types
207
--
21-
-- We check these index terms in this order and use the first one that exists for both parameters
8+
-- Uses `eql_v2.compare` for the actual comparison logic.
229
--
2310
--
24-
2511
CREATE FUNCTION eql_v2.neq(a eql_v2_encrypted, b eql_v2_encrypted)
2612
RETURNS boolean
2713
IMMUTABLE STRICT PARALLEL SAFE
2814
AS $$
2915
BEGIN
30-
RETURN NOT eql_v2.eq(a, b );
16+
RETURN eql_v2.compare(a, b) <> 0;
3117
END;
3218
$$ LANGUAGE plpgsql;
3319

src/operators/<_test.sql

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ DECLARE
4141

4242
-- -- Check the $.hello path
4343
-- -- Returned encrypted does not have ore_cllw_u64_8
44-
PERFORM assert_no_result(
44+
-- Falls back to jsonb literal comparison
45+
PERFORM assert_result(
4546
format('eql_v2_encrypted < eql_v2_encrypted with ore index term'),
4647
format('SELECT e FROM encrypted WHERE e->''a7cea93975ed8c01f861ccb6bd082784''::text < %L::eql_v2_encrypted', term));
4748

@@ -85,7 +86,8 @@ DECLARE
8586

8687
-- -- Check the $.n path
8788
-- -- Returned encrypted does not have ore_cllw_var_8
88-
PERFORM assert_no_result(
89+
-- Falls back to jsonb literal comparison
90+
PERFORM assert_result(
8991
format('eql_v2_encrypted < eql_v2_encrypted with ore_cllw_var_8 index term'),
9092
format('SELECT e FROM encrypted WHERE e->''2517068c0d1f9d4d41d2c666211f785e''::text < %L::eql_v2_encrypted', term));
9193

0 commit comments

Comments
 (0)