Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.84_2) (envelope-from ) id 1e6NCB-00043I-06 for pgsql-bugs@arkaria.postgresql.org; Sun, 22 Oct 2017 20:50:07 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84_2) (envelope-from ) id 1e6NCA-0001zH-Is for pgsql-bugs@arkaria.postgresql.org; Sun, 22 Oct 2017 20:50:06 +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 1e6NC9-0001uF-VI for pgsql-bugs@postgresql.org; Sun, 22 Oct 2017 20:50:06 +0000 Received: from mail-yw0-x230.google.com ([2607:f8b0:4002:c05::230]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84_2) (envelope-from ) id 1e6NC6-00078u-Sv for pgsql-bugs@postgresql.org; Sun, 22 Oct 2017 20:50:04 +0000 Received: by mail-yw0-x230.google.com with SMTP id k11so10445579ywh.1 for ; Sun, 22 Oct 2017 13:50:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=2ndquadrant-com.20150623.gappssmtp.com; s=20150623; h=from:subject:to:cc:references:message-id:date:user-agent :mime-version:in-reply-to:content-language; bh=buuI0T3tO4FS7BLQdMvE5EiKPWTBQhAAr9rsnNt1GFU=; b=QEsk1hLNcS9snoW/KP5PCzTZIwqSq4o0WNB6Jqfhea1PLOXsplZ6FV2Cxfvwpqpa67 Ut9XkLOj9zlCCxX1EkT8SO/VVsFHHS8QO410ItZnL3IbBTzZQbV8qXQtkXigwpGDeSvA /0EZWTOjYOxID78MnwhFhFTP8xDLloc5T6X57Gny6L9emAWXuQzSozBXNluEE9gBqkXn pgcIRQVHfZTC7Yz+W2onVO3fXFRVOInMwZGKw21PN3QN3JnxENerzan/Qqdn4QHGGlj7 XuYUl20QRAWENEG/13OrI2MAfCckVhoPXok4DUzYkh86AfCEInwqtmcIV4vo21w5IgRa hlsw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:subject:to:cc:references:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=buuI0T3tO4FS7BLQdMvE5EiKPWTBQhAAr9rsnNt1GFU=; b=jihoQt4dmdIO2ZNQ62hhKtXlTMydsAIrQYxA1Q9LVACndPBW0VevXz7M1qdOU+DRzB 8uRz7SQ6rC1LX3GdttAkmCTvV0mWlVznXRnTr0EpkAK9zcFmiyBighr1K4D3BIl0m9xv 7L5c8ThM1/vYZVggisnvPI7Cxnr5mrU5HVSjWQ4DV6OA8HPzP5MAGkRNuK++i5VsRBza 9/IBpG5jAkwZ9TEyam0WN3ySQDBd3lEsd25FbA/ddG3uf9uJRCBih9Kz+Hu2tUFbJsOe DVh+RyBk2dbjo7ClXsreqJ3aZtYPfv9iqZTDPj/CVpcZ5xz6OiCqMkVgir925kWw33WE y/0A== X-Gm-Message-State: AMCzsaWKxI4d6TSFu0DDHtgmXpO3/vhZJlpMODie6I/+QvEL7fiHeZHV OlEPhCppEGAnLR4YcMGWt/iO X-Google-Smtp-Source: ABhQp+QlgMUpTRJW+zpjv+b3QrEpUxHXkbcttz+Wm1ng5ar74MN0ON5xY0UM9DMi8nLhUTcLTwoFdw== X-Received: by 10.129.114.10 with SMTP id n10mr7851691ywc.54.1508705401713; Sun, 22 Oct 2017 13:50:01 -0700 (PDT) Received: from [192.168.10.146] ([98.122.175.38]) by smtp.gmail.com with ESMTPSA id i133sm2949668ywc.77.2017.10.22.13.50.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Sun, 22 Oct 2017 13:50:01 -0700 (PDT) From: Andrew Dunstan X-Google-Original-From: Andrew Dunstan Subject: Re: BUG #14849: jsonb_build_object doesn't like VARIADIC calls very much To: Michael Paquier Cc: Tom Lane , Dmitry Dolgov <9erthalion6@gmail.com>, Marko Tiikkaja , PostgreSQL mailing lists , PostgreSQL-development References: <20171011020058.27986.42308@wrigleys.postgresql.org> <18157.1508600622@sss.pgh.pa.us> <20566.1508604219@sss.pgh.pa.us> Message-ID: Date: Sun, 22 Oct 2017 16:50:00 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: multipart/mixed; boundary="------------F83D743D9974766F9A18170D" Content-Language: en-MW List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org This is a multi-part message in MIME format. --------------F83D743D9974766F9A18170D Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit On 10/22/2017 04:35 PM, Michael Paquier wrote: > On Mon, Oct 23, 2017 at 1:44 AM, Andrew Dunstan > wrote: > >> here's a patch that works that way, based on Michael's code. > Patch not attached :) > I still have a patch half-cooked, that I can send if necessary, but > you are on it, right? Sorry. Here it is. cheers andrew -- Andrew Dunstan https://www.2ndQuadrant.com PostgreSQL Development, 24x7 Support, Remote DBA, Training & Services --------------F83D743D9974766F9A18170D Content-Type: text/x-patch; name="extract_variadic_args.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="extract_variadic_args.patch" diff --git a/src/backend/utils/fmgr/funcapi.c b/src/backend/utils/fmgr/funcapi.c index 9c3f451..dfd5d8c 100644 --- a/src/backend/utils/fmgr/funcapi.c +++ b/src/backend/utils/fmgr/funcapi.c @@ -1400,3 +1400,120 @@ TypeGetTupleDesc(Oid typeoid, List *colaliases) return tupdesc; } + +/* + * Extract a set of variadic argument values, types and NULL markers. This is + * useful for abstracting away whether or not the call has been done + * with an explicit VARIADIC array or a normal set of arguments, for a + * VARIADIC "any" function. When doing a VARIADIC call, the caller has + * provided one argument made of an array of keys, so deconstruct the + * array data before using it for the next processing. + * If no explicit VARIADIC call is used, just fill in the data based on + * all the arguments given by the caller. + * This function returns the number of arguments generated. In the event + * where the caller provided a NULL array input, return -1. + * nfixed is the number of non-variadic arguments to the function - these + * will be ignored by this function. + * If required by the caller, values of type "unknown" will be converted + * to text datums. + */ +int +extract_variadic_args(FunctionCallInfo fcinfo, Datum **args, + Oid **types, bool **nulls, int nfixed, + bool convert_unknown_to_text) +{ + bool variadic = get_fn_expr_variadic(fcinfo->flinfo); + Datum *args_res; + bool *nulls_res; + Oid *types_res; + int nargs, i; + + *args = NULL; + *types = NULL; + *nulls = NULL; + + if (variadic) + { + ArrayType *array_in; + Oid element_type; + bool typbyval; + char typalign; + int16 typlen; + + Assert(PG_NARGS() == nfixed + 1); + + if (PG_ARGISNULL(nfixed)) + return -1; + + array_in = PG_GETARG_ARRAYTYPE_P(nfixed); + element_type = ARR_ELEMTYPE(array_in); + + get_typlenbyvalalign(element_type, + &typlen, &typbyval, &typalign); + deconstruct_array(array_in, element_type, typlen, typbyval, + typalign, &args_res, &nulls_res, + &nargs); + + /* All the elements of the array have the same type */ + types_res = (Oid *) palloc0(nargs * sizeof(Oid)); + for (i = 0; i < nargs; i++) + types_res[i] = element_type; + } + else + { + nargs = PG_NARGS() - nfixed; + Assert (nargs > 0); + nulls_res = (bool *) palloc0(nargs * sizeof(bool)); + args_res = (Datum *) palloc0(nargs * sizeof(Datum)); + types_res = (Oid *) palloc0(nargs * sizeof(Oid)); + + for (i = 0; i < nargs; i++) + { + nulls_res[i] = PG_ARGISNULL(i + nfixed); + types_res[i] = get_fn_expr_argtype(fcinfo->flinfo, i + nfixed); + + if (!OidIsValid(types_res[i])) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("could not determine data type for argument %d", + i + nfixed + 1))); + + /* + * Turn a constant (more or less literal) value that's of unknown + * type into text if required . Unknowns come in as a cstring + * pointer. + */ + if (convert_unknown_to_text && types_res[i] == UNKNOWNOID && + get_fn_expr_arg_stable(fcinfo->flinfo, i + nfixed)) + { + types_res[i] = TEXTOID; + + /* important for value, key cannot being NULL */ + if (PG_ARGISNULL(i + nfixed)) + args_res[i] = (Datum) 0; + else + args_res[i] = + CStringGetTextDatum(PG_GETARG_POINTER(i + nfixed)); + } + else + { + /* no conversion needed, just take the datum as given */ + args_res[i] = PG_GETARG_DATUM(i + nfixed); + } + + if (!OidIsValid(types_res[i]) || + (convert_unknown_to_text && (types_res[i] == UNKNOWNOID))) + ereport(ERROR, + (errcode(ERRCODE_INVALID_PARAMETER_VALUE), + errmsg("could not determine data type for argument %d", + i + nfixed + 1))); + } + } + + /* Fill in results */ + *args = args_res; + *nulls = nulls_res; + *types = types_res; + + return nargs; +} diff --git a/src/include/funcapi.h b/src/include/funcapi.h index 951af2a..0550c07 100644 --- a/src/include/funcapi.h +++ b/src/include/funcapi.h @@ -281,6 +281,9 @@ extern TupleTableSlot *TupleDescGetSlot(TupleDesc tupdesc); extern FuncCallContext *init_MultiFuncCall(PG_FUNCTION_ARGS); extern FuncCallContext *per_MultiFuncCall(PG_FUNCTION_ARGS); extern void end_MultiFuncCall(PG_FUNCTION_ARGS, FuncCallContext *funcctx); +extern int extract_variadic_args(FunctionCallInfo fcinfo, Datum **args, + Oid **types, bool **nulls, int nfixed, + bool convert_unknown_to_text); #define SRF_IS_FIRSTCALL() (fcinfo->flinfo->fn_extra == NULL) --------------F83D743D9974766F9A18170D Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --------------F83D743D9974766F9A18170D--