2002-10-19 02:41:22 +08:00
|
|
|
-- Adjust this setting to control where the objects get created.
|
|
|
|
SET search_path = public;
|
|
|
|
|
2003-09-14 05:44:50 +08:00
|
|
|
CREATE OR REPLACE FUNCTION normal_rand(int4, float8, float8)
|
2002-10-19 02:41:22 +08:00
|
|
|
RETURNS setof float8
|
|
|
|
AS 'MODULE_PATHNAME','normal_rand'
|
|
|
|
LANGUAGE 'C' VOLATILE STRICT;
|
As mentioned above, here is my contrib/tablefunc patch. It includes
three functions which exercise the tablefunc API.
show_all_settings()
- returns the same information as SHOW ALL, but as a query result
normal_rand(int numvals, float8 mean, float8 stddev, int seed)
- returns a set of normally distributed float8 values
- This routine implements Algorithm P (Polar method for normal
deviates) from Knuth's _The_Art_of_Computer_Programming_, Volume 2,
3rd ed., pages 122-126. Knuth cites his source as "The polar
method", G. E. P. Box, M. E. Muller, and G. Marsaglia,
_Annals_Math,_Stat._ 29 (1958), 610-611.
crosstabN(text sql)
- returns a set of row_name plus N category value columns
- crosstab2(), crosstab3(), and crosstab4() are defined for you,
but you can create additional crosstab functions per directions
in the README.
Joe Conway
2002-07-31 00:31:11 +08:00
|
|
|
|
2005-05-31 07:09:07 +08:00
|
|
|
-- the generic crosstab function:
|
|
|
|
CREATE OR REPLACE FUNCTION crosstab(text)
|
|
|
|
RETURNS setof record
|
|
|
|
AS 'MODULE_PATHNAME','crosstab'
|
|
|
|
LANGUAGE 'C' STABLE STRICT;
|
|
|
|
|
|
|
|
-- examples of building custom type-specific crosstab functions:
|
2002-09-15 03:53:59 +08:00
|
|
|
CREATE TYPE tablefunc_crosstab_2 AS
|
|
|
|
(
|
2002-10-19 02:41:22 +08:00
|
|
|
row_name TEXT,
|
|
|
|
category_1 TEXT,
|
|
|
|
category_2 TEXT
|
2002-09-15 03:53:59 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TYPE tablefunc_crosstab_3 AS
|
|
|
|
(
|
2002-10-19 02:41:22 +08:00
|
|
|
row_name TEXT,
|
|
|
|
category_1 TEXT,
|
|
|
|
category_2 TEXT,
|
|
|
|
category_3 TEXT
|
2002-09-15 03:53:59 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
CREATE TYPE tablefunc_crosstab_4 AS
|
|
|
|
(
|
2002-10-19 02:41:22 +08:00
|
|
|
row_name TEXT,
|
|
|
|
category_1 TEXT,
|
|
|
|
category_2 TEXT,
|
|
|
|
category_3 TEXT,
|
|
|
|
category_4 TEXT
|
2002-09-15 03:53:59 +08:00
|
|
|
);
|
As mentioned above, here is my contrib/tablefunc patch. It includes
three functions which exercise the tablefunc API.
show_all_settings()
- returns the same information as SHOW ALL, but as a query result
normal_rand(int numvals, float8 mean, float8 stddev, int seed)
- returns a set of normally distributed float8 values
- This routine implements Algorithm P (Polar method for normal
deviates) from Knuth's _The_Art_of_Computer_Programming_, Volume 2,
3rd ed., pages 122-126. Knuth cites his source as "The polar
method", G. E. P. Box, M. E. Muller, and G. Marsaglia,
_Annals_Math,_Stat._ 29 (1958), 610-611.
crosstabN(text sql)
- returns a set of row_name plus N category value columns
- crosstab2(), crosstab3(), and crosstab4() are defined for you,
but you can create additional crosstab functions per directions
in the README.
Joe Conway
2002-07-31 00:31:11 +08:00
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION crosstab2(text)
|
2002-10-19 02:41:22 +08:00
|
|
|
RETURNS setof tablefunc_crosstab_2
|
|
|
|
AS 'MODULE_PATHNAME','crosstab'
|
|
|
|
LANGUAGE 'C' STABLE STRICT;
|
As mentioned above, here is my contrib/tablefunc patch. It includes
three functions which exercise the tablefunc API.
show_all_settings()
- returns the same information as SHOW ALL, but as a query result
normal_rand(int numvals, float8 mean, float8 stddev, int seed)
- returns a set of normally distributed float8 values
- This routine implements Algorithm P (Polar method for normal
deviates) from Knuth's _The_Art_of_Computer_Programming_, Volume 2,
3rd ed., pages 122-126. Knuth cites his source as "The polar
method", G. E. P. Box, M. E. Muller, and G. Marsaglia,
_Annals_Math,_Stat._ 29 (1958), 610-611.
crosstabN(text sql)
- returns a set of row_name plus N category value columns
- crosstab2(), crosstab3(), and crosstab4() are defined for you,
but you can create additional crosstab functions per directions
in the README.
Joe Conway
2002-07-31 00:31:11 +08:00
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION crosstab3(text)
|
2002-10-19 02:41:22 +08:00
|
|
|
RETURNS setof tablefunc_crosstab_3
|
|
|
|
AS 'MODULE_PATHNAME','crosstab'
|
|
|
|
LANGUAGE 'C' STABLE STRICT;
|
As mentioned above, here is my contrib/tablefunc patch. It includes
three functions which exercise the tablefunc API.
show_all_settings()
- returns the same information as SHOW ALL, but as a query result
normal_rand(int numvals, float8 mean, float8 stddev, int seed)
- returns a set of normally distributed float8 values
- This routine implements Algorithm P (Polar method for normal
deviates) from Knuth's _The_Art_of_Computer_Programming_, Volume 2,
3rd ed., pages 122-126. Knuth cites his source as "The polar
method", G. E. P. Box, M. E. Muller, and G. Marsaglia,
_Annals_Math,_Stat._ 29 (1958), 610-611.
crosstabN(text sql)
- returns a set of row_name plus N category value columns
- crosstab2(), crosstab3(), and crosstab4() are defined for you,
but you can create additional crosstab functions per directions
in the README.
Joe Conway
2002-07-31 00:31:11 +08:00
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION crosstab4(text)
|
2002-10-19 02:41:22 +08:00
|
|
|
RETURNS setof tablefunc_crosstab_4
|
|
|
|
AS 'MODULE_PATHNAME','crosstab'
|
|
|
|
LANGUAGE 'C' STABLE STRICT;
|
As mentioned above, here is my contrib/tablefunc patch. It includes
three functions which exercise the tablefunc API.
show_all_settings()
- returns the same information as SHOW ALL, but as a query result
normal_rand(int numvals, float8 mean, float8 stddev, int seed)
- returns a set of normally distributed float8 values
- This routine implements Algorithm P (Polar method for normal
deviates) from Knuth's _The_Art_of_Computer_Programming_, Volume 2,
3rd ed., pages 122-126. Knuth cites his source as "The polar
method", G. E. P. Box, M. E. Muller, and G. Marsaglia,
_Annals_Math,_Stat._ 29 (1958), 610-611.
crosstabN(text sql)
- returns a set of row_name plus N category value columns
- crosstab2(), crosstab3(), and crosstab4() are defined for you,
but you can create additional crosstab functions per directions
in the README.
Joe Conway
2002-07-31 00:31:11 +08:00
|
|
|
|
2005-05-31 07:09:07 +08:00
|
|
|
-- obsolete:
|
2002-08-15 10:51:27 +08:00
|
|
|
CREATE OR REPLACE FUNCTION crosstab(text,int)
|
2002-10-19 02:41:22 +08:00
|
|
|
RETURNS setof record
|
|
|
|
AS 'MODULE_PATHNAME','crosstab'
|
|
|
|
LANGUAGE 'C' STABLE STRICT;
|
Attached is an update to contrib/tablefunc. It introduces a new
function, connectby(), which can serve as a reference implementation for
the changes made in the last few days -- namely the ability of a
function to return an entire tuplestore, and the ability of a function
to make use of the query provided "expected" tuple description.
Description:
connectby(text relname, text keyid_fld, text parent_keyid_fld,
text start_with, int max_depth [, text branch_delim])
- returns keyid, parent_keyid, level, and an optional branch string
- requires anonymous composite type syntax in the FROM clause. See
the instructions in the documentation below.
Joe Conway
2002-09-02 13:44:05 +08:00
|
|
|
|
Attached is an update to contrib/tablefunc. It implements a new hashed
version of crosstab. This fixes a major deficiency in real-world use of
the original version. Easiest to undestand with an illustration:
Data:
-------------------------------------------------------------------
select * from cth;
id | rowid | rowdt | attribute | val
----+-------+---------------------+----------------+---------------
1 | test1 | 2003-03-01 00:00:00 | temperature | 42
2 | test1 | 2003-03-01 00:00:00 | test_result | PASS
3 | test1 | 2003-03-01 00:00:00 | volts | 2.6987
4 | test2 | 2003-03-02 00:00:00 | temperature | 53
5 | test2 | 2003-03-02 00:00:00 | test_result | FAIL
6 | test2 | 2003-03-02 00:00:00 | test_startdate | 01 March 2003
7 | test2 | 2003-03-02 00:00:00 | volts | 3.1234
(7 rows)
Original crosstab:
-------------------------------------------------------------------
SELECT * FROM crosstab(
'SELECT rowid, attribute, val FROM cth ORDER BY 1,2',4)
AS c(rowid text, temperature text, test_result text, test_startdate
text, volts text);
rowid | temperature | test_result | test_startdate | volts
-------+-------------+-------------+----------------+--------
test1 | 42 | PASS | 2.6987 |
test2 | 53 | FAIL | 01 March 2003 | 3.1234
(2 rows)
Hashed crosstab:
-------------------------------------------------------------------
SELECT * FROM crosstab(
'SELECT rowid, attribute, val FROM cth ORDER BY 1',
'SELECT DISTINCT attribute FROM cth ORDER BY 1')
AS c(rowid text, temperature int4, test_result text, test_startdate
timestamp, volts float8);
rowid | temperature | test_result | test_startdate | volts
-------+-------------+-------------+---------------------+--------
test1 | 42 | PASS | | 2.6987
test2 | 53 | FAIL | 2003-03-01 00:00:00 | 3.1234
(2 rows)
Notice that the original crosstab slides data over to the left in the
result tuple when it encounters missing data. In order to work around
this you have to be make your source sql do all sorts of contortions
(cartesian join of distinct rowid with distinct attribute; left join
that back to the real source data). The new version avoids this by
building a hash table using a second distinct attribute query.
The new version also allows for "extra" columns (see the README) and
allows the result columns to be coerced into differing datatypes if they
are suitable (as shown above).
In testing a "real-world" data set (69 distinct rowid's, 27 distinct
categories/attributes, multiple missing data points) I saw about a
5-fold improvement in execution time (from about 2200 ms old, to 440 ms
new).
I left the original version intact because: 1) BC, 2) it is probably
slightly faster if you know that you have no missing attributes.
README and regression test adjustments included. If there are no
objections, please apply.
Joe Conway
2003-03-20 14:46:30 +08:00
|
|
|
CREATE OR REPLACE FUNCTION crosstab(text,text)
|
|
|
|
RETURNS setof record
|
|
|
|
AS 'MODULE_PATHNAME','crosstab_hash'
|
|
|
|
LANGUAGE 'C' STABLE STRICT;
|
|
|
|
|
Attached is an update to contrib/tablefunc. It introduces a new
function, connectby(), which can serve as a reference implementation for
the changes made in the last few days -- namely the ability of a
function to return an entire tuplestore, and the ability of a function
to make use of the query provided "expected" tuple description.
Description:
connectby(text relname, text keyid_fld, text parent_keyid_fld,
text start_with, int max_depth [, text branch_delim])
- returns keyid, parent_keyid, level, and an optional branch string
- requires anonymous composite type syntax in the FROM clause. See
the instructions in the documentation below.
Joe Conway
2002-09-02 13:44:05 +08:00
|
|
|
CREATE OR REPLACE FUNCTION connectby(text,text,text,text,int,text)
|
2002-10-19 02:41:22 +08:00
|
|
|
RETURNS setof record
|
|
|
|
AS 'MODULE_PATHNAME','connectby_text'
|
|
|
|
LANGUAGE 'C' STABLE STRICT;
|
Attached is an update to contrib/tablefunc. It introduces a new
function, connectby(), which can serve as a reference implementation for
the changes made in the last few days -- namely the ability of a
function to return an entire tuplestore, and the ability of a function
to make use of the query provided "expected" tuple description.
Description:
connectby(text relname, text keyid_fld, text parent_keyid_fld,
text start_with, int max_depth [, text branch_delim])
- returns keyid, parent_keyid, level, and an optional branch string
- requires anonymous composite type syntax in the FROM clause. See
the instructions in the documentation below.
Joe Conway
2002-09-02 13:44:05 +08:00
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION connectby(text,text,text,text,int)
|
2002-10-19 02:41:22 +08:00
|
|
|
RETURNS setof record
|
|
|
|
AS 'MODULE_PATHNAME','connectby_text'
|
|
|
|
LANGUAGE 'C' STABLE STRICT;
|
2003-07-27 11:51:59 +08:00
|
|
|
|
|
|
|
-- These 2 take the name of a field to ORDER BY as 4th arg (for sorting siblings)
|
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION connectby(text,text,text,text,text,int,text)
|
|
|
|
RETURNS setof record
|
|
|
|
AS 'MODULE_PATHNAME','connectby_text_serial'
|
|
|
|
LANGUAGE 'C' STABLE STRICT;
|
|
|
|
|
|
|
|
CREATE OR REPLACE FUNCTION connectby(text,text,text,text,text,int)
|
|
|
|
RETURNS setof record
|
|
|
|
AS 'MODULE_PATHNAME','connectby_text_serial'
|
|
|
|
LANGUAGE 'C' STABLE STRICT;
|