Subject: Alias Functions

From: "Richard M. Bennema" <[email-masked]>
Date: Wed, 10 Nov 1993 20:24:23 -0600 (CST)

My database class is using Postgres for the first time as an example database.

We are working are way through the User Manual and have run into a snag.
Section 7.1 on page 10 describes defining functions to be used as "aliases" for
commonly-used queries.  We have had no problem defining the function, but we
haven't been able to make it very useful.

Here is the example from the manual and what we've tried:
>> Script started on Wed Nov 10 20:10:10 1993
>> csh> cat emp_file
>> create EMP (name = text, salary = int4, age = int4, dept = char16)
>> 
>> append EMP (name = "Joe", salary = 1400, age = 40, dept = "shoe")
>> append EMP (name = "Sam", salary = 1200, age = 29, dept = "toy")
>> append EMP (name = "Bill", salary = 1600, age = 36, dept = "candy")
>> 
>> define function high_pay (language = "postquel", returntype = setof EMP)
>>     as "retrieve (EMP.all) where EMP.salary > 1500"
>> 
>> csh> monitor rich
>> Welcome to the C POSTGRES terminal monitor
>> 
>> Go 
>> * retrieve (this_works = name(high_pay())) \g
>> 
>> Query sent to backend is "retrieve (this_works = name(high_pay())) "
>> ---------------
>> | this_works  |
>> ---------------
>> | Bill        |
>> ---------------
>> 
>> Go 
>> * retrieve (this_doesnt = all(high_pay())) \g
>> 
>> Query sent to backend is "retrieve (this_doesnt = all(high_pay())) "
>> WARN:Nov 10 20:12:09:parser: syntax error at or near "all"
>> 
>> 
>> Go 
>> * high_pay() \g
>> 
>> Query sent to backend is "high_pay() "
>> WARN:Nov 10 20:12:17:parser: syntax error at or near "high_pay"
>> 
>> 
>> Go 
>> * \q
>> I live to serve you.
>> csh> 
>> script done on Wed Nov 10 20:12:25 1993
Is there a way to call an alias function without having to type out a retrieve
statement every time?  If there is not, is there any way to get multiple columns
returned instead of only one?  Any help would be appreciated.

					Thanks,

					Richard M. Bennema
-- 
[email-masked]
Richard M. Bennema		"The brain can be seen as a complex machine,
Computer Science/Math		 like a gooey computer."
Wheaton College 			-Robert C. Solomon

---

From: [email-masked] (Paul M. Aoki)
Date: Wed, 10 Nov 93 23:06:20 -0800

> haven't been able to make it very useful.

i think it's safe to say that the manual description is bogus.
functions are not supposed to be aliases in the same sense as shell
aliases.. they do not "extend" the syntax by some kind of preprocessing.
think of them as, well, functions.  functions are used as part of
other expressions, which implies that they are used in target lists 
and qualifications, which implies that they must be part of a
retrieve/replace/append/delete query.  hence..

> statement every time?

no.

> If there is not, is there any way to get multiple columns
> returned instead of only one?

foo.bar.all/all(bar(foo)) does not expand as it does for un-nested
expressions (foo.all).  this will probably change sometime since .all
is too useful.  foo.bar.a, foo.bar.b should work; carol paxson recently 
found (and fixed) a bug in the executor such that it would not if a and 
b had different types.
--
  Paul M. Aoki  |  CS Div., Dept. of EECS, UCB  |  [email-masked]
                |  Berkeley, CA 94720           |  ...!uunet!ucbvax!aoki