Skip to content

Commit 63c4637

Browse files
committed
Closer, but order is inverted (ASC is DESC etc)
1 parent 134e2a6 commit 63c4637

File tree

2 files changed

+26
-12
lines changed

2 files changed

+26
-12
lines changed

sql/000-ore.sql

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,21 @@ CREATE TYPE ore_64_8_v1 AS (
88
terms ore_64_8_v1_term[]
99
);
1010

11+
DROP FUNCTION IF EXISTS cs_cast_ore_64_8_v1_term_to_bytea(t ore_64_8_v1_term);
12+
13+
CREATE FUNCTION cs_cast_ore_64_8_v1_term_to_bytea(t ore_64_8_v1_term)
14+
RETURNS bytea
15+
LANGUAGE sql IMMUTABLE STRICT PARALLEL SAFE
16+
BEGIN ATOMIC
17+
RETURN t.bytes;
18+
END;
19+
20+
DROP CAST IF EXISTS (ore_64_8_v1_term AS bytea);
21+
22+
CREATE CAST (ore_64_8_v1_term AS bytea)
23+
WITH FUNCTION cs_cast_ore_64_8_v1_term_to_bytea(ore_64_8_v1_term) AS IMPLICIT;
24+
25+
1126
DROP FUNCTION IF EXISTS compare_ore_64_8_v1_term(a ore_64_8_v1_term, b ore_64_8_v1_term);
1227
DROP FUNCTION IF EXISTS compare_ore_64_8_v1_term(a bytea, b bytea);
1328

@@ -239,14 +254,15 @@ CREATE OPERATOR CLASS ore_64_8_v1_term_btree_ops DEFAULT FOR TYPE ore_64_8_v1_te
239254
-- doesn't always make sense but it's here for completeness.
240255
-- If both are non-empty, we compare the first element. If they are equal
241256
-- we need to consider the next block so we recurse, otherwise we return the comparison result.
242-
DROP FUNCTION IF EXISTS compare_ore_array(a ore_64_8_v1_term[], b ore_64_8_v1_term[]);
257+
-- DROP FUNCTION IF EXISTS compare_ore_array(a ore_64_8_v1_term[], b ore_64_8_v1_term[]);
243258

244-
CREATE FUNCTION compare_ore_array(a ore_64_8_v1_term[], b ore_64_8_v1_term[])
245-
RETURNS integer AS $$
246-
BEGIN
247-
SELECT compare_ore_array(a, b);
248-
END
249-
$$ LANGUAGE plpgsql;
259+
-- CREATE FUNCTION compare_ore_array(a ore_64_8_v1_term[], b ore_64_8_v1_term[])
260+
-- RETURNS integer AS $$
261+
-- BEGIN
262+
-- -- SELECT compare_ore_array(a, b);
263+
-- SELECT compare_ore_array(CAST(a AS bytea[]), CAST(b AS bytea[]));
264+
-- END
265+
-- $$ LANGUAGE plpgsql;
250266

251267
CREATE FUNCTION compare_ore_array(a bytea[], b bytea[])
252268
RETURNS integer AS $$
@@ -282,7 +298,7 @@ RETURNS integer AS $$
282298
cmp_result integer;
283299
BEGIN
284300
-- Recursively compare blocks bailing as soon as we can make a decision
285-
RETURN compare_ore_array(a.terms, b.terms);
301+
RETURN compare_ore_array(a.terms::bytea[], b.terms::bytea[]);
286302
END
287303
$$ LANGUAGE plpgsql;
288304

tests/operators-ore-order.sql

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,11 @@ INSERT INTO users (name_encrypted) VALUES (
4545
);
4646

4747

48-
4948
-- SELECT id FROM users WHERE cs_ore_64_8_v1(name_encrypted) > cs_ore_64_8_v1('{"o":"(\"{\"\"(\\\\\"\"\\\\\\\\\\\\\\\\x12121212121259bfe28282d03415e7714fccd69eb7eb476c70743e485e20331f59cbc1c848dcdeda716f351eb20588c406a7df5fb8917ebf816739aa1414ac3b8498e493bf0badea5c9fdb3cc34da8b152b995957591880c523beb1d3f12487c38d18f62dd26209a727674e5a5fe3a3e3037860839afd801ff4a28b714e4cde8df10625dce72602fdbdcc53d515857f1119f5912804ce09c6cf6c2d37393a27a465134523b512664582f834e15003b7216cb668480bc3e7d1c069f2572ece7c848b9eb9a28b4e62bfc2b97c93e61b2054154e621c5bbb7bed37de3d7c343bd3dbcf7b4af20128c961351bf55910a855f08a8587c2059a5f05ca8d7a082e695b3dd4ff3ce86694d4fe98972220eea1ab90f5de493ef3a502b74a569f103ee2897ebc9ae9b16a17e7be67415ee830519beb3058ffc1c1eb0e574d66c8b365919f27eb00aa7bce475d7bdaad4ed800f8fc3d626e0eb842e312b0cc22a1ccf89847ebb2cd0a6e18aec21bd2deeec1c47301fc687f7f764bb882b50f553c246a6da5816b78b3530119ea68b08a8403a90e063e58502670563bd4d\\\\\"\")\"\"}\")"}');
5049

51-
SELECT id, name_encrypted FROM users ORDER BY name_encrypted DESC;
52-
50+
SELECT id, name_encrypted FROM users ORDER BY cs_ore_64_8_v1(name_encrypted) ASC;
5351

54-
-- SELECT id, name_encrypted FROM users ORDER BY name_encrypted ASC;
52+
SELECT id, name_encrypted FROM users ORDER BY name_encrypted;
5553

5654
-- SELECT id FROM users WHERE name_encrypted::cs_encrypted_v1 > '{
5755
-- "v": 1,

0 commit comments

Comments
 (0)