Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qg7bY-00EVsq-1k for pgsql-bugs@arkaria.postgresql.org; Tue, 12 Sep 2023 17:55:16 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1qg7bW-009xfY-IO for pgsql-bugs@arkaria.postgresql.org; Tue, 12 Sep 2023 17:55:14 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qg7bW-009xf9-9C for pgsql-bugs@lists.postgresql.org; Tue, 12 Sep 2023 17:55:14 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qg7bT-004Blh-4I for pgsql-bugs@lists.postgresql.org; Tue, 12 Sep 2023 17:55:12 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 38CHt9WW2820125; Tue, 12 Sep 2023 13:55:09 -0400 From: Tom Lane To: Aleksandr Vinokurov cc: pgsql-bugs@lists.postgresql.org Subject: Re: "as name" is shadowed by name "value" when selecting with "left join jsonb_array_elements(d.items) as item on true" In-reply-to: <7AC1DE23-F815-421B-8F01-C49A8764354D@gmail.com> References: <7AC1DE23-F815-421B-8F01-C49A8764354D@gmail.com> Comments: In-reply-to Aleksandr Vinokurov message dated "Tue, 12 Sep 2023 18:59:43 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <2820123.1694541309.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Tue, 12 Sep 2023 13:55:09 -0400 Message-ID: <2820124.1694541309@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Aleksandr Vinokurov writes: > select *, item as item > from (select '[1]'::jsonb as items) as d > left join jsonb_array_elements(d.items) as item on true; > items | value | item > -------+-------+------ > [1] | 1 | 1 > 1. It shows the name =E2=80=9Cvalue=E2=80=9D for the column for which th= e elements were requested to be named =E2=80=9Cas item=E2=80=9D, I see no bug here. In the FROM entry "jsonb_array_elements(d.items) as item", you've declared the *table* alias to be "item", but you left the column name(s) of the table unspecified --- and jsonb_array_elements declares its output argument to be named "value": =3D# \sf jsonb_array_elements CREATE OR REPLACE FUNCTION pg_catalog.jsonb_array_elements(from_json jsonb= , OUT value jsonb) RETURNS SETOF jsonb LANGUAGE internal IMMUTABLE PARALLEL SAFE STRICT ROWS 100 AS $function$jsonb_array_elements$function$ So "select *" expands the available columns as "items" from table "d" and "value" from table "item". Referencing "item" in the SELECT list is really a whole-table reference, although this isn't too obvious because we hack that to act identical to a column reference if the reference is to a scalar-producing function. To clarify what's happening, you could specify the column alias explicitly: =3D# select *, item as item from (select '[1]'::jsonb as items) as d left join jsonb_array_elements(d.items) as item(zed) on true; items | zed | item = -------+-----+------ [1] | 1 | 1 (1 row) or even =3D# select *, item.zed as item from (select '[1]'::jsonb as items) as d left join jsonb_array_elements(d.items) as item(zed) on true; items | zed | item = -------+-----+------ [1] | 1 | 1 (1 row) Most scalar-producing functions don't declare an output argument name, and in that case "as foo" works effectively like "as foo(foo)" to set both the table and column alias. I'm not sure why jsonb_array_elements goes out of its way to do this differently. regards, tom lane