Skip to content

Commit e63cb56

Browse files
committed
fix: correctly cast and handle nulls for order ops
1 parent 3a3a22b commit e63cb56

File tree

4 files changed

+101
-1
lines changed

4 files changed

+101
-1
lines changed

src/encrypted/casts.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
CREATE FUNCTION eql_v2.to_encrypted(data jsonb)
1010
RETURNS public.eql_v2_encrypted AS $$
1111
BEGIN
12+
IF data IS NULL THEN
13+
RETURN NULL;
14+
END IF;
15+
1216
RETURN ROW(data)::public.eql_v2_encrypted;
1317
END;
1418
$$ LANGUAGE plpgsql;
@@ -28,6 +32,10 @@ CREATE CAST (jsonb AS public.eql_v2_encrypted)
2832
CREATE FUNCTION eql_v2.to_encrypted(data text)
2933
RETURNS public.eql_v2_encrypted AS $$
3034
BEGIN
35+
IF data IS NULL THEN
36+
RETURN NULL;
37+
END IF;
38+
3139
RETURN ROW(data::jsonb)::public.eql_v2_encrypted;
3240
END;
3341
$$ LANGUAGE plpgsql;
@@ -48,6 +56,10 @@ CREATE CAST (text AS public.eql_v2_encrypted)
4856
CREATE FUNCTION eql_v2.to_jsonb(e public.eql_v2_encrypted)
4957
RETURNS jsonb AS $$
5058
BEGIN
59+
IF e IS NULL THEN
60+
RETURN NULL;
61+
END IF;
62+
5163
RETURN e.data;
5264
END;
5365
$$ LANGUAGE plpgsql;

src/operators/operator_class.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,18 @@ AS $$
2222
a_ore := eql_v2.ore_block_u64_8_256(a);
2323
b_ore := eql_v2.ore_block_u64_8_256(b);
2424

25+
IF a_ore IS NULL AND b_ore IS NULL THEN
26+
RETURN 0;
27+
END IF;
28+
29+
IF a_ore IS NULL THEN
30+
RETURN -1;
31+
END IF;
32+
33+
IF b_ore IS NULL THEN
34+
RETURN 1;
35+
END IF;
36+
2537
RETURN eql_v2.compare_ore_array(a_ore.terms, b_ore.terms);
2638
END;
2739
$$ LANGUAGE plpgsql;

src/operators/order_by_test.sql

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,76 @@ DECLARE
7272
'43');
7373

7474
END;
75-
$$ LANGUAGE plpgsql;
75+
$$ LANGUAGE plpgsql;
76+
77+
78+
SELECT create_table_with_encrypted();
79+
80+
--
81+
-- ORE - ORDER BY NULL handling
82+
--
83+
DO $$
84+
DECLARE
85+
e eql_v2_encrypted;
86+
ore_term eql_v2_encrypted;
87+
BEGIN
88+
89+
-- Insert records with NULL values
90+
-- record with ID=1 and e=NULL
91+
INSERT INTO encrypted(e) VALUES (NULL::jsonb::eql_v2_encrypted);
92+
93+
94+
-- Pull records from the ore table and insert
95+
SELECT ore.e FROM ore WHERE id = 42 INTO ore_term;
96+
-- record with ID=2 and e=42
97+
INSERT INTO encrypted(e) VALUES (ore_term);
98+
99+
100+
SELECT ore.e FROM ore WHERE id = 3 INTO ore_term;
101+
-- record with ID=3 and e=3
102+
INSERT INTO encrypted(e) VALUES (ore_term);
103+
104+
105+
-- record with ID=4 and e=NULL
106+
INSERT INTO encrypted(e) VALUES (NULL::jsonb::eql_v2_encrypted);
107+
108+
PERFORM assert_result(
109+
'ORDER BY encrypted',
110+
format('SELECT id FROM encrypted ORDER BY e'),
111+
'3');
112+
113+
PERFORM assert_result(
114+
'ORDER BY encrypted ASC',
115+
format('SELECT id FROM encrypted ORDER BY e ASC'),
116+
'3');
117+
118+
PERFORM assert_result(
119+
'ORDER BY eql_v2.order_by(e) ASC NULLS FIRST',
120+
format('SELECT id FROM encrypted ORDER BY e ASC NULLS FIRST'),
121+
'1');
122+
123+
PERFORM assert_result(
124+
'ORDER BY eql_v2.order_by(e) ASC NULLS LAST',
125+
format('SELECT id FROM encrypted ORDER BY e ASC NULLS LAST'),
126+
'3');
127+
128+
-- NULLS FIRST when DESC
129+
PERFORM assert_result(
130+
'ORDER BY encrypted DESC',
131+
format('SELECT id FROM encrypted ORDER BY e DESC'),
132+
'1');
133+
134+
PERFORM assert_result(
135+
'ORDER BY eql_v2.order_by(e) DESC NULLS FIRST',
136+
format('SELECT id FROM encrypted ORDER BY e DESC NULLS FIRST'),
137+
'1');
138+
139+
PERFORM assert_result(
140+
'ORDER BY eql_v2.order_by(e) DESC NULLS LAST',
141+
format('SELECT id FROM encrypted ORDER BY e DESC NULLS LAST'),
142+
'2');
143+
144+
END;
145+
$$ LANGUAGE plpgsql;
146+
147+
PERFORM drop_table_with_encrypted();

src/ore_block_u64_8_256/functions.sql

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,10 @@ CREATE FUNCTION eql_v2.ore_block_u64_8_256(val jsonb)
5454
IMMUTABLE STRICT PARALLEL SAFE
5555
AS $$
5656
BEGIN
57+
IF val IS NULL THEN
58+
RETURN NULL;
59+
END IF;
60+
5761
IF val ? 'ob' THEN
5862
RETURN eql_v2.jsonb_array_to_ore_block_u64_8_256(val->'ob');
5963
END IF;

0 commit comments

Comments
 (0)