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 1soa97-00CJPc-39 for pgsql-hackers@arkaria.postgresql.org; Thu, 12 Sep 2024 03:05:26 +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 1soa94-00DfMi-Pi for pgsql-hackers@arkaria.postgresql.org; Thu, 12 Sep 2024 03:05:22 +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 1soa93-00DfHI-RP for pgsql-hackers@lists.postgresql.org; Thu, 12 Sep 2024 03:05:22 +0000 Received: from m16.mail.163.com ([117.135.210.4]) by makus.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1soa8w-000jTt-JN for pgsql-hackers@lists.postgresql.org; Thu, 12 Sep 2024 03:05:19 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com; s=s110527; h=From:Subject:Date:Message-ID:MIME-Version: Content-Type; bh=ApzbKdLRVCQ1HtmHdT2nDhlObAJDTWA99d0FrQZMxaA=; b=SnU1wFVqzNwT17dPFqJOk7LR8xocnBlzp2sovMB+pxgz2T2NxqkCkjcap18qWa s57BJ2XKo56kmtFGVOiH0yqusCZXcrbSENK8US6BnAv1tgL09hiNqgExMQYRVfP8 M3cigedHSIG9MBiL0pfgG3YP+9NjmqyKAHp4vL2D8oT9c= Received: from lovely-coding (unknown [219.151.179.98]) by gzga-smtp-mta-g3-3 (Coremail) with SMTP id _____wDnL4r3WeJme1wjDA--.44943S3; Thu, 12 Sep 2024 11:03:19 +0800 (CST) From: Andy Fan To: David Rowley Cc: Peter Eisentraut , Amit Langote , Alvaro Herrera , jian he , Chapman Flack , pgsql-hackers@lists.postgresql.org Subject: Re: Extract numeric filed in JSONB more effectively In-Reply-To: (David Rowley's message of "Thu, 12 Sep 2024 09:00:41 +1200") References: <169880504467.94392.3769687331705514588.pgcf@coridan.postgresql.org> <87a5rry0bz.fsf@163.com> <87h6jpob9x.fsf@163.com> <87r0hmvuvr.fsf@163.com> <8102ff5b-b156-409e-a48f-e53e63a39b36@eisentraut.org> <8734t6c5rh.fsf@163.com> <87o7bn7z56.fsf@163.com> <875xx197bp.fsf@163.com> <87ttk0lgcx.fsf@163.com> Date: Thu, 12 Sep 2024 03:03:18 +0000 Message-ID: <8734m5fua1.fsf@163.com> MIME-Version: 1.0 Content-Type: text/plain X-CM-TRANSID:_____wDnL4r3WeJme1wjDA--.44943S3 X-Coremail-Antispam: 1Uf129KBjvJXoW7uF1xXrW3Cw47XF43ZFyrJFb_yoW8Zr4Upr WrCF9rCr1kCrZrCwsrXan3Jwn5Ga93Ar4UJwn0qw1qkw4Yqws2gF4ktr1YyayUJr13trZ3 Za909F1qya1DuFJanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07UyE_iUUUUU= X-Originating-IP: [219.151.179.98] X-CM-SenderInfo: x2klx3xlid0iqsrtqiywtou0bp/1tbiNhhYU2XAnecM+gAAsM List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hello David, Thanks for checking this! > There's a lot of complexity in the v18 patch that I don't understand > the need for. > > I imagined you'd the patch should create a SupportRequestSimplify > support function for jsonb_numeric() that checks if the input > expression is an OpExpr with funcid of jsonb_object_field(). All you > do then is ditch the cast and change the OpExpr to call a new function > named jsonb_object_field_numeric() which returns the val.numeric > directly. Likely the same support function could handle jsonb casts > to other types too, in which case you'd just call some other function, > e.g jsonb_object_field_timestamp() or jsonb_object_field_boolean(). Basically yes. The reason complexity comes when we many operators we want to optimize AND my patch I want to reduce the number of function created. The optimized functions and operators includes: 1. jsonb_object_field / -> 2. jsonb_array_element / -> 3. jsonb_extract_path / #> 4. jsonb_path_query 5. jsonb_path_query_first > ..., in which case you'd just call some other function, > e.g jsonb_object_field_timestamp() or jsonb_object_field_boolean(). This works, but We need to create 2 functions for each operator. In the patched case, we have 5 operators, so we need to create 10 functions. op[1,2,3,4,5]_bool op[1,2,3,4,5]_numeric. Within the start / finish function, we need to create *7* functions. op[1,2,3,4,5]_start : return the "JsonbVaue". jsonb_finish_numeric: convert jsonbvalue to numeric jsonb_finish_bool : convert jsonbvalue to bool. I think the above is the major factor for the additional complexity. Some other factors contribute to complexity a bit. 1. we also have jsonb_int{2,4,8}/float{4,8} in pg_proc for '->' operator, not only numeric. 2. user may use OpExpr, like (jb->'x')::numeric, user may also use FuncExpr, like (jsonb_object_field(a, 'x'))::numeric. -- Best Regards Andy Fan