Skip to content

Commit 2905a33

Browse files
authored
Merge pull request #115 from cipherstash/encrypted-stabby-selector-param
-> and ->> variants with eql_v2_encrypted selector
2 parents 6699a9a + 6d1297f commit 2905a33

18 files changed

+201
-119
lines changed

src/encrypted/aggregates.sql

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ CREATE FUNCTION eql_v2.min(a eql_v2_encrypted, b eql_v2_encrypted)
99
STRICT
1010
AS $$
1111
BEGIN
12-
PERFORM eql_v2.log('eql_v2.min');
1312
IF eql_v2.ore_block_u64_8_256(a) < eql_v2.ore_block_u64_8_256(b) THEN
1413
RETURN a;
1514
ELSE
@@ -31,7 +30,6 @@ RETURNS eql_v2_encrypted
3130
STRICT
3231
AS $$
3332
BEGIN
34-
PERFORM eql_v2.log('eql_v2.max');
3533
IF eql_v2.ore_block_u64_8_256(a) > eql_v2.ore_block_u64_8_256(b) THEN
3634
RETURN a;
3735
ELSE

src/operators/->.sql

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,21 @@
55

66

77
--
8-
-- The -> operator returns an encrypted matching the selector
8+
-- The -> operator returns an encrypted matching the provided selector
9+
--
910
-- Encyprted JSON is represented as an array of `eql_v2_encrypted`.
10-
-- Each `eql_v2_encrypted` value has a selector, ciphertext, and an index term of
11-
-- - blake3
12-
-- - ore_cllw_u64_8
13-
-- - ore_cllw_var_8
11+
-- Each `eql_v2_encrypted` value has a selector, ciphertext, and an index term
1412
--
1513
-- {
1614
-- "sv": [ {"c": "", "s": "", "b3": "" } ]
1715
-- }
1816
--
19-
20-
17+
-- Note on oeprator resolution:
18+
-- Assignment casts are considered for operator resolution (see PostgreSQL docs),
19+
-- the system may pick the "more specific" one, which is the one with both arguments of the same type.
20+
--
21+
-- This means that to use the text operator, the parameter will need to be cast to text
22+
--
2123
CREATE FUNCTION eql_v2."->"(e eql_v2_encrypted, selector text)
2224
RETURNS eql_v2_encrypted
2325
IMMUTABLE STRICT PARALLEL SAFE
@@ -43,10 +45,34 @@ AS $$
4345
END;
4446
$$ LANGUAGE plpgsql;
4547

48+
CREATE OPERATOR ->(
49+
FUNCTION=eql_v2."->",
50+
LEFTARG=eql_v2_encrypted,
51+
RIGHTARG=text
52+
);
53+
54+
---------------------------------------------------
4655

47-
--
56+
57+
CREATE FUNCTION eql_v2."->"(e eql_v2_encrypted, selector eql_v2_encrypted)
58+
RETURNS eql_v2_encrypted
59+
IMMUTABLE STRICT PARALLEL SAFE
60+
AS $$
61+
BEGIN
62+
RETURN eql_v2."->"(e, eql_v2.selector(selector));
63+
END;
64+
$$ LANGUAGE plpgsql;
65+
66+
67+
68+
CREATE OPERATOR ->(
69+
FUNCTION=eql_v2."->",
70+
LEFTARG=eql_v2_encrypted,
71+
RIGHTARG=eql_v2_encrypted
72+
);
4873

4974

75+
---------------------------------------------------
5076

5177

5278
CREATE FUNCTION eql_v2."->"(e eql_v2_encrypted, selector integer)
@@ -76,11 +102,7 @@ AS $$
76102
$$ LANGUAGE plpgsql;
77103

78104

79-
CREATE OPERATOR ->(
80-
FUNCTION=eql_v2."->",
81-
LEFTARG=eql_v2_encrypted,
82-
RIGHTARG=text
83-
);
105+
84106

85107

86108
CREATE OPERATOR ->(

src/operators/->>.sql

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,7 @@ AS $$
1111
DECLARE
1212
found eql_v2_encrypted;
1313
BEGIN
14-
1514
found = eql_v2."->"(e, selector);
16-
1715
RETURN eql_v2.ciphertext(found);
1816
END;
1917
$$ LANGUAGE plpgsql;
@@ -26,3 +24,22 @@ CREATE OPERATOR ->> (
2624
);
2725

2826

27+
28+
---------------------------------------------------
29+
30+
31+
CREATE FUNCTION eql_v2."->>"(e eql_v2_encrypted, selector eql_v2_encrypted)
32+
RETURNS text
33+
IMMUTABLE STRICT PARALLEL SAFE
34+
AS $$
35+
BEGIN
36+
RETURN eql_v2."->>"(e, eql_v2.selector(selector));
37+
END;
38+
$$ LANGUAGE plpgsql;
39+
40+
41+
CREATE OPERATOR ->> (
42+
FUNCTION=eql_v2."->>",
43+
LEFTARG=eql_v2_encrypted,
44+
RIGHTARG=eql_v2_encrypted
45+
);

src/operators/->>_test.sql

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@
33
SELECT create_table_with_encrypted();
44
SELECT seed_encrypted_json();
55

6+
67
--
78
-- The ->> operator returns ciphertext matching the selector
89
DO $$
910
BEGIN
1011
PERFORM assert_result(
1112
'Selector ->> returns at least one eql_v2_encrypted',
12-
'SELECT e->>''bca213de9ccce676fa849ff9c4807963'' FROM encrypted;');
13+
'SELECT e->>''bca213de9ccce676fa849ff9c4807963''::text FROM encrypted;');
1314

1415
PERFORM assert_count(
1516
'Selector ->> returns all eql_v2_encrypted',
16-
'SELECT e->>''bca213de9ccce676fa849ff9c4807963'' FROM encrypted;',
17+
'SELECT e->>''bca213de9ccce676fa849ff9c4807963''::text FROM encrypted;',
1718
3);
1819
END;
1920
$$ LANGUAGE plpgsql;
@@ -25,7 +26,7 @@ DO $$
2526
BEGIN
2627
PERFORM assert_no_result(
2728
'Unknown selector -> returns null',
28-
'SELECT e->>''blahvtha'' FROM encrypted;');
29+
'SELECT e->>''blahvtha''::text FROM encrypted;');
2930

3031
END;
3132
$$ LANGUAGE plpgsql;
@@ -38,8 +39,29 @@ DO $$
3839

3940
PERFORM assert_result(
4041
'Selector ->> returns all eql_v2_encrypted',
41-
'SELECT e->>''bca213de9ccce676fa849ff9c4807963'' FROM encrypted LIMIT 1;',
42+
'SELECT e->>''bca213de9ccce676fa849ff9c4807963''::text FROM encrypted LIMIT 1;',
4243
'mBbLGB9xHAGzLvUj-`@Wmf=IhD87n7r3ir3n!Sk6AKir_YawR=0c>pk(OydB;ntIEXK~c>V&4>)rNkf<JN7fmlO)c^iBv;-X0+3XyK5d`&&I-oeIEOcwPf<3zy');
4344
END;
4445
$$ LANGUAGE plpgsql;
4546

47+
48+
--
49+
-- The ->> operator accepts an eql_v2_encrypted as the selector
50+
--
51+
DO $$
52+
DECLARE
53+
term text;
54+
BEGIN
55+
term := '{"s": "bca213de9ccce676fa849ff9c4807963"}';
56+
57+
PERFORM assert_result(
58+
'Selector ->> returns at least one eql_v2_encrypted',
59+
format('SELECT e->>%L::jsonb::eql_v2_encrypted FROM encrypted;', term));
60+
61+
PERFORM assert_count(
62+
'Selector ->> returns all eql_v2_encrypted',
63+
format('SELECT e->>%L::jsonb::eql_v2_encrypted FROM encrypted;', term),
64+
3);
65+
END;
66+
$$ LANGUAGE plpgsql;
67+

src/operators/->_test.sql

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,18 @@ SELECT create_table_with_encrypted();
55
SELECT seed_encrypted_json();
66

77

8+
89
--
910
-- The -> operator returns an encrypted matching the selector
1011
DO $$
1112
BEGIN
1213
PERFORM assert_result(
1314
'Selector -> returns at least one eql_v2_encrypted',
14-
'SELECT e->''bca213de9ccce676fa849ff9c4807963'' FROM encrypted;');
15+
'SELECT e->''bca213de9ccce676fa849ff9c4807963''::text FROM encrypted;');
1516

1617
PERFORM assert_count(
1718
'Selector -> returns all eql_v2_encrypted',
18-
'SELECT e->''bca213de9ccce676fa849ff9c4807963'' FROM encrypted;',
19+
'SELECT e->''bca213de9ccce676fa849ff9c4807963''::text FROM encrypted;',
1920
3);
2021
END;
2122
$$ LANGUAGE plpgsql;
@@ -27,13 +28,34 @@ DO $$
2728
BEGIN
2829
PERFORM assert_no_result(
2930
'Unknown selector -> returns null',
30-
'SELECT e->''blahvtha'' FROM encrypted;');
31+
'SELECT e->''blahvtha''::text FROM encrypted;');
3132

3233
END;
3334
$$ LANGUAGE plpgsql;
3435

3536

3637

38+
--
39+
-- The -> operator accepts an eql_v2_encrypted as the selector
40+
--
41+
DO $$
42+
DECLARE
43+
term text;
44+
BEGIN
45+
term := '{"s": "bca213de9ccce676fa849ff9c4807963"}';
46+
47+
PERFORM assert_result(
48+
'Selector -> returns at least one eql_v2_encrypted',
49+
format('SELECT e->%L::jsonb::eql_v2_encrypted FROM encrypted;', term));
50+
51+
PERFORM assert_count(
52+
'Selector -> returns all eql_v2_encrypted',
53+
format('SELECT e->%L::jsonb::eql_v2_encrypted FROM encrypted;', term),
54+
3);
55+
END;
56+
$$ LANGUAGE plpgsql;
57+
58+
3759
--
3860
-- encrypted returned from -> operator expression called via eql_v2.ciphertext
3961
--
@@ -43,11 +65,11 @@ DO $$
4365
BEGIN
4466
PERFORM assert_result(
4567
'Fetch ciphertext via selector',
46-
'SELECT eql_v2.ciphertext(e->''2517068c0d1f9d4d41d2c666211f785e'') FROM encrypted;');
68+
'SELECT eql_v2.ciphertext(e->''2517068c0d1f9d4d41d2c666211f785e''::text) FROM encrypted;');
4769

4870
PERFORM assert_count(
4971
'Fetch ciphertext via selector returns all eql_v2_encrypted',
50-
'SELECT eql_v2.ciphertext(e->''2517068c0d1f9d4d41d2c666211f785e'') FROM encrypted;',
72+
'SELECT eql_v2.ciphertext(e->''2517068c0d1f9d4d41d2c666211f785e''::text) FROM encrypted;',
5173
3);
5274
END;
5375
$$ LANGUAGE plpgsql;

src/operators/<=_test.sql

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

6-
SELECT e FROM encrypted WHERE e->'a7cea93975ed8c01f861ccb6bd082784' <= '("{""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+
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;
77

88

99
-- ------------------------------------------------------------------------
@@ -29,23 +29,23 @@ DECLARE
2929
-- json n: 30
3030
sv := get_numeric_ste_vec_30()::eql_v2_encrypted;
3131
-- extract the term at $.n returned as eql_v2_encrypted
32-
term := sv->'2517068c0d1f9d4d41d2c666211f785e';
32+
term := sv->'2517068c0d1f9d4d41d2c666211f785e'::text;
3333

3434
-- -- -- -- $.n
3535
PERFORM assert_result(
3636
format('eql_v2_encrypted <= eql_v2_encrypted with ore_cllw_u64_8 index term'),
37-
format('SELECT e FROM encrypted WHERE e->''2517068c0d1f9d4d41d2c666211f785e'' <= %L::eql_v2_encrypted', term));
37+
format('SELECT e FROM encrypted WHERE e->''2517068c0d1f9d4d41d2c666211f785e''::text <= %L::eql_v2_encrypted', term));
3838

3939
PERFORM assert_count(
4040
format('eql_v2_encrypted <= eql_v2_encrypted with ore index term'),
41-
format('SELECT e FROM encrypted WHERE e->''2517068c0d1f9d4d41d2c666211f785e'' <= %L::eql_v2_encrypted', term),
41+
format('SELECT e FROM encrypted WHERE e->''2517068c0d1f9d4d41d2c666211f785e''::text <= %L::eql_v2_encrypted', term),
4242
3);
4343

4444
-- -- Check the $.hello path
4545
-- -- Returned encrypted does not have ore_cllw_u64_8
4646
PERFORM assert_no_result(
4747
format('eql_v2_encrypted <= eql_v2_encrypted with ore_cllw_u64_8 index term'),
48-
format('SELECT e FROM encrypted WHERE e->''a7cea93975ed8c01f861ccb6bd082784'' <= %L::eql_v2_encrypted', term));
48+
format('SELECT e FROM encrypted WHERE e->''a7cea93975ed8c01f861ccb6bd082784''::text <= %L::eql_v2_encrypted', term));
4949

5050
END;
5151
$$ LANGUAGE plpgsql;
@@ -73,23 +73,23 @@ DECLARE
7373
-- json n: 30
7474
sv := get_numeric_ste_vec_30()::eql_v2_encrypted;
7575
-- extract the term at $.n returned as eql_v2_encrypted
76-
term := sv->'a7cea93975ed8c01f861ccb6bd082784';
76+
term := sv->'a7cea93975ed8c01f861ccb6bd082784'::text;
7777

7878
-- -- -- -- $.n
7979
PERFORM assert_result(
8080
format('eql_v2_encrypted <= eql_v2_encrypted with ore_cllw_var_8 index term'),
81-
format('SELECT e FROM encrypted WHERE e->''a7cea93975ed8c01f861ccb6bd082784'' <= %L::eql_v2_encrypted', term));
81+
format('SELECT e FROM encrypted WHERE e->''a7cea93975ed8c01f861ccb6bd082784''::text <= %L::eql_v2_encrypted', term));
8282

8383
PERFORM assert_count(
8484
format('eql_v2_encrypted <= eql_v2_encrypted with ore_cllw_var_8 index term'),
85-
format('SELECT e FROM encrypted WHERE e->''a7cea93975ed8c01f861ccb6bd082784'' <= %L::eql_v2_encrypted', term),
85+
format('SELECT e FROM encrypted WHERE e->''a7cea93975ed8c01f861ccb6bd082784''::text <= %L::eql_v2_encrypted', term),
8686
2);
8787

8888
-- -- Check the $.n path
8989
-- -- Returned encrypted does not have ore_cllw_u64_8
9090
PERFORM assert_no_result(
9191
format('eql_v2_encrypted <= eql_v2_encrypted with ore_cllw_var_8 index term'),
92-
format('SELECT e FROM encrypted WHERE e->''2517068c0d1f9d4d41d2c666211f785e'' <= %L::eql_v2_encrypted', term));
92+
format('SELECT e FROM encrypted WHERE e->''2517068c0d1f9d4d41d2c666211f785e''::text <= %L::eql_v2_encrypted', term));
9393

9494
END;
9595
$$ LANGUAGE plpgsql;

src/operators/<>_ore_cllw_u64_8_test.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,23 +31,23 @@ DECLARE
3131
-- json n: 10
3232
sv := get_numeric_ste_vec_10()::eql_v2_encrypted;
3333
-- extract the term at $.n returned as eql_v2_encrypted
34-
term := sv->'2517068c0d1f9d4d41d2c666211f785e';
34+
term := sv->'2517068c0d1f9d4d41d2c666211f785e'::text;
3535

3636
-- -- -- -- $.n
3737
PERFORM assert_result(
3838
format('eql_v2_encrypted <> eql_v2_encrypted with ore_cllw_u64_8 index term'),
39-
format('SELECT e FROM encrypted WHERE (e->''2517068c0d1f9d4d41d2c666211f785e'') <> %L::eql_v2_encrypted', term));
39+
format('SELECT e FROM encrypted WHERE (e->''2517068c0d1f9d4d41d2c666211f785e''::text) <> %L::eql_v2_encrypted', term));
4040

4141
PERFORM assert_count(
4242
format('eql_v2_encrypted <> eql_v2_encrypted with ore index term'),
43-
format('SELECT e FROM encrypted WHERE e->''2517068c0d1f9d4d41d2c666211f785e'' <> %L::eql_v2_encrypted', term),
43+
format('SELECT e FROM encrypted WHERE e->''2517068c0d1f9d4d41d2c666211f785e''::text <> %L::eql_v2_encrypted', term),
4444
2);
4545

4646
-- -- Check the $.hello path
4747
-- -- Returned encrypted does not have ore_cllw_u64_8
4848
PERFORM assert_result(
4949
format('eql_v2_encrypted <> eql_v2_encrypted with ore index term'),
50-
format('SELECT e FROM encrypted WHERE e->''a7cea93975ed8c01f861ccb6bd082784'' <> %L::eql_v2_encrypted', term));
50+
format('SELECT e FROM encrypted WHERE e->''a7cea93975ed8c01f861ccb6bd082784''::text <> %L::eql_v2_encrypted', term));
5151

5252
END;
5353
$$ LANGUAGE plpgsql;

src/operators/<>_ore_cllw_var_8_test.sql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,23 @@ DECLARE
3030
-- json n: 10
3131
sv := get_numeric_ste_vec_10()::eql_v2_encrypted;
3232
-- extract the term at $.n returned as eql_v2_encrypted
33-
term := sv->'a7cea93975ed8c01f861ccb6bd082784';
33+
term := sv->'a7cea93975ed8c01f861ccb6bd082784'::text;
3434

3535
-- -- -- -- $.n
3636
PERFORM assert_result(
3737
format('eql_v2_encrypted <> eql_v2_encrypted with ore_cllw_var_8 index term'),
38-
format('SELECT e FROM encrypted WHERE (e->''a7cea93975ed8c01f861ccb6bd082784'') <> %L::eql_v2_encrypted', term));
38+
format('SELECT e FROM encrypted WHERE (e->''a7cea93975ed8c01f861ccb6bd082784''::text) <> %L::eql_v2_encrypted', term));
3939

4040
PERFORM assert_count(
4141
format('eql_v2_encrypted <> eql_v2_encrypted with ore_cllw_var_8 index term'),
42-
format('SELECT e FROM encrypted WHERE e->''a7cea93975ed8c01f861ccb6bd082784'' <> %L::eql_v2_encrypted', term),
42+
format('SELECT e FROM encrypted WHERE e->''a7cea93975ed8c01f861ccb6bd082784''::text <> %L::eql_v2_encrypted', term),
4343
2);
4444

4545
-- -- Check the $.n path
4646
-- -- Returned encrypted does not have ore_cllw_var_8
4747
PERFORM assert_result(
4848
format('eql_v2_encrypted <> eql_v2_encrypted with ore_cllw_var_8 index term'),
49-
format('SELECT e FROM encrypted WHERE e->''2517068c0d1f9d4d41d2c666211f785e'' <> %L::eql_v2_encrypted', term));
49+
format('SELECT e FROM encrypted WHERE e->''2517068c0d1f9d4d41d2c666211f785e''::text <> %L::eql_v2_encrypted', term));
5050

5151
END;
5252
$$ LANGUAGE plpgsql;

src/operators/<@_test.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ DECLARE
2525
-- This extracts the data associated with the field from the test eql_v2_encrypted
2626
sv := get_numeric_ste_vec_10()::eql_v2_encrypted;
2727
-- extract the term at $.n returned as eql_v2_encrypted
28-
term := sv->'a7cea93975ed8c01f861ccb6bd082784';
28+
term := sv->'a7cea93975ed8c01f861ccb6bd082784'::text;
2929

3030
-- -- -- -- $.n
3131
PERFORM assert_result(

0 commit comments

Comments
 (0)