pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Adrian Klaver <adrian.klaver@aklaver.com>
To: Alexander Farber <alexander.farber@gmail.com>
Cc: pgsql-general <pgsql-general@postgresql.org>
Subject: Re: jsonb_array_length: ERROR: 22023: cannot get array length of a scalar
Date: Fri, 2 Mar 2018 10:52:32 -0800
Message-ID: <5a8262d5-ba14-bff8-fe92-4958d11f0d92@aklaver.com> (raw)
In-Reply-To: <CAADeyWh0+ATf+Qw4YN_6479MCsvGEMnqrG7dHyfY9esWdCc3OA@mail.gmail.com>
References: <CAADeyWhfBuFiWz0Au0J2XyU5ZTDCBionqMGHqL657xseWq913g@mail.gmail.com>
	<0325b36d-f990-deb9-3086-0fb626c95e14@aklaver.com>
	<CAADeyWhx0B_z4at-X6sMqz+m6sr28U4kaxmKFcqphYYV7UuAuw@mail.gmail.com>
	<39bf9413-1465-9880-24e0-ee8d1c84e8a0@aklaver.com>
	<CAADeyWi25Ts_3q8d62CAHvBW8TdsROe-jOVQGSDPB-8qcUXF7g@mail.gmail.com>
	<100e4ceb-a799-3e63-53d6-4c497dfb9978@aklaver.com>
	<CAKFQuwb2b7y3OrPCWxOqEm4u4my4VkvXUx3reNo7OuP==7ELHA@mail.gmail.com>
	<796da8b2-f538-a753-f3ed-ad69f3374308@aklaver.com>
	<CAADeyWiKK7OX2JrLvBDm1WxEZJgGUmQt-bgwnJjqGcvcu3t=HA@mail.gmail.com>
	<CAADeyWh0+ATf+Qw4YN_6479MCsvGEMnqrG7dHyfY9esWdCc3OA@mail.gmail.com>

On 03/02/2018 10:43 AM, Alexander Farber wrote:
> # select * from words_moves where gid=609 limit 3;
> -[ RECORD 1 
> ]----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> mid    | 1040
> action | play
> gid    | 609
> uid    | 1192
> played | 2018-03-02 10:13:57.943876+01
> tiles  | [{"col": 3, "row": 7, "value": 2, "letter": "С"}, {"col": 4, 
> "row": 7, "value": 1, "letter": "О"}, {"col": 5, "row": 7, "value": 2, 
> "letter": "П"}, {"col": 6, "row": 7, "value": 0, "letter": "Л"}, {"col": 
> 7, "row": 7, "value": 3, "letter": "Я"}]
> score  | 10
> -[ RECORD 2 
> ]----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> mid    | 1041
> action | play
> gid    | 609
> uid    | 7
> played | 2018-03-02 10:56:58.72503+01
> tiles  | [{"col": 3, "row": 8, "value": 2, "letter": "В"}, {"col": 3, 
> "row": 9, "value": 1, "letter": "И"}, {"col": 3, "row": 10, "value": 2, 
> "letter": "Т"}, {"col": 3, "row": 11, "value": 2, "letter": "К"}, 
> {"col": 3, "row": 12, "value": 1, "letter": "А"}]
> score  | 14
> -[ RECORD 3 
> ]----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
> mid    | 1043
> action | play
> gid    | 609
> uid    | 1192
> played | 2018-03-02 11:03:58.614094+01
> tiles  | [{"col": 0, "row": 10, "value": 2, "letter": "С"}, {"col": 1, 
> "row": 10, "value": 2, "letter": "М"}, {"col": 2, "row": 10, "value": 1, 
> "letter": "О"}, {"col": 4, "row": 10, "value": 2, "letter": "Р"}]
> score  | 13
> 
> #  select column_name, data_type from information_schema.columns where 
> table_name='words_moves';
>   column_name |        data_type
> -------------+--------------------------
>   mid         | bigint
>   action      | text
>   gid         | integer
>   uid         | integer
>   played      | timestamp with time zone
>   tiles       | jsonb
>   score       | integer
> (7 rows)
> 
> #  select jsonb_array_length(tiles) from words_moves where gid=609 limit 3;
>   jsonb_array_length
> --------------------
>                    5
>                    5
>                    4
> (3 rows)
> 
> BUT:
> 
> #  select jsonb_array_length(tiles) from words_moves where gid=609 ;
> ERROR:  22023: cannot get array length of a scalar
> LOCATION:  jsonb_array_length, jsonfuncs.c:1579
> 
> 
> Which means only some data is bad, but how to find it please?

What is? :

select count(*) from words_moves where gid=609;

A simplistic approach would be:

select mid, jsonb_array_length(tiles) from words_moves where gid=609 
order by mid limit x;

where you  increment x until you trigger the error.

A more sophisticated approach would be to use plpgsql EXCEPTION handling:

https://www.postgresql.org/docs/10/static/plpgsql-control-structures.html#PLPGSQL-ERROR-TRAPPING

and create a function that loops through:

select jsonb_array_length(tiles) from words_moves where gid=609 ;

and RAISES a NOTICE for each incorrect value along with its mid value.



> 
> 
> 


-- 
Adrian Klaver
adrian.klaver@aklaver.com




view thread (18+ messages)  latest in thread

Message-ID: <5a8262d5-ba14-bff8-fe92-4958d11f0d92@aklaver.com>
Permalink:  ../5a8262d5-ba14-bff8-fe92-4958d11f0d92@aklaver.com/
Also on:    postgresql.org/message-id/5a8262d5-ba14-bff8-fe92-4958d11f0d92@aklaver.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: adrian.klaver@aklaver.com, alexander.farber@gmail.com
  Subject: Re: jsonb_array_length: ERROR: 22023: cannot get array length of a scalar
  In-Reply-To: <5a8262d5-ba14-bff8-fe92-4958d11f0d92@aklaver.com>
  References: <CAADeyWhfBuFiWz0Au0J2XyU5ZTDCBionqMGHqL657xseWq913g@mail.gmail.com> <0325b36d-f990-deb9-3086-0fb626c95e14@aklaver.com> <CAADeyWhx0B_z4at-X6sMqz+m6sr28U4kaxmKFcqphYYV7UuAuw@mail.gmail.com> <39bf9413-1465-9880-24e0-ee8d1c84e8a0@aklaver.com> <CAADeyWi25Ts_3q8d62CAHvBW8TdsROe-jOVQGSDPB-8qcUXF7g@mail.gmail.com> <100e4ceb-a799-3e63-53d6-4c497dfb9978@aklaver.com> <CAKFQuwb2b7y3OrPCWxOqEm4u4my4VkvXUx3reNo7OuP==7ELHA@mail.gmail.com> <796da8b2-f538-a753-f3ed-ad69f3374308@aklaver.com> <CAADeyWiKK7OX2JrLvBDm1WxEZJgGUmQt-bgwnJjqGcvcu3t=HA@mail.gmail.com> <CAADeyWh0+ATf+Qw4YN_6479MCsvGEMnqrG7dHyfY9esWdCc3OA@mail.gmail.com> <5a8262d5-ba14-bff8-fe92-4958d11f0d92@aklaver.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

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