Skip to content

Commit

Permalink
Make the subscript fetch call strict to resolve issue #5
Browse files Browse the repository at this point in the history
  • Loading branch information
jim-mlodgenski committed Jan 27, 2025
1 parent c328526 commit b735f60
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/collection_subs.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ collection_subscript_handler(PG_FUNCTION_ARGS)
static const SubscriptRoutines sbsroutines = {
.transform = collection_subscript_transform,
.exec_setup = collection_exec_setup,
.fetch_strict = false, /* fetch returns NULL for NULL inputs */
.fetch_strict = true, /* fetch returns NULL for NULL inputs */
.fetch_leakproof = true, /* fetch returns NULL for bad subscript */
.store_leakproof = false /* ... but assignment throws error */
};
Expand Down
20 changes: 16 additions & 4 deletions test/expected/subscript.out
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,13 @@ DO $$
DECLARE
u collection('text');
BEGIN
RAISE NOTICE 'Test 10';
RAISE NOTICE 'Subscript test 10';
u := add(u, 'x1', 'Hello World'::text);
u := add(u, 'y3', '12-31-1999'::date);
RAISE NOTICE 'count: %', count(u);
END
$$;
NOTICE: Test 10
NOTICE: Subscript test 10
ERROR: Incompatible value data type
DETAIL: Expecting text, but received date
CONTEXT: PL/pgSQL function inline_code_block line 7 at assignment
Expand All @@ -148,7 +148,7 @@ DECLARE
child1 collection('text');
child2 collection('text');
BEGIN
RAISE NOTICE 'Test 11';
RAISE NOTICE 'Subscript test 11';

child1['aaa'] := 'Hello World';
child1['bbb'] := 'Hello All';
Expand All @@ -162,5 +162,17 @@ BEGIN
RAISE NOTICE 'Parent: %', parent;
END
$$;
NOTICE: Test 11
NOTICE: Subscript test 11
NOTICE: Parent: {"value_type": "public.collection", "entries": {"child1": "{\"value_type\": \"pg_catalog.text\", \"entries\": {\"aaa\": \"Hello World\", \"bbb\": \"Hello All\"}}", "child2": "{\"value_type\": \"pg_catalog.text\", \"entries\": {\"AAA\": \"Hallo Welt\", \"BBB\": \"Hola Mundo\"}}"}}
DO
$$
DECLARE
t collection;
BEGIN
RAISE NOTICE 'Subscript test 12';

RAISE NOTICE 'The current val is %', t['2'];
END
$$;
NOTICE: Subscript test 12
NOTICE: The current val is <NULL>
15 changes: 13 additions & 2 deletions test/sql/subscript.sql
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ DO $$
DECLARE
u collection('text');
BEGIN
RAISE NOTICE 'Test 10';
RAISE NOTICE 'Subscript test 10';
u := add(u, 'x1', 'Hello World'::text);
u := add(u, 'y3', '12-31-1999'::date);
RAISE NOTICE 'count: %', count(u);
Expand All @@ -133,7 +133,7 @@ DECLARE
child1 collection('text');
child2 collection('text');
BEGIN
RAISE NOTICE 'Test 11';
RAISE NOTICE 'Subscript test 11';

child1['aaa'] := 'Hello World';
child1['bbb'] := 'Hello All';
Expand All @@ -147,3 +147,14 @@ BEGIN
RAISE NOTICE 'Parent: %', parent;
END
$$;

DO
$$
DECLARE
t collection;
BEGIN
RAISE NOTICE 'Subscript test 12';

RAISE NOTICE 'The current val is %', t['2'];
END
$$;

0 comments on commit b735f60

Please sign in to comment.