You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Improve wrong-tuple-type error reports in contrib/tablefunc.
These messages were fairly confusing, and didn't match the
column names used in the SGML docs. Try to improve that.
Also use error codes more specific than ERRCODE_SYNTAX_ERROR.
Patch by me, reviewed by Joe Conway
Discussion: https://postgr.es/m/18937.1709676295@sss.pgh.pa.us
Copy file name to clipboardExpand all lines: contrib/tablefunc/expected/tablefunc.out
+77-8
Original file line number
Diff line number
Diff line change
@@ -145,6 +145,23 @@ SELECT * FROM crosstab_out('SELECT rowid, attribute, val FROM ct where rowclass
145
145
| val9 | val10 | val11
146
146
(3 rows)
147
147
148
+
-- check error reporting
149
+
SELECT * FROM crosstab('SELECT rowid, val FROM ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') ORDER BY 1,2;')
150
+
AS ct(row_name text, category_1 text, category_2 text);
151
+
ERROR: invalid crosstab source data query
152
+
DETAIL: The query must return 3 columns: row_name, category, and value.
153
+
SELECT * FROM crosstab('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') ORDER BY 1,2;')
154
+
AS ct(row_name text);
155
+
ERROR: invalid crosstab return type
156
+
DETAIL: Return row must have at least two columns.
157
+
SELECT * FROM crosstab('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') ORDER BY 1,2;')
158
+
AS ct(row_name int, category_1 text, category_2 text);
159
+
ERROR: invalid crosstab return type
160
+
DETAIL: Source row_name datatype text does not match return row_name datatype integer.
161
+
SELECT * FROM crosstab('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') ORDER BY 1,2;')
162
+
AS ct(row_name text, category_1 text, category_2 int);
163
+
ERROR: invalid crosstab return type
164
+
DETAIL: Source value datatype text does not match return value datatype integer in column 3.
148
165
--
149
166
-- hash based crosstab
150
167
--
@@ -216,13 +233,20 @@ SELECT * FROM crosstab(
216
233
'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1',
217
234
'SELECT DISTINCT attribute FROM cth WHERE attribute = ''a'' ORDER BY 1')
218
235
AS c(rowid text, rowdt timestamp, temperature int4, test_result text, test_startdate timestamp, volts float8);
219
-
ERROR: provided "categories" SQL must return 1 column of at least one row
236
+
ERROR: crosstab categories query must return at least one row
220
237
-- if category query generates more than one column, get expected error
221
238
SELECT * FROM crosstab(
222
239
'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1',
223
240
'SELECT DISTINCT rowdt, attribute FROM cth ORDER BY 2')
224
241
AS c(rowid text, rowdt timestamp, temperature int4, test_result text, test_startdate timestamp, volts float8);
225
-
ERROR: provided "categories" SQL must return 1 column of at least one row
242
+
ERROR: invalid crosstab categories query
243
+
DETAIL: The query must return one column.
244
+
-- if category query generates a NULL value, get expected error
245
+
SELECT * FROM crosstab(
246
+
'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1',
247
+
'SELECT NULL::text')
248
+
AS c(rowid text, rowdt timestamp, temperature int4, test_result text, test_startdate timestamp, volts float8);
249
+
ERROR: crosstab category value must not be null
226
250
-- if source query returns zero rows, get zero rows returned
227
251
SELECT * FROM crosstab(
228
252
'SELECT rowid, rowdt, attribute, val FROM cth WHERE false ORDER BY 1',
@@ -241,6 +265,26 @@ AS c(rowid text, rowdt timestamp, temperature text, test_result text, test_start
Copy file name to clipboardExpand all lines: contrib/tablefunc/sql/tablefunc.sql
+42
Original file line number
Diff line number
Diff line change
@@ -44,6 +44,16 @@ LANGUAGE C STABLE STRICT;
44
44
45
45
SELECT*FROM crosstab_out('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' ORDER BY 1,2;');
46
46
47
+
-- check error reporting
48
+
SELECT*FROM crosstab('SELECT rowid, val FROM ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') ORDER BY 1,2;')
49
+
AS ct(row_name text, category_1 text, category_2 text);
50
+
SELECT*FROM crosstab('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') ORDER BY 1,2;')
51
+
AS ct(row_name text);
52
+
SELECT*FROM crosstab('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') ORDER BY 1,2;')
53
+
AS ct(row_name int, category_1 text, category_2 text);
54
+
SELECT*FROM crosstab('SELECT rowid, attribute, val FROM ct where rowclass = ''group1'' and (attribute = ''att2'' or attribute = ''att3'') ORDER BY 1,2;')
55
+
AS ct(row_name text, category_1 text, category_2 int);
56
+
47
57
--
48
58
-- hash based crosstab
49
59
--
@@ -99,6 +109,12 @@ SELECT * FROM crosstab(
99
109
'SELECT DISTINCT rowdt, attribute FROM cth ORDER BY 2')
100
110
AS c(rowid text, rowdt timestamp, temperature int4, test_result text, test_startdate timestamp, volts float8);
101
111
112
+
-- if category query generates a NULL value, get expected error
113
+
SELECT*FROM crosstab(
114
+
'SELECT rowid, rowdt, attribute, val FROM cth ORDER BY 1',
115
+
'SELECT NULL::text')
116
+
AS c(rowid text, rowdt timestamp, temperature int4, test_result text, test_startdate timestamp, volts float8);
117
+
102
118
-- if source query returns zero rows, get zero rows returned
103
119
SELECT*FROM crosstab(
104
120
'SELECT rowid, rowdt, attribute, val FROM cth WHERE false ORDER BY 1',
@@ -111,6 +127,22 @@ SELECT * FROM crosstab(
111
127
'SELECT DISTINCT attribute FROM cth WHERE false ORDER BY 1')
112
128
AS c(rowid text, rowdt timestamp, temperature text, test_result text, test_startdate text, volts text);
113
129
130
+
-- check errors with inappropriate input rowtype
131
+
SELECT*FROM crosstab(
132
+
'SELECT rowid, attribute FROM cth ORDER BY 1',
133
+
'SELECT DISTINCT attribute FROM cth ORDER BY 1')
134
+
AS c(rowid text, temperature text, test_result text, test_startdate text, volts text);
135
+
SELECT*FROM crosstab(
136
+
'SELECT rowid, rowdt, rowdt, attribute, val FROM cth ORDER BY 1',
137
+
'SELECT DISTINCT attribute FROM cth ORDER BY 1')
138
+
AS c(rowid text, rowdt timestamp, temperature int4, test_result text, test_startdate timestamp, volts float8);
139
+
140
+
-- check errors with inappropriate result rowtype
141
+
SELECT*FROM crosstab(
142
+
'SELECT rowid, attribute, val FROM cth ORDER BY 1',
143
+
'SELECT DISTINCT attribute FROM cth ORDER BY 1')
144
+
AS c(rowid text);
145
+
114
146
-- check it works with a named result rowtype
115
147
116
148
createtypemy_crosstab_resultas (
@@ -186,6 +218,16 @@ SELECT * FROM connectby('connectby_int', 'keyid', 'parent_keyid', '2', 0, '~') A
186
218
187
219
-- should fail as key field datatype should match return datatype
0 commit comments