Skip to content

Commit

Permalink
Fix segfault when using certain DO block in function (#7554)
Browse files Browse the repository at this point in the history
When using a CASE WHEN expression in the body
of the function that is used in the DO block, a segmentation
fault occured. This fixes that.

Fixes #7381

---------

Co-authored-by: Konstantin Morozov <vzbdryn@yahoo.com>
  • Loading branch information
copetol and Konstantin Morozov committed Mar 8, 2024
1 parent f0043b6 commit 12f5643
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/backend/distributed/planner/function_call_delegation.c
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,10 @@ bool InDelegatedFunctionCall = false;
static bool
contain_param_walker(Node *node, void *context)
{
if (node == NULL)
{
return false;
}
if (IsA(node, Param))
{
Param *paramNode = (Param *) node;
Expand Down
32 changes: 32 additions & 0 deletions src/test/regress/expected/function_with_case_when.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
CREATE SCHEMA function_with_case;
SET search_path TO function_with_case;
-- create function
CREATE OR REPLACE FUNCTION test_err(v1 text)
RETURNS text
LANGUAGE plpgsql
SECURITY DEFINER
AS $function$

begin
return v1 || ' - ok';
END;
$function$;
do $$ declare
lNewValues text;
val text;
begin
val = 'test';
lNewValues = test_err(v1 => case when val::text = 'test'::text then 'yes' else 'no' end);
raise notice 'lNewValues= %', lNewValues;
end;$$ ;
NOTICE: lNewValues= yes - ok
CONTEXT: PL/pgSQL function inline_code_block line XX at RAISE
-- call function
SELECT test_err('test');
test_err
---------------------------------------------------------------------
test - ok
(1 row)

DROP SCHEMA function_with_case CASCADE;
NOTICE: drop cascades to function test_err(text)
1 change: 1 addition & 0 deletions src/test/regress/multi_schedule
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ test: run_command_on_all_nodes
test: background_task_queue_monitor
test: other_databases grant_role_from_non_maindb role_operations_from_non_maindb seclabel_non_maindb
test: citus_internal_access
test: function_with_case_when

# Causal clock test
test: clock
Expand Down
27 changes: 27 additions & 0 deletions src/test/regress/sql/function_with_case_when.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
CREATE SCHEMA function_with_case;
SET search_path TO function_with_case;

-- create function
CREATE OR REPLACE FUNCTION test_err(v1 text)
RETURNS text
LANGUAGE plpgsql
SECURITY DEFINER
AS $function$

begin
return v1 || ' - ok';
END;
$function$;
do $$ declare
lNewValues text;
val text;
begin
val = 'test';
lNewValues = test_err(v1 => case when val::text = 'test'::text then 'yes' else 'no' end);
raise notice 'lNewValues= %', lNewValues;
end;$$ ;

-- call function
SELECT test_err('test');

DROP SCHEMA function_with_case CASCADE;

0 comments on commit 12f5643

Please sign in to comment.