pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #13996: json_to_record() returns non-NULL, malformed value for omitted key under some circumstances
4+ messages / 3 participants
[nested] [flat]

* BUG #13996: json_to_record() returns non-NULL, malformed value for omitted key under some circumstances
@ 2016-03-02 08:03 johann@visagie.za.net
  2016-03-03 01:04 ` Re: BUG #13996: json_to_record() returns non-NULL, malformed value for omitted key under some circumstances Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 4+ messages in thread

From: johann@visagie.za.net @ 2016-03-02 08:03 UTC (permalink / raw)
  To: pgsql-bugs

The following bug has been logged on the website:

Bug reference:      13996
Logged by:          Johann Visagie
Email address:      johann@visagie.za.net
PostgreSQL version: 9.5.1
Operating system:   OS X 10.11.3
Description:        

In the following call to json_to_record(), the target row format `t`
specifies a text column `c` which does *not* appear as a key in the JSON
object used as the function’s argument.

SELECT t.*
FROM json_to_record('{"a":1, "b":{"c":16, "d":2}, "x":8}'::json)
AS t(a int, b json, c text, x int);

By the definition of the function in section 9.15 of the manual, the
statement should return a NULL value for the column `c`.  This is made
explicit in the second Note at the bottom of that manual page, which states
i.a.:

>JSON fields that do not appear in the target row type will be omitted from
the output, and target columns that do not match any JSON field will simply
be NULL.

However, if (as in this example) another key in the JSON object — `b` in
this case — refers to a nested JSON object which *does* contain a key `c`,
the function returns a malformed JSON string as (text)  value for column
`c`:

 a |       b        |    c    | x 
---+----------------+---------+---
 1 | {"c":16,"d":2} | {"c":16 | 8
(1 row)

I have not tested whether json_ro_recordset() exhibits the same
misbehaviour.


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: BUG #13996: json_to_record() returns non-NULL, malformed value for omitted key under some circumstances
  2016-03-02 08:03 BUG #13996: json_to_record() returns non-NULL, malformed value for omitted key under some circumstances johann@visagie.za.net
@ 2016-03-03 01:04 ` Tom Lane <tgl@sss.pgh.pa.us>
  2016-03-03 03:00   ` Re: BUG #13996: json_to_record() returns non-NULL, malformed value for omitted key under some circumstances Andrew Dunstan <andrew@dunslane.net>
  0 siblings, 1 reply; 4+ messages in thread

From: Tom Lane @ 2016-03-03 01:04 UTC (permalink / raw)
  To: johann@visagie.za.net; +Cc: pgsql-bugs@postgresql.org, Andrew Dunstan <andrew@dunslane.net>

johann@visagie.za.net writes:
> SELECT t.*
> FROM json_to_record('{"a":1, "b":{"c":16, "d":2}, "x":8}'::json)
> AS t(a int, b json, c text, x int);

> However, if (as in this example) another key in the JSON object - `b` in
> this case - refers to a nested JSON object which *does* contain a key `c`,
> the function returns a malformed JSON string as (text)  value for column
> `c`:

>  a |       b        |    c    | x
> ---+----------------+---------+---
>  1 | {"c":16,"d":2} | {"c":16 | 8
> (1 row)


AFAICT this is a simple thinko in the hash_object_field_end() callback,
as per attached patch that fixes this and doesn't break any existing
regression test cases.  Andrew, do you concur that this is correct,
or is there something I'm missing about the tracking of lex_level?

			regards, tom lane

-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs

Attachments:

fix-nested-objects-in-json-to-record.patch (text/x-diff, 481B) download diff --git a/src/backend/utils/adt/jsonfuncs.c b/src/backend/utils/adt/jsonfuncs.c index 88225aa..363afa7 100644 *** a/src/backend/utils/adt/jsonfuncs.c --- b/src/backend/utils/adt/jsonfuncs.c *************** hash_object_field_end(void *state, char *** 2438,2444 **** /* * Ignore nested fields. */ ! if (_state->lex->lex_level > 2) return; /* --- 2438,2444 ---- /* * Ignore nested fields. */ ! if (_state->lex->lex_level > 1) return; /*
^ permalink raw reply [nested|flat] 4+ messages in thread

* Re: BUG #13996: json_to_record() returns non-NULL, malformed value for omitted key under some circumstances
  2016-03-02 08:03 BUG #13996: json_to_record() returns non-NULL, malformed value for omitted key under some circumstances johann@visagie.za.net
  2016-03-03 01:04 ` Re: BUG #13996: json_to_record() returns non-NULL, malformed value for omitted key under some circumstances Tom Lane <tgl@sss.pgh.pa.us>
@ 2016-03-03 03:00   ` Andrew Dunstan <andrew@dunslane.net>
  2016-03-03 03:48     ` Re: BUG #13996: json_to_record() returns non-NULL, malformed value for omitted key under some circumstances Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 4+ messages in thread

From: Andrew Dunstan @ 2016-03-03 03:00 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; johann@visagie.za.net; +Cc: pgsql-bugs



On 03/02/2016 08:04 PM, Tom Lane wrote:
> johann@visagie.za.net writes:
>> SELECT t.*
>> FROM json_to_record('{"a":1, "b":{"c":16, "d":2}, "x":8}'::json)
>> AS t(a int, b json, c text, x int);
>> However, if (as in this example) another key in the JSON object - `b` in
>> this case - refers to a nested JSON object which *does* contain a key `c`,
>> the function returns a malformed JSON string as (text)  value for column
>> `c`:
>>   a |       b        |    c    | x
>> ---+----------------+---------+---
>>   1 | {"c":16,"d":2} | {"c":16 | 8
>> (1 row)
>
> AFAICT this is a simple thinko in the hash_object_field_end() callback,
> as per attached patch that fixes this and doesn't break any existing
> regression test cases.  Andrew, do you concur that this is correct,
> or is there something I'm missing about the tracking of lex_level?
>
> 			


Looks like you're right. lex_level is incremented at object/array start 
and decremented at object/array end. So keys of the outermost object 
will be at lex_level 1, and we shouldn't be using anything at a higher 
level. I guess we should add something like this as an extra regression 
test.

cheers

andrew


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 4+ messages in thread

* Re: BUG #13996: json_to_record() returns non-NULL, malformed value for omitted key under some circumstances
  2016-03-02 08:03 BUG #13996: json_to_record() returns non-NULL, malformed value for omitted key under some circumstances johann@visagie.za.net
  2016-03-03 01:04 ` Re: BUG #13996: json_to_record() returns non-NULL, malformed value for omitted key under some circumstances Tom Lane <tgl@sss.pgh.pa.us>
  2016-03-03 03:00   ` Re: BUG #13996: json_to_record() returns non-NULL, malformed value for omitted key under some circumstances Andrew Dunstan <andrew@dunslane.net>
@ 2016-03-03 03:48     ` Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 0 replies; 4+ messages in thread

From: Tom Lane @ 2016-03-03 03:48 UTC (permalink / raw)
  To: Andrew Dunstan <andrew@dunslane.net>; +Cc: johann@visagie.za.net; pgsql-bugs

Andrew Dunstan <andrew@dunslane.net> writes:
> On 03/02/2016 08:04 PM, Tom Lane wrote:
>> AFAICT this is a simple thinko in the hash_object_field_end() callback,
>> as per attached patch that fixes this and doesn't break any existing
>> regression test cases.  Andrew, do you concur that this is correct,
>> or is there something I'm missing about the tracking of lex_level?

> Looks like you're right. lex_level is incremented at object/array start 
> and decremented at object/array end. So keys of the outermost object 
> will be at lex_level 1, and we shouldn't be using anything at a higher 
> level. I guess we should add something like this as an extra regression 
> test.

Yeah, I was going to adopt exactly this test case ...

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



^ permalink  raw  reply  [nested|flat] 4+ messages in thread


end of thread, other threads:[~2016-03-03 03:48 UTC | newest]

Thread overview: 4+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2016-03-02 08:03 BUG #13996: json_to_record() returns non-NULL, malformed value for omitted key under some circumstances johann@visagie.za.net
2016-03-03 01:04 ` Tom Lane <tgl@sss.pgh.pa.us>
2016-03-03 03:00   ` Andrew Dunstan <andrew@dunslane.net>
2016-03-03 03:48     ` Tom Lane <tgl@sss.pgh.pa.us>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox