pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Alvaro Herrera <alvherre@2ndquadrant.com>
To: ua.san.alex@gmail.com
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #11768: "jsonb ?" -  not support int type operator
Date: Wed, 17 Dec 2014 11:18:36 -0300
Message-ID: <20141217141836.GC1768@alvh.no-ip.org> (raw)
In-Reply-To: <20141022235702.2662.82328@wrigleys.postgresql.org>
References: <20141022235702.2662.82328@wrigleys.postgresql.org>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

ua.san.alex@gmail.com wrote:
> The following bug has been logged on the website:
> 
> Bug reference:      11768
> Logged by:          Alex
> Email address:      ua.san.alex@gmail.com
> PostgreSQL version: 9.4beta2
> Operating system:   CentOS 7
> Description:        
> 
> Error - SELECT '[2, 5, 9]'::jsonb ? 5
> 
> Support for numeric types is very important, without it, it is not
> convenient.

Not sure that would do what you want anyway:

alvherre=# select '[2,5,9]'::jsonb ? '5';
 ?column? 
----------
 f
(1 fila)

It only matches an element if it's of type string, as can be seen here:

alvherre=# select '[2,"5",9]'::jsonb ? '5';
 ?column? 
----------
 t
(1 fila)


I think primarily this operator was intended to look for keys in an
object (which is why it's called "exists"), which cannot be strings;
extending it to arrays might have been an afterthought.  Example:

alvherre=# select '{"5":"cinco"}'::jsonb ? '5';
 ?column? 
----------
 t
(1 fila)

For definitional reasons, JSON keys cannot be numbers:

alvherre=# select '{5:"cinco"}'::jsonb ;
ERROR:  invalid input syntax for type json
LÍNEA 1: select '{5:"cinco"}'::jsonb ;
                ^
DETALLE:  Expected string or "}", but found "5".
CONTEXTO:  JSON data, line 1: {5...


I think you might be looking for the "contains" operator:

alvherre=# select '[2,5,9]'::jsonb @> '5';
 ?column? 
----------
 t
(1 fila)

This one only accepts a jsonb as right argument, so it won't work with a
bare integer either:

alvherre=# \do @>
                              Listado de operadores
  Esquema   | Nombre | Tipo arg izq | Tipo arg der | Tipo resultado | Descripción 
------------+--------+--------------+--------------+----------------+-------------
...
 pg_catalog | @>     | jsonb        | jsonb        | boolean        | contains
...
(13 filas)


-- 
Álvaro Herrera                http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services


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



view thread (2+ messages)

Message-ID: <20141217141836.GC1768@alvh.no-ip.org>
Permalink:  ../20141217141836.GC1768@alvh.no-ip.org/
Also on:    postgresql.org/message-id/20141217141836.GC1768@alvh.no-ip.org

 · 

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-bugs@postgresql.org
  Cc: alvherre@2ndquadrant.com, ua.san.alex@gmail.com
  Subject: Re: BUG #11768: "jsonb ?" -  not support int type operator
  In-Reply-To: <20141217141836.GC1768@alvh.no-ip.org>
  References: <20141022235702.2662.82328@wrigleys.postgresql.org> <20141217141836.GC1768@alvh.no-ip.org>

* 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