Re: Extract numeric filed in JSONB more effectively - Mailing list pgsql-hackers

From Andy Fan
Subject Re: Extract numeric filed in JSONB more effectively
Date
Msg-id CAKU4AWrYOYSXMmotwhqiErci7aMz--8LxfpLiSg+0+7XNJBcuA@mail.gmail.com
Whole thread Raw
In response to Re: Extract numeric filed in JSONB more effectively  (Andy Fan <zhihui.fan1213@gmail.com>)
Responses Re: Extract numeric filed in JSONB more effectively
List pgsql-hackers
 
An incompatible issue at error message level is found during test: 
create table jb(a jsonb);
insert into jb select '{"a": "a"}'::jsonb;
select (a->'a')::int4 from jb;

master:   ERROR:  cannot cast jsonb string to type integer
patch:  ERROR:  cannot cast jsonb string to type numeric

That's mainly because we first extract the field to numeric and
then cast it to int4 and the error raised at the first step and it
doesn't know the final type.  One way to fix it is adding a 2nd
argument for jsonb_finish_numeric for the real type, but
it looks weird and more suggestions on this would be good. 


v12 is attached to address the above issue, I added a new argument
named target_oid for jsonb_finish_numeric so that it can raise a
correct error message.  I also fixed the issue reported by opr_sanity
in this version. 

Chap, do you still think we should refactor the code for the previous
existing functions like jsonb_object_field for less code duplication
purpose?  I think we should not do it because a). The code duplication
is just ~10 rows.  b).  If we do the refactor, we have to implement
two DirectFunctionCall1.   Point b) is the key reason I am not willing
to do it.  Or do I miss other important reasons? 

--
Best Regards
Andy Fan
Attachment

pgsql-hackers by date:

Previous
From: Richard Guo
Date:
Subject: Re: Should we use MemSet or {0} for struct initialization?
Next
From: Corey Huinker
Date:
Subject: Re: SQL:2011 application time