|
62 | 62 | cl.relkind relation_kind |
63 | 63 | FROM |
64 | 64 | pg_class cl |
65 | | - ), |
66 | | - queryable_relations AS |
67 | | - ( |
68 | | - SELECT DISTINCT ON (relation_name) relations.* |
69 | | - FROM relations |
70 | | - WHERE relation_kind IN |
| 65 | + WHERE relkind IN |
71 | 66 | -- Lots of different types of relations exist, but we're only interested in |
72 | 67 | -- the ones that can be queried. |
73 | 68 | ( |
|
82 | 77 | -- c = composite type, |
83 | 78 | -- I = partitioned index |
84 | 79 | ) |
85 | | - |
86 | | - -- Since we will _not_ be grouping by a key we need this to be ordered |
87 | | - -- to get deterministic results. |
88 | | - -- (Specificically, we do not yet take schemas into account) |
89 | | - ORDER BY relation_name, schema_id, relation_kind |
90 | 80 | ), |
91 | 81 |
|
92 | 82 | -- Columns are recorded in `pg_attribute`. An 'attribute' is the generic term |
@@ -301,78 +291,37 @@ WITH |
301 | 291 | -- their schema. |
302 | 292 | aggregates AS |
303 | 293 | ( |
304 | | - WITH |
305 | | - -- The arguments to an aggregate function is an array of type oids, which |
306 | | - -- we want to resolve to an array of type names instead. |
307 | | - -- Somewhat awkwardly, this means we have to unnest, join on types, and |
308 | | - -- array_agg and group by. |
309 | | - aggregate_argument_types AS |
310 | | - ( |
311 | | - SELECT |
312 | | - arg.proc_id, |
313 | | - array_agg(arg.type_name) AS argument_types |
314 | | - FROM |
315 | | - ( |
316 | | - SELECT |
317 | | - proc.proc_id, |
318 | | - t.type_name |
319 | | - FROM |
320 | | - ( |
321 | | - SELECT |
322 | | - proc.oid AS proc_id, |
323 | | - unnest(proc.proargtypes) AS type_id |
324 | | - FROM |
325 | | - pg_catalog.pg_proc AS proc |
326 | | - WHERE |
327 | | - -- We only support single-argument aggregates currently. |
328 | | - -- This assertion is important to make here since joining with |
329 | | - -- 'types' filter arguments of polymorphic type, and we might |
330 | | - -- risk ending up with one argument later. |
331 | | - cardinality(proc.proargtypes) = 1 |
332 | | - ) |
333 | | - AS proc |
334 | | - INNER JOIN |
335 | | - scalar_types AS t |
336 | | - USING (type_id) |
337 | | - ) |
338 | | - AS arg |
339 | | - GROUP BY arg.proc_id |
340 | | - HAVING |
341 | | - -- We need to check that we still have an argument, since we're |
342 | | - -- filtering by our restricted notion of scalar types, which may |
343 | | - -- exclude some types (e.g. pseudo-types and array types). |
344 | | - cardinality(array_agg(arg.type_name)) = 1 |
345 | | - ) |
346 | 294 | SELECT |
347 | 295 | proc.oid AS proc_id, |
348 | 296 | proc.proname AS proc_name, |
349 | 297 | proc.pronamespace AS schema_id, |
350 | | - args.argument_types, |
| 298 | + arg_type.type_name as argument_type, |
351 | 299 | ret_type.type_name as return_type |
352 | | - |
353 | 300 | -- Columns that will likely be of interest soon: |
354 | 301 | -- proc.proargnames AS argument_names, |
355 | 302 |
|
356 | 303 | FROM |
357 | 304 | pg_catalog.pg_proc AS proc |
358 | 305 |
|
359 | | - INNER JOIN aggregate_argument_types |
360 | | - AS args |
361 | | - ON (proc.oid = args.proc_id) |
| 306 | + -- fetch the argument type name, discarding any unsupported types |
| 307 | + INNER JOIN scalar_types AS arg_type |
| 308 | + ON (arg_type.type_id = proc.proargtypes[0]) |
362 | 309 |
|
363 | | - INNER JOIN scalar_types |
364 | | - AS ret_type |
| 310 | + -- fetch the return type name, discarding any unsupported types |
| 311 | + INNER JOIN scalar_types AS ret_type |
365 | 312 | ON (ret_type.type_id = proc.prorettype) |
366 | 313 |
|
367 | | - -- Restrict our scope to only aggregation functions |
368 | | - INNER JOIN pg_aggregate |
369 | | - ON (pg_aggregate.aggfnoid = proc.oid) |
| 314 | + -- restrict our scope to only aggregation functions |
| 315 | + INNER JOIN pg_aggregate AS aggregate |
| 316 | + ON (aggregate.aggfnoid = proc.oid) |
370 | 317 |
|
371 | 318 | WHERE |
372 | | - -- We are only interested in functions: |
373 | | - -- * Which are aggregation functions. |
374 | | - -- * Which don't take any 'direct' (i.e., non-aggregation) arguments |
375 | | - pg_aggregate.aggnumdirectargs = 0 |
| 319 | + -- We are only interested in functions: |
| 320 | + -- * which take a single input argument |
| 321 | + -- * which are aggregation functions |
| 322 | + -- * which don't take any 'direct' (i.e., non-aggregation) arguments |
| 323 | + proc.pronargs = 1 |
| 324 | + AND aggregate.aggnumdirectargs = 0 |
376 | 325 |
|
377 | 326 | ), |
378 | 327 |
|
|
849 | 798 | ) |
850 | 799 | AS result |
851 | 800 | FROM |
852 | | - queryable_relations |
| 801 | + relations |
853 | 802 | AS rel |
854 | 803 |
|
855 | 804 | LEFT OUTER JOIN |
@@ -1055,10 +1004,9 @@ FROM |
1055 | 1004 | ) AS routines |
1056 | 1005 | FROM |
1057 | 1006 | ( |
1058 | | - -- We only support aggregation functions that take a single argument. |
1059 | | - SELECT DISTINCT ON (argument_type, proc_name) |
| 1007 | + SELECT |
1060 | 1008 | agg.proc_name, |
1061 | | - agg.argument_types[1] as argument_type, |
| 1009 | + agg.argument_type, |
1062 | 1010 | agg.return_type |
1063 | 1011 | FROM |
1064 | 1012 | aggregates AS agg |
|
0 commit comments