Subject: rewrite rule

From: Peter van Oosterom <[email-masked]>
Date: Thu, 11 Nov 93 15:38:44 MET

Dear Postgres-group,

There seams to be an error in the "rewrite rule" system.
Consider the following example; create, fill, and inspect a relation:
  create rel(a=text, b=int4)\g
  append rel(a="AAB", b=10)\g
  append rel(a="HLP", b=3)\g
  retrieve (rel.all)\g
This gives:
  -----------------------------
  | a           | b           |
  -----------------------------
  | HLP         | 3           |
  -----------------------------
  | AAB         | 10          |
  -----------------------------
Now define a rule to change "AAB" into "Y" and inspect the relation again:
  define rewrite rule r1 is
    on retrieve to rel.a where current.a = "AAB"
    do instead retrieve (a = "Y")\g
  retrieve (rel.all)\g
This gives:
  -----------------------------
  | a           | b           |
  -----------------------------
  | Y           | 3           |
  -----------------------------
  | Y           | 10          |
  -----------------------------
Note that "HLP" is also changed into "Y", which should not be the case.
More testing reveals that every string is converted to "Y".
However, using an "instance rule" instead of a "rewrite rule" works fine:
  remove rewrite rule r1\g
  define instance rule r2 is
    on retrieve to rel.a where current.a = "AAB"
    do instead retrieve (a = "Y")\g
  retrieve (rel.all)\g
This gives:
  -----------------------------
  | a           | b           |
  -----------------------------
  | HLP         | 3           |
  -----------------------------
  | Y           | 10          |
  -----------------------------
This is the right result.
Can somebody tell me what is wrong with the rewrite rule (or perhaps
what did I do wrong with the rewrite rule)?

Kind regards, Peter van Oosterom ([email-masked])

---

From: [email-masked] (Paul M. Aoki)
Date: Fri, 12 Nov 93 01:42:16 -0800

>     do instead retrieve (a = "Y")\g

the manual says (in its usual crystal-clear prose):

     When choosing between the rewrite and instance rule systems for a par-
     ticular rule application, remember that in the rewrite system current
     refers to a relation and some qualifiers whereas in the instance system
     it refers to an instance (tuple).

in the rewrite system, this effectively means that the query
is rewritten as	
	retrieve (a = "Y") from current in rel where current.a = "AAB"
and since there is at least one element in rel that contains AAB, the
qualification is always true! (i.e., this is an existential query).
hence, "Y" is retrieved in place of every rel.a...
--
  Paul M. Aoki  |  CS Div., Dept. of EECS, UCB  |  [email-masked]
                |  Berkeley, CA 94720           |  ...!uunet!ucbvax!aoki