From 116570342bff0c29fcd57f4e37ecf44a2757b3b1 Mon Sep 17 00:00:00 2001 From: Toby Hede Date: Thu, 31 Jul 2025 12:50:36 +1000 Subject: [PATCH 1/3] fix: add immutable to cast functions --- src/encrypted/casts.sql | 12 ++++++-- src/operators/operator_class_test.sql | 40 +++++++++++++++++++++++++++ src/operators/order_by.sql | 1 - 3 files changed, 49 insertions(+), 4 deletions(-) diff --git a/src/encrypted/casts.sql b/src/encrypted/casts.sql index 7a5e047..212d32c 100644 --- a/src/encrypted/casts.sql +++ b/src/encrypted/casts.sql @@ -7,7 +7,9 @@ -- CREATE FUNCTION eql_v2.to_encrypted(data jsonb) -RETURNS public.eql_v2_encrypted AS $$ +RETURNS public.eql_v2_encrypted +IMMUTABLE STRICT PARALLEL SAFE +AS $$ BEGIN IF data IS NULL THEN RETURN NULL; @@ -17,6 +19,7 @@ BEGIN END; $$ LANGUAGE plpgsql; + -- -- Cast jsonb to eql_v2.encrypted -- @@ -30,16 +33,19 @@ CREATE CAST (jsonb AS public.eql_v2_encrypted) -- CREATE FUNCTION eql_v2.to_encrypted(data text) -RETURNS public.eql_v2_encrypted AS $$ +RETURNS public.eql_v2_encrypted + IMMUTABLE STRICT PARALLEL SAFE +AS $$ BEGIN IF data IS NULL THEN RETURN NULL; END IF; - RETURN ROW(data::jsonb)::public.eql_v2_encrypted; + RETURN eql_v2.to_encrypted(data::jsonb); END; $$ LANGUAGE plpgsql; + -- -- Cast text to eql_v2.encrypted -- diff --git a/src/operators/operator_class_test.sql b/src/operators/operator_class_test.sql index e901ed7..98b6420 100644 --- a/src/operators/operator_class_test.sql +++ b/src/operators/operator_class_test.sql @@ -152,9 +152,49 @@ DO $$ INSERT INTO encrypted (e) VALUES ('("{\"hm\": \"abc\"}")'); INSERT INTO encrypted (e) VALUES ('("{\"hm\": \"def\"}")'); INSERT INTO encrypted (e) VALUES ('("{\"hm\": \"ghi\"}")'); + INSERT INTO encrypted (e) VALUES ('("{\"hm\": \"jkl\"}")'); + INSERT INTO encrypted (e) VALUES ('("{\"hm\": \"mno\"}")'); + -- Literal row type type thing EXECUTE 'EXPLAIN ANALYZE SELECT e::jsonb FROM encrypted WHERE e = ''("{\"hm\": \"abc\"}")'';' into result; + IF position('Index Only Scan using encrypted' in result) > 0 THEN + ASSERT true; + ELSE + RAISE EXCEPTION 'Expected Index Only Scan: %', result; + END IF; + + -- Cast to jsonb to eql_v2_encrypted + EXECUTE 'EXPLAIN ANALYZE SELECT e::jsonb FROM encrypted WHERE e = ''{"hm": "abc"}''::jsonb::eql_v2_encrypted;' into result; + + IF position('Index Only Scan using encrypted' in result) > 0 THEN + ASSERT true; + ELSE + RAISE EXCEPTION 'Expected Index Only Scan: %', result; + END IF; + + -- Cast to text to eql_v2_encrypted + EXECUTE 'EXPLAIN ANALYZE SELECT e::jsonb FROM encrypted WHERE e = ''{"hm": "abc"}''::text::eql_v2_encrypted;' into result; + + IF position('Index Only Scan using encrypted' in result) > 0 THEN + ASSERT true; + ELSE + RAISE EXCEPTION 'Expected Index Only Scan: %', result; + END IF; + + -- Use to_encrypted with jsonb + EXECUTE 'EXPLAIN ANALYZE SELECT e::jsonb FROM encrypted WHERE e = eql_v2.to_encrypted(''{"hm": "abc"}''::jsonb);' into result; + + IF position('Index Only Scan using encrypted' in result) > 0 THEN + ASSERT true; + ELSE + RAISE EXCEPTION 'Expected Index Only Scan: %', result; + END IF; + + -- Use to_encrypted with text + EXECUTE 'EXPLAIN ANALYZE SELECT e::jsonb FROM encrypted WHERE e = eql_v2.to_encrypted(''{"hm": "abc"}'');' into result; + + IF position('Index Only Scan using encrypted' in result) > 0 THEN ASSERT true; ELSE diff --git a/src/operators/order_by.sql b/src/operators/order_by.sql index cc8fe26..b604d73 100644 --- a/src/operators/order_by.sql +++ b/src/operators/order_by.sql @@ -1,7 +1,6 @@ -- REQUIRE: src/encrypted/types.sql -- REQUIRE: src/ore_block_u64_8_256/types.sql -- REQUIRE: src/ore_block_u64_8_256/functions.sql --- REQUIRE: src/ore_block_u64_8_256/operators.sql -- REQUIRE: src/ore_cllw_u64_8/types.sql -- REQUIRE: src/ore_cllw_u64_8/functions.sql From 22eac95d4150582b652b9ed9d531146cf4288c00 Mon Sep 17 00:00:00 2001 From: Toby Hede Date: Thu, 31 Jul 2025 14:05:46 +1000 Subject: [PATCH 2/3] fix: make spacing consistent --- src/encrypted/casts.sql | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/encrypted/casts.sql b/src/encrypted/casts.sql index 212d32c..7d6eea3 100644 --- a/src/encrypted/casts.sql +++ b/src/encrypted/casts.sql @@ -7,8 +7,8 @@ -- CREATE FUNCTION eql_v2.to_encrypted(data jsonb) -RETURNS public.eql_v2_encrypted -IMMUTABLE STRICT PARALLEL SAFE + RETURNS public.eql_v2_encrypted + IMMUTABLE STRICT PARALLEL SAFE AS $$ BEGIN IF data IS NULL THEN @@ -33,7 +33,7 @@ CREATE CAST (jsonb AS public.eql_v2_encrypted) -- CREATE FUNCTION eql_v2.to_encrypted(data text) -RETURNS public.eql_v2_encrypted + RETURNS public.eql_v2_encrypted IMMUTABLE STRICT PARALLEL SAFE AS $$ BEGIN @@ -60,7 +60,9 @@ CREATE CAST (text AS public.eql_v2_encrypted) -- CREATE FUNCTION eql_v2.to_jsonb(e public.eql_v2_encrypted) -RETURNS jsonb AS $$ + RETURNS jsonb + IMMUTABLE STRICT PARALLEL SAFE +AS $$ BEGIN IF e IS NULL THEN RETURN NULL; From 8b775888c7831cbca18ee81cfee560fd09d492f8 Mon Sep 17 00:00:00 2001 From: Toby Hede Date: Thu, 31 Jul 2025 14:06:12 +1000 Subject: [PATCH 3/3] test: add test for no index case --- src/operators/operator_class_test.sql | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/operators/operator_class_test.sql b/src/operators/operator_class_test.sql index 98b6420..efb836c 100644 --- a/src/operators/operator_class_test.sql +++ b/src/operators/operator_class_test.sql @@ -164,6 +164,16 @@ DO $$ RAISE EXCEPTION 'Expected Index Only Scan: %', result; END IF; + -- Cast to jsonb + EXECUTE 'EXPLAIN ANALYZE SELECT e::jsonb FROM encrypted WHERE e = ''{"hm": "abc"}''::jsonb;' into result; + + -- INDEX IS NOT USED + IF position('Seq Scan on encrypted' in result) > 0 THEN + ASSERT true; + ELSE + RAISE EXCEPTION 'Unexpected Seq Scan: %', result; + END IF; + -- Cast to jsonb to eql_v2_encrypted EXECUTE 'EXPLAIN ANALYZE SELECT e::jsonb FROM encrypted WHERE e = ''{"hm": "abc"}''::jsonb::eql_v2_encrypted;' into result;