Re: plpgsql's case bug?

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Jaime Casanova <jcasanov(at)systemguards(dot)com(dot)ec>
Cc: PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: plpgsql's case bug?
Date: 2010-03-28 03:44:56
Message-ID: 603c8f071003272044k47875b0eu4b53615f799413e9@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sat, Mar 27, 2010 at 10:08 PM, Jaime Casanova
<jcasanov(at)systemguards(dot)com(dot)ec> wrote:
> I found something strange (at least for my limited view).
>
> in pl_exec.c:1549 in the function exec_stmt_case() we have this:
>
> """
>    /* SQL2003 mandates this error if there was no ELSE clause */
>    if (!stmt->have_else)
>        ereport(ERROR,
>                (errcode(ERRCODE_CASE_NOT_FOUND),
>                 errmsg("case not found"),
>                 errhint("CASE statement is missing ELSE part.")));
> """
>
> resulting in this function to fail when i try to execute it
>
> """
> postgres=# create or replace function test_case() returns boolean as $$
> declare
>        v_one integer = 1;
>        v_two boolean = false;
> begin
>        case when v_one is null then
>       v_two = true;
>        end case;
>        return v_two;
> end
> $$ language plpgsql;
> CREATE FUNCTION
>
> postgres=# select test_case();
> ERROR:  case not found
> HINT:  CASE statement is missing ELSE part.
> CONTEXT:  PL/pgSQL function "test_case" line 5 at CASE
> """
>
> this is based on General Rules case 1 of chapter 13.6 (case statement)
> but i don't think that behaviour is the meaning of that Rule. Even if
> it is, the exception should be: "case not found for case statement"
> and not just "case not found"
>
> """
> General Rules
> 1) Case:
>   a) If the <search condition> of some <searched case statement when
> clause> in a <case statement> is
>      True, then let SL be the <SQL statement list> of the first
> (leftmost) <searched case statement when
>      clause> whose <search condition> is True.
>   b) If the <case statement> simply contains a <case statement else
> clause>, then let SL be the <SQL statement
>      list> of that <case statement else clause>.
>   c) Otherwise, an exception condition is raised: case not found for
> case statement, and the execution of
>      the <case statement> is terminated immediately.
> """
>
> comments?

It seems odd to require a dummy ELSE clause that does nothing just to
avoid having an exception thrown, but I'm not sure what else to make
of the quoted portion of the spec. What do you think it's saying?

...Robert

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2010-03-28 04:19:39 Re: join removal
Previous Message Jaime Casanova 2010-03-28 02:14:10 Re: plpgsql's case bug?