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 1sZbZl-0029Zc-TK for pgsql-bugs@arkaria.postgresql.org; Thu, 01 Aug 2024 19:35:01 +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 1sZbZk-00CwHZ-9k for pgsql-bugs@arkaria.postgresql.org; Thu, 01 Aug 2024 19:35:00 +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 1sZbZk-00CwHR-0v for pgsql-bugs@lists.postgresql.org; Thu, 01 Aug 2024 19:35:00 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sZbZh-002ejU-M1 for pgsql-bugs@lists.postgresql.org; Thu, 01 Aug 2024 19:34:59 +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 471JYtZb3783689; Thu, 1 Aug 2024 15:34:55 -0400 From: Tom Lane To: "David G. Johnston" cc: sunnybluemoon050@gmail.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #18564: Incorrect sorting with using NULLS LAST (NULLS FIRST) In-reply-to: References: <18564-5985f90678ed7512@postgresql.org> Comments: In-reply-to "David G. Johnston" message dated "Thu, 01 Aug 2024 10:36:08 -0700" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <3783687.1722540895.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Thu, 01 Aug 2024 15:34:55 -0400 Message-ID: <3783688.1722540895@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk "David G. Johnston" writes: > On Thu, Aug 1, 2024 at 10:19 AM PG Bug reporting form < > noreply@postgresql.org> wrote: >> I am encountering a bug when performing a query with sorting on a nullable >> float field within a jsonb column, using NULLS LAST. > Since a JSON typed null value is not an SQL NULL value the order by > machinery sees a perfectly valid non-null value to be sorted alongside the > non-null data. When forcing the json to be text the cast does convert a > json null value to a SQL text NULL value I agree it's not a bug, because a json null isn't in itself a SQL null. You can get the desired result by =# SELECT example_jsonb_column->'sorting_param' FROM example_table ORDER BY (example_jsonb_column->>'sorting_param')::float ASC NULLS LAST; ?column? ---------- 1.0 2.0 null null (4 rows) This is fairly awkward though because it's converting to text and thence to numeric. I initially tried =# SELECT example_jsonb_column->'sorting_param' FROM example_table ORDER BY (example_jsonb_column->'sorting_param')::float ASC NULLS LAST; ERROR: cannot cast jsonb null to type double precision which seems to me, if not a bug, at least very poorly-chosen behavior. If we allow casting of json null to a SQL null for text values, why not for values of other types? regards, tom lane