Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cYyJf-0000J2-Mv for pgsql-hackers@arkaria.postgresql.org; Wed, 01 Feb 2017 17:03:32 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1cYyJf-0005pH-9w for pgsql-hackers@arkaria.postgresql.org; Wed, 01 Feb 2017 17:03:31 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1cYxmZ-0008SN-73 for pgsql-hackers@postgresql.org; Wed, 01 Feb 2017 16:29:19 +0000 Received: from mail.postgrespro.ru ([93.174.131.138]) by makus.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cYxmR-0005xt-Vm for pgsql-hackers@postgresql.org; Wed, 01 Feb 2017 16:29:18 +0000 Received: from localhost (localhost [127.0.0.1]) by mail.postgrespro.ru (Postfix) with ESMTP id A059F21C2A87; Wed, 1 Feb 2017 19:29:09 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at postgrespro.ru Received: from [10.100.236.30] (unknown [195.82.154.27]) by mail.postgrespro.ru (Postfix) with ESMTPSA id 5C03D21C2A43; Wed, 1 Feb 2017 19:29:09 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1485966549; bh=BxxtdHS6rNSp78CQkQvvpepRVaQfgjQyTDMTyzcf8Xc=; h=In-Reply-To:References:To:Cc:From:Subject:Date; b=jSIS/zw/yiPuTKWdW6ojmlQxM/gvre0SsP0BGw9ANk30HCW1kTTmuVMEhRFUVhTfR kCmbB6XNPPVDZNdKpuRNkLrONfM2qJ8tNK6Rk6tLAYM20dzIOnKnjuomlUrUJNqp0p PHhMxAA/RbY9N/fh6ImtOoRMhNp3yuznHB79wj1Q= In-Reply-To: <0154d35a-24ae-f063-5273-9ffcdf1c7f2e@postgrespro.ru> References: <0154d35a-24ae-f063-5273-9ffcdf1c7f2e@postgrespro.ru> To: a.lubennikova@postgrespro.ru Cc: PostgreSQL mailing lists From: Nikita Glukhov Subject: Re: Cast jsonb to numeric, int, float, bool Organization: Postgres Professional Message-ID: <55137679-fd6b-baf0-bfbf-6558ce5cb409@postgrespro.ru> Date: Wed, 1 Feb 2017 19:26:33 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.7.0 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Pg-Spam-Score: -2.0 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-hackers Precedence: bulk Sender: pgsql-hackers-owner@postgresql.org On 01.02.2017 14:21,Anastasia Lubennikova wrote: > Now the simplest way to extract booleans and numbers from json/jsonb is > to cast it to text and then cast to the appropriate type: ... > This patch implements direct casts from jsonb numeric (jbvNumeric) to > numeric, int4 and float8, and from jsonb bool (jbvBool) to bool. Thank you for this patch. I always wanted to add such casts by myself. > If you find it useful, I can also add support of json and other types, > such as smallint and bigint. Yes, I'd like to have support for other types and maybe for json. Some comments about the code: I think it would be better to * add function for extraction of scalars from pseudo-arrays * iterate until WJB_DONE to pfree iterator Example: static bool JsonbGetScalar(Jsonb *jb, JsonbValue *v) { JsonbIterator *it; JsonbIteratorToken tok; JsonbValue jbv; if (!JB_ROOT_IS_SCALAR(jb)) return false; /* * A root scalar is stored as an array of one element, so we get the * array and then its first (and only) member. */ it = JsonbIteratorInit(&jb->root); tok = JsonbIteratorNext(&it, &jbv, true); Assert(tok == WJB_BEGIN_ARRAY); tok = JsonbIteratorNext(&it, v, true); Assert(tok == WJB_ELEM); tok = JsonbIteratorNext(&it, &jbv, true); Assert(tok == WJB_END_ARRAY); tok = JsonbIteratorNext(&it, &jbv, true); Assert(tok == WJB_DONE); return true; } Datum jsonb_int4(PG_FUNCTION_ARGS) { Jsonb *in = PG_GETARG_JSONB(0); JsonbValue v; if (!JsonbGetScalar(in, &v) || v.type != jbvNumeric) ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("key value must be json numeric"))); PG_RETURN_INT32(DatumGetInt32(DirectFunctionCall1(numeric_int4, NumericGetDatum(v.val.numeric)))); } -- Nikita Glukhov Postgres Professional:http://www.postgrespro.com The Russian Postgres Company -- Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-hackers