Skip to content

Parse ORE indexes as JSON arrays of hex-encoded strings #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion sql/011-core-functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,28 @@ DROP CAST IF EXISTS (text AS ore_64_8_v1_term);
CREATE CAST (text AS ore_64_8_v1_term)
WITH FUNCTION _cs_text_to_ore_64_8_v1_term_v1_0(text) AS IMPLICIT;

DROP FUNCTION IF EXISTS jsonb_array_to_ore_64_8_v1(val jsonb);

-- Casts a jsonb array of hex-encoded strings to the `ore_64_8_v1` composite type.
-- In other words, this function takes the ORE index format sent through in the
-- EQL payload from Proxy and decodes it as the composite type that we use for
-- ORE operations on the Postgres side.
CREATE FUNCTION jsonb_array_to_ore_64_8_v1(val jsonb)
RETURNS ore_64_8_v1 AS $$
DECLARE
terms_arr ore_64_8_v1_term[];
BEGIN
IF jsonb_typeof(val) = 'null' THEN
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL

RETURN NULL;
END IF;

SELECT array_agg(ROW(decode(value::text, 'hex'))::ore_64_8_v1_term)
INTO terms_arr
FROM jsonb_array_elements_text(val) AS value;

RETURN ROW(terms_arr)::ore_64_8_v1;
END;
$$ LANGUAGE plpgsql;

-- extracts ore index from an encrypted column
DROP FUNCTION IF EXISTS cs_ore_64_8_v1_v0_0(val jsonb);
Expand All @@ -166,7 +188,7 @@ CREATE FUNCTION cs_ore_64_8_v1_v0_0(val jsonb)
AS $$
BEGIN
IF val ? 'o' THEN
RETURN (val->>'o')::ore_64_8_v1;
RETURN jsonb_array_to_ore_64_8_v1(val->'o');
END IF;
RAISE 'Expected an ore index (o) value in json: %', val;
END;
Expand Down
5 changes: 2 additions & 3 deletions tests/core-functions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ DO $$
ASSERT (SELECT EXISTS (SELECT cs_unique_v1('{"u": "u"}'::jsonb)));
ASSERT (SELECT EXISTS (SELECT cs_match_v1('{"m": []}'::jsonb)));
ASSERT (SELECT EXISTS (SELECT cs_ste_vec_v1('{"sv": [[]]}'::jsonb)));
ASSERT (SELECT EXISTS (SELECT cs_ore_64_8_v1('{"o": "()"}'::jsonb)));
ASSERT (SELECT EXISTS (SELECT cs_ore_64_8_v1('{"o": []}'::jsonb)));

END;
$$ LANGUAGE plpgsql;

DO $$
BEGIN
-- sanity check
PERFORM cs_ore_64_8_v1('{"o": "()"}'::jsonb);
PERFORM cs_ore_64_8_v1('{"o": []}'::jsonb);

BEGIN
PERFORM cs_ore_64_8_v1('{}'::jsonb);
Expand Down Expand Up @@ -76,4 +76,3 @@ DO $$
END;
END;
$$ LANGUAGE plpgsql;

Loading