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 1qVSWo-000FfI-No for pgsql-hackers@arkaria.postgresql.org; Mon, 14 Aug 2023 08:02:19 +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 1qVSWm-002C5W-8U for pgsql-hackers@arkaria.postgresql.org; Mon, 14 Aug 2023 08:02:16 +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 1qVSWl-002C5O-Ro for pgsql-hackers@lists.postgresql.org; Mon, 14 Aug 2023 08:02:16 +0000 Received: from mail-yb1-xb2b.google.com ([2607:f8b0:4864:20::b2b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qVSWg-0003Tg-Qn for pgsql-hackers@postgresql.org; Mon, 14 Aug 2023 08:02:15 +0000 Received: by mail-yb1-xb2b.google.com with SMTP id 3f1490d57ef6-d678b44d1f3so3354089276.0 for ; Mon, 14 Aug 2023 01:02:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1692000129; x=1692604929; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=R37EB8RAcyUwa9ptlYsY/ZLE+2Shwcp5jsD5HRbxkJ8=; b=jhWjZdIPKjPzyhneoAT4la0skb3CN/IGEGoY+AXGXrtaHs5fvUf5TrDCH1abCnfwGm icyCg98yoszt7bx4+Q9AT6UoZHaCeUJIuA/gHHjkjThSKxIhg3JvodxlNwI92KC2UUN3 CpKlLY1NdCvFy17fkarauyyF7oocg+2QgPgh+5AuY6jiZ0fzkknyLjJrsQtZqxSR/lI0 thKVngoXMyeqYOWpFjp+Q2/PrMTKwrw0sIsItTXvEYC5oj9SHZ6q8IB+h2hioYFuhXWo rCG7PymQn+Lm2uo973m9noTCnLcbavnmFFqUvrvQELR34yKdHLV4GKnLJvET5t4zoxCd 1/eA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1692000129; x=1692604929; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=R37EB8RAcyUwa9ptlYsY/ZLE+2Shwcp5jsD5HRbxkJ8=; b=E3IRHZK2KJBZcAXdGLbLftpWuXZ7PfizOXsJ4dWUqJwTyHVEf76QyYtpFbkw9RLyW1 k2pOu9toGQzKdMOEuxK0jmT05SnbJSOHnGeFykSDhkmxM6DRcEqZQfUrJs95fRBpVBQC OUdS//YxcBjd8B3krIYyxj4/CU7bHxok/RGBKOzeULbxufBYYVBrf5X1ytVoTOwRGN0s QRkuo4ucvH6zsVNKJFqF1eslsm1Fp6dr9Uzc5LbQ34UoLjwjpOXl7JHvyadcuXQWbhvS DrZ6p98+zYVmxUC9jC9exOd7/O2AgujJkMy/DqjIhqyE6mL9GqzzwCJSrdORfcQKzc16 T4jw== X-Gm-Message-State: AOJu0YwDX2OsNIr5AOD7xjn0/sy1CQZv/3+Rz8NZA6sfKf0/41+YZ6me v/YcGqoWVxmusqIFZjnR++9MvVGLPfAGOF3/wUM= X-Google-Smtp-Source: AGHT+IGIB3XRH6Y21mZaz6o6h6zPsDYgC4akjTyUciXt1sI6Pq07EghS8yr42TNq/DotcJK8mtKQ5pOSshb+danF/a4= X-Received: by 2002:a25:d341:0:b0:d62:81c4:1f73 with SMTP id e62-20020a25d341000000b00d6281c41f73mr15155827ybf.10.1692000128905; Mon, 14 Aug 2023 01:02:08 -0700 (PDT) MIME-Version: 1.0 References: <3507485.1691090027@sss.pgh.pa.us> In-Reply-To: From: Pavel Stehule Date: Mon, 14 Aug 2023 10:01:31 +0200 Message-ID: Subject: Re: Extract numeric filed in JSONB more effectively To: Andy Fan Cc: Tom Lane , jian he , pgsql-hackers Content-Type: multipart/alternative; boundary="000000000000a4b5e10602dd7abb" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --000000000000a4b5e10602dd7abb Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable po 14. 8. 2023 v 9:06 odes=C3=ADlatel Andy Fan n= apsal: > >> We'd still have functions like jsonb_field_as_numeric() under the >> hood, but there's not an expectation that users call them explicitly. >> > > To avoid the lots of functions like jsonb_field_as_int2/int4, I defined > Datum jsonb_object_field_type(.., Oid target_oid) at last, so the > function must return "internal" or "anyelement". Then we can see: > > select jsonb_object_field_type(tb.a, 'a'::text, 1700) from tb; > ERROR: cannot display a value of type anyelement. > you cannot to use type as parameter. There should be some typed value - lik= e jsonb_object_field, '{"a":10}', 'a', NULL::int) and return type should be anyelement. Another solution should be more deeper change like implementation of "coalesce" > > The reason is clear to me, but I'm not sure how to fix that or deserves > a fix? Or shall I define jsonb_object_field_int2/int8 to avoid this? > > This is an unresolved issue at the latest patch. > -- > Best Regards > Andy Fan > --000000000000a4b5e10602dd7abb Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
po 14. 8. 2023 v=C2=A09:06 odes=C3=AD= latel Andy Fan <zhihui.fan12= 13@gmail.com> napsal:

We'd still have functions like jsonb_field_as_numeric() under the
hood, but there's not an expectation that users call them explicitly.

To avoid the lots of functions like json= b_field_as_int2/int4, I defined
Datum jsonb_object_field_type(..,= Oid target_oid) at last,=C2=A0 so the
function must return "= ;internal" or "anyelement".=C2=A0 Then we can see:

select jsonb_object_field_type(tb.a, 'a'::text, 1700) fr= om tb;
ERROR: =C2=A0cannot display a value of type anyelement.

you cannot to use type as parameter. The= re should be some typed value - like

jsonb_object_= field, '{"a":10}', 'a', NULL::int)

=
and return type should be anyelement.

A= nother solution should be more deeper change like implementation of "c= oalesce"

=C2=A0
<= br>
The reason is clear to me, but=C2=A0 I&= #39;m not sure how to fix=C2=A0that or deserves
a fix? Or shall I define jsonb_object_field_int2/int8 to avoid this?= =C2=A0

This is an unres= olved issue at the latest patch.=C2=A0
--
Best Regards
Andy Fan
--000000000000a4b5e10602dd7abb--