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 1qgLqk-00FPnT-Ue for pgsql-bugs@arkaria.postgresql.org; Wed, 13 Sep 2023 09:07:54 +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 1qgLqj-00G2nW-RA for pgsql-bugs@arkaria.postgresql.org; Wed, 13 Sep 2023 09:07:53 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qg8ff-00A9x6-Pw for pgsql-bugs@lists.postgresql.org; Tue, 12 Sep 2023 19:03:35 +0000 Received: from mail-ej1-x636.google.com ([2a00:1450:4864:20::636]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qg8fc-004hTG-7N for pgsql-bugs@lists.postgresql.org; Tue, 12 Sep 2023 19:03:34 +0000 Received: by mail-ej1-x636.google.com with SMTP id a640c23a62f3a-99bdcade7fbso745683366b.1 for ; Tue, 12 Sep 2023 12:03:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1694545411; x=1695150211; darn=lists.postgresql.org; h=to:in-reply-to:cc:references:message-id:date:subject:mime-version :from:content-transfer-encoding:from:to:cc:subject:date:message-id :reply-to; bh=1xtrn2LoNXOfeJjI1jqhPjOrrSNl+Go9c8HN2CGwFZg=; b=TkyVPQVhyWwQJEJ5lBejkKL2UsYjcHnvotl9zLSrz8LJSibHmDV4r7H/x0vtCIu6K9 1SGkLj7dP5/ssTk47JdBdFpxEn88X2eNPyTPuSfosgqB05vs0hzZkQP1x0tqOZdyVaxP jsOxjmBXV9bon6EWrk1GdTwcTYGij1+v6cgODlUd9q/5fQ+CFhimFPLez+kw04UICpSd scIPD+bRre24ufeNpU6sCTn6JMGkgamZA9m2qAT7TbJ4cGhnNWs0dHxNGw3QiDQGHC1N MFuODIclX4DUj0lr3h/vKfI8//HbNPYsvANQPot/HXUsZgHTUO9B6bErYNbUKTXzZeCD reAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694545411; x=1695150211; h=to:in-reply-to:cc:references:message-id:date:subject:mime-version :from:content-transfer-encoding:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=1xtrn2LoNXOfeJjI1jqhPjOrrSNl+Go9c8HN2CGwFZg=; b=e1Q8P8TIBE2HJloXlGEhQf/5jToeb1db5Kv+/NFQxul0Fpbm/LOuAxs3UKTJA0ppav UX04HnabB/uPabA+ZCWqT8YEa5vYgbgeBHIot5GmpRAT/YChpX3FebvfQffnGr4lhbD+ EU4UWbaUVwO/WGHPyOSeS9osKfqvRDhIeu3dgzNwFw4RvxqR4A/8tVZxDz3qg7+qvpUN lWc7iI0KUlDtcrBosdbZQpz7kRz8pLbbf3bDFi4IQyZHr2OOtBBRr9K2j02wmqYlp5UN N9CZz8dVv9Om7t1PvwdZodD0a1/xHER8TDifRTcP82j7WPtUg0lIDfRfCvnKIzFHmQ/w iMhg== X-Gm-Message-State: AOJu0Yy0iQrOewRYbhJRt5QenCtYNJL0BmKoxKDV5zw+l4H2ou464SGk RoUGo5fl6bjK0Kl/W2s0jl/9JDoYIg8= X-Google-Smtp-Source: AGHT+IHWbsnTGS6xVQAVQtdfn8JsjvJz+l2nOiCaRTV/U91Sk0FRy+AkgM/Cry7SP0KKSnHF/MYaXw== X-Received: by 2002:a17:906:310b:b0:9a6:4fe0:4552 with SMTP id 11-20020a170906310b00b009a64fe04552mr198298ejx.46.1694545410878; Tue, 12 Sep 2023 12:03:30 -0700 (PDT) Received: from smtpclient.apple ([2a02:58:14a:c400:d885:33e5:f45b:65ed]) by smtp.gmail.com with ESMTPSA id i10-20020a170906114a00b00992076f4a01sm7167528eja.190.2023.09.12.12.03.30 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 12 Sep 2023 12:03:30 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: Aleksandr Vinokurov Mime-Version: 1.0 (1.0) Subject: Re: "as name" is shadowed by name "value" when selecting with "left join jsonb_array_elements(d.items) as item on true" Date: Tue, 12 Sep 2023 21:03:20 +0200 Message-Id: <286F430B-6939-4C07-BF5B-DFD2BBE94E7A@gmail.com> References: <2820124.1694541309@sss.pgh.pa.us> Cc: pgsql-bugs@lists.postgresql.org In-Reply-To: <2820124.1694541309@sss.pgh.pa.us> To: Tom Lane X-Mailer: iPhone Mail (20D67) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Oh, That's very interesting, thanks a lot for quick response. And have a nice ev= ening. With best regards, Aleksandr Vinokourov > On 12 Sep 2023, at 19:55, Tom Lane wrote: >=20 > =EF=BB=BFAleksandr 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; >=20 >> items | value | item >> -------+-------+------ >> [1] | 1 | 1 >=20 >> 1. It shows the name =E2=80=9Cvalue=E2=80=9D for the column for which the= elements were requested to be named =E2=80=9Cas item=E2=80=9D, >=20 > 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": >=20 > =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$ >=20 > 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. >=20 > To clarify what's happening, you could specify the column alias > explicitly: >=20 > =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=20 > -------+-----+------ > [1] | 1 | 1 > (1 row) >=20 > or even >=20 > =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=20 > -------+-----+------ > [1] | 1 | 1 > (1 row) >=20 > 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. >=20 > regards, tom lane