pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #8218: Error when querying an JSON data, 9.3beta
5+ messages / 4 participants
[nested] [flat]
* BUG #8218: Error when querying an JSON data, 9.3beta
@ 2013-06-09 15:12 daniel.zlatev@gmail.com
0 siblings, 1 reply; 5+ messages in thread
From: daniel.zlatev@gmail.com @ 2013-06-09 15:12 UTC (permalink / raw)
To: pgsql-bugs
The following bug has been logged on the website:
Bug reference: 8218
Logged by: Daniel Zlatev
Email address: daniel.zlatev@gmail.com
PostgreSQL version: Unsupported/Unknown
Operating system: Windows
Description:
Dears,
today I was playing with some JSON data, and I have noticed something
strange.
1. I've created the follwing table:
CREATE TABLE products (
data JSON
);
2. Then inserting some data:
INSERT INTO products(data) VALUES('{"id": 1, "name": "shoes", "in_stock":
5}');
INSERT INTO products(data) VALUES('[1,2,3,4,5]');
3. Then i've run this SELECT statement
SELECT * FROM products WHERE (data->>'in_stock')::integer > 0
Output was:
[Err] ERROR: cannot extract field from a non-object
I can understand the reason behind this error(JSON array don't has fields),
but for me it is very logical postgres to exclude this row from the
returning set, rather to throw an error.
So that is it.
Best regards
Daniel
--
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] 5+ messages in thread
* Re: BUG #8218: Error when querying an JSON data, 9.3beta
@ 2013-06-09 17:24 Tom Lane <tgl@sss.pgh.pa.us>
parent: daniel.zlatev@gmail.com
0 siblings, 1 reply; 5+ messages in thread
From: Tom Lane @ 2013-06-09 17:24 UTC (permalink / raw)
To: daniel.zlatev@gmail.com; +Cc: pgsql-bugs
daniel.zlatev@gmail.com writes:
> CREATE TABLE products (
> data JSON
> );
> INSERT INTO products(data) VALUES('{"id": 1, "name": "shoes", "in_stock": 5}');
> INSERT INTO products(data) VALUES('[1,2,3,4,5]');
> SELECT * FROM products WHERE (data->>'in_stock')::integer > 0
> Output was:
> [Err] ERROR: cannot extract field from a non-object
> I can understand the reason behind this error(JSON array don't has fields),
> but for me it is very logical postgres to exclude this row from the
> returning set, rather to throw an error.
Hm. In principle we could allow ->> to return NULL rather than failing
when there's no such field, but I'm not sure that would represent good
language design. However, this example definitely shows there are some
holes in the current set of JSON manipulation functions. The only way
to avoid a failure here would be to write something like
WHERE (CASE WHEN json_has_field(data, 'in_stock') THEN
(data->>'in_stock')::integer ELSE NULL::integer END) > 0
but there is no "json_has_field" test function, nor any nice way to
build one from the provided functions.
It's probably too late to address this for 9.3, but we ought to put it
on the to-do list for 9.4.
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] 5+ messages in thread
* Re: BUG #8218: Error when querying an JSON data, 9.3beta
@ 2014-01-24 03:05 Bruce Momjian <bruce@momjian.us>
parent: Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 5+ messages in thread
From: Bruce Momjian @ 2014-01-24 03:05 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: daniel.zlatev@gmail.com; pgsql-bugs; Andrew Dunstan <andrew@dunslane.net>
On Sun, Jun 9, 2013 at 01:24:26PM -0400, Tom Lane wrote:
> daniel.zlatev@gmail.com writes:
> > CREATE TABLE products (
> > data JSON
> > );
> > INSERT INTO products(data) VALUES('{"id": 1, "name": "shoes", "in_stock": 5}');
> > INSERT INTO products(data) VALUES('[1,2,3,4,5]');
> > SELECT * FROM products WHERE (data->>'in_stock')::integer > 0
>
> > Output was:
> > [Err] ERROR: cannot extract field from a non-object
>
> > I can understand the reason behind this error(JSON array don't has fields),
> > but for me it is very logical postgres to exclude this row from the
> > returning set, rather to throw an error.
>
> Hm. In principle we could allow ->> to return NULL rather than failing
> when there's no such field, but I'm not sure that would represent good
> language design. However, this example definitely shows there are some
> holes in the current set of JSON manipulation functions. The only way
> to avoid a failure here would be to write something like
>
> WHERE (CASE WHEN json_has_field(data, 'in_stock') THEN
> (data->>'in_stock')::integer ELSE NULL::integer END) > 0
>
> but there is no "json_has_field" test function, nor any nice way to
> build one from the provided functions.
>
> It's probably too late to address this for 9.3, but we ought to put it
> on the to-do list for 9.4.
Was this addressed for 9.4 because I don't see it?
--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com
+ Everyone has their own god. +
--
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] 5+ messages in thread
* Re: BUG #8218: Error when querying an JSON data, 9.3beta
@ 2014-01-24 04:32 Andrew Dunstan <andrew@dunslane.net>
parent: Bruce Momjian <bruce@momjian.us>
0 siblings, 1 reply; 5+ messages in thread
From: Andrew Dunstan @ 2014-01-24 04:32 UTC (permalink / raw)
To: Bruce Momjian <bruce@momjian.us>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; daniel.zlatev@gmail.com; pgsql-bugs
On 01/23/2014 10:05 PM, Bruce Momjian wrote:
> On Sun, Jun 9, 2013 at 01:24:26PM -0400, Tom Lane wrote:
>> daniel.zlatev@gmail.com writes:
>>> CREATE TABLE products (
>>> data JSON
>>> );
>>> INSERT INTO products(data) VALUES('{"id": 1, "name": "shoes", "in_stock": 5}');
>>> INSERT INTO products(data) VALUES('[1,2,3,4,5]');
>>> SELECT * FROM products WHERE (data->>'in_stock')::integer > 0
>>> Output was:
>>> [Err] ERROR: cannot extract field from a non-object
>>> I can understand the reason behind this error(JSON array don't has fields),
>>> but for me it is very logical postgres to exclude this row from the
>>> returning set, rather to throw an error.
>> Hm. In principle we could allow ->> to return NULL rather than failing
>> when there's no such field, but I'm not sure that would represent good
>> language design. However, this example definitely shows there are some
>> holes in the current set of JSON manipulation functions. The only way
>> to avoid a failure here would be to write something like
>>
>> WHERE (CASE WHEN json_has_field(data, 'in_stock') THEN
>> (data->>'in_stock')::integer ELSE NULL::integer END) > 0
>>
>> but there is no "json_has_field" test function, nor any nice way to
>> build one from the provided functions.
>>
>> It's probably too late to address this for 9.3, but we ought to put it
>> on the to-do list for 9.4.
> Was this addressed for 9.4 because I don't see it?
>
9.4 does have json_typeof(), which should let you construct an adequate
test along the lines Tom suggests.
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] 5+ messages in thread
* Re: BUG #8218: Error when querying an JSON data, 9.3beta
@ 2014-01-24 16:34 Bruce Momjian <bruce@momjian.us>
parent: Andrew Dunstan <andrew@dunslane.net>
0 siblings, 0 replies; 5+ messages in thread
From: Bruce Momjian @ 2014-01-24 16:34 UTC (permalink / raw)
To: Andrew Dunstan <andrew@dunslane.net>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; daniel.zlatev@gmail.com; pgsql-bugs
On Thu, Jan 23, 2014 at 11:32:25PM -0500, Andrew Dunstan wrote:
>
> On 01/23/2014 10:05 PM, Bruce Momjian wrote:
> >On Sun, Jun 9, 2013 at 01:24:26PM -0400, Tom Lane wrote:
> >>daniel.zlatev@gmail.com writes:
> >>>CREATE TABLE products (
> >>> data JSON
> >>>);
> >>>INSERT INTO products(data) VALUES('{"id": 1, "name": "shoes", "in_stock": 5}');
> >>>INSERT INTO products(data) VALUES('[1,2,3,4,5]');
> >>>SELECT * FROM products WHERE (data->>'in_stock')::integer > 0
> >>>Output was:
> >>>[Err] ERROR: cannot extract field from a non-object
> >>>I can understand the reason behind this error(JSON array don't has fields),
> >>>but for me it is very logical postgres to exclude this row from the
> >>>returning set, rather to throw an error.
> >>Hm. In principle we could allow ->> to return NULL rather than failing
> >>when there's no such field, but I'm not sure that would represent good
> >>language design. However, this example definitely shows there are some
> >>holes in the current set of JSON manipulation functions. The only way
> >>to avoid a failure here would be to write something like
> >>
> >> WHERE (CASE WHEN json_has_field(data, 'in_stock') THEN
> >> (data->>'in_stock')::integer ELSE NULL::integer END) > 0
> >>
> >>but there is no "json_has_field" test function, nor any nice way to
> >>build one from the provided functions.
> >>
> >>It's probably too late to address this for 9.3, but we ought to put it
> >>on the to-do list for 9.4.
> >Was this addressed for 9.4 because I don't see it?
> >
>
> 9.4 does have json_typeof(), which should let you construct an
> adequate test along the lines Tom suggests.
Ah, I didn't catch that. Thank you!
--
Bruce Momjian <bruce@momjian.us> http://momjian.us
EnterpriseDB http://enterprisedb.com
+ Everyone has their own god. +
--
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] 5+ messages in thread
end of thread, other threads:[~2014-01-24 16:34 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2013-06-09 15:12 BUG #8218: Error when querying an JSON data, 9.3beta daniel.zlatev@gmail.com
2013-06-09 17:24 ` Tom Lane <tgl@sss.pgh.pa.us>
2014-01-24 03:05 ` Bruce Momjian <bruce@momjian.us>
2014-01-24 04:32 ` Andrew Dunstan <andrew@dunslane.net>
2014-01-24 16:34 ` Bruce Momjian <bruce@momjian.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