mirror of
https://github.com/postgres/postgres.git
synced 2025-07-27 12:41:57 +03:00
Have crosstab variants treat NULL rowid as a category in its own right,
per suggestion from Tom Lane. This fixes crash-bug reported by Stefan Schwarzer.
This commit is contained in:
@ -23,42 +23,48 @@ SELECT * FROM crosstab2('SELECT rowid, attribute, val FROM ct where rowclass = '
|
||||
----------+------------+------------
|
||||
test1 | val2 | val3
|
||||
test2 | val6 | val7
|
||||
(2 rows)
|
||||
| val10 | val11
|
||||
(3 rows)
|
||||
|
||||
SELECT * FROM crosstab3('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') ORDER BY 1,2;');
|
||||
row_name | category_1 | category_2 | category_3
|
||||
----------+------------+------------+------------
|
||||
test1 | val2 | val3 |
|
||||
test2 | val6 | val7 |
|
||||
(2 rows)
|
||||
| val10 | val11 |
|
||||
(3 rows)
|
||||
|
||||
SELECT * FROM crosstab4('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') ORDER BY 1,2;');
|
||||
row_name | category_1 | category_2 | category_3 | category_4
|
||||
----------+------------+------------+------------+------------
|
||||
test1 | val2 | val3 | |
|
||||
test2 | val6 | val7 | |
|
||||
(2 rows)
|
||||
| val10 | val11 | |
|
||||
(3 rows)
|
||||
|
||||
SELECT * FROM crosstab2('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' ORDER BY 1,2;');
|
||||
row_name | category_1 | category_2
|
||||
----------+------------+------------
|
||||
test1 | val1 | val2
|
||||
test2 | val5 | val6
|
||||
(2 rows)
|
||||
| val9 | val10
|
||||
(3 rows)
|
||||
|
||||
SELECT * FROM crosstab3('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' ORDER BY 1,2;');
|
||||
row_name | category_1 | category_2 | category_3
|
||||
----------+------------+------------+------------
|
||||
test1 | val1 | val2 | val3
|
||||
test2 | val5 | val6 | val7
|
||||
(2 rows)
|
||||
| val9 | val10 | val11
|
||||
(3 rows)
|
||||
|
||||
SELECT * FROM crosstab4('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' ORDER BY 1,2;');
|
||||
row_name | category_1 | category_2 | category_3 | category_4
|
||||
----------+------------+------------+------------+------------
|
||||
test1 | val1 | val2 | val3 | val4
|
||||
test2 | val5 | val6 | val7 | val8
|
||||
(2 rows)
|
||||
| val9 | val10 | val11 | val12
|
||||
(3 rows)
|
||||
|
||||
SELECT * FROM crosstab2('SELECT rowid, attribute, val FROM ct where rowclass = ''group2'' and (attribute = ''att1'' or attribute = ''att2'') ORDER BY 1,2;');
|
||||
row_name | category_1 | category_2
|
||||
@ -103,25 +109,28 @@ SELECT * FROM crosstab4('SELECT rowid, attribute, val FROM ct where rowclass = '
|
||||
(2 rows)
|
||||
|
||||
SELECT * FROM crosstab('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' ORDER BY 1,2;') AS c(rowid text, att1 text, att2 text);
|
||||
rowid | att1 | att2
|
||||
-------+------+------
|
||||
rowid | att1 | att2
|
||||
-------+------+-------
|
||||
test1 | val1 | val2
|
||||
test2 | val5 | val6
|
||||
(2 rows)
|
||||
| val9 | val10
|
||||
(3 rows)
|
||||
|
||||
SELECT * FROM crosstab('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' ORDER BY 1,2;') AS c(rowid text, att1 text, att2 text, att3 text);
|
||||
rowid | att1 | att2 | att3
|
||||
-------+------+------+------
|
||||
test1 | val1 | val2 | val3
|
||||
test2 | val5 | val6 | val7
|
||||
(2 rows)
|
||||
rowid | att1 | att2 | att3
|
||||
-------+------+-------+-------
|
||||
test1 | val1 | val2 | val3
|
||||
test2 | val5 | val6 | val7
|
||||
| val9 | val10 | val11
|
||||
(3 rows)
|
||||
|
||||
SELECT * FROM crosstab('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' ORDER BY 1,2;') AS c(rowid text, att1 text, att2 text, att3 text, att4 text);
|
||||
rowid | att1 | att2 | att3 | att4
|
||||
-------+------+------+------+------
|
||||
test1 | val1 | val2 | val3 | val4
|
||||
test2 | val5 | val6 | val7 | val8
|
||||
(2 rows)
|
||||
rowid | att1 | att2 | att3 | att4
|
||||
-------+------+-------+-------+-------
|
||||
test1 | val1 | val2 | val3 | val4
|
||||
test2 | val5 | val6 | val7 | val8
|
||||
| val9 | val10 | val11 | val12
|
||||
(3 rows)
|
||||
|
||||
-- check it works with OUT parameters, too
|
||||
CREATE FUNCTION crosstab_out(text,
|
||||
@ -130,11 +139,12 @@ RETURNS setof record
|
||||
AS '$libdir/tablefunc','crosstab'
|
||||
LANGUAGE C STABLE STRICT;
|
||||
SELECT * FROM crosstab_out('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' ORDER BY 1,2;');
|
||||
rowid | att1 | att2 | att3
|
||||
-------+------+------+------
|
||||
test1 | val1 | val2 | val3
|
||||
test2 | val5 | val6 | val7
|
||||
(2 rows)
|
||||
rowid | att1 | att2 | att3
|
||||
-------+------+-------+-------
|
||||
test1 | val1 | val2 | val3
|
||||
test2 | val5 | val6 | val7
|
||||
| val9 | val10 | val11
|
||||
(3 rows)
|
||||
|
||||
--
|
||||
-- hash based crosstab
|
||||
@ -150,38 +160,46 @@ insert into cth values(DEFAULT,'test2','02 March 2003','temperature','53');
|
||||
insert into cth values(DEFAULT,'test2','02 March 2003','test_result','FAIL');
|
||||
insert into cth values(DEFAULT,'test2','02 March 2003','test_startdate','01 March 2003');
|
||||
insert into cth values(DEFAULT,'test2','02 March 2003','volts','3.1234');
|
||||
-- next group tests for NULL rowids
|
||||
insert into cth values(DEFAULT,NULL,'25 October 2007','temperature','57');
|
||||
insert into cth values(DEFAULT,NULL,'25 October 2007','test_result','PASS');
|
||||
insert into cth values(DEFAULT,NULL,'25 October 2007','test_startdate','24 October 2007');
|
||||
insert into cth values(DEFAULT,NULL,'25 October 2007','volts','1.41234');
|
||||
-- return attributes as plain text
|
||||
SELECT * FROM crosstab(
|
||||
'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1',
|
||||
'SELECT DISTINCT attribute FROM cth ORDER BY 1')
|
||||
AS c(rowid text, rowdt timestamp, temperature text, test_result text, test_startdate text, volts text);
|
||||
rowid | rowdt | temperature | test_result | test_startdate | volts
|
||||
-------+--------------------------+-------------+-------------+----------------+--------
|
||||
test1 | Sat Mar 01 00:00:00 2003 | 42 | PASS | | 2.6987
|
||||
test2 | Sun Mar 02 00:00:00 2003 | 53 | FAIL | 01 March 2003 | 3.1234
|
||||
(2 rows)
|
||||
rowid | rowdt | temperature | test_result | test_startdate | volts
|
||||
-------+--------------------------+-------------+-------------+-----------------+---------
|
||||
test1 | Sat Mar 01 00:00:00 2003 | 42 | PASS | | 2.6987
|
||||
test2 | Sun Mar 02 00:00:00 2003 | 53 | FAIL | 01 March 2003 | 3.1234
|
||||
| Thu Oct 25 00:00:00 2007 | 57 | PASS | 24 October 2007 | 1.41234
|
||||
(3 rows)
|
||||
|
||||
-- this time without rowdt
|
||||
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 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)
|
||||
rowid | temperature | test_result | test_startdate | volts
|
||||
-------+-------------+-------------+-----------------+---------
|
||||
test1 | 42 | PASS | | 2.6987
|
||||
test2 | 53 | FAIL | 01 March 2003 | 3.1234
|
||||
| 57 | PASS | 24 October 2007 | 1.41234
|
||||
(3 rows)
|
||||
|
||||
-- convert attributes to specific datatypes
|
||||
SELECT * FROM crosstab(
|
||||
'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1',
|
||||
'SELECT DISTINCT attribute FROM cth ORDER BY 1')
|
||||
AS c(rowid text, rowdt timestamp, temperature int4, test_result text, test_startdate timestamp, volts float8);
|
||||
rowid | rowdt | temperature | test_result | test_startdate | volts
|
||||
-------+--------------------------+-------------+-------------+--------------------------+--------
|
||||
test1 | Sat Mar 01 00:00:00 2003 | 42 | PASS | | 2.6987
|
||||
test2 | Sun Mar 02 00:00:00 2003 | 53 | FAIL | Sat Mar 01 00:00:00 2003 | 3.1234
|
||||
(2 rows)
|
||||
rowid | rowdt | temperature | test_result | test_startdate | volts
|
||||
-------+--------------------------+-------------+-------------+--------------------------+---------
|
||||
test1 | Sat Mar 01 00:00:00 2003 | 42 | PASS | | 2.6987
|
||||
test2 | Sun Mar 02 00:00:00 2003 | 53 | FAIL | Sat Mar 01 00:00:00 2003 | 3.1234
|
||||
| Thu Oct 25 00:00:00 2007 | 57 | PASS | Wed Oct 24 00:00:00 2007 | 1.41234
|
||||
(3 rows)
|
||||
|
||||
-- source query and category query out of sync
|
||||
SELECT * FROM crosstab(
|
||||
@ -192,7 +210,8 @@ AS c(rowid text, rowdt timestamp, temperature int4, test_result text, test_start
|
||||
-------+--------------------------+-------------+-------------+--------------------------
|
||||
test1 | Sat Mar 01 00:00:00 2003 | 42 | PASS |
|
||||
test2 | Sun Mar 02 00:00:00 2003 | 53 | FAIL | Sat Mar 01 00:00:00 2003
|
||||
(2 rows)
|
||||
| Thu Oct 25 00:00:00 2007 | 57 | PASS | Wed Oct 24 00:00:00 2007
|
||||
(3 rows)
|
||||
|
||||
-- if category query generates no rows, get expected error
|
||||
SELECT * FROM crosstab(
|
||||
@ -235,11 +254,12 @@ LANGUAGE C STABLE STRICT;
|
||||
SELECT * FROM crosstab_named(
|
||||
'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1',
|
||||
'SELECT DISTINCT attribute FROM cth ORDER BY 1');
|
||||
rowid | rowdt | temperature | test_result | test_startdate | volts
|
||||
-------+--------------------------+-------------+-------------+--------------------------+--------
|
||||
test1 | Sat Mar 01 00:00:00 2003 | 42 | PASS | | 2.6987
|
||||
test2 | Sun Mar 02 00:00:00 2003 | 53 | FAIL | Sat Mar 01 00:00:00 2003 | 3.1234
|
||||
(2 rows)
|
||||
rowid | rowdt | temperature | test_result | test_startdate | volts
|
||||
-------+--------------------------+-------------+-------------+--------------------------+---------
|
||||
test1 | Sat Mar 01 00:00:00 2003 | 42 | PASS | | 2.6987
|
||||
test2 | Sun Mar 02 00:00:00 2003 | 53 | FAIL | Sat Mar 01 00:00:00 2003 | 3.1234
|
||||
| Thu Oct 25 00:00:00 2007 | 57 | PASS | Wed Oct 24 00:00:00 2007 | 1.41234
|
||||
(3 rows)
|
||||
|
||||
-- check it works with OUT parameters
|
||||
CREATE FUNCTION crosstab_out(text, text,
|
||||
@ -252,11 +272,12 @@ LANGUAGE C STABLE STRICT;
|
||||
SELECT * FROM crosstab_out(
|
||||
'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1',
|
||||
'SELECT DISTINCT attribute FROM cth ORDER BY 1');
|
||||
rowid | rowdt | temperature | test_result | test_startdate | volts
|
||||
-------+--------------------------+-------------+-------------+--------------------------+--------
|
||||
test1 | Sat Mar 01 00:00:00 2003 | 42 | PASS | | 2.6987
|
||||
test2 | Sun Mar 02 00:00:00 2003 | 53 | FAIL | Sat Mar 01 00:00:00 2003 | 3.1234
|
||||
(2 rows)
|
||||
rowid | rowdt | temperature | test_result | test_startdate | volts
|
||||
-------+--------------------------+-------------+-------------+--------------------------+---------
|
||||
test1 | Sat Mar 01 00:00:00 2003 | 42 | PASS | | 2.6987
|
||||
test2 | Sun Mar 02 00:00:00 2003 | 53 | FAIL | Sat Mar 01 00:00:00 2003 | 3.1234
|
||||
| Thu Oct 25 00:00:00 2007 | 57 | PASS | Wed Oct 24 00:00:00 2007 | 1.41234
|
||||
(3 rows)
|
||||
|
||||
--
|
||||
-- connectby
|
||||
|
Reference in New Issue
Block a user