Return-Path: postarch
Received: by postgres.Berkeley.EDU (5.61/1.29)
	id AA23975; Mon, 4 May 92 15:26:47 -0700
Message-Id: <9205042226.AA23975@postgres.Berkeley.EDU>
From: postarch (Postgres Mailing Archive)
Subject: Re: Trouble with rules
To: postgres@postgres.berkeley.edu
Sender: pg_adm@postgres.berkeley.edu
In-Reply-To: Your message of "Thu, 30 Apr 92 02:24:37 PDT."
             <9204300924.AA29963@postgres.Berkeley.EDU> 
Date: Mon, 04 May 92 15:25:06 PDT

you write:

> I am having some problems in using rules.   The first problem concerns 
> a difference between  instance and rewrite rules.  The second is in 
> interpreting the result of a rule.   I would be grateful for advice.
> 
> * retrieve (emp.all) \g
> 
> Query sent to backend is "retrieve (emp.all) "
> -----------------------------------------------------------------------
> | salary      | manager     | location    | age         | name        |
> -----------------------------------------------------------------------
> | 1000        | sharon      | (11,10)     | 20          | bill        |
> -----------------------------------------------------------------------
> | 1000        | sam         | (15,12)     | 25          | joe         |
> -----------------------------------------------------------------------
> | 80          | bill        | (10,5)      | 30          | sam         |
> -----------------------------------------------------------------------
> 
> THE REWRITE RULE USED IS 

>    define rewrite rule q3rule_1 is
>       on append to emp
>    do instead
>       retrieve (e.all)
>         from e in emp 
>         where e.name = new.manager
> 
>  
> I would expect the following APPEND query to act like
>     retrieve (emp.all) where e.name = "x"
> and produce no instances - however I get 2 which seem to fit the query    
>     retrieve (e.all) from e in emp   where e.name = emp.manager
> it seems that 'new' is the whole emp table and not just the instance
> in the APPEND query.

> THE FOLLOWING QUERY DOES THE SAME THING.
> 
> * append emp(name="a", manager="sam")  \g
> 

As near as I can tell this is a bug in the rewrite system.  A slight
modification to your above rule works in the instance level system
(see below).

> 2. The rule was changed from a rewrite to an instance rule and 
>     caused retrieve to be rejected with a warning about parameters
>     as below.   Removing the rule allowed retrieve to work again.
> --------------------------------
> define rule q3rule_1 is
> on append to emp
> do instead
> retrieve (e.all)
>   from e in emp 
>   where e.name = new.manager
> * \g
> 
> retrieve (emp.all) \g
> 
> Query sent to backend is "retrieve (emp.all) "
> WARN:Apr 16 18:06:14:View Rules must not have parameters!

[ This from Spyros, The "Official" Postgres Rules personality ]

a) "on append .... do retrieve" rules are not allowed.
In general, a "retrieve" can appear in the action of a rule ONLY if the
event that triggers the rule is a "retrieve" too.
(exception: a "retrieve into <relation>" can appear in the action of
any rule).

[ I tried to run the exception case and ran into several problems, which
  I spent some time fixing.  I don't think it will work in 3.1, but you
  can try it. -Jeff ]

b) yes, there is a bug! The system should have complained when the rule
was defined (instead of waiting till you actually fire the rule)

Spyros.
