Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kLWOj-00045N-Ug for pgsql-docs@arkaria.postgresql.org; Thu, 24 Sep 2020 18:55:17 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kLWOh-00053i-FR for pgsql-docs@arkaria.postgresql.org; Thu, 24 Sep 2020 18:55:15 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kLWOh-00052w-8z for pgsql-docs@lists.postgresql.org; Thu, 24 Sep 2020 18:55:15 +0000 Received: from momjian.us ([72.94.173.45]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kLWOe-0000DX-VQ for pgsql-docs@lists.postgresql.org; Thu, 24 Sep 2020 18:55:14 +0000 Received: from bruce by momjian.us with local (Exim 4.92) (envelope-from ) id 1kLWOd-0001Kr-2h; Thu, 24 Sep 2020 14:55:11 -0400 Date: Thu, 24 Sep 2020 14:55:11 -0400 From: Bruce Momjian To: Tom Lane Cc: "David G. Johnston" , Guyren Howe , Pg Docs Subject: Re: Please provide examples of rows from Message-ID: <20200924185511.GC22807@momjian.us> References: <158638264419.662.2482095087061084020@wrigleys.postgresql.org> <20200904014618.GB13932@momjian.us> <20200920003910.GA22746@momjian.us> <798909.1600562993@sss.pgh.pa.us> <20200920013846.GC22746@momjian.us> <835988.1600613834@sss.pgh.pa.us> <878265.1600625640@sss.pgh.pa.us> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="2fHTh5uZTiUOsy+g" Content-Disposition: inline In-Reply-To: <878265.1600625640@sss.pgh.pa.us> User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --2fHTh5uZTiUOsy+g Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Sun, Sep 20, 2020 at 02:14:00PM -0400, Tom Lane wrote: > I wrote: > > You're right that the only suitable core function is going to be > > json[b]_to_recordset, but I don't see why you can't extend the > > existing example for that. Something like > > Meh, I was too hasty and pushed "send" with a broken example. Better > > =# select * from rows from (json_to_recordset('[{"a":1,"b":"foo"},{"a":"2","b":"bar"}]') as (a int, b text), generate_series(1,3)) as x(p,q,s); > p | q | s > ---+-----+--- > 1 | foo | 1 > 2 | bar | 2 > | | 3 > (3 rows) Yes, this is very helpful. I was afraid the JSON would overwhelm the example, but this looks good. I wrote the attached doc patch which I think improves this. I plan to apply it to all supported versions. -- Bruce Momjian https://momjian.us EnterpriseDB https://enterprisedb.com The usefulness of a cup is in its emptiness, Bruce Lee --2fHTh5uZTiUOsy+g Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="row.diff" diff --git a/doc/src/sgml/queries.sgml b/doc/src/sgml/queries.sgml index 0a643ef597..f6dec5aaf6 100644 --- a/doc/src/sgml/queries.sgml +++ b/doc/src/sgml/queries.sgml @@ -762,7 +762,8 @@ SELECT * FROM vw_getfoo; In some cases it is useful to define table functions that can return different column sets depending on how they are invoked. To support this, the table function can be declared as returning - the pseudo-type record. When such a function is used in + the pseudo-type record with no OUT + parameters. When such a function is used in a query, the expected row structure must be specified in the query itself, so that the system can know how to parse and plan the query. This syntax looks like: @@ -771,7 +772,7 @@ SELECT * FROM vw_getfoo; function_call AS alias (column_definition , ... ) function_call AS alias (column_definition , ... ) -ROWS FROM( ... function_call AS (column_definition , ... ) , ... ) +ROWS FROM(function_call AS (column_definition , ... ) , ... ) @@ -803,6 +804,31 @@ SELECT * that the parser knows, for example, what * should expand to. + + + This example uses ROWS FROM: + +SELECT * +FROM ROWS FROM + ( + json_to_recordset('[{"a":40,"b":"foo"},{"a":"100","b":"bar"}]') + AS (a INTEGER, b TEXT), + generate_series(1, 3) + ) AS x (p, q, s) +ORDER BY p; + + p | q | s +-----+-----+--- + 40 | foo | 1 + 100 | bar | 2 + | | 3 + + It joins two functions into a single FROM target. + The first column is defined as returning an integer; the second, + text. The third column uses the function's return + type directly. The ORDER BY clause sorts its + column as an integer. + --2fHTh5uZTiUOsy+g--