Skip to content

Commit e3e291e

Browse files
committed
Parse ORE indexes as JSON arrays of hex-encoded strings
This change updates `cs_ore_64_8_v1` to parse ORE indexes (the `'o'` field) as JSON arrays of hex-encoded strings (instead of casting from the Postgres text format). The corresponding change for encoding ORE indexes as JSON arrays of hex-encoded stings has already been merged in Proxy. This is similar to some of the changes in #86, but we're parsing into the composite types for ORE indexes instead of into a plain `bytea[]`. Parsing into the composite type allows for ordering with an operator class on the output from `cs_ore_64_8_v1`.
1 parent ab9afc1 commit e3e291e

File tree

3 files changed

+32
-15
lines changed

3 files changed

+32
-15
lines changed

sql/011-core-functions.sql

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,24 @@ DROP CAST IF EXISTS (text AS ore_64_8_v1_term);
156156
CREATE CAST (text AS ore_64_8_v1_term)
157157
WITH FUNCTION _cs_text_to_ore_64_8_v1_term_v1_0(text) AS IMPLICIT;
158158

159+
DROP FUNCTION IF EXISTS jsonb_array_to_ore_64_8_v1(val jsonb);
160+
161+
CREATE FUNCTION jsonb_array_to_ore_64_8_v1(val jsonb)
162+
RETURNS ore_64_8_v1 AS $$
163+
DECLARE
164+
terms_arr ore_64_8_v1_term[];
165+
BEGIN
166+
IF jsonb_typeof(val) = 'null' THEN
167+
RETURN NULL;
168+
END IF;
169+
170+
SELECT array_agg(ROW(decode(value::text, 'hex'))::ore_64_8_v1_term)
171+
INTO terms_arr
172+
FROM jsonb_array_elements_text(val) AS value;
173+
174+
RETURN ROW(terms_arr)::ore_64_8_v1;
175+
END;
176+
$$ LANGUAGE plpgsql;
159177

160178
-- extracts ore index from an encrypted column
161179
DROP FUNCTION IF EXISTS cs_ore_64_8_v1_v0_0(val jsonb);
@@ -166,7 +184,7 @@ CREATE FUNCTION cs_ore_64_8_v1_v0_0(val jsonb)
166184
AS $$
167185
BEGIN
168186
IF val ? 'o' THEN
169-
RETURN (val->>'o')::ore_64_8_v1;
187+
RETURN jsonb_array_to_ore_64_8_v1(val->'o');
170188
END IF;
171189
RAISE 'Expected an ore index (o) value in json: %', val;
172190
END;

tests/core-functions.sql

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,15 @@ DO $$
66
ASSERT (SELECT EXISTS (SELECT cs_unique_v1('{"u": "u"}'::jsonb)));
77
ASSERT (SELECT EXISTS (SELECT cs_match_v1('{"m": []}'::jsonb)));
88
ASSERT (SELECT EXISTS (SELECT cs_ste_vec_v1('{"sv": [[]]}'::jsonb)));
9-
ASSERT (SELECT EXISTS (SELECT cs_ore_64_8_v1('{"o": "()"}'::jsonb)));
9+
ASSERT (SELECT EXISTS (SELECT cs_ore_64_8_v1('{"o": []}'::jsonb)));
1010

1111
END;
1212
$$ LANGUAGE plpgsql;
1313

1414
DO $$
1515
BEGIN
1616
-- sanity check
17-
PERFORM cs_ore_64_8_v1('{"o": "()"}'::jsonb);
17+
PERFORM cs_ore_64_8_v1('{"o": []}'::jsonb);
1818

1919
BEGIN
2020
PERFORM cs_ore_64_8_v1('{}'::jsonb);
@@ -76,4 +76,3 @@ DO $$
7676
END;
7777
END;
7878
$$ LANGUAGE plpgsql;
79-

0 commit comments

Comments
 (0)