pg.ddx.io  pgsql-hackers@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Heikki Linnakangas <heikki.linnakangas@enterprisedb.com>
Cc: PostgreSQL-development <pgsql-hackers@postgresql.org>
Subject: Re: Nested CASE-WHEN scoping
Date: Wed, 25 May 2011 17:31:50 -0400
Message-ID: <27772.1306359110@sss.pgh.pa.us> (raw)
In-Reply-To: <4DDD35B6.2050203@enterprisedb.com>
References: <4DDCEEB8.50602@enterprisedb.com>
	<5487.1306334876@sss.pgh.pa.us>
	<4DDD35B6.2050203@enterprisedb.com>

Heikki Linnakangas <heikki.linnakangas@enterprisedb.com> writes:
> On 25.05.2011 17:47, Tom Lane wrote:
>> [ scratches head ... ]  Why does the save/restore in ExecEvalCase not
>> take care of this?

> The mistake happens during planning, when the SQL function is inlined 
> and pre-evaluated.

OK, I see the problem now: we have a CaseTestExpr in the arguments of
the function, which we are unable to reduce to a constant, so it gets
substituted as-is into the body of the function during inlining.  And
then it's physically inside the CASE expression that's in the function
body, so it looks like it syntactically belongs to that expression,
which it doesn't.  You're probably right that this is impractical to fix
without redesigning the expression representation, and that
CoerceToDomainValue has got a similar issue.

My advice is to not change the parser output representation, though.
What I think you ought to do about this is to have the planner replace 
CaseTestExpr and CoerceToDomainValue with PARAM_EXEC Params during
expression preprocessing, and assign suitable Param numbers which it
sticks into the CaseExpr (resp CoerceToDomainExpr) so that those
expressions know which Param slot to fill at runtime.  The
const-simplification logic can avoid getting dumber by treating the
cases like known-Param-value cases.  I don't think you need to invent
something separate from the PARAM_EXEC infrastructure to handle these.

The main annoyance here is that standalone expressions may now need
Param slots to execute, which will complicate places that need to
evaluate them, but there's probably no way around that (a bespoke
mechanism would complicate those callers just as much, if the number
of value slots it needs is variable, which it will be).

> BTW, i just stumbled into this:

> postgres=# explain verbose SELECT CASE now() WHEN (29+random()::int4) 
> THEN 'foo' ELSE 'bar' END;
> ERROR:  unexpected CASE WHEN clause: 326

> Looks like ruleutils.c is also not prepared for the case that the 
> implicit equality operation gets inlined into something else than an OpExpr.

Grumble ... I thought we'd fixed that ...

			regards, tom lane



view thread (15+ messages)  latest in thread

Message-ID: <27772.1306359110@sss.pgh.pa.us>
Permalink:  ../27772.1306359110@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/27772.1306359110@sss.pgh.pa.us

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-hackers@postgresql.org
  Cc: tgl@sss.pgh.pa.us, heikki.linnakangas@enterprisedb.com
  Subject: Re: Nested CASE-WHEN scoping
  In-Reply-To: <27772.1306359110@sss.pgh.pa.us>
  References: <4DDCEEB8.50602@enterprisedb.com> <5487.1306334876@sss.pgh.pa.us> <4DDD35B6.2050203@enterprisedb.com> <27772.1306359110@sss.pgh.pa.us>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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