/usr/share/doc/postgresql-9.1-plr/plr-funcs.html is in postgresql-9.1-plr 1:8.3.0.13-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML
><HEAD
><TITLE
>Functions and Arguments</TITLE
><META
NAME="GENERATOR"
CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK
REV="MADE"
HREF="mailto:pgsql-docs@postgresql.org"><LINK
REL="HOME"
TITLE="PL/R User's Guide - R Procedural Language"
HREF="index.html"><LINK
REL="PREVIOUS"
TITLE="Installation"
HREF="plr-install.html"><LINK
REL="NEXT"
TITLE="Passing Data Values"
HREF="plr-data.html"><LINK
REL="STYLESHEET"
TYPE="text/css"
HREF="stylesheet.css"><META
HTTP-EQUIV="Content-Type"
CONTENT="text/html; charset=ISO-8859-1"><META
NAME="creation"
CONTENT="2011-09-10T18:39:12"></HEAD
><BODY
CLASS="CHAPTER"
><DIV
CLASS="NAVHEADER"
><TABLE
SUMMARY="Header navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TH
COLSPAN="5"
ALIGN="center"
VALIGN="bottom"
><A
HREF="index.html"
>PL/R User's Guide - R Procedural Language</A
></TH
></TR
><TR
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
TITLE="Installation"
HREF="plr-install.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="10%"
ALIGN="left"
VALIGN="top"
><A
TITLE="Installation"
HREF="plr-install.html"
>Fast Backward</A
></TD
><TD
WIDTH="60%"
ALIGN="center"
VALIGN="bottom"
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
TITLE="Passing Data Values"
HREF="plr-data.html"
>Fast Forward</A
></TD
><TD
WIDTH="10%"
ALIGN="right"
VALIGN="top"
><A
TITLE="Passing Data Values"
HREF="plr-data.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
></TABLE
><HR
ALIGN="LEFT"
WIDTH="100%"></DIV
><DIV
CLASS="CHAPTER"
><H1
><A
NAME="PLR-FUNCS"
></A
>Chapter 3. Functions and Arguments</H1
><P
> To create a function in the <SPAN
CLASS="APPLICATION"
>PL/R</SPAN
> language, use standard R
syntax, but without the enclosing braces or function assignment.
Instead of <TT
CLASS="LITERAL"
>myfunc <- function(arguments) { function body }</TT
>,
the body of your PL/R function is just <TT
CLASS="LITERAL"
>function body</TT
>
</P><PRE
CLASS="PROGRAMLISTING"
>CREATE OR REPLACE FUNCTION <TT
CLASS="REPLACEABLE"
><I
>funcname</I
></TT
> (<TT
CLASS="REPLACEABLE"
><I
>argument-types</I
></TT
>)
RETURNS <TT
CLASS="REPLACEABLE"
><I
>return-type</I
></TT
> AS '
function body
' LANGUAGE 'plr';
</PRE
><P>
</P
><P
> The body of the function is simply a piece of R script. When the function
is called, the argument values are passed as variables <TT
CLASS="LITERAL"
> arg1</TT
> ... <TT
CLASS="LITERAL"
>argN</TT
> to the R script. The result
is returned from the R code in the usual way. For example, a function
returning the greater of two integer values could be defined as:
</P><PRE
CLASS="PROGRAMLISTING"
>CREATE OR REPLACE FUNCTION r_max (integer, integer) RETURNS integer AS '
if (arg1 > arg2)
return(arg1)
else
return(arg2)
' LANGUAGE 'plr' STRICT;
</PRE
><P>
Starting with PostgreSQL 8.0, arguments may be explicitly named when
creating a function. If an argument is explicitly named at function
creation time, that name will be available to your R script in place
of the usual <TT
CLASS="LITERAL"
>argN</TT
> variable. For example:
</P><PRE
CLASS="PROGRAMLISTING"
>CREATE OR REPLACE FUNCTION sd(vals float8[]) RETURNS float AS '
sd(vals)
' LANGUAGE 'plr' STRICT;
</PRE
><P>
</P
><P
> Starting with PostgreSQL 8.4, a PL/R function may be declared to be a
<TT
CLASS="LITERAL"
>WINDOW</TT
>. In this case, in addition to the usual
<TT
CLASS="LITERAL"
>argN</TT
> (or named) variables, PL/R automatically
creates several other arguments to your function. For each explicit
argument, a corresponding variable called <TT
CLASS="LITERAL"
>farg1</TT
>
... <TT
CLASS="LITERAL"
>fargN</TT
> is passed to the R script. These contain
an R vector of all the values of the related argument for the moving
<TT
CLASS="LITERAL"
>WINDOW</TT
> frame within the current <TT
CLASS="LITERAL"
>PARTITION</TT
>.
For example:
</P><PRE
CLASS="PROGRAMLISTING"
>CREATE OR REPLACE
FUNCTION r_regr_slope(float8, float8)
RETURNS float8 AS
$BODY$
slope <- NA
y <- farg1
x <- farg2
if (fnumrows==9) try (slope <- lm(y ~ x)$coefficients[2])
return(slope)
$BODY$
LANGUAGE plr WINDOW;
</PRE
><P>
In the preceding example, <TT
CLASS="LITERAL"
>farg1</TT
> and <TT
CLASS="LITERAL"
>farg2</TT
> are
R vectors containing the current row's data plus that of related
rows. The determination as to which rows qualify as related is
determined by the frame specification of the query at run time.
</P
><P
> The example also illustrates one of two additional autogenerated
arguments. <TT
CLASS="LITERAL"
>fnumrows</TT
> is the number of rows in the current
<TT
CLASS="LITERAL"
>WINDOW</TT
> frame. The other (not shown) auto-argument is called
<TT
CLASS="LITERAL"
>prownum</TT
>. This argument provides the 1-based row offset of the
current row in the current <TT
CLASS="LITERAL"
>PARTITION</TT
>.
See <A
HREF="plr-window-funcs.html"
>Chapter 9</A
> for more information and a more
complete example.
</P
><P
> In some of the the definitions above, note the clause <TT
CLASS="LITERAL"
>STRICT</TT
>,
which saves us from having to think about NULL input values: if a NULL is
passed, the function will not be called at all, but will just return a NULL
result automatically. In a non-strict function, if the actual value of an
argument is NULL, the corresponding <TT
CLASS="LITERAL"
>argN</TT
> variable will
be set to a <TT
CLASS="LITERAL"
>NULL</TT
> R object. For example, suppose that we
wanted <CODE
CLASS="FUNCTION"
>r_max</CODE
> with one null and one non-null argument
to return the non-null argument, rather than NULL:
</P><PRE
CLASS="PROGRAMLISTING"
>CREATE OR REPLACE FUNCTION r_max (integer, integer) RETURNS integer AS '
if (is.null(arg1) && is.null(arg2))
return(NULL)
if (is.null(arg1))
return(arg2)
if (is.null(arg2))
return(arg1)
if (arg1 > arg2)
return(arg1)
arg2
' LANGUAGE 'plr';
</PRE
><P>
</P
><P
> As shown above, to return a NULL value from a PL/R function, return
<TT
CLASS="LITERAL"
>NULL</TT
>. This can be done whether the function is strict
or not.
</P
><P
> Composite-type (tuple) arguments are passed to the procedure as
R data.frames. The element names of the frame are the attribute
names of the composite type. If an attribute in the passed row
has the NULL value, it will appear as an "NA" in the frame. Here is
an example:
</P><PRE
CLASS="PROGRAMLISTING"
>CREATE TABLE emp (name text, age int, salary numeric(10,2));
INSERT INTO emp VALUES ('Joe', 41, 250000.00);
INSERT INTO emp VALUES ('Jim', 25, 120000.00);
INSERT INTO emp VALUES ('Jon', 35, 50000.00);
CREATE OR REPLACE FUNCTION overpaid (emp) RETURNS bool AS '
if (200000 < arg1$salary) {
return(TRUE)
}
if (arg1$age < 30 && 100000 < arg1$salary) {
return(TRUE)
}
return(FALSE)
' LANGUAGE 'plr';
SELECT name, overpaid(emp) FROM emp;
name | overpaid
------+----------
Joe | t
Jim | t
Jon | f
(3 rows)
</PRE
><P>
</P
><P
> There is also support for returning a composite-type result value:
</P><PRE
CLASS="PROGRAMLISTING"
>CREATE OR REPLACE FUNCTION get_emps() RETURNS SETOF emp AS '
names <- c("Joe","Jim","Jon")
ages <- c(41,25,35)
salaries <- c(250000,120000,50000)
df <- data.frame(name = names, age = ages, salary = salaries)
return(df)
' LANGUAGE 'plr';
select * from get_emps();
name | age | salary
------+-----+-----------
Jim | 41 | 250000.00
Joe | 25 | 120000.00
Jon | 35 | 50000.00
(3 rows)
</PRE
><P>
</P
><P
> An alternative method may be used to create a function in PL/R, if
certain criteria are met. First, the function must be a simple call
to an existing R function. Second, the function name used for the PL/R
function must match that of the R function exactly. If these two criteria
are met, the PL/R function may be defined with no body, and the arguments
will be passed directly to the R function of the same name. For example:
</P><PRE
CLASS="PROGRAMLISTING"
>create or replace function sd(_float8) returns float as '' language 'plr';
select round(sd('{1.23,1.31,1.42,1.27}'::_float8)::numeric,8);
round
------------
0.08180261
(1 row)
</PRE
><P>
</P
><DIV
CLASS="TIP"
><BLOCKQUOTE
CLASS="TIP"
><P
><B
>Tip: </B
> Because the function body is passed as an SQL string literal to
<TT
CLASS="COMMAND"
>CREATE FUNCTION</TT
>, you have to escape single
quotes and backslashes within your R source, typically by
doubling them.
</P
></BLOCKQUOTE
></DIV
></DIV
><DIV
CLASS="NAVFOOTER"
><HR
ALIGN="LEFT"
WIDTH="100%"><TABLE
SUMMARY="Footer navigation table"
WIDTH="100%"
BORDER="0"
CELLPADDING="0"
CELLSPACING="0"
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
><A
HREF="plr-install.html"
ACCESSKEY="P"
>Prev</A
></TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
><A
HREF="index.html"
ACCESSKEY="H"
>Home</A
></TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
><A
HREF="plr-data.html"
ACCESSKEY="N"
>Next</A
></TD
></TR
><TR
><TD
WIDTH="33%"
ALIGN="left"
VALIGN="top"
>Installation</TD
><TD
WIDTH="34%"
ALIGN="center"
VALIGN="top"
> </TD
><TD
WIDTH="33%"
ALIGN="right"
VALIGN="top"
>Passing Data Values</TD
></TR
></TABLE
></DIV
></BODY
></HTML
>
|