pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
PL/pgSQL THEN binging in condition
3+ messages / 3 participants
[nested] [flat]

* PL/pgSQL THEN binging in condition
@ 2024-09-17 18:03  PetSerAl <petseral@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: PetSerAl @ 2024-09-17 18:03 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org

postgres=# SELECT version();
                          version
------------------------------------------------------------
 PostgreSQL 16.4, compiled by Visual C++ build 1940, 64-bit
(1 row)


postgres=# DO $$
postgres$#   BEGIN
postgres$#     IF
postgres$#       CASE
postgres$#         WHEN TRUE
postgres$#         THEN TRUE
postgres$#       END
postgres$#     THEN
postgres$#       NULL;
postgres$#     END IF;
postgres$#   END
postgres$# $$;
ERROR:  syntax error at end of input
LINE 5:         WHEN TRUE
                         ^

It seems error here because first THEN bound to IF statement rather than
CASE expression. Workaround here would be using parenthesis:

postgres=# DO $$
postgres$#   BEGIN
postgres$#     IF
postgres$#       (CASE
postgres$#         WHEN TRUE
postgres$#         THEN TRUE
postgres$#       END)
postgres$#     THEN
postgres$#       NULL;
postgres$#     END IF;
postgres$#   END
postgres$# $$;
DO

If behavior can not be changed for compatibility reasons, it at least can
use better error message.

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: PL/pgSQL THEN binging in condition
@ 2024-09-17 18:45  Pavel Stehule <pavel.stehule@gmail.com>
  parent: PetSerAl <petseral@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Pavel Stehule @ 2024-09-17 18:45 UTC (permalink / raw)
  To: PetSerAl <petseral@gmail.com>; +Cc: pgsql-bugs@lists.postgresql.org

Hi

út 17. 9. 2024 v 20:04 odesílatel PetSerAl <petseral@gmail.com> napsal:

> postgres=# SELECT version();
>                           version
> ------------------------------------------------------------
>  PostgreSQL 16.4, compiled by Visual C++ build 1940, 64-bit
> (1 row)
>
>
> postgres=# DO $$
> postgres$#   BEGIN
> postgres$#     IF
> postgres$#       CASE
> postgres$#         WHEN TRUE
> postgres$#         THEN TRUE
> postgres$#       END
> postgres$#     THEN
> postgres$#       NULL;
> postgres$#     END IF;
> postgres$#   END
> postgres$# $$;
> ERROR:  syntax error at end of input
> LINE 5:         WHEN TRUE
>                          ^
>
> It seems error here because first THEN bound to IF statement rather than
> CASE expression. Workaround here would be using parenthesis:
>
> postgres=# DO $$
> postgres$#   BEGIN
> postgres$#     IF
> postgres$#       (CASE
> postgres$#         WHEN TRUE
> postgres$#         THEN TRUE
> postgres$#       END)
> postgres$#     THEN
> postgres$#       NULL;
> postgres$#     END IF;
> postgres$#   END
> postgres$# $$;
> DO
>
> If behavior can not be changed for compatibility reasons, it at least can
> use better error message.
>

It is an interesting use case, but I am afraid it can be fixed (enhanced)
without a strong rewrite of the plpgsql engine.

plpgsql parser knows nothing about embedded SQL expressions. It just tries
to cut the string between IF and THEN, and in this case, it should be
confused.

This is a feature - it is the cost of a very simple plpgsql parser, that
doesn't support expressions explicitly.

Regards

Pavel

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: PL/pgSQL THEN binging in condition
@ 2024-09-17 20:00  Tom Lane <tgl@sss.pgh.pa.us>
  parent: Pavel Stehule <pavel.stehule@gmail.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Tom Lane @ 2024-09-17 20:00 UTC (permalink / raw)
  To: Pavel Stehule <pavel.stehule@gmail.com>; +Cc: PetSerAl <petseral@gmail.com>; pgsql-bugs@lists.postgresql.org

Pavel Stehule <pavel.stehule@gmail.com> writes:
> út 17. 9. 2024 v 20:04 odesílatel PetSerAl <petseral@gmail.com> napsal:
>> It seems error here because first THEN bound to IF statement rather than
>> CASE expression.

> It is an interesting use case, but I am afraid it can be fixed (enhanced)
> without a strong rewrite of the plpgsql engine.

It would not be that hard to teach read_sql_construct to count nesting
of CASE ... END, as it already does for parentheses.  However, I'm a
little worried whether that'd interact badly with plpgsql's own uses
of CASE and END.

			regards, tom lane





^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2024-09-17 20:00 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2024-09-17 18:03 PL/pgSQL THEN binging in condition PetSerAl <petseral@gmail.com>
2024-09-17 18:45 ` Pavel Stehule <pavel.stehule@gmail.com>
2024-09-17 20:00   ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox