Skip to content

Commit 0840f4a

Browse files
committed
feat: add json functions that take encrypted
1 parent 2905a33 commit 0840f4a

File tree

3 files changed

+75
-60
lines changed

3 files changed

+75
-60
lines changed

src/jsonb/functions.sql

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ AS $$
7171
$$ LANGUAGE plpgsql;
7272

7373

74+
CREATE FUNCTION eql_v2.jsonb_path_query(val eql_v2_encrypted, selector eql_v2_encrypted)
75+
RETURNS SETOF eql_v2_encrypted
76+
IMMUTABLE STRICT PARALLEL SAFE
77+
AS $$
78+
BEGIN
79+
RETURN QUERY
80+
SELECT * FROM eql_v2.jsonb_path_query(val.data, eql_v2.selector(selector));
81+
END;
82+
$$ LANGUAGE plpgsql;
83+
7484

7585
CREATE FUNCTION eql_v2.jsonb_path_query(val eql_v2_encrypted, selector text)
7686
RETURNS SETOF eql_v2_encrypted
@@ -83,6 +93,8 @@ AS $$
8393
$$ LANGUAGE plpgsql;
8494

8595

96+
------------------------------------------------------------------------------------
97+
8698

8799
CREATE FUNCTION eql_v2.jsonb_path_exists(val jsonb, selector text)
88100
RETURNS boolean
@@ -96,6 +108,17 @@ AS $$
96108
$$ LANGUAGE plpgsql;
97109

98110

111+
CREATE FUNCTION eql_v2.jsonb_path_exists(val eql_v2_encrypted, selector eql_v2_encrypted)
112+
RETURNS boolean
113+
IMMUTABLE STRICT PARALLEL SAFE
114+
AS $$
115+
BEGIN
116+
RETURN EXISTS (
117+
SELECT eql_v2.jsonb_path_query(val, eql_v2.selector(selector))
118+
);
119+
END;
120+
$$ LANGUAGE plpgsql;
121+
99122

100123
CREATE FUNCTION eql_v2.jsonb_path_exists(val eql_v2_encrypted, selector text)
101124
RETURNS boolean
@@ -109,8 +132,8 @@ AS $$
109132
$$ LANGUAGE plpgsql;
110133

111134

112-
--
113-
--
135+
------------------------------------------------------------------------------------
136+
114137

115138
CREATE FUNCTION eql_v2.jsonb_path_query_first(val jsonb, selector text)
116139
RETURNS eql_v2_encrypted
@@ -128,6 +151,19 @@ AS $$
128151
$$ LANGUAGE plpgsql;
129152

130153

154+
CREATE FUNCTION eql_v2.jsonb_path_query_first(val eql_v2_encrypted, selector eql_v2_encrypted)
155+
RETURNS eql_v2_encrypted
156+
IMMUTABLE STRICT PARALLEL SAFE
157+
AS $$
158+
BEGIN
159+
RETURN (
160+
SELECT e
161+
FROM eql_v2.jsonb_path_query(val.data, eql_v2.selector(selector)) as e
162+
LIMIT 1
163+
);
164+
END;
165+
$$ LANGUAGE plpgsql;
166+
131167

132168
CREATE FUNCTION eql_v2.jsonb_path_query_first(val eql_v2_encrypted, selector text)
133169
RETURNS eql_v2_encrypted
@@ -144,7 +180,7 @@ $$ LANGUAGE plpgsql;
144180

145181

146182

147-
--
183+
------------------------------------------------------------------------------------
148184

149185

150186
-- =====================================================================

src/jsonb/functions_test.sql

Lines changed: 32 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -5,87 +5,62 @@ SELECT create_table_with_encrypted();
55
SELECT seed_encrypted_json();
66

77

8-
-- CREATE TABLE unencrypted
9-
-- (
10-
-- id bigint GENERATED ALWAYS AS IDENTITY,
11-
-- u jsonb,
12-
-- PRIMARY KEY(id)
13-
-- );
14-
-- INSERT INTO unencrypted (u)
15-
-- VALUES
16-
-- ('{"a": [1, 2, 3] }'),
17-
-- ('{"a": [1, 2, 3, 4] }'),
18-
-- ('{"a": [1, 2, 3, 4, 5] }');
19-
20-
-- SELECT *
21-
-- FROM unencrypted
22-
-- WHERE EXISTS (
23-
-- SELECT 1
24-
-- FROM jsonb_array_elements(u->'a') AS elem
25-
-- WHERE elem::int < 2
26-
-- );
27-
28-
-- SELECT seed_encrypted(get_array_ste_vec()::eql_v2_encrypted);
29-
-- SELECT *
30-
-- FROM encrypted
31-
-- WHERE EXISTS (
32-
-- SELECT 1
33-
-- FROM eql_v2.jsonb_array_elements(e->'f510853730e1c3dbd31b86963f029dd5') AS elem
34-
-- WHERE elem > '{"ocf": "b0c0a7385cb2f7dfe32a2649a9d8294794b8fc05585a240c1315f1e45ee7d9012616db3f01b43fa94351618670a29c24fc75df1392d52764c757b34495888b1c"}'::jsonb
35-
-- );
36-
37-
-- SELECT eql_v2.jsonb_path_query_first(e, '33743aed3ae636f6bf05cff11ac4b519') as e
38-
-- FROM encrypted
39-
-- WHERE eql_v2.jsonb_path_query(e, '33743aed3ae636f6bf05cff11ac4b519') IS NOT NULL;
40-
41-
42-
43-
-- "ocf": "b0c0a7385cb2f7dfe32a2649a9d8294794b8fc05585a240c1315f1e45ee7d9012616db3f01b43fa94351618670a29c24fc75df1392d52764c757b34495888b1c",
44-
45-
-- SELECT eql_v2.jsonb_array_elements(eql_v2.jsonb_path_query(e, 'f510853730e1c3dbd31b86963f029dd5')) as e FROM encrypted ;
46-
47-
48-
8+
-- ========================================================================
9+
--
10+
-- Selector &.a[*]
11+
-- -> 33743aed3ae636f6bf05cff11ac4b519
12+
--
13+
DO $$
14+
BEGIN
4915

50-
-- -- SELECT eql_v2.jsonb_path_exists(e, ''f510853730e1c3dbd31b86963f029dd5'') FROM encrypted;
51-
-- -- SELECT eql_v2.jsonb_path_query(e, 'f510853730e1c3dbd31b86963f029dd5') FROM encrypted;
16+
PERFORM seed_encrypted_json();
17+
PERFORM seed_encrypted(get_array_ste_vec()::eql_v2_encrypted);
5218

53-
-- -- SELECT eql_v2.jsonb_path_query(e, 'f510853730e1c3dbd31b86963f029dd5') as e FROM encrypted;
54-
-- -- SELECT eql_v2.jsonb_array_length(eql_v2.jsonb_path_query(e, 'f510853730e1c3dbd31b86963f029dd5')) as e FROM encrypted LIMIT 1;
55-
-- -- SELECT eql_v2.jsonb_array_elements(eql_v2.jsonb_path_query(e, 'f510853730e1c3dbd31b86963f029dd5')) as e FROM encrypted ;
56-
-- -- SELECT eql_v2.jsonb_array_elements_text(eql_v2.jsonb_path_query(e, 'f510853730e1c3dbd31b86963f029dd5')) as e FROM encrypted ;
57-
-- -- SELECT eql_v2.jsonb_array_length(eql_v2.jsonb_path_query(e, 'f510853730e1c3dbd31b86963f029dd5')) as e FROM encrypted LIMIT 1;
58-
-- -- SELECT eql_v2.jsonb_path_query(e, 'f510853730e1c3dbd31b86963f029dd5') as e FROM encrypted;
19+
PERFORM assert_result(
20+
'jsonb_array_elements returns array elements from jsonb_path_query result',
21+
'SELECT eql_v2.jsonb_array_elements(eql_v2.jsonb_path_query(e, ''f510853730e1c3dbd31b86963f029dd5'')) as e FROM encrypted;');
5922

23+
PERFORM assert_count(
24+
'jsonb_array_elements returns the correct number of array elements from jsonb_path_query result',
25+
'SELECT eql_v2.jsonb_array_elements(eql_v2.jsonb_path_query(e, ''f510853730e1c3dbd31b86963f029dd5'')) as e FROM encrypted;',
26+
5);
6027

28+
PERFORM assert_exception(
29+
'jsonb_array_elements exception if input is not an array',
30+
'SELECT eql_v2.jsonb_array_elements(eql_v2.jsonb_path_query(e, ''33743aed3ae636f6bf05cff11ac4b519'')) as e FROM encrypted LIMIT 1;');
6131

32+
END;
33+
$$ LANGUAGE plpgsql;
6234

63-
-- ========================================================================
6435
--
65-
-- Selector &.a[*]
36+
-- Selector &.a[*] as eql_v2_encrypted
6637
-- -> 33743aed3ae636f6bf05cff11ac4b519
6738
--
6839
DO $$
6940
DECLARE
70-
sv eql_v2_encrypted;
71-
results eql_v2_encrypted[];
41+
selector eql_v2_encrypted;
42+
7243
BEGIN
7344

7445
PERFORM seed_encrypted_json();
7546
PERFORM seed_encrypted(get_array_ste_vec()::eql_v2_encrypted);
7647

48+
selector := '{"s": "f510853730e1c3dbd31b86963f029dd5"}'::jsonb::eql_v2_encrypted;
49+
7750
PERFORM assert_result(
78-
'jsonb_array_elements returns array elements from jsonb_path_query result',
79-
'SELECT eql_v2.jsonb_array_elements(eql_v2.jsonb_path_query(e, ''f510853730e1c3dbd31b86963f029dd5'')) as e FROM encrypted;');
51+
'jsonb_array_elements returns array elements from jsonb_path_query result using eql_v2_encrypted selector',
52+
format('SELECT eql_v2.jsonb_array_elements_text(eql_v2.jsonb_path_query(e, %L::eql_v2_encrypted)) as e FROM encrypted;', selector));
8053

8154
PERFORM assert_count(
8255
'jsonb_array_elements returns the correct number of array elements from jsonb_path_query result',
83-
'SELECT eql_v2.jsonb_array_elements(eql_v2.jsonb_path_query(e, ''f510853730e1c3dbd31b86963f029dd5'')) as e FROM encrypted;',
56+
format('SELECT eql_v2.jsonb_array_elements_text(eql_v2.jsonb_path_query(e, %L::eql_v2_encrypted)) as e FROM encrypted;', selector),
8457
5);
8558

59+
selector := '{"s": "33743aed3ae636f6bf05cff11ac4b519"}'::jsonb::eql_v2_encrypted;
60+
8661
PERFORM assert_exception(
8762
'jsonb_array_elements exception if input is not an array',
88-
'SELECT eql_v2.jsonb_array_elements(eql_v2.jsonb_path_query(e, ''33743aed3ae636f6bf05cff11ac4b519'')) as e FROM encrypted LIMIT 1;');
63+
format('SELECT eql_v2.jsonb_array_elements_text(eql_v2.jsonb_path_query(e, %L::eql_v2_encrypted)) as e FROM encrypted;', selector));
8964

9065
END;
9166
$$ LANGUAGE plpgsql;

src/ste_vec/functions.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ CREATE FUNCTION eql_v2.selector(val jsonb)
4545
IMMUTABLE STRICT PARALLEL SAFE
4646
AS $$
4747
BEGIN
48+
IF val IS NULL THEN
49+
RETURN NULL;
50+
END IF;
51+
4852
IF val ? 's' THEN
4953
RETURN val->>'s';
5054
END IF;

0 commit comments

Comments
 (0)