Skip to content

Commit c338529

Browse files
authored
Merge pull request #128 from cipherstash/fix/cast-for-indexing
Add immutable to cast functions
2 parents f8257f2 + 8b77588 commit c338529

File tree

3 files changed

+62
-5
lines changed

3 files changed

+62
-5
lines changed

src/encrypted/casts.sql

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
--
88

99
CREATE FUNCTION eql_v2.to_encrypted(data jsonb)
10-
RETURNS public.eql_v2_encrypted AS $$
10+
RETURNS public.eql_v2_encrypted
11+
IMMUTABLE STRICT PARALLEL SAFE
12+
AS $$
1113
BEGIN
1214
IF data IS NULL THEN
1315
RETURN NULL;
@@ -17,6 +19,7 @@ BEGIN
1719
END;
1820
$$ LANGUAGE plpgsql;
1921

22+
2023
--
2124
-- Cast jsonb to eql_v2.encrypted
2225
--
@@ -30,16 +33,19 @@ CREATE CAST (jsonb AS public.eql_v2_encrypted)
3033
--
3134

3235
CREATE FUNCTION eql_v2.to_encrypted(data text)
33-
RETURNS public.eql_v2_encrypted AS $$
36+
RETURNS public.eql_v2_encrypted
37+
IMMUTABLE STRICT PARALLEL SAFE
38+
AS $$
3439
BEGIN
3540
IF data IS NULL THEN
3641
RETURN NULL;
3742
END IF;
3843

39-
RETURN ROW(data::jsonb)::public.eql_v2_encrypted;
44+
RETURN eql_v2.to_encrypted(data::jsonb);
4045
END;
4146
$$ LANGUAGE plpgsql;
4247

48+
4349
--
4450
-- Cast text to eql_v2.encrypted
4551
--
@@ -54,7 +60,9 @@ CREATE CAST (text AS public.eql_v2_encrypted)
5460
--
5561

5662
CREATE FUNCTION eql_v2.to_jsonb(e public.eql_v2_encrypted)
57-
RETURNS jsonb AS $$
63+
RETURNS jsonb
64+
IMMUTABLE STRICT PARALLEL SAFE
65+
AS $$
5866
BEGIN
5967
IF e IS NULL THEN
6068
RETURN NULL;

src/operators/operator_class_test.sql

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,9 +152,59 @@ DO $$
152152
INSERT INTO encrypted (e) VALUES ('("{\"hm\": \"abc\"}")');
153153
INSERT INTO encrypted (e) VALUES ('("{\"hm\": \"def\"}")');
154154
INSERT INTO encrypted (e) VALUES ('("{\"hm\": \"ghi\"}")');
155+
INSERT INTO encrypted (e) VALUES ('("{\"hm\": \"jkl\"}")');
156+
INSERT INTO encrypted (e) VALUES ('("{\"hm\": \"mno\"}")');
155157

158+
-- Literal row type type thing
156159
EXECUTE 'EXPLAIN ANALYZE SELECT e::jsonb FROM encrypted WHERE e = ''("{\"hm\": \"abc\"}")'';' into result;
157160

161+
IF position('Index Only Scan using encrypted' in result) > 0 THEN
162+
ASSERT true;
163+
ELSE
164+
RAISE EXCEPTION 'Expected Index Only Scan: %', result;
165+
END IF;
166+
167+
-- Cast to jsonb
168+
EXECUTE 'EXPLAIN ANALYZE SELECT e::jsonb FROM encrypted WHERE e = ''{"hm": "abc"}''::jsonb;' into result;
169+
170+
-- INDEX IS NOT USED
171+
IF position('Seq Scan on encrypted' in result) > 0 THEN
172+
ASSERT true;
173+
ELSE
174+
RAISE EXCEPTION 'Unexpected Seq Scan: %', result;
175+
END IF;
176+
177+
-- Cast to jsonb to eql_v2_encrypted
178+
EXECUTE 'EXPLAIN ANALYZE SELECT e::jsonb FROM encrypted WHERE e = ''{"hm": "abc"}''::jsonb::eql_v2_encrypted;' into result;
179+
180+
IF position('Index Only Scan using encrypted' in result) > 0 THEN
181+
ASSERT true;
182+
ELSE
183+
RAISE EXCEPTION 'Expected Index Only Scan: %', result;
184+
END IF;
185+
186+
-- Cast to text to eql_v2_encrypted
187+
EXECUTE 'EXPLAIN ANALYZE SELECT e::jsonb FROM encrypted WHERE e = ''{"hm": "abc"}''::text::eql_v2_encrypted;' into result;
188+
189+
IF position('Index Only Scan using encrypted' in result) > 0 THEN
190+
ASSERT true;
191+
ELSE
192+
RAISE EXCEPTION 'Expected Index Only Scan: %', result;
193+
END IF;
194+
195+
-- Use to_encrypted with jsonb
196+
EXECUTE 'EXPLAIN ANALYZE SELECT e::jsonb FROM encrypted WHERE e = eql_v2.to_encrypted(''{"hm": "abc"}''::jsonb);' into result;
197+
198+
IF position('Index Only Scan using encrypted' in result) > 0 THEN
199+
ASSERT true;
200+
ELSE
201+
RAISE EXCEPTION 'Expected Index Only Scan: %', result;
202+
END IF;
203+
204+
-- Use to_encrypted with text
205+
EXECUTE 'EXPLAIN ANALYZE SELECT e::jsonb FROM encrypted WHERE e = eql_v2.to_encrypted(''{"hm": "abc"}'');' into result;
206+
207+
158208
IF position('Index Only Scan using encrypted' in result) > 0 THEN
159209
ASSERT true;
160210
ELSE

src/operators/order_by.sql

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
-- REQUIRE: src/encrypted/types.sql
22
-- REQUIRE: src/ore_block_u64_8_256/types.sql
33
-- REQUIRE: src/ore_block_u64_8_256/functions.sql
4-
-- REQUIRE: src/ore_block_u64_8_256/operators.sql
54
-- REQUIRE: src/ore_cllw_u64_8/types.sql
65
-- REQUIRE: src/ore_cllw_u64_8/functions.sql
76

0 commit comments

Comments
 (0)