Re: Extract numeric filed in JSONB more effectively

Andy Fan <zhihuifan1213@163.com>

From: Andy Fan <zhihuifan1213@163.com>
To: Dmitry Dolgov <9erthalion6@gmail.com>
Cc: David Rowley <dgrowleyml@gmail.com>, Peter Eisentraut <peter@eisentraut.org>, Amit Langote <amitlangote09@gmail.com>, Alvaro Herrera <alvherre@alvh.no-ip.org>, jian he <jian.universality@gmail.com>, Chapman Flack <chap@anastigmatix.net>, pgsql-hackers@lists.postgresql.org
Date: 2024-11-18T00:23:52Z
Lists: pgsql-hackers
Hi Dmitry,

>> On Thu, Sep 12, 2024 at 03:03:18AM GMT, Andy Fan wrote:
>>
>> > 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.
>
> Any particular reason you want to keep number of functions minimal? Is
> it just to make the patch smaller? I might be missing something without
> looking at the implementation in details, but the difference between 10
> and 7 functions doesn't seem to be significant.

Another reason is for reducing code duplication, writting too many
similar function looks not good to me. Chapman expressed this idea
first at [1]. Search "it would make me happy to further reduce some
of the code" in the message.

Acutally this doesn't make the patch complexer too much.

[1]
https://www.postgresql.org/message-id/5138c6b5fd239e7ce4e1a4e63826ac27%40anastigmatix.net 


-- 
Best Regards
Andy Fan




Commits

Same data as JSON: GET /api/v1/messages/:b64id/commits the thread's linked commits as JSON, with link sources. API reference →
  1. Add trailing commas to enum definitions